40d94e9e66c3b3830ead60827b1a04042d839b49
[public/netxms.git] / webui / webapp / OSM / src / org / netxms / ui / eclipse / osm / views / HistoryView.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.osm.views;
20
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.IMenuListener;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.action.MenuManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Menu;
32 import org.eclipse.ui.IActionBars;
33 import org.eclipse.ui.IViewSite;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.part.ViewPart;
36 import org.netxms.base.GeoLocation;
37 import org.netxms.client.NXCSession;
38 import org.netxms.client.datacollection.GraphSettings;
39 import org.netxms.client.objects.AbstractObject;
40 import org.netxms.ui.eclipse.console.resources.SharedIcons;
41 import org.netxms.ui.eclipse.osm.Messages;
42 import org.netxms.ui.eclipse.osm.dialogs.TimeSelectionDialog;
43 import org.netxms.ui.eclipse.osm.tools.MapAccessor;
44 import org.netxms.ui.eclipse.osm.widgets.GeoMapViewer;
45 import org.netxms.ui.eclipse.osm.widgets.helpers.GeoMapListener;
46 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
47
48 /**
49 * Base class for all geographical views
50 */
51 public class HistoryView extends ViewPart
52 {
53 public static final String ID = "org.netxms.ui.eclipse.osm.views.HistoryView"; //$NON-NLS-1$
54 public static final String JOB_FAMILY = "MapViewJob"; //$NON-NLS-1$
55 private static final int[] presetUnits = { GraphSettings.TIME_UNIT_MINUTE, GraphSettings.TIME_UNIT_MINUTE,
56 GraphSettings.TIME_UNIT_HOUR, GraphSettings.TIME_UNIT_HOUR, GraphSettings.TIME_UNIT_HOUR, GraphSettings.TIME_UNIT_HOUR,
57 GraphSettings.TIME_UNIT_DAY, GraphSettings.TIME_UNIT_DAY, GraphSettings.TIME_UNIT_DAY, GraphSettings.TIME_UNIT_DAY,
58 GraphSettings.TIME_UNIT_DAY, GraphSettings.TIME_UNIT_DAY };
59 private static final int[] presetRanges = { 10, 30, 1, 2, 4, 12, 1, 2, 5, 7, 31, 365 };
60 private static final String[] presetNames =
61 { Messages.get().HistoryView_Preset10min, Messages.get().HistoryView_Preset30min, Messages.get().HistoryView_Preset1hour, Messages.get().HistoryView_Preset2hours, Messages.get().HistoryView_Preset4hours, Messages.get().HistoryView_Preset12hours, Messages.get().HistoryView_Preset1day,
62 Messages.get().HistoryView_Preset2days, Messages.get().HistoryView_Preset5days, Messages.get().HistoryView_Preset1week, Messages.get().HistoryView_Preset1month,Messages.get().HistoryView_Preset1year };
63
64
65 protected GeoMapViewer map;
66
67 private MapAccessor mapAccessor;
68 private int zoomLevel = 15;
69 private Action actionZoomIn;
70 private Action actionZoomOut;
71 private Action[] presetActions;
72 private Action setConfigurableTime;
73 private AbstractObject object;
74
75 /**
76 * Get initial center point for displayed map
77 *
78 * @return
79 */
80 protected GeoLocation getInitialCenterPoint()
81 {
82 return object.getGeolocation();
83 }
84
85 /**
86 * Get initial zoom level
87 *
88 * @return
89 */
90 protected int getInitialZoomLevel()
91 {
92 return 15;
93 }
94
95 /* (non-Javadoc)
96 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
97 */
98 @Override
99 public void init(IViewSite site) throws PartInitException
100 {
101 super.init(site);
102
103 // Initiate loading of required plugins if they was not loaded yet
104 try
105 {
106 Platform.getAdapterManager().loadAdapter(((NXCSession)ConsoleSharedData.getSession()).getTopLevelObjects()[0], "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
107 }
108 catch(Exception e)
109 {
110 }
111
112 try
113 {
114 long id = Long.parseLong(site.getSecondaryId());
115 object = ((NXCSession)ConsoleSharedData.getSession()).findObjectById(id);
116 setPartName(Messages.get().LocationMap_PartNamePrefix + object.getObjectName());
117 }
118 catch(Exception e)
119 {
120 throw new PartInitException(Messages.get().LocationMap_InitError1, e);
121 }
122 if (object == null)
123 throw new PartInitException(Messages.get().LocationMap_InitError2);
124 }
125
126 /* (non-Javadoc)
127 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
128 */
129 @Override
130 public void createPartControl(Composite parent)
131 {
132 // Map control
133 map = new GeoMapViewer(parent, SWT.BORDER, true, object);
134 map.setViewPart(this);
135
136 createActions(parent);
137 contributeToActionBars();
138 createPopupMenu();
139
140 // Initial map view
141 mapAccessor = new MapAccessor(getInitialCenterPoint());
142 zoomLevel = getInitialZoomLevel();
143 mapAccessor.setZoom(zoomLevel);
144 map.showMap(mapAccessor);
145
146 map.addMapListener(new GeoMapListener() {
147 @Override
148 public void onZoom(int zoomLevel)
149 {
150 HistoryView.this.zoomLevel = zoomLevel;
151 mapAccessor.setZoom(zoomLevel);
152 actionZoomIn.setEnabled(zoomLevel < 18);
153 actionZoomOut.setEnabled(zoomLevel > 0);
154 }
155
156 @Override
157 public void onPan(GeoLocation centerPoint)
158 {
159 mapAccessor.setLatitude(centerPoint.getLatitude());
160 mapAccessor.setLongitude(centerPoint.getLongitude());
161 }
162 });
163 }
164
165 /**
166 * Create actions
167 */
168 protected void createActions(final Composite parent)
169 {
170 actionZoomIn = new Action(Messages.get().AbstractGeolocationView_ZoomIn) {
171 @Override
172 public void run()
173 {
174 setZoomLevel(zoomLevel + 1);
175 }
176 };
177 actionZoomIn.setImageDescriptor(SharedIcons.ZOOM_IN);
178
179 actionZoomOut = new Action(Messages.get().AbstractGeolocationView_ZoomOut) {
180 @Override
181 public void run()
182 {
183 setZoomLevel(zoomLevel - 1);
184 }
185 };
186 actionZoomOut.setImageDescriptor(SharedIcons.ZOOM_OUT);
187
188 presetActions = new Action[presetRanges.length];
189 for(int i = 0; i < presetRanges.length; i++)
190 {
191 final Integer presetIndex = i;
192 presetActions[i] = new Action(presetNames[i]) {
193 @Override
194 public void run()
195 {
196 map.changeTimePeriod(presetRanges[presetIndex], presetUnits[presetIndex]);
197 }
198 };
199 }
200
201 setConfigurableTime = new Action(Messages.get().HistoryView_CustomTimeFrame) {
202 @Override
203 public void run()
204 {
205 TimeSelectionDialog dialog = new TimeSelectionDialog(parent.getShell(), map.getTimePeriod());
206 int result = dialog.open();
207 if (result == Window.CANCEL)
208 return;
209 map.setTimePeriod(dialog.getTimePeriod());
210 }
211 };
212 }
213
214 /**
215 * Contribute actions to action bar
216 */
217 private void contributeToActionBars()
218 {
219 IActionBars bars = getViewSite().getActionBars();
220 fillLocalPullDown(bars.getMenuManager());
221 fillLocalToolBar(bars.getToolBarManager());
222 }
223
224 /**
225 * Fill local pull-down menu
226 *
227 * @param manager Menu manager for pull-down menu
228 */
229 protected void fillLocalPullDown(IMenuManager manager)
230 {
231 MenuManager presets = new MenuManager(Messages.get().HistoryView_Presets);
232 for(int i = 0; i < presetActions.length; i++)
233 presets.add(presetActions[i]);
234
235 manager.add(presets);
236 manager.add(new Separator());
237 manager.add(setConfigurableTime);
238 manager.add(new Separator());
239 manager.add(actionZoomIn);
240 manager.add(actionZoomOut);
241 }
242
243 /**
244 * Fill local tool bar
245 *
246 * @param manager Menu manager for local toolbar
247 */
248 protected void fillLocalToolBar(IToolBarManager manager)
249 {
250 manager.add(actionZoomIn);
251 manager.add(actionZoomOut);
252 }
253
254 /**
255 * Create pop-up menu for variable list
256 */
257 private void createPopupMenu()
258 {
259 // Create menu manager.
260 MenuManager menuMgr = new MenuManager();
261 menuMgr.setRemoveAllWhenShown(true);
262 menuMgr.addMenuListener(new IMenuListener() {
263 public void menuAboutToShow(IMenuManager mgr)
264 {
265 fillContextMenu(mgr);
266 }
267 });
268
269 // Create menu.
270 Menu menu = menuMgr.createContextMenu(map);
271 map.setMenu(menu);
272 }
273
274 /**
275 * Fill context menu
276 *
277 * @param manager
278 * Menu manager
279 */
280 protected void fillContextMenu(final IMenuManager manager)
281 {
282 MenuManager presets = new MenuManager(Messages.get().HistoryView_Presets);
283 for(int i = 0; i < presetActions.length; i++)
284 presets.add(presetActions[i]);
285
286 manager.add(presets);
287 manager.add(new Separator());
288 manager.add(setConfigurableTime);
289 manager.add(new Separator());
290 manager.add(actionZoomIn);
291 manager.add(actionZoomOut);
292 }
293
294 /* (non-Javadoc)
295 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
296 */
297 @Override
298 public void setFocus()
299 {
300 map.setFocus();
301 }
302
303 /**
304 * Set new zoom level for map
305 *
306 * @param newLevel new zoom level
307 */
308 private void setZoomLevel(int newLevel)
309 {
310 if ((newLevel < 0) || (newLevel > 18))
311 return;
312
313 zoomLevel = newLevel;
314 mapAccessor.setZoom(zoomLevel);
315 map.showMap(mapAccessor);
316
317 actionZoomIn.setEnabled(zoomLevel < 18);
318 actionZoomOut.setEnabled(zoomLevel > 0);
319 }
320
321 /**
322 * @return the mapAccessor
323 */
324 protected MapAccessor getMapAccessor()
325 {
326 return mapAccessor;
327 }
328 }