Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / checkout / VCSGisJCheckoutImpl.java @ 3519

History | View | Annotate | Download (15.6 KB)

1 2276 fdiaz
/*
2 2572 jjdelcerro
 * 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 2276 fdiaz
 */
22 2381 jjdelcerro
package org.gvsig.vcsgis.swing.impl.checkout;
23 2276 fdiaz
24 2325 fdiaz
import java.awt.Dimension;
25 2822 fdiaz
import java.awt.event.ActionEvent;
26
import javax.swing.ComboBoxModel;
27 2276 fdiaz
import javax.swing.JComponent;
28 2892 fdiaz
import javax.swing.SwingUtilities;
29 2276 fdiaz
import javax.swing.event.ChangeEvent;
30 2822 fdiaz
import org.apache.commons.lang3.StringUtils;
31
import org.gvsig.fmap.dal.feature.FeatureStore;
32 2827 fdiaz
import org.gvsig.tools.ToolsLocator;
33 3459 fdiaz
import org.gvsig.tools.dispose.DisposeUtils;
34 2827 fdiaz
import org.gvsig.tools.i18n.I18nManager;
35 2276 fdiaz
import org.gvsig.tools.swing.api.Component;
36
import org.gvsig.tools.swing.api.ToolsSwingLocator;
37
import org.gvsig.tools.swing.api.ToolsSwingManager;
38
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
39 2999 jjdelcerro
import org.gvsig.tools.swing.api.task.TaskStatusController;
40
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager;
41 2276 fdiaz
import org.gvsig.tools.swing.api.windowmanager.Dialog;
42
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
43 2822 fdiaz
import org.gvsig.tools.util.LabeledValue;
44 2697 jjdelcerro
import org.gvsig.vcsgis.lib.VCSGisEntity;
45 2999 jjdelcerro
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT;
46 2822 fdiaz
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
47 3180 fdiaz
import org.gvsig.vcsgis.lib.VCSGisRevision;
48 3458 jjdelcerro
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
49 2699 jjdelcerro
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
50 2631 fdiaz
import org.gvsig.vcsgis.swing.VCSGisJCheckout;
51
import org.gvsig.vcsgis.swing.VCSGisSwingLocator;
52
import org.gvsig.vcsgis.swing.VCSGisSwingManager;
53 2822 fdiaz
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
54 3234 jjdelcerro
import org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController;
55 3319 fdiaz
import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.REMOTE_ENTITIES;
56 2999 jjdelcerro
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater;
57 3233 fdiaz
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument;
58 3459 fdiaz
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationErrors;
59 3519 fdiaz
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationRequiredMessage;
60 3068 fdiaz
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_GROUP_NAME;
61
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_PROVIDER_NAME;
62
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.registerIcons;
63 3077 jjdelcerro
import org.gvsig.vcsgis.swing.impl.revisions.VCSGisRevisionPickerControllerImpl;
64 2276 fdiaz
import org.slf4j.LoggerFactory;
65
66
/**
67
 *
68 2572 jjdelcerro
 * @author gvSIG Team
69 2276 fdiaz
 */
70 2381 jjdelcerro
public class VCSGisJCheckoutImpl extends VCSGisJCheckoutView
71
        implements Component, VCSGisJCheckout {
72 2895 fdiaz
73 2381 jjdelcerro
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJCheckoutImpl.class);
74 2276 fdiaz
75
    private Dialog dialog;
76 2895 fdiaz
77 2998 fdiaz
    private boolean processing;
78 3234 jjdelcerro
    private PickerController<VCSGisWorkspace> workspacePicker;
79 2999 jjdelcerro
    private TaskStatusController taskStatusController;
80 3077 jjdelcerro
    private VCSGisRevisionPickerControllerImpl revisionPicker;
81 3234 jjdelcerro
    private VCSGisEntitySelectorController entitySelector;
