Statistics
| Revision:

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

History | View | Annotate | Download (8.32 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4

    
5
import javax.swing.JOptionPane;
6

    
7
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.exceptions.layers.StartEditionLayerException;
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.shp.IndexedShpDriver;
15
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
16
import com.iver.cit.gvsig.fmap.edition.rules.IRule;
17
import com.iver.cit.gvsig.fmap.edition.rules.RulePolygon;
18
import com.iver.cit.gvsig.fmap.layers.FLayer;
19
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
20
import com.iver.cit.gvsig.fmap.layers.XMLException;
21
import com.iver.cit.gvsig.fmap.rendering.ILegend;
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.console.jedit.KeywordMap;
30
import com.iver.utiles.console.jedit.Token;
31

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

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

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

    
55
        /**
56
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
57
         */
58
        public void execute(String actionCommand) {
59

    
60
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
61
                .getActiveWindow();
62

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

    
66
                        MapControl mapControl = vista.getMapControl();
67

    
68
                        IProjectView model = vista.getModel();
69
                        FLayer[] actives = model.getMapContext().getLayers().getActives();
70

    
71
                        boolean bEditingStarted = false;
72
                        if (actives.length == 1 &&        actives[0] instanceof FLyrVect) {
73
                                FLyrVect lv = (FLyrVect) actives[0];
74
                                if (!mapControl.getProjection().getAbrev().equals(lv.getProjection().getAbrev())){
75
                                        NotificationManager.showMessageInfo(PluginServices.getText(this,"no_es_posible_editar_capa_reproyectada"),null);
76
                                        return;
77
                                }
78
                                if (lv.isJoined()) {
79
                                        int resp = JOptionPane.showConfirmDialog((Component) PluginServices
80
                                                        .getMainFrame(), PluginServices.getText(this,"se_perdera_la_union")+
81
                                                        "\n" + PluginServices.getText(this,"desea_continuar"),
82
                                                        PluginServices.getText(this,"start_edition"),
83
                                                        JOptionPane.YES_NO_OPTION);
84
                                        if (resp != JOptionPane.YES_OPTION) { // CANCEL EDITING
85
                                                return; // Salimos sin iniciar edici?n
86
                                        }
87
                                }
88
                                CADExtension.initFocus();
89
                                vista.showConsole();
90
                                EditionManager editionManager=CADExtension.getEditionManager();
91
                                editionManager.setMapControl(mapControl);
92
//                                mapControl.getMapContext().clearAllCachingImageDrawnLayers();
93
                                /*
94
                                 * for (int j = 0; j < i; j++) {
95
                                 * layers.getLayer(j).setVisible(false); }
96
                                 */
97

    
98

    
99
                                // lv.setVisible(true);
100
                                lv.addLayerListener(editionManager);
101
                                try {
102
                                        ILegend legendOriginal=lv.getLegend().cloneLegend();
103

    
104
                                        if (!lv.isWritable())
105
                                        {
106
                                                JOptionPane.showMessageDialog(
107
                                                                (Component) PluginServices.getMDIManager().getActiveWindow(),
108
                                                                PluginServices.getText(this, "this_layer_is_not_self_editable"),
109
                                                                PluginServices.getText(this, "warning_title"),
110
                                                                JOptionPane.WARNING_MESSAGE);
111
                                        }
112
                                        lv.setEditing(true);
113
                                        VectorialEditableAdapter vea = (VectorialEditableAdapter) lv
114
                                        .getSource();
115

    
116
                                        vea.getRules().clear();
117
                                        if (vea.getShapeType() == FShape.POLYGON)
118
                                        {
119
                                                IRule rulePol = new RulePolygon();
120
                                                vea.getRules().add(rulePol);
121
                                        }
122

    
123
                                        if (!(lv.getSource().getDriver() instanceof IndexedShpDriver)){
124
                                                VectorialLayerEdited vle=(VectorialLayerEdited)editionManager.getLayerEdited(lv);
125
                                                vle.setLegend(legendOriginal);
126
                                        }
127
                                        vea.getCommandRecord().addCommandListener(mapControl);
128
                                        // Si existe una tabla asociada a esta capa se cambia su
129
                                        // modelo por el VectorialEditableAdapter.
130
                                        ProjectExtension pe = (ProjectExtension) PluginServices
131
                                        .getExtension(ProjectExtension.class);
132
                                        ProjectTable pt = pe.getProject().getTable(lv);
133
                                        if (pt != null){
134
                                                pt.setModel(vea);
135
                                                changeModelTable(pt,vea);
136
                                        }
137

    
138
                                        startCommandsApplicable(vista,lv);
139
                                        vista.repaintMap();
140

    
141
                                } catch (XMLException e) {
142
                                        NotificationManager.addError(e.getMessage(),e);
143
                                } catch (StartEditionLayerException e) {
144
                                        NotificationManager.addError(e.getMessage(),e);
145
                                } catch (ReadDriverException e) {
146
                                        NotificationManager.addError(e.getMessage(),e);
147
                                }
148

    
149
//                                return;
150
                        }
151
                }
152

    
153
                /*
154
                 * PluginServices.getMDIManager().setWaitCursor(); try { if
155
                 * (((FLyrVect) capa).getSource().getDriver().getClass() ==
156
                 * DXFCadDriver.class) { if (JOptionPane.showConfirmDialog(
157
                 * (Component) PluginServices.getMainFrame(), "Todas las geometr?as
158
                 * del formato DXF no se pueden editar, de momento podemos editar:
159
                 * Line, Point, Polyline, Arc, Circle y Ellipse. \n El resto de
160
                 * geometr?as se perder?n con la edici?n. \n ?Desea continuar?") ==
161
                 * JOptionPane.YES_OPTION) { capa.startEdition();
162
                 * vista.getMapControl().setCadTool("selection"); } else { } } else {
163
                 * capa.startEdition();
164
                 * vista.getMapControl().setCadTool("selection"); } } catch
165
                 * (EditionException e) { // TODO Auto-generated catch block
166
                 * e.printStackTrace(); }
167
                 * PluginServices.getMDIManager().restoreCursor();
168
                 */
169
                // vista.getMapControl().drawMap(false);
170
        }
