Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / ThemeToAnnotationExtension.java @ 11143

History | View | Annotate | Download (9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.util.ArrayList;
44

    
45
import javax.swing.ImageIcon;
46

    
47
import jwizardcomponent.FinishAction;
48
import jwizardcomponent.JWizardComponents;
49

    
50
import com.hardcode.gdbms.engine.data.driver.DriverException;
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.IWindow;
54
import com.iver.cit.gvsig.fmap.MapContext;
55
import com.iver.cit.gvsig.fmap.core.FShape;
56
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
59
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
60
import com.iver.cit.gvsig.fmap.layers.LayerListener;
61
import com.iver.cit.gvsig.fmap.layers.MappingAnnotation;
62
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
63
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
64
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
65
import com.iver.cit.gvsig.gui.panels.annotation.SelectAnnotationLayerNameAndField;
66
import com.iver.cit.gvsig.gui.simpleWizard.SimpleWizard;
67
import com.iver.cit.gvsig.project.Project;
68
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
69
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
70
import com.iver.cit.gvsig.project.documents.view.IProjectView;
71

    
72

    
73
/**
74
 * DOCUMENT ME!
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class ThemeToAnnotationExtension extends Extension {
79
    private MapContext map=null;
80
        /**
81
     * @see com.iver.andami.plugins.IExtension#initialize()
82
     */
83
    public void initialize() {
84
    }
85

    
86
    /**
87
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
88
     */
89

    
90
    public class MyFinishAction extends FinishAction {
91
            private JWizardComponents myWizardComponents;
92
            private MapContext map;
93
            private FLyrVect layerVectorial;
94
            private FLyrAnnotation layerAnnotation;
95

    
96
                public MyFinishAction(JWizardComponents wizardComponents, MapContext map,FLyrVect layerVectorial, FLyrAnnotation layerAnnotation ) {
97
                        super(wizardComponents);
98
                        this.map = map;
99
                        this.layerVectorial = layerVectorial;
100
                        this.layerAnnotation = layerAnnotation;
101
                        myWizardComponents = wizardComponents;
102

    
103
                }
104

    
105
                public void performAction() {
106

    
107
                        myWizardComponents.getFinishButton().setEnabled(false);
108
                        SelectAnnotationLayerNameAndField panel1 = (SelectAnnotationLayerNameAndField) myWizardComponents.getWizardPanel(0);
109
                        ConfigureLabel panel2 = (ConfigureLabel) myWizardComponents.getWizardPanel(1);
110

    
111

    
112
                        SelectableDataSource source;
113
                        MappingAnnotation mapping=new MappingAnnotation();
114

    
115
                        try {
116
                                source = this.layerAnnotation.getRecordset();
117

    
118

    
119

    
120
                                mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
121

    
122
                                if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
123
                                        mapping.setColumnRotate(source.getFieldIndexByName(panel2.getAngleFieldName()));
124
                                }
125

    
126
                                if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
127
                                        mapping.setColumnColor(source.getFieldIndexByName(panel2.getColorFieldName()));
128
                                }
129

    
130
                                if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
131
                                        mapping.setColumnHeight(source.getFieldIndexByName(panel2.getSizeFieldName()));
132
                                }
133
                                this.layerAnnotation.setInPixels(panel2.sizeUnitsInPixels());
134

    
135
                                if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
136
                                        mapping.setColumnTypeFont(source.getFieldIndexByName(panel2.getFontFieldName()));
137
                                }
138
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
139
                                // TODO Que hacemos aqui
140
                                e.printStackTrace();
141
                                return;
142
                        } catch (DriverException e) {
143
                                // TODO Auto-generated catch block
144
                                e.printStackTrace();
145
                        }
146

    
147

    
148
                        this.layerAnnotation.setName(panel1.getNewLayerName());
149
                        this.layerAnnotation.setMapping(mapping);
150

    
151

    
152
                this.map.getLayers().addLayer(this.layerAnnotation);
153
                this.map.getLayers().removeLayer(this.layerVectorial);
