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 @ 3459
History | View | Annotate | Download (14.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.checkoutDataModel; |
23 |
|
24 |
import java.awt.Dimension; |
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ItemEvent; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashSet; |
29 |
import java.util.List; |
30 |
import java.util.Set; |
31 |
import javax.swing.ComboBoxModel; |
32 |
import javax.swing.DefaultComboBoxModel; |
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.SwingUtilities; |
35 |
import javax.swing.event.ChangeEvent; |
36 |
import org.apache.commons.lang3.StringUtils; |
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dispose.DisposeUtils; |
40 |
import org.gvsig.tools.i18n.I18nManager; |
41 |
import org.gvsig.tools.swing.api.Component; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
44 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
45 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
46 |
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager; |
47 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
48 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
49 |
import org.gvsig.tools.util.LabeledValue; |
50 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
51 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT; |
52 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
53 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
54 |
import org.gvsig.vcsgis.swing.VCSGisJCheckoutDataModel; |
55 |
import org.gvsig.vcsgis.swing.VCSGisSwingLocator; |
56 |
import org.gvsig.vcsgis.swing.VCSGisSwingManager; |
57 |
import org.gvsig.vcsgis.swing.VCSGisSwingServices; |
58 |
import org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController; |
59 |
import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.ALL_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.VCSGisSwingManagerImpl.ICON_GROUP_NAME; |
63 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_PROVIDER_NAME; |
64 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.registerIcons; |
65 |
import org.slf4j.LoggerFactory; |
66 |
|
67 |
/**
|
68 |
*
|
69 |
* @author gvSIG Team
|
70 |
*/
|
71 |
public class VCSGisJCheckoutDataModelImpl extends VCSGisJCheckoutDataModelView |
72 |
implements Component, VCSGisJCheckoutDataModel { |
73 |
|
74 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJCheckoutDataModelImpl.class); |
75 |
|
76 |
private Dialog dialog; |
77 |
|
78 |
private boolean processing; |
79 |
private PickerController<VCSGisWorkspace> workspacePicker;
|
80 |
private TaskStatusController taskStatusController;
|
81 |
private VCSGisEntitySelectorController entitySelector;
|
82 |
|
83 |
public VCSGisJCheckoutDataModelImpl() {
|
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(ALL_ENTITIES);
|
97 |
this.entitySelector.setViewFilter(ALL_ENTITIES);
|
98 |
this.entitySelector.setChecksEnabled(true); |
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.cboDataModels.addItemListener((ItemEvent e) -> { |
127 |
String selectedDataModel = getDataModel();
|
128 |
this.entitySelector.setViewFilter(
|
129 |
(VCSGisEntity t) -> (t.getDataModelsAsList().contains(selectedDataModel))); |
130 |
}); |
131 |
|
132 |
ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
|
133 |
|
134 |
cboView.setModel(viewDocumentsModel); |
135 |
cboView.addActionListener((ActionEvent e) -> {
|
136 |
doUpdateComponents(); |
137 |
}); |
138 |
|
139 |
this.rdbDontAddToProject.addActionListener((ActionEvent e) -> { |
140 |
doUpdateComponents(); |
141 |
}); |
142 |
|
143 |
this.rdbAddLayerToView.addActionListener((ActionEvent e) -> { |
144 |
doUpdateComponents(); |
145 |
}); |
146 |
|
147 |
this.rdbAddTableToProject.addActionListener((ActionEvent e) -> { |
148 |
doUpdateComponents(); |
149 |
}); |
150 |
|
151 |
this.chkOverwriteExistingTables.addActionListener((ActionEvent e) -> { |
152 |
doUpdateComponents(); |
153 |
}); |
154 |
|
155 |
showMessage(" ", null); |
156 |
|
157 |
this.setPreferredSize(new Dimension(520, 400)); |
158 |
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public JComponent asJComponent() { |
163 |
return this; |
164 |
} |
165 |
|
166 |
public void setVisibleStatus(boolean visible) { |
167 |
if( notInSwingThreadInvokeLater(() -> {setVisibleStatus(visible);}) ) {
|
168 |
return;
|
169 |
} |
170 |
this.lblStatusTitle.setVisible(visible);
|
171 |
this.pbStatus.setVisible(visible);
|
172 |
this.lblStatusMessages.setVisible(true); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void setDialog(Dialog dialog) { |
177 |
this.dialog = dialog;
|
178 |
this.doUpdateComponents();
|
179 |
} |
180 |
|
181 |
private void doChangeWorkspace() { |
182 |
VCSGisWorkspace workspace = workspacePicker.get(); |
183 |
if (workspace == null) { |
184 |
return;
|
185 |
} |
186 |
ComboBoxModel<String> model = new DefaultComboBoxModel<>(workspace.getDataModels().toArray(new String[0])); |
187 |
this.cboDataModels.setModel(model);
|
188 |
this.cboDataModels.setSelectedIndex(-1); |
189 |
this.entitySelector.setWorkspace(workspace);
|
190 |
doUpdateComponents(); |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public VCSGisWorkspace getWorkspace() {
|
195 |
return this.workspacePicker.get(); |
196 |
} |
197 |
|
198 |
private void doUpdateComponents() { |
199 |
if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) { |
200 |
return;
|
201 |
} |
202 |
boolean isProcessing = this.processing || this.entitySelector.isProcessing(); |
203 |
|
204 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
205 |
VCSGisWorkspace workspace = getWorkspace(); |
206 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
207 |
|
208 |
boolean exists = workspace!=null && |
209 |
workspace.existsInWorkspace(selectedTable); |
210 |
|
211 |
this.chkOverwriteExistingTables.setEnabled(!isProcessing && exists);
|
212 |
|
213 |
boolean canCheckout = !isProcessing &&
|
214 |
workspace!=null &&
|
215 |
selectedTable != null &&
|
216 |
(!exists || this.chkOverwriteExistingTables.isSelected());
|
217 |
|
218 |
this.rdbDontAddToProject.setEnabled(canCheckout);
|
219 |
this.rdbAddTableToProject.setEnabled(canCheckout);
|
220 |
if (canCheckout) {
|
221 |
if (isLayer(selectedTable)) {
|
222 |
this.rdbAddLayerToView.setEnabled(canCheckout);
|
223 |
this.cboView.setEnabled(canCheckout);
|
224 |
} else {
|
225 |
this.rdbAddLayerToView.setEnabled(false); |
226 |
this.cboView.setEnabled(false); |
227 |
} |
228 |
} else {
|
229 |
this.rdbAddLayerToView.setEnabled(false); |
230 |
this.cboView.setEnabled(false); |
231 |
} |
232 |
|
233 |
this.workspacePicker.setEnabled(!isProcessing);
|
234 |
this.entitySelector.setEnabled(!isProcessing && workspace!=null ); |
235 |
|
236 |
this.pbStatus.setVisible(isProcessing);
|
237 |
this.lblStatusTitle.setVisible(isProcessing);
|
238 |
|
239 |
if (dialog != null) { |
240 |
LabeledValue selectedView = (LabeledValue) cboView.getSelectedItem(); |
241 |
boolean dontAddNothing = rdbDontAddToProject.isSelected();
|
242 |
boolean addLayerToView = rdbAddLayerToView.isSelected() && selectedView != null && selectedView.getValue() != null; |
243 |
boolean addTableToProject = rdbAddTableToProject.isSelected();
|
244 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
|
245 |
canCheckout && (dontAddNothing || (addLayerToView || addTableToProject)) |
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 |
|
262 |
if (workspace != null && entities != null && entities.size()>0) { |
263 |
List<String> entityCodes = new ArrayList<>(); |
264 |
for (VCSGisEntity entity : entities) {
|
265 |
entityCodes.add(entity.getEntityCode()); |
266 |
} |
267 |
|
268 |
try {
|
269 |
processing = true;
|
270 |
doUpdateComponents(); |
271 |
|
272 |
showMessage(i18n.getTranslation("_Processing"), null); |
273 |
int res = ERR_OK;
|
274 |
if(chkOverwriteExistingTables.isSelected()){
|
275 |
res = workspace.removeEntities(entityCodes); |
276 |
} |
277 |
if(res == ERR_OK){
|
278 |
res = workspace.checkout( |
279 |
entityCodes, |
280 |
null
|
281 |
); |
282 |
for (VCSGisEntity entity : entities) {
|
283 |
VCSGisEntity lEntity = workspace.getEntity(entity.getEntityCode()); |
284 |
workspace.getExplorer().setCustomResources(entity.getEntityName(), lEntity.getResources()); |
285 |
} |
286 |
postCheckout(res, entities); |
287 |
} |
288 |
showMessage(i18n.getTranslation("_Ended_process"), null); |
289 |
return res;
|
290 |
} finally {
|
291 |
processing = false;
|
292 |
doUpdateComponents(); |
293 |
} |
294 |
} |
295 |
return ERR_CANT_CHECKOUT;
|
296 |
} |
297 |
|
298 |
private void postCheckout(int checkoutStatus, List<VCSGisEntity> entities) { |
299 |
if( notInSwingThreadInvokeLater(() -> {postCheckout(checkoutStatus,entities);}) ) {
|
300 |
return;
|
301 |
} |
302 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
303 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
304 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
305 |
|
306 |
if (checkoutStatus == ERR_OK) {
|
307 |
//Nos vamos guardando los stores para, después, refrescar solo una vez cada documento al que pertenecen
|
308 |
Set stores = new HashSet(); |
309 |
for (VCSGisEntity entity : entities) {
|
310 |
FeatureStore store = workspace.getFeatureStore(entity.getEntityName()); |
311 |
stores.add(store); |
312 |
if (!rdbDontAddToProject.isSelected()) {
|
313 |
if (rdbAddLayerToView.isSelected()) {
|
314 |
if (isLayer(entity)) {
|
315 |
String layerName = store.getName();
|
316 |
services.addLayerToView(store, (LabeledValue) cboView.getSelectedItem(), layerName); |
317 |
} |
318 |
} |
319 |
if (rdbAddTableToProject.isSelected()) {
|
320 |
services.addTableToProject(workspace, store); |
321 |
} |
322 |
} |
323 |
} |
324 |
if (chkOverwriteExistingTables.isSelected() && !stores.isEmpty()) {
|
325 |
refreshDocument(stores); |
326 |
} |
327 |
for (Object store : stores) { |
328 |
DisposeUtils.dispose(store); |
329 |
} |
330 |
|
331 |
this.setVisibleStatus(false); |
332 |
} else {
|
333 |
this.setVisibleStatus(true); |
334 |
} |
335 |
this.doUpdateComponents();
|
336 |
} |
337 |
|
338 |
private void showMessage(String message, String toolTipText) { |
339 |
if( notInSwingThreadInvokeLater(() -> {showMessage(message, toolTipText);}) ) {
|
340 |
return;
|
341 |
} |
342 |
this.lblStatusMessages.setText(message);
|
343 |
this.lblStatusMessages.setToolTipText(toolTipText);
|
344 |
} |
345 |
|
346 |
private void translate() { |
347 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
348 |
swingManager.translate(this.lblWorkspace);
|
349 |
swingManager.translate(this.lblDataModel);
|
350 |
swingManager.translate(this.rdbDontAddToProject);
|
351 |
swingManager.translate(this.rdbAddLayerToView);
|
352 |
swingManager.translate(this.rdbAddTableToProject);
|
353 |
swingManager.translate(this.chkOverwriteExistingTables);
|
354 |
} |
355 |
|
356 |
@Override
|
357 |
public boolean isProcessing() { |
358 |
return processing;
|
359 |
} |
360 |
|
361 |
public static void selfRegister() { |
362 |
String[][] iconsInfo = new String[][]{ |
363 |
new String[]{ICON_PROVIDER_NAME, ICON_GROUP_NAME, "vcsgis-checkout"},}; |
364 |
registerIcons(iconsInfo); |
365 |
} |
366 |
|
367 |
private void doChangeTable() { |
368 |
VCSGisWorkspace workspace = getWorkspace(); |
369 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
370 |
|
371 |
boolean isLayer = isLayer(selectedTable);
|
372 |
this.rdbAddLayerToView.setSelected(isLayer);
|
373 |
this.rdbAddTableToProject.setSelected(!isLayer);
|
374 |
|
375 |
this.chkOverwriteExistingTables.setSelected(workspace!=null && workspace.existsInWorkspace(selectedTable)); |
376 |
|
377 |
doUpdateComponents(); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public String getDataModel() { |
382 |
return (String)this.cboDataModels.getSelectedItem(); |
383 |
} |
384 |
|
385 |
} |