gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.swing / org.gvsig.online.swing.impl / src / main / java / org / gvsig / online / swing / impl / OnlineEntitySelectorControllerJComboBox.java @ 9518
History | View | Annotate | Download (14.8 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.online.swing.impl; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
import java.awt.event.ItemEvent; |
28 |
import java.awt.event.ItemListener; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Collection; |
31 |
import java.util.Collections; |
32 |
import java.util.List; |
33 |
import java.util.Objects; |
34 |
import java.util.function.Function; |
35 |
import java.util.function.Predicate; |
36 |
import javax.swing.DefaultComboBoxModel; |
37 |
import javax.swing.JComboBox; |
38 |
import javax.swing.JLabel; |
39 |
import javax.swing.JList; |
40 |
import javax.swing.JOptionPane; |
41 |
import javax.swing.ListCellRenderer; |
42 |
import javax.swing.ListModel; |
43 |
import javax.swing.event.ChangeListener; |
44 |
import org.apache.commons.lang3.StringUtils; |
45 |
import static org.gvsig.online.lib.api.OnlineManager.STATE_REMOTE_NEW; |
46 |
import org.gvsig.online.lib.api.workingcopy.OnlineEntity; |
47 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy; |
48 |
import org.gvsig.online.swing.api.OnlineEntitySelectorController; |
49 |
import static org.gvsig.online.swing.impl.OnlineSwingCommons.notInSwingThreadInvokeLater; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.dispose.DisposeUtils; |
52 |
import org.gvsig.tools.dispose.impl.AbstractDisposable; |
53 |
import org.gvsig.tools.exception.BaseException; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
56 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
58 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
59 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
60 |
import org.gvsig.tools.util.LabeledValue; |
61 |
import org.gvsig.tools.util.LabeledValueImpl; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
/**
|
66 |
*
|
67 |
* @author gvSIG Team
|
68 |
*/
|
69 |
public class OnlineEntitySelectorControllerJComboBox extends AbstractDisposable implements OnlineEntitySelectorController { |
70 |
|
71 |
private static final Logger LOGGER = LoggerFactory.getLogger(OnlineEntitySelectorControllerJComboBox.class); |
72 |
|
73 |
private final JComboBox cboTables; |
74 |
private final ActionListenerSupport actionListeners; |
75 |
private final ChangeListenerHelper changeListeners; |
76 |
private OnlineWorkingcopy workingcopy;
|
77 |
private boolean processing; |
78 |
private boolean enabled; |
79 |
private ArrayList<OnlineEntity> entities; |
80 |
private Predicate<OnlineEntity> viewFilter; |
81 |
private Predicate<OnlineEntity> filter; |
82 |
private Thread lastThread; |
83 |
private Function<OnlineEntity, String> labelEntityFormatter; |
84 |
|
85 |
public OnlineEntitySelectorControllerJComboBox(JComboBox cboTables) { |
86 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
87 |
|
88 |
viewFilter = ALL_ENTITIES; |
89 |
filter = ALL_ENTITIES; |
90 |
|
91 |
this.cboTables = cboTables;
|
92 |
this.processing = false; |
93 |
this.actionListeners = toolsSwingManager.createActionListenerSupport();
|
94 |
this.changeListeners = toolsSwingManager.createChangeListenerHelper();
|
95 |
|
96 |
this.initComponents();
|
97 |
} |
98 |
|
99 |
private void initComponents() { |
100 |
this.cboTables.addItemListener(new ItemListener() { |
101 |
boolean alreadyInEvent = false; |
102 |
|
103 |
@Override
|
104 |
public void itemStateChanged(ItemEvent e) { |
105 |
if( e.getStateChange()==ItemEvent.SELECTED ) { |
106 |
try {
|
107 |
this.alreadyInEvent = true; |
108 |
fireChangeEvent(); |
109 |
fireActionEvent(new ActionEvent(this, ACTION_SELECT_ID, ACTION_SELECT)); |
110 |
} finally {
|
111 |
this.alreadyInEvent = false; |
112 |
} |
113 |
} |
114 |
} |
115 |
}); |
116 |
|
117 |
this.cboTables.setRenderer(new ListCellRenderer() { |
118 |
JLabel label = new JLabel(); |
119 |
@Override
|
120 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
121 |
if( value instanceof OnlineEntity && workingcopy!=null ) { |
122 |
this.label.setText(workingcopy.formatEntityLabel((OnlineEntity)value));
|
123 |
} else {
|
124 |
this.label.setText(Objects.toString(value, null)); |
125 |
} |
126 |
return this.label; |
127 |
} |
128 |
}); |
129 |
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public boolean isProcessing() { |
134 |
return this.processing; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public OnlineEntity getSelectedEntity() {
|
139 |
if (this.workingcopy == null) { |
140 |
return null; |
141 |
} |
142 |
LabeledValue selected = (LabeledValue) cboTables.getSelectedItem(); |
143 |
if (selected == null) { |
144 |
return null; |
145 |
} |
146 |
OnlineEntity entity = (OnlineEntity) selected.getValue(); |
147 |
if (!this.filter.test(entity)) { |
148 |
return null; |
149 |
} |
150 |
return entity;
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void setSelectedEntity(OnlineEntity entity) { |
155 |
if (this.workingcopy == null) { |
156 |
return;
|
157 |
} |
158 |
ListModel model = cboTables.getModel();
|
159 |
for (int i = 0; i < model.getSize(); i++) { |
160 |
OnlineEntity value = ((LabeledValue<OnlineEntity>) model.getElementAt(i)).getValue(); |
161 |
if (StringUtils.equals(entity.getEntityCode(), value.getEntityCode())) {
|
162 |
cboTables.setSelectedIndex(i); |
163 |
return;
|
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public List<OnlineEntity> getCheckedEntities() { |
170 |
return Collections.EMPTY_LIST; |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void setWorkspace(OnlineWorkingcopy workspace) { |
175 |
DisposeUtils.disposeQuietly(workspace); |
176 |
this.workingcopy = workspace;
|
177 |
DisposeUtils.bind(this.workingcopy);
|
178 |
reloadEntities(); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public OnlineWorkingcopy getWorkspace() {
|
183 |
return this.workingcopy; |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void reloadEntities() { |
188 |
DefaultComboBoxModel<LabeledValue<OnlineEntity>> model = new DefaultComboBoxModel<>(); |
189 |
this.cboTables.setModel(model);
|
190 |
if (workingcopy == null) { |
191 |
return;
|
192 |
} |
193 |
Thread task = new Thread(() -> { |
194 |
reloadTables(this.workingcopy);
|
195 |
}, "VCSGisEntitySelectorComboReloadEntities");
|
196 |
task.start(); |
197 |
this.lastThread = task;
|
198 |
} |
199 |
|
200 |
private boolean isCancelled(){ |
201 |
return Thread.currentThread() != this.lastThread; |
202 |
} |
203 |
|
204 |
private void reloadTables(OnlineWorkingcopy workspace) { |
205 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
206 |
try {
|
207 |
fireActionEvent(new ActionEvent(this, ACTION_BEGIN_PROCESSING_ID, ACTION_BEGIN_PROCESSING)); |
208 |
this.processing = true; |
209 |
this.doUpdateComponents();
|
210 |
// workspace.updateEntitiesFromRepository();
|
211 |
|
212 |
//FIXME: XXXXXXXXXXXXXXXXx
|
213 |
// List<OnlineEntity> repoEntities = workspace.getRepositoryEntities();
|
214 |
|
215 |
List<LabeledValue<OnlineEntity>> theEntities = new ArrayList<>(); |
216 |
// for (OnlineEntity rentity : repoEntities) {
|
217 |
// if(isCancelled()){
|
218 |
// return;
|
219 |
// }
|
220 |
// OnlineEntity lentity = workspace.getWorkspaceEntityByCode(rentity.getEntityCode());
|
221 |
// LabeledValue entry;
|
222 |
// String s = OnlineSwingCommons.getHTMLColorTag(
|
223 |
// lentity == null ? STATE_REMOTE_NEW : lentity.getState(),
|
224 |
// this.getLabelOfEntity(rentity)
|
225 |
// );
|
226 |
// if (lentity != null && this.viewFilter.test(lentity)) {
|
227 |
// entry = new LabeledValueImpl(s, lentity);
|
228 |
// theEntities.add(entry);
|
229 |
// } else {
|
230 |
// if (this.viewFilter.test(rentity)) {
|
231 |
// entry = new LabeledValueImpl(s, rentity);
|
232 |
// theEntities.add(entry);
|
233 |
// }
|
234 |
// }
|
235 |
// }
|
236 |
|
237 |
List<OnlineEntity> localEntities = workspace.getWorkspaceEntities();
|
238 |
for (OnlineEntity localEntity : localEntities) {
|
239 |
if(isCancelled()){
|
240 |
return;
|
241 |
} |
242 |
// if (StringUtils.isBlank(localEntity.getRepositoryRevisionCode())) { //Es entidad local nueva
|
243 |
// if (!this.viewFilter.test(localEntity)) {
|
244 |
// continue;
|
245 |
// }
|
246 |
// LabeledValue entry;
|
247 |
// String s = OnlineSwingCommons.getHTMLColorTag(
|
248 |
// localEntity.getState(),
|
249 |
// this.getLabelOfEntity(localEntity)
|
250 |
// );
|
251 |
// entry = new LabeledValueImpl(s, localEntity);
|
252 |
// theEntities.add(entry);
|
253 |
// }
|
254 |
} |
255 |
|
256 |
Collections.sort(theEntities, (LabeledValue<OnlineEntity> o1, LabeledValue<OnlineEntity> o2) -> StringUtils.compareIgnoreCase(
|
257 |
o1.getValue().getLabelOrName(), |
258 |
o2.getValue().getLabelOrName() |
259 |
)); |
260 |
if (!isCancelled()) {
|
261 |
postReloadTables(theEntities); |
262 |
} |
263 |
|
264 |
} catch (Exception e) { |
265 |
LOGGER.warn("_Cant_retrieve_entities_from_repository", e);
|
266 |
if (!isCancelled()) {
|
267 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
268 |
dialogs.messageDialog( |
269 |
i18n.getTranslation("_Cant_retrieve_entities_from_repository") + "\n" + e.getMessage(), |
270 |
i18n.getTranslation("_VCS_Checkout"),
|
271 |
JOptionPane.WARNING_MESSAGE
|
272 |
); |
273 |
} |
274 |
} finally {
|
275 |
if (!isCancelled()) {
|
276 |
this.processing = false; |
277 |
this.doUpdateComponents();
|
278 |
fireActionEvent(new ActionEvent(this, ACTION_END_PROCESSING_ID, ACTION_END_PROCESSING)); |
279 |
} |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
private void postReloadTables(List<LabeledValue<OnlineEntity>> entities) { |
285 |
if (notInSwingThreadInvokeLater(() -> {postReloadTables(entities);})) {
|
286 |
return;
|
287 |
} |
288 |
this.entities = new ArrayList<>(); |
289 |
DefaultComboBoxModel<LabeledValue<OnlineEntity>> model = new DefaultComboBoxModel<>(); |
290 |
model.addElement(new LabeledValueImpl<>("", null)); |
291 |
for (LabeledValue<OnlineEntity> entity : entities) {
|
292 |
if (entity != null) { |
293 |
this.entities.add(entity.getValue());
|
294 |
model.addElement(entity); |
295 |
} |
296 |
} |
297 |
this.cboTables.setModel(model);
|
298 |
this.processing = false; |
299 |
fireActionEvent(new ActionEvent(this, ACTION_RELOAD_ID, ACTION_RELOAD)); |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public Collection<OnlineEntity> getEntities() { |
304 |
return this.entities; |
305 |
} |
306 |
|
307 |
private void doUpdateComponents() { |
308 |
if (notInSwingThreadInvokeLater(this::doUpdateComponents)) { |
309 |
return;
|
310 |
} |
311 |
this.cboTables.setEnabled(enabled && !processing);
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public void setEnabled(boolean enabled) { |
316 |
this.enabled = enabled;
|
317 |
doUpdateComponents(); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public boolean isEnabled() { |
322 |
return enabled;
|
323 |
} |
324 |
|
325 |
@Override
|
326 |
public void setChecksEnabled(boolean enabled) { |
327 |
} |
328 |
|
329 |
@Override
|
330 |
public boolean isChecksEnabled() { |
331 |
return false; |
332 |
} |
333 |
|
334 |
@Override
|
335 |
public void addActionListener(ActionListener al) { |
336 |
this.actionListeners.addActionListener(al);
|
337 |
} |
338 |
|
339 |
@Override
|
340 |
public ActionListener[] getActionListeners() { |
341 |
return this.actionListeners.getActionListeners(); |
342 |
} |
343 |
|
344 |
@Override
|
345 |
public void removeActionListener(ActionListener al) { |
346 |
this.actionListeners.removeActionListener(al);
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public void removeAllActionListener() { |
351 |
this.actionListeners.removeAllActionListener();
|
352 |
} |
353 |
|
354 |
@Override
|
355 |
public void fireActionEvent(ActionEvent ae) { |
356 |
this.actionListeners.fireActionEvent(ae);
|
357 |
} |
358 |
|
359 |
private void fireChangeEvent() { |
360 |
this.changeListeners.fireEvent();
|
361 |
} |
362 |
|
363 |
@Override
|
364 |
public boolean hasActionListeners() { |
365 |
return this.actionListeners.hasActionListeners(); |
366 |
} |
367 |
|
368 |
@Override
|
369 |
public void addChangeListener(ChangeListener cl) { |
370 |
this.changeListeners.addChangeListener(cl);
|
371 |
} |
372 |
|
373 |
@Override
|
374 |
public ChangeListener[] getChangeListeners() { |
375 |
return this.changeListeners.getChangeListeners(); |
376 |
} |
377 |
|
378 |
@Override
|
379 |
public void removeChangeListener(ChangeListener cl) { |
380 |
this.changeListeners.removeChangeListener(cl);
|
381 |
} |
382 |
|
383 |
@Override
|
384 |
public void removeAllChangeListener() { |
385 |
this.changeListeners.removeAllChangeListener();
|
386 |
} |
387 |
|
388 |
@Override
|
389 |
public boolean hasChangeListeners() { |
390 |
return this.changeListeners.hasChangeListeners(); |
391 |
} |
392 |
|
393 |
@Override
|
394 |
public void setViewFilter(Predicate<OnlineEntity> viewFilter) { |
395 |
this.viewFilter = viewFilter;
|
396 |
reloadEntities(); |
397 |
} |
398 |
|
399 |
@Override
|
400 |
public void setFilter(Predicate<OnlineEntity> filter) { |
401 |
this.filter = filter;
|
402 |
reloadEntities(); |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public void check(OnlineEntity entity) { |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void unCheck(OnlineEntity entity) { |
411 |
} |
412 |
|
413 |
@Override
|
414 |
public void clearChecks() { |
415 |
} |
416 |
|
417 |
@Override
|
418 |
public void setCheck(Collection<String> entityCodes, boolean check) { |
419 |
} |
420 |
|
421 |
@Override
|
422 |
public void checkAll() { |
423 |
} |
424 |
|
425 |
@Override
|
426 |
public void clear() { |
427 |
this.setWorkspace(null); |
428 |
} |
429 |
|
430 |
@Override
|
431 |
protected void doDispose() throws BaseException { |
432 |
DisposeUtils.disposeQuietly(this.workingcopy);
|
433 |
} |
434 |
|
435 |
private String getLabelOfEntity(OnlineEntity entity) { |
436 |
if( this.labelEntityFormatter==null ) { |
437 |
return entity.getLabelOrName();
|
438 |
} |
439 |
return this.labelEntityFormatter.apply(entity); |
440 |
} |
441 |
|
442 |
public void setLabelEntityFormatter(Function<OnlineEntity,String> formatter) { |
443 |
this.labelEntityFormatter = formatter;
|
444 |
} |
445 |
|
446 |
@Override
|
447 |
public String getCategory(OnlineEntity entity) { |
448 |
String category = entity.getFirstCategory();
|
449 |
return category;
|
450 |
} |
451 |
|
452 |
@Override
|
453 |
public void collapseAll() { |
454 |
} |
455 |
|
456 |
@Override
|
457 |
public void expandAll() { |
458 |
} |
459 |
} |