Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / StartEditing.java @ 24500

History | View | Annotate | Download (7.94 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4

    
5
import javax.swing.JOptionPane;
6

    
7
import org.gvsig.fmap.dal.exception.DataException;
8
import org.gvsig.fmap.dal.exception.ReadException;
9
import org.gvsig.fmap.dal.feature.FeatureRule;
10
import org.gvsig.fmap.dal.feature.FeatureType;
11
import org.gvsig.fmap.dal.feature.rule.FeatureRulePolygon;
12
import org.gvsig.fmap.geom.Geometry;
13
import org.gvsig.fmap.geom.primitive.FShape;
14
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
15
import org.gvsig.fmap.mapcontext.layers.FLayer;
16
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
17
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
18
import org.gvsig.fmap.mapcontrol.MapControl;
19

    
20
import com.iver.andami.PluginServices;
21
import com.iver.andami.messages.NotificationManager;
22
import com.iver.andami.plugins.Extension;
23
import com.iver.cit.gvsig.gui.cad.CADTool;
24
import com.iver.cit.gvsig.gui.tokenmarker.ConsoleToken;
25
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
26
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
27
import com.iver.cit.gvsig.project.documents.table.gui.Table;
28
import com.iver.cit.gvsig.project.documents.view.IProjectView;
29
import com.iver.cit.gvsig.project.documents.view.gui.View;
30
import com.iver.utiles.XMLException;
31
import com.iver.utiles.console.jedit.KeywordMap;
32
import com.iver.utiles.console.jedit.Token;
33

    
34
/**
35
 * DOCUMENT ME!
36
 *
37
 * @author Vicente Caballero Navarro
38
 */
39
public class StartEditing extends Extension {
40

    
41
//        private class MyAction extends AbstractAction
42
//        {
43
//
44
//                public void actionPerformed(ActionEvent e) {
45
//                        System.err.println("F3");
46
//                }
47
//
48
//        }
49

    
50
        //View vista;
51
        /**
52
         * @see com.iver.andami.plugins.IExtension#initialize()
53
         */
54
        public void initialize() {
55
        }
56

    
57
        /**
58
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
59
         */
60
        public void execute(String actionCommand) {
61
                CADExtension.initFocus();
62
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
63
                .getActiveWindow();
64

    
65
                if (f instanceof View) {
66
                        View vista = (View) f;
67

    
68
                        MapControl mapControl = vista.getMapControl();
69
                        mapControl.initializeSnapping();
70
                        IProjectView model = vista.getModel();
71
                        FLayer[] actives = model.getMapContext().getLayers().getActives();
72

    
73
                        boolean bEditingStarted = false;
74
                        if (actives.length == 1 &&        actives[0] instanceof FLyrVect) {
75

    
76
                                vista.showConsole();
77
                                EditionManager editionManager=CADExtension.getEditionManager();
78
                                editionManager.setMapControl(mapControl);
79
                                mapControl.getMapContext().clearAllCachingImageDrawnLayers();
80
                                /*
81
                                 * for (int j = 0; j < i; j++) {
82
                                 * layers.getLayer(j).setVisible(false); }
83
                                 */
84

    
85
                                FLyrVect lv = (FLyrVect) actives[0];
86
                                // lv.setVisible(true);
87
                                lv.addLayerListener(editionManager);
88
                                try {
89
                                        ILegend legendOriginal=lv.getLegend().cloneLegend();
90

    
91
                                        if (!lv.isWritable())
92
                                        {
93
                                                JOptionPane.showMessageDialog(
94
                                                                (Component) PluginServices.getMDIManager().getActiveWindow(),
95
                                                                PluginServices.getText(this, "this_layer_is_not_self_editable"),
96
                                                                PluginServices.getText(this, "warning_title"),
97
                                                                JOptionPane.WARNING_MESSAGE);
98
                                        }
99

    
100
                                        lv.setEditing(true);
101
                                        FeatureType featureType=lv.getFeatureStore().getDefaultFeatureType();
102
                                        featureType.getRules().clear();
103
//                                        if (lv.getShapeType()==Geometry.TYPES.SURFACE){
104
//                                                FeatureRule rulePol = new FeatureRulePolygon("PolygonRule", "Sentido correcto de un pol?gono");
105
//                                                featureType.addRule(rulePol);
106
//                                        }
107

    
108
//                                        if (!(lv.getSource().getDriver() instanceof IndexedShpDriver)){
109
//                                                VectorialLayerEdited vle=(VectorialLayerEdited)editionManager.getLayerEdited(lv);
110
//                                                vle.setLegend(legendOriginal);
111
//                                        }
112
                                        lv.getFeatureStore().addObserver(mapControl);
113
//                                        vea.getCommandRecord().addCommandListener(mapControl);
114
                                        // Si existe una tabla asociada a esta capa se cambia su
115
                                        // modelo por el VectorialEditableAdapter.
116
                                        ProjectExtension pe = (ProjectExtension) PluginServices
117
                                        .getExtension(ProjectExtension.class);
118
                                        ProjectTable pt = pe.getProject().getTable(lv);
119
                                        if (pt != null){
120
                                                pt.setModel(lv.getFeatureStore());
121
//                                                changeModelTable(pt,vea);
122
                                        }
123

    
124
                                        startCommandsApplicable(vista,lv);
125
                                        vista.repaintMap();
126

    
127
                                } catch (XMLException e) {
128
                                        NotificationManager.addError(e.getMessage(),e);
129
                                } catch (StartEditionLayerException e) {
130
                                        NotificationManager.addError(e.getMessage(),e);
131
                                } catch (ReadException e) {
132
                                        NotificationManager.addError(e.getMessage(),e);
133
                                } catch (DataException e) {
134
                                        NotificationManager.addError(e.getMessage(),e);
135
                                }
136

    
137
//                                return;
138
                        }
139
                }
140

    
141
                /*
142
                 * PluginServices.getMDIManager().setWaitCursor(); try { if
143
                 * (((FLyrVect) capa).getSource().getDriver().getClass() ==
144
                 * DXFCadDriver.class) { if (JOptionPane.showConfirmDialog(
145
                 * (Component) PluginServices.getMainFrame(), "Todas las geometr?as
146
                 * del formato DXF no se pueden editar, de momento podemos editar:
147
                 * Line, Point, Polyline, Arc, Circle y Ellipse. \n El resto de
148
                 * geometr?as se perder?n con la edici?n. \n ?Desea continuar?") ==
149
                 * JOptionPane.YES_OPTION) { capa.startEdition();
150
                 * vista.getMapControl().setCadTool("selection"); } else { } } else {
151
                 * capa.startEdition();
152
                 * vista.getMapControl().setCadTool("selection"); } } catch
153
                 * (EditionException e) { // TODO Auto-generated catch block
154
                 * e.printStackTrace(); }
155
                 * PluginServices.getMDIManager().restoreCursor();
156
                 */
157
                // vista.getMapControl().drawMap(false);
158
        }
159

    
160
//         private void registerKeyStrokes() {
161
//                 JComponent theComponent = vista.getConsolePanel().getTxt();
162
//
163
//                 // The actions
164
//                 Action F3Action = new AbstractAction("REFENT") {
165
//                        public void actionPerformed(ActionEvent evt) {
166
//                                System.err.println("SOY F3");
167
//                        }
168
//                };
169
//
170
//                 InputMap inputMap = theComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
171
//                 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), F3Action.getValue(Action.NAME));
172
//
173
//                 ActionMap actionMap = theComponent.getActionMap();
174
//                 // actionMap.put("REFENT", new MyAction());
175
//                 actionMap.put(F3Action.getValue(Action.NAME), F3Action);
176
//
177
//        }
178

    
179
        public static void startCommandsApplicable(View vista,FLyrVect lv) {
180
                if (vista==null)
181
                        vista=(View)PluginServices.getMDIManager().getActiveWindow();
182
                CADTool[] cadtools = CADExtension.getCADTools();
183
                KeywordMap keywordMap = new KeywordMap(true);
184
                for (int i = 0; i < cadtools.length; i++) {
185
                        try {
186
                                if (cadtools[i].isApplicable(lv.getShapeType())){
187
                                        keywordMap.add(cadtools[i].getName(), Token.KEYWORD2);
188
                                        keywordMap.add(cadtools[i].toString(), Token.KEYWORD3);
189
                                }
190
                        } catch (ReadException e) {
191
                                NotificationManager.addError(e.getMessage(),e);
192
                        }
193

    
194
                }
195
                ConsoleToken consoletoken = new ConsoleToken(keywordMap);
196
                vista.getConsolePanel().setTokenMarker(consoletoken);
197

    
198
        }
199

    
200
//        private void changeModelTable(ProjectTable pt, VectorialEditableAdapter vea){
201
//            com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
202
//
203
//                for (int i=0 ; i<views.length ; i++){
204
//                        if (views[i] instanceof Table){
205
//                                Table table=(Table)views[i];
206
//                                ProjectTable model =table.getModel();
207
//                                if (model.equals(pt)){
208
//                                                table.setModel(pt);
209
//                                                vea.getCommandRecord().addCommandListener(table);
210
//                                }
211
//                        }
212
//                }
213
//   }
214
        /**
215
         * @see com.iver.andami.plugins.IExtension#isEnabled()
216
         */
217
        public boolean isEnabled() {
218
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
219

    
220
                if (f == null) {
221
                        return false;
222
                }
223

    
224
                FLayer[] selected = f.getModel().getMapContext().getLayers()
225
                                .getActives();
226
                if (selected.length == 1 && selected[0].isAvailable() && selected[0] instanceof FLyrVect) {
227
                        if (selected[0].isEditing())
228
                                return false;
229
                        return true;
230
                }
231
                return false;
232
        }
233

    
234
        /**
235
         * @see com.iver.andami.plugins.IExtension#isVisible()
236
         */
237
        public boolean isVisible() {
238
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
239
                                .getActiveWindow();
240

    
241
                if (f == null) {
242
                        return false;
243
                }
244

    
245
                if (f instanceof View)
246
                        return true;
247
                return false;
248
        }
249
}