25bfa7a44bce244b75ce68e92a55dd0ab13224d1
[public/netxms.git] / src / java / netxms-eclipse / DataCollection / src / org / netxms / ui / eclipse / datacollection / views / DataCollectionEditor.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2014 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.datacollection.views;
20
21 import java.util.Iterator;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.GroupMarker;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.commands.ActionHandler;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.viewers.ArrayContentProvider;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.events.DisposeEvent;
42 import org.eclipse.swt.events.DisposeListener;
43 import org.eclipse.swt.events.ModifyEvent;
44 import org.eclipse.swt.events.ModifyListener;
45 import org.eclipse.swt.layout.FormAttachment;
46 import org.eclipse.swt.layout.FormData;
47 import org.eclipse.swt.layout.FormLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Menu;
50 import org.eclipse.ui.IActionBars;
51 import org.eclipse.ui.IViewSite;
52 import org.eclipse.ui.IWorkbenchActionConstants;
53 import org.eclipse.ui.PartInitException;
54 import org.eclipse.ui.contexts.IContextService;
55 import org.eclipse.ui.handlers.IHandlerService;
56 import org.eclipse.ui.part.ViewPart;
57 import org.netxms.client.NXCException;
58 import org.netxms.client.NXCSession;
59 import org.netxms.client.constants.RCC;
60 import org.netxms.client.datacollection.DataCollectionConfiguration;
61 import org.netxms.client.datacollection.DataCollectionItem;
62 import org.netxms.client.datacollection.DataCollectionObject;
63 import org.netxms.client.datacollection.DataCollectionTable;
64 import org.netxms.client.objects.AbstractNode;
65 import org.netxms.client.objects.AbstractObject;
66 import org.netxms.client.objects.Cluster;
67 import org.netxms.client.objects.DataCollectionTarget;
68 import org.netxms.client.objects.MobileDevice;
69 import org.netxms.client.objects.Template;
70 import org.netxms.ui.eclipse.actions.RefreshAction;
71 import org.netxms.ui.eclipse.console.resources.GroupMarkers;
72 import org.netxms.ui.eclipse.console.resources.SharedIcons;
73 import org.netxms.ui.eclipse.datacollection.Activator;
74 import org.netxms.ui.eclipse.datacollection.Messages;
75 import org.netxms.ui.eclipse.datacollection.views.helpers.DciComparator;
76 import org.netxms.ui.eclipse.datacollection.views.helpers.DciFilter;
77 import org.netxms.ui.eclipse.datacollection.views.helpers.DciLabelProvider;
78 import org.netxms.ui.eclipse.jobs.ConsoleJob;
79 import org.netxms.ui.eclipse.objectbrowser.dialogs.ObjectSelectionDialog;
80 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
81 import org.netxms.ui.eclipse.tools.ExtendedPropertyDialog;
82 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
83 import org.netxms.ui.eclipse.tools.WidgetHelper;
84 import org.netxms.ui.eclipse.widgets.FilterText;
85 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
86 import org.netxms.ui.eclipse.actions.ExportToCsvAction;
87
88 /**
89 * Data collection configuration view
90 *
91 */
92 public class DataCollectionEditor extends ViewPart
93 {
94 public static final String ID = "org.netxms.ui.eclipse.datacollection.view.data_collection_editor"; //$NON-NLS-1$
95 public static final String JOB_FAMILY = "DataCollectionEditorJob"; //$NON-NLS-1$
96
97 // Columns
98 public static final int COLUMN_ID = 0;
99 public static final int COLUMN_ORIGIN = 1;
100 public static final int COLUMN_DESCRIPTION = 2;
101 public static final int COLUMN_PARAMETER = 3;
102 public static final int COLUMN_DATATYPE = 4;
103 public static final int COLUMN_INTERVAL = 5;
104 public static final int COLUMN_RETENTION = 6;
105 public static final int COLUMN_STATUS = 7;
106 public static final int COLUMN_THRESHOLD = 8;
107 public static final int COLUMN_TEMPLATE = 9;
108
109 private boolean initShowFilter = true;
110 private Composite content;
111 private FilterText filterText;
112 private SortableTableViewer viewer;
113 private NXCSession session;
114 private IDialogSettings settings;
115 private AbstractObject object;
116 private DataCollectionConfiguration dciConfig = null;
117 private DciFilter filter;
118 private Action actionCreateItem;
119 private Action actionCreateTable;
120 private Action actionEdit;
121 private Action actionDelete;
122 private Action actionCopy;
123 private Action actionMove;
124 private Action actionConvert;
125 private Action actionDuplicate;
126 private Action actionActivate;
127 private Action actionDisable;
128 private Action actionShowFilter;
129 private RefreshAction actionRefresh;
130 private Action actionExportToCsv;
131 private Action actionExportAllToCsv;
132
133 /* (non-Javadoc)
134 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
135 */
136 @Override
137 public void init(IViewSite site) throws PartInitException
138 {
139 super.init(site);
140
141 session = (NXCSession)ConsoleSharedData.getSession();
142 AbstractObject obj = session.findObjectById(Long.parseLong(site.getSecondaryId()));
143 IDialogSettings settings = Activator.getDefault().getDialogSettings();
144 initShowFilter = safeCast(settings.get("DataCollectionEditor.showFilter"), settings.getBoolean("DataCollectionEditor.showFilter"), initShowFilter);
145 object = ((obj != null) && ((obj instanceof DataCollectionTarget) || (obj instanceof Template))) ? obj : null;
146 setPartName(Messages.get().DataCollectionEditor_PartNamePrefix + ((object != null) ? object.getObjectName() : Messages.get().DataCollectionEditor_Error));
147 }
148
149 /**
150 * @param b
151 * @param defval
152 * @return
153 */
154 private static boolean safeCast(String s, boolean b, boolean defval)
155 {
156 return (s != null) ? b : defval;
157 }
158
159 /* (non-Javadoc)
160 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
161 */
162 @Override
163 public void createPartControl(Composite parent)
164 {
165 content = new Composite(parent, SWT.NONE);
166 content.setLayout(new FormLayout());
167
168 // Create filter area
169 filterText = new FilterText(content, SWT.NONE);
170 filterText.addModifyListener(new ModifyListener() {
171 @Override
172 public void modifyText(ModifyEvent e)
173 {
174 onFilterModify();
175 }
176 });
177
178 final String[] names = { Messages.get().DataCollectionEditor_ColID, Messages.get().DataCollectionEditor_ColOrigin, Messages.get().DataCollectionEditor_ColDescription, Messages.get().DataCollectionEditor_ColParameter, Messages.get().DataCollectionEditor_ColDataType, Messages.get().DataCollectionEditor_ColPollingInterval, Messages.get().DataCollectionEditor_ColRetentionTime, Messages.get().DataCollectionEditor_ColStatus, Messages.get().DataCollectionEditor_ColThresholds, Messages.get().DataCollectionEditor_ColTemplate };
179 final int[] widths = { 60, 100, 250, 200, 90, 90, 90, 100, 200, 150 };
180 viewer = new SortableTableViewer(content, names, widths, 0, SWT.UP, SortableTableViewer.DEFAULT_STYLE);
181 viewer.setContentProvider(new ArrayContentProvider());
182 viewer.setLabelProvider(new DciLabelProvider());
183 viewer.setComparator(new DciComparator((DciLabelProvider)viewer.getLabelProvider()));
184 filter = new DciFilter();
185 viewer.addFilter(filter);
186 WidgetHelper.restoreTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), "DataCollectionEditor"); //$NON-NLS-1$
187
188 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
189 @Override
190 public void selectionChanged(SelectionChangedEvent event)
191 {
192 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
193 if (selection != null)
194 {
195 actionEdit.setEnabled(selection.size() == 1);
196 actionDelete.setEnabled(selection.size() > 0);
197 actionCopy.setEnabled(selection.size() > 0);
198 actionMove.setEnabled(selection.size() > 0);
199 actionConvert.setEnabled(selection.size() > 0);
200 actionDuplicate.setEnabled(selection.size() > 0);
201
202 Iterator<?> it = selection.iterator();
203 boolean canActivate = false;
204 boolean canDisable = false;
205 while(it.hasNext() && (!canActivate || !canDisable))
206 {
207 DataCollectionObject dci = (DataCollectionObject)it.next();
208 if (dci.getStatus() != DataCollectionObject.ACTIVE)
209 canActivate = true;
210 if (dci.getStatus() != DataCollectionObject.DISABLED)
211 canDisable = true;
212 }
213 actionActivate.setEnabled(canActivate);
214 actionDisable.setEnabled(canDisable);
215 }
216 }
217 });
218 viewer.addDoubleClickListener(new IDoubleClickListener() {
219 @Override
220 public void doubleClick(DoubleClickEvent event)
221 {
222 actionEdit.run();
223 }
224 });
225 viewer.getTable().addDisposeListener(new DisposeListener() {
226 @Override
227 public void widgetDisposed(DisposeEvent e)
228 {
229 WidgetHelper.saveTableViewerSettings(viewer, Activator.getDefault().getDialogSettings(), "DataCollectionEditor"); //$NON-NLS-1$
230 }
231 });
232
233 // Setup layout
234 FormData fd = new FormData();
235 fd.left = new FormAttachment(0, 0);
236 fd.top = new FormAttachment(filterText);
237 fd.right = new FormAttachment(100, 0);
238 fd.bottom = new FormAttachment(100, 0);
239 viewer.getTable().setLayoutData(fd);
240
241 fd = new FormData();
242 fd.left = new FormAttachment(0, 0);
243 fd.top = new FormAttachment(0, 0);
244 fd.right = new FormAttachment(100, 0);
245 filterText.setLayoutData(fd);
246
247 createActions();
248 contributeToActionBars();
249 createPopupMenu();
250
251 filterText.setCloseAction(actionShowFilter);
252
253 // Request server to open data collection configuration
254 new ConsoleJob(Messages.get().DataCollectionEditor_OpenJob_Title + object.getObjectName(), this, Activator.PLUGIN_ID, null) {
255 @Override
256 protected void runInternal(IProgressMonitor monitor) throws Exception
257 {
258 dciConfig = session.openDataCollectionConfiguration(object.getObjectId());
259 dciConfig.setUserData(viewer);
260 runInUIThread(new Runnable() {
261 @Override
262 public void run()
263 {
264 viewer.setInput(dciConfig.getItems());
265 }
266 });
267 }
268
269 @Override
270 protected void jobFailureHandler()
271 {
272 runInUIThread(new Runnable() {
273 @Override
274 public void run()
275 {
276 DataCollectionEditor.this.getViewSite().getPage().hideView(DataCollectionEditor.this);
277 }
278 });
279 }
280
281 @Override
282 protected String getErrorMessage()
283 {
284 return Messages.get().DataCollectionEditor_OpenJob_Error + object.getObjectName();
285 }
286 }.start();
287
288 // Set initial focus to filter input line
289 if (initShowFilter)
290 filterText.setFocus();
291 else
292 enableFilter(false); // Will hide filter area correctly
293
294 activateContext();
295 }
296
297 /**
298 * Activate context
299 */
300 private void activateContext()
301 {
302 IContextService contextService = (IContextService)getSite().getService(IContextService.class);
303 if (contextService != null)
304 {
305 contextService.activateContext("org.netxms.ui.eclipse.datacollection.context.LastValues"); //$NON-NLS-1$
306 }
307 }
308
309 /**
310 * Contribute actions to action bar
311 */
312 private void contributeToActionBars()
313 {
314 IActionBars bars = getViewSite().getActionBars();
315 fillLocalPullDown(bars.getMenuManager());
316 fillLocalToolBar(bars.getToolBarManager());
317 }
318
319 /**
320 * Fill local pull-down menu
321 *
322 * @param manager
323 * Menu manager for pull-down menu
324 */
325 private void fillLocalPullDown(IMenuManager manager)
326 {
327 manager.add(actionShowFilter);
328 manager.add(new Separator());
329 manager.add(actionCreateItem);
330 manager.add(actionCreateTable);
331 manager.add(actionEdit);
332 manager.add(actionDelete);
333 manager.add(actionCopy);
334 manager.add(actionMove);
335 manager.add(actionConvert);
336 manager.add(actionDuplicate);
337 manager.add(new Separator());
338 manager.add(actionActivate);
339 manager.add(actionDisable);
340 manager.add(new Separator());
341 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
342 manager.add(new Separator());
343 manager.add(new GroupMarker(GroupMarkers.MB_SECONDARY));
344 manager.add(new Separator());
345 manager.add(actionRefresh);
346 manager.add(actionExportAllToCsv);
347 }
348
349 /**
350 * Fill local tool bar
351 *
352 * @param manager
353 * Menu manager for local toolbar
354 */
355 private void fillLocalToolBar(IToolBarManager manager)
356 {
357 manager.add(actionShowFilter);
358 manager.add(actionCreateItem);
359 manager.add(actionRefresh);
360 manager.add(actionExportAllToCsv);
361 }
362
363 /**
364 * Fill context menu
365 *
366 * @param mgr Menu manager
367 */
368 protected void fillContextMenu(final IMenuManager manager)
369 {
370 manager.add(actionCreateItem);
371 manager.add(actionCreateTable);
372 manager.add(actionEdit);
373 manager.add(actionDelete);
374 manager.add(actionCopy);
375 manager.add(actionMove);
376 manager.add(actionConvert);
377 manager.add(actionDuplicate);
378 manager.add(actionExportToCsv);
379 manager.add(new Separator());
380 manager.add(actionActivate);
381 manager.add(actionDisable);
382 manager.add(new Separator());
383 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
384 manager.add(new Separator());
385 manager.add(new GroupMarker(GroupMarkers.MB_SECONDARY));
386 }
387
388 /**
389 * Create actions
390 */
391 private void createActions()
392 {
393 final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
394
395 actionRefresh = new RefreshAction() {
396 @Override
397 public void run()
398 {
399 viewer.setInput(dciConfig.getItems());
400 }
401 };
402
403 actionCreateItem = new Action(Messages.get().DataCollectionEditor_NewParam, Activator.getImageDescriptor("icons/new.png")) { //$NON-NLS-1$
404 @Override
405 public void run()
406 {
407 createItem();
408 }
409 };
410
411 actionCreateTable = new Action(Messages.get().DataCollectionEditor_NewTable) {
412 @Override
413 public void run()
414 {
415 createTable();
416 }
417 };
418
419 actionEdit = new Action(Messages.get().DataCollectionEditor_ActionEdit, SharedIcons.EDIT) {
420 @Override
421 public void run()
422 {
423 editSelectedObject();
424 }
425 };
426 actionEdit.setText(Messages.get().DataCollectionEditor_Edit);
427 actionEdit.setImageDescriptor(Activator.getImageDescriptor("icons/edit.png")); //$NON-NLS-1$
428 actionEdit.setEnabled(false);
429
430 actionDelete = new Action(Messages.get().DataCollectionEditor_Delete, Activator.getImageDescriptor("icons/delete.png")) { //$NON-NLS-1$
431 @Override
432 public void run()
433 {
434 deleteItems();
435 }
436 };
437 actionDelete.setEnabled(false);
438
439 actionCopy = new Action(Messages.get().DataCollectionEditor_Copy) {
440 @Override
441 public void run()
442 {
443 copyItems(false);
444 }
445 };
446 actionCopy.setEnabled(false);
447
448 actionMove = new Action(Messages.get().DataCollectionEditor_Move) {
449 @Override
450 public void run()
451 {
452 copyItems(true);
453 }
454 };
455 actionMove.setEnabled(false);
456
457 actionConvert = new Action(Messages.get().DataCollectionEditor_Convert) {
458 @Override
459 public void run()
460 {
461 convertToTemplate();
462 }
463 };
464 actionConvert.setEnabled(false);
465
466 actionDuplicate = new Action(Messages.get().DataCollectionEditor_Duplicate) {
467 @Override
468 public void run()
469 {
470 duplicateItems();
471 }
472 };
473 actionDuplicate.setEnabled(false);
474
475 actionActivate = new Action(Messages.get().DataCollectionEditor_Activate, Activator.getImageDescriptor("icons/active.gif")) { //$NON-NLS-1$
476 @Override
477 public void run()
478 {
479 setItemStatus(DataCollectionObject.ACTIVE);
480 actionActivate.setEnabled(false);
481 actionDisable.setEnabled(true);
482 }
483 };
484 actionActivate.setEnabled(false);
485
486 actionDisable = new Action(Messages.get().DataCollectionEditor_Disable, Activator.getImageDescriptor("icons/disabled.gif")) { //$NON-NLS-1$
487 @Override
488 public void run()
489 {
490 setItemStatus(DataCollectionObject.DISABLED);
491 actionActivate.setEnabled(true);
492 actionDisable.setEnabled(false);
493 }
494 };
495 actionDisable.setEnabled(false);
496
497 actionShowFilter = new Action(Messages.get().DataCollectionEditor_ShowFilter, Action.AS_CHECK_BOX) {
498 @Override
499 public void run()
500 {
501 enableFilter(!initShowFilter);
502 actionShowFilter.setChecked(initShowFilter);
503 }
504 };
505 actionShowFilter.setImageDescriptor(SharedIcons.FILTER);
506 actionShowFilter.setChecked(initShowFilter);
507 actionShowFilter.setActionDefinitionId("org.netxms.ui.eclipse.datacollection.commands.show_dci_filter"); //$NON-NLS-1$
508 handlerService.activateHandler(actionShowFilter.getActionDefinitionId(), new ActionHandler(actionShowFilter));
509
510 actionExportToCsv = new ExportToCsvAction(this, viewer, true);
511 actionExportAllToCsv = new ExportToCsvAction(this, viewer, false);
512 }
513
514 /**
515 * Create pop-up menu for variable list
516 */
517 private void createPopupMenu()
518 {
519 // Create menu manager.
520 MenuManager menuMgr = new MenuManager();
521 menuMgr.setRemoveAllWhenShown(true);
522 menuMgr.addMenuListener(new IMenuListener()
523 {
524 public void menuAboutToShow(IMenuManager mgr)
525 {
526 fillContextMenu(mgr);
527 }
528 });
529
530 // Create menu.
531 Menu menu = menuMgr.createContextMenu(viewer.getControl());
532 viewer.getControl().setMenu(menu);
533
534 // Register menu for extension.
535 getSite().registerContextMenu(menuMgr, viewer);
536 }
537
538 /* (non-Javadoc)
539 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
540 */
541 @Override
542 public void setFocus()
543 {
544 viewer.getControl().setFocus();
545 }
546
547 /* (non-Javadoc)
548 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
549 */
550 @Override
551 public void dispose()
552 {
553 settings.put("DataCollectionEditor.showFilter", initShowFilter);
554
555 if (dciConfig != null)
556 {
557 new ConsoleJob(Messages.get().DataCollectionEditor_UnlockJob_Title + object.getObjectName(), null, Activator.PLUGIN_ID, null) {
558 @Override
559 protected void runInternal(IProgressMonitor monitor) throws Exception
560 {
561 dciConfig.close();
562 dciConfig = null;
563 }
564
565 @Override
566 protected String getErrorMessage()
567 {
568 return Messages.get().DataCollectionEditor_UnlockJob_Error + object.getObjectName();
569 }
570 }.start();
571 }
572 super.dispose();
573 }
574
575 /**
576 * Change status for selected items
577 *
578 * @param newStatus New status
579 */
580 private void setItemStatus(final int newStatus)
581 {
582 final IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
583 if (selection.size() <= 0)
584 return;
585
586 new ConsoleJob(Messages.get().DataCollectionEditor_ChStatusJob_Title + object.getObjectName(), this, Activator.PLUGIN_ID, null) {
587 @Override
588 protected void runInternal(IProgressMonitor monitor) throws Exception
589 {
590 final long[] itemList = new long[selection.size()];
591 int pos = 0;
592 for(Object dci : selection.toList())
593 {
594 itemList[pos++] = ((DataCollectionObject)dci).getId();
595 }
596 dciConfig.setObjectStatus(itemList, newStatus);
597 runInUIThread(new Runnable() {
598 @Override
599 public void run()
600 {
601 for(Object dci : selection.toList())
602 {
603 ((DataCollectionObject)dci).setStatus(newStatus);
604 viewer.update(dci, null);
605 }
606 }
607 });
608 }
609
610 @Override
611 protected String getErrorMessage()
612 {
613 return Messages.get().DataCollectionEditor_ChStatusJob_Error + object.getObjectName();
614 }
615 }.start();
616 }
617
618 /**
619 * Delete currently selected DCIs
620 */
621 private void deleteItems()
622 {
623 final IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
624 if (selection.size() <= 0)
625 return;
626
627 if (!MessageDialogHelper.openConfirm(getSite().getShell(), Messages.get().DataCollectionEditor_DeleteConfirmTitle,
628 Messages.get().DataCollectionEditor_DeleteConfirmText))
629 return;
630
631 new ConsoleJob(Messages.get().DataCollectionEditor_DeleteJob_Title + object.getObjectName(), this, Activator.PLUGIN_ID, null) {
632 @Override
633 protected void runInternal(IProgressMonitor monitor) throws Exception
634 {
635 for(Object dci : selection.toList())
636 {
637 dciConfig.deleteObject(((DataCollectionObject)dci).getId());
638 }
639 runInUIThread(new Runnable() {
640 @Override
641 public void run()
642 {
643 viewer.setInput(dciConfig.getItems());
644 }
645 });
646 }
647
648 @Override
649 protected String getErrorMessage()
650 {
651 return Messages.get().DataCollectionEditor_DeleteJob_Error + object.getObjectName();
652 }
653 }.start();
654 }
655
656 /**
657 * Create new data collection item
658 */
659 private void createItem()
660 {
661 DataCollectionItem dci = new DataCollectionItem(dciConfig, 0);
662 if((object instanceof AbstractNode) && !((AbstractNode)object).hasAgent())
663 {
664 if(((AbstractNode)object).hasSnmpAgent())
665 {
666 dci.setOrigin(DataCollectionObject.SNMP);
667 }
668 else
669 {
670 dci.setOrigin(DataCollectionObject.INTERNAL);
671 }
672 }
673 viewer.add(dci);
674 viewer.setSelection(new StructuredSelection(dci), true);
675 actionEdit.run();
676 }
677
678 /**
679 * Create new data collection table
680 */
681 private void createTable()
682 {
683 DataCollectionTable dci = new DataCollectionTable(dciConfig, 0);
684 if((object instanceof AbstractNode) && !((AbstractNode)object).hasAgent())
685 {
686 if(((AbstractNode)object).hasSnmpAgent())
687 {
688 dci.setOrigin(DataCollectionObject.SNMP);
689 }
690 else
691 {
692 dci.setOrigin(DataCollectionObject.INTERNAL);
693 }
694 }
695 viewer.add(dci);
696 viewer.setSelection(new StructuredSelection(dci), true);
697 actionEdit.run();
698 }
699
700 /**
701 *
702 */
703 private void editSelectedObject()
704 {
705 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
706 if (selection.size() != 1)
707 return;
708
709 ExtendedPropertyDialog dlg = ExtendedPropertyDialog.createDialogOn(getSite().getShell(), null, selection.getFirstElement(), ""); //$NON-NLS-1$
710 dlg.createAllPages();
711 int result = dlg.open();
712 if(((DataCollectionObject)selection.getFirstElement()).isNewItem() && result != SWT.OK)
713 {
714 viewer.remove(selection.getFirstElement());
715 }
716 }
717
718 /**
719 * Duplicate selected item(s)
720 */
721 private void duplicateItems()
722 {
723 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
724 Iterator<?> it = selection.iterator();
725 final long[] dciList = new long[selection.size()];
726 for(int i = 0; (i < dciList.length) && it.hasNext(); i++)
727 dciList[i] = ((DataCollectionObject)it.next()).getId();
728
729 new ConsoleJob(Messages.get().DataCollectionEditor_DupJob_Title + object.getObjectName(), this, Activator.PLUGIN_ID, null) {
730 @Override
731 protected void runInternal(IProgressMonitor monitor) throws Exception
732 {
733 dciConfig.copyObjects(dciConfig.getNodeId(), dciList);
734 dciConfig.close();
735 dciConfig.open();
736 runInUIThread(new Runnable() {
737 @Override
738 public void run()
739 {
740 viewer.setInput(dciConfig.getItems());
741 }
742 });
743 }
744
745 @Override
746 protected String getErrorMessage()
747 {
748 return Messages.get().DataCollectionEditor_DupJob_Error + object.getObjectName();
749 }
750 }.start();
751 }
752
753 /**
754 * Copy items to another node
755 */
756 private void copyItems(final boolean doMove)
757 {
758 final ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, ObjectSelectionDialog.createNodeAndTemplateSelectionFilter(true));
759 if (dlg.open() != Window.OK)
760 return;
761
762 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
763 Iterator<?> it = selection.iterator();
764 final long[] dciList = new long[selection.size()];
765 for(int i = 0; (i < dciList.length) && it.hasNext(); i++)
766 dciList[i] = ((DataCollectionObject)it.next()).getId();
767
768 new ConsoleJob(Messages.get().DataCollectionEditor_CopyJob_Title + object.getObjectName(), this, Activator.PLUGIN_ID, null) {
769 @Override
770 protected void runInternal(IProgressMonitor monitor) throws Exception
771 {
772 for(AbstractObject o : dlg.getSelectedObjects(AbstractNode.class))
773 dciConfig.copyObjects(o.getObjectId(), dciList);
774 for(AbstractObject o : dlg.getSelectedObjects(Template.class))
775 dciConfig.copyObjects(o.getObjectId(), dciList);
776 for(AbstractObject o : dlg.getSelectedObjects(Cluster.class))
777 dciConfig.copyObjects(o.getObjectId(), dciList);
778 for(AbstractObject o : dlg.getSelectedObjects(MobileDevice.class))
779 dciConfig.copyObjects(o.getObjectId(), dciList);
780 if (doMove)
781 {
782 for(long id : dciList)
783 dciConfig.deleteObject(id);
784 runInUIThread(new Runnable() {
785 @Override
786 public void run()
787 {
788 viewer.setInput(dciConfig.getItems());
789 }
790 });
791 }
792 }
793
794 @Override
795 protected String getErrorMessage()
796 {
797 return Messages.get().DataCollectionEditor_CopyJob_Error + object.getObjectName();
798 }
799 }.start();
800 }
801
802 /**
803 * Convert selected item(s) to template items
804 */
805 private void convertToTemplate()
806 {
807 final ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, ObjectSelectionDialog.createTemplateSelectionFilter());
808 dlg.showFilterToolTip(false);
809 if (dlg.open() != Window.OK)
810 return;
811
812 AbstractObject[] objects = dlg.getSelectedObjects(Template.class);
813 if (objects.length == 0)
814 return;
815 final Template template = (Template)objects[0];
816
817 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
818 Iterator<?> it = selection.iterator();
819 final long[] dciList = new long[selection.size()];
820 for(int i = 0; (i < dciList.length) && it.hasNext(); i++)
821 dciList[i] = ((DataCollectionObject)it.next()).getId();
822
823 new ConsoleJob(Messages.get().DataCollectionEditor_ConvertJob_TitlePrefix + object.getObjectName() + Messages.get().DataCollectionEditor_ConvertJob_TitleSuffix, this, Activator.PLUGIN_ID, null) {
824 @Override
825 protected void runInternal(IProgressMonitor monitor) throws Exception
826 {
827 monitor.beginTask(Messages.get().DataCollectionEditor_ConvertJob_TaskName, 4);
828
829 boolean needApply = true;
830 for(long id : template.getChildIdList())
831 {
832 if (id == dciConfig.getNodeId())
833 {
834 needApply = false;
835 break;
836 }
837 }
838 monitor.worked(1);
839
840 dciConfig.copyObjects(template.getObjectId(), dciList);
841 for(long id : dciList)
842 dciConfig.deleteObject(id);
843 dciConfig.close();
844 monitor.worked(1);
845
846 if (needApply)
847 {
848 boolean success = false;
849 int retries = 5;
850 do
851 {
852 try
853 {
854 session.applyTemplate(template.getObjectId(), dciConfig.getNodeId());
855 success = true;
856 }
857 catch(NXCException e)
858 {
859 if (e.getErrorCode() != RCC.COMPONENT_LOCKED)
860 throw e;
861 Thread.sleep(200);
862 }
863 retries--;
864 } while(!success && (retries > 0));
865 }
866 monitor.worked(1);
867
868 boolean success = false;
869 int retries = 5;
870 do
871 {
872 try
873 {
874 Thread.sleep(500);
875 dciConfig.open();
876 success = true;
877 }
878 catch(NXCException e)
879 {
880 if (e.getErrorCode() != RCC.COMPONENT_LOCKED)
881 throw e;
882 }
883 retries--;
884 } while(!success && (retries > 0));
885
886 runInUIThread(new Runnable() {
887 @Override
888 public void run()
889 {
890 viewer.setInput(dciConfig.getItems());
891 }
892 });
893 monitor.done();
894 }
895
896 @Override
897 protected String getErrorMessage()
898 {
899 return Messages.get().DataCollectionEditor_ConvertJob_ErrorPrefix + object.getObjectName() + Messages.get().DataCollectionEditor_ConvertJob_ErrorSuffix;
900 }
901 }.start();
902 }
903
904 /**
905 * Enable or disable filter
906 *
907 * @param enable New filter state
908 */
909 private void enableFilter(boolean enable)
910 {
911 initShowFilter = enable;
912 filterText.setVisible(initShowFilter);
913 FormData fd = (FormData)viewer.getTable().getLayoutData();
914 fd.top = enable ? new FormAttachment(filterText) : new FormAttachment(0, 0);
915 content.layout();
916 if (enable)
917 {
918 filterText.setFocus();
919 }
920 else
921 {
922 filterText.setText(""); //$NON-NLS-1$
923 onFilterModify();
924 }
925 }
926
927 /**
928 * Handler for filter modification
929 */
930 private void onFilterModify()
931 {
932 final String text = filterText.getText();
933 filter.setFilterString(text);
934 viewer.refresh(false);
935 }
936 }