Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / ShowTable.java @ 39374

History | View | Annotate | Download (6.95 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.IconThemeHelper;
25
import org.gvsig.andami.PluginServices;
26
import org.gvsig.andami.plugins.Extension;
27
import org.gvsig.app.project.ProjectManager;
28
import org.gvsig.app.project.documents.table.TableDocument;
29
import org.gvsig.app.project.documents.table.TableManager;
30
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
31
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.mapcontext.layers.CancelationException;
34
import org.gvsig.fmap.mapcontext.layers.FLayer;
35
import org.gvsig.fmap.mapcontext.layers.FLayers;
36
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
37
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
38
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
40

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

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

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

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

    
63
        boolean algunaTabla = false;
64

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

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

    
81
        return algunaTabla;
82
    }
83

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

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

    
95
        return (f instanceof AbstractViewPanel);
96
    }
97

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

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

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

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

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

    
133
                                        FeatureTableDocumentPanel featureTableDocumentPanel = (FeatureTableDocumentPanel) projectTable
134
                                                        .getFactory().getMainWindow(projectTable);
135

    
136
                                        featureTableDocumentPanel.getModel().setModified(true);
137
                                        PluginServices.getMDIManager().addWindow(
138
                                                        featureTableDocumentPanel);
139
                                }
140
                        }
141
                }
142
        }
143

    
144
    private ProjectManager getProjectManager() {
145
        return ProjectManager.getInstance();
146
    }
147

    
148
    private TableManager getTableManager() {
149
        TableManager tableManager =
150
            (TableManager) getProjectManager().getDocumentManager(
151
                TableManager.TYPENAME);
152
        return tableManager;
153
    }
154

    
155
    /**
156
     * @see org.gvsig.andami.plugins.IExtension#initialize()
157
     */
158
    public void initialize() {
159
            IconThemeHelper.registerIcon("action", "layer-show-attributes-table", this);
160
    }
161

    
162
    public void layerAdded(LayerCollectionEvent e) {
163
        // Nothing to do
164
    }
165

    
166
    public void layerMoved(LayerPositionEvent e) {
167
        // Nothing to do
168
    }
169

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

    
192
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
193
        // Nothing to do
194
    }
195

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

    
200
    public void layerRemoving(LayerCollectionEvent e)
201
        throws CancelationException {
202
        // Nothing to do
203
    }
204

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