Statistics
| Revision:

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

History | View | Annotate | Download (7.78 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4

    
5
import javax.swing.JOptionPane;
6

    
7
import com.iver.andami.PluginServices;
8
import com.iver.andami.messages.NotificationManager;
9
import com.iver.andami.plugins.Extension;
10
import com.iver.cit.gvsig.fmap.MapContext;
11
import com.iver.cit.gvsig.fmap.MapControl;
12
import com.iver.cit.gvsig.fmap.core.FShape;
13
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
14
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
15
import com.iver.cit.gvsig.fmap.edition.EditionException;
16
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
17
import com.iver.cit.gvsig.fmap.edition.rules.IRule;
18
import com.iver.cit.gvsig.fmap.edition.rules.RulePolygon;
19
import com.iver.cit.gvsig.fmap.layers.FLayer;
20
import com.iver.cit.gvsig.fmap.layers.FLayers;
21
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
22
import com.iver.cit.gvsig.fmap.layers.XMLException;
23
import com.iver.cit.gvsig.fmap.rendering.Legend;
24
import com.iver.cit.gvsig.gui.cad.CADTool;
25
import com.iver.cit.gvsig.gui.tokenmarker.ConsoleToken;
26
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
27
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
28
import com.iver.cit.gvsig.project.documents.table.gui.Table;
29
import com.iver.cit.gvsig.project.documents.view.IProjectView;
30
import com.iver.cit.gvsig.project.documents.view.gui.View;
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
                        vista = (View) f;
67

    
68
                        vista.showConsole();
69
                        MapControl mapControl = vista.getMapControl();
70
                        EditionManager editionManager=CADExtension.getEditionManager();
71
                        editionManager.setMapControl(mapControl);
72

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

    
85
                                        FLyrVect lv = (FLyrVect) layers.getLayer(i);
86
                                        // lv.setVisible(true);
87
                                        lv.addLayerListener(editionManager);
88
                                        try {
89
                                                Legend 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
                                                VectorialEditableAdapter vea = (VectorialEditableAdapter) lv
102
                                                .getSource();
103

    
104
                                                vea.getRules().clear();
105
                                                if (vea.getShapeType() == FShape.POLYGON)
106
                                                {
107
                                                        IRule rulePol = new RulePolygon();
108
                                                        vea.getRules().add(rulePol);
109
                                                }
110

    
111
                                                if (!(lv.getSource().getDriver() instanceof IndexedShpDriver)){
112
                                                        VectorialLayerEdited vle=(VectorialLayerEdited)editionManager.getLayerEdited(lv);
113
                                                        vle.setLegend(legendOriginal);
114
                                                }
115
                                                vea.getCommandRecord().addCommandListener(mapControl);
116
                                                // Si existe una tabla asociada a esta capa se cambia su
117
                                                // modelo por el VectorialEditableAdapter.
118
                                                ProjectExtension pe = (ProjectExtension) PluginServices
119
                                                                .getExtension(ProjectExtension.class);
120
                                                ProjectTable pt = pe.getProject().getTable(lv);
121
                                                if (pt != null){
122
                                                        pt.setModel(vea);
123
                                                        changeModelTable(pt);
124
                                                }
125
                                                startCommandsApplicable(vista,lv);
126
                                                vista.repaintMap();
127
                                        } catch (EditionException e) {
128
                                                e.printStackTrace();
129
                                                NotificationManager.addError(e);
130
                                        } catch (DriverIOException e) {
131
                                                e.printStackTrace();
132
                                                NotificationManager.addError(e);
133
                                        } catch (XMLException e) {
134
                                                e.printStackTrace();
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 (com.iver.cit.gvsig.fmap.DriverException e) {
191
                                e.printStackTrace();
192
                        }
193

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

    
198
        }
199

    
200
        private void changeModelTable(ProjectTable pt){
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
                                 }
210
                         }
211
                 }
212
    }
213
        /**
214
         * @see com.iver.andami.plugins.IExtension#isEnabled()
215
         */
216
        public boolean isEnabled() {
217
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
218

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

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

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

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

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