Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geometrymeasurement.app / org.gvsig.geometrymeasurement.app.mainplugin / src / main / java / org / gvsig / geometrymeasurement / app / extension / AbstractGeometryMeasurementExtension.java @ 40557

History | View | Annotate | Download (6.81 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.geometrymeasurement.app.extension;
25

    
26
import java.awt.Component;
27
import java.util.Locale;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.project.documents.table.TableDocument;
40
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.ReadException;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.geom.type.GeometryType;
46
import org.gvsig.i18n.Messages;
47

    
48
/**
49
 * Abstract class for Geometry measurement operations extensions.
50
 * 
51
 * @author gvSIG Team
52
 * @version $Id$
53
 */
54
public abstract class AbstractGeometryMeasurementExtension extends Extension {
55

    
56
    private static final Logger LOG = LoggerFactory
57
        .getLogger(AbstractGeometryMeasurementExtension.class);
58
    
59
    //Just to know if the resources has been initialized
60
    private static boolean isInitialized = false;
61
       
62
    public void initialize() {
63
        if (!isInitialized){
64
            //Register the strings
65
            if (!Messages.hasLocales()) {
66
                Messages.addLocale(Locale.getDefault());
67
            }
68
            
69
            Messages.addResourceFamily(
70
                    "org.gvsig.geometrymeasurement.app.extension.i18n.text",
71
                    AbstractGeometryMeasurementExtension.class.getClassLoader(),
72
                    AbstractGeometryMeasurementExtension.class.getClass().getName());
73
            
74
            //Register the icons
75
            
76
            isInitialized = true;
77
        }        
78
    }
79

    
80
    public boolean isEnabled() {
81
        TableDocument tableDocument;
82
        String name = "unknow";
83
        try {
84
            tableDocument = getActiveTableDocument();
85
            if (tableDocument != null) {
86
                name = tableDocument.getName();
87
                    return (tableDocument != null && tableDocument.getStore().allowWrite());
88
            }
89
        } catch (Throwable e) {
90
            LOG.info("Error accesing to the table document ("+name+")", e);
91
        }
92
        return false;
93
    }
94

    
95
    public final boolean isVisible() {
96
        TableDocument tableDocument;
97
        String name = "unknow";
98
        try {
99
            tableDocument = getActiveTableDocument();
100
            if (tableDocument != null) {
101
                name = tableDocument.getName();
102
                return isVisibleForTable(tableDocument);
103
            }
104
        } catch (Throwable e) {
105
            LOG.info("Error accesing to the table document ("+name+")", e);
106
        }       
107
        return false;
108
    }
109

    
110
    /**
111
     * Returns if the current extension is visible for the provided 
112
     * table.
113
     * 
114
     * The provided table will be always non null.
115
     * 
116
     * @param layer
117
     *            the active table
118
     * @return if the extension is visible
119
     */
120
    protected abstract boolean isVisibleForTable(TableDocument tableDocument);
121

    
122
    /**
123
     * Returns the active table document. If the active document is not a table,
124
     * or if the table has not a geometry, return null.
125
     * 
126
     * @return the active table document
127
     * @throws DataException 
128
     */
129
    protected TableDocument getActiveTableDocument() throws DataException {
130
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
131

    
132
        if (window instanceof FeatureTableDocumentPanel) {
133
            FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) window;
134
            TableDocument tableDocument = (TableDocument)table.getDocument();
135
            if (tableDocument.getStore().getDefaultFeatureType().getDefaultGeometryAttribute() != null){
136
                return tableDocument;
137
            }
138
           
139
        }
140
        return null;
141
    }
142

    
143
    protected boolean isStoreOfAnyType(TableDocument tableDocument, int[] geomTypes) {
144
        try {
145
            FeatureType featureType = tableDocument.getStore().getDefaultFeatureType();
146
            GeometryType geometryType = featureType.getDefaultGeometryAttribute().getGeomType();
147
            for (int i = 0; i < geomTypes.length; i++) {
148
                if (geometryType.isTypeOf(geomTypes[i])) {
149
                    return true;
150
                }
151
            }
152
        } catch (ReadException e) {
153
            LOG.error("Error reading the geometry type", e);
154
        } catch (DataException e) {
155
            LOG.error("Error reading the geometry type", e);
156
        }
157
        return false;
158
    }
159

    
160
    public void execute(String actionCommand) {
161
        try {
162
            TableDocument tableDocument = getActiveTableDocument();
163
            if (tableDocument == null) {
164
                return;
165
            }
166
            Component parentWindow = 
167
                (Component)ApplicationLocator.getManager().getUIManager().getActiveWindow();
168
            int addField = JOptionPane.showConfirmDialog(parentWindow, Messages.getText("new_field_query"),
169
                Messages.getText("info"), JOptionPane.YES_NO_OPTION);
170
            
171
            if (addField == JOptionPane.YES_OPTION){
172
                FeatureStore featureStore = tableDocument.getStore();
173
                execute(actionCommand, featureStore);
174
            }
175
        } catch (Exception e) {
176
            LOG.error("Not possible to add the new field", e);
177
            NotificationManager.addError(e);
178
        }
179
    }
180

    
181
    /**
182
     * Executes a command for the given {@link FeatureStore}.
183
     * 
184
     * @param actionCommand
185
     *            to execute
186
     * @param featureStore
187
     *            to use
188
     * @throws Exception
189
     *             if there is an error while executing
190
     */
191
    protected abstract void execute(String actionCommand,
192
        FeatureStore featureStore) throws Exception;
193

    
194
}