82 2276 fdiaz
83 2381 jjdelcerro
    public VCSGisJCheckoutImpl() {
84 2999 jjdelcerro
        processing = false;
85 2276 fdiaz
        translate();
86 2330 fdiaz
87 2822 fdiaz
        initComponents();
88 2325 fdiaz
    }
89 2330 fdiaz
90 2822 fdiaz
    private void initComponents() {
91 2999 jjdelcerro
        TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager();
92
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
93
        VCSGisSwingServices services = swingManager.getDefaultServices();
94
95 3234 jjdelcerro
        this.entitySelector = VCSGisEntitySelectorController.create(lstTables, txtFilter, btnTable);
96 3319 fdiaz
        this.entitySelector.setFilter(REMOTE_ENTITIES);
97
        this.entitySelector.setViewFilter(REMOTE_ENTITIES);
98 3234 jjdelcerro
        this.entitySelector.setChecksEnabled(false);
99
        this.entitySelector.addActionListener((ActionEvent e) -> {
100
            doChangeTable();
101
        });
102
        this.entitySelector.addChangeListener((ChangeEvent e) -> {
103
            doUpdateComponents();
104
        });
105
106 2999 jjdelcerro
        this.taskStatusController = taskStatusManager.createTaskStatusController(
107
                this.lblStatusTitle,
108
                this.lblStatusMessages,
109
                this.pbStatus
110
        );
111
        this.taskStatusController.setShowCancelButton(false);
112
        this.taskStatusController.setShowRemoveTaskButton(false);
113
        this.taskStatusController.bind(ToolsLocator.getTaskStatusManager());
114
        SwingUtilities.invokeLater(() -> {this.setVisibleStatus(false); });
115
116 2381 jjdelcerro
        this.workspacePicker = swingManager.createWorkspacePickerController(
117 2331 fdiaz
                cboWorkspaces,
118
                btnInitWorkspace
119
        );
120 2895 fdiaz
121 2331 fdiaz
        this.workspacePicker.addChangeListener((ChangeEvent e) -> {
122
            doChangeWorkspace();
123 2895 fdiaz
            doUpdateComponents();
124 2331 fdiaz
        });
125 2895 fdiaz
126 3077 jjdelcerro
        this.revisionPicker = new VCSGisRevisionPickerControllerImpl(txtRevision, btnRevision);
127
128 2822 fdiaz
        ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
129 2895 fdiaz
130 2822 fdiaz
        cboView.setModel(viewDocumentsModel);
131
        cboView.addActionListener((ActionEvent e) -> {
132 2895 fdiaz
            doUpdateComponents();
133 2822 fdiaz
        });
134
135
        this.rdbDontAddToProject.addActionListener((ActionEvent e) -> {
136 2895 fdiaz
            doUpdateComponents();
137 2822 fdiaz
        });
138 2895 fdiaz
139 2822 fdiaz
        this.rdbAddLayerToView.addActionListener((ActionEvent e) -> {
140 2895 fdiaz
            doUpdateComponents();
141 2822 fdiaz
        });
142 2895 fdiaz
143 2822 fdiaz
        this.rdbAddTableToProject.addActionListener((ActionEvent e) -> {
144 2895 fdiaz
            doUpdateComponents();
145 2822 fdiaz
        });
146 3233 fdiaz
147
        this.chkOverwriteTable.addActionListener((ActionEvent e) -> {
148
            doUpdateComponents();
149
        });
150 2999 jjdelcerro
151 2998 fdiaz
        showMessage(" ", null);
152 2822 fdiaz
153 2999 jjdelcerro
        this.setPreferredSize(new Dimension(520, 400));
154 2325 fdiaz
155 2276 fdiaz
    }
156 2895 fdiaz
157 2276 fdiaz
    @Override
158
    public JComponent asJComponent() {
159
        return this;
160
    }
