Statistics
| Revision:

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 @ 2125

History | View | Annotate | Download (9.75 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

    
32
import javax.swing.AbstractAction;
33
import javax.swing.JComponent;
34
import javax.swing.JOptionPane;
35
import javax.swing.JSeparator;
36
import javax.swing.SwingConstants;
37
import javax.swing.SwingUtilities;
38
import org.apache.commons.lang3.StringUtils;
39

    
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
import org.gvsig.fmap.geom.primitive.Point;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.i18n.I18nManager;
46
import org.gvsig.tools.task.RunnableWithParameters;
47
import org.gvsig.vectorediting.lib.api.EditingService;
48
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
49
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
50
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
51
import org.gvsig.vectorediting.swing.api.EditingContext;
52
import org.gvsig.vectorediting.swing.api.contextmenu.AcceptValueListener;
53
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu;
54

    
55
/**
56
 * @author llmarques
57
 *
58
 */
59
public class DefaultEditingContextMenu extends EditingContextMenuView implements
60
EditingContextMenu {
61

    
62
    private static Logger LOGGER = LoggerFactory
63
        .getLogger(DefaultEditingContextMenu.class);
64

    
65
    private static final long serialVersionUID = -3207749321400180897L;
66

    
67
    private AcceptValueListener listener;
68

    
69
    private Component parent;
70

    
71
    private EditingServiceParameter parameter;
72

    
73
    private EditingContext context;
74

    
75
    /**
76
     * @param parent
77
     * @param listener
78
     * @param parameter
79
     */
80
    public DefaultEditingContextMenu(Component parent,
81
        AcceptValueListener listener, EditingServiceParameter parameter) {
82

    
83
        this(parent, listener, parameter, null);
84
    }
85

    
86
    /**
87
     * @param parent
88
     * @param listener
89
     * @param parameter
90
     * @param context
91
     */
92
    public DefaultEditingContextMenu(Component parent,
93
        AcceptValueListener listener, EditingServiceParameter parameter, EditingContext context) {
94

    
95
        super();
96
        this.listener = listener;
97
        this.parent = parent;
98
        this.parameter = parameter;
99
        this.context = context;
100
        initActions();
101
    }
102

    
103
    @SuppressWarnings("serial")
104
    private void initActions() {
105
        
106
        EditingService service = context.getActiveService();
107
        List<EditingServiceParameter> parameters = service.getParameters();
108
        
109
        for (EditingServiceParameter param : parameters) {
110
            if(param.equals(this.parameter)){
111
                continue;
112
            }
113
            if(param.isOptional() && service.isEnabled(param)){
114
                initAction(param, new AcceptValueListener() {
115

    
116
                    @Override
117
                    public void acceptValue(Object value) {
118
                        try {
119
                            context.setValue(param, (String)value);
120
                        } catch (InvalidEntryException ex) {
121
                            LOGGER.warn("Invalid value", ex);
122
                        }
123
                    }
124
                });
125
            }
126
        }
127
        
128
        initAction(parameter, this.listener);
129

    
130
        I18nManager i18nManager = ToolsLocator.getI18nManager();
131
        AbstractAction action = null;
132

    
133
        this.add(new JSeparator(SwingConstants.HORIZONTAL));
134
        action = new AbstractAction(i18nManager.getTranslation("cancel")) {
135

    
136
            public void actionPerformed(ActionEvent e) {
137
                listener.acceptValue(null);
138
            }
139
        };
140
        this.add(action);
141

    
142

    
143
        ((JComponent) parent).add(this);
144

    
145
    }
146
    
147
    private void initAction(EditingServiceParameter param, AcceptValueListener listener){
148
        
149
        I18nManager i18nManager = ToolsLocator.getI18nManager();
150
        AbstractAction action = null;
151

    
152
        for (final TYPE type : param.getTypes()) {
153

    
154
            switch (type) {
155
            case OPTION:
156
                Map<String, String> options = param.getOptions();
157
                for (String key : options.keySet()) {
158
                    String value = i18nManager.getTranslation(options.get(key));
159
                    action = new AbstractAction(value) {
160

    
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
                    public void actionPerformed(ActionEvent e) {
177
                        String value = showValueDialog(param);
178
                        if (StringUtils.isNotEmpty(value)) {
179
                            listener.acceptValue(value);
180
                        }
181
                    }
182
                };
183
                this.add(action);
184
                break;
185

    
186
            case POSITION:
187
            case LIST_POSITIONS:
188
                name =
189
                i18nManager.getTranslation(param.getName()) + " ("
190
                    + i18nManager.getTranslation("point") + ")";
191
                action = new AbstractAction(name) {
192

    
193
                    public void actionPerformed(ActionEvent e) {
194
                        String value = showPointDialog(param);
195
                        if (value != null) {
196
                            listener.acceptValue(value);
197
                        }
198
                    }
199
                };
200
                this.add(action);
201
                break;
202

    
203
            case SELECTION:
204
            case GEOMETRY:
205
                break;
206
            }
207

    
208
        }
209

    
210
    }
211

    
212
    /**
213
     * @return this as JComponent
214
     */
215
    public JComponent asJComponent() {
216
        return this;
217
    }
218

    
219
    private String showPointDialog(EditingServiceParameter parameter) {
220

    
221
        int dimension = 2;
222

    
223
        Object defaultValue = parameter.getDefaultValue();
224
        Point defaultPoint = null;
225
        if (defaultValue != null && defaultValue instanceof Point) {
226
            defaultPoint = (Point)defaultValue;
227
            dimension = defaultPoint.getDimension();
228
        }
229

    
230
        final DefaultDynamicEditingPointPanel pointPanel = new DefaultDynamicEditingPointPanel(null, defaultPoint);
231
        if(defaultPoint!=null){
232
            for(int i=0; i<dimension; i++){
233
                pointPanel.setValue(i, String.valueOf(defaultPoint.getCoordinateAt(i)));
234
            }
235
        }
236
        final String title = ToolsLocator.getI18nManager().getTranslation(parameter.getName());
237

    
238
        RunnableWithParameters runnable = new RunnableWithParameters() {
239

    
240
            public void run() {
241
                this.returnValue =
242
                    JOptionPane.showConfirmDialog(null, pointPanel, title, JOptionPane.OK_CANCEL_OPTION);
243
            }
244
        };
245

    
246
        if (SwingUtilities.isEventDispatchThread()) {
247
            runnable.run();
248
        } else {
249
            try {
250
                SwingUtilities.invokeAndWait(runnable);
251
            } catch (Exception e) {
252
                LOGGER.info("Can't show point dialog.", e);
253
            }
254
        }
255

    
256
            int result = ((Integer) runnable.getReturnValue());
257

    
258
            if (result == JOptionPane.OK_OPTION) {
259
                StringBuilder stb = new StringBuilder();
260
                for(int i=0; i<dimension; i++){
261
                    String value = pointPanel.getValue(i);
262
                    stb.append(value);
263
                    if(i<dimension-1){
264
                        stb.append(",");
265
                    }
266
                }
267
                return stb.toString();
268
            }
269

    
270
            return null;
271
    }
272

    
273
    private String showValueDialog(EditingServiceParameter parameter) {
274

    
275
        final DefaultEditingValuePanel valuePanel =
276
            new DefaultEditingValuePanel();
277

    
278
        final String title =
279
            ToolsLocator.getI18nManager().getTranslation(parameter.getName());
280

    
281
        RunnableWithParameters runnable = new RunnableWithParameters() {
282

    
283
            public void run() {
284
                this.returnValue =
285
                    JOptionPane.showConfirmDialog(null, valuePanel, title,
286
                        JOptionPane.OK_CANCEL_OPTION);
287
            }
288
        };
289

    
290
        if (SwingUtilities.isEventDispatchThread()) {
291
            runnable.run();
292
        } else {
293
            try {
294
                SwingUtilities.invokeAndWait(runnable);
295
            } catch (Exception e) {
296
                LOGGER.info("Can't show value dialog.", e);
297
            }
298
        }
299

    
300
        int result = ((Integer) runnable.getReturnValue());
301

    
302
        if (result == JOptionPane.OK_OPTION) {
303
            return valuePanel.getValue();
304
        }
305

    
306
        return null;
307

    
308
    }
309

    
310
}