c6d98efec431d235b7788eb4c7b1fc982b5bff4d
[public/netxms.git] / src / java / netxms-eclipse / OSM / src / org / netxms / ui / eclipse / osm / views / AbstractGeolocationView.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 java.util.HashSet;
22 import java.util.Set;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.action.Action;
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.viewers.ISelection;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.ISelectionProvider;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.ui.IActionBars;
37 import org.eclipse.ui.IViewSite;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.part.ViewPart;
40 import org.netxms.base.GeoLocation;
41 import org.netxms.client.NXCSession;
42 import org.netxms.client.objects.AbstractObject;
43 import org.netxms.ui.eclipse.console.resources.SharedIcons;
44 import org.netxms.ui.eclipse.objectbrowser.api.ObjectContextMenu;
45 import org.netxms.ui.eclipse.osm.Messages;
46 import org.netxms.ui.eclipse.osm.tools.MapAccessor;
47 import org.netxms.ui.eclipse.osm.widgets.GeoMapViewer;
48 import org.netxms.ui.eclipse.osm.widgets.helpers.GeoMapListener;
49 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
50
51 /**
52 * Base class for all geographical views
53 */
54 public abstract class AbstractGeolocationView extends ViewPart implements ISelectionProvider
55 {
56 public static final String JOB_FAMILY = "MapViewJob"; //$NON-NLS-1$
57
58 protected GeoMapViewer map;
59
60 private MapAccessor mapAccessor;
61 private int zoomLevel = 15;
62 private Action actionZoomIn;
63 private Action actionZoomOut;
64 private ISelection selection;
65 private Set<ISelectionChangedListener> selectionChangeListeners = new HashSet<ISelectionChangedListener>();
66
67 /**
68 * Get initial center point for displayed map
69 *
70 * @return
71 */
72 protected abstract GeoLocation getInitialCenterPoint();
73
74 /**
75 * Get initial zoom level
76 *
77 * @return
78 */
79 protected abstract int getInitialZoomLevel();
80
81 /* (non-Javadoc)
82 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
83 */
84 @Override
85 public void init(IViewSite site) throws PartInitException
86 {
87 super.init(site);
88
89 // Initiate loading of required plugins if they was not loaded yet
90 try
91 {
92 Platform.getAdapterManager().loadAdapter(((NXCSession)ConsoleSharedData.getSession()).getTopLevelObjects()[0], "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
93 }
94 catch(Exception e)
95 {
96 }
97 }
98
99 /* (non-Javadoc)
100 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
101 */
102 @Override
103 public void createPartControl(Composite parent)
104 {
105 // Map control
106 map = new GeoMapViewer(parent, SWT.BORDER, false, null);
107 map.setViewPart(this);
108
109 createActions();
110 contributeToActionBars();
111 createPopupMenu();
112
113 // Initial map view
114 mapAccessor = new MapAccessor(getInitialCenterPoint());
115 zoomLevel = getInitialZoomLevel();
116 mapAccessor.setZoom(zoomLevel);
117 map.showMap(mapAccessor);
118
119 map.addMapListener(new GeoMapListener() {
120 @Override
121 public void onZoom(int zoomLevel)
122 {
123 AbstractGeolocationView.this.zoomLevel = zoomLevel;
124 mapAccessor.setZoom(zoomLevel);
125 actionZoomIn.setEnabled(zoomLevel < 18);
126 actionZoomOut.setEnabled(zoomLevel > 0);
127 }
128
129 @Override
130 public void onPan(GeoLocation centerPoint)
131 {
132 mapAccessor.setLatitude(centerPoint.getLatitude());
133 mapAccessor.setLongitude(centerPoint.getLongitude());
134 }
135 });
136
137 getSite().setSelectionProvider(this);
138 }
139
140 /**
141 * Create actions
142 */
143 protected void createActions()
144 {
145 actionZoomIn = new Action(Messages.get().AbstractGeolocationView_ZoomIn) {
146 @Override
147 public void run()
148 {
149 setZoomLevel(zoomLevel + 1);
150 }
151 };
152 actionZoomIn.setImageDescriptor(SharedIcons.ZOOM_IN);
153
154 actionZoomOut = new Action(Messages.get().AbstractGeolocationView_ZoomOut) {
155 @Override
156 public void run()
157 {
158 setZoomLevel(zoomLevel - 1);
159 }
160 };
161 actionZoomOut.setImageDescriptor(SharedIcons.ZOOM_OUT);
162 }
163
164 /**
165 * Contribute actions to action bar
166 */
167 private void contributeToActionBars()
168 {
169 IActionBars bars = getViewSite().getActionBars();
170 fillLocalPullDown(bars.getMenuManager());
171 fillLocalToolBar(bars.getToolBarManager());
172 }
173
174 /**
175 * Fill local pull-down menu
176 *
177 * @param manager Menu manager for pull-down menu
178 */
179 protected void fillLocalPullDown(IMenuManager manager)
180 {
181 manager.add(actionZoomIn);
182 manager.add(actionZoomOut);
183 }
184
185 /**
186 * Fill local tool bar
187 *
188 * @param manager Menu manager for local toolbar
189 */
190 protected void fillLocalToolBar(IToolBarManager manager)
191 {
192 manager.add(actionZoomIn);
193 manager.add(actionZoomOut);
194 }
195
196 /**
197 * Create pop-up menu for variable list
198 */
199 private void createPopupMenu()
200 {
201 // Create menu manager.
202 MenuManager menuMgr = new MenuManager();
203 menuMgr.setRemoveAllWhenShown(true);
204 menuMgr.addMenuListener(new IMenuListener() {
205 public void menuAboutToShow(IMenuManager mgr)
206 {
207 fillContextMenu(mgr);
208 }
209 });
210
211 // Create menu.
212 Menu menu = menuMgr.createContextMenu(map);
213 map.setMenu(menu);
214
215 // Register menu for extension.
216 getSite().registerContextMenu(menuMgr, this);
217 }
218
219 /**
220 * Fill context menu
221 *
222 * @param manager
223 * Menu manager
224 */
225 protected void fillContextMenu(final IMenuManager manager)
226 {
227 AbstractObject object = map.getObjectAtPoint(map.getCurrentPoint());
228 selection = (object != null) ? new StructuredSelection(object) : new StructuredSelection();
229 if (!selection.isEmpty())
230 {
231 ObjectContextMenu.fill(manager, getSite(), this);
232 }
233 else
234 {
235 manager.add(actionZoomIn);
236 manager.add(actionZoomOut);
237 }
238 }
239
240 /* (non-Javadoc)
241 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
242 */
243 @Override
244 public void setFocus()
245 {
246 map.setFocus();
247 }
248
249 /**
250 * Set new zoom level for map
251 *
252 * @param newLevel new zoom level
253 */
254 private void setZoomLevel(int newLevel)
255 {
256 if ((newLevel < 0) || (newLevel > 18))
257 return;
258
259 zoomLevel = newLevel;
260 mapAccessor.setZoom(zoomLevel);
261 map.showMap(mapAccessor);
262
263 actionZoomIn.setEnabled(zoomLevel < 18);
264 actionZoomOut.setEnabled(zoomLevel > 0);
265 }
266
267 /**
268 * @return the mapAccessor
269 */
270 protected MapAccessor getMapAccessor()
271 {
272 return mapAccessor;
273 }
274
275 /* (non-Javadoc)
276 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
277 */
278 @Override
279 public void addSelectionChangedListener(ISelectionChangedListener listener)
280 {
281 selectionChangeListeners.add(listener);
282 }
283
284 /* (non-Javadoc)
285 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
286 */
287 @Override
288 public void removeSelectionChangedListener(ISelectionChangedListener listener)
289 {
290 selectionChangeListeners.remove(listener);
291 }
292
293 /* (non-Javadoc)
294 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
295 */
296 @Override
297 public ISelection getSelection()
298 {
299 return selection;
300 }
301
302 /* (non-Javadoc)
303 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
304 */
305 @Override
306 public void setSelection(ISelection selection)
307 {
308 this.selection = selection;
309 }
310 }