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 @ 2466
History | View | Annotate | Download (12.3 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 java.util.Objects; |
32 |
import javax.swing.AbstractAction; |
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.JMenu; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.JSeparator; |
37 |
import javax.swing.SwingConstants; |
38 |
import javax.swing.SwingUtilities; |
39 |
import org.apache.commons.lang3.StringUtils; |
40 |
import org.gvsig.fmap.dal.DataTypeUtils; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.fmap.mapcontrol.MapControl; |
43 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PolylineBehavior; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.dataTypes.CoercionException; |
46 |
import org.gvsig.tools.dataTypes.DataTypes; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.task.RunnableWithParameters; |
49 |
import org.gvsig.vectorediting.lib.api.EditingService; |
50 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
51 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
53 |
import org.gvsig.vectorediting.swing.api.EditingContext; |
54 |
import org.gvsig.vectorediting.swing.api.contextmenu.AcceptValueListener; |
55 |
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
* @author gvSIG Team
|
61 |
*
|
62 |
*/
|
63 |
@SuppressWarnings("UseSpecificCatch") |
64 |
public class DefaultEditingContextMenu extends EditingContextMenuView implements |
65 |
EditingContextMenu { |
66 |
|
67 |
private static Logger LOGGER = LoggerFactory |
68 |
.getLogger(DefaultEditingContextMenu.class); |
69 |
|
70 |
private static final long serialVersionUID = -3207749321400180897L; |
71 |
|
72 |
private AcceptValueListener listener;
|
73 |
|
74 |
private Component parent; |
75 |
|
76 |
private EditingServiceParameter parameter;
|
77 |
|
78 |
private EditingContext context;
|
79 |
|
80 |
/**
|
81 |
* @param parent
|
82 |
* @param listener
|
83 |
* @param parameter
|
84 |
*/
|
85 |
public DefaultEditingContextMenu(Component parent, |
86 |
AcceptValueListener listener, EditingServiceParameter parameter) { |
87 |
|
88 |
this(parent, listener, parameter, null); |
89 |
} |
90 |
|
91 |
/**
|
92 |
* @param parent
|
93 |
* @param listener
|
94 |
* @param parameter
|
95 |
* @param context
|
96 |
*/
|
97 |
public DefaultEditingContextMenu(Component parent, |
98 |
AcceptValueListener listener, EditingServiceParameter parameter, EditingContext context) { |
99 |
|
100 |
super();
|
101 |
this.listener = listener;
|
102 |
this.parent = parent;
|
103 |
this.parameter = parameter;
|
104 |
this.context = context;
|
105 |
initActions(); |
106 |
} |
107 |
|
108 |
@SuppressWarnings("serial") |
109 |
private void initActions() { |
110 |
|
111 |
EditingService service = context.getActiveService(); |
112 |
List<EditingServiceParameter> parameters = service.getParameters();
|
113 |
|
114 |
initAction(parameter, this.listener);
|
115 |
|
116 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
117 |
|
118 |
boolean thereAreOptionalParameters = false; |
119 |
|
120 |
for (EditingServiceParameter param : parameters) {
|
121 |
if(param.equals(this.parameter)){ |
122 |
continue;
|
123 |
} |
124 |
if(param.isOptional() && service.isEnabled(param)){
|
125 |
thereAreOptionalParameters = true;
|
126 |
initAction(param, (Object value) -> {
|
127 |
try {
|
128 |
context.setValue(param, (String)value);
|
129 |
} catch (InvalidEntryException ex) {
|
130 |
LOGGER.warn("Invalid value", ex);
|
131 |
} |
132 |
}); |
133 |
} |
134 |
} |
135 |
|
136 |
if (thereAreOptionalParameters) {
|
137 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
138 |
} |
139 |
|
140 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
141 |
AbstractAction action;
|
142 |
|
143 |
action = new AbstractAction(i18nManager.getTranslation("cancel")) { |
144 |
|
145 |
@Override
|
146 |
public void actionPerformed(ActionEvent e) { |
147 |
listener.acceptValue(null);
|
148 |
} |
149 |
}; |
150 |
|
151 |
this.add(action);
|
152 |
((JComponent) parent).add(this); |
153 |
|
154 |
} |
155 |
|
156 |
private void initAction(EditingServiceParameter param, AcceptValueListener listener){ |
157 |
|
158 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
159 |
AbstractAction action;
|
160 |
|
161 |
for (final TYPE type : param.getTypes()) { |
162 |
|
163 |
switch (type) {
|
164 |
case OPTION:
|
165 |
Map<String, String> options = param.getOptions(); |
166 |
JMenu submenu = null; |
167 |
if(param != this.parameter) { |
168 |
EditingService service = context.getActiveService(); |
169 |
Object value = service.getValue(param);
|
170 |
StringBuilder builder = new StringBuilder(); |
171 |
builder.append(i18nManager.getTranslation(param.getName())); |
172 |
if(value != null){ |
173 |
try {
|
174 |
builder.append(" (");
|
175 |
builder.append(i18nManager.getTranslation((String)DataTypeUtils.coerce(DataTypes.STRING, value)));
|
176 |
builder.append(")");
|
177 |
} catch (CoercionException ex) {
|
178 |
LOGGER.warn("Can't coerce '" + Objects.toString(value) + "' to String", ex); |
179 |
} |
180 |
} |
181 |
submenu = new JMenu(builder.toString()); |
182 |
} |
183 |
for (String key : options.keySet()) { |
184 |
String value = i18nManager.getTranslation(options.get(key));
|
185 |
action = new AbstractAction(value) { |
186 |
|
187 |
@Override
|
188 |
public void actionPerformed(ActionEvent e) { |
189 |
String key = (String) this.getValue("key"); |
190 |
listener.acceptValue(key); |
191 |
} |
192 |
}; |
193 |
action.putValue("key", key);
|
194 |
if(submenu != null){ |
195 |
submenu.add(action); |
196 |
} else {
|
197 |
this.add(action);
|
198 |
} |
199 |
} |
200 |
if(submenu != null){ |
201 |
this.add(submenu);
|
202 |
} |
203 |
break;
|
204 |
|
205 |
|
206 |
case VALUE:
|
207 |
String name =
|
208 |
i18nManager.getTranslation(param.getName()) + " ("
|
209 |
+ i18nManager.getTranslation("value") + ")"; |
210 |
action = new AbstractAction(name) { |
211 |
|
212 |
@Override
|
213 |
public void actionPerformed(ActionEvent e) { |
214 |
String value = showValueDialog(param);
|
215 |
if (StringUtils.isNotEmpty(value)) {
|
216 |
listener.acceptValue(value); |
217 |
} |
218 |
} |
219 |
}; |
220 |
this.add(action);
|
221 |
break;
|
222 |
|
223 |
case POSITION:
|
224 |
case LIST_POSITIONS:
|
225 |
name = |
226 |
i18nManager.getTranslation(param.getName()) + " ("
|
227 |
+ i18nManager.getTranslation("point") + ")"; |
228 |
action = new AbstractAction(name) { |
229 |
|
230 |
@Override
|
231 |
public void actionPerformed(ActionEvent e) { |
232 |
String value = showPointDialog(param);
|
233 |
if (value != null) { |
234 |
listener.acceptValue(value); |
235 |
} |
236 |
} |
237 |
}; |
238 |
this.add(action);
|
239 |
break;
|
240 |
|
241 |
case DISTANCE:
|
242 |
name = i18nManager.getTranslation(param.getName()) + " ("
|
243 |
+ i18nManager.getTranslation("_graphic_distance") + ")"; |
244 |
|
245 |
MapControl mapCtrl = context.getMapControl(); |
246 |
String currentTool = mapCtrl.getCurrentTool();
|
247 |
DistanceListener mli = new DistanceListener(mapCtrl, listener);
|
248 |
PolylineBehavior polylineBehavior = new PolylineBehavior(mli);
|
249 |
mapCtrl.addBehavior("distance", polylineBehavior);
|
250 |
|
251 |
action = new AbstractAction(name) { |
252 |
|
253 |
@Override
|
254 |
public void actionPerformed(ActionEvent e) { |
255 |
mapCtrl.setTool("distance");
|
256 |
} |
257 |
}; |
258 |
this.add(action);
|
259 |
break;
|
260 |
|
261 |
|
262 |
case SELECTION:
|
263 |
case GEOMETRY:
|
264 |
break;
|
265 |
} |
266 |
|
267 |
} |
268 |
|
269 |
} |
270 |
|
271 |
/**
|
272 |
* @return this as JComponent
|
273 |
*/
|
274 |
public JComponent asJComponent() { |
275 |
return this; |
276 |
} |
277 |
|
278 |
private String showPointDialog(EditingServiceParameter parameter) { |
279 |
|
280 |
int dimension = 2; |
281 |
|
282 |
Object defaultValue = parameter.getDefaultValue();
|
283 |
Point defaultPoint = null; |
284 |
if (defaultValue != null && defaultValue instanceof Point) { |
285 |
defaultPoint = (Point)defaultValue;
|
286 |
dimension = defaultPoint.getDimension(); |
287 |
} |
288 |
|
289 |
final DefaultDynamicEditingPointPanel pointPanel = new DefaultDynamicEditingPointPanel(null, defaultPoint); |
290 |
if(defaultPoint!=null){ |
291 |
for(int i=0; i<dimension; i++){ |
292 |
pointPanel.setValue(i, String.valueOf(defaultPoint.getCoordinateAt(i)));
|
293 |
} |
294 |
} |
295 |
final String title = ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
296 |
|
297 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
298 |
|
299 |
@Override
|
300 |
public void run() { |
301 |
this.returnValue =
|
302 |
JOptionPane.showConfirmDialog(null, pointPanel, title, JOptionPane.OK_CANCEL_OPTION); |
303 |
} |
304 |
}; |
305 |
|
306 |
if (SwingUtilities.isEventDispatchThread()) { |
307 |
runnable.run(); |
308 |
} else {
|
309 |
try {
|
310 |
SwingUtilities.invokeAndWait(runnable);
|
311 |
} catch (Exception e) { |
312 |
LOGGER.info("Can't show point dialog.", e);
|
313 |
} |
314 |
} |
315 |
|
316 |
int result = ((Integer) runnable.getReturnValue()); |
317 |
|
318 |
if (result == JOptionPane.OK_OPTION) { |
319 |
StringBuilder stb = new StringBuilder(); |
320 |
for(int i=0; i<dimension; i++){ |
321 |
String value = pointPanel.getValue(i);
|
322 |
stb.append(value); |
323 |
if(i<dimension-1){ |
324 |
stb.append(",");
|
325 |
} |
326 |
} |
327 |
return stb.toString();
|
328 |
} |
329 |
|
330 |
return null; |
331 |
} |
332 |
|
333 |
private String showValueDialog(EditingServiceParameter parameter) { |
334 |
|
335 |
final DefaultEditingValuePanel valuePanel =
|
336 |
new DefaultEditingValuePanel();
|
337 |
|
338 |
final String title = |
339 |
ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
340 |
|
341 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
342 |
|
343 |
@Override
|
344 |
public void run() { |
345 |
this.returnValue =
|
346 |
JOptionPane.showConfirmDialog(null, valuePanel, title, |
347 |
JOptionPane.OK_CANCEL_OPTION);
|
348 |
} |
349 |
}; |
350 |
|
351 |
if (SwingUtilities.isEventDispatchThread()) { |
352 |
runnable.run(); |
353 |
} else {
|
354 |
try {
|
355 |
SwingUtilities.invokeAndWait(runnable);
|
356 |
} catch (Exception e) { |
357 |
LOGGER.info("Can't show value dialog.", e);
|
358 |
} |
359 |
} |
360 |
|
361 |
int result = ((Integer) runnable.getReturnValue()); |
362 |
|
363 |
if (result == JOptionPane.OK_OPTION) { |
364 |
return valuePanel.getValue();
|
365 |
} |
366 |
|
367 |
return null; |
368 |
|
369 |
} |
370 |
|
371 |
} |