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 |
/*
|
---|---|
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.Dimension; |
25 |
import java.awt.event.ActionEvent; |
26 |
import javax.swing.ComboBoxModel; |
27 |
import javax.swing.JComponent; |
28 |
import javax.swing.SwingUtilities; |
29 |
import javax.swing.event.ChangeEvent; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.tools.ToolsLocator; |
33 |
import org.gvsig.tools.dispose.DisposeUtils; |
34 |
import org.gvsig.tools.i18n.I18nManager; |
35 |
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 |
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.impl.VCSGisEntitySelectorController; |
55 |
import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.REMOTE_ENTITIES; |
56 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater; |
57 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument; |
58 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationErrors; |
59 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationRequiredMessage; |
60 |
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 |
import org.gvsig.vcsgis.swing.impl.revisions.VCSGisRevisionPickerControllerImpl; |
64 |
import org.slf4j.LoggerFactory; |
65 |
|
66 |
/**
|
67 |
*
|
68 |
* @author gvSIG Team
|
69 |
*/
|
70 |
public class VCSGisJCheckoutImpl extends VCSGisJCheckoutView |
71 |
implements Component, VCSGisJCheckout { |
72 |
|
73 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJCheckoutImpl.class); |
74 |
|
75 |
private Dialog dialog; |
76 |
|
77 |
private boolean processing; |
78 |
private PickerController<VCSGisWorkspace> workspacePicker;
|
79 |
private TaskStatusController taskStatusController;
|
80 |
private VCSGisRevisionPickerControllerImpl revisionPicker;
|
81 |
private VCSGisEntitySelectorController entitySelector;
|
82 |
|
83 |
public VCSGisJCheckoutImpl() {
|
84 |
processing = false;
|
85 |
translate(); |
86 |
|
87 |
initComponents(); |
88 |
} |
89 |
|
90 |
private void initComponents() { |
91 |
TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager(); |
92 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
93 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
94 |
|
95 |
this.entitySelector = VCSGisEntitySelectorController.create(lstTables, txtFilter, btnTable);
|
96 |
this.entitySelector.setFilter(REMOTE_ENTITIES);
|
97 |
this.entitySelector.setViewFilter(REMOTE_ENTITIES);
|
98 |
this.entitySelector.setChecksEnabled(false); |
99 |
this.entitySelector.addActionListener((ActionEvent e) -> { |
100 |
doChangeTable(); |
101 |
}); |
102 |
this.entitySelector.addChangeListener((ChangeEvent e) -> { |
103 |
doUpdateComponents(); |
104 |
}); |
105 |
|
106 |
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 |
this.workspacePicker = swingManager.createWorkspacePickerController(
|
117 |
cboWorkspaces, |
118 |
btnInitWorkspace |
119 |
); |
120 |
|
121 |
this.workspacePicker.addChangeListener((ChangeEvent e) -> { |
122 |
doChangeWorkspace(); |
123 |
doUpdateComponents(); |
124 |
}); |
125 |
|
126 |
this.revisionPicker = new VCSGisRevisionPickerControllerImpl(txtRevision, btnRevision); |
127 |
|
128 |
ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
|
129 |
|
130 |
cboView.setModel(viewDocumentsModel); |
131 |
cboView.addActionListener((ActionEvent e) -> {
|
132 |
doUpdateComponents(); |
133 |
}); |
134 |
|
135 |
this.rdbDontAddToProject.addActionListener((ActionEvent e) -> { |
136 |
doUpdateComponents(); |
137 |
}); |
138 |
|
139 |
this.rdbAddLayerToView.addActionListener((ActionEvent e) -> { |
140 |
doUpdateComponents(); |
141 |
}); |
142 |
|
143 |
this.rdbAddTableToProject.addActionListener((ActionEvent e) -> { |
144 |
doUpdateComponents(); |
145 |
}); |
146 |
|
147 |
this.chkOverwriteTable.addActionListener((ActionEvent e) -> { |
148 |
doUpdateComponents(); |
149 |
}); |
150 |
|
151 |
showMessage(" ", null); |
152 |
|
153 |
this.setPreferredSize(new Dimension(520, 400)); |
154 |
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public JComponent asJComponent() { |
159 |
return this; |
160 |
} |
161 |
|
162 |
public void setVisibleStatus(boolean visible) { |
163 |
if( notInSwingThreadInvokeLater(() -> {setVisibleStatus(visible);}) ) {
|
164 |
return;
|
165 |
} |
166 |
this.lblStatusTitle.setVisible(visible);
|
167 |
this.pbStatus.setVisible(visible);
|
168 |
this.lblStatusMessages.setVisible(true); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void setDialog(Dialog dialog) { |
173 |
this.dialog = dialog;
|
174 |
this.doUpdateComponents();
|
175 |
} |
176 |
|
177 |
private void doChangeWorkspace() { |
178 |
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 |
} |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public VCSGisWorkspace getWorkspace() {
|
206 |
return this.workspacePicker.get(); |
207 |
} |
208 |
|
209 |
private void doUpdateComponents() { |
210 |
if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) { |
211 |
return;
|
212 |
} |
213 |
try {
|
214 |
boolean isProcessing = this.processing || this.entitySelector.isProcessing(); |
215 |
|
216 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
217 |
VCSGisWorkspace workspace = getWorkspace(); |
218 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
219 |
|
220 |
boolean exists = workspace!=null && |
221 |
workspace.existsInWorkspace(selectedTable); |
222 |
|
223 |
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 |
} else {
|
233 |
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 |
this.rdbAddLayerToView.setEnabled(false); |
253 |
this.cboView.setEnabled(false); |
254 |
} |
255 |
|
256 |
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 |
if (showAuthenticationErrors("_VCS_Checkout", e1)) { |
271 |
this.workspacePicker.set(null); |
272 |
doChangeWorkspace(); |
273 |
doUpdateComponents(); |
274 |
} |
275 |
} catch (Exception e2) { |
276 |
LOGGER.warn("Can't updating components.", e2);
|
277 |
} |
278 |
} |
279 |
|
280 |
private boolean isLayer(VCSGisEntity entity) { |
281 |
return entity != null && !StringUtils.isBlank(entity.getGeometryFieldName()); |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public int checkout() { |
286 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
287 |
setVisibleStatus(true);
|
288 |
|
289 |
VCSGisEntity entity = entitySelector.getSelectedEntity(); |
290 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
291 |
|
292 |
VCSGisRevision revision = this.revisionPicker.get();
|
293 |
if (workspace != null && entity != null) { |
294 |
try {
|
295 |
processing = true;
|
296 |
doUpdateComponents(); |
297 |
|
298 |
showMessage(i18n.getTranslation("_Processing"), null); |
299 |
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 |
VCSGisEntity lEntity = workspace.getEntity(entity.getEntityCode()); |
309 |
if(StringUtils.isNotBlank(lEntity.getResources())){
|
310 |
workspace.getExplorer().setCustomResources(entity.getEntityName(), lEntity.getResources()); |
311 |
} |
312 |
postCheckout(res, entity); |
313 |
} |
314 |
showMessage(i18n.getTranslation("_Ended_process"), null); |
315 |
return res;
|
316 |
} finally {
|
317 |
processing = false;
|
318 |
doUpdateComponents(); |
319 |
} |
320 |
} |
321 |
return ERR_CANT_CHECKOUT;
|
322 |
} |
323 |
|
324 |
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 |
|
332 |
if (checkoutStatus == ERR_OK) {
|
333 |
FeatureStore store = workspace.getFeatureStore(entity.getEntityName()); |
334 |
if (!rdbDontAddToProject.isSelected()) {
|
335 |
if (rdbAddLayerToView.isSelected()) {
|
336 |
if (isLayer(entity)) {
|
337 |
String layerName = store.getName();
|
338 |
services.addLayerToView(store, (LabeledValue) cboView.getSelectedItem(), layerName); |
339 |
} |
340 |
} |
341 |
if (rdbAddTableToProject.isSelected()) {
|
342 |
services.addTableToProject(workspace, store); |
343 |
} |
344 |
} |
345 |
if(chkOverwriteTable.isSelected()){
|
346 |
refreshDocument(store); |
347 |
} |
348 |
DisposeUtils.disposeQuietly(store); |
349 |
|
350 |
this.setVisibleStatus(false); |
351 |
} else {
|
352 |
this.setVisibleStatus(true); |
353 |
} |
354 |
this.doUpdateComponents();
|
355 |
} |
356 |
|
357 |
private void showMessage(String message, String toolTipText) { |
358 |
if( notInSwingThreadInvokeLater(() -> {showMessage(message, toolTipText);}) ) {
|
359 |
return;
|
360 |
} |
361 |
this.lblStatusMessages.setText(message);
|
362 |
this.lblStatusMessages.setToolTipText(toolTipText);
|
363 |
} |
364 |
|
365 |
private void translate() { |
366 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
367 |
swingManager.translate(this.lblWorkspace);
|
368 |
swingManager.translate(this.lblTable);
|
369 |
swingManager.translate(this.lblRevisions);
|
370 |
swingManager.translate(this.rdbDontAddToProject);
|
371 |
swingManager.translate(this.rdbAddLayerToView);
|
372 |
swingManager.translate(this.rdbAddTableToProject);
|
373 |
swingManager.translate(this.chkOverwriteTable);
|
374 |
} |
375 |
|
376 |
@Override
|
377 |
public boolean isProcessing() { |
378 |
return processing;
|
379 |
} |
380 |
|
381 |
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 |
private void doChangeTable() { |
388 |
VCSGisWorkspace workspace = getWorkspace(); |
389 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
390 |
|
391 |
boolean isLayer = isLayer(selectedTable);
|
392 |
this.rdbAddLayerToView.setSelected(isLayer);
|
393 |
this.rdbAddTableToProject.setSelected(!isLayer);
|
394 |
|
395 |
this.revisionPicker.setEntity(selectedTable);
|
396 |
|
397 |
this.chkOverwriteTable.setSelected(workspace!=null && workspace.existsInWorkspace(selectedTable)); |
398 |
|
399 |
doUpdateComponents(); |
400 |
} |
401 |
|
402 |
@Override
|
403 |
public VCSGisEntity getTable() {
|
404 |
return this.entitySelector.getSelectedEntity(); |
405 |
} |
406 |
|
407 |
} |