Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extDataLocator / src / com / iver / gvsig / datalocator / DataLocatorExtension.java @ 28915

History | View | Annotate | Download (6.59 KB)

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

    
46
import java.util.prefs.Preferences;
47

    
48
import com.hardcode.gdbms.engine.data.DataSource;
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.plugins.Extension;
51
import com.iver.andami.ui.mdiManager.IWindow;
52
import com.iver.andami.ui.mdiManager.WindowInfo;
53
import com.iver.cit.gvsig.fmap.MapContext;
54
import com.iver.cit.gvsig.fmap.layers.FLayer;
55
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
56
import com.iver.cit.gvsig.fmap.layers.LayersIterator;
57
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
58
import com.iver.cit.gvsig.project.documents.view.IProjectView;
59
import com.iver.cit.gvsig.project.documents.view.gui.View;
60
import com.iver.gvsig.datalocator.gui.DataSelectionPanel;
61

    
62
/**
63
 * The DataLocatorExtension class allows to make a quick zoom based on an
64
 * alphanumeric attribute.
65
 *
66
 * @author jmorell
67
 */
68
public class DataLocatorExtension extends Extension {
69

    
70
        IWindow iWDataSelection = null;
71
        IWindow previousView = null;
72

    
73
    /* (non-Javadoc)
74
     * @see com.iver.andami.plugins.Extension#inicializar()
75
     */
76
    public void initialize() {
77
            registerIcons();
78

    
79
    }
80

    
81
    private void registerIcons(){
82
            //view-locator
83
            PluginServices.getIconTheme().registerDefault(
84
                                "view-locator",
85
                                this.getClass().getClassLoader().getResource("images/locator.png")
86
                        );
87
    }
88

    
89
    /* (non-Javadoc)
90
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
91
     */
92
    public void execute(String actionCommand) {
93
                View vista = (View)PluginServices.getMDIManager().getActiveWindow();
94
                MapContext mapContext = vista.getModel().getMapContext();
95
                DataSelectionPanel dataSelectionPanel = new DataSelectionPanel(mapContext);
96
                WindowInfo vi = dataSelectionPanel.getWindowInfo();
97
                vi.setX(Preferences.userRoot().getInt("gvSIG.DataLocator.x", vi.getX()));
98
                vi.setY(Preferences.userRoot().getInt("gvSIG.DataLocator.y", vi.getY()));
99
//                vi.setWidth(Preferences.userRoot().getInt("gvSIG.DataLocator.w", vi.getWidth()));
100
//                vi.setHeight(Preferences.userRoot().getInt("gvSIG.DataLocator.h", vi.getHeight()));
101

    
102
                PluginServices.getMDIManager().addWindow(dataSelectionPanel);
103
                iWDataSelection = dataSelectionPanel;
104
                iWDataSelection.getWindowInfo();
105
    }
106

    
107
    /* (non-Javadoc)
108
     * @see com.iver.andami.plugins.Extension#isEnabled()
109
     */
110
    public boolean isEnabled() {
111
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
112
                 .getActiveWindow();
113
                if (f == null) {
114
                    return false;
115
                }
116
                if (f.getClass() == View.class) {
117

    
118
                    View vista = (View) f;
119
                    IProjectView model = vista.getModel();
120
                    MapContext mapContext = model.getMapContext();
121
            if (mapContext.getLayers().getLayersCount() > 0) {
122
                LayersIterator iter = newValidLayersIterator(mapContext.getLayers());
123
                if (!iter.hasNext()){
124
                        return false;
125
                }
126
            } else {
127
                return false;
128
            }
129

    
130
                        // Si la vista tiene el tema con el que est? configurado
131
                        // el DataLocator y el usuario ha decidido que se abra
132
                        // autom?ticamente, la abrimos inmediatamente.
133
            if (iWDataSelection == null || (PluginServices.getMDIManager()
134
                            .getWindowInfo(iWDataSelection).isClosed() && f!=previousView)) {
135
                            int userOpen = Preferences.userRoot().getInt("gvSIG.DataLocator.open_first_time", -1);
136
                            if (userOpen == 1)
137
                            {
138
                                    String layerName = Preferences.userRoot().get("LAYERNAME_FOR_DATA_LOCATION", "");
139
                                    FLayer lyr = mapContext.getLayers().getLayer(layerName);
140
                                    if (lyr != null)
141
                                    {
142
                                            DataSelectionPanel dataSelectionPanel = new DataSelectionPanel(mapContext);
143
                                            WindowInfo vi = dataSelectionPanel.getWindowInfo();
144
                                            vi.setX(Preferences.userRoot().getInt("gvSIG.DataLocator.x", vi.getX()));
145
                                            vi.setY(Preferences.userRoot().getInt("gvSIG.DataLocator.y", vi.getY()));
146
//                                          vi.setWidth(Preferences.userRoot().getInt("gvSIG.DataLocator.w", vi.getWidth()));
147
//                                          vi.setHeight(Preferences.userRoot().getInt("gvSIG.DataLocator.h", vi.getHeight()));
148

    
149
                                            PluginServices.getMDIManager().addWindow(dataSelectionPanel);
150
                                            iWDataSelection = dataSelectionPanel;
151
                                            iWDataSelection.getWindowInfo();
152
                                    }
153
                            }
154
            }
155
            previousView = f;
156
                }
157
        return true;
158
    }
159

    
160
    /* (non-Javadoc)
161
     * @see com.iver.andami.plugins.Extension#isVisible()
162
     */
163
    public boolean isVisible() {
164
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
165
                 .getActiveWindow();
166
                if (f == null) {
167
                    return false;
168
                }
169
                if (f.getClass() == View.class) {
170
                    View vista = (View) f;
171
                    IProjectView model = vista.getModel();
172
                    MapContext mapContext = model.getMapContext();
173
            return mapContext.getLayers().getLayersCount() > 0;
174
                } else {
175
                    return false;
176
                }
177
    }
178

    
179
    public static LayersIterator newValidLayersIterator(LayerCollection layer) {
180
            return new LayersIterator((FLayer)layer){
181
                    public boolean evaluate(FLayer layer) {
182
                            if (!(layer instanceof FLyrVect))
183
                                    return false;
184
                            DataSource ds;
185
                            try {
186
                                    ds = ((FLyrVect)layer).getRecordset();
187
                                    if (ds.getFieldCount() < 1)
188
                                            return false;
189

    
190
                            } catch (Exception e) {
191
                                    return false;
192
                            }
193
                            return true;
194
                    }
195
            };
196
    }
197
}