a96ad201831c5b1ee9573e1af8be44d50410dd6b
[public/netxms.git] / src / java / netxms-eclipse / SNMP / src / org / netxms / ui / eclipse / snmp / views / SnmpCredentials.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 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.snmp.views;
20
21 import java.util.List;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.dialogs.InputDialog;
27 import org.eclipse.jface.viewers.ArrayContentProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.TableViewer;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.ISaveablePart;
37 import org.eclipse.ui.IViewSite;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.forms.events.HyperlinkAdapter;
40 import org.eclipse.ui.forms.events.HyperlinkEvent;
41 import org.eclipse.ui.forms.widgets.FormToolkit;
42 import org.eclipse.ui.forms.widgets.ImageHyperlink;
43 import org.eclipse.ui.forms.widgets.ScrolledForm;
44 import org.eclipse.ui.forms.widgets.Section;
45 import org.eclipse.ui.forms.widgets.TableWrapData;
46 import org.eclipse.ui.forms.widgets.TableWrapLayout;
47 import org.eclipse.ui.part.ViewPart;
48 import org.netxms.client.NXCSession;
49 import org.netxms.client.snmp.SnmpUsmCredential;
50 import org.netxms.ui.eclipse.console.resources.SharedIcons;
51 import org.netxms.ui.eclipse.jobs.ConsoleJob;
52 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
53 import org.netxms.ui.eclipse.snmp.Activator;
54 import org.netxms.ui.eclipse.snmp.Messages;
55 import org.netxms.ui.eclipse.snmp.dialogs.AddUsmCredDialog;
56 import org.netxms.ui.eclipse.snmp.views.helpers.SnmpConfig;
57 import org.netxms.ui.eclipse.snmp.views.helpers.SnmpUsmComparator;
58 import org.netxms.ui.eclipse.snmp.views.helpers.SnmpUsmLabelProvider;
59 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
60 import org.netxms.ui.eclipse.tools.StringComparator;
61
62 /**
63 * Configurator for network discovery
64 */
65 public class SnmpCredentials extends ViewPart implements ISaveablePart
66 {
67 public static final String ID = "org.netxms.ui.eclipse.snmp.views.SnmpCredentials"; //$NON-NLS-1$
68
69 private NXCSession session;
70 private boolean modified = false;
71 private FormToolkit toolkit;
72 private ScrolledForm form;
73 private TableViewer snmpCommunityList;
74 private TableViewer snmpUsmCredList;
75 private TableViewer snmpPortList;
76 private Action actionSave;
77 private SnmpConfig config;
78
79 /* (non-Javadoc)
80 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
81 */
82 @Override
83 public void init(IViewSite site) throws PartInitException
84 {
85 super.init(site);
86 session = ConsoleSharedData.getSession();
87 }
88
89 /* (non-Javadoc)
90 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
91 */
92 @Override
93 public void createPartControl(Composite parent)
94 {
95 toolkit = new FormToolkit(getSite().getShell().getDisplay());
96 form = toolkit.createScrolledForm(parent);
97 form.setText(Messages.get().SnmpCredentials_FormTitle);
98
99 TableWrapLayout layout = new TableWrapLayout();
100 layout.numColumns = 2;
101 form.getBody().setLayout(layout);
102
103 createSnmpCommunitySection();
104 createSnmpUsmCredSection();
105 createSnmpPortList();
106
107 createActions();
108 contributeToActionBars();
109
110 // Load config
111 new ConsoleJob(Messages.get().SnmpCredentials_LoadingConfig, this, Activator.PLUGIN_ID, null) {
112 @Override
113 protected void runInternal(IProgressMonitor monitor) throws Exception
114 {
115 final SnmpConfig loadedConfig = SnmpConfig.load(session);
116 runInUIThread(new Runnable() {
117 @Override
118 public void run()
119 {
120 setConfig(loadedConfig);
121 }
122 });
123 }
124
125 @Override
126 protected String getErrorMessage()
127 {
128 return Messages.get().SnmpCredentials_ErrorLoadingConfig;
129 }
130 }.start();
131 }
132
133 /**
134 * Create actions
135 */
136 private void createActions()
137 {
138 actionSave = new Action(Messages.get().SnmpConfigurator_Save, SharedIcons.SAVE) {
139 @Override
140 public void run()
141 {
142 save();
143 }
144 };
145 }
146
147 /**
148 * Contribute actions to action bar
149 */
150 private void contributeToActionBars()
151 {
152 IActionBars bars = getViewSite().getActionBars();
153 fillLocalPullDown(bars.getMenuManager());
154 fillLocalToolBar(bars.getToolBarManager());
155 }
156
157 /**
158 * Fill local pull-down menu
159 *
160 * @param manager
161 * Menu manager for pull-down menu
162 */
163 private void fillLocalPullDown(IMenuManager manager)
164 {
165 manager.add(actionSave);
166 }
167
168 /**
169 * Fill local tool bar
170 *
171 * @param manager
172 * Menu manager for local toolbar
173 */
174 private void fillLocalToolBar(IToolBarManager manager)
175 {
176 manager.add(actionSave);
177 }
178
179 /**
180 * Create "SNMP Communities" section
181 */
182 private void createSnmpCommunitySection()
183 {
184 Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION | Section.TITLE_BAR);
185 section.setText(Messages.get().SnmpConfigurator_SectionCommunities);
186 section.setDescription(Messages.get().SnmpConfigurator_SectionCommunitiesDescr);
187 TableWrapData td = new TableWrapData();
188 td.align = TableWrapData.FILL;
189 td.grabHorizontal = true;
190 section.setLayoutData(td);
191
192 Composite clientArea = toolkit.createComposite(section);
193 GridLayout layout = new GridLayout();
194 layout.numColumns = 2;
195 clientArea.setLayout(layout);
196 section.setClient(clientArea);
197
198 snmpCommunityList = new TableViewer(clientArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
199 toolkit.adapt(snmpCommunityList.getTable());
200 GridData gd = new GridData();
201 gd.horizontalAlignment = SWT.FILL;
202 gd.grabExcessHorizontalSpace = true;
203 gd.verticalAlignment = SWT.FILL;
204 gd.grabExcessVerticalSpace = true;
205 gd.verticalSpan = 2;
206 gd.heightHint = 150;
207 snmpCommunityList.getTable().setLayoutData(gd);
208 snmpCommunityList.getTable().setSortDirection(SWT.UP);
209 snmpCommunityList.setContentProvider(new ArrayContentProvider());
210 snmpCommunityList.setComparator(new StringComparator());
211
212 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
213 linkAdd.setText(Messages.get().SnmpConfigurator_Add);
214 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
215 gd = new GridData();
216 gd.verticalAlignment = SWT.TOP;
217 linkAdd.setLayoutData(gd);
218 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
219 @Override
220 public void linkActivated(HyperlinkEvent e)
221 {
222 addCommunity();
223 }
224 });
225
226 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
227 linkRemove.setText(Messages.get().SnmpConfigurator_Remove);
228 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
229 gd = new GridData();
230 gd.verticalAlignment = SWT.TOP;
231 linkRemove.setLayoutData(gd);
232 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
233 @Override
234 public void linkActivated(HyperlinkEvent e)
235 {
236 removeCommunity();
237 }
238 });
239 }
240
241 /**
242 * Create "Address Filters" section
243 */
244 private void createSnmpUsmCredSection()
245 {
246 Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION | Section.TITLE_BAR);
247 section.setText(Messages.get().SnmpConfigurator_SectionUSM);
248 section.setDescription(Messages.get().SnmpConfigurator_SectionUSMDescr);
249 TableWrapData td = new TableWrapData();
250 td.align = TableWrapData.FILL;
251 td.grabHorizontal = true;
252 section.setLayoutData(td);
253
254 Composite clientArea = toolkit.createComposite(section);
255 GridLayout layout = new GridLayout();
256 layout.numColumns = 2;
257 clientArea.setLayout(layout);
258 section.setClient(clientArea);
259
260 snmpUsmCredList = new TableViewer(clientArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
261 toolkit.adapt(snmpUsmCredList.getTable());
262 GridData gd = new GridData();
263 gd.horizontalAlignment = SWT.FILL;
264 gd.grabExcessHorizontalSpace = true;
265 gd.verticalAlignment = SWT.FILL;
266 gd.grabExcessVerticalSpace = true;
267 gd.verticalSpan = 2;
268 gd.heightHint = 150;
269 snmpUsmCredList.getTable().setLayoutData(gd);
270 snmpUsmCredList.setContentProvider(new ArrayContentProvider());
271 snmpUsmCredList.setLabelProvider(new SnmpUsmLabelProvider());
272 snmpUsmCredList.setComparator(new SnmpUsmComparator());
273
274 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
275 linkAdd.setText(Messages.get().SnmpConfigurator_Add);
276 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
277 gd = new GridData();
278 gd.verticalAlignment = SWT.TOP;
279 linkAdd.setLayoutData(gd);
280 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
281 @Override
282 public void linkActivated(HyperlinkEvent e)
283 {
284 addUsmCredentials();
285 }
286 });
287
288 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
289 linkRemove.setText(Messages.get().SnmpConfigurator_Remove);
290 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
291 gd = new GridData();
292 gd.verticalAlignment = SWT.TOP;
293 linkRemove.setLayoutData(gd);
294 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
295 @Override
296 public void linkActivated(HyperlinkEvent e)
297 {
298 removeUsmCredentials();
299 }
300 });
301 }
302
303 /**
304 * Create "Port List" section
305 */
306 private void createSnmpPortList()
307 {
308 Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION | Section.TITLE_BAR);
309 section.setText(Messages.get().SnmpCredentials_Ports);
310 section.setDescription(Messages.get().SnmpCredentials_PortsDescription);
311 TableWrapData td = new TableWrapData();
312 td.align = TableWrapData.FILL;
313 td.grabHorizontal = true;
314 section.setLayoutData(td);
315
316 Composite clientArea = toolkit.createComposite(section);
317 GridLayout layout = new GridLayout();
318 layout.numColumns = 2;
319 clientArea.setLayout(layout);
320 section.setClient(clientArea);
321
322 snmpPortList = new TableViewer(clientArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
323 toolkit.adapt(snmpPortList.getTable());
324 GridData gd = new GridData();
325 gd.horizontalAlignment = SWT.FILL;
326 gd.grabExcessHorizontalSpace = true;
327 gd.verticalAlignment = SWT.FILL;
328 gd.grabExcessVerticalSpace = true;
329 gd.verticalSpan = 2;
330 gd.heightHint = 150;
331 snmpPortList.getTable().setLayoutData(gd);
332 snmpPortList.setContentProvider(new ArrayContentProvider());
333 snmpPortList.setComparator(new StringComparator());
334
335 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
336 linkAdd.setText(Messages.get().SnmpConfigurator_Add);
337 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
338 gd = new GridData();
339 gd.verticalAlignment = SWT.TOP;
340 linkAdd.setLayoutData(gd);
341 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
342 @Override
343 public void linkActivated(HyperlinkEvent e)
344 {
345 addSnmpPort();
346 }
347 });
348
349 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
350 linkRemove.setText(Messages.get().SnmpConfigurator_Remove);
351 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
352 gd = new GridData();
353 gd.verticalAlignment = SWT.TOP;
354 linkRemove.setLayoutData(gd);
355 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
356 @Override
357 public void linkActivated(HyperlinkEvent e)
358 {
359 removeSnmpPort();
360 }
361 });
362 }
363
364 /* (non-Javadoc)
365 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
366 */
367 @Override
368 public void setFocus()
369 {
370 form.setFocus();
371 }
372
373 /**
374 * @param config
375 */
376 public void setConfig(SnmpConfig config)
377 {
378 this.config = config;
379
380 snmpCommunityList.setInput(config.getCommunities().toArray());
381 snmpUsmCredList.setInput(config.getUsmCredentials().toArray());
382 snmpPortList.setInput(config.getPorts().toArray());
383
384 modified = false;
385 firePropertyChange(PROP_DIRTY);
386 }
387
388 /**
389 * Mark view as modified
390 */
391 private void setModified()
392 {
393 if (!modified)
394 {
395 modified = true;
396 firePropertyChange(PROP_DIRTY);
397 }
398 }
399
400 /* (non-Javadoc)
401 * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
402 */
403 @Override
404 public void doSave(IProgressMonitor monitor)
405 {
406 try
407 {
408 config.save(session);
409 }
410 catch(Exception e)
411 {
412 MessageDialogHelper.openError(getSite().getShell(), Messages.get().SnmpConfigurator_Error,
413 String.format(Messages.get().SnmpCredentials_CannotSaveConfig, e.getLocalizedMessage()));
414 }
415 }
416
417 /* (non-Javadoc)
418 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
419 */
420 @Override
421 public void doSaveAs()
422 {
423 }
424
425 /* (non-Javadoc)
426 * @see org.eclipse.ui.ISaveablePart#isDirty()
427 */
428 @Override
429 public boolean isDirty()
430 {
431 return modified;
432 }
433
434 /* (non-Javadoc)
435 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
436 */
437 @Override
438 public boolean isSaveAsAllowed()
439 {
440 return false;
441 }
442
443 /* (non-Javadoc)
444 * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
445 */
446 @Override
447 public boolean isSaveOnCloseNeeded()
448 {
449 return modified;
450 }
451
452 /**
453 * Save settings
454 */
455 private void save()
456 {
457 new ConsoleJob(Messages.get().SnmpCredentials_SaveConfig, this, Activator.PLUGIN_ID, null) {
458 @Override
459 protected void runInternal(IProgressMonitor monitor) throws Exception
460 {
461 config.save(session);
462 runInUIThread(new Runnable() {
463 @Override
464 public void run()
465 {
466 modified = false;
467 firePropertyChange(PROP_DIRTY);
468 }
469 });
470 }
471
472 @Override
473 protected String getErrorMessage()
474 {
475 return Messages.get().SnmpCredentials_ErrorSavingConfig;
476 }
477 }.start();
478 }
479
480 /**
481 * Add SNMP community to the list
482 */
483 private void addCommunity()
484 {
485 InputDialog dlg = new InputDialog(getSite().getShell(), Messages.get().SnmpConfigurator_AddCommunity,
486 Messages.get().SnmpConfigurator_AddCommunityDescr, "", null); //$NON-NLS-1$
487 if (dlg.open() == Window.OK)
488 {
489 String s = dlg.getValue();
490 final List<String> list = config.getCommunities();
491 if (!list.contains(s))
492 {
493 list.add(s);
494 snmpCommunityList.setInput(list.toArray());
495 setModified();
496 }
497 }
498 }
499
500 /**
501 * Remove selected SNMP communities
502 */
503 private void removeCommunity()
504 {
505 final List<String> list = config.getCommunities();
506 IStructuredSelection selection = (IStructuredSelection)snmpCommunityList.getSelection();
507 if (selection.size() > 0)
508 {
509 for(Object o : selection.toList())
510 {
511 list.remove(o);
512 }
513 snmpCommunityList.setInput(list.toArray());
514 setModified();
515 }
516 }
517
518 /**
519 * Add SNMP USM credentials to the list
520 */
521 private void addUsmCredentials()
522 {
523 AddUsmCredDialog dlg = new AddUsmCredDialog(getSite().getShell());
524 if (dlg.open() == Window.OK)
525 {
526 SnmpUsmCredential cred = dlg.getValue();
527 final List<SnmpUsmCredential> list = config.getUsmCredentials();
528 if (!list.contains(cred))
529 {
530 list.add(cred);
531 snmpUsmCredList.setInput(list.toArray());
532 setModified();
533 }
534 }
535 }
536
537 /**
538 * Remove selected SNMP USM credentials
539 */
540 private void removeUsmCredentials()
541 {
542 final List<SnmpUsmCredential> list = config.getUsmCredentials();
543 IStructuredSelection selection = (IStructuredSelection)snmpUsmCredList.getSelection();
544 if (selection.size() > 0)
545 {
546 for(Object o : selection.toList())
547 {
548 list.remove(o);
549 }
550 snmpUsmCredList.setInput(list.toArray());
551 setModified();
552 }
553 }
554
555 /**
556 * Add SNMP USM credentials to the list
557 */
558 private void addSnmpPort()
559 {
560 InputDialog dlg = new InputDialog(getSite().getShell(), Messages.get().SnmpCredentials_AddPort,
561 Messages.get().SnmpCredentials_PleaseEnterPort, "", null); //$NON-NLS-1$
562 if (dlg.open() == Window.OK)
563 {
564 String value = dlg.getValue();
565 final List<String> list = config.getPorts();
566 if (!list.contains(value))
567 {
568 list.add(value);
569 snmpPortList.setInput(list.toArray());
570 setModified();
571 }
572 }
573 }
574
575 /**
576 * Remove selected SNMP USM credentials
577 */
578 private void removeSnmpPort()
579 {
580 final List<String> list = config.getPorts();
581 IStructuredSelection selection = (IStructuredSelection)snmpPortList.getSelection();
582 if (selection.size() > 0)
583 {
584 for(Object o : selection.toList())
585 {
586 list.remove(o);
587 }
588 snmpPortList.setInput(list.toArray());
589 setModified();
590 }
591 }
592 }