Statistics
| Revision:

gvsig-plugintemplates / org.gvsig.landregistryviewer.app / trunk / org.gvsig.landregistryviewer.app / org.gvsig.landregistryviewer.app.mainplugin / src / main / java / org / gvsig / landregistryviewer / app / mainplugin / LandRegistryViewerExtension.java @ 145

History | View | Annotate | Download (7.03 KB)

1
/* 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.landregistryviewer.app.mainplugin;
23

    
24
import java.awt.GridBagConstraints;
25
import java.beans.PropertyVetoException;
26
import java.io.File;
27
import java.net.URL;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.andami.PluginsLocator;
33
import org.gvsig.andami.PluginsManager;
34
import org.gvsig.andami.plugins.ExclusiveUIExtension;
35
import org.gvsig.andami.plugins.Extension;
36
import org.gvsig.andami.plugins.IExtension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.ApplicationManager;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.view.ViewDocument;
42
import org.gvsig.app.project.documents.view.ViewManager;
43
import org.gvsig.app.project.documents.view.gui.IView;
44
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
46
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
47
import org.gvsig.landregistryviewer.LandRegistryViewerLocator;
48
import org.gvsig.landregistryviewer.LandRegistryViewerManager;
49

    
50
/**
51
 * Andami extension to show LandRegistryViewer in the application.
52
 * 
53
 * @author gvSIG Team
54
 * @version $Id$
55
 */
56
public class LandRegistryViewerExtension extends Extension  implements ExclusiveUIExtension  {
57
    
58
    private static final Logger logger =
59
        LoggerFactory.getLogger(LandRegistryViewerExtension.class);
60

    
61
    private static final String MY_VIEW_NAME = "Land registry viewer";
62

    
63
    private static final String TOOL_NAME = "LandRegistryViewer.infotool";
64
    private static final String ACTION_SETINFOTOOL = "SetInfoTool";
65

    
66
    private LandRegistryViewerManager manager;
67
//    private LandRegistryViewerSwingManager swingManager;
68
    
69
    private IView viewWindow;
70
    
71
    public void initialize() {
72
        PluginsManager manager = PluginsLocator.getManager();
73
        manager.setExclusiveUIExtension(this);
74
    }
75

    
76
    public void postInitialize() {
77
        try {
78
            manager = LandRegistryViewerLocator.getManager();
79
//            swingManager = LandRegistryViewerSwingLocator.getSwingManager();
80
            this.initializeStores();
81
                    viewWindow = this.createViewWindow();
82

    
83
        } catch (LoadLayerException e) {
84
                
85
            }
86
    }
87

    
88
    /**
89
     * Execute the actions associated to this extension. 
90
     */
91
    public void execute(String actionCommand) {
92
            if( ACTION_SETINFOTOOL.equalsIgnoreCase(actionCommand) ) {
93
                    // Set the tool in the mapcontrol of the active view.
94
                
95
                ApplicationManager application = ApplicationLocator.getManager();
96

    
97
            if( application.getActiveWindow() != viewWindow ) {
98
                return;
99
            }
100
                viewWindow.getMapControl().setTool(TOOL_NAME);
101
            }
102
    }
103

    
104
    /**
105
     * Check if tools of this extension are enabled.
106
     */
107
    public boolean isEnabled() {
108
            //
109
            // By default the tool is always enabled
110
            //
111
        return true;
112
    }
113

    
114
    /**
115
     * Check if tools of this extension are visible.
116
     */
117
    public boolean isVisible() {
118
            //
119
            // The tool is visible only when our view is active
120
            //
121
            
122
            ApplicationManager application = ApplicationLocator.getManager();
123
            
124
        return application.getActiveWindow() == viewWindow;
125
    }
126

    
127
    /**
128
     * Check if an extension is enabled.
129
     */
130
    public boolean isEnabled(IExtension extension) {
131
        if( extension.getClass().getName().startsWith("org.gvsig.editing.") ) {
132
            // Disable all extension if package "org.gvsig.editing"
133
            return false;
134
        }
135
            // Relies in the extension to verify if is enabled
136
            return extension.isEnabled();
137
    }
138

    
139
    /**
140
     * Check if an extension is visible.
141
     */
142
    public boolean isVisible(IExtension extension) {
143
        if( extension.getClass().getName().startsWith("org.gvsig.editing.") ) {
144
            // Hide all extension if package "org.gvsig.editing"
145
            return false;
146
        }
147
            // Relies in the extension to verify if is visible
148
            return extension.isVisible();
149
    }
150

    
151
    /**
152
     * Create the view in the project. Add the blocks layer
153
     * to the view, and register the tool for get info of the
154
     * blocks.
155
     */
156
    private IView createViewWindow() throws LoadLayerException {
157
            ApplicationManager application = ApplicationLocator.getManager();
158

    
159
            ProjectManager projectManager = application.getProjectManager();
160

    
161
            // 1. Create a new view and set the name.
162
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManagers(ViewManager.TYPENAME);
163
            ViewDocument view = (ViewDocument) viewManager.createDocument();
164
            view.setName(MY_VIEW_NAME);
165

    
166
            // 2. Create a new layer with the blocks
167
            FLyrVect layer = (FLyrVect) application.getMapContextManager().createLayer("Blocks", this.manager.getBlocks());
168
            
169
            // 3. Add this layer to the mapcontext of the new view.
170
            view.getMapContext().getLayers().addLayer(layer);
171

    
172
            // 4. Add the view to the current project.
173
            projectManager.getCurrentProject().add(view);
174

    
175
            // 5. Force to show the view's window.
176
            IView viewWindow = (IView) viewManager.getMainWindow(view);
177

    
178
            application.getUIManager().addWindow(viewWindow, GridBagConstraints.CENTER);
179
            try {
180
                        application.getUIManager().setMaximum((IWindow) viewWindow, true);
181
                } catch (PropertyVetoException e) {
182
                        logger.info("Can't maximize view.",e);
183
                }
184
            
185
            // 6. Register my tool in the mapcontrol of the view.
186
            PropertiesOfBlockListener listener = new PropertiesOfBlockListener();
187
            viewWindow.getMapControl().addBehavior(TOOL_NAME, new PointBehavior(listener));
188
            
189
            return viewWindow;
190
    }
191

    
192
    /**
193
     * Open the stores and initialize the logic manager whit this
194
     * stores.
195
     */
196
    private void initializeStores() {
197
            manager.initialize(
198
                            getResource("data/properties.shp"), 
199
                            getResource("data/blocks.shp")
200
            );
201
    }
202

    
203
    /**
204
     * Get a resource as a File from a path name in the
205
     * class path.
206
     * 
207
     * @param pathname
208
     * 
209
     * @return resource as a File
210
     */
211
    private File getResource(String pathname) {
212
            URL res = this.getClass().getClassLoader().getResource(pathname);
213
            return new File(res.getPath());
214
    }
215
    
216
}