161
162 2999 jjdelcerro
    public void setVisibleStatus(boolean visible) {
163
        if( notInSwingThreadInvokeLater(() -> {setVisibleStatus(visible);}) ) {
164
            return;
165
        }
166 3068 fdiaz
        this.lblStatusTitle.setVisible(visible);
167
        this.pbStatus.setVisible(visible);
168 2999 jjdelcerro
        this.lblStatusMessages.setVisible(true);
169
    }
170
171 2330 fdiaz
    @Override
172 2276 fdiaz
    public void setDialog(Dialog dialog) {
173
        this.dialog = dialog;
174 2895 fdiaz
        this.doUpdateComponents();
175 2276 fdiaz
    }
176 2895 fdiaz
177 2331 fdiaz
    private void doChangeWorkspace() {
178 3459 fdiaz
        try {
179
            VCSGisWorkspace ws = this.getWorkspace();
180
            if (ws == null) {
181
                this.entitySelector.setWorkspace(ws);
182
                this.revisionPicker.setWorkspace(ws);
183
                return;
184
            }
185
            if (ws.authenticate(null)) {
186
                this.entitySelector.setWorkspace(ws);
187
                this.revisionPicker.setWorkspace(ws);
188
                return;
189
            }
190
            showAuthenticationRequiredMessage("_VCS_Checkout");
191
            this.workspacePicker.set(null);
192
            doChangeWorkspace();
193
        } catch (VCSGisRuntimeException e1) {
194
            LOGGER.warn("Can't set workspace.", e1);
195
            if (showAuthenticationErrors("_VCS_Checkout", e1)) {
196
                this.workspacePicker.set(null);
197
                doChangeWorkspace();
198
            }
199
        } catch (Exception e2) {
200
            LOGGER.warn("Can't set workspace.", e2);
201 2999 jjdelcerro
        }
202
    }
203
204 2381 jjdelcerro
    @Override
205
    public VCSGisWorkspace getWorkspace() {
206 2647 fdiaz
        return this.workspacePicker.get();
207 2331 fdiaz
    }
208
209 2895 fdiaz
    private void doUpdateComponents() {
210 2999 jjdelcerro
        if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) {
211
            return;
212
        }
213 3458 jjdelcerro
        try {
214
            boolean isProcessing = this.processing || this.entitySelector.isProcessing();
215 3234 jjdelcerro
216 3458 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
217
            VCSGisWorkspace workspace = getWorkspace();
218
            VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
219 2894 fdiaz
220 3458 jjdelcerro
            boolean exists = workspace!=null &&
221
                    workspace.existsInWorkspace(selectedTable);
222 2999 jjdelcerro
223 3458 jjdelcerro
            this.chkOverwriteTable.setEnabled(!isProcessing && exists);
224
225
            boolean canCheckout = !isProcessing &&
226
                    workspace!=null &&
227
                    selectedTable != null &&
228
                    (!exists || this.chkOverwriteTable.isSelected());
229
230
            if (selectedTable == null || canCheckout) {
231
                showMessage("", null);
232 2822 fdiaz
            } else {
233 3458 jjdelcerro
                showMessage(
234
                        i18n.getTranslation("_The_selected_table_is_already_in_the_working_copy"),
235
                        i18n.getTranslation("_To_download_updated_data_use_show_changes_tool")
236
                );
237
            }
238
            this.revisionPicker.setEnabled(canCheckout);
239
            this.lblRevisions.setEnabled(canCheckout);
240
241
            this.rdbDontAddToProject.setEnabled(canCheckout);
242
            this.rdbAddTableToProject.setEnabled(canCheckout);
243
            if (canCheckout) {
244
                if (isLayer(selectedTable)) {
245
                    this.rdbAddLayerToView.setEnabled(canCheckout);
246
                    this.cboView.setEnabled(canCheckout);
247
                } else {
248
                    this.rdbAddLayerToView.setEnabled(false);
249
                    this.cboView.setEnabled(false);
250
                }
251
            } else {
252 3233 fdiaz
                this.rdbAddLayerToView.setEnabled(false);
253
                this.cboView.setEnabled(false);
254 2822 fdiaz
            }
255 2331 fdiaz
256 3458 jjdelcerro
            this.workspacePicker.setEnabled(!isProcessing);
257
            this.entitySelector.setEnabled(!isProcessing && workspace!=null );
258
259
            if (dialog != null) {
260
                LabeledValue selectedView = (LabeledValue) cboView.getSelectedItem();
261
                boolean dontAddNothing = rdbDontAddToProject.isSelected();
262
                boolean addLayerToView = rdbAddLayerToView.isSelected() && selectedView != null && selectedView.getValue() != null;
263
                boolean addTableToProject = rdbAddTableToProject.isSelected();
264
                this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
265
                        canCheckout && (dontAddNothing || (addLayerToView || addTableToProject))
266
                );
267
            }
