Statistics
| Revision:

gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.attributeeditor.app / org.gvsig.attributeeditor.app.mainplugin / src / main / java / org / gvsig / app / mainplugin / extension / AttributeEditorExtension.java @ 1146

History | View | Annotate | Download (6.94 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.mainplugin.extension;
24

    
25

    
26
import org.gvsig.andami.IconThemeHelper;
27
import org.gvsig.andami.plugins.Extension;
28
import org.gvsig.app.ApplicationLocator;
29
import org.gvsig.app.ApplicationManager;
30
import org.gvsig.app.project.documents.view.ViewDocument;
31
import org.gvsig.app.project.documents.view.ViewManager;
32
import org.gvsig.app.project.documents.view.gui.IView;
33
import org.gvsig.expressionevaluator.Expression;
34
import org.gvsig.fmap.dal.EditingNotification;
35
import org.gvsig.fmap.dal.EditingNotificationManager;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.fmap.dal.swing.DALSwingLocator;
41
import org.gvsig.fmap.mapcontrol.MapControl;
42
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
43
import org.gvsig.fmap.mapcontrol.tools.Listeners.AttributeEditorBehavior;
44
import org.gvsig.fmap.mapcontrol.tools.Listeners.AttributeEditorPointListener;
45
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynObjectManager;
48
import org.gvsig.tools.dynobject.Tags;
49
import org.gvsig.tools.observer.Observable;
50
import org.gvsig.tools.observer.Observer;
51

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public class AttributeEditorExtension extends Extension {
57

    
58
    private static final String TAG_ATTRIBUTEEDITOR_LOADONINSERT = "attributeeditor.loadoninsert";
59
    
60
    private Observer editingObserver;
61
       
62

    
63
    @Override
64
    public void execute(String actionCommand) {
65
        if (!actionCommand.equalsIgnoreCase("attribute-editor") ) {
66
            return;
67
        }
68
        ApplicationManager application = ApplicationLocator.getManager();
69
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
70
        if (view != null) {
71
            MapControl mapControl = view.getMapControl();
72
            if( mapControl.getMapContext().hasActiveVectorLayers() ) {
73
                if (!mapControl.hasTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME)) {
74
                    createMapTool(mapControl);
75
                }
76
                mapControl.setTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME);                
77
            }
78
        }
79

    
80
    }
81

    
82
    @Override
83
    public void initialize() {
84
        IconThemeHelper.registerIcon("action", "attribute-editor", this);
85
    }
86

    
87
    @Override
88
    @SuppressWarnings("Convert2Lambda")
89
    public void postInitialize() {
90
        this.editingObserver = new Observer() {
91
            @Override
92
            public void update(Observable o, Object notification) {
93
                if( !isEnabled() ) {
94
                    return;
95
                }
96
                EditingNotification n = (EditingNotification) notification;
97
                if( n.getType().equalsIgnoreCase(EditingNotification.AFTER_INSERT_FEATURE) ) {
98
                    if( n.getFeature()!=null && n.getFeatureStore()!=null ) {
99
                        FeatureType featureType = n.getFeatureStore().getDefaultFeatureTypeQuietly();
100
                        if( featureType != null ) {
101
                            Tags tags = featureType.getTags();
102
                            if( tags.getBoolean(TAG_ATTRIBUTEEDITOR_LOADONINSERT, false) ) {
103
                                doShowFeature(n.getFeatureStore(),n.getFeature());
104
                            }
105
                        }
106
                    }
107
                }
108
            }
109
        };
110
        EditingNotificationManager editingNotificationManager = DALSwingLocator.getEditingNotificationManager();
111
        editingNotificationManager.addObserver(this.editingObserver);       
112
        
113
        DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
114
        dynObjectManager.registerTag(
115
                TAG_ATTRIBUTEEDITOR_LOADONINSERT, 
116
                "Indicates if the attribute editor should be opened when creating a feature of this type. By default false.",
117
                new String[] { "true", "false" }
118
        );
119
        
120
    }
121
    
122
    @Override
123
    public boolean isEnabled() {
124
        ViewDocument viewDoc = getActiveViewDocument();
125
        if (viewDoc == null) {
126
            return false;
127
        }
128
        return viewDoc.getMapContext().hasActiveVectorLayers();
129
    }
130

    
131
    @Override
132
    public boolean isVisible() {
133
        return getActiveViewDocument() != null;
134
    }
135

    
136
    /**
137
     * Gets active window
138
     *
139
     * @return
140
     */
141
    private ViewDocument getActiveViewDocument() {
142
        return (ViewDocument) ApplicationLocator.getManager().getActiveDocument(ViewManager.TYPENAME);
143
    }
144

    
145
    private void createMapTool(MapControl mapControl) {
146
        ApplicationManager application = ApplicationLocator.getManager();
147
        
148
        AttributeEditorBehavior attributeEditorBehavior = new AttributeEditorBehavior(mapControl);
149
        attributeEditorBehavior.getListener().setRefreshCallback((FeatureStore store) -> {
150
            application.refreshDocument(store);
151
            return true;
152
        });
153
        mapControl.addBehavior(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME, attributeEditorBehavior);
154
    }
155
    
156
    private void doShowFeature(FeatureStore store, Feature feature) {
157
        Expression filter = feature.createFilter();
158
        if( filter == null ) {
159
            return;
160
        }
161
        ApplicationManager application = ApplicationLocator.getManager();
162
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
163
        if (view != null) {
164
            MapControl mapControl = view.getMapControl();
165
            Behavior tool = mapControl.getTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME);
166
            if (tool == null ) {
167
                createMapTool(mapControl);
168
                tool = mapControl.getTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME);
169
            }            
170
            AttributeEditorPointListener listener = (AttributeEditorPointListener) tool.getListener();
171
            listener.showOrUpdateForm(store.getLabel(), store, store.createFeatureQuery(filter));
172
        }
173
        
174
    }
175
}