Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / StartEditing.java @ 23657

History | View | Annotate | Download (7.8 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.data.ReadException;
8
import org.gvsig.fmap.data.feature.FeatureRule;
9
import org.gvsig.fmap.data.feature.FeatureType;
10
import org.gvsig.fmap.data.feature.rule.FeatureRulePolygon;
11
import org.gvsig.fmap.geom.Geometry;
12
import org.gvsig.fmap.geom.primitive.FShape;
13
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
14
import org.gvsig.fmap.mapcontext.layers.FLayer;
15
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
16
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
17
import org.gvsig.fmap.mapcontrol.MapControl;
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
134
//                                return;
135
                        }
136
                }
137

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

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

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

    
191
                }
192
                ConsoleToken consoletoken = new ConsoleToken(keywordMap);
193
                vista.getConsolePanel().setTokenMarker(consoletoken);
194

    
195
        }
196

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

    
217
                if (f == null) {
218
                        return false;
219
                }
220

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

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

    
238
                if (f == null) {
239
                        return false;
240
                }
241

    
242
                if (f instanceof View)
243
                        return true;
244
                return false;
245
        }
246
}