gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / contextmenu / DefaultEditingContextMenu.java @ 2207
History | View | Annotate | Download (11 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 gvSIG Association
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.vectorediting.swing.impl.contextmenu; |
26 |
|
27 |
import java.awt.Component; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.JOptionPane; |
34 |
import javax.swing.JSeparator; |
35 |
import javax.swing.SwingConstants; |
36 |
import javax.swing.SwingUtilities; |
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.mapcontrol.MapControl; |
40 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PolylineBehavior; |
41 |
import org.gvsig.tools.ToolsLocator; |
42 |
import org.gvsig.tools.i18n.I18nManager; |
43 |
import org.gvsig.tools.task.RunnableWithParameters; |
44 |
import org.gvsig.vectorediting.lib.api.EditingService; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
46 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
47 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
48 |
import org.gvsig.vectorediting.swing.api.EditingContext; |
49 |
import org.gvsig.vectorediting.swing.api.contextmenu.AcceptValueListener; |
50 |
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
* @author llmarques
|
56 |
*
|
57 |
*/
|
58 |
public class DefaultEditingContextMenu extends EditingContextMenuView implements |
59 |
EditingContextMenu { |
60 |
|
61 |
private static Logger LOGGER = LoggerFactory |
62 |
.getLogger(DefaultEditingContextMenu.class); |
63 |
|
64 |
private static final long serialVersionUID = -3207749321400180897L; |
65 |
|
66 |
private AcceptValueListener listener;
|
67 |
|
68 |
private Component parent; |
69 |
|
70 |
private EditingServiceParameter parameter;
|
71 |
|
72 |
private EditingContext context;
|
73 |
|
74 |
/**
|
75 |
* @param parent
|
76 |
* @param listener
|
77 |
* @param parameter
|
78 |
*/
|
79 |
public DefaultEditingContextMenu(Component parent, |
80 |
AcceptValueListener listener, EditingServiceParameter parameter) { |
81 |
|
82 |
this(parent, listener, parameter, null); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* @param parent
|
87 |
* @param listener
|
88 |
* @param parameter
|
89 |
* @param context
|
90 |
*/
|
91 |
public DefaultEditingContextMenu(Component parent, |
92 |
AcceptValueListener listener, EditingServiceParameter parameter, EditingContext context) { |
93 |
|
94 |
super();
|
95 |
this.listener = listener;
|
96 |
this.parent = parent;
|
97 |
this.parameter = parameter;
|
98 |
this.context = context;
|
99 |
initActions(); |
100 |
} |
101 |
|
102 |
@SuppressWarnings("serial") |
103 |
private void initActions() { |
104 |
|
105 |
EditingService service = context.getActiveService(); |
106 |
List<EditingServiceParameter> parameters = service.getParameters();
|
107 |
|
108 |
initAction(parameter, this.listener);
|
109 |
|
110 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
111 |
|
112 |
boolean thereAreOptionalParameters = false; |
113 |
|
114 |
for (EditingServiceParameter param : parameters) {
|
115 |
if(param.equals(this.parameter)){ |
116 |
continue;
|
117 |
} |
118 |
if(param.isOptional() && service.isEnabled(param)){
|
119 |
thereAreOptionalParameters = true;
|
120 |
initAction(param, new AcceptValueListener() {
|
121 |
|
122 |
@Override
|
123 |
public void acceptValue(Object value) { |
124 |
try {
|
125 |
context.setValue(param, (String)value);
|
126 |
} catch (InvalidEntryException ex) {
|
127 |
LOGGER.warn("Invalid value", ex);
|
128 |
} |
129 |
} |
130 |
}); |
131 |
} |
132 |
} |
133 |
|
134 |
if (thereAreOptionalParameters) {
|
135 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
136 |
} |
137 |
|
138 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
139 |
AbstractAction action;
|
140 |
|
141 |
action = new AbstractAction(i18nManager.getTranslation("cancel")) { |
142 |
|
143 |
@Override
|
144 |
public void actionPerformed(ActionEvent e) { |
145 |
listener.acceptValue(null);
|
146 |
} |
147 |
}; |
148 |
|
149 |
this.add(action);
|
150 |
((JComponent) parent).add(this); |
151 |
|
152 |
} |
153 |
|
154 |
private void initAction(EditingServiceParameter param, AcceptValueListener listener){ |
155 |
|
156 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
157 |
AbstractAction action;
|
158 |
|
159 |
for (final TYPE type : param.getTypes()) { |
160 |
|
161 |
switch (type) {
|
162 |
case OPTION:
|
163 |
Map<String, String> options = param.getOptions(); |
164 |
for (String key : options.keySet()) { |
165 |
String value = i18nManager.getTranslation(options.get(key));
|
166 |
action = new AbstractAction(value) { |
167 |
|
168 |
@Override
|
169 |
public void actionPerformed(ActionEvent e) { |
170 |
String key = (String) this.getValue("key"); |
171 |
listener.acceptValue(key); |
172 |
} |
173 |
}; |
174 |
action.putValue("key", key);
|
175 |
this.add(action);
|
176 |
} |
177 |
break;
|
178 |
case VALUE:
|
179 |
String name =
|
180 |
i18nManager.getTranslation(param.getName()) + " ("
|
181 |
+ i18nManager.getTranslation("value") + ")"; |
182 |
action = new AbstractAction(name) { |
183 |
|
184 |
@Override
|
185 |
public void actionPerformed(ActionEvent e) { |
186 |
String value = showValueDialog(param);
|
187 |
if (StringUtils.isNotEmpty(value)) {
|
188 |
listener.acceptValue(value); |
189 |
} |
190 |
} |
191 |
}; |
192 |
this.add(action);
|
193 |
break;
|
194 |
|
195 |
case POSITION:
|
196 |
case LIST_POSITIONS:
|
197 |
name = |
198 |
i18nManager.getTranslation(param.getName()) + " ("
|
199 |
+ i18nManager.getTranslation("point") + ")"; |
200 |
action = new AbstractAction(name) { |
201 |
|
202 |
@Override
|
203 |
public void actionPerformed(ActionEvent e) { |
204 |
String value = showPointDialog(param);
|
205 |
if (value != null) { |
206 |
listener.acceptValue(value); |
207 |
} |
208 |
} |
209 |
}; |
210 |
this.add(action);
|
211 |
break;
|
212 |
|
213 |
case DISTANCE:
|
214 |
name = i18nManager.getTranslation(param.getName()) + " ("
|
215 |
+ i18nManager.getTranslation("_graphic_distance") + ")"; |
216 |
|
217 |
MapControl mapCtrl = context.getMapControl(); |
218 |
String currentTool = mapCtrl.getCurrentTool();
|
219 |
DistanceListener mli = new DistanceListener(mapCtrl, listener);
|
220 |
PolylineBehavior polylineBehavior = new PolylineBehavior(mli);
|
221 |
mapCtrl.addBehavior("distance", polylineBehavior);
|
222 |
|
223 |
action = new AbstractAction(name) { |
224 |
|
225 |
@Override
|
226 |
public void actionPerformed(ActionEvent e) { |
227 |
mapCtrl.setTool("distance");
|
228 |
} |
229 |
}; |
230 |
this.add(action);
|
231 |
break;
|
232 |
|
233 |
|
234 |
case SELECTION:
|
235 |
case GEOMETRY:
|
236 |
break;
|
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* @return this as JComponent
|
245 |
*/
|
246 |
public JComponent asJComponent() { |
247 |
return this; |
248 |
} |
249 |
|
250 |
private String showPointDialog(EditingServiceParameter parameter) { |
251 |
|
252 |
int dimension = 2; |
253 |
|
254 |
Object defaultValue = parameter.getDefaultValue();
|
255 |
Point defaultPoint = null; |
256 |
if (defaultValue != null && defaultValue instanceof Point) { |
257 |
defaultPoint = (Point)defaultValue;
|
258 |
dimension = defaultPoint.getDimension(); |
259 |
} |
260 |
|
261 |
final DefaultDynamicEditingPointPanel pointPanel = new DefaultDynamicEditingPointPanel(null, defaultPoint); |
262 |
if(defaultPoint!=null){ |
263 |
for(int i=0; i<dimension; i++){ |
264 |
pointPanel.setValue(i, String.valueOf(defaultPoint.getCoordinateAt(i)));
|
265 |
} |
266 |
} |
267 |
final String title = ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
268 |
|
269 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
270 |
|
271 |
public void run() { |
272 |
this.returnValue =
|
273 |
JOptionPane.showConfirmDialog(null, pointPanel, title, JOptionPane.OK_CANCEL_OPTION); |
274 |
} |
275 |
}; |
276 |
|
277 |
if (SwingUtilities.isEventDispatchThread()) { |
278 |
runnable.run(); |
279 |
} else {
|
280 |
try {
|
281 |
SwingUtilities.invokeAndWait(runnable);
|
282 |
} catch (Exception e) { |
283 |
LOGGER.info("Can't show point dialog.", e);
|
284 |
} |
285 |
} |
286 |
|
287 |
int result = ((Integer) runnable.getReturnValue()); |
288 |
|
289 |
if (result == JOptionPane.OK_OPTION) { |
290 |
StringBuilder stb = new StringBuilder(); |
291 |
for(int i=0; i<dimension; i++){ |
292 |
String value = pointPanel.getValue(i);
|
293 |
stb.append(value); |
294 |
if(i<dimension-1){ |
295 |
stb.append(",");
|
296 |
} |
297 |
} |
298 |
return stb.toString();
|
299 |
} |
300 |
|
301 |
return null; |
302 |
} |
303 |
|
304 |
private String showValueDialog(EditingServiceParameter parameter) { |
305 |
|
306 |
final DefaultEditingValuePanel valuePanel =
|
307 |
new DefaultEditingValuePanel();
|
308 |
|
309 |
final String title = |
310 |
ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
311 |
|
312 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
313 |
|
314 |
@Override
|
315 |
public void run() { |
316 |
this.returnValue =
|
317 |
JOptionPane.showConfirmDialog(null, valuePanel, title, |
318 |
JOptionPane.OK_CANCEL_OPTION);
|
319 |
} |
320 |
}; |
321 |
|
322 |
if (SwingUtilities.isEventDispatchThread()) { |
323 |
runnable.run(); |
324 |
} else {
|
325 |
try {
|
326 |
SwingUtilities.invokeAndWait(runnable);
|
327 |
} catch (Exception e) { |
328 |
LOGGER.info("Can't show value dialog.", e);
|
329 |
} |
330 |
} |
331 |
|
332 |
int result = ((Integer) runnable.getReturnValue()); |
333 |
|
334 |
if (result == JOptionPane.OK_OPTION) { |
335 |
return valuePanel.getValue();
|
336 |
} |
337 |
|
338 |
return null; |
339 |
|
340 |
} |
341 |
|
342 |
} |