268
        } catch (VCSGisRuntimeException e1) {
269
            LOGGER.warn("Can't updating components.", e1);
270 3459 fdiaz
            if (showAuthenticationErrors("_VCS_Checkout", e1)) {
271 3458 jjdelcerro
                this.workspacePicker.set(null);
272
                doChangeWorkspace();
273
                doUpdateComponents();
274
            }
275
        } catch (Exception e2) {
276
            LOGGER.warn("Can't updating components.", e2);
277 2276 fdiaz
        }
278
    }
279 2895 fdiaz
280
    private boolean isLayer(VCSGisEntity entity) {
281
        return entity != null && !StringUtils.isBlank(entity.getGeometryFieldName());
282 2822 fdiaz
    }
283 2330 fdiaz
284
    @Override
285 2331 fdiaz
    public int checkout() {
286 2998 fdiaz
        I18nManager i18n = ToolsLocator.getI18nManager();
287 3519 fdiaz
        setVisibleStatus(true);
288 2999 jjdelcerro
289 3234 jjdelcerro
        VCSGisEntity entity = entitySelector.getSelectedEntity();
290 2999 jjdelcerro
        final VCSGisWorkspace workspace = this.getWorkspace();
291 3233 fdiaz
292 3180 fdiaz
        VCSGisRevision revision = this.revisionPicker.get();
293 2999 jjdelcerro
        if (workspace != null && entity != null) {
294 2892 fdiaz
            try {
295 2999 jjdelcerro
                processing = true;
296 3006 fdiaz
                doUpdateComponents();
297 2999 jjdelcerro
298
                showMessage(i18n.getTranslation("_Processing"), null);
299 3233 fdiaz
                int res = ERR_OK;
300
                if(chkOverwriteTable.isSelected()){
301
                    res = workspace.removeEntity(entity.getEntityCode());
302
                }
303
                if(res == ERR_OK){
304
                    res = workspace.checkout(
305
                            entity.getEntityName(),
306
                            (revision == null) ? null : revision.getRevisionCode()
307
                    );
308 3453 fdiaz
                    VCSGisEntity lEntity = workspace.getEntity(entity.getEntityCode());
309 3519 fdiaz
                    if(StringUtils.isNotBlank(lEntity.getResources())){
310
                        workspace.getExplorer().setCustomResources(entity.getEntityName(), lEntity.getResources());
311
                    }
312 3453 fdiaz
                    postCheckout(res, entity);
313 3233 fdiaz
                }
314 2999 jjdelcerro
                showMessage(i18n.getTranslation("_Ended_process"), null);
315
                return res;
316
            } finally {
317
                processing = false;
318 3006 fdiaz
                doUpdateComponents();
319 2892 fdiaz
            }
320
        }
321 2999 jjdelcerro
        return ERR_CANT_CHECKOUT;
322
    }
323 2822 fdiaz
324 2999 jjdelcerro
    private void postCheckout(int checkoutStatus, VCSGisEntity entity) {
325
        if( notInSwingThreadInvokeLater(() -> {postCheckout(checkoutStatus,entity);}) ) {
326
            return;
327
        }
328
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
329
        VCSGisSwingServices services = swingManager.getDefaultServices();
330
        final VCSGisWorkspace workspace = this.getWorkspace();
331 2895 fdiaz
332 2999 jjdelcerro
        if (checkoutStatus == ERR_OK) {
333 3233 fdiaz
            FeatureStore store = workspace.getFeatureStore(entity.getEntityName());
334 2999 jjdelcerro
            if (!rdbDontAddToProject.isSelected()) {
335
                if (rdbAddLayerToView.isSelected()) {
336
                    if (isLayer(entity)) {
337 3233 fdiaz
                        String layerName = store.getName();
338 2999 jjdelcerro
                        services.addLayerToView(store, (LabeledValue) cboView.getSelectedItem(), layerName);
339 2822 fdiaz
                    }
340
                }
341 2999 jjdelcerro
                if (rdbAddTableToProject.isSelected()) {
342
                    services.addTableToProject(workspace, store);
343
                }
344 2822 fdiaz
            }
345 3233 fdiaz
            if(chkOverwriteTable.isSelected()){
346
                refreshDocument(store);
347 3459 fdiaz
            }
348
            DisposeUtils.disposeQuietly(store);
349 3233 fdiaz
350 2999 jjdelcerro
            this.setVisibleStatus(false);
351
        } else {
352
            this.setVisibleStatus(true);
353
        }
354 3006 fdiaz
        this.doUpdateComponents();
355 2330 fdiaz
    }
356 2999 jjdelcerro
357 2998 fdiaz
    private void showMessage(String message, String toolTipText) {
358 2999 jjdelcerro
        if( notInSwingThreadInvokeLater(() -> {showMessage(message, toolTipText);}) ) {
359 2998 fdiaz
            return;
360
        }
361 2999 jjdelcerro
        this.lblStatusMessages.setText(message);
362
        this.lblStatusMessages.setToolTipText(toolTipText);
363 2998 fdiaz
    }
364 2330 fdiaz
365 2276 fdiaz
    private void translate() {
366
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
367 2331 fdiaz
        swingManager.translate(this.lblWorkspace);
368
        swingManager.translate(this.lblTable);
369 3077 jjdelcerro
        swingManager.translate(this.lblRevisions);
370 2822 fdiaz
        swingManager.translate(this.rdbDontAddToProject);
371
        swingManager.translate(this.rdbAddLayerToView);
372
        swingManager.translate(this.rdbAddTableToProject);
373 3180 fdiaz
        swingManager.translate(this.chkOverwriteTable);
374 2276 fdiaz
    }
375
376 2999 jjdelcerro
    @Override
377
    public boolean isProcessing() {
378 2998 fdiaz
        return processing;
379
    }
380 2892 fdiaz
381 3068 fdiaz
    public static void selfRegister() {
382
        String[][] iconsInfo = new String[][]{
383
            new String[]{ICON_PROVIDER_NAME, ICON_GROUP_NAME, "vcsgis-checkout"},};
384
        registerIcons(iconsInfo);
385
    }
386
387 3234 jjdelcerro
    private void doChangeTable() {
388
        VCSGisWorkspace workspace = getWorkspace();
389
        VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
390
391 3233 fdiaz
        boolean isLayer = isLayer(selectedTable);
392
        this.rdbAddLayerToView.setSelected(isLayer);
393
        this.rdbAddTableToProject.setSelected(!isLayer);
394 3234 jjdelcerro
395
        this.revisionPicker.setEntity(selectedTable);
396
397
        this.chkOverwriteTable.setSelected(workspace!=null && workspace.existsInWorkspace(selectedTable));
398
399 3233 fdiaz
        doUpdateComponents();
400
    }
401
402 3234 jjdelcerro
    @Override
403
    public VCSGisEntity getTable() {
404
        return this.entitySelector.getSelectedEntity();
405
    }
406
407 2276 fdiaz
}