8500402fe89ba0784a9e491c5679e85b0485dae4
[public/netxms.git] / webui / webapp / NetworkMaps / src / org / netxms / ui / eclipse / networkmaps / views / AbstractNetworkMapView.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 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.networkmaps.views;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IConfigurationElement;
31 import org.eclipse.core.runtime.IExtensionRegistry;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.draw2d.ManhattanConnectionRouter;
34 import org.eclipse.draw2d.geometry.Dimension;
35 import org.eclipse.draw2d.geometry.Point;
36 import org.eclipse.gef4.zest.core.viewers.AbstractZoomableViewer;
37 import org.eclipse.gef4.zest.core.viewers.IZoomableWorkbenchPart;
38 import org.eclipse.gef4.zest.core.widgets.Graph;
39 import org.eclipse.gef4.zest.core.widgets.GraphConnection;
40 import org.eclipse.gef4.zest.core.widgets.GraphNode;
41 import org.eclipse.gef4.zest.layouts.LayoutAlgorithm;
42 import org.eclipse.gef4.zest.layouts.algorithms.CompositeLayoutAlgorithm;
43 import org.eclipse.gef4.zest.layouts.algorithms.GridLayoutAlgorithm;
44 import org.eclipse.gef4.zest.layouts.algorithms.RadialLayoutAlgorithm;
45 import org.eclipse.gef4.zest.layouts.algorithms.SpringLayoutAlgorithm;
46 import org.eclipse.gef4.zest.layouts.algorithms.TreeLayoutAlgorithm;
47 import org.eclipse.jface.action.Action;
48 import org.eclipse.jface.action.GroupMarker;
49 import org.eclipse.jface.action.IContributionItem;
50 import org.eclipse.jface.action.IMenuListener;
51 import org.eclipse.jface.action.IMenuManager;
52 import org.eclipse.jface.action.IToolBarManager;
53 import org.eclipse.jface.action.MenuManager;
54 import org.eclipse.jface.action.Separator;
55 import org.eclipse.jface.commands.ActionHandler;
56 import org.eclipse.jface.dialogs.IDialogSettings;
57 import org.eclipse.jface.preference.IPreferenceStore;
58 import org.eclipse.jface.viewers.DoubleClickEvent;
59 import org.eclipse.jface.viewers.IDoubleClickListener;
60 import org.eclipse.jface.viewers.ISelection;
61 import org.eclipse.jface.viewers.ISelectionChangedListener;
62 import org.eclipse.jface.viewers.ISelectionProvider;
63 import org.eclipse.jface.viewers.IStructuredSelection;
64 import org.eclipse.jface.viewers.SelectionChangedEvent;
65 import org.eclipse.jface.viewers.StructuredSelection;
66 import org.eclipse.swt.SWT;
67 import org.eclipse.swt.events.DisposeEvent;
68 import org.eclipse.swt.events.DisposeListener;
69 import org.eclipse.swt.layout.FillLayout;
70 import org.eclipse.swt.widgets.Composite;
71 import org.eclipse.swt.widgets.Display;
72 import org.eclipse.swt.widgets.Menu;
73 import org.eclipse.ui.IActionBars;
74 import org.eclipse.ui.IViewSite;
75 import org.eclipse.ui.IWorkbenchActionConstants;
76 import org.eclipse.ui.IWorkbenchPage;
77 import org.eclipse.ui.PartInitException;
78 import org.eclipse.ui.PlatformUI;
79 import org.eclipse.ui.contexts.IContextService;
80 import org.eclipse.ui.handlers.IHandlerService;
81 import org.eclipse.ui.part.ViewPart;
82 import org.netxms.client.NXCSession;
83 import org.netxms.client.SessionListener;
84 import org.netxms.client.SessionNotification;
85 import org.netxms.client.maps.MapLayoutAlgorithm;
86 import org.netxms.client.maps.MapObjectDisplayMode;
87 import org.netxms.client.maps.NetworkMapLink;
88 import org.netxms.client.maps.NetworkMapPage;
89 import org.netxms.client.maps.configs.DCIImageConfiguration;
90 import org.netxms.client.maps.configs.SingleDciConfig;
91 import org.netxms.client.maps.elements.NetworkMapDCIContainer;
92 import org.netxms.client.maps.elements.NetworkMapDCIImage;
93 import org.netxms.client.maps.elements.NetworkMapElement;
94 import org.netxms.client.maps.elements.NetworkMapObject;
95 import org.netxms.client.objects.AbstractObject;
96 import org.netxms.client.objects.NetworkMap;
97 import org.netxms.ui.eclipse.actions.RefreshAction;
98 import org.netxms.ui.eclipse.console.resources.GroupMarkers;
99 import org.netxms.ui.eclipse.console.resources.SharedIcons;
100 import org.netxms.ui.eclipse.networkmaps.Activator;
101 import org.netxms.ui.eclipse.networkmaps.Messages;
102 import org.netxms.ui.eclipse.networkmaps.algorithms.ExpansionAlgorithm;
103 import org.netxms.ui.eclipse.networkmaps.algorithms.ManualLayout;
104 import org.netxms.ui.eclipse.networkmaps.api.ObjectDoubleClickHandler;
105 import org.netxms.ui.eclipse.networkmaps.views.helpers.BendpointEditor;
106 import org.netxms.ui.eclipse.networkmaps.views.helpers.ExtendedGraphViewer;
107 import org.netxms.ui.eclipse.networkmaps.views.helpers.LinkDciValueProvider;
108 import org.netxms.ui.eclipse.networkmaps.views.helpers.MapContentProvider;
109 import org.netxms.ui.eclipse.networkmaps.views.helpers.MapLabelProvider;
110 import org.netxms.ui.eclipse.objectbrowser.api.ObjectContextMenu;
111 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
112 import org.netxms.ui.eclipse.tools.FilteringMenuManager;
113 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
114
115 /**
116 * Base class for network map views
117 */
118 public abstract class AbstractNetworkMapView extends ViewPart implements ISelectionProvider, IZoomableWorkbenchPart
119 {
120 protected static final int LAYOUT_SPRING = 0;
121 protected static final int LAYOUT_RADIAL = 1;
122 protected static final int LAYOUT_HTREE = 2;
123 protected static final int LAYOUT_VTREE = 3;
124 protected static final int LAYOUT_SPARSE_VTREE = 4;
125
126 private final String[] layoutAlgorithmNames = {
127 Messages.get().AbstractNetworkMapView_LayoutSpring,
128 Messages.get().AbstractNetworkMapView_LayoutRadial,
129 Messages.get().AbstractNetworkMapView_LayoutHorzTree,
130 Messages.get().AbstractNetworkMapView_LayoutVertTree,
131 Messages.get().AbstractNetworkMapView_LayoutSparseVertTree
132 };
133 private final String[] connectionRouterNames = { Messages.get().AbstractNetworkMapView_RouterDirect, Messages.get().AbstractNetworkMapView_RouterManhattan };
134
135 private static final int SELECTION_EMPTY = 0;
136 private static final int SELECTION_MIXED = 1;
137 private static final int SELECTION_OBJECTS = 2;
138 private static final int SELECTION_ELEMENTS = 3;
139 private static final int SELECTION_LINKS = 4;
140
141 protected NXCSession session;
142 protected AbstractObject rootObject;
143 protected NetworkMapPage mapPage;
144 protected ExtendedGraphViewer viewer;
145 protected MapLabelProvider labelProvider;
146 protected MapLayoutAlgorithm layoutAlgorithm = MapLayoutAlgorithm.SPRING;
147 protected int routingAlgorithm = NetworkMapLink.ROUTING_DIRECT;
148 protected boolean allowManualLayout = false; // True if manual layout can be switched on
149 protected boolean automaticLayoutEnabled = true; // Current layout mode - automatic or manual
150 protected boolean alwaysFitLayout = false;
151 protected boolean disableGeolocationBackground = false;
152
153 protected Action actionRefresh;
154 protected Action actionShowStatusIcon;
155 protected Action actionShowStatusBackground;
156 protected Action actionShowStatusFrame;
157 protected Action actionZoomIn;
158 protected Action actionZoomOut;
159 protected Action actionZoomFit;
160 protected Action[] actionZoomTo;
161 protected Action[] actionSetAlgorithm;
162 protected Action[] actionSetRouter;
163 protected Action actionAlwaysFitLayout;
164 protected Action actionEnableAutomaticLayout;
165 protected Action actionSaveLayout;
166 protected Action actionOpenSubmap;
167 protected Action actionFiguresIcons;
168 protected Action actionFiguresSmallLabels;
169 protected Action actionFiguresLargeLabels;
170 protected Action actionFiguresStatusIcons;
171 protected Action actionShowGrid;
172 protected Action actionAlignToGrid;
173 protected Action actionSnapToGrid;
174 protected Action actionShowObjectDetails;
175 protected Action actionHideLinkLabels;
176 protected Action actionSelectAllObjects;
177
178 private String viewId;
179 private IStructuredSelection currentSelection = new StructuredSelection(new Object[0]);
180 private Set<ISelectionChangedListener> selectionListeners = new HashSet<ISelectionChangedListener>();
181 private BendpointEditor bendpointEditor = null;
182 private SessionListener sessionListener;
183 private List<DoubleClickHandlerData> doubleClickHandlers = new ArrayList<DoubleClickHandlerData>(0);
184 private LinkDciValueProvider dciValueProvider;
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
190 */
191 @Override
192 public void init(IViewSite site) throws PartInitException
193 {
194 super.init(site);
195
196 dciValueProvider = LinkDciValueProvider.getInstance();
197
198 final IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
199 disableGeolocationBackground = ps.getBoolean("DISABLE_GEOLOCATION_BACKGROUND"); //$NON-NLS-1$
200
201 session = (NXCSession)ConsoleSharedData.getSession();
202 String[] parts = site.getSecondaryId().split("&"); //$NON-NLS-1$
203 rootObject = session.findObjectById(Long.parseLong((parts.length > 0) ? parts[0] : site.getSecondaryId()));
204 if (rootObject == null)
205 throw new PartInitException(Messages.get().AbstractNetworkMapView_RootObjectNotFound);
206
207 viewId = site.getId() + "."; //$NON-NLS-1$
208 viewId += (parts.length > 0) ? parts[0] : site.getSecondaryId();
209
210 parseSecondaryId(parts);
211 }
212
213 /**
214 * Parse secondary ID (already splitted by & characters). Intended to be overrided in subclasses.
215 *
216 * @param parts secondary ID divided at & characters
217 * @throws PartInitException
218 */
219 protected void parseSecondaryId(String[] parts) throws PartInitException
220 {
221 }
222
223 /**
224 * Build map page containing data to display. Should be implemented in derived classes.
225 */
226 protected abstract void buildMapPage();
227
228 /*
229 * (non-Javadoc)
230 *
231 * @see org.eclipse.ui.part.ViewPart#createPartControl(org.eclipse.swt.widgets.Composite)
232 */
233 @Override
234 public final void createPartControl(Composite parent)
235 {
236 FillLayout layout = new FillLayout();
237 parent.setLayout(layout);
238
239 viewer = new ExtendedGraphViewer(parent, SWT.NONE);
240 viewer.setContentProvider(new MapContentProvider(viewer));
241 labelProvider = new MapLabelProvider(viewer);
242 viewer.setLabelProvider(labelProvider);
243 viewer.setBackgroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB());
244
245 IDialogSettings settings = Activator.getDefault().getDialogSettings();
246 try
247 {
248 alwaysFitLayout = settings.getBoolean(viewId + ".alwaysFitLayout");
249 labelProvider.setObjectFigureType(MapObjectDisplayMode.getByValue(settings.getInt(viewId + ".objectFigureType"))); //$NON-NLS-1$
250 }
251 catch(Exception e)
252 {
253 }
254
255 // Zoom level restore and save
256 try
257 {
258 viewer.zoomTo(settings.getDouble(viewId + ".zoom"));
259 }
260 catch(NumberFormatException e)
261 {
262 }
263 viewer.getGraphControl().addDisposeListener(new DisposeListener() {
264 @Override
265 public void widgetDisposed(DisposeEvent e)
266 {
267 IDialogSettings settings = Activator.getDefault().getDialogSettings();
268 settings.put(viewId + ".zoom", viewer.getZoom());
269 }
270 });
271
272 getSite().setSelectionProvider(this);
273
274 ISelectionChangedListener listener = new ISelectionChangedListener() {
275 @Override
276 public void selectionChanged(SelectionChangedEvent e)
277 {
278 if (bendpointEditor != null)
279 {
280 bendpointEditor.stop();
281 bendpointEditor = null;
282 }
283
284 currentSelection = transformSelection(e.getSelection());
285
286 if (currentSelection.size() == 1)
287 {
288 int selectionType = analyzeSelection(currentSelection);
289 if (selectionType == SELECTION_OBJECTS)
290 {
291 AbstractObject object = (AbstractObject)currentSelection.getFirstElement();
292 actionOpenSubmap.setEnabled(object.getSubmapId() != 0);
293 }
294 else
295 {
296 actionOpenSubmap.setEnabled(false);
297 if (selectionType == SELECTION_LINKS)
298 {
299 NetworkMapLink link = (NetworkMapLink)currentSelection.getFirstElement();
300 if (link.getRouting() == NetworkMapLink.ROUTING_BENDPOINTS)
301 {
302 bendpointEditor = new BendpointEditor(link,
303 (GraphConnection)viewer.getGraphControl().getSelection().get(0), viewer);
304 }
305 }
306 }
307 }
308 else
309 {
310 actionOpenSubmap.setEnabled(false);
311 }
312
313 if (selectionListeners.isEmpty())
314 return;
315
316 SelectionChangedEvent event = new SelectionChangedEvent(AbstractNetworkMapView.this, currentSelection);
317 for(ISelectionChangedListener l : selectionListeners)
318 {
319 l.selectionChanged(event);
320 }
321 }
322 };
323 viewer.addPostSelectionChangedListener(listener);
324
325 viewer.addDoubleClickListener(new IDoubleClickListener() {
326 @Override
327 public void doubleClick(DoubleClickEvent event)
328 {
329 int selectionType = analyzeSelection(currentSelection);
330 if (selectionType == SELECTION_OBJECTS)
331 {
332 AbstractObject object = (AbstractObject)currentSelection.getFirstElement();
333
334 for(DoubleClickHandlerData h : doubleClickHandlers)
335 {
336 if ((h.enabledFor == null) || (h.enabledFor.isInstance(object)))
337 {
338 if (h.handler.onDoubleClick(object))
339 {
340 return;
341 }
342 }
343 }
344 }
345
346 // Default behaviour
347 actionOpenSubmap.run();
348 }
349 });
350
351 sessionListener = new SessionListener() {
352 @Override
353 public void notificationHandler(final SessionNotification n)
354 {
355 if (n.getCode() == SessionNotification.OBJECT_CHANGED)
356 {
357 viewer.getControl().getDisplay().asyncExec(new Runnable() {
358 @Override
359 public void run()
360 {
361 onObjectChange((AbstractObject)n.getObject());
362 }
363 });
364 }
365 }
366 };
367 session.addListener(sessionListener);
368
369 createActions();
370 contributeToActionBars();
371 createPopupMenu();
372
373 if (automaticLayoutEnabled)
374 {
375 setLayoutAlgorithm(layoutAlgorithm, true);
376 }
377 else
378 {
379 viewer.setLayoutAlgorithm(new ManualLayout());
380 }
381
382 activateContext();
383 registerDoubleClickHandlers();
384 setupMapControl();
385 refreshMap();
386 }
387
388 /**
389 * Called from createPartControl to allow subclasses to do additional map setup. Subclasses
390 * should override this method instead of createPartControl. Default implementation do nothing.
391 */
392 protected void setupMapControl()
393 {
394 }
395
396 /**
397 * Activate context
398 */
399 private void activateContext()
400 {
401 IContextService contextService = (IContextService)getSite().getService(IContextService.class);
402 if (contextService != null)
403 {
404 contextService.activateContext("org.netxms.ui.eclipse.networkmaps.context.NetworkMaps"); //$NON-NLS-1$
405 }
406 }
407
408 /**
409 * Register double click handlers
410 */
411 private void registerDoubleClickHandlers()
412 {
413 // Read all registered extensions and create handlers
414 final IExtensionRegistry reg = Platform.getExtensionRegistry();
415 IConfigurationElement[] elements = reg
416 .getConfigurationElementsFor("org.netxms.ui.eclipse.networkmaps.objectDoubleClickHandlers"); //$NON-NLS-1$
417 for(int i = 0; i < elements.length; i++)
418 {
419 try
420 {
421 final DoubleClickHandlerData h = new DoubleClickHandlerData();
422 h.handler = (ObjectDoubleClickHandler)elements[i].createExecutableExtension("class"); //$NON-NLS-1$
423 h.priority = safeParseInt(elements[i].getAttribute("priority")); //$NON-NLS-1$
424 final String className = elements[i].getAttribute("enabledFor"); //$NON-NLS-1$
425 try
426 {
427 h.enabledFor = (className != null) ? Class.forName(className) : null;
428 }
429 catch(Exception e)
430 {
431 h.enabledFor = null;
432 }
433 doubleClickHandlers.add(h);
434 }
435 catch(CoreException e)
436 {
437 e.printStackTrace();
438 }
439 }
440
441 // Sort handlers by priority
442 Collections.sort(doubleClickHandlers, new Comparator<DoubleClickHandlerData>() {
443 @Override
444 public int compare(DoubleClickHandlerData arg0, DoubleClickHandlerData arg1)
445 {
446 return arg0.priority - arg1.priority;
447 }
448 });
449 }
450
451 /**
452 * Do full map refresh
453 */
454 protected void refreshMap()
455 {
456 buildMapPage();
457 viewer.setInput(mapPage);
458 viewer.setSelection(StructuredSelection.EMPTY);
459 }
460
461 /**
462 * Replace current map page with new one
463 *
464 * @param page new map page
465 */
466 protected void replaceMapPage(final NetworkMapPage page, Display display)
467 {
468 display.asyncExec(new Runnable() {
469 @Override
470 public void run()
471 {
472 mapPage = page;
473 addDciToRequestList();
474 viewer.setInput(mapPage);
475 }
476 });
477 }
478
479 /**
480 * Set layout algorithm for map
481 *
482 * @param alg Layout algorithm
483 * @param forceChange
484 */
485 protected void setLayoutAlgorithm(MapLayoutAlgorithm alg, boolean forceChange)
486 {
487 if (alg == MapLayoutAlgorithm.MANUAL)
488 {
489 if (!automaticLayoutEnabled)
490 return; // manual layout already
491
492 automaticLayoutEnabled = false;
493 // TODO: rewrite, enum value should not be used as index
494 actionSetAlgorithm[layoutAlgorithm.getValue()].setChecked(false);
495 actionEnableAutomaticLayout.setChecked(false);
496 return;
497 }
498
499 if (automaticLayoutEnabled && (alg == layoutAlgorithm) && !forceChange)
500 return; // nothing to change
501
502 if (!automaticLayoutEnabled)
503 {
504 actionEnableAutomaticLayout.setChecked(true);
505 automaticLayoutEnabled = true;
506 }
507
508 LayoutAlgorithm algorithm;
509
510 switch(alg)
511 {
512 case SPRING:
513 algorithm = new SpringLayoutAlgorithm();
514 break;
515 case RADIAL:
516 algorithm = new RadialLayoutAlgorithm();
517 break;
518 case HTREE:
519 algorithm = new TreeLayoutAlgorithm(TreeLayoutAlgorithm.LEFT_RIGHT);
520 break;
521 case VTREE:
522 algorithm = new TreeLayoutAlgorithm(TreeLayoutAlgorithm.TOP_DOWN);
523 break;
524 case SPARSE_VTREE:
525 algorithm = new TreeLayoutAlgorithm(TreeLayoutAlgorithm.TOP_DOWN);
526 ((TreeLayoutAlgorithm)algorithm).setNodeSpace(new Dimension(100, 100));
527 break;
528 default:
529 algorithm = new GridLayoutAlgorithm();
530 break;
531 }
532
533 viewer.setLayoutAlgorithm(alwaysFitLayout ? algorithm : new CompositeLayoutAlgorithm(new LayoutAlgorithm[] { algorithm, new ExpansionAlgorithm() }));
534
535 actionSetAlgorithm[layoutAlgorithm.getValue()].setChecked(false);
536 layoutAlgorithm = alg;
537 actionSetAlgorithm[layoutAlgorithm.getValue()].setChecked(true);
538 }
539
540 /**
541 * Update stored object positions with actual positions read from graph control
542 */
543 protected void updateObjectPositions()
544 {
545 Graph graph = viewer.getGraphControl();
546 List<?> nodes = graph.getNodes();
547 for(Object o : nodes)
548 {
549 if (o instanceof GraphNode)
550 {
551 Object data = ((GraphNode)o).getData();
552 if (data instanceof NetworkMapElement)
553 {
554 Point loc = ((GraphNode)o).getLocation();
555 Dimension size = ((GraphNode)o).getSize();
556 ((NetworkMapElement)data).setLocation(loc.x + (size.width + 1) / 2, loc.y + (size.height + 1) / 2);
557 }
558 }
559 }
560 }
561
562 /**
563 * Set manual layout mode
564 */
565 protected void setManualLayout()
566 {
567 updateObjectPositions();
568
569 automaticLayoutEnabled = false;
570 viewer.setLayoutAlgorithm(new ManualLayout(), true);
571
572 for(int i = 0; i < actionSetAlgorithm.length; i++)
573 actionSetAlgorithm[i].setEnabled(false);
574 actionSaveLayout.setEnabled(true);
575 }
576
577 /**
578 * Set automatic layout mode
579 */
580 protected void setAutomaticLayout()
581 {
582 automaticLayoutEnabled = true;
583 setLayoutAlgorithm(layoutAlgorithm, true);
584
585 for(int i = 0; i < actionSetAlgorithm.length; i++)
586 actionSetAlgorithm[i].setEnabled(true);
587 actionSaveLayout.setEnabled(false);
588 }
589
590 /**
591 * Create actions
592 */
593 protected void createActions()
594 {
595 final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
596
597 actionRefresh = new RefreshAction(this) {
598 @Override
599 public void run()
600 {
601 refreshMap();
602 }
603 };
604
605 actionShowStatusBackground = new Action(Messages.get().AbstractNetworkMapView_ShowStatusBkgnd, Action.AS_CHECK_BOX) {
606 @Override
607 public void run()
608 {
609 labelProvider.setShowStatusBackground(!labelProvider.isShowStatusBackground());
610 setChecked(labelProvider.isShowStatusBackground());
611 updateObjectPositions();
612 saveLayout();
613 viewer.refresh();
614 }
615 };
616 actionShowStatusBackground.setChecked(labelProvider.isShowStatusBackground());
617 actionShowStatusBackground.setEnabled(labelProvider.getObjectFigureType() == MapObjectDisplayMode.ICON);
618
619 actionShowStatusIcon = new Action(Messages.get().AbstractNetworkMapView_ShowStatusIcon, Action.AS_CHECK_BOX) {
620 @Override
621 public void run()
622 {
623 labelProvider.setShowStatusIcons(!labelProvider.isShowStatusIcons());
624 setChecked(labelProvider.isShowStatusIcons());
625 updateObjectPositions();
626 saveLayout();
627 viewer.refresh();
628 }
629 };
630 actionShowStatusIcon.setChecked(labelProvider.isShowStatusIcons());
631 actionShowStatusIcon.setEnabled(labelProvider.getObjectFigureType() == MapObjectDisplayMode.ICON);
632
633 actionShowStatusFrame = new Action(Messages.get().AbstractNetworkMapView_ShowStatusFrame, Action.AS_CHECK_BOX) {
634 @Override
635 public void run()
636 {
637 labelProvider.setShowStatusFrame(!labelProvider.isShowStatusFrame());
638 setChecked(labelProvider.isShowStatusFrame());
639 updateObjectPositions();
640 saveLayout();
641 viewer.refresh();
642 }
643 };
644 actionShowStatusFrame.setChecked(labelProvider.isShowStatusFrame());
645 actionShowStatusFrame.setEnabled(labelProvider.getObjectFigureType() == MapObjectDisplayMode.ICON);
646
647 actionZoomIn = new Action(Messages.get().AbstractNetworkMapView_ZoomIn, SharedIcons.ZOOM_IN) {
648 @Override
649 public void run()
650 {
651 viewer.zoomIn();
652 }
653 };
654 actionZoomIn.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.ZoomIn"); //$NON-NLS-1$
655 actionZoomIn.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.ZoomIn"); //$NON-NLS-1$
656 handlerService.activateHandler(actionZoomIn.getActionDefinitionId(), new ActionHandler(actionZoomIn));
657
658 actionZoomOut = new Action(Messages.get().AbstractNetworkMapView_ZoomOut, SharedIcons.ZOOM_OUT) {
659 @Override
660 public void run()
661 {
662 viewer.zoomOut();
663 }
664 };
665 actionZoomOut.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.ZoomOut"); //$NON-NLS-1$
666 actionZoomOut.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.ZoomOut"); //$NON-NLS-1$
667 handlerService.activateHandler(actionZoomOut.getActionDefinitionId(), new ActionHandler(actionZoomOut));
668
669 actionZoomFit = new Action(Messages.get().AbstractNetworkMapView_ZoomFit, Activator.getImageDescriptor("icons/fit.png")) {
670 @Override
671 public void run()
672 {
673 viewer.zoomFit();
674 }
675 };
676 actionZoomFit.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.ZoomToFit"); //$NON-NLS-1$
677 actionZoomFit.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.ZoomToFit"); //$NON-NLS-1$
678 handlerService.activateHandler(actionZoomFit.getActionDefinitionId(), new ActionHandler(actionZoomFit));
679
680 actionZoomTo = viewer.createZoomActions(handlerService);
681
682 actionSetAlgorithm = new Action[layoutAlgorithmNames.length];
683 for(int i = 0; i < layoutAlgorithmNames.length; i++)
684 {
685 final MapLayoutAlgorithm alg = MapLayoutAlgorithm.getByValue(i);
686 actionSetAlgorithm[i] = new Action(layoutAlgorithmNames[i], Action.AS_RADIO_BUTTON) {
687 @Override
688 public void run()
689 {
690 setLayoutAlgorithm(alg, true);
691 viewer.setInput(mapPage);
692 }
693 };
694 actionSetAlgorithm[i].setChecked(layoutAlgorithm.getValue() == i);
695 actionSetAlgorithm[i].setEnabled(automaticLayoutEnabled);
696 }
697
698 actionAlwaysFitLayout = new Action(Messages.get().AbstractNetworkMapView_AlwaysFitLayout, Action.AS_CHECK_BOX) {
699 @Override
700 public void run()
701 {
702 alwaysFitLayout = actionAlwaysFitLayout.isChecked();
703 setLayoutAlgorithm(layoutAlgorithm, true);
704 IDialogSettings settings = Activator.getDefault().getDialogSettings();
705 settings.put(viewId + ".alwaysFitLayout", alwaysFitLayout);
706 }
707 };
708 actionAlwaysFitLayout.setChecked(alwaysFitLayout);
709
710 actionSetRouter = new Action[connectionRouterNames.length];
711 for(int i = 0; i < connectionRouterNames.length; i++)
712 {
713 final int alg = i + 1;
714 actionSetRouter[i] = new Action(connectionRouterNames[i], Action.AS_RADIO_BUTTON) {
715 @Override
716 public void run()
717 {
718 setConnectionRouter(alg, true);
719 }
720 };
721 actionSetRouter[i].setChecked(routingAlgorithm == alg);
722 }
723
724 actionEnableAutomaticLayout = new Action(Messages.get().AbstractNetworkMapView_EnableAutoLayout, Action.AS_CHECK_BOX) {
725 @Override
726 public void run()
727 {
728 if (automaticLayoutEnabled)
729 {
730 setManualLayout();
731 }
732 else
733 {
734 setAutomaticLayout();
735 }
736 setChecked(automaticLayoutEnabled);
737 }
738 };
739 actionEnableAutomaticLayout.setChecked(automaticLayoutEnabled);
740
741 actionSaveLayout = new Action(Messages.get().AbstractNetworkMapView_SaveLayout) {
742 @Override
743 public void run()
744 {
745 updateObjectPositions();
746 saveLayout();
747 }
748 };
749 actionSaveLayout.setImageDescriptor(SharedIcons.SAVE);
750 actionSaveLayout.setEnabled(!automaticLayoutEnabled);
751
752 actionOpenSubmap = new Action(Messages.get().AbstractNetworkMapView_OpenSubmap) {
753 @Override
754 public void run()
755 {
756 openSubmap();
757 }
758 };
759 actionOpenSubmap.setEnabled(false);
760
761 actionFiguresIcons = new Action(Messages.get().AbstractNetworkMapView_Icons, Action.AS_RADIO_BUTTON) {
762 @Override
763 public void run()
764 {
765 setObjectDisplayMode(MapObjectDisplayMode.ICON, true);
766 }
767 };
768 actionFiguresIcons.setChecked(labelProvider.getObjectFigureType() == MapObjectDisplayMode.ICON);
769
770 actionFiguresSmallLabels = new Action(Messages.get().AbstractNetworkMapView_SmallLabels, Action.AS_RADIO_BUTTON) {
771 @Override
772 public void run()
773 {
774 setObjectDisplayMode(MapObjectDisplayMode.SMALL_LABEL, true);
775 }
776 };
777 actionFiguresSmallLabels.setChecked(labelProvider.getObjectFigureType() == MapObjectDisplayMode.SMALL_LABEL);
778
779 actionFiguresLargeLabels = new Action(Messages.get().AbstractNetworkMapView_LargeLabels, Action.AS_RADIO_BUTTON) {
780 @Override
781 public void run()
782 {
783 setObjectDisplayMode(MapObjectDisplayMode.LARGE_LABEL, true);
784 }
785 };
786 actionFiguresLargeLabels.setChecked(labelProvider.getObjectFigureType() == MapObjectDisplayMode.LARGE_LABEL);
787
788 actionFiguresStatusIcons = new Action(Messages.get().AbstractNetworkMapView_StatusIcons, Action.AS_RADIO_BUTTON) {
789 @Override
790 public void run()
791 {
792 setObjectDisplayMode(MapObjectDisplayMode.STATUS, true);
793 }
794 };
795 actionFiguresStatusIcons.setChecked(labelProvider.getObjectFigureType() == MapObjectDisplayMode.STATUS);
796
797 actionShowGrid = new Action(Messages.get().AbstractNetworkMapView_ShowGrid, Action.AS_CHECK_BOX) {
798 @Override
799 public void run()
800 {
801 viewer.showGrid(actionShowGrid.isChecked());
802 }
803 };
804 actionShowGrid.setImageDescriptor(Activator.getImageDescriptor("icons/grid.png")); //$NON-NLS-1$
805 actionShowGrid.setChecked(viewer.isGridVisible());
806 actionShowGrid.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.ShowGrid"); //$NON-NLS-1$
807 actionShowGrid.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.ShowGrid"); //$NON-NLS-1$
808 handlerService.activateHandler(actionShowGrid.getActionDefinitionId(), new ActionHandler(actionShowGrid));
809
810 actionSnapToGrid = new Action(Messages.get().AbstractNetworkMapView_SnapToGrid, Action.AS_CHECK_BOX) {
811 @Override
812 public void run()
813 {
814 viewer.setSnapToGrid(actionSnapToGrid.isChecked());
815 }
816 };
817 actionSnapToGrid.setImageDescriptor(Activator.getImageDescriptor("icons/snap_to_grid.png")); //$NON-NLS-1$
818 actionSnapToGrid.setChecked(viewer.isSnapToGrid());
819
820 actionAlignToGrid = new Action(Messages.get().AbstractNetworkMapView_AlignToGrid, Activator.getImageDescriptor("icons/align_to_grid.gif")) { //$NON-NLS-1$
821 @Override
822 public void run()
823 {
824 viewer.alignToGrid(false);
825 updateObjectPositions();
826 }
827 };
828 actionAlignToGrid.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.AlignToGrid"); //$NON-NLS-1$
829 actionAlignToGrid.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.AlignToGrid"); //$NON-NLS-1$
830 handlerService.activateHandler(actionAlignToGrid.getActionDefinitionId(), new ActionHandler(actionAlignToGrid));
831
832 actionShowObjectDetails = new Action(Messages.get().AbstractNetworkMapView_ShowObjDetails) {
833 @Override
834 public void run()
835 {
836 showObjectDetails();
837 }
838 };
839
840 actionHideLinkLabels = new Action(Messages.get().AbstractNetworkMapView_HideLinkLabels, Action.AS_CHECK_BOX) {
841 @Override
842 public void run()
843 {
844 labelProvider.setLabelHideStatus(actionHideLinkLabels.isChecked());
845 viewer.refresh(true);
846 }
847 };
848 actionHideLinkLabels.setImageDescriptor(Activator.getImageDescriptor("icons/hide_link.png")); //$NON-NLS-1$
849
850 actionSelectAllObjects = new Action("Select &all objects") {
851 @Override
852 public void run()
853 {
854 viewer.setSelection(new StructuredSelection(mapPage.getObjectElements()));
855 }
856 };
857 actionSelectAllObjects.setId("org.netxms.ui.eclipse.networkmaps.localActions.AbstractMap.SelectAllObjects"); //$NON-NLS-1$
858 actionSelectAllObjects.setActionDefinitionId("org.netxms.ui.eclipse.networkmaps.localCommands.AbstractMap.SelectAllObjects"); //$NON-NLS-1$
859 handlerService.activateHandler(actionSelectAllObjects.getActionDefinitionId(), new ActionHandler(actionSelectAllObjects));
860 }
861
862 /**
863 * Create "Layout" submenu
864 *
865 * @return
866 */
867 protected IContributionItem createLayoutSubmenu()
868 {
869 MenuManager layout = new MenuManager(Messages.get().AbstractNetworkMapView_Layout);
870 if (allowManualLayout)
871 {
872 layout.add(actionEnableAutomaticLayout);
873 }
874 layout.add(actionAlwaysFitLayout);
875 layout.add(new Separator());
876 for(int i = 0; i < actionSetAlgorithm.length; i++)
877 layout.add(actionSetAlgorithm[i]);
878 if (allowManualLayout)
879 {
880 layout.add(new Separator());
881 layout.add(actionSaveLayout);
882 }
883 return layout;
884 }
885
886 /**
887 * Create "Routing" submenu
888 *
889 * @return
890 */
891 protected IContributionItem createRoutingSubmenu()
892 {
893 MenuManager submenu = new MenuManager(Messages.get().AbstractNetworkMapView_Routing);
894 for(int i = 0; i < actionSetRouter.length; i++)
895 submenu.add(actionSetRouter[i]);
896 return submenu;
897 }
898
899 /**
900 * Fill action bars
901 */
902 private void contributeToActionBars()
903 {
904 IActionBars bars = getViewSite().getActionBars();
905 fillLocalPullDown(bars.getMenuManager());
906 fillLocalToolBar(bars.getToolBarManager());
907 }
908
909 /**
910 * Fill local pull-down menu
911 *
912 * @param manager
913 */
914 protected void fillLocalPullDown(IMenuManager manager)
915 {
916 MenuManager zoom = new MenuManager(Messages.get().AbstractNetworkMapView_Zoom);
917 for(int i = 0; i < actionZoomTo.length; i++)
918 zoom.add(actionZoomTo[i]);
919
920 MenuManager figureType = new MenuManager(Messages.get().AbstractNetworkMapView_DisplayObjectAs);
921 figureType.add(actionFiguresIcons);
922 figureType.add(actionFiguresSmallLabels);
923 figureType.add(actionFiguresLargeLabels);
924 figureType.add(actionFiguresStatusIcons);
925
926 manager.add(actionShowStatusBackground);
927 manager.add(actionShowStatusIcon);
928 manager.add(actionShowStatusFrame);
929 manager.add(new Separator());
930 manager.add(createLayoutSubmenu());
931 manager.add(createRoutingSubmenu());
932 manager.add(figureType);
933 manager.add(new Separator());
934 manager.add(actionZoomIn);
935 manager.add(actionZoomOut);
936 manager.add(actionZoomFit);
937 manager.add(zoom);
938 manager.add(new Separator());
939 manager.add(actionAlignToGrid);
940 manager.add(actionSnapToGrid);
941 manager.add(actionShowGrid);
942 manager.add(new Separator());
943 manager.add(actionHideLinkLabels);
944 manager.add(new Separator());
945 manager.add(actionSelectAllObjects);
946 manager.add(new Separator());
947 manager.add(actionRefresh);
948 }
949
950 /**
951 * Fill local tool bar
952 *
953 * @param manager
954 */
955 protected void fillLocalToolBar(IToolBarManager manager)
956 {
957 manager.add(actionZoomIn);
958 manager.add(actionZoomOut);
959 manager.add(actionZoomFit);
960 manager.add(new Separator());
961 manager.add(actionAlignToGrid);
962 manager.add(actionSnapToGrid);
963 manager.add(actionShowGrid);
964 manager.add(new Separator());
965 manager.add(actionHideLinkLabels);
966 manager.add(new Separator());
967 if (allowManualLayout)
968 {
969 manager.add(actionSaveLayout);
970 }
971 manager.add(actionRefresh);
972 }
973
974 /**
975 * Create popup menu for map
976 */
977 private void createPopupMenu()
978 {
979 // Create menu manager.
980 MenuManager menuMgr = new FilteringMenuManager(Activator.PLUGIN_ID);
981 menuMgr.setRemoveAllWhenShown(true);
982 menuMgr.addMenuListener(new IMenuListener() {
983 public void menuAboutToShow(IMenuManager manager)
984 {
985 int selType = analyzeSelection(currentSelection);
986 switch(selType)
987 {
988 case SELECTION_EMPTY:
989 fillMapContextMenu(manager);
990 break;
991 case SELECTION_OBJECTS:
992 fillObjectContextMenu(manager);
993 break;
994 case SELECTION_ELEMENTS:
995 fillElementContextMenu(manager);
996 break;
997 case SELECTION_LINKS:
998 fillLinkContextMenu(manager);
999 break;
1000 }
1001 }
1002 });
1003
1004 // Create menu.
1005 Menu menu = menuMgr.createContextMenu(viewer.getControl());
1006 viewer.getControl().setMenu(menu);
1007
1008 // Register menu for extension.
1009 getSite().registerContextMenu(menuMgr, this);
1010 }
1011
1012 /**
1013 * Fill context menu for map object
1014 *
1015 * @param manager Menu manager
1016 */
1017 protected void fillObjectContextMenu(IMenuManager manager)
1018 {
1019 manager.add(actionOpenSubmap);
1020 manager.add(new Separator());
1021 ObjectContextMenu.fill(manager, getSite(), this);
1022 if (currentSelection.size() == 1)
1023 {
1024 manager.insertAfter(GroupMarkers.MB_PROPERTIES, actionShowObjectDetails);
1025 }
1026 }
1027
1028 /**
1029 * Fill context menu for map element
1030 *
1031 * @param manager Menu manager
1032 */
1033 protected void fillElementContextMenu(IMenuManager manager)
1034 {
1035 }
1036
1037 /**
1038 * Fill context menu for link between objects
1039 *
1040 * @param manager Menu manager
1041 */
1042 protected void fillLinkContextMenu(IMenuManager manager)
1043 {
1044 }
1045
1046 /**
1047 * Fill context menu for map view
1048 *
1049 * @param manager Menu manager
1050 */
1051 protected void fillMapContextMenu(IMenuManager manager)
1052 {
1053 MenuManager zoom = new MenuManager(Messages.get().AbstractNetworkMapView_Zoom);
1054 for(int i = 0; i < actionZoomTo.length; i++)
1055 zoom.add(actionZoomTo[i]);
1056
1057 MenuManager figureType = new MenuManager(Messages.get().AbstractNetworkMapView_DisplayObjectAs);
1058 figureType.add(actionFiguresIcons);
1059 figureType.add(actionFiguresSmallLabels);
1060 figureType.add(actionFiguresLargeLabels);
1061 figureType.add(actionFiguresStatusIcons);
1062
1063 manager.add(actionShowStatusBackground);
1064 manager.add(actionShowStatusIcon);
1065 manager.add(actionShowStatusFrame);
1066 manager.add(new Separator());
1067 manager.add(createLayoutSubmenu());
1068 manager.add(createRoutingSubmenu());
1069 manager.add(figureType);
1070 manager.add(new Separator());
1071 manager.add(actionZoomIn);
1072 manager.add(actionZoomOut);
1073 manager.add(actionZoomFit);
1074 manager.add(zoom);
1075 manager.add(new Separator());
1076 manager.add(actionAlignToGrid);
1077 manager.add(actionSnapToGrid);
1078 manager.add(actionShowGrid);
1079 manager.add(new Separator());
1080 manager.add(actionHideLinkLabels);
1081 manager.add(new Separator());
1082 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
1083 manager.add(new Separator());
1084 manager.add(actionSelectAllObjects);
1085 manager.add(new Separator());
1086 manager.add(actionRefresh);
1087 }
1088
1089 /**
1090 * Tests if given selection contains only NetXMS objects
1091 *
1092 * @param selection
1093 * @return
1094 */
1095 @SuppressWarnings("rawtypes")
1096 private int analyzeSelection(IStructuredSelection selection)
1097 {
1098 if (selection.isEmpty())
1099 return SELECTION_EMPTY;
1100
1101 Iterator it = selection.iterator();
1102 Object first = it.next();
1103 int type;
1104 Class firstClass;
1105 if (first instanceof AbstractObject)
1106 {
1107 type = SELECTION_OBJECTS;
1108 firstClass = AbstractObject.class;
1109 }
1110 else if (first instanceof NetworkMapElement)
1111 {
1112 type = SELECTION_ELEMENTS;
1113 firstClass = NetworkMapElement.class;
1114 }
1115 else if (first instanceof NetworkMapLink)
1116 {
1117 type = SELECTION_LINKS;
1118 firstClass = NetworkMapLink.class;
1119 }
1120 else
1121 {
1122 return SELECTION_MIXED;
1123 }
1124
1125 while(it.hasNext())
1126 {
1127 final Object o = it.next();
1128 if (!firstClass.isInstance(o))
1129 return SELECTION_MIXED;
1130 }
1131 return type;
1132 }
1133
1134 /**
1135 * Called by session listener when NetXMS object was changed.
1136 *
1137 * @param object changed NetXMS object
1138 */
1139 protected void onObjectChange(final AbstractObject object)
1140 {
1141 NetworkMapObject element = mapPage.findObjectElement(object.getObjectId());
1142 if (element != null)
1143 viewer.refresh(element, true);
1144
1145 List<NetworkMapLink> links = mapPage.findLinksWithStatusObject(object.getObjectId());
1146 if (links != null)
1147 {
1148 for(NetworkMapLink l : links)
1149 viewer.refresh(l);
1150 addDciToRequestList();
1151 }
1152
1153 if (object.getObjectId() == rootObject.getObjectId())
1154 rootObject = object;
1155 }
1156
1157 /**
1158 * Called when map layout has to be saved. Object positions already updated when this method is called. Default implementation
1159 * does nothing.
1160 */
1161 protected void saveLayout()
1162 {
1163 }
1164
1165 /*
1166 * (non-Javadoc)
1167 *
1168 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
1169 */
1170 @Override
1171 public void setFocus()
1172 {
1173 viewer.getControl().setFocus();
1174 }
1175
1176 /*
1177 * (non-Javadoc)
1178 *
1179 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
1180 */
1181 @Override
1182 public void dispose()
1183 {
1184 if (sessionListener != null)
1185 session.removeListener(sessionListener);
1186 if (labelProvider != null)
1187 {
1188 IDialogSettings settings = Activator.getDefault().getDialogSettings();
1189 settings.put(viewId + ".objectFigureType", labelProvider.getObjectFigureType().ordinal()); //$NON-NLS-1$
1190 }
1191 removeDciFromRequestList();
1192 super.dispose();
1193 }
1194
1195 /*
1196 * (non-Javadoc)
1197 *
1198 * @see
1199 * org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1200 */
1201 @Override
1202 public void addSelectionChangedListener(ISelectionChangedListener listener)
1203 {
1204 selectionListeners.add(listener);
1205 }
1206
1207 /**
1208 * Transform viewer's selection to form usable by another plugins by extracting NetXMS objects from map elements.
1209 *
1210 * @param viewerSelection viewer's selection
1211 * @return selection containing only NetXMS objects
1212 */
1213 @SuppressWarnings("rawtypes")
1214 private IStructuredSelection transformSelection(ISelection viewerSelection)
1215 {
1216 IStructuredSelection selection = (IStructuredSelection)viewerSelection;
1217 if (selection.isEmpty())
1218 return selection;
1219
1220 List<Object> objects = new ArrayList<Object>();
1221 Iterator it = selection.iterator();
1222 while(it.hasNext())
1223 {
1224 Object element = it.next();
1225 if (element instanceof NetworkMapObject)
1226 {
1227 AbstractObject object = session.findObjectById(((NetworkMapObject)element).getObjectId());
1228 if (object != null)
1229 {
1230 objects.add(object);
1231 }
1232 else
1233 {
1234 // Fix for issue NX-24
1235 // If object not found, add map element to selection
1236 // This will allow removal of unknown objects from map
1237 objects.add(element);
1238 }
1239 }
1240 else if (isSelectableElement(element))
1241 {
1242 objects.add(element);
1243 }
1244 }
1245
1246 return new StructuredSelection(objects.toArray());
1247 }
1248
1249 /**
1250 * Tests if given map element is selectable. Default implementation always returns false.
1251 *
1252 * @param element element to test
1253 * @return true if given element is selectable
1254 */
1255 protected boolean isSelectableElement(Object element)
1256 {
1257 return false;
1258 }
1259
1260 /*
1261 * (non-Javadoc)
1262 *
1263 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
1264 */
1265 @Override
1266 public ISelection getSelection()
1267 {
1268 return currentSelection;
1269 }
1270
1271 /*
1272 * (non-Javadoc)
1273 *
1274 * @see
1275 * org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener
1276 * )
1277 */
1278 @Override
1279 public void removeSelectionChangedListener(ISelectionChangedListener listener)
1280 {
1281 selectionListeners.remove(listener);
1282 }
1283
1284 /*
1285 * (non-Javadoc)
1286 *
1287 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
1288 */
1289 @Override
1290 public void setSelection(ISelection selection)
1291 {
1292 }
1293
1294 /*
1295 * (non-Javadoc)
1296 *
1297 * @see org.eclipse.gef4.zest.core.viewers.IZoomableWorkbenchPart#getZoomableViewer()
1298 */
1299 @Override
1300 public AbstractZoomableViewer getZoomableViewer()
1301 {
1302 return viewer;
1303 }
1304
1305 /**
1306 * Open submap for currently selected object
1307 */
1308 private void openSubmap()
1309 {
1310 if (currentSelection == null)
1311 return;
1312
1313 Object object = currentSelection.getFirstElement();
1314 if (object instanceof AbstractObject)
1315 {
1316 long submapId = (object instanceof NetworkMap) ? ((AbstractObject)object).getObjectId() : ((AbstractObject)object).getSubmapId();
1317 if (submapId != 0)
1318 {
1319 try
1320 {
1321 getSite().getPage().showView(PredefinedMap.ID, Long.toString(submapId), IWorkbenchPage.VIEW_ACTIVATE);
1322 }
1323 catch(PartInitException e)
1324 {
1325 MessageDialogHelper.openError(getSite().getShell(), Messages.get().AbstractNetworkMapView_Error, String.format(Messages.get().AbstractNetworkMapView_OpenSubmapError, e.getMessage()));
1326 }
1327 }
1328 }
1329 }
1330
1331 /**
1332 * Set map default connection routing algorithm
1333 *
1334 * @param routingAlgorithm
1335 */
1336 public void setConnectionRouter(int routingAlgorithm, boolean doSave)
1337 {
1338 switch(routingAlgorithm)
1339 {
1340 case NetworkMapLink.ROUTING_MANHATTAN:
1341 this.routingAlgorithm = NetworkMapLink.ROUTING_MANHATTAN;
1342 viewer.getGraphControl().setRouter(new ManhattanConnectionRouter());
1343 break;
1344 default:
1345 this.routingAlgorithm = NetworkMapLink.ROUTING_DIRECT;
1346 viewer.getGraphControl().setRouter(null);
1347 break;
1348 }
1349 for(int i = 0; i < actionSetRouter.length; i++)
1350 actionSetRouter[i].setChecked(routingAlgorithm == (i + 1));
1351 if (doSave)
1352 {
1353 updateObjectPositions();
1354 saveLayout();
1355 }
1356 viewer.refresh();
1357 }
1358
1359 /**
1360 * Show details for selected object
1361 */
1362 private void showObjectDetails()
1363 {
1364 if ((currentSelection.size() != 1) || !(currentSelection.getFirstElement() instanceof AbstractObject))
1365 return;
1366
1367 AbstractObject object = (AbstractObject)currentSelection.getFirstElement();
1368 if (object != null)
1369 {
1370 try
1371 {
1372 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
1373 .showView("org.netxms.ui.eclipse.objectview.view.tabbed_object_view"); //$NON-NLS-1$
1374
1375 if (!selectionListeners.isEmpty())
1376 {
1377 SelectionChangedEvent event = new SelectionChangedEvent(AbstractNetworkMapView.this, currentSelection);
1378 for(ISelectionChangedListener l : selectionListeners)
1379 {
1380 l.selectionChanged(event);
1381 }
1382 }
1383 }
1384 catch(PartInitException e)
1385 {
1386 MessageDialogHelper.openError(getSite().getShell(), Messages.get().AbstractNetworkMapView_Error,
1387 String.format(Messages.get().AbstractNetworkMapView_OpenObjDetailsError, e.getLocalizedMessage()));
1388 }
1389 }
1390 }
1391
1392 /**
1393 * @param string
1394 * @return
1395 */
1396 private static int safeParseInt(String string)
1397 {
1398 if (string == null)
1399 return 65535;
1400 try
1401 {
1402 return Integer.parseInt(string);
1403 }
1404 catch(NumberFormatException e)
1405 {
1406 return 65535;
1407 }
1408 }
1409
1410 /**
1411 * Internal data for object double click handlers
1412 */
1413 private class DoubleClickHandlerData
1414 {
1415 ObjectDoubleClickHandler handler;
1416 int priority;
1417 Class<?> enabledFor;
1418 }
1419
1420 /**
1421 * Goes thought all links and trys to add to request list required DCIs.
1422 */
1423 protected void addDciToRequestList()
1424 {
1425 Collection<NetworkMapLink> linkList = mapPage.getLinks();
1426 for (NetworkMapLink item : linkList)
1427 {
1428 if(item.hasDciData())
1429 {
1430 for (SingleDciConfig value :item.getDciAsList())
1431 {
1432 if(value.type == SingleDciConfig.ITEM)
1433 {
1434 dciValueProvider.addDci(value.getNodeId(), value.dciId, mapPage);
1435 }
1436 else
1437 {
1438 dciValueProvider.addDci(value.getNodeId(), value.dciId, value.column, value.instance, mapPage);
1439 }
1440 }
1441 }
1442 }
1443 Collection<NetworkMapElement> mapElements = mapPage.getElements();
1444 for (NetworkMapElement element : mapElements)
1445 {
1446 if(element instanceof NetworkMapDCIContainer)
1447 {
1448 NetworkMapDCIContainer item = (NetworkMapDCIContainer)element;
1449 if(item.hasDciData())
1450 {
1451 for (SingleDciConfig value : item.getObjectDCIArray())
1452 {
1453 if(value.type == SingleDciConfig.ITEM)
1454 {
1455 dciValueProvider.addDci(value.getNodeId(), value.dciId, mapPage);
1456 }
1457 else
1458 {
1459 dciValueProvider.addDci(value.getNodeId(), value.dciId, value.column, value.instance, mapPage);
1460 }
1461 }
1462 }
1463 }
1464
1465 if(element instanceof NetworkMapDCIImage)
1466 {
1467 NetworkMapDCIImage item = (NetworkMapDCIImage)element;
1468 DCIImageConfiguration config = item.getImageOptions();
1469 SingleDciConfig value = config.getDci();
1470 if(value.type == SingleDciConfig.ITEM)
1471 {
1472 dciValueProvider.addDci(value.getNodeId(), value.dciId, mapPage);
1473 }
1474 else
1475 {
1476 dciValueProvider.addDci(value.getNodeId(), value.dciId, value.column, value.instance, mapPage);
1477 }
1478 }
1479 }
1480 }
1481
1482 /**
1483 * Removes DCIs from request list
1484 */
1485 protected void removeDciFromRequestList()
1486 {
1487 Collection<NetworkMapLink> linkList = mapPage.getLinks();
1488 for (NetworkMapLink item : linkList)
1489 {
1490 if(item.hasDciData())
1491 {
1492 dciValueProvider.removeDcis(mapPage);
1493 }
1494 }
1495 }
1496
1497 /**
1498 * @param mode
1499 * @param saveLayout
1500 */
1501 protected void setObjectDisplayMode(MapObjectDisplayMode mode, boolean saveLayout)
1502 {
1503 labelProvider.setObjectFigureType(mode);
1504 if (saveLayout)
1505 {
1506 updateObjectPositions();
1507 saveLayout();
1508 }
1509 viewer.refresh(true);
1510 actionShowStatusBackground.setEnabled(mode == MapObjectDisplayMode.ICON);
1511 actionShowStatusFrame.setEnabled(mode == MapObjectDisplayMode.ICON);
1512 actionShowStatusIcon.setEnabled(mode == MapObjectDisplayMode.ICON);
1513 }
1514 }