Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / StartEditing.java @ 7304

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 com.hardcode.driverManager.Driver;
8
import com.iver.andami.PluginServices;
9
import com.iver.andami.messages.NotificationManager;
10
import com.iver.andami.plugins.Extension;
11
import com.iver.cit.gvsig.fmap.MapContext;
12
import com.iver.cit.gvsig.fmap.MapControl;
13
import com.iver.cit.gvsig.fmap.core.FShape;
14
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
15
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
16
import com.iver.cit.gvsig.fmap.edition.EditionException;
17
import com.iver.cit.gvsig.fmap.edition.IWriteable;
18
import com.iver.cit.gvsig.fmap.edition.IWriter;
19
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
20
import com.iver.cit.gvsig.fmap.edition.rules.IRule;
21
import com.iver.cit.gvsig.fmap.edition.rules.RulePolygon;
22
import com.iver.cit.gvsig.fmap.layers.FLayer;
23
import com.iver.cit.gvsig.fmap.layers.FLayers;
24
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
25
import com.iver.cit.gvsig.fmap.layers.XMLException;
26
import com.iver.cit.gvsig.fmap.rendering.Legend;
27
import com.iver.cit.gvsig.gui.cad.CADTool;
28
import com.iver.cit.gvsig.gui.tokenmarker.ConsoleToken;
29
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
30
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
31
import com.iver.cit.gvsig.project.documents.table.gui.Table;
32
import com.iver.cit.gvsig.project.documents.view.ProjectView;
33
import com.iver.cit.gvsig.project.documents.view.gui.View;
34
import com.iver.utiles.console.jedit.KeywordMap;
35
import com.iver.utiles.console.jedit.Token;
36

    
37
/**
38
 * DOCUMENT ME!
39
 *
40
 * @author Vicente Caballero Navarro
41
 */
42
public class StartEditing extends Extension {
43

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

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

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

    
68
                if (f instanceof View) {
69
                        vista = (View) f;
70

    
71
                        vista.showConsole();
72
                        MapControl mapControl = vista.getMapControl();
73
                        EditionManager editionManager=CADExtension.getEditionManager();
74
                        editionManager.setMapControl(mapControl);
75

    
76
                        ProjectView model = vista.getModel();
77
                        MapContext mapa = model.getMapContext();
78
                        FLayers layers = mapa.getLayers();
79
                        boolean bEditingStarted = false;
80
                        for (int i = 0; i < layers.getLayersCount(); i++) {
81
                                if (layers.getLayer(i) instanceof FLyrVect
82
                                                && layers.getLayer(i).isActive()) {
83
                                        /*
84
                                         * for (int j = 0; j < i; j++) {
85
                                         * layers.getLayer(j).setVisible(false); }
86
                                         */
87

    
88
                                        FLyrVect lv = (FLyrVect) layers.getLayer(i);
89
                                        // lv.setVisible(true);
90
                                        lv.addLayerListener(editionManager);
91
                                        try {
92
                                                Legend legendOriginal=lv.getLegend().cloneLegend();
93

    
94
                                    if (!lv.isWritable())
95
                                    {
96
                                                JOptionPane.showMessageDialog(
97
                                                                (Component) PluginServices.getMDIManager().getActiveWindow(),
98
                                                                PluginServices.getText(this, "this_layer_is_not_self_editable"),
99
                                                                PluginServices.getText(this, "warning_title"), 
100
                                                                JOptionPane.WARNING_MESSAGE);
101
                                    }
102
                                
103
                                                lv.setEditing(true);
104
                                                VectorialEditableAdapter vea = (VectorialEditableAdapter) lv
105
                                                .getSource();
106
                                                
107
                                                vea.getRules().clear();
108
                                                if (vea.getShapeType() == FShape.POLYGON)
109
                                                {
110
                                                        IRule rulePol = new RulePolygon();
111
                                                        vea.getRules().add(rulePol);
112
                                                }
113

    
114
                                                if (!(lv.getSource().getDriver() instanceof IndexedShpDriver)){
115
                                                        VectorialLayerEdited vle=(VectorialLayerEdited)editionManager.getLayerEdited(lv);
116
                                                        vle.setLegend(legendOriginal);
117
                                                }
118
                                                vea.getCommandRecord().addCommandListener(mapControl);
119
                                                // Si existe una tabla asociada a esta capa se cambia su
120
                                                // modelo por el VectorialEditableAdapter.
121
                                                ProjectExtension pe = (ProjectExtension) PluginServices
122
                                                                .getExtension(ProjectExtension.class);
123
                                                ProjectTable pt = pe.getProject().getTable(lv);
124
                                                if (pt != null){
125
                                                        pt.setModel(vea);
126
                                                        changeModelTable(pt);
127
                                                }
128
                                                startCommandsApplicable(vista,lv);
129
                                                vista.repaintMap();
130
                                        } catch (EditionException e) {
131
                                                e.printStackTrace();
132
                                                NotificationManager.addError(e);
133
                                        } catch (DriverIOException e) {
134
                                                e.printStackTrace();
135
                                                NotificationManager.addError(e);
136
                                        } catch (XMLException e) {
137
                                                e.printStackTrace();
138
                                        }
139

    
140
//                                        return;
141
                                }
142
                        }
143

    
144
                        /*
145
                         * PluginServices.getMDIManager().setWaitCursor(); try { if
146
                         * (((FLyrVect) capa).getSource().getDriver().getClass() ==
147
                         * DXFCadDriver.class) { if (JOptionPane.showConfirmDialog(
148
                         * (Component) PluginServices.getMainFrame(), "Todas las geometr?as
149
                         * del formato DXF no se pueden editar, de momento podemos editar:
150
                         * Line, Point, Polyline, Arc, Circle y Ellipse. \n El resto de
151
                         * geometr?as se perder?n con la edici?n. \n ?Desea continuar?") ==
152
                         * JOptionPane.YES_OPTION) { capa.startEdition();
153
                         * vista.getMapControl().setCadTool("selection"); } else { } } else {
154
                         * capa.startEdition();
155
                         * vista.getMapControl().setCadTool("selection"); } } catch
156
                         * (EditionException e) { // TODO Auto-generated catch block
157
                         * e.printStackTrace(); }
158
                         * PluginServices.getMDIManager().restoreCursor();
159
                         */
160
                        // vista.getMapControl().drawMap(false);
161
                }
162
        }
