Java tutorial
/* Copyright (C) 2016 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.jobmanagement.composite; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; 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.layout.RowData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import com.clustercontrol.bean.SizeConstant; import com.clustercontrol.jobmanagement.action.GetJobKickParameterTableDefine; import com.clustercontrol.jobmanagement.dialog.RuntimeParameterDialog; import com.clustercontrol.jobmanagement.util.JobDialogUtil; import com.clustercontrol.util.Messages; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.viewer.CommonTableViewer; import com.clustercontrol.ws.jobmanagement.JobRuntimeParam; /** * ???? * * @version 5.1.0 */ public class JobKickParamComposite extends Composite { /** */ private Shell m_shell = null; /** */ private CommonTableViewer m_viewer = null; /** */ private Button m_btnAdd = null; /** */ private Button m_btnModify = null; /** */ private Button m_btnDelete = null; /** */ private Map<String, JobRuntimeParam> m_jobRuntimeParamMap = new HashMap<>(); /** * * * @param parent ?? * @param style * * @see org.eclipse.swt.SWT * @see org.eclipse.swt.widgets.Composite#Composite(Composite parent, int style) * @see #initialize() */ public JobKickParamComposite(Composite parent, int style) { super(parent, style); initialize(); } /** * ???? */ private void initialize() { m_shell = this.getShell(); this.setLayout(JobDialogUtil.getParentLayout()); // Composite Composite composite = new Composite(this, SWT.NONE); composite.setLayout(new GridLayout(4, false)); composite.setLayoutData(new RowData()); ((RowData) composite.getLayoutData()).width = 525; // Table table = new Table(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI); WidgetTestUtil.setTestId(this, "table", table); table.setHeaderVisible(true); table.setLinesVisible(true); table.setLayoutData(new GridData(465, 150)); ((GridData) table.getLayoutData()).horizontalSpan = 4; // dummy new Label(composite, SWT.NONE).setLayoutData(new GridData(325, SizeConstant.SIZE_LABEL_HEIGHT)); // this.m_btnAdd = new Button(composite, SWT.NONE); WidgetTestUtil.setTestId(this, "m_btnAdd", this.m_btnAdd); this.m_btnAdd.setText(Messages.getString("add")); this.m_btnAdd.setLayoutData(new GridData(50, SizeConstant.SIZE_BUTTON_HEIGHT)); this.m_btnAdd.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { RuntimeParameterDialog dialog = new RuntimeParameterDialog(m_shell, m_jobRuntimeParamMap); if (dialog.open() == IDialogConstants.OK_ID) { m_jobRuntimeParamMap.put(dialog.getInputData().getParamId(), dialog.getInputData()); reflectParamInfo(); } } }); // this.m_btnModify = new Button(composite, SWT.NONE); WidgetTestUtil.setTestId(this, "m_btnModify", this.m_btnModify); this.m_btnModify.setText(Messages.getString("modify")); this.m_btnModify.setLayoutData(new GridData(50, SizeConstant.SIZE_BUTTON_HEIGHT)); this.m_btnModify.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { int order = m_viewer.getTable().getSelectionIndex(); if (order >= 0) { String paramId = (String) ((ArrayList<?>) m_viewer.getTable().getSelection()[0].getData()) .get(0); RuntimeParameterDialog dialog = new RuntimeParameterDialog(m_shell, m_jobRuntimeParamMap, m_jobRuntimeParamMap.get(paramId)); if (dialog.open() == IDialogConstants.OK_ID) { m_jobRuntimeParamMap.remove(paramId); m_jobRuntimeParamMap.put(dialog.getInputData().getParamId(), dialog.getInputData()); reflectParamInfo(); } } else { MessageDialog.openWarning(null, Messages.getString("warning"), Messages.getString("message.job.129")); } } }); // this.m_btnDelete = new Button(composite, SWT.NONE); WidgetTestUtil.setTestId(this, "m_btnDelete", this.m_btnDelete); this.m_btnDelete.setText(Messages.getString("delete")); this.m_btnDelete.setLayoutData(new GridData(50, SizeConstant.SIZE_BUTTON_HEIGHT)); this.m_btnDelete.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { int order = m_viewer.getTable().getSelectionIndex(); if (order >= 0) { String paramId = (String) ((ArrayList<?>) m_viewer.getTable().getSelection()[0].getData()) .get(0); if (paramId == null) { paramId = ""; } String[] args = { paramId }; if (MessageDialog.openConfirm(null, Messages.getString("confirmed"), Messages.getString("message.job.130", args))) { m_jobRuntimeParamMap.remove(paramId); reflectParamInfo(); } } else { MessageDialog.openWarning(null, Messages.getString("warning"), Messages.getString("message.job.129")); } } }); this.m_viewer = new CommonTableViewer(table); this.m_viewer.createTableColumn(GetJobKickParameterTableDefine.get(), GetJobKickParameterTableDefine.SORT_COLUMN_INDEX, GetJobKickParameterTableDefine.SORT_ORDER); this.m_viewer.addDoubleClickListener(new IDoubleClickListener() { @Override public void doubleClick(DoubleClickEvent event) { int order = m_viewer.getTable().getSelectionIndex(); if (order >= 0) { String paramId = (String) ((ArrayList<?>) m_viewer.getTable().getSelection()[0].getData()) .get(0); RuntimeParameterDialog dialog = new RuntimeParameterDialog(m_shell, m_jobRuntimeParamMap, m_jobRuntimeParamMap.get(paramId)); if (dialog.open() == IDialogConstants.OK_ID) { m_jobRuntimeParamMap.remove(paramId); m_jobRuntimeParamMap.put(dialog.getInputData().getParamId(), dialog.getInputData()); reflectParamInfo(); } } else { MessageDialog.openWarning(null, Messages.getString("warning"), Messages.getString("message.job.129")); } } }); } /** * ??????? * */ public void reflectParamInfo() { if (this.m_jobRuntimeParamMap != null) { // ArrayList<ArrayList<?>> tableData = new ArrayList<ArrayList<?>>(); for (JobRuntimeParam jobRuntimeParam : this.m_jobRuntimeParamMap.values()) { ArrayList<Object> tableLineData = new ArrayList<Object>(); tableLineData.add(jobRuntimeParam.getParamId()); tableLineData.add(jobRuntimeParam.getParamType()); tableLineData.add(jobRuntimeParam.getValue()); tableLineData.add(jobRuntimeParam.getDescription()); tableData.add(tableLineData); } this.m_viewer.setInput(tableData); } } /** * ? * */ @Override public void update() { // ??? } /** * ??? * @return */ public List<JobRuntimeParam> getJobRuntimeParamList() { return new ArrayList<>(this.m_jobRuntimeParamMap.values()); } /** * ??? * @param jobRuntimeParamList */ public void setJobRuntimeParamList(List<JobRuntimeParam> jobRuntimeParamList) { if (jobRuntimeParamList == null) { this.m_jobRuntimeParamMap = new HashMap<>(); } else { for (JobRuntimeParam jobRuntimeParam : jobRuntimeParamList) { this.m_jobRuntimeParamMap.put(jobRuntimeParam.getParamId(), jobRuntimeParam); } reflectParamInfo(); } } }