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 @ 1575
History | View | Annotate | Download (8.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.Map; |
30 |
|
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 |
|
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.i18n.I18nManager; |
44 |
import org.gvsig.tools.task.RunnableWithParameters; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
46 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
47 |
import org.gvsig.vectorediting.swing.api.contextmenu.AcceptValueListener; |
48 |
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu; |
49 |
|
50 |
/**
|
51 |
* @author llmarques
|
52 |
*
|
53 |
*/
|
54 |
public class DefaultEditingContextMenu extends EditingContextMenuView implements |
55 |
EditingContextMenu { |
56 |
|
57 |
private static Logger logger = LoggerFactory |
58 |
.getLogger(DefaultEditingContextMenu.class); |
59 |
|
60 |
private static final long serialVersionUID = -3207749321400180897L; |
61 |
|
62 |
private AcceptValueListener listener;
|
63 |
|
64 |
private Component parent; |
65 |
|
66 |
private EditingServiceParameter parameter;
|
67 |
|
68 |
/**
|
69 |
* @param parent
|
70 |
* @param listener
|
71 |
* @param parameter
|
72 |
*/
|
73 |
public DefaultEditingContextMenu(Component parent, |
74 |
AcceptValueListener listener, EditingServiceParameter parameter) { |
75 |
|
76 |
super();
|
77 |
this.listener = listener;
|
78 |
this.parent = parent;
|
79 |
this.parameter = parameter;
|
80 |
initActions(); |
81 |
} |
82 |
|
83 |
@SuppressWarnings("serial") |
84 |
private void initActions() { |
85 |
|
86 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
87 |
AbstractAction action = null; |
88 |
|
89 |
for (final TYPE type : parameter.getTypes()) { |
90 |
|
91 |
switch (type) {
|
92 |
case OPTION:
|
93 |
Map<String, String> options = parameter.getOptions(); |
94 |
for (String key : options.keySet()) { |
95 |
String value = i18nManager.getTranslation(options.get(key));
|
96 |
action = new AbstractAction(value) { |
97 |
|
98 |
public void actionPerformed(ActionEvent e) { |
99 |
String key = (String) this.getValue("key"); |
100 |
listener.acceptValue(key); |
101 |
} |
102 |
}; |
103 |
action.putValue("key", key);
|
104 |
this.add(action);
|
105 |
} |
106 |
break;
|
107 |
case VALUE:
|
108 |
String name =
|
109 |
i18nManager.getTranslation(parameter.getName()) + " ("
|
110 |
+ i18nManager.getTranslation("value") + ")"; |
111 |
; |
112 |
action = new AbstractAction(name) { |
113 |
|
114 |
public void actionPerformed(ActionEvent e) { |
115 |
String value = showValueDialog(parameter);
|
116 |
if (value != null) { |
117 |
listener.acceptValue(value); |
118 |
} |
119 |
} |
120 |
}; |
121 |
this.add(action);
|
122 |
break;
|
123 |
|
124 |
case POSITION:
|
125 |
case LIST_POSITIONS:
|
126 |
name = |
127 |
i18nManager.getTranslation(parameter.getName()) + " ("
|
128 |
+ i18nManager.getTranslation("point") + ")"; |
129 |
action = new AbstractAction(name) { |
130 |
|
131 |
public void actionPerformed(ActionEvent e) { |
132 |
String value = showPointDialog(parameter);
|
133 |
if (value != null) { |
134 |
listener.acceptValue(value); |
135 |
} |
136 |
} |
137 |
}; |
138 |
this.add(action);
|
139 |
break;
|
140 |
|
141 |
case SELECTION:
|
142 |
case GEOMETRY:
|
143 |
break;
|
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
this.add(new JSeparator(SwingConstants.HORIZONTAL)); |
149 |
action = new AbstractAction(i18nManager.getTranslation("cancel")) { |
150 |
|
151 |
public void actionPerformed(ActionEvent e) { |
152 |
listener.acceptValue(null);
|
153 |
// setVisible(false);
|
154 |
} |
155 |
}; |
156 |
this.add(action);
|
157 |
|
158 |
((JComponent) parent).add(this); |
159 |
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* @return this as JComponent
|
164 |
*/
|
165 |
public JComponent asJComponent() { |
166 |
return this; |
167 |
} |
168 |
|
169 |
private String showPointDialog(EditingServiceParameter parameter) { |
170 |
|
171 |
int dimension = 2; |
172 |
|
173 |
Object defaultValue = parameter.getDefaultValue();
|
174 |
Point defaultPoint = null; |
175 |
if (defaultValue != null && defaultValue instanceof Point) { |
176 |
defaultPoint = (Point)defaultValue;
|
177 |
dimension = defaultPoint.getDimension(); |
178 |
} |
179 |
|
180 |
final DefaultDynamicEditingPointPanel pointPanel = new DefaultDynamicEditingPointPanel(null, defaultPoint); |
181 |
if(defaultPoint!=null){ |
182 |
for(int i=0; i<dimension; i++){ |
183 |
pointPanel.setValue(i, String.valueOf(defaultPoint.getCoordinateAt(i)));
|
184 |
} |
185 |
} |
186 |
final String title = ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
187 |
|
188 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
189 |
|
190 |
public void run() { |
191 |
this.returnValue =
|
192 |
JOptionPane.showConfirmDialog(null, pointPanel, title, JOptionPane.OK_CANCEL_OPTION); |
193 |
} |
194 |
}; |
195 |
|
196 |
if (SwingUtilities.isEventDispatchThread()) { |
197 |
runnable.run(); |
198 |
} else {
|
199 |
try {
|
200 |
SwingUtilities.invokeAndWait(runnable);
|
201 |
} catch (Exception e) { |
202 |
logger.info("Can't show point dialog.", e);
|
203 |
} |
204 |
} |
205 |
|
206 |
int result = ((Integer) runnable.getReturnValue()); |
207 |
|
208 |
if (result == JOptionPane.OK_OPTION) { |
209 |
StringBuilder stb = new StringBuilder(); |
210 |
for(int i=0; i<dimension; i++){ |
211 |
String value = pointPanel.getValue(i);
|
212 |
stb.append(value); |
213 |
if(i<dimension-1){ |
214 |
stb.append(",");
|
215 |
} |
216 |
} |
217 |
return stb.toString();
|
218 |
} |
219 |
|
220 |
return null; |
221 |
} |
222 |
|
223 |
private String showValueDialog(EditingServiceParameter parameter) { |
224 |
|
225 |
final DefaultEditingValuePanel valuePanel =
|
226 |
new DefaultEditingValuePanel();
|
227 |
|
228 |
final String title = |
229 |
ToolsLocator.getI18nManager().getTranslation(parameter.getName()); |
230 |
|
231 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
232 |
|
233 |
public void run() { |
234 |
this.returnValue =
|
235 |
JOptionPane.showConfirmDialog(null, valuePanel, title, |
236 |
JOptionPane.OK_CANCEL_OPTION);
|
237 |
} |
238 |
}; |
239 |
|
240 |
if (SwingUtilities.isEventDispatchThread()) { |
241 |
runnable.run(); |
242 |
} else {
|
243 |
try {
|
244 |
SwingUtilities.invokeAndWait(runnable);
|
245 |
} catch (Exception e) { |
246 |
logger.info("Can't show value dialog.", e);
|
247 |
} |
248 |
} |
249 |
|
250 |
int result = ((Integer) runnable.getReturnValue()); |
251 |
|
252 |
if (result == JOptionPane.OK_OPTION) { |
253 |
return valuePanel.getValue();
|
254 |
} |
255 |
|
256 |
return null; |
257 |
|
258 |
} |
259 |
|
260 |
} |