163
//         private void registerKeyStrokes() {
164
//                 JComponent theComponent = vista.getConsolePanel().getTxt();
165
//
166
//                 // The actions
167
//                 Action F3Action = new AbstractAction("REFENT") {
168
//                        public void actionPerformed(ActionEvent evt) {
169
//                                System.err.println("SOY F3");
170
//                        }
171
//                };
172
//
173
//                 InputMap inputMap = theComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
174
//                 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), F3Action.getValue(Action.NAME));
175
//
176
//                 ActionMap actionMap = theComponent.getActionMap();
177
//                 // actionMap.put("REFENT", new MyAction());
178
//                 actionMap.put(F3Action.getValue(Action.NAME), F3Action);
179
//
180
//        }
181

    
182
        public static void startCommandsApplicable(View vista,FLyrVect lv) {
183
                if (vista==null)
184
                        vista=(View)PluginServices.getMDIManager().getActiveWindow();
185
                CADTool[] cadtools = CADExtension.getCADTools();
186
                KeywordMap keywordMap = new KeywordMap(true);
187
                for (int i = 0; i < cadtools.length; i++) {
188
                        try {
189
                                if (cadtools[i].isApplicable(lv.getShapeType())){
190
                                        keywordMap.add(cadtools[i].getName(), Token.KEYWORD2);
191
                                        keywordMap.add(cadtools[i].toString(), Token.KEYWORD3);
192
                                }
193
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
194
                                e.printStackTrace();
195
                        }
196

    
197
                }
198
                ConsoleToken consoletoken = new ConsoleToken(keywordMap);
199
                vista.getConsolePanel().setTokenMarker(consoletoken);
200

    
201
        }
202

    
203
        private void changeModelTable(ProjectTable pt){
204
             com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
205

    
206
                 for (int i=0 ; i<views.length ; i++){
207
                         if (views[i] instanceof Table){
208
                                 Table table=(Table)views[i];
209
                                 ProjectTable model =table.getModel();
210
                                 if (model.equals(pt)){
211
                                                 table.setModel(pt);
212
                                 }
213
                         }
214
                 }
215
    }
216
        /**
217
         * @see com.iver.andami.plugins.IExtension#isEnabled()
218
         */
219
        public boolean isEnabled() {
220
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
221

    
222
                if (f == null) {
223
                        return false;
224
                }
225

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

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

    
243
                if (f == null) {
244
                        return false;
245
                }
246

    
247
                if (f instanceof View)
248
                        return true;
249
                return false;
250
        }
251
}