Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_902 / extensions / extCAD / src / com / iver / cit / gvsig / StartEditing.java @ 10681

History | View | Annotate | Download (7.62 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
                        View vista = (View) f;
67
                        
68
                        MapControl mapControl = vista.getMapControl();
69
                        
70
                        IProjectView model = vista.getModel();                                                
71
                        FLayer[] actives = model.getMapContext().getLayers().getActives();
72
                        
73
                        boolean bEditingStarted = false;
74
                        if (actives.length == 1 &&        actives[0] instanceof FLyrVect) {
75

    
76
                                vista.showConsole();
77
                                EditionManager editionManager=CADExtension.getEditionManager();
78
                                editionManager.setMapControl(mapControl);
79

    
80
                                /*
81
                                 * for (int j = 0; j < i; j++) {
82
                                 * layers.getLayer(j).setVisible(false); }
83
                                 */
84
                                
85
                                FLyrVect lv = (FLyrVect) actives[0];
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
                                        
126
                                        startCommandsApplicable(vista,lv);
127
                                        vista.repaintMap();
128
                                        
129
                                } catch (EditionException e) {
130
                                        e.printStackTrace();
131
                                        NotificationManager.addError(e);
132
                                } catch (DriverIOException e) {
133
                                        e.printStackTrace();
134
                                        NotificationManager.addError(e);
135
                                } catch (XMLException e) {
136
                                        e.printStackTrace();
137
                                }
138
                                
139
//                                return;
140
                        }
141
                }
142
                
143
                /*
144
                 * PluginServices.getMDIManager().setWaitCursor(); try { if
145
                 * (((FLyrVect) capa).getSource().getDriver().getClass() ==
146
                 * DXFCadDriver.class) { if (JOptionPane.showConfirmDialog(
147
                 * (Component) PluginServices.getMainFrame(), "Todas las geometr?as
148
                 * del formato DXF no se pueden editar, de momento podemos editar:
149
                 * Line, Point, Polyline, Arc, Circle y Ellipse. \n El resto de
150
                 * geometr?as se perder?n con la edici?n. \n ?Desea continuar?") ==
151
                 * JOptionPane.YES_OPTION) { capa.startEdition();
152
                 * vista.getMapControl().setCadTool("selection"); } else { } } else {
153
                 * capa.startEdition();
154
                 * vista.getMapControl().setCadTool("selection"); } } catch
155
                 * (EditionException e) { // TODO Auto-generated catch block
156
                 * e.printStackTrace(); }
157
                 * PluginServices.getMDIManager().restoreCursor();
158
                 */
159
                // vista.getMapControl().drawMap(false);
160
        }
161

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

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

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

    
200
        }
201

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

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

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

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

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

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

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