Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.annotation.app / org.gvsig.annotation.app.extension / src / main / java / org / gvsig / annotation / app / extension / AnnotationExtension.java @ 38564

History | View | Annotate | Download (5.31 KB)

1 33274 jpiera
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.annotation.app.extension;
23
24 37429 jpiera
import java.util.Locale;
25
26 38564 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
27 33274 jpiera
import org.gvsig.andami.PluginServices;
28
import org.gvsig.andami.messages.NotificationManager;
29
import org.gvsig.andami.plugins.Extension;
30
import org.gvsig.annotation.AnnotationCreationService;
31
import org.gvsig.annotation.AnnotationLocator;
32
import org.gvsig.annotation.AnnotationManager;
33
import org.gvsig.annotation.swing.AnnotationSwingLocator;
34
import org.gvsig.annotation.swing.AnnotationSwingManager;
35
import org.gvsig.annotation.swing.AnnotationWindowManager;
36
import org.gvsig.annotation.swing.JAnnotationCreationServicePanel;
37 38194 vacevedo
import org.gvsig.app.ApplicationLocator;
38
import org.gvsig.app.ApplicationManager;
39 33274 jpiera
import org.gvsig.app.project.documents.view.ViewDocument;
40 38194 vacevedo
import org.gvsig.app.project.documents.view.ViewManager;
41 33274 jpiera
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.mapcontext.MapContext;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
45 37429 jpiera
import org.gvsig.i18n.Messages;
46 33278 jpiera
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.extensionpoint.ExtensionPoint;
48
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
49 33274 jpiera
import org.gvsig.tools.service.ServiceException;
50
51
/**
52
 * Andami extension to show Annotation in the application.
53
 *
54
 * @author gvSIG Team
55
 * @version $Id$
56
 */
57
public class AnnotationExtension extends Extension {
58
        private AnnotationManager manager;
59
        private AnnotationSwingManager swingManager;
60
61
        public void initialize() {
62 33278 jpiera
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
63
                ExtensionPoint ep = extensionPoints.add("AplicationPreferences", "");
64
65
                ep.append("AnnotationPreferencesPage", "", new AnnotationPreferencesPage());
66
67 38564 jjdelcerro
                IconThemeHelper.registerIcon("preferences", "annotation-preferences", this);
68 37429 jpiera
69
                if (!Messages.hasLocales()) {
70
                    Messages.addLocale(Locale.getDefault());
71
                }
72
                Messages.addResourceFamily("org.gvsig.annotation.app.extension.i18n.text",
73
                    AnnotationExtension.class.getClassLoader(),
74
                    AnnotationExtension.class.getClass().getName());
75 33274 jpiera
        }
76
77
        @Override
78
        public void postInitialize() {
79
                super.postInitialize();
80
                manager = AnnotationLocator.getManager();
81
                // Asignamos el locator e iniciamos la instancia
82
                swingManager = AnnotationSwingLocator.getSwingManager();
83
84 37429 jpiera
                swingManager.registerWindowManager(new GvSIGAnnotationWindowManager());
85
86 33274 jpiera
        }
87
88 38194 vacevedo
        private FLyrVect getCurrentLayer() {
89
                return this.getCurrentLayer(null);
90
        }
91
92
        private FLyrVect getCurrentLayer(MapContext mapContext) {
93
                FLyrVect layer = null;
94
95
                if( mapContext == null ) {
96
                        ApplicationManager application = ApplicationLocator.getManager();
97 33278 jpiera
98 38194 vacevedo
                        ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
99
                        if( view == null ) {
100
                                return null;
101
                        }
102
                        mapContext = view.getMapContext();
103
                }
104
                FLayer[] actives = mapContext.getLayers().getActives();
105 33278 jpiera
106 38194 vacevedo
                for (int i=0 ; i<actives.length ; i++){
107
                        if (actives[i] instanceof FLyrVect){
108
                                layer = (FLyrVect)actives[i];
109
                                break;
110 33274 jpiera
                        }
111 38194 vacevedo
                }
112
                return layer;
113
        }
114
115
        public void execute(String actionCommand) {
116
                ApplicationManager application = ApplicationLocator.getManager();
117 33278 jpiera
118 38194 vacevedo
                ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
119
                if( view == null ) {
120
                        return;
121 33274 jpiera
                }
122 38194 vacevedo
                MapContext mapContext = view.getMapContext();
123
                FLyrVect layer = getCurrentLayer(mapContext);
124
                if (layer == null){
125
                        return;
126
                }
127
                showAnnotation(manager, layer.getFeatureStore(), mapContext);
128 33274 jpiera
        }
129
130 38194 vacevedo
        public void showAnnotation(AnnotationManager manager, FeatureStore featureStore, MapContext mapContext) {
131 33274 jpiera
                try {
132
                        AnnotationCreationService service =
133
                                (AnnotationCreationService) manager.getAnnotationCreationService(featureStore);
134 38194 vacevedo
                        service.setAnnotationCreationFinishAction(new AddLayerFinishAction(mapContext));
135 33278 jpiera
136 33274 jpiera
                        JAnnotationCreationServicePanel panel =
137
                                swingManager.createAnnotation(service);
138
                        swingManager.getWindowManager().showWindow(panel, "Annotation",
139 36679 jpiera
                                        AnnotationWindowManager.MODE_WINDOW);
140 33274 jpiera
141
                } catch (ServiceException e) {
142
                        NotificationManager.addError(e);
143
                }
144
        }
145
146
        public boolean isEnabled() {
147 38194 vacevedo
                FLayer layer = getCurrentLayer();
148
                if( layer == null ) {
149
                        return false;
150 33274 jpiera
                }
151 38194 vacevedo
                return true;
152 33274 jpiera
        }
153
154
        public boolean isVisible() {
155 38194 vacevedo
                ApplicationManager application = ApplicationLocator.getManager();
156 33274 jpiera
157 38194 vacevedo
                ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
158
                if( view == null ) {
159 33274 jpiera
                        return false;
160
                }
161 38194 vacevedo
                return true;
162 33274 jpiera
        }
163
}