fca2d4ec09c68410aba54131f6b86cb9737597b2
[public/netxms.git] / src / java / netxms-eclipse / ObjectManager / src / org / netxms / ui / eclipse / objectmanager / dialogs / CreateNodeDialog.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2011 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.objectmanager.dialogs;
20
21 import java.net.Inet4Address;
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.layout.RowLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.PlatformUI;
40 import org.netxms.client.NXCObjectCreationData;
41 import org.netxms.client.NXCSession;
42 import org.netxms.client.objects.GenericObject;
43 import org.netxms.client.objects.Node;
44 import org.netxms.client.objects.Zone;
45 import org.netxms.ui.eclipse.jobs.ConsoleJob;
46 import org.netxms.ui.eclipse.objectbrowser.widgets.ObjectSelector;
47 import org.netxms.ui.eclipse.objectmanager.Activator;
48 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
49 import org.netxms.ui.eclipse.tools.WidgetHelper;
50 import org.netxms.ui.eclipse.widgets.LabeledText;
51
52 /**
53 * Dialog for creating new node
54 */
55 public class CreateNodeDialog extends Dialog
56 {
57 private NXCSession session;
58
59 private LabeledText nameField;
60 private LabeledText ipAddrField;
61 private Button checkUnmanaged;
62 private Button checkDisableAgent;
63 private Button checkDisableSNMP;
64 private Button checkDisablePing;
65 private ObjectSelector agentProxySelector;
66 private ObjectSelector snmpProxySelector;
67 private ObjectSelector zoneSelector;
68
69 private String name;
70 private InetAddress ipAddress;
71 private int creationFlags;
72 private long agentProxy;
73 private long snmpProxy;
74 private long zoneId;
75
76 /**
77 * @param parentShell
78 */
79 public CreateNodeDialog(Shell parentShell)
80 {
81 super(parentShell);
82 session = (NXCSession)ConsoleSharedData.getSession();
83 zoneId = 0;
84 }
85
86 /* (non-Javadoc)
87 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
88 */
89 @Override
90 protected void configureShell(Shell newShell)
91 {
92 super.configureShell(newShell);
93 newShell.setText("Create Node Object");
94 }
95
96 /* (non-Javadoc)
97 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
98 */
99 @Override
100 protected Control createDialogArea(Composite parent)
101 {
102 Composite dialogArea = (Composite)super.createDialogArea(parent);
103
104 GridLayout layout = new GridLayout();
105 layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
106 layout.marginHeight = WidgetHelper.DIALOG_HEIGHT_MARGIN;
107 layout.marginWidth = WidgetHelper.DIALOG_WIDTH_MARGIN;
108 dialogArea.setLayout(layout);
109
110 nameField = new LabeledText(dialogArea, SWT.NONE);
111 nameField.setLabel("Name");
112 nameField.getTextControl().setTextLimit(255);
113 GridData gd = new GridData();
114 gd.horizontalAlignment = SWT.FILL;
115 gd.grabExcessHorizontalSpace = true;
116 gd.widthHint = 300;
117 nameField.setLayoutData(gd);
118
119 final Composite ipAddrGroup = new Composite(dialogArea, SWT.NONE);
120 layout = new GridLayout();
121 layout.marginHeight = 0;
122 layout.marginWidth = 0;
123 layout.numColumns = 2;
124 ipAddrGroup.setLayout(layout);
125 gd = new GridData();
126 gd.horizontalAlignment = SWT.FILL;
127 gd.grabExcessHorizontalSpace = true;
128 ipAddrGroup.setLayoutData(gd);
129
130 ipAddrField = new LabeledText(ipAddrGroup, SWT.NONE);
131 ipAddrField.setLabel("Primary IP address");
132 ipAddrField.getTextControl().setTextLimit(255);
133 gd = new GridData();
134 gd.horizontalAlignment = SWT.FILL;
135 gd.grabExcessHorizontalSpace = true;
136 ipAddrField.setLayoutData(gd);
137
138 final Button resolve = new Button(ipAddrGroup, SWT.PUSH);
139 resolve.setText("&Resolve");
140 gd = new GridData();
141 gd.widthHint = WidgetHelper.BUTTON_WIDTH_HINT;
142 gd.verticalAlignment = SWT.BOTTOM;
143 resolve.setLayoutData(gd);
144 resolve.addSelectionListener(new SelectionListener() {
145 @Override
146 public void widgetSelected(SelectionEvent e)
147 {
148 resolveName();
149 }
150
151 @Override
152 public void widgetDefaultSelected(SelectionEvent e)
153 {
154 widgetSelected(e);
155 }
156 });
157
158 Group optionsGroup = new Group(dialogArea, SWT.NONE);
159 optionsGroup.setText("Options");
160 gd = new GridData();
161 gd.horizontalAlignment = SWT.FILL;
162 gd.grabExcessHorizontalSpace = true;
163 optionsGroup.setLayoutData(gd);
164 optionsGroup.setLayout(new RowLayout(SWT.VERTICAL));
165
166 checkUnmanaged = new Button(optionsGroup, SWT.CHECK);
167 checkUnmanaged.setText("Create as &unmanaged object");
168
169 checkDisableAgent = new Button(optionsGroup, SWT.CHECK);
170 checkDisableAgent.setText("Disable usage of NetXMS &agent for all polls");
171
172 checkDisableSNMP = new Button(optionsGroup, SWT.CHECK);
173 checkDisableSNMP.setText("Disable usage of &SNMP for all polls");
174
175 checkDisablePing = new Button(optionsGroup, SWT.CHECK);
176 checkDisablePing.setText("Disable usage of &ICMP ping for all polls");
177
178 agentProxySelector = new ObjectSelector(dialogArea, SWT.NONE);
179 agentProxySelector.setLabel("Proxy for NetXMS agent");
180 agentProxySelector.setObjectClass(Node.class);
181 gd = new GridData();
182 gd.horizontalAlignment = SWT.FILL;
183 gd.grabExcessHorizontalSpace = true;
184 agentProxySelector.setLayoutData(gd);
185
186 snmpProxySelector = new ObjectSelector(dialogArea, SWT.NONE);
187 snmpProxySelector.setLabel("Proxy for SNMP");
188 snmpProxySelector.setObjectClass(Node.class);
189 gd = new GridData();
190 gd.horizontalAlignment = SWT.FILL;
191 gd.grabExcessHorizontalSpace = true;
192 snmpProxySelector.setLayoutData(gd);
193
194 if (session.isZoningEnabled())
195 {
196 zoneSelector = new ObjectSelector(dialogArea, SWT.NONE);
197 zoneSelector.setLabel("Zone");
198 zoneSelector.setObjectClass(Zone.class);
199 gd = new GridData();
200 gd.horizontalAlignment = SWT.FILL;
201 gd.grabExcessHorizontalSpace = true;
202 zoneSelector.setLayoutData(gd);
203 }
204
205 return dialogArea;
206 }
207
208 /* (non-Javadoc)
209 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
210 */
211 @Override
212 protected void okPressed()
213 {
214 try
215 {
216 ipAddress = InetAddress.getByName(ipAddrField.getText());
217 }
218 catch(UnknownHostException e)
219 {
220 MessageDialog.openWarning(getShell(), "Warning", "Invalid IP address or host name");
221 return;
222 }
223
224 name = nameField.getText().trim();
225 if (name.isEmpty())
226 name = ipAddrField.getText();
227
228 creationFlags = 0;
229 if (checkUnmanaged.getSelection())
230 creationFlags |= NXCObjectCreationData.CF_CREATE_UNMANAGED;
231 if (checkDisableAgent.getSelection())
232 creationFlags |= NXCObjectCreationData.CF_DISABLE_NXCP;
233 if (checkDisablePing.getSelection())
234 creationFlags |= NXCObjectCreationData.CF_DISABLE_ICMP;
235 if (checkDisableSNMP.getSelection())
236 creationFlags |= NXCObjectCreationData.CF_DISABLE_SNMP;
237
238 agentProxy = agentProxySelector.getObjectId();
239 snmpProxy = snmpProxySelector.getObjectId();
240 if (session.isZoningEnabled())
241 {
242 long zoneObjectId = zoneSelector.getObjectId();
243 GenericObject object = session.findObjectById(zoneObjectId);
244 if ((object != null) && (object instanceof Zone))
245 {
246 zoneId = ((Zone)object).getZoneId();
247 }
248 }
249
250 super.okPressed();
251 }
252
253 /**
254 * Resolve entered name to IP address
255 */
256 private void resolveName()
257 {
258 final String name = nameField.getText();
259 new ConsoleJob("Resolve host name", null, Activator.PLUGIN_ID, null) {
260 @Override
261 protected void runInternal(IProgressMonitor monitor) throws Exception
262 {
263 final InetAddress addr = Inet4Address.getByName(name);
264 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
265 @Override
266 public void run()
267 {
268 ipAddrField.setText(addr.getHostAddress());
269 }
270 });
271 }
272
273 @Override
274 protected String getErrorMessage()
275 {
276 return "Cannot resolve host name " + name + " to IP address";
277 }
278 }.start();
279 }
280
281 /**
282 * @return the name
283 */
284 public String getName()
285 {
286 return name;
287 }
288
289 /**
290 * @return the ipAddress
291 */
292 public InetAddress getIpAddress()
293 {
294 return ipAddress;
295 }
296
297 /**
298 * @return the creationFlags
299 */
300 public int getCreationFlags()
301 {
302 return creationFlags;
303 }
304
305 /**
306 * @return the agentProxy
307 */
308 public long getAgentProxy()
309 {
310 return agentProxy;
311 }
312
313 /**
314 * @return the snmpProxy
315 */
316 public long getSnmpProxy()
317 {
318 return snmpProxy;
319 }
320
321 /**
322 * @return the zoneId
323 */
324 public long getZoneId()
325 {
326 return zoneId;
327 }
328 }