2afdf5a36ffe8be05baa58e3633bc4ee66f2f25d
[public/netxms.git] / src / java / netxms-eclipse / ActionManager / src / org / netxms / ui / eclipse / actionmanager / dialogs / EditActionDlg.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.actionmanager.dialogs;
20
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.layout.RowLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34 import org.netxms.client.ServerAction;
35 import org.netxms.ui.eclipse.actionmanager.Messages;
36 import org.netxms.ui.eclipse.tools.WidgetHelper;
37 import org.netxms.ui.eclipse.widgets.LabeledText;
38
39 /**
40 * Action edit dialog
41 *
42 */
43 public class EditActionDlg extends Dialog
44 {
45 private ServerAction action;
46 private boolean createNew;
47 private Text name;
48 private LabeledText recipient;
49 private LabeledText subject;
50 private LabeledText data;
51 private Button typeLocalExec;
52 private Button typeRemoteExec;
53 private Button typeExecScript;
54 private Button typeEMail;
55 private Button typeSMS;
56 private Button typeForward;
57 private Button markDisabled;
58
59 /**
60 * Selection listener for action type radio buttons
61 *
62 */
63 class TypeButtonSelectionListener implements SelectionListener
64 {
65 @Override
66 public void widgetDefaultSelected(SelectionEvent e)
67 {
68 widgetSelected(e);
69 }
70
71 @Override
72 public void widgetSelected(SelectionEvent e)
73 {
74 onTypeChange();
75 }
76 };
77
78 /**
79 * @param parentShell
80 */
81 public EditActionDlg(Shell parentShell, ServerAction action, boolean createNew)
82 {
83 super(parentShell);
84 this.action = action;
85 this.createNew = createNew;
86 }
87
88 /* (non-Javadoc)
89 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
90 */
91 @Override
92 protected void configureShell(Shell newShell)
93 {
94 super.configureShell(newShell);
95 newShell.setText(createNew ? Messages.get().EditActionDlg_CreateAction : Messages.get().EditActionDlg_EditAction);
96 }
97
98 /* (non-Javadoc)
99 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
100 */
101 @Override
102 protected Control createDialogArea(Composite parent)
103 {
104 Composite dialogArea = (Composite)super.createDialogArea(parent);
105
106 GridLayout layout = new GridLayout();
107 layout.horizontalSpacing = WidgetHelper.OUTER_SPACING;
108 layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
109 dialogArea.setLayout(layout);
110
111 name = WidgetHelper.createLabeledText(dialogArea, SWT.BORDER, 300, Messages.get().EditActionDlg_Name, action.getName(), WidgetHelper.DEFAULT_LAYOUT_DATA);
112
113 /* type selection radio buttons */
114 Group typeGroup = new Group(dialogArea, SWT.NONE);
115 typeGroup.setText(Messages.get().EditActionDlg_Type);
116 typeGroup.setLayout(new RowLayout(SWT.VERTICAL));
117 GridData gd = new GridData();
118 gd.horizontalAlignment = SWT.FILL;
119 gd.grabExcessHorizontalSpace = true;
120 typeGroup.setLayoutData(gd);
121
122 typeLocalExec = new Button(typeGroup, SWT.RADIO);
123 typeLocalExec.setText(Messages.get().EditActionDlg_ExecCommandOnServer);
124 typeLocalExec.setSelection(action.getType() == ServerAction.EXEC_LOCAL);
125 typeLocalExec.addSelectionListener(new TypeButtonSelectionListener());
126
127 typeRemoteExec = new Button(typeGroup, SWT.RADIO);
128 typeRemoteExec.setText(Messages.get().EditActionDlg_ExecCommandOnNode);
129 typeRemoteExec.setSelection(action.getType() == ServerAction.EXEC_REMOTE);
130 typeRemoteExec.addSelectionListener(new TypeButtonSelectionListener());
131
132 typeExecScript = new Button(typeGroup, SWT.RADIO);
133 typeExecScript.setText(Messages.get().EditActionDlg_ExecuteScript);
134 typeExecScript.setSelection(action.getType() == ServerAction.EXEC_NXSL_SCRIPT);
135 typeExecScript.addSelectionListener(new TypeButtonSelectionListener());
136
137 typeEMail = new Button(typeGroup, SWT.RADIO);
138 typeEMail.setText(Messages.get().EditActionDlg_SenMail);
139 typeEMail.setSelection(action.getType() == ServerAction.SEND_EMAIL);
140 typeEMail.addSelectionListener(new TypeButtonSelectionListener());
141
142 typeSMS = new Button(typeGroup, SWT.RADIO);
143 typeSMS.setText(Messages.get().EditActionDlg_SendSMS);
144 typeSMS.setSelection(action.getType() == ServerAction.SEND_SMS);
145 typeSMS.addSelectionListener(new TypeButtonSelectionListener());
146
147 typeForward = new Button(typeGroup, SWT.RADIO);
148 typeForward.setText(Messages.get().EditActionDlg_ForwardEvent);
149 typeForward.setSelection(action.getType() == ServerAction.FORWARD_EVENT);
150 typeForward.addSelectionListener(new TypeButtonSelectionListener());
151 /* type selection radio buttons - end */
152
153 Group optionsGroup = new Group(dialogArea, SWT.NONE);
154 optionsGroup.setText(Messages.get().EditActionDlg_Options);
155 optionsGroup.setLayout(new RowLayout(SWT.VERTICAL));
156 gd = new GridData();
157 gd.horizontalAlignment = SWT.FILL;
158 gd.grabExcessHorizontalSpace = true;
159 optionsGroup.setLayoutData(gd);
160
161 markDisabled = new Button(optionsGroup, SWT.CHECK);
162 markDisabled.setText(Messages.get().EditActionDlg_ActionDisabled);
163 markDisabled.setSelection(action.isDisabled());
164
165 recipient = new LabeledText(dialogArea, SWT.NONE);
166 recipient.setLabel(getRcptLabel(action.getType()));
167 recipient.setText(action.getRecipientAddress());
168 gd = new GridData();
169 gd.horizontalAlignment = SWT.FILL;
170 gd.grabExcessHorizontalSpace = true;
171 recipient.setLayoutData(gd);
172
173 subject = new LabeledText(dialogArea, SWT.NONE);
174 subject.setLabel(Messages.get().EditActionDlg_MailSubject);
175 subject.setText(action.getEmailSubject());
176 gd = new GridData();
177 gd.horizontalAlignment = SWT.FILL;
178 gd.grabExcessHorizontalSpace = true;
179 subject.setLayoutData(gd);
180
181 data = new LabeledText(dialogArea, SWT.NONE);
182 data.setLabel(getDataLabel(action.getType()));
183 data.setText(action.getData());
184 gd = new GridData();
185 gd.horizontalAlignment = SWT.FILL;
186 gd.grabExcessHorizontalSpace = true;
187 data.setLayoutData(gd);
188
189 onTypeChange();
190
191 return dialogArea;
192 }
193
194 /**
195 * Get "recipient" field label depending on action type
196 *
197 * @param type Action type
198 * @return Label for "recipient" field
199 */
200 private String getRcptLabel(int type)
201 {
202 switch(type)
203 {
204 case ServerAction.EXEC_REMOTE:
205 return Messages.get().EditActionDlg_RemoteHost;
206 case ServerAction.SEND_SMS:
207 return Messages.get().EditActionDlg_PhoneNumber;
208 case ServerAction.FORWARD_EVENT:
209 return Messages.get().EditActionDlg_RemoteServer;
210 case ServerAction.EXEC_NXSL_SCRIPT:
211 return Messages.get().EditActionDlg_ScriptName;
212 }
213 return Messages.get().EditActionDlg_Recipient;
214 }
215
216 /**
217 * Get "data" field label depending on action type
218 *
219 * @param type Action type
220 * @return Label for "data" field
221 */
222 private String getDataLabel(int type)
223 {
224 switch(type)
225 {
226 case ServerAction.EXEC_LOCAL:
227 return Messages.get().EditActionDlg_Command;
228 case ServerAction.EXEC_REMOTE:
229 return Messages.get().EditActionDlg_Action;
230 }
231 return Messages.get().EditActionDlg_MessageText;
232 }
233
234 /* (non-Javadoc)
235 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
236 */
237 @Override
238 protected void okPressed()
239 {
240 if (typeLocalExec.getSelection())
241 action.setType(ServerAction.EXEC_LOCAL);
242 else if (typeRemoteExec.getSelection())
243 action.setType(ServerAction.EXEC_REMOTE);
244 else if (typeExecScript.getSelection())
245 action.setType(ServerAction.EXEC_NXSL_SCRIPT);
246 else if (typeEMail.getSelection())
247 action.setType(ServerAction.SEND_EMAIL);
248 else if (typeSMS.getSelection())
249 action.setType(ServerAction.SEND_SMS);
250 else if (typeForward.getSelection())
251 action.setType(ServerAction.FORWARD_EVENT);
252
253 action.setName(name.getText());
254 action.setRecipientAddress(recipient.getText());
255 action.setEmailSubject(subject.getText());
256 action.setData(data.getText());
257 action.setDisabled(markDisabled.getSelection());
258
259 super.okPressed();
260 }
261
262 /**
263 * Handle action type change
264 */
265 private void onTypeChange()
266 {
267 int type = -1;
268
269 if (typeLocalExec.getSelection())
270 type = ServerAction.EXEC_LOCAL;
271 else if (typeRemoteExec.getSelection())
272 type = ServerAction.EXEC_REMOTE;
273 else if (typeExecScript.getSelection())
274 type = ServerAction.EXEC_NXSL_SCRIPT;
275 else if (typeEMail.getSelection())
276 type = ServerAction.SEND_EMAIL;
277 else if (typeSMS.getSelection())
278 type = ServerAction.SEND_SMS;
279 else if (typeForward.getSelection())
280 type = ServerAction.FORWARD_EVENT;
281
282 switch(type)
283 {
284 case ServerAction.EXEC_LOCAL:
285 recipient.setEnabled(false);
286 subject.setEnabled(false);
287 data.setEnabled(true);
288 break;
289 case ServerAction.EXEC_REMOTE:
290 recipient.setEnabled(true);
291 subject.setEnabled(false);
292 data.setEnabled(true);
293 break;
294 case ServerAction.SEND_EMAIL:
295 recipient.setEnabled(true);
296 subject.setEnabled(true);
297 data.setEnabled(true);
298 break;
299 case ServerAction.SEND_SMS:
300 recipient.setEnabled(true);
301 subject.setEnabled(false);
302 data.setEnabled(true);
303 break;
304 case ServerAction.FORWARD_EVENT:
305 recipient.setEnabled(true);
306 subject.setEnabled(false);
307 data.setEnabled(false);
308 break;
309 case ServerAction.EXEC_NXSL_SCRIPT:
310 recipient.setEnabled(true);
311 subject.setEnabled(false);
312 data.setEnabled(false);
313 break;
314 }
315
316 recipient.setLabel(getRcptLabel(type));
317 data.setLabel(getDataLabel(type));
318 }
319 }