154

    
155
                Project project=((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
156
                ArrayList projectTables=project.getDocumentsByType(ProjectTableFactory.registerName);
157
                for (int i=0;i<projectTables.size();i++){
158
                        ProjectTable pt=(ProjectTable)projectTables.get(i);
159
                        if (pt.getAssociatedTable()!=null && pt.getAssociatedTable().equals(this.layerVectorial)){
160
                                pt.setAssociatedTable(this.layerAnnotation);
161
                        }
162
                }
163

    
164
                this.layerAnnotation.setActive(true);
165
                this.myWizardComponents.getCancelAction().performAction();
166
                }
167

    
168
    }
169
    public void execute(String actionCommand) {
170
        if ("LAYERTOANNOTATION".equals(actionCommand)) {
171
                ImageIcon Logo = new javax.swing.ImageIcon(this.getClass().getClassLoader()
172
                                        .getResource("images/package_graphics.png"));
173

    
174
                SimpleWizard wizard = new SimpleWizard(Logo);
175

    
176
            FLyrVect lv=(FLyrVect)map.getLayers().getActives()[0];
177
            FLyrAnnotation la=new FLyrAnnotation();
178
            LayerListener[] layerListeners=lv.getLayerListeners();
179
            for (int i=0;i<layerListeners.length;i++) {
180
                    la.addLayerListener(layerListeners[i]);
181
            }
182

    
183
            la.setSource(lv.getSource());
184
            la.setProjection(lv.getProjection());
185

    
186
                SelectAnnotationLayerNameAndField panel1 = new SelectAnnotationLayerNameAndField(wizard.getWizardComponents(),la);
187
                ConfigureLabel panel2 = new ConfigureLabel(wizard.getWizardComponents(),la);
188

    
189

    
190

    
191
                wizard.getWizardComponents().addWizardPanel(panel1);
192
                wizard.getWizardComponents().addWizardPanel(panel2);
193

    
194
                        wizard.getWizardComponents().setFinishAction(
195
                                        new MyFinishAction(wizard.getWizardComponents(),
196
                                                        map,lv, la));
197

    
198
                        wizard.getWindowInfo().setWidth(540);
199
                        wizard.getWindowInfo().setHeight(380);
200
                        wizard.getWindowInfo().setTitle(PluginServices.getText(this,"to_annotation"));
201

    
202
                        PluginServices.getMDIManager().addWindow(wizard);
203

    
204

    
205

    
206
                /*
207
            FLyrVect lv=(FLyrVect)map.getLayers().getActives()[0];
208
            FLyrAnnotation la=new FLyrAnnotation();
209
            la.setSource(lv.getSource());
210

211
                        MappingFieldsToAnotation mfta=new MappingFieldsToAnotation(la);
212
            PluginServices.getMDIManager().addView(mfta);
213

214
            if (mfta.isOk()){
215
                    map.getLayers().addLayer(la);
216
                    map.getLayers().removeLayer(lv);
217
            }
218
            */
219

    
220
        }
221
    }
222

    
223
    /**
224
     * @see com.iver.andami.plugins.IExtension#isEnabled()
225
     */
226
    public boolean isEnabled() {
227
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
228

    
229
        if (v != null && v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
230
                com.iver.cit.gvsig.project.documents.view.gui.View vista=(com.iver.cit.gvsig.project.documents.view.gui.View)v;
231
                IProjectView model = vista.getModel();
232
                    map = model.getMapContext();
233
                    FLayer[] layers=map.getLayers().getActives();
234
                    if (layers.length==1){
235
                            if (layers[0].isAvailable() && layers[0] instanceof FLyrVect){
236
                                    FLyrVect lv=(FLyrVect)layers[0];
237
                                    ReadableVectorial src = lv.getSource();
238
                                    try {
239
                                                if (src == null || src.getShapeType()==FShape.POLYGON || src.getShapeType()==FShape.LINE)
240
                                                        return false;
241
                                                SelectableDataSource sds=lv.getSource().getRecordset();
242
                                                if (sds.getFieldCount()>0)
243
                                                        return true;
244
                                        } catch (DriverException e) {
245
                                                return false;
246
                                        } catch (DriverIOException e1) {
247
                                                e1.printStackTrace();
248
                                        }
249
                            }
250
                    }
251
        }
252
        return false;
253
    }
254

    
255
    /**
256
     * @see com.iver.andami.plugins.IExtension#isVisible()
257
     */
258
    public boolean isVisible() {
259
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
260

    
261
        if (v == null) {
262
            return false;
263
        } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
264
            return true;
265
        } else {
266
            return false;
267
        }
268
    }
269

    
270

    
271
}