171

    
172
//         private void registerKeyStrokes() {
173
//                 JComponent theComponent = vista.getConsolePanel().getTxt();
174
//
175
//                 // The actions
176
//                 Action F3Action = new AbstractAction("REFENT") {
177
//                        public void actionPerformed(ActionEvent evt) {
178
//                                System.err.println("SOY F3");
179
//                        }
180
//                };
181
//
182
//                 InputMap inputMap = theComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
183
//                 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), F3Action.getValue(Action.NAME));
184
//
185
//                 ActionMap actionMap = theComponent.getActionMap();
186
//                 // actionMap.put("REFENT", new MyAction());
187
//                 actionMap.put(F3Action.getValue(Action.NAME), F3Action);
188
//
189
//        }
190

    
191
        public static void startCommandsApplicable(View vista,FLyrVect lv) {
192
                if (vista==null)
193
                        vista=(View)PluginServices.getMDIManager().getActiveWindow();
194
                CADTool[] cadtools = CADExtension.getCADTools();
195
                KeywordMap keywordMap = new KeywordMap(true);
196
                for (int i = 0; i < cadtools.length; i++) {
197
                        try {
198
                                if (cadtools[i].isApplicable(lv.getShapeType())){
199
                                        keywordMap.add(cadtools[i].getName(), Token.KEYWORD2);
200
                                        keywordMap.add(cadtools[i].toString(), Token.KEYWORD3);
201
                                }
202
                        } catch (ReadDriverException e) {
203
                                NotificationManager.addError(e.getMessage(),e);
204
                        }
205

    
206
                }
207
                ConsoleToken consoletoken = new ConsoleToken(keywordMap);
208
                vista.getConsolePanel().setTokenMarker(consoletoken);
209

    
210
        }
211

    
212
        protected void changeModelTable(ProjectTable pt, VectorialEditableAdapter vea){
213
            com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
214

    
215
                for (int i=0 ; i<views.length ; i++){
216
                        if (views[i] instanceof Table){
217
                                Table table=(Table)views[i];
218
                                ProjectTable model =table.getModel();
219
                                if (model.equals(pt)){
220
                                                table.setModel(pt);
221
                                                vea.getCommandRecord().addCommandListener(table);
222
                                }
223
                        }
224
                }
225
   }
226
        /**
227
         * @see com.iver.andami.plugins.IExtension#isEnabled()
228
         */
229
        public boolean isEnabled() {
230
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
231

    
232
                if (f == null) {
233
                        return false;
234
                }
235

    
236
                FLayer[] selected = f.getModel().getMapContext().getLayers()
237
                                .getActives();
238
                if (selected.length == 1 && selected[0].isAvailable() && selected[0] instanceof FLyrVect) {
239
                        if (selected[0].isEditing())
240
                                return false;
241
                        return true;
242
                }
243
                return false;
244
        }
245

    
246
        /**
247
         * @see com.iver.andami.plugins.IExtension#isVisible()
248
         */
249
        public boolean isVisible() {
250
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
251
                                .getActiveWindow();
252

    
253
                if (f == null) {
254
                        return false;
255
                }
256

    
257
                if (f instanceof View)
258
                        return true;
259
                return false;
260
        }
261
}