Statistics
| Revision:

gvsig-projects-pool / org.gvsig.republicadominicana.catastro / trunk / org.gvsig.republicadominicana.catastro / org.gvsig.republicadominicana.catastro.app / org.gvsig.republicadominicana.catastro.app.mainplugin / src / main / java / org / gvsig / republicadominicana / catastro / app / mainplugin / RDCatastroExtension.java @ 9680

History | View | Annotate | Download (10.6 KB)

1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 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, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22
package org.gvsig.republicadominicana.catastro.app.mainplugin;
23

    
24
import java.awt.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.util.Collections;
27
import org.apache.commons.collections.CollectionUtils;
28
import org.apache.commons.lang3.StringUtils;
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.documents.view.ViewDocument;
33
import org.gvsig.app.project.documents.view.ViewManager;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.mapcontext.layers.FLayer;
38
import org.gvsig.fmap.mapcontext.layers.FLayers;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
40
import org.gvsig.online.lib.api.OnlineLocator;
41
import org.gvsig.online.lib.api.OnlineManager;
42
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy;
43
import org.gvsig.republicadominicana.catastro.app.mainplugin.RDCatastroDialogsHelper.RDCatastroDialogInfo;
44
import org.gvsig.republicadominicana.catastro.lib.api.RDCatastroLocator;
45
import org.gvsig.republicadominicana.catastro.lib.api.RDCatastroManager;
46
import static org.gvsig.republicadominicana.catastro.lib.api.RDCatastroManager.RDCATASTRO_CADASTRAL_REFERENCE_FIELD_NAME;
47
import org.gvsig.republicadominicana.catastro.swing.api.RDCatastroSwingLocator;
48
import org.gvsig.republicadominicana.catastro.swing.api.RDCatastroSwingManager;
49
import org.gvsig.tools.arguments.Arguments;
50
import org.gvsig.tools.dispose.DisposeUtils;
51
import org.gvsig.tools.swing.api.ToolsSwingUtils;
52

    
53

    
54
/**
55
 *
56
 * @author gvSIG Team
57
 */
58
public class RDCatastroExtension 
59
        extends Extension  
60
//        implements IPreferenceExtension 
61
    {
62

    
63
    private RDCatastroDialogsHelper dialogsHelper;
64

    
65
    @Override
66
    public void initialize() {
67
//        ToolsSwingUtils.registerIcons(
68
//                this.getClass(), 
69
//                "/images/action",
70
//                "online",
71
//                new String[] { "action", "tools-online-init-workspace","online" },
72
//                new String[] { "action", "tools-online-download","online" },
73
//                new String[] { "action", "tools-online-showchanges","online" }
74
//        );        
75
//        ToolsSwingUtils.registerSubgroupIconDescription("action", "online", "Acciones asociadas al complemento de Online.\nSe encuentran en el menu 'Heramientas/Online'");
76
        
77
        
78
        this.dialogsHelper = new RDCatastroDialogsHelper();
79
    }
80

    
81
    @Override
82
    public void postInitialize() {
83
        super.postInitialize();
84
        
85
        ToolsSwingUtils.registerIcons(
86
                this.getClass(), 
87
                "/images/preferences",
88
                "online",
89
                new String[] { "preferences", "online-preferences","online" }
90
        );        
91
        
92
        ApplicationManager application = ApplicationLocator.getApplicationManager();
93
        RDCatastroManager manager = RDCatastroLocator.getRDCatastroManager();
94
        RDCatastroSwingManager swingManager = RDCatastroSwingLocator.getRDCatastroSwingManager();
95
//        swingManager.setDefaultServices(new OnlineSwingServicesImpl());
96
//
97
//        DynObject pluginProperties = this.getPlugin().getPluginProperties();
98
//        Map<String, OnlineWorkingcopyDescriptor> workspaces = (Map) pluginProperties.getDynValue("workspaces");
99
//        manager.restoreWorkingcopy(workspaces);
100
////        swingManager.setRepositoryEntityFeatureTypeEditorMode((int)pluginProperties.getDynValue("RepositoryEntityFeatureTypeEditorMode"));
101
//
102
//        PluginsLocator.getPluginsManager().addShutdownTask(
103
//                "OnlineSave", 
104
//                () -> {
105
//                    DynObject pluginProps = getPlugin().getPluginProperties();
106
//                    pluginProps.setDynValue("workspaces", manager.getWorkingcopy());
107
////                    pluginProps.setDynValue("RepositoryEntityFeatureTypeEditorMode", swingManager.getRepositoryEntityFeatureTypeEditorMode());
108
//                },
109
//                false,
110
//                100
111
//        );
112
//
113
//        ApplicationLocator.getManager().registerAddTableWizard(
114
//                "Online", 
115
//                "Online", 
116
//                WizardOnlineTabular.class
117
//        );
118
//        AddLayer.addWizard(WizardOnlineVectorial.class);
119
//        
120
//        application.getProjectManager().addProjectListener((EventListener e) -> {
121
//            if( e instanceof ProjectEvent ) {
122
//                ProjectEvent pe = (ProjectEvent)e;
123
//                switch(pe.getId()) {
124
//                    case AFTER_LOAD_FROM_FILE:
125
//                        OnlineSwingServicesImpl services = (OnlineSwingServicesImpl) OnlineSwingLocator.getOnlineSwingManager().getDefaultServices();
126
//                        services.projectLoaded(pe.getProject());
127
//                        break;
128
//                    case BEFORE_SAVE_TO_FILE:
129
//                        break;
130
//                }
131
//            }
132
//        });
133
//        
134
//        EditingNotificationManager editingNotificationManager = DALSwingLocator.getEditingNotificationManager();
135
//        editingNotificationManager.addObserver(new EditingNotificationObserver());
136
    }
137

    
138
    @Override
139
    public void execute(String actionCommand) {
140
        this.execute(actionCommand, null);
141
    }
142
    
143
    @Override
144
    public void execute(String actionCommand, Object[] args) {
145
        Arguments arguments = Arguments.create(args);
146
        switch (actionCommand) {
147
            case "tools-rdcatastro-searchbycadastralreference":
148
                FeatureStore store = (FeatureStore) arguments.get("store", FeatureStore.class, null);
149
                if(store == null) {
150
                    store = this.getActiveOnlineStore();
151
                    showSearchByCadastralReferenceDialog(
152
                        store,
153
                        arguments
154
                    );
155
                    DisposeUtils.dispose(store);
156
                } else {
157
                    showSearchByCadastralReferenceDialog(
158
                        store,
159
                        arguments
160
                    );
161
                }
162
                break;
163
        }
164
    }
165

    
166
    @Override
167
    public boolean isEnabled() {
168
        return true;
169
    }
170

    
171
    @Override
172
    public boolean isEnabled(String action) {
173
        if (StringUtils.equals(action, "tools-rdcatastro-searchbycadastralreference")) {
174
            FeatureStore store = null;
175
            try { //FIXME:
176
                store = getActiveOnlineStore();
177
                return store != null;
178
            } catch (Throwable th) {
179
                logger.warn("Can't check if is enabled online action '" + action + "'", th);
180
                return false;
181
            } finally {
182
                DisposeUtils.disposeQuietly(store);
183
            }
184
        }
185
        return isEnabled();
186
    }
187

    
188
        public boolean canQueryByAction() {
189
                return true;
190
        }
191
    
192
    @Override
193
    public boolean isVisible() {
194
        return true;
195
    }
196

    
197

    
198
//    @Override
199
//    public IPreference[] getPreferencesPages() {
200
//        List<IPreference> prefs = new ArrayList<>();
201
//        prefs.add(new OnlinePage());
202
//
203
//        return prefs.toArray(new IPreference[prefs.size()]);
204
//    }
205

    
206
    private FeatureStore getActiveOnlineStore() {
207
        ViewDocument view = (ViewDocument) ApplicationLocator.getManager().getActiveDocument(ViewManager.TYPENAME);
208
        if (view == null) {
209
            return null;
210
        }
211
        FLayers layers = view.getMapContext().getLayers();
212
        if (CollectionUtils.isEmpty(layers)) {
213
            return null;
214
        }
215
        FLayer[] activeLayers = layers.getActives();
216
        if (activeLayers == null || activeLayers.length != 1) {
217
            return null;
218
        }
219
        OnlineManager onlineManager = OnlineLocator.getOnlineManager();
220
        FLayer activeLayer = activeLayers[0];
221
        if (activeLayer instanceof VectorLayer) {
222
            FeatureStore store = ((VectorLayer) activeLayer).getFeatureStore();
223
            if (store == null) {
224
                return null;
225
            }
226
            OnlineWorkingcopy wc = null;
227
            try {
228
                wc = onlineManager.getWorkingcopy(store);
229
                if (wc != null) {
230
                    FeatureType featType = store.getDefaultFeatureTypeQuietly();
231
                    for (FeatureAttributeDescriptor featureAttributeDescriptor : featType) {
232
                        if(StringUtils.equalsIgnoreCase(RDCATASTRO_CADASTRAL_REFERENCE_FIELD_NAME, featureAttributeDescriptor.getName())){
233
                            DisposeUtils.bind(store);
234
                            return store;
235
                        }
236
                    }
237
                }
238
            } finally {
239
                DisposeUtils.dispose(wc);
240
            }
241
        }
242
        return null;
243
    }
244

    
245
    private void showSearchByCadastralReferenceDialog(FeatureStore store, Arguments arguments) {
246
        RDCatastroSwingManager swingManager = RDCatastroSwingLocator.getRDCatastroSwingManager();
247

    
248
        final RDCatastroDialogInfo dialog = this.dialogsHelper.getOrCreateDialog(
249
                "_Search_by_cadastral_reference",
250
                swingManager.createSearchByCadastralReferencePanel(store, arguments),
251
                "_RDCatastro_Search_by_cadastral_reference"
252
        );
253
//        Dimension screensize = ApplicationLocator.getApplicationManager().getUIManager().getInternalSize();
254
//        Dimension panelsize = dialog.getPanel().asJComponent().getPreferredSize();
255
//        ToolsSwingUtils.ensureHeightWitdh(
256
//                dialog.getPanel().asJComponent(), 
257
//                Math.max(3*(screensize.height/4), panelsize.height),
258
//                Math.max(2*(screensize.width/4), panelsize.width), 
259
//                7*(screensize.height/8), 
260
//                3*(screensize.width/4)
261
//        );                    
262
        dialog.show(
263
                Collections.singletonMap("align", GridBagConstraints.CENTER)
264
        );
265
    }
266

    
267
}