Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / ShowTable.java @ 36664

History | View | Annotate | Download (7.37 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.app.extension;
23

    
24
import org.gvsig.andami.PluginServices;
25
import org.gvsig.andami.plugins.Extension;
26
import org.gvsig.app.project.ProjectManager;
27
import org.gvsig.app.project.documents.table.TableDocument;
28
import org.gvsig.app.project.documents.table.TableManager;
29
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
30
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
31
import org.gvsig.fmap.dal.feature.FeatureStore;
32
import org.gvsig.fmap.mapcontext.layers.CancelationException;
33
import org.gvsig.fmap.mapcontext.layers.FLayer;
34
import org.gvsig.fmap.mapcontext.layers.FLayers;
35
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
36
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
37
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39

    
40
/**
41
 * Extensi?n que abre las tablas asociadas a las vistas.
42
 * 
43
 * @author Vicente Caballero Navarro
44
 */
45
public class ShowTable extends Extension implements LayerCollectionListener {
46

    
47
    /**
48
     * @see org.gvsig.andami.plugins.IExtension#isEnabled()
49
     */
50
    public boolean isEnabled() {
51
        AbstractViewPanel f =
52
            (AbstractViewPanel) PluginServices.getMDIManager()
53
                .getActiveWindow();
54

    
55
        if (f == null) {
56
            return false;
57
        }
58

    
59
        FLayer[] selected =
60
            f.getViewDocument().getMapContext().getLayers().getActives();
61

    
62
        boolean algunaTabla = false;
63

    
64
        for (int i = 0; i < selected.length; i++) {
65
            if (selected[i].isAvailable() && selected[i] instanceof FLyrVect) {
66
                FLyrVect co = (FLyrVect) selected[i];
67

    
68
                try {
69
                    if (co.getFeatureStore() != null) {
70
                        algunaTabla = true;
71
                    }
72
                    // } catch (ReadException e) {
73
                    // return false;
74
                } catch (NullPointerException e) {
75
                    return false;
76
                }
77
            }
78
        }
79

    
80
        return algunaTabla;
81
    }
82

    
83
    /**
84
     * @see org.gvsig.andami.plugins.IExtension#isVisible()
85
     */
86
    public boolean isVisible() {
87
        org.gvsig.andami.ui.mdiManager.IWindow f =
88
            PluginServices.getMDIManager().getActiveWindow();
89

    
90
        if (f == null) {
91
            return false;
92
        }
93

    
94
        return (f instanceof AbstractViewPanel);
95
    }
96

    
97
    /**
98
     * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
99
     */
100
    public void execute(String s) {
101
        AbstractViewPanel vista =
102
            (AbstractViewPanel) PluginServices.getMDIManager()
103
                .getActiveWindow();
104
        FLayer[] actives =
105
            vista.getViewDocument().getMapContext().getLayers().getActives();
106

    
107
        for (int i = 0; i < actives.length; i++) {
108
            if (actives[i] instanceof FLyrVect) {
109
                FLyrVect co = (FLyrVect) actives[i];
110

    
111
                ProjectManager projectManager = getProjectManager();
112
                TableManager tableManager = getTableManager();
113

    
114
                TableDocument projectTable = tableManager.getTableDocument(co);
115
                FeatureStore fs = ((FLyrVect) actives[i]).getFeatureStore();
116

    
117
                if (projectTable == null) {
118
                    projectTable =
119
                        (TableDocument) projectManager.createDocument(
120
                            TableManager.TYPENAME,
121
                            PluginServices.getText(this, "Tabla_de_Atributos")
122
                                + ": " + actives[i].getName());
123
                    projectTable.setStore(fs);
124
                    projectTable.setAssociatedLayer(co);
125
                    co.getParentLayer().addLayerCollectionListener(this);
126
                    projectManager.getCurrentProject().add(projectTable);
127
                }
128

    
129
                FeatureTableDocumentPanel featureTableDocumentPanel =
130
                    (FeatureTableDocumentPanel) projectTable.getFactory()
131
                        .getMainWindow(projectTable);
132

    
133
                featureTableDocumentPanel.getModel().setModified(true);
134
                PluginServices.getMDIManager().addWindow(
135
                    featureTableDocumentPanel);
136
            }
137
        }
138

    
139
    }
140

    
141
    private ProjectManager getProjectManager() {
142
        return ProjectManager.getInstance();
143
    }
144

    
145
    private TableManager getTableManager() {
146
        TableManager tableManager =
147
            (TableManager) getProjectManager().getDocumentManager(
148
                TableManager.TYPENAME);
149
        return tableManager;
150
    }
151

    
152
    /**
153
     * @see org.gvsig.andami.plugins.IExtension#initialize()
154
     */
155
    public void initialize() {
156
        registerIcons();
157
    }
158

    
159
    private void registerIcons() {
160
        PluginServices.getIconTheme().registerDefault(
161
            "layer-show-attribute-table",
162
            this.getClass().getClassLoader()
163
                .getResource("images/ResultConsulta.png"));
164
    }
165

    
166
    public void layerAdded(LayerCollectionEvent e) {
167
        // Nothing to do
168
    }
169

    
170
    public void layerMoved(LayerPositionEvent e) {
171
        // Nothing to do
172
    }
173

    
174
    public void layerRemoved(LayerCollectionEvent e) {
175
        FLayer layer = e.getAffectedLayer();
176
        // Just in case we where listening to a group of layers being removed
177
        // remove us from there
178
        if (layer instanceof FLayers) {
179
            ((FLayers) layer).removeLayerCollectionListener(this);
180
        }
181
        // Remove the related table document, if any
182
        if (layer instanceof FLyrVect) {
183
            getTableManager().removeTableDocument((FLyrVect) layer);
184
            // If the parent layers has not other child layers, for sure there
185
            // are not related tables opened, don't need to listen
186
            // LayerCollectionEvents anymore.
187
            // TODO: remove us also when there are not any child layers with
188
            // related table documents
189
            FLayers layers = layer.getParentLayer();
190
            if (layers != null && layers.getLayersCount() == 0) {
191
                layers.removeLayerCollectionListener(this);
192
            }
193
        }
194
    }
195

    
196
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
197
        // Nothing to do
198
    }
199

    
200
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
201
        // Nothing to do
202
    }
203

    
204
    public void layerRemoving(LayerCollectionEvent e)
205
        throws CancelationException {
206
        // Nothing to do
207
    }
208

    
209
    public void visibilityChanged(LayerCollectionEvent e)
210
        throws CancelationException {
211
        // Nothing to do
212
    }
213
}