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.winservice.dialog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.bean.RequiredFieldColorConstant; import com.clustercontrol.monitor.run.dialog.CommonMonitorTruthDialog; import com.clustercontrol.monitor.util.MonitorSettingEndpointWrapper; import com.clustercontrol.util.Messages; import com.clustercontrol.ws.monitor.InvalidRole_Exception; import com.clustercontrol.ws.monitor.MonitorDuplicate_Exception; import com.clustercontrol.ws.monitor.MonitorInfo; import com.clustercontrol.ws.monitor.WinServiceCheckInfo; /** * Windows?<BR> * * @version 4.0.0 * @since 4.0.0 */ public class WinServiceCreateDialog extends CommonMonitorTruthDialog { // private static Log m_log = LogFactory.getLog(WinServiceCreateDialog.class); // ----- instance ----- // /** Windows?? */ private Text m_serviceName = null; // ----- ----- // /** * ????? * * @param parent * ? */ public WinServiceCreateDialog(Shell parent) { super(parent); } /** * ???? * * @param parent * ? * @param monitorId * ?ID * @param updateFlg * ????true:?false:? */ public WinServiceCreateDialog(Shell parent, String managerName, String monitorId, boolean updateFlg) { super(parent, managerName, monitorId); this.updateFlg = updateFlg; } // ----- instance ----- // /** * ???? * * @param parent * ? */ @Override protected void customizeDialog(Composite parent) { super.customizeDialog(parent); // shell.setText(Messages.getString("dialog.winservice.create.modify")); // ???? Label label = null; // ???? GridData gridData = null; /* * ???? */ Group groupCheckRule = new Group(groupRule, SWT.NONE); WidgetTestUtil.setTestId(this, null, groupCheckRule); GridLayout layout = new GridLayout(1, true); layout.marginWidth = HALF_MARGIN; layout.marginHeight = HALF_MARGIN; layout.numColumns = BASIC_UNIT; groupCheckRule.setLayout(layout); gridData = new GridData(); gridData.horizontalSpan = BASIC_UNIT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; groupCheckRule.setLayoutData(gridData); groupCheckRule.setText(Messages.getString("check.rule")); /* * */ // label = new Label(groupCheckRule, SWT.NONE); WidgetTestUtil.setTestId(this, "winservicename", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TITLE_LONG; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("winservice.name") + " : "); // this.m_serviceName = new Text(groupCheckRule, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, null, m_serviceName); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT_LONG; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.m_serviceName.setLayoutData(gridData); this.m_serviceName.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent arg0) { update(); } }); // this.adjustDialog(); // ? MonitorInfo info = null; if (this.monitorId == null) { // ??? info = new MonitorInfo(); this.setInfoInitialValue(info); } else { // ???? try { MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(getManagerName()); info = wrapper.getMonitor(this.monitorId); } catch (InvalidRole_Exception e) { // ?????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); throw new InternalError(e.getMessage()); } catch (Exception e) { // ? m_log.warn("customizeDialog(), " + HinemosMessage.replace(e.getMessage()), e); MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.hinemos.failure.unexpected") + ", " + HinemosMessage.replace(e.getMessage())); throw new InternalError(e.getMessage()); } } this.setInputData(info); } /** * ? * */ @Override public void update() { super.update(); // ?????? if ("".equals(this.m_serviceName.getText())) { this.m_serviceName.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.m_serviceName.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } } /** * ????? * * @param monitor * ???? */ @Override protected void setInputData(MonitorInfo monitor) { super.setInputData(monitor); this.inputData = monitor; // ? Windows WinServiceCheckInfo winServiceInfo = monitor.getWinServiceCheckInfo(); if (winServiceInfo == null) { winServiceInfo = new WinServiceCheckInfo(); } if (winServiceInfo.getServiceName() != null) { this.m_serviceName.setText(winServiceInfo.getServiceName()); } // this.update(); m_truthValueInfo.setInputData(monitor); } /** * ?????? * * @return ???? */ @Override protected MonitorInfo createInputData() { super.createInputData(); if (validateResult != null) { return null; } // Windows monitorInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_WINSERVICE); // ? Windows WinServiceCheckInfo winServiceInfo = new WinServiceCheckInfo(); winServiceInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_WINSERVICE); winServiceInfo.setMonitorId(monitorInfo.getMonitorId()); if (this.m_serviceName.getText() != null && !"".equals((this.m_serviceName.getText()).trim())) { winServiceInfo.setServiceName(this.m_serviceName.getText()); } monitorInfo.setWinServiceCheckInfo(winServiceInfo); validateResult = m_truthValueInfo.createInputData(monitorInfo); if (validateResult != null) { return null; } // ?? validateResult = m_notifyInfo.createInputData(monitorInfo); if (validateResult != null) { if (validateResult.getID() == null) { // ID if (!displayQuestion(validateResult)) { validateResult = null; return null; } } else { // ? return null; } } return monitorInfo; } /** * ????? * * @return true?false * * @see com.clustercontrol.dialog.CommonDialog#action() */ @Override protected boolean action() { boolean result = false; MonitorInfo info = this.inputData; String managerName = this.getManagerName(); if (info != null) { String[] args = { info.getMonitorId(), managerName }; MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(managerName); if (!this.updateFlg) { // ??? try { result = wrapper.addMonitor(info); if (result) { MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.monitor.33", args)); } else { MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.monitor.34", args)); } } catch (MonitorDuplicate_Exception e) { // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.monitor.53", args)); } catch (Exception e) { String errMessage = ""; if (e instanceof InvalidRole_Exception) { // ?????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } else { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.monitor.34", args) + errMessage); } } else { // ?? String errMessage = ""; try { result = wrapper.modifyMonitor(info); } catch (InvalidRole_Exception e) { // ?????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } catch (Exception e) { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } if (result) { MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.monitor.35", args)); } else { MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.monitor.36", args) + errMessage); } } } return result; } }