7c1e9ace14fd8aef82879aad81c167eba84df928
[public/netxms.git] / src / java / netxms-eclipse / OSM / src / org / netxms / ui / eclipse / osm / views / WorldMap.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.IProgressMonitor;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.ui.IMemento;
27 import org.eclipse.ui.IViewSite;
28 import org.eclipse.ui.PartInitException;
29 import org.netxms.base.GeoLocation;
30 import org.netxms.client.NXCObjectModificationData;
31 import org.netxms.client.NXCSession;
32 import org.netxms.ui.eclipse.jobs.ConsoleJob;
33 import org.netxms.ui.eclipse.objectbrowser.dialogs.ObjectSelectionDialog;
34 import org.netxms.ui.eclipse.osm.Activator;
35 import org.netxms.ui.eclipse.osm.Messages;
36 import org.netxms.ui.eclipse.osm.tools.MapAccessor;
37 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
38
39 /**
40 * World map view
41 */
42 public class WorldMap extends AbstractGeolocationView
43 {
44 public static final String ID = "org.netxms.ui.eclipse.osm.views.WorldMap"; //$NON-NLS-1$
45
46 private GeoLocation initialLocation = new GeoLocation(0.0, 0.0);
47 private int initialZoom = 2;
48 private Action actionPlaceObject;
49
50 /* (non-Javadoc)
51 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
52 */
53 @Override
54 public void init(IViewSite site, IMemento memento) throws PartInitException
55 {
56 if (memento != null)
57 {
58 if (memento.getInteger("zoom") != null) //$NON-NLS-1$
59 initialZoom = memento.getInteger("zoom"); //$NON-NLS-1$
60
61 Float lat = memento.getFloat("latitude"); //$NON-NLS-1$
62 Float lon = memento.getFloat("longitude"); //$NON-NLS-1$
63 if ((lat != null) && (lon != null))
64 initialLocation = new GeoLocation(lat, lon);
65 }
66 super.init(site, memento);
67 }
68
69 /* (non-Javadoc)
70 * @see org.eclipse.ui.part.ViewPart#saveState(org.eclipse.ui.IMemento)
71 */
72 @Override
73 public void saveState(IMemento memento)
74 {
75 super.saveState(memento);
76 MapAccessor m = getMapAccessor();
77 memento.putFloat("latitude", (float)m.getLatitude()); //$NON-NLS-1$
78 memento.putFloat("longitude", (float)m.getLongitude()); //$NON-NLS-1$
79 memento.putInteger("zoom", m.getZoom()); //$NON-NLS-1$
80 }
81
82 /* (non-Javadoc)
83 * @see org.netxms.ui.eclipse.osm.views.AbstractGeolocationView#getInitialCenterPoint()
84 */
85 @Override
86 protected GeoLocation getInitialCenterPoint()
87 {
88 return initialLocation;
89 }
90
91 /* (non-Javadoc)
92 * @see org.netxms.ui.eclipse.osm.views.AbstractGeolocationView#getInitialZoomLevel()
93 */
94 @Override
95 protected int getInitialZoomLevel()
96 {
97 return initialZoom;
98 }
99
100 /* (non-Javadoc)
101 * @see org.netxms.ui.eclipse.osm.views.AbstractGeolocationView#createActions()
102 */
103 @Override
104 protected void createActions()
105 {
106 super.createActions();
107
108 actionPlaceObject = new Action(Messages.get().WorldMap_PlaceObject) {
109 @Override
110 public void run()
111 {
112 placeObject();
113 }
114 };
115 }
116
117 /* (non-Javadoc)
118 * @see org.netxms.ui.eclipse.osm.views.AbstractGeolocationView#fillContextMenu(org.eclipse.jface.action.IMenuManager)
119 */
120 @Override
121 protected void fillContextMenu(IMenuManager manager)
122 {
123 super.fillContextMenu(manager);
124
125 if (getSelection().isEmpty())
126 {
127 manager.add(new Separator());
128 manager.add(actionPlaceObject);
129 }
130 }
131
132 /**
133 * Place object on map at mouse position
134 */
135 private void placeObject()
136 {
137 final ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, ObjectSelectionDialog.createNodeSelectionFilter(true));
138 if (dlg.open() == Window.OK)
139 {
140 final NXCObjectModificationData md = new NXCObjectModificationData(dlg.getSelectedObjects().get(0).getObjectId());
141 md.setGeolocation(map.getLocationAtPoint(map.getCurrentPoint()));
142 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
143 new ConsoleJob(Messages.get().WorldMap_JobTitle, this, Activator.PLUGIN_ID, null) {
144 @Override
145 protected void runInternal(IProgressMonitor monitor) throws Exception
146 {
147 session.modifyObject(md);
148 }
149
150 @Override
151 protected String getErrorMessage()
152 {
153 return Messages.get().WorldMap_JobError;
154 }
155 }.start();
156 }
157 }
158 }
159