Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geometrymeasurement.app / org.gvsig.geometrymeasurement.app.extension / src / main / java / org / gvsig / geometrymeasurement / app / extension / AbstractGeometryMeasurementExtension.java @ 37506

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

    
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
import org.gvsig.andami.PluginServices;
28
import org.gvsig.andami.messages.NotificationManager;
29
import org.gvsig.andami.plugins.Extension;
30
import org.gvsig.andami.ui.mdiManager.IWindow;
31
import org.gvsig.app.project.documents.table.TableDocument;
32
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.exception.ReadException;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.geom.type.GeometryType;
38

    
39
/**
40
 * Abstract class for Geometry measurement operations extensions.
41
 * 
42
 * @author gvSIG Team
43
 * @version $Id$
44
 */
45
public abstract class AbstractGeometryMeasurementExtension extends Extension {
46

    
47
    private static final Logger LOG = LoggerFactory
48
        .getLogger(AbstractGeometryMeasurementExtension.class);
49

    
50
    public boolean isEnabled() {
51
        TableDocument tableDocument;
52
        try {
53
            tableDocument = getActiveTableDocument();
54
            return (tableDocument != null && tableDocument.getStore().allowWrite());
55
        } catch (DataException e) {
56
            LOG.error("Error getting the table document", e);
57
        }
58
        return false;
59
    }
60

    
61
    public final boolean isVisible() {
62
        TableDocument tableDocument;
63
        try {
64
            tableDocument = getActiveTableDocument();
65
            if (tableDocument != null) {
66
                return isVisibleForTable(tableDocument);
67
            }
68
        } catch (DataException e) {
69
            LOG.error("Error getting the table document", e);
70
        }       
71
        return false;
72
    }
73

    
74
    /**
75
     * Returns if the current extension is visible for the provided 
76
     * table.
77
     * 
78
     * The provided table will be always non null.
79
     * 
80
     * @param layer
81
     *            the active table
82
     * @return if the extension is visible
83
     */
84
    protected abstract boolean isVisibleForTable(TableDocument tableDocument);
85

    
86
    /**
87
     * Returns the active table document. If the active document is not a table,
88
     * or if the table has not a geometry, return null.
89
     * 
90
     * @return the active table document
91
     * @throws DataException 
92
     */
93
    protected TableDocument getActiveTableDocument() throws DataException {
94
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
95

    
96
        if (window instanceof FeatureTableDocumentPanel) {
97
            FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) window;
98
            TableDocument tableDocument = (TableDocument)table.getDocument();
99
            if (tableDocument.getStore().getDefaultFeatureType().getDefaultGeometryAttribute() != null){
100
                return tableDocument;
101
            }
102
           
103
        }
104
        return null;
105
    }
106

    
107
    protected boolean isStoreOfAnyType(TableDocument tableDocument, int[] geomTypes) {
108
        try {
109
            FeatureType featureType = tableDocument.getStore().getDefaultFeatureType();
110
            GeometryType geometryType = featureType.getDefaultGeometryAttribute().getGeomType();
111
            for (int i = 0; i < geomTypes.length; i++) {
112
                if (geometryType.isTypeOf(geomTypes[i])) {
113
                    return true;
114
                }
115
            }
116
        } catch (ReadException e) {
117
            LOG.error("Error reading the geometry type", e);
118
        } catch (DataException e) {
119
            LOG.error("Error reading the geometry type", e);
120
        }
121
        return false;
122
    }
123

    
124
    public void execute(String actionCommand) {
125
        try {
126
            TableDocument tableDocument = getActiveTableDocument();
127
            if (tableDocument == null) {
128
                return;
129
            }
130
            FeatureStore featureStore = tableDocument.getStore();
131
            execute(actionCommand, featureStore);
132
        } catch (Exception e) {
133
            LOG.error("Not possible to create the area field", e);
134
            NotificationManager.addError(e);
135
        }
136
    }
137

    
138
    /**
139
     * Executes a command for the given {@link FeatureStore}.
140
     * 
141
     * @param actionCommand
142
     *            to execute
143
     * @param featureStore
144
     *            to use
145
     * @throws Exception
146
     *             if there is an error while executing
147
     */
148
    protected abstract void execute(String actionCommand,
149
        FeatureStore featureStore) throws Exception;
150

    
151
}