769ad0b55d2aa1dae972007c98b25ef5868812d2
[public/netxms.git] / src / java / netxms-eclipse / NetworkMaps / src / org / netxms / ui / eclipse / networkmaps / views / PredefinedMap.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.networkmaps.views;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.ui.IViewSite;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.progress.UIJob;
37 import org.eclipse.zest.core.widgets.ZestStyles;
38 import org.netxms.client.NXCObjectModificationData;
39 import org.netxms.client.maps.NetworkMapLink;
40 import org.netxms.client.maps.elements.NetworkMapObject;
41 import org.netxms.client.objects.GenericObject;
42 import org.netxms.ui.eclipse.jobs.ConsoleJob;
43 import org.netxms.ui.eclipse.networkmaps.Activator;
44 import org.netxms.ui.eclipse.objectbrowser.dialogs.ObjectSelectionDialog;
45
46 /**
47 * View for predefined map
48 *
49 */
50 public class PredefinedMap extends NetworkMap
51 {
52 public static final String ID = "org.netxms.ui.eclipse.networkmaps.views.PredefinedMap";
53
54 private org.netxms.client.objects.NetworkMap mapObject;
55 private Action actionAddObject;
56 private Action actionLinkObjects;
57
58 /* (non-Javadoc)
59 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#init(org.eclipse.ui.IViewSite)
60 */
61 @Override
62 public void init(IViewSite site) throws PartInitException
63 {
64 super.init(site);
65 mapObject = (org.netxms.client.objects.NetworkMap)rootObject;
66 setPartName(rootObject.getObjectName());
67 }
68
69 /* (non-Javadoc)
70 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#createPartControl(org.eclipse.swt.widgets.Composite)
71 */
72 @Override
73 public void createPartControl(Composite parent)
74 {
75 super.createPartControl(parent);
76 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
77 @Override
78 public void selectionChanged(SelectionChangedEvent event)
79 {
80 actionLinkObjects.setEnabled(((IStructuredSelection)event.getSelection()).size() == 2);
81 }
82 });
83 }
84
85 /* (non-Javadoc)
86 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#buildMapPage()
87 */
88 @Override
89 protected void buildMapPage()
90 {
91 mapPage = ((org.netxms.client.objects.NetworkMap)rootObject).createMapPage();
92 }
93
94 /* (non-Javadoc)
95 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#createActions()
96 */
97 @Override
98 protected void createActions()
99 {
100 super.createActions();
101
102 actionAddObject = new Action("&Add object...") {
103 @Override
104 public void run()
105 {
106 addObjectToMap();
107 }
108 };
109 actionAddObject.setAccelerator(SWT.CTRL | 'A');
110
111 actionLinkObjects = new Action("&Link selected objects") {
112 @Override
113 public void run()
114 {
115 linkSelectedObjects();
116 }
117 };
118 actionLinkObjects.setAccelerator(SWT.CTRL | 'L');
119 actionLinkObjects.setImageDescriptor(Activator.getImageDescriptor("icons/link_add.png"));
120 }
121
122 /* (non-Javadoc)
123 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#fillMapContextMenu(org.eclipse.jface.action.IMenuManager)
124 */
125 @Override
126 protected void fillMapContextMenu(IMenuManager manager)
127 {
128 manager.add(actionAddObject);
129 manager.add(new Separator());
130 super.fillMapContextMenu(manager);
131 }
132
133 /* (non-Javadoc)
134 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#fillObjectContextMenu(org.eclipse.jface.action.IMenuManager)
135 */
136 @Override
137 protected void fillObjectContextMenu(IMenuManager manager)
138 {
139 if (((IStructuredSelection)viewer.getSelection()).size() == 2)
140 {
141 manager.add(actionLinkObjects);
142 manager.add(new Separator());
143 }
144 super.fillObjectContextMenu(manager);
145 }
146
147 /* (non-Javadoc)
148 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#fillLocalPullDown(org.eclipse.jface.action.IMenuManager)
149 */
150 @Override
151 protected void fillLocalPullDown(IMenuManager manager)
152 {
153 manager.add(actionAddObject);
154 manager.add(actionLinkObjects);
155 manager.add(new Separator());
156 super.fillLocalPullDown(manager);
157 }
158
159 /* (non-Javadoc)
160 * @see org.netxms.ui.eclipse.networkmaps.views.NetworkMap#fillLocalToolBar(org.eclipse.jface.action.IToolBarManager)
161 */
162 @Override
163 protected void fillLocalToolBar(IToolBarManager manager)
164 {
165 manager.add(actionLinkObjects);
166 manager.add(new Separator());
167 super.fillLocalToolBar(manager);
168 }
169
170 /**
171 * Add object to map
172 */
173 private void addObjectToMap()
174 {
175 ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, null);
176 if (dlg.open() != Window.OK)
177 return;
178
179 int added = 0;
180 for(GenericObject object : dlg.getSelectedObjects())
181 {
182 if (mapPage.findObjectElement(object.getObjectId()) == null)
183 {
184 mapPage.addElement(new NetworkMapObject(mapPage.createElementId(), object.getObjectId()));
185 added++;
186 }
187 }
188
189 if (added > 0)
190 saveMap();
191 }
192
193 /**
194 * Link currently selected objects
195 */
196 private void linkSelectedObjects()
197 {
198 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
199 if (selection.size() != 2)
200 return;
201
202 Object[] objects = selection.toArray();
203 long id1 = ((NetworkMapObject)objects[0]).getId();
204 long id2 = ((NetworkMapObject)objects[1]).getId();
205 if (!mapPage.areObjectsConnected(id1, id1))
206 {
207 mapPage.addLink(new NetworkMapLink(NetworkMapLink.NORMAL, id1, id2));
208 saveMap();
209 }
210 }
211
212 /**
213 * Save map on server
214 */
215 private void saveMap()
216 {
217 final NXCObjectModificationData md = new NXCObjectModificationData(rootObject.getObjectId());
218 md.setMapContent(mapPage.getElements(), mapPage.getLinks());
219 new ConsoleJob("Save map object " + rootObject.getObjectName(), this, Activator.PLUGIN_ID, Activator.PLUGIN_ID) {
220 @Override
221 protected void runInternal(IProgressMonitor monitor) throws Exception
222 {
223 session.modifyObject(md);
224 new UIJob("Refresh map view") {
225 @Override
226 public IStatus runInUIThread(IProgressMonitor monitor)
227 {
228 viewer.setInput(mapPage);
229 return Status.OK_STATUS;
230 }
231 };
232 }
233
234 @Override
235 protected String getErrorMessage()
236 {
237 return "Cannot update map content on server";
238 }
239 }.start();
240 }
241 }