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 / checkoutDataModel / VCSGisJCheckoutDataModelImpl.java @ 3517
History | View | Annotate | Download (15.4 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.checkoutDataModel; |
23 |
|
24 |
import java.awt.Cursor; |
25 |
import java.awt.Dimension; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ItemEvent; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashSet; |
30 |
import java.util.List; |
31 |
import java.util.Set; |
32 |
import javax.swing.ComboBoxModel; |
33 |
import javax.swing.DefaultComboBoxModel; |
34 |
import javax.swing.ImageIcon; |
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.SwingUtilities; |
37 |
import javax.swing.event.ChangeEvent; |
38 |
import org.apache.commons.lang3.StringUtils; |
39 |
import org.gvsig.fmap.dal.BaseStoresRepository; |
40 |
import org.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
43 |
import org.gvsig.fmap.dal.StoresRepository; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.dispose.DisposeUtils; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.swing.api.Component; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
51 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
52 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
53 |
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager; |
54 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
55 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
56 |
import org.gvsig.tools.util.ContainerUtils; |
57 |
import static org.gvsig.tools.util.ContainerUtils.EQUALS_IGNORECASE_COMPARATOR; |
58 |
import org.gvsig.tools.util.LabeledValue; |
59 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
60 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT; |
61 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
62 |
import org.gvsig.vcsgis.lib.VCSGisRuntimeException; |
63 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
64 |
import org.gvsig.vcsgis.swing.VCSGisJCheckoutDataModel; |
65 |
import org.gvsig.vcsgis.swing.VCSGisSwingLocator; |
66 |
import org.gvsig.vcsgis.swing.VCSGisSwingManager; |
67 |
import org.gvsig.vcsgis.swing.VCSGisSwingServices; |
68 |
import org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController; |
69 |
import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.ALL_ENTITIES; |
70 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater; |
71 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument; |
72 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationErrors; |
73 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationRequiredMessage; |
74 |
import org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl; |
75 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_COMMON_GROUP_NAME; |
76 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_GROUP_NAME; |
77 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_PROVIDER_NAME; |
78 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.registerIcons; |
79 |
import org.slf4j.LoggerFactory; |
80 |
|
81 |
/**
|
82 |
*
|
83 |
* @author gvSIG Team
|
84 |
*/
|
85 |
public class VCSGisJCheckoutDataModelImpl extends VCSGisJCheckoutDataModelView |
86 |
implements Component, VCSGisJCheckoutDataModel { |
87 |
|
88 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJCheckoutDataModelImpl.class); |
89 |
|
90 |
private Dialog dialog; |
91 |
|
92 |
private boolean processing; |
93 |
private PickerController<VCSGisWorkspace> workspacePicker;
|
94 |
private TaskStatusController taskStatusController;
|
95 |
private VCSGisEntitySelectorController entitySelector;
|
96 |
|
97 |
public VCSGisJCheckoutDataModelImpl() {
|
98 |
processing = false;
|
99 |
initComponents(); |
100 |
} |
101 |
|
102 |
private void initComponents() { |
103 |
TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager(); |
104 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
105 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
106 |
|
107 |
translate(); |
108 |
|
109 |
this.btnCheckAllEntities.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
110 |
this.btnUnCheckAllEntities.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
111 |
|
112 |
|
113 |
this.entitySelector = VCSGisEntitySelectorController.create(lstTables, txtFilter, btnTable, btnCheckAllEntities, btnUnCheckAllEntities);
|
114 |
this.entitySelector.setFilter(ALL_ENTITIES);
|
115 |
this.entitySelector.setViewFilter(ALL_ENTITIES);
|
116 |
this.entitySelector.setChecksEnabled(true); |
117 |
// this.entitySelector.addActionListener((ActionEvent e) -> {
|
118 |
// doChangeTable();
|
119 |
// });
|
120 |
this.entitySelector.addChangeListener((ChangeEvent e) -> { |
121 |
doUpdateComponents(); |
122 |
}); |
123 |
|
124 |
this.taskStatusController = taskStatusManager.createTaskStatusController(
|
125 |
this.lblStatusTitle,
|
126 |
this.lblStatusMessages,
|
127 |
this.pbStatus
|
128 |
); |
129 |
this.taskStatusController.setShowCancelButton(false); |
130 |
this.taskStatusController.setShowRemoveTaskButton(false); |
131 |
this.taskStatusController.bind(ToolsLocator.getTaskStatusManager());
|
132 |
SwingUtilities.invokeLater(() -> {this.setVisibleStatus(false); }); |
133 |
|
134 |
this.workspacePicker = swingManager.createWorkspacePickerController(
|
135 |
cboWorkspaces, |
136 |
btnInitWorkspace |
137 |
); |
138 |
|
139 |
this.workspacePicker.addChangeListener((ChangeEvent e) -> { |
140 |
doChangeWorkspace(); |
141 |
doUpdateComponents(); |
142 |
}); |
143 |
|
144 |
this.cboDataModels.addItemListener((ItemEvent e) -> { |
145 |
doChangeDataModel(); |
146 |
}); |
147 |
|
148 |
ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
|
149 |
|
150 |
this.chkOverwriteExistingTables.addActionListener((ActionEvent e) -> { |
151 |
doUpdateComponents(); |
152 |
}); |
153 |
|
154 |
showMessage(" ", null); |
155 |
|
156 |
this.setPreferredSize(new Dimension(520, 400)); |
157 |
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public JComponent asJComponent() { |
162 |
return this; |
163 |
} |
164 |
|
165 |
public void setVisibleStatus(boolean visible) { |
166 |
if( notInSwingThreadInvokeLater(() -> {setVisibleStatus(visible);}) ) {
|
167 |
return;
|
168 |
} |
169 |
this.lblStatusTitle.setVisible(visible);
|
170 |
this.pbStatus.setVisible(visible);
|
171 |
this.lblStatusMessages.setVisible(true); |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void setDialog(Dialog dialog) { |
176 |
this.dialog = dialog;
|
177 |
this.doUpdateComponents();
|
178 |
} |
179 |
|
180 |
private void doChangeWorkspace() { |
181 |
try {
|
182 |
VCSGisWorkspace ws = this.getWorkspace();
|
183 |
if (ws == null) { |
184 |
ComboBoxModel<String> model = new DefaultComboBoxModel<>(); |
185 |
this.cboDataModels.setModel(model);
|
186 |
this.cboDataModels.setSelectedIndex(-1); |
187 |
this.entitySelector.setWorkspace(ws);
|
188 |
doUpdateComponents(); |
189 |
return;
|
190 |
} |
191 |
if (ws.authenticate(null)) { |
192 |
ComboBoxModel<String> model = new DefaultComboBoxModel<>(ws.getDataModels().toArray(new String[0])); |
193 |
this.cboDataModels.setModel(model);
|
194 |
this.cboDataModels.setSelectedIndex(-1); |
195 |
this.entitySelector.setWorkspace(ws);
|
196 |
doUpdateComponents(); |
197 |
return;
|
198 |
} |
199 |
showAuthenticationRequiredMessage("_VCS_Checkout");
|
200 |
this.workspacePicker.set(null); |
201 |
doChangeWorkspace(); |
202 |
} catch (VCSGisRuntimeException e1) {
|
203 |
LOGGER.warn("Can't set workspace.", e1);
|
204 |
if (showAuthenticationErrors("_VCS_Checkout", e1)) { |
205 |
this.workspacePicker.set(null); |
206 |
doChangeWorkspace(); |
207 |
} |
208 |
} catch (Exception e2) { |
209 |
LOGGER.warn("Can't set workspace.", e2);
|
210 |
} |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public VCSGisWorkspace getWorkspace() {
|
215 |
return this.workspacePicker.get(); |
216 |
} |
217 |
|
218 |
private void doUpdateComponents() { |
219 |
if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) { |
220 |
return;
|
221 |
} |
222 |
boolean isProcessing = this.processing || this.entitySelector.isProcessing(); |
223 |
|
224 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
225 |
VCSGisWorkspace workspace = getWorkspace(); |
226 |
List<VCSGisEntity> checkedEntities = this.entitySelector.getCheckedEntities(); |
227 |
|
228 |
this.btnCheckAllEntities.setEnabled(!isProcessing);
|
229 |
this.btnUnCheckAllEntities.setEnabled(!isProcessing);
|
230 |
|
231 |
this.chkOverwriteExistingTables.setEnabled(!isProcessing);
|
232 |
|
233 |
boolean canCheckout = !isProcessing &&
|
234 |
workspace!=null &&
|
235 |
checkedEntities.size() > 0;
|
236 |
|
237 |
this.workspacePicker.setEnabled(!isProcessing);
|
238 |
this.entitySelector.setEnabled(!isProcessing && workspace!=null ); |
239 |
|
240 |
this.pbStatus.setVisible(isProcessing);
|
241 |
this.lblStatusTitle.setVisible(isProcessing);
|
242 |
|
243 |
if (dialog != null) { |
244 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
|
245 |
canCheckout |
246 |
); |
247 |
} |
248 |
} |
249 |
|
250 |
private boolean isLayer(VCSGisEntity entity) { |
251 |
return entity != null && !StringUtils.isBlank(entity.getGeometryFieldName()); |
252 |
} |
253 |
|
254 |
@Override
|
255 |
public int checkout() { |
256 |
|
257 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
258 |
|
259 |
List<VCSGisEntity> entities = entitySelector.getCheckedEntities();
|
260 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
261 |
final String dataModelName = this.getDataModel(); |
262 |
|
263 |
if (workspace != null && entities != null && entities.size()>0) { |
264 |
List<String> entityCodes = new ArrayList<>(); |
265 |
for (VCSGisEntity entity : entities) {
|
266 |
entityCodes.add(entity.getEntityCode()); |
267 |
} |
268 |
|
269 |
try {
|
270 |
processing = true;
|
271 |
doUpdateComponents(); |
272 |
|
273 |
showMessage(i18n.getTranslation("_Processing"), null); |
274 |
int res = ERR_OK;
|
275 |
if(chkOverwriteExistingTables.isSelected()){
|
276 |
res = workspace.removeEntities(entityCodes); |
277 |
} |
278 |
if(res == ERR_OK){
|
279 |
res = workspace.checkout( |
280 |
entityCodes, |
281 |
null
|
282 |
); |
283 |
for (VCSGisEntity entity : entities) {
|
284 |
VCSGisEntity lEntity = workspace.getEntity(entity.getEntityCode()); |
285 |
workspace.getExplorer().setCustomResources(entity.getEntityName(), lEntity.getResources()); |
286 |
} |
287 |
registerStoresRepository(workspace, dataModelName, entities); |
288 |
postCheckout(res, entities); |
289 |
} |
290 |
showMessage(i18n.getTranslation("_Ended_process"), null); |
291 |
return res;
|
292 |
} finally {
|
293 |
processing = false;
|
294 |
doUpdateComponents(); |
295 |
} |
296 |
} |
297 |
return ERR_CANT_CHECKOUT;
|
298 |
} |
299 |
|
300 |
private void postCheckout(int checkoutStatus, List<VCSGisEntity> entities) { |
301 |
if( notInSwingThreadInvokeLater(() -> {postCheckout(checkoutStatus,entities);}) ) {
|
302 |
return;
|
303 |
} |
304 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
305 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
306 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
307 |
|
308 |
if (checkoutStatus == ERR_OK) {
|
309 |
//Nos vamos guardando los stores para, después, refrescar solo una vez cada documento al que pertenecen
|
310 |
Set stores = new HashSet(); |
311 |
for (VCSGisEntity entity : entities) {
|
312 |
FeatureStore store = workspace.getFeatureStore(entity.getEntityName()); |
313 |
stores.add(store); |
314 |
} |
315 |
if (chkOverwriteExistingTables.isSelected() && !stores.isEmpty()) {
|
316 |
refreshDocument(stores); |
317 |
} |
318 |
for (Object store : stores) { |
319 |
DisposeUtils.dispose(store); |
320 |
} |
321 |
|
322 |
this.setVisibleStatus(false); |
323 |
} else {
|
324 |
this.setVisibleStatus(true); |
325 |
} |
326 |
this.doUpdateComponents();
|
327 |
} |
328 |
|
329 |
private void registerStoresRepository(VCSGisWorkspace workspace, String name, List<VCSGisEntity> entities) { |
330 |
DataManager dataManager = DALLocator.getDataManager(); |
331 |
BaseStoresRepository modelRepository = new BaseStoresRepository(name);
|
332 |
for (VCSGisEntity entity : entities) {
|
333 |
OpenDataStoreParameters params = workspace.createOpenStoreParameters(entity.getEntityName()); |
334 |
modelRepository.add(entity.getEntityName(), params); |
335 |
} |
336 |
StoresRepository globalRepository = dataManager.getStoresRepository(); |
337 |
globalRepository.addRepository(modelRepository); |
338 |
} |
339 |
|
340 |
private void showMessage(String message, String toolTipText) { |
341 |
if( notInSwingThreadInvokeLater(() -> {showMessage(message, toolTipText);}) ) {
|
342 |
return;
|
343 |
} |
344 |
this.lblStatusMessages.setText(message);
|
345 |
this.lblStatusMessages.setToolTipText(toolTipText);
|
346 |
} |
347 |
|
348 |
private void translate() { |
349 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
350 |
swingManager.translate(this.lblWorkspace);
|
351 |
swingManager.translate(this.lblDataModel);
|
352 |
swingManager.translate(this.chkOverwriteExistingTables);
|
353 |
} |
354 |
|
355 |
@Override
|
356 |
public boolean isProcessing() { |
357 |
return processing;
|
358 |
} |
359 |
|
360 |
public static void selfRegister() { |
361 |
String[][] iconsInfo = new String[][]{ |
362 |
new String[]{ICON_PROVIDER_NAME, ICON_GROUP_NAME, "vcsgis-checkout"}, |
363 |
new String[]{ICON_PROVIDER_NAME, ICON_COMMON_GROUP_NAME, "common-check-off"}, |
364 |
new String[]{ICON_PROVIDER_NAME, ICON_COMMON_GROUP_NAME, "common-check-on"}, |
365 |
}; |
366 |
registerIcons(iconsInfo); |
367 |
} |
368 |
|
369 |
// private void doChangeTable() {
|
370 |
// VCSGisWorkspace workspace = getWorkspace();
|
371 |
// VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
372 |
//
|
373 |
// this.chkOverwriteExistingTables.setSelected(workspace!=null && workspace.existsInWorkspace(selectedTable));
|
374 |
//
|
375 |
// doUpdateComponents();
|
376 |
// }
|
377 |
|
378 |
@Override
|
379 |
public String getDataModel() { |
380 |
return (String)this.cboDataModels.getSelectedItem(); |
381 |
} |
382 |
|
383 |
private void doChangeDataModel() { |
384 |
String selectedDataModel = getDataModel();
|
385 |
this.entitySelector.setViewFilter(
|
386 |
(VCSGisEntity t) -> (ContainerUtils.contains( |
387 |
t.getDataModelsAsList(), |
388 |
selectedDataModel, |
389 |
EQUALS_IGNORECASE_COMPARATOR)) |
390 |
); |
391 |
|
392 |
} |
393 |
|
394 |
@Override
|
395 |
public ImageIcon loadImage(String imageName) { |
396 |
return VCSGisSwingManagerImpl.loadImage(imageName);
|
397 |
} |
398 |
|
399 |
|
400 |
} |