Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.notify.composite; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.bean.Property; import com.clustercontrol.notify.action.GetNotify; import com.clustercontrol.notify.action.GetNotifyTableDefineCheckBox; import com.clustercontrol.notify.action.GetNotifyTableDefineNoCheckBox; import com.clustercontrol.notify.composite.action.NotifyDoubleClickListener; import com.clustercontrol.util.Messages; import com.clustercontrol.viewer.CommonTableViewer; import com.clustercontrol.ws.notify.InvalidRole_Exception; import com.clustercontrol.ws.notify.NotifyInfo; import com.clustercontrol.ws.notify.NotifyRelationInfo; /** * ?<BR> * * * @version 3.0.0 * @since 2.0.0 */ public class NotifyListComposite extends Composite { private static Log m_log = LogFactory.getLog(NotifyListComposite.class); /** */ private CommonTableViewer tableViewer = null; /***/ private boolean isSelect = false; /** ? */ private Label totalLabel = null; /** ? */ private Property condition = null; /**/ private List<NotifyRelationInfo> notify; /** ??? */ private String managerName = null; // ID private String ownerRoleId = null; // private boolean showFlg = true; /** * ??? * <p> * ?????????? * * @param parent ?? * @param style * * @see org.eclipse.swt.SWT * @see org.eclipse.swt.widgets.Composite#Composite(Composite parent, int style) * @see #initialize() */ public NotifyListComposite(Composite parent, int style, boolean isSelect, String ownerRoleId) { super(parent, style); this.isSelect = isSelect; this.ownerRoleId = ownerRoleId; this.initialize(); } /** * ????? * * @see com.clustercontrol.notify.action.GetNotifyTableDefineCheckBox#get() * @see #update() */ private void initialize() { GridLayout layout = new GridLayout(1, true); this.setLayout(layout); layout.marginHeight = 0; layout.marginWidth = 0; final Table table = new Table(this, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); WidgetTestUtil.setTestId(this, null, table); table.setHeaderVisible(true); table.setLinesVisible(true); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; table.setLayoutData(gridData); // ?? this.tableViewer = new CommonTableViewer(table); if (this.isSelect) { this.tableViewer.createTableColumn(GetNotifyTableDefineCheckBox.get(), GetNotifyTableDefineCheckBox.SORT_COLUMN_INDEX, GetNotifyTableDefineCheckBox.SORT_ORDER); } else { this.tableViewer.createTableColumn(GetNotifyTableDefineNoCheckBox.get(), GetNotifyTableDefineNoCheckBox.SORT_COLUMN_INDEX1, GetNotifyTableDefineNoCheckBox.SORT_COLUMN_INDEX2, GetNotifyTableDefineNoCheckBox.SORT_ORDER); // ??????????????? for (int i = 0; i < table.getColumnCount(); i++) { table.getColumn(i).setMoveable(true); } // ? this.tableViewer.addDoubleClickListener(new NotifyDoubleClickListener(this)); } if (this.isSelect) { /**???*/ table.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { //???TableColumn???? TableItem[] ti = table.getSelection(); for (int i = 0; i < ti.length; i++) { @SuppressWarnings("unchecked") ArrayList<Object> al = (ArrayList<Object>) ti[i].getData(); WidgetTestUtil.setTestId(this, "tableitem" + i, ti[i]); if ((Boolean) al.get(0)) { //YES?NO al.set(GetNotifyTableDefineCheckBox.SELECTION, false); } else { //NO?YES al.set(GetNotifyTableDefineCheckBox.SELECTION, true); } } //???????? tableViewer.refresh(); } }); } else { // ??? this.totalLabel = new Label(this, SWT.RIGHT); WidgetTestUtil.setTestId(this, "total", totalLabel); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; this.totalLabel.setLayoutData(gridData); } } /** * ???????? * * @return */ public CommonTableViewer getTableViewer() { return this.tableViewer; } /** * ???????? * * @return */ public Table getTable() { return this.tableViewer.getTable(); } /** * ????<BR> * ??????? * * @see com.clustercontrol.notify.action.GetNotify#getNotifyList() */ @Override public void update() { // ??? Map<String, List<NotifyInfo>> dispDataMap = null; ArrayList<ArrayList<Object>> listInput = new ArrayList<ArrayList<Object>>(); if (this.isSelect) { if (this.ownerRoleId == null || this.ownerRoleId.equals("")) { m_log.info("ownerRole=" + ownerRoleId); return; } try { dispDataMap = new GetNotify().getNotifyListByOwnerRole(this.managerName, this.ownerRoleId); } catch (InvalidRole_Exception e) { // ??????? setShowFlg(false); return; } } else { dispDataMap = new GetNotify().getNotifyList(this.managerName); } for (Map.Entry<String, List<NotifyInfo>> entrySet : dispDataMap.entrySet()) { List<NotifyInfo> list = null; list = entrySet.getValue(); if (list == null) { //???????? list = new ArrayList<NotifyInfo>(); } for (NotifyInfo info : list) { ArrayList<Object> a = new ArrayList<Object>(); if (this.isSelect) { a.add(false); //a.add(entrySet.getKey()); a.add(info.isValidFlg()); a.add(info.getNotifyId()); a.add(info.getDescription()); a.add(info.getNotifyType()); } else { a.add(entrySet.getKey()); a.add(info.getNotifyId()); a.add(info.getDescription()); a.add(info.getNotifyType()); a.add(info.isValidFlg()); } a.add(info.getOwnerRoleId()); a.add(info.getCalendarId()); a.add(info.getRegUser()); a.add(info.getRegDate() == null ? null : new Date(info.getRegDate())); a.add(info.getUpdateUser()); a.add(info.getUpdateDate() == null ? null : new Date(info.getUpdateDate())); a.add(null); listInput.add(a); } if (notify == null) { //???????notify?? notify = new ArrayList<NotifyRelationInfo>(); } if (isSelect) { //????????????? String tableNotifyId; boolean flg = false; //?????? Iterator<NotifyRelationInfo> it = notify.iterator(); NotifyRelationInfo nri = null; ArrayList<NotifyRelationInfo> removeNotifyList = new ArrayList<NotifyRelationInfo>(); while (it.hasNext()) { nri = it.next(); nri.setNotifyGroupId(null); flg = false; for (int i = 0; i < list.size(); i++) { NotifyInfo info = list.get(i); ArrayList<Object> objList = listInput.get(i); tableNotifyId = info.getNotifyId(); if (tableNotifyId.equals(nri.getNotifyId())) { objList.set(GetNotifyTableDefineCheckBox.SELECTION, true); flg = true; } objList.set(GetNotifyTableDefineCheckBox.VALID_FLG, info.isValidFlg()); objList.set(GetNotifyTableDefineCheckBox.NOTIFY_ID, info.getNotifyId()); objList.set(GetNotifyTableDefineCheckBox.DESCRIPTION, info.getDescription()); objList.set(GetNotifyTableDefineCheckBox.NOTIFY_TYPE, info.getNotifyType()); objList.set(GetNotifyTableDefineCheckBox.OWNER_ROLE, info.getOwnerRoleId()); objList.set(GetNotifyTableDefineCheckBox.CALENDAR_ID, info.getCalendarId()); objList.set(GetNotifyTableDefineCheckBox.CREATE_USER, info.getRegUser()); objList.set(GetNotifyTableDefineCheckBox.CREATE_TIME, new Date(info.getRegDate())); objList.set(GetNotifyTableDefineCheckBox.UPDATE_USER, info.getUpdateUser()); objList.set(GetNotifyTableDefineCheckBox.UPDATE_TIME, new Date(info.getUpdateDate())); objList.set(GetNotifyTableDefineCheckBox.DUMMY, null); listInput.set(i, objList); } if (!flg) { //??????? removeNotifyList.add(nri); } } notify.removeAll(removeNotifyList); } else { // ? String[] args = { String.valueOf(listInput.size()) }; String message = null; if (this.condition == null) { message = Messages.getString("records", args); } else { message = Messages.getString("filtered.records", args); } this.totalLabel.setText(message); } } // this.tableViewer.setInput(listInput); } /** * ??????? * * @param notify */ public void setSelectNotify(List<NotifyRelationInfo> notify) { if (notify != null) { this.notify = new ArrayList<NotifyRelationInfo>(); } this.notify = notify; } /** * ????? * * @return ? */ public List<NotifyRelationInfo> getSelectNotify() { return notify; } /** * OK????????? * ???? * */ public boolean makeNotifyData() { TableItem[] ti = this.tableViewer.getTable().getItems(); ArrayList<?> al; Collection<NotifyRelationInfo> ct = new ArrayList<NotifyRelationInfo>(); ; if (this.isSelect) { for (int i = 0; i < ti.length; i++) { al = (ArrayList<?>) ti[i].getData(); WidgetTestUtil.setTestId(this, "tableitem" + i, ti[i]); if ((Boolean) al.get(GetNotifyTableDefineCheckBox.SELECTION)) { NotifyRelationInfo nri = new NotifyRelationInfo(); nri.setNotifyGroupId(null); nri.setNotifyId((String) al.get(GetNotifyTableDefineCheckBox.NOTIFY_ID)); nri.setNotifyType((Integer) al.get(GetNotifyTableDefineCheckBox.NOTIFY_TYPE)); ct.add(nri); } } } else { for (int i = 0; i < ti.length; i++) { al = (ArrayList<?>) ti[i].getData(); } } // ? if (notify != null) { notify.clear(); notify.addAll(ct); } return true; } /** * ????ID?? * * @return */ public ArrayList<String> getSelectionData() { ArrayList<String> data = new ArrayList<String>(); //???? StructuredSelection selection = (StructuredSelection) tableViewer.getSelection(); List<?> list = selection.toList(); if (list != null) { for (int index = 0; index < list.size(); index++) { ArrayList<?> info = (ArrayList<?>) list.get(index); if (info != null && info.size() > 0) { String notifyId = (String) info.get(GetNotifyTableDefineCheckBox.NOTIFY_ID); data.add(notifyId); } } } return data; } public boolean isShowFlg() { return showFlg; } private void setShowFlg(boolean showFlg) { this.showFlg = showFlg; } /** * @return the managerName */ public String getManagerName() { return managerName; } /** * @param managerName the managerName to set */ public void setManagerName(String managerName) { this.managerName = managerName; } }