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 / ShowGraphicsLayerTable.java @ 38608

History | View | Annotate | Download (5.27 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 java.beans.PropertyChangeEvent;
25
import java.beans.PropertyChangeListener;
26

    
27
import org.gvsig.andami.IconThemeHelper;
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.plugins.Extension;
30
import org.gvsig.app.ApplicationLocator;
31
import org.gvsig.app.ApplicationManager;
32
import org.gvsig.app.project.ProjectManager;
33
import org.gvsig.app.project.documents.table.TableDocument;
34
import org.gvsig.app.project.documents.table.TableManager;
35
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
36
import org.gvsig.app.project.documents.view.ViewDocument;
37
import org.gvsig.app.project.documents.view.ViewManager;
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * Opens a table document of the current view's graphic layer.
46
 * 
47
 * @author gvSIG team
48
 */
49
public class ShowGraphicsLayerTable extends Extension implements
50
                PropertyChangeListener {
51

    
52
        private static final Logger LOG = LoggerFactory
53
                        .getLogger(ShowGraphicsLayerTable.class);
54

    
55
        private ApplicationManager appManager = ApplicationLocator.getManager();
56

    
57
        /**
58
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
59
         */
60
        public boolean isEnabled() {
61
                return getCurrentViewDocument() != null;
62
        }
63

    
64
        private ViewDocument getCurrentViewDocument() {
65
                return (ViewDocument) appManager
66
                                .getActiveDocument(ViewManager.TYPENAME);
67
        }
68

    
69
        /**
70
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
71
         */
72
        public boolean isVisible() {
73
                try {
74
                        ViewDocument document = getCurrentViewDocument();
75
                        if (document == null) {
76
                                return false;
77
                        }
78
                        GraphicLayer graphicsLayer = document.getMapContext()
79
                                        .getGraphicsLayer();
80
                        TableManager tableManager = getTableManager();
81
                        TableDocument tableDocument = tableManager
82
                                        .getTableDocument(graphicsLayer);
83
                        return tableDocument != null
84
                                        || document.getMapContext().getGraphicsLayer()
85
                                                        .getFeatureStore().getFeatureCount() > 0;
86
                } catch (DataException e) {
87
                        LOG.error("Error checking if the current document is a "
88
                                        + "view and has data in the graphics layer", e);
89
                        return false;
90
                }
91
        }
92

    
93
        /**
94
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
95
         */
96
        public void execute(String action) {
97

    
98
                ViewDocument document = getCurrentViewDocument();
99
                GraphicLayer graphicsLayer = document.getMapContext()
100
                                .getGraphicsLayer();
101

    
102
                ProjectManager projectManager = getProjectManager();
103
                TableManager tableManager = getTableManager();
104

    
105
                TableDocument tableDocument = tableManager
106
                                .getTableDocument(graphicsLayer);
107
                FeatureStore fs = graphicsLayer.getFeatureStore();
108

    
109
                if (tableDocument == null) {
110
                        tableDocument = (TableDocument) projectManager.createDocument(
111
                                        TableManager.TYPENAME,
112
                                        PluginServices.getText(this, "Tabla_de_Atributos") + ": "
113
                                                        + graphicsLayer.getName());
114
                        tableDocument.setStore(fs);
115
                        tableDocument.setAssociatedLayer(graphicsLayer);
116
                        projectManager.getCurrentProject().add(tableDocument);
117
                }
118

    
119
                tableDocument.setModified(true);
120
                FeatureTableDocumentPanel featureTableDocumentPanel = (FeatureTableDocumentPanel) tableManager
121
                                .getMainWindow(tableDocument);
122

    
123
                appManager.getUIManager().addWindow(featureTableDocumentPanel);
124

    
125
        }
126

    
127
        private ProjectManager getProjectManager() {
128
                return ProjectManager.getInstance();
129
        }
130

    
131
        private TableManager getTableManager() {
132
                TableManager tableManager = (TableManager) getProjectManager()
133
                                .getDocumentManager(TableManager.TYPENAME);
134
                return tableManager;
135
        }
136

    
137
        /**
138
         * @see org.gvsig.andami.plugins.IExtension#initialize()
139
         */
140
        public void initialize() {
141
                registerIcons();
142
        }
143

    
144
        public void postInitialize() {
145
                // Listen just in case a view document is removed, to remove also
146
                // the related graphics layer table document, if any
147
                getProjectManager().getCurrentProject().addPropertyChangeListener(this);
148
        }
149

    
150
        private void registerIcons() {
151
                IconThemeHelper.registerIcon("action", "layer-show-attributes-table", this);
152
        }
153

    
154
        public void propertyChange(PropertyChangeEvent evt) {
155
                if ("delDocument".equals(evt.getPropertyName())) {
156
                        if (evt.getOldValue() != null
157
                                        && evt.getOldValue() instanceof ViewDocument) {
158
                                ViewDocument viewDocument = (ViewDocument) evt.getOldValue();
159
                                getTableManager().removeTableDocument(
160
                                                viewDocument.getMapContext().getGraphicsLayer());
161
                        }
162
                }
163
        }
164
}