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