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 @ 2204
History | View | Annotate | Download (10.8 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 |
for (EditingServiceParameter param : parameters) {
|
109 |
if(param.equals(this.parameter)){ |
110 |
continue;
|
111 |
} |
112 |
if(param.isOptional() && service.isEnabled(param)){
|
113 |
initAction(param, new AcceptValueListener() {
|
114 |
|
115 |
@Override
|
116 |
public void acceptValue(Object value) { |
117 |
try {
|
118 |
context.setValue(param, (String)value);
|
119 |
} catch (InvalidEntryException ex) {
|
120 |
LOGGER.warn("Invalid value", ex);
|
121 |
} |
122 |
} |
123 |
}); |
124 |
} |
125 |
} |
126 |
|
127 |
initAction(parameter, this.listener);
|
128 |
|
129 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
130 |
AbstractAction action;
|
131 |
|
132 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
133 |
action = new AbstractAction(i18nManager.getTranslation("cancel")) { |
134 |
|
135 |
@Override
|
136 |
public void actionPerformed(ActionEvent e) { |
137 |
listener.acceptValue(null);
|
138 |
} |
139 |
}; |
140 |
|
141 |
this.add(action);
|
142 |
((JComponent) parent).add(this); |
143 |
|
144 |
} |
145 |
|
146 |
private void initAction(EditingServiceParameter param, AcceptValueListener listener){ |
147 |
|
148 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
149 |
AbstractAction action;
|
150 |
|
151 |
for (final TYPE type : param.getTypes()) { |
152 |
|
153 |
switch (type) {
|
154 |
case OPTION:
|
155 |
Map<String, String> options = param.getOptions(); |
156 |
for (String key : options.keySet()) { |
157 |
String value = i18nManager.getTranslation(options.get(key));
|
158 |
action = new AbstractAction(value) { |
159 |
|
160 |
@Override
|
161 |
public void actionPerformed(ActionEvent e) { |
162 |
String key = (String) this.getValue("key"); |
163 |
listener.acceptValue(key); |
164 |
} |
165 |
}; |
166 |
action.putValue("key", key);
|
167 |
this.add(action);
|
168 |
} |
169 |
break;
|
170 |
case VALUE:
|
171 |
String name =
|
172 |
i18nManager.getTranslation(param.getName()) + " ("
|
173 |
+ i18nManager.getTranslation("value") + ")"; |
174 |
action = new AbstractAction(name) { |
175 |
|
176 |
@Override
|
177 |
public void actionPerformed(ActionEvent e) { |
178 |
String value = showValueDialog(param);
|
179 |
if (StringUtils.isNotEmpty(value)) {
|
180 |
listener.acceptValue(value); |
181 |
} |
182 |
} |
183 |
}; |
184 |
this.add(action);
|
185 |
break;
|
186 |
|
187 |
case POSITION:
|
188 |
case LIST_POSITIONS:
|
189 |
name = |
190 |
i18nManager.getTranslation(param.getName()) + " ("
|
191 |
+ i18nManager.getTranslation("point") + ")"; |
192 |
action = new AbstractAction(name) { |
193 |
|
194 |
@Override
|
195 |
public void actionPerformed(ActionEvent e) { |
196 |
String value = showPointDialog(param);
|
197 |
if (value != null) { |
198 |
listener.acceptValue(value); |
199 |
} |
200 |
} |
201 |
}; |
202 |
this.add(action);
|
203 |
break;
|
204 |
|
205 |
case DISTANCE:
|
206 |
name = i18nManager.getTranslation(param.getName()) + " ("
|
207 |
+ i18nManager.getTranslation("_graphic_distance") + ")"; |
208 |
|
209 |
MapControl mapCtrl = context.getMapControl(); |
210 |
String currentTool = mapCtrl.getCurrentTool();
|
211 |
DistanceListener mli = new DistanceListener(mapCtrl, listener);
|
212 |
PolylineBehavior polylineBehavior = new PolylineBehavior(mli);
|
213 |
mapCtrl.addBehavior("distance", polylineBehavior);
|
214 |
|
215 |
action = new AbstractAction(name) { |
216 |
|
217 |
@Override
|
218 |
public void actionPerformed(ActionEvent e) { |
219 |
mapCtrl.setTool("distance");
|
220 |
} |
221 |
}; |
222 |
this.add(action);
|
223 |
break;
|
224 |
|
225 |
|
226 |
case SELECTION:
|
227 |
case GEOMETRY:
|
228 |
break;
|
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
/**
|
236 |
* @return this as JComponent
|
237 |
*/
|
238 |
public JComponent asJComponent() { |
239 |
return this; |
240 |
} |
241 |
|
242 |
private String showPointDialog(EditingServiceParameter parameter) { |
243 |
|
244 |
int dimension = 2; |
245 |
|
246 |
Object defaultValue = parameter.getDefaultValue();
|
247 |
Point defaultPoint = null; |
248 |
if (defaultValue != null && defaultValue instanceof Point) { |
249 |
defaultPoint = (Point)defaultValue;
|
250 |
dimension = defaultPoint.getDimension(); |
251 |
} |
252 |
|
253 |
final DefaultDynamicEditingPointPanel pointPanel = new DefaultDynamicEditingPointPanel(null, defaultPoint); |
254 |
if(defaultPoint!=null){ |
255 |
for(int i=0; i<dimension; i++){ |
256 |
pointPanel.setValue(i, String.valueOf(defaultPoint.getCoordinateAt(i)));
|
257 |
} |
258 |
} |
259 |
final String title = ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
260 |
|
261 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
262 |
|
263 |
public void run() { |
264 |
this.returnValue =
|
265 |
JOptionPane.showConfirmDialog(null, pointPanel, title, JOptionPane.OK_CANCEL_OPTION); |
266 |
} |
267 |
}; |
268 |
|
269 |
if (SwingUtilities.isEventDispatchThread()) { |
270 |
runnable.run(); |
271 |
} else {
|
272 |
try {
|
273 |
SwingUtilities.invokeAndWait(runnable);
|
274 |
} catch (Exception e) { |
275 |
LOGGER.info("Can't show point dialog.", e);
|
276 |
} |
277 |
} |
278 |
|
279 |
int result = ((Integer) runnable.getReturnValue()); |
280 |
|
281 |
if (result == JOptionPane.OK_OPTION) { |
282 |
StringBuilder stb = new StringBuilder(); |
283 |
for(int i=0; i<dimension; i++){ |
284 |
String value = pointPanel.getValue(i);
|
285 |
stb.append(value); |
286 |
if(i<dimension-1){ |
287 |
stb.append(",");
|
288 |
} |
289 |
} |
290 |
return stb.toString();
|
291 |
} |
292 |
|
293 |
return null; |
294 |
} |
295 |
|
296 |
private String showValueDialog(EditingServiceParameter parameter) { |
297 |
|
298 |
final DefaultEditingValuePanel valuePanel =
|
299 |
new DefaultEditingValuePanel();
|
300 |
|
301 |
final String title = |
302 |
ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
303 |
|
304 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
305 |
|
306 |
@Override
|
307 |
public void run() { |
308 |
this.returnValue =
|
309 |
JOptionPane.showConfirmDialog(null, valuePanel, title, |
310 |
JOptionPane.OK_CANCEL_OPTION);
|
311 |
} |
312 |
}; |
313 |
|
314 |
if (SwingUtilities.isEventDispatchThread()) { |
315 |
runnable.run(); |
316 |
} else {
|
317 |
try {
|
318 |
SwingUtilities.invokeAndWait(runnable);
|
319 |
} catch (Exception e) { |
320 |
LOGGER.info("Can't show value dialog.", e);
|
321 |
} |
322 |
} |
323 |
|
324 |
int result = ((Integer) runnable.getReturnValue()); |
325 |
|
326 |
if (result == JOptionPane.OK_OPTION) { |
327 |
return valuePanel.getValue();
|
328 |
} |
329 |
|
330 |
return null; |
331 |
|
332 |
} |
333 |
|
334 |
} |