Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ThemeToAnnotationExtension.java @ 28407

History | View | Annotate | Download (9.87 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.driver.exceptions.ReadDriverException;
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.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
58
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
59
import com.iver.cit.gvsig.fmap.layers.LayerListener;
60
import com.iver.cit.gvsig.fmap.layers.MappingAnnotation;
61
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
62
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
63
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
64
import com.iver.cit.gvsig.gui.panels.annotation.SelectAnnotationLayerNameAndField;
65
import com.iver.cit.gvsig.gui.simpleWizard.SimpleWizard;
66
import com.iver.cit.gvsig.project.Project;
67
import com.iver.cit.gvsig.project.documents.ProjectDocument;
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
import com.iver.cit.gvsig.project.documents.view.gui.View;
72

    
73

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

    
89
    private void registerIcons(){
90

    
91
    }
92

    
93
    /**
94
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
95
     */
96

    
97
    public class MyFinishAction extends FinishAction {
98
        private JWizardComponents myWizardComponents;
99
        private MapContext map;
100
        private FLyrVect layerVectorial;
101
        private FLyrAnnotation layerAnnotation;
102

    
103
        public MyFinishAction(JWizardComponents wizardComponents, MapContext map,FLyrVect layerVectorial, FLyrAnnotation layerAnnotation ) {
104
            super(wizardComponents);
105
            this.map = map;
106
            this.layerVectorial = layerVectorial;
107
            this.layerAnnotation = layerAnnotation;
108
            myWizardComponents = wizardComponents;
109

    
110
        }
111

    
112
        public void performAction() {
113

    
114
            myWizardComponents.getFinishButton().setEnabled(false);
115
            SelectAnnotationLayerNameAndField panel1 = (SelectAnnotationLayerNameAndField) myWizardComponents.getWizardPanel(0);
116
            ConfigureLabel panel2 = (ConfigureLabel) myWizardComponents.getWizardPanel(1);
117

    
118

    
119
            SelectableDataSource source;
120
            MappingAnnotation mapping=new MappingAnnotation();
121

    
122
            try {
123
                source = this.layerAnnotation.getRecordset();
124

    
125

    
126

    
127
                mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
128

    
129
                if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
130
                    mapping.setColumnRotate(source.getFieldIndexByName(panel2.getAngleFieldName()));
131
                }
132

    
133
                if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
134
                    mapping.setColumnColor(source.getFieldIndexByName(panel2.getColorFieldName()));
135
                }
136

    
137
                if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
138
                    mapping.setColumnHeight(source.getFieldIndexByName(panel2.getSizeFieldName()));
139
                }
140
                this.layerAnnotation.setInPixels(panel2.sizeUnitsInPixels());
141

    
142
                if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
143
                    mapping.setColumnTypeFont(source.getFieldIndexByName(panel2.getFontFieldName()));
144
                }
145
            } catch (ReadDriverException e) {
146
                e.printStackTrace();
147
                return;
148
            }
149

    
150

    
151
            this.layerAnnotation.setName(panel1.getNewLayerName());
152
            this.layerAnnotation.setMapping(mapping);
153

    
154

    
155
            this.map.getLayers().addLayer(this.layerAnnotation);
156
            this.map.getLayers().removeLayer(this.layerVectorial);
157

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

    
167
            this.layerAnnotation.setActive(true);
168
            this.myWizardComponents.getCancelAction().performAction();
169
            PluginServices.getMainFrame().enableControls();
170
        }
171

    
172
    }
173
    public void execute(String actionCommand) {
174
        if ("LAYERTOANNOTATION".equals(actionCommand)) {
175
            ImageIcon Logo = new javax.swing.ImageIcon(this.getClass().getClassLoader()
176
                    .getResource("images/package_graphics.png"));
177

    
178
            SimpleWizard wizard = new SimpleWizard(Logo);
179

    
180
            FLyrVect lv=(FLyrVect)map.getLayers().getActives()[0];
181
            FLyrAnnotation la=new FLyrAnnotation();
182
            LayerListener[] layerListeners=lv.getLayerListeners();
183
            for (int i=0;i<layerListeners.length;i++) {
184
                la.addLayerListener(layerListeners[i]);
185
            }
186

    
187
            la.setSource(lv.getSource());
188
            la.setProjection(lv.getProjection());
189

    
190
            SelectAnnotationLayerNameAndField panel1 = new SelectAnnotationLayerNameAndField(wizard.getWizardComponents(),la);
191
            ConfigureLabel panel2 = new ConfigureLabel(wizard.getWizardComponents(),la);
192

    
193

    
194

    
195
            wizard.getWizardComponents().addWizardPanel(panel1);
196
            wizard.getWizardComponents().addWizardPanel(panel2);
197

    
198
            wizard.getWizardComponents().setFinishAction(
199
                    new MyFinishAction(wizard.getWizardComponents(),
200
                            map,lv, la));
201

    
202
            wizard.getWindowInfo().setWidth(540);
203
            wizard.getWindowInfo().setHeight(380);
204
            wizard.getWindowInfo().setTitle(PluginServices.getText(this,"to_annotation"));
205

    
206
            PluginServices.getMDIManager().addWindow(wizard);
207
            ((ProjectDocument)((View)view).getModel()).setModified(true);
208

    
209

    
210
            /*
211
            FLyrVect lv=(FLyrVect)map.getLayers().getActives()[0];
212
            FLyrAnnotation la=new FLyrAnnotation();
213
            la.setSource(lv.getSource());
214

215
            MappingFieldsToAnotation mfta=new MappingFieldsToAnotation(la);
216
            PluginServices.getMDIManager().addView(mfta);
217

218
            if (mfta.isOk()){
219
                map.getLayers().addLayer(la);
220
                map.getLayers().removeLayer(lv);
221
            }
222
            */
223

    
224
        }
225
    }
226

    
227
    /**
228
     * @see com.iver.andami.plugins.IExtension#isEnabled()
229
     */
230
    public boolean isEnabled() {
231
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
232

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

    
257
    /**
258
     * @see com.iver.andami.plugins.IExtension#isVisible()
259
     */
260
    public boolean isVisible() {
261
        view = PluginServices.getMDIManager().getActiveWindow();
262

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

    
272

    
273
}