Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / StartEditing.java @ 15602

History | View | Annotate | Download (7.66 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
                CADExtension.initFocus();
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

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

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

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

    
98
                                        lv.setEditing(true);
99
                                        VectorialEditableAdapter vea = (VectorialEditableAdapter) lv
100
                                        .getSource();
101

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

    
109
                                        if (!(lv.getSource().getDriver() instanceof IndexedShpDriver)){
110
                                                VectorialLayerEdited vle=(VectorialLayerEdited)editionManager.getLayerEdited(lv);
111
                                                vle.setLegend(legendOriginal);
112
                                        }
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(vea);
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 (ReadDriverException e) {
132
                                        NotificationManager.addError(e.getMessage(),e);
133
                                }
134

    
135
//                                return;
136
                        }
137
                }
138

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

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

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

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

    
196
        }
197

    
198
        private void changeModelTable(ProjectTable pt, VectorialEditableAdapter vea){
199
            com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
200

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

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

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

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

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

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