Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / ShowGraphicsLayerTable.java @ 40558

History | View | Annotate | Download (5.31 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.extension;
25

    
26
import java.beans.PropertyChangeEvent;
27
import java.beans.PropertyChangeListener;
28

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

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

    
54
        private static final Logger LOG = LoggerFactory
55
                        .getLogger(ShowGraphicsLayerTable.class);
56

    
57
        private ApplicationManager appManager = ApplicationLocator.getManager();
58

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

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

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

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

    
100
                ViewDocument document = getCurrentViewDocument();
101
                GraphicLayer graphicsLayer = document.getMapContext()
102
                                .getGraphicsLayer();
103

    
104
                ProjectManager projectManager = getProjectManager();
105
                TableManager tableManager = getTableManager();
106

    
107
                TableDocument tableDocument = tableManager
108
                                .getTableDocument(graphicsLayer);
109
                FeatureStore fs = graphicsLayer.getFeatureStore();
110

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

    
121
                tableDocument.setModified(true);
122
                FeatureTableDocumentPanel featureTableDocumentPanel = (FeatureTableDocumentPanel) tableManager
123
                                .getMainWindow(tableDocument);
124

    
125
                appManager.getUIManager().addWindow(featureTableDocumentPanel);
126

    
127
        }
128

    
129
        private ProjectManager getProjectManager() {
130
                return ProjectManager.getInstance();
131
        }
132

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

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

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

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

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