root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / InfoListener.java @ 38564
History | View | Annotate | Download (7.44 KB)
1 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib��ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.app.project.documents.view.toolListeners; |
42 |
|
43 |
import java.awt.Image; |
44 |
import java.util.HashMap; |
45 |
import java.util.Map; |
46 |
|
47 |
import javax.swing.JDialog; |
48 |
import javax.swing.JPanel; |
49 |
|
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
import org.gvsig.andami.PluginServices; |
54 |
import org.gvsig.andami.messages.NotificationManager; |
55 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
56 |
import org.gvsig.app.ApplicationLocator; |
57 |
import org.gvsig.app.ApplicationManager; |
58 |
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog; |
59 |
import org.gvsig.fmap.geom.primitive.Point; |
60 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
61 |
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint; |
62 |
import org.gvsig.fmap.mapcontrol.MapControl; |
63 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
64 |
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent; |
65 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
66 |
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
67 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dynobject.DynObjectSet; |
70 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
71 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
72 |
import org.gvsig.tools.extensionpoint.ExtensionSingleton; |
73 |
|
74 |
/**
|
75 |
* Listener that looks for information at the point selected by one
|
76 |
* click of any mouse's button, in the active layers of the associated
|
77 |
* <code>MapControl</code>, and displays that data on a {@link FInfoDialog
|
78 |
* FInfoDialog} dialog.
|
79 |
*
|
80 |
* @author -2009 Vicente Caballero Navarro
|
81 |
* @author 2009- gvSIG Team
|
82 |
*/
|
83 |
public class InfoListener implements PointListener { |
84 |
|
85 |
/**
|
86 |
* Object used to log messages for this listener.
|
87 |
*/
|
88 |
//
|
89 |
private static final Logger logger = LoggerFactory.getLogger(InfoListener.class); |
90 |
|
91 |
private static final String EP_INFOTOOL_NAME = "org.gvsig.app.infotool"; |
92 |
private static final String EP_INFOTOOL_RENDERER = "renderer"; |
93 |
|
94 |
public static void initializeExtensionPoint() { |
95 |
ExtensionPointManager manager = ToolsLocator.getExtensionPointManager(); |
96 |
ExtensionPoint point = manager.add(EP_INFOTOOL_NAME, "Register of data relative to infotool");
|
97 |
point.append( |
98 |
EP_INFOTOOL_RENDERER, |
99 |
"Renderer used to show in a panel the info by point tool.",
|
100 |
new DefaultInfoByPointRenderer()
|
101 |
); |
102 |
} |
103 |
|
104 |
/**
|
105 |
* The image to display when the cursor is active.
|
106 |
*/
|
107 |
private final Image img = PluginServices.getIconTheme() |
108 |
.get("cursor-info-by-point").getImage();
|
109 |
|
110 |
/**
|
111 |
* Reference to the <code>MapControl</code> object that uses.
|
112 |
*/
|
113 |
private MapControl mapCtrl;
|
114 |
|
115 |
/**
|
116 |
* Radius as tolerance around the selected point, the area will be used to
|
117 |
* look for information.
|
118 |
*/
|
119 |
private static int TOL = 7; |
120 |
|
121 |
/**
|
122 |
* <p>
|
123 |
* Creates a new <code>InfoListener</code> object.
|
124 |
* </p>
|
125 |
*
|
126 |
* @param mc
|
127 |
* the <code>MapControl</code> where will be applied the changes
|
128 |
*/
|
129 |
public InfoListener(MapControl mc) {
|
130 |
this.mapCtrl = mc;
|
131 |
} |
132 |
|
133 |
public void point(PointEvent event) throws BehaviorException { |
134 |
try {
|
135 |
ApplicationManager application = ApplicationLocator.getManager(); |
136 |
int numLayersInfoable = 0; |
137 |
Point point = event.getMapPoint();
|
138 |
double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
|
139 |
|
140 |
FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
|
141 |
Map<String, DynObjectSet> layer2info = |
142 |
new HashMap<String, DynObjectSet>(sel.length); |
143 |
|
144 |
for (int i = 0; i < sel.length; i++) { |
145 |
FLayer laCapa = sel[i]; |
146 |
if (laCapa instanceof InfoByPoint) { |
147 |
InfoByPoint layer = (InfoByPoint) laCapa; |
148 |
DynObjectSet info = layer.getInfo(point, tolerance); |
149 |
layer2info.put(laCapa.getName(), info); |
150 |
numLayersInfoable++; |
151 |
} |
152 |
} |
153 |
|
154 |
if (numLayersInfoable == 0) { |
155 |
return;
|
156 |
} |
157 |
|
158 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
159 |
InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER); |
160 |
|
161 |
IWindow window = renderer.getPanel(); |
162 |
boolean first_time = (window == null); |
163 |
renderer.resetPanel(layer2info); |
164 |
|
165 |
window = renderer.getPanel(); |
166 |
if (window == null) { |
167 |
logger.info("Error. Unable to create info panel.");
|
168 |
return;
|
169 |
} |
170 |
|
171 |
|
172 |
if (first_time) {
|
173 |
PluginServices.getMDIManager().addCentredWindow(window); |
174 |
} else {
|
175 |
// perhaps bring to front
|
176 |
} |
177 |
|
178 |
|
179 |
} catch (Exception e) { |
180 |
NotificationManager.addError("Info by Point", e);
|
181 |
e.printStackTrace(); |
182 |
} |
183 |
} |
184 |
|
185 |
public Image getImageCursor() { |
186 |
return img;
|
187 |
} |
188 |
|
189 |
public boolean cancelDrawing() { |
190 |
return false; |
191 |
} |
192 |
|
193 |
public void pointDoubleClick(PointEvent event) throws BehaviorException { |
194 |
// Nothing to do
|
195 |
} |
196 |
|
197 |
public interface InfoByPointRenderer { |
198 |
|
199 |
public FInfoDialog getPanel();
|
200 |
public void resetPanel(Map<String, DynObjectSet> layersInfo); |
201 |
|
202 |
} |
203 |
|
204 |
public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton { |
205 |
|
206 |
private FInfoDialog dlgInfo = null; |
207 |
|
208 |
public FInfoDialog getPanel() {
|
209 |
return dlgInfo;
|
210 |
} |
211 |
|
212 |
|
213 |
public void resetPanel(Map<String, DynObjectSet> layersInfo) { |
214 |
// TODO: set the writable parameter to true to activate
|
215 |
// edition of the info by point information.
|
216 |
LayersDynObjectSetComponent infoComponent = |
217 |
MapControlLocator.getMapControlManager() |
218 |
.createLayersDynObjectSetComponent(layersInfo, false);
|
219 |
|
220 |
if (dlgInfo == null) { |
221 |
dlgInfo = new FInfoDialog(infoComponent);
|
222 |
} else {
|
223 |
dlgInfo.setInfo(infoComponent); |
224 |
} |
225 |
} |
226 |
|
227 |
} |
228 |
|
229 |
|
230 |
} |