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 / VCSGisEntitySelectorControllerJTree.java @ 3519
History | View | Annotate | Download (25.4 KB)
1 | 3234 | jjdelcerro | /*
|
---|---|---|---|
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; |
||
23 | |||
24 | 3379 | jjdelcerro | import java.awt.Component; |
25 | 3234 | jjdelcerro | import java.awt.Cursor; |
26 | 3379 | jjdelcerro | import java.awt.FlowLayout; |
27 | 3234 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | import java.awt.event.ActionListener; |
||
29 | import java.util.ArrayList; |
||
30 | 3519 | fdiaz | import java.util.Collection; |
31 | 3389 | fdiaz | import java.util.Collections; |
32 | 3379 | jjdelcerro | import java.util.HashMap; |
33 | 3365 | fdiaz | import java.util.HashSet; |
34 | 3234 | jjdelcerro | import java.util.List; |
35 | 3379 | jjdelcerro | import java.util.Map; |
36 | 3365 | fdiaz | import java.util.Set; |
37 | 3379 | jjdelcerro | import java.util.TreeMap; |
38 | 3319 | fdiaz | import java.util.function.Predicate; |
39 | 3379 | jjdelcerro | import javax.swing.AbstractCellEditor; |
40 | 3234 | jjdelcerro | import javax.swing.JButton; |
41 | 3379 | jjdelcerro | import javax.swing.JCheckBox; |
42 | import javax.swing.JLabel; |
||
43 | 3234 | jjdelcerro | import javax.swing.JOptionPane; |
44 | 3379 | jjdelcerro | import javax.swing.JPanel; |
45 | 3234 | jjdelcerro | import javax.swing.JTextField; |
46 | 3379 | jjdelcerro | import javax.swing.JTree; |
47 | 3234 | jjdelcerro | import javax.swing.event.ChangeListener; |
48 | 3379 | jjdelcerro | import javax.swing.event.TreeSelectionEvent; |
49 | import javax.swing.event.TreeSelectionListener; |
||
50 | import javax.swing.tree.DefaultMutableTreeNode; |
||
51 | import javax.swing.tree.DefaultTreeCellRenderer; |
||
52 | import javax.swing.tree.DefaultTreeModel; |
||
53 | import javax.swing.tree.TreeCellEditor; |
||
54 | import javax.swing.tree.TreePath; |
||
55 | import static javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION; |
||
56 | 3234 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
57 | import org.gvsig.tools.ToolsLocator; |
||
58 | import org.gvsig.tools.i18n.I18nManager; |
||
59 | import org.gvsig.tools.swing.api.ActionListenerSupport; |
||
60 | import org.gvsig.tools.swing.api.ChangeListenerHelper; |
||
61 | 3379 | jjdelcerro | import org.gvsig.tools.swing.api.FilteredTreeController; |
62 | 3234 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
63 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
64 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
65 | 3389 | fdiaz | import org.gvsig.tools.util.CompareUtils; |
66 | 3234 | jjdelcerro | import org.gvsig.tools.util.LabeledValue; |
67 | import org.gvsig.tools.util.LabeledValueImpl; |
||
68 | import org.gvsig.vcsgis.lib.VCSGisEntity; |
||
69 | import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_REPOSITORY_NEW; |
||
70 | import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
||
71 | import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity; |
||
72 | 3448 | fdiaz | import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.ACTION_CHECK; |
73 | import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.ACTION_CHECK_ID; |
||
74 | 3383 | jjdelcerro | import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.ACTION_SELECT; |
75 | 3234 | jjdelcerro | import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater; |
76 | import org.slf4j.Logger; |
||
77 | import org.slf4j.LoggerFactory; |
||
78 | |||
79 | /**
|
||
80 | *
|
||
81 | * @author gvSIG Team
|
||
82 | */
|
||
83 | 3379 | jjdelcerro | public class VCSGisEntitySelectorControllerJTree |
84 | implements
|
||
85 | VCSGisEntitySelectorController { |
||
86 | |||
87 | private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisEntitySelectorControllerJTree.class); |
||
88 | |||
89 | private final JTextField txtFilter; |
||
90 | private final JTree treeEntities; |
||
91 | private Set<String> checkedEntityCodes; |
||
92 | private Map<String, VCSGisEntity> entities; |
||
93 | private final JButton btnEntities; |
||
94 | private FilteredTreeController filteredTree;
|
||
95 | private ActionListenerSupport actionListeners;
|
||
96 | private ChangeListenerHelper changeListeners;
|
||
97 | private VCSGisWorkspace workspace;
|
||
98 | private boolean processing; |
||
99 | private boolean enabled; |
||
100 | private Predicate<VCSGisEntity> viewFilter; |
||
101 | private Predicate<VCSGisEntity> filter; |
||
102 | private boolean checksEnabled; |
||
103 | 3516 | fdiaz | private JButton btnCheckAll; |
104 | private JButton btnUnCheckAll; |
||
105 | 3379 | jjdelcerro | |
106 | private class EntityCellEditor extends AbstractCellEditor implements TreeCellEditor { |
||
107 | |||
108 | 3383 | jjdelcerro | private final JCheckBox check; |
109 | private final JPanel panel; |
||
110 | private final JLabel label; |
||
111 | private Object currentUserObject; |
||
112 | 3381 | fdiaz | |
113 | 3383 | jjdelcerro | public EntityCellEditor() {
|
114 | this.check = new JCheckBox(); |
||
115 | this.label = new JLabel(); |
||
116 | this.panel = new JPanel(); |
||
117 | this.panel.setOpaque(false); |
||
118 | this.check.setOpaque(false); |
||
119 | this.panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 2,1)); |
||
120 | this.panel.add(this.check); |
||
121 | this.panel.add(this.label); |
||
122 | this.currentUserObject = null; |
||
123 | this.check.addActionListener(new ActionListener() { |
||
124 | @Override
|
||
125 | public void actionPerformed(ActionEvent e) { |
||
126 | if( currentUserObject==null ) { |
||
127 | 3448 | fdiaz | if( check.isSelected() ) {
|
128 | checkedEntityCodes.addAll(getAllEntityCodesFromCategory(null));
|
||
129 | } else {
|
||
130 | checkedEntityCodes.removeAll(getAllEntityCodesFromCategory(null));
|
||
131 | } |
||
132 | postCheck(); |
||
133 | 3383 | jjdelcerro | return;
|
134 | } |
||
135 | 3448 | fdiaz | if( !(currentUserObject instanceof LabeledValue) ) { //Category |
136 | String category = (String)currentUserObject; |
||
137 | if( check.isSelected() ) {
|
||
138 | checkedEntityCodes.addAll(getAllEntityCodesFromCategory(category)); |
||
139 | } else {
|
||
140 | checkedEntityCodes.removeAll(getAllEntityCodesFromCategory(category)); |
||
141 | } |
||
142 | postCheck(); |
||
143 | 3383 | jjdelcerro | return;
|
144 | } |
||
145 | VCSGisEntity entity = ((LabeledValue<VCSGisEntity>) currentUserObject).getValue(); |
||
146 | if( entity == null ) { |
||
147 | return;
|
||
148 | } |
||
149 | if( check.isSelected() ) {
|
||
150 | checkedEntityCodes.add(entity.getEntityCode()); |
||
151 | } else {
|
||
152 | checkedEntityCodes.remove(entity.getEntityCode()); |
||
153 | } |
||
154 | 3448 | fdiaz | postCheck(); |
155 | 3383 | jjdelcerro | } |
156 | }); |
||
157 | } |
||
158 | 3448 | fdiaz | |
159 | private void postCheck() { |
||
160 | this.stopCellEditing();
|
||
161 | treeEntities.repaint(); |
||
162 | fireActionEvent(new ActionEvent(this, ACTION_CHECK_ID, ACTION_CHECK)); |
||
163 | } |
||
164 | 3381 | fdiaz | |
165 | 3379 | jjdelcerro | @Override
|
166 | public Object getCellEditorValue() { |
||
167 | 3383 | jjdelcerro | return currentUserObject;
|
168 | 3379 | jjdelcerro | } |
169 | |||
170 | @Override
|
||
171 | public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) { |
||
172 | 3383 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
173 | if( checksEnabled ) {
|
||
174 | this.check.setVisible(true); |
||
175 | this.check.setEnabled(false); |
||
176 | this.check.setSelected(false); |
||
177 | if(value instanceof DefaultMutableTreeNode){ |
||
178 | currentUserObject = ((DefaultMutableTreeNode) value).getUserObject();
|
||
179 | if( currentUserObject instanceof LabeledValue ) { |
||
180 | VCSGisEntity entity = ((LabeledValue<VCSGisEntity>) currentUserObject).getValue(); |
||
181 | if( entity != null ) { |
||
182 | this.check.setEnabled(true); |
||
183 | this.check.setSelected(checkedEntityCodes.contains(entity.getEntityCode()));
|
||
184 | this.label.setText(((LabeledValue<VCSGisEntity>) currentUserObject).getLabel());
|
||
185 | } |
||
186 | 3389 | fdiaz | } else if( currentUserObject instanceof String || currentUserObject == null) { |
187 | 3448 | fdiaz | this.check.setEnabled(true); |
188 | this.check.setSelected(areSelectedAllEntitiesFromCategory((String)currentUserObject)); |
||
189 | 3383 | jjdelcerro | if(StringUtils.isBlank((String)currentUserObject)){ |
190 | this.label.setText(i18n.getTranslation("_Others")); |
||
191 | } else {
|
||
192 | this.label.setText(i18n.getTranslation((String)currentUserObject)); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } else {
|
||
197 | this.check.setVisible(false); |
||
198 | } |
||
199 | return this.panel; |
||
200 | 3379 | jjdelcerro | } |
201 | 3381 | fdiaz | |
202 | 3379 | jjdelcerro | } |
203 | 3234 | jjdelcerro | |
204 | 3379 | jjdelcerro | private class EntityCellRenderer extends DefaultTreeCellRenderer { |
205 | |||
206 | private final JCheckBox check; |
||
207 | private final JPanel panel; |
||
208 | private final JLabel label; |
||
209 | |||
210 | public EntityCellRenderer() {
|
||
211 | this.check = new JCheckBox(); |
||
212 | this.label = new JLabel(); |
||
213 | this.panel = new JPanel(); |
||
214 | this.panel.setOpaque(false); |
||
215 | this.check.setOpaque(false); |
||
216 | this.panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 2,1)); |
||
217 | this.panel.add(this.check); |
||
218 | this.panel.add(this.label); |
||
219 | 3383 | jjdelcerro | this.setLeafIcon(null); |
220 | 3379 | jjdelcerro | } |
221 | |||
222 | @Override
|
||
223 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
||
224 | JLabel c = (JLabel) super.getTreeCellRendererComponent(tree, value, leaf, expanded, leaf, row, hasFocus); |
||
225 | this.label.setText(c.getText());
|
||
226 | this.label.setForeground(c.getForeground());
|
||
227 | this.label.setBackground(c.getBackground());
|
||
228 | if( checksEnabled ) {
|
||
229 | this.check.setVisible(true); |
||
230 | this.check.setForeground(c.getForeground());
|
||
231 | this.check.setBackground(c.getBackground());
|
||
232 | this.check.setEnabled(false); |
||
233 | this.check.setSelected(false); |
||
234 | 3448 | fdiaz | |
235 | 3381 | fdiaz | if(value instanceof DefaultMutableTreeNode){ |
236 | Object userObject = ((DefaultMutableTreeNode) value).getUserObject(); |
||
237 | if( userObject instanceof LabeledValue ) { |
||
238 | VCSGisEntity entity = ((LabeledValue<VCSGisEntity>) userObject).getValue(); |
||
239 | if( entity != null ) { |
||
240 | this.check.setEnabled(true); |
||
241 | this.check.setSelected(checkedEntityCodes.contains(entity.getEntityCode()));
|
||
242 | } |
||
243 | 3389 | fdiaz | } else if( userObject instanceof String || userObject == null) { |
244 | 3448 | fdiaz | this.check.setEnabled(true); |
245 | this.check.setSelected(areSelectedAllEntitiesFromCategory((String)userObject)); |
||
246 | 3381 | fdiaz | if(StringUtils.isBlank((String)userObject)){ |
247 | 3383 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
248 | 3381 | fdiaz | this.label.setText(i18n.getTranslation("_Others")); |
249 | 3383 | jjdelcerro | } else {
|
250 | this.label.setText((String)userObject); |
||
251 | 3381 | fdiaz | } |
252 | 3379 | jjdelcerro | } |
253 | } |
||
254 | } else {
|
||
255 | this.check.setVisible(false); |
||
256 | } |
||
257 | return this.panel; |
||
258 | } |
||
259 | |||
260 | 3381 | fdiaz | protected JCheckBox getCheckBox() { |
261 | return this.check; |
||
262 | } |
||
263 | |||
264 | 3234 | jjdelcerro | } |
265 | |||
266 | 3379 | jjdelcerro | public VCSGisEntitySelectorControllerJTree(JTree treeEntities) { |
267 | 3516 | fdiaz | this(treeEntities, null, null, null, null); |
268 | 3379 | jjdelcerro | } |
269 | |||
270 | 3516 | fdiaz | public VCSGisEntitySelectorControllerJTree(JTree treeEntities, JTextField txtFilter, JButton btnTable, JButton btnCheckAll, JButton btnUnCheckAll) { |
271 | 3319 | fdiaz | viewFilter = ALL_ENTITIES; |
272 | filter = ALL_ENTITIES; |
||
273 | 3379 | jjdelcerro | |
274 | this.treeEntities = treeEntities;
|
||
275 | this.checkedEntityCodes = new HashSet<>(); |
||
276 | 3389 | fdiaz | this.entities = new HashMap<>(); |
277 | 3379 | jjdelcerro | if (txtFilter == null) { |
278 | 3234 | jjdelcerro | this.txtFilter = new JTextField(); |
279 | } else {
|
||
280 | this.txtFilter = txtFilter;
|
||
281 | } |
||
282 | 3379 | jjdelcerro | if (btnTable == null) { |
283 | this.btnEntities = new JButton(); |
||
284 | 3234 | jjdelcerro | } else {
|
285 | 3379 | jjdelcerro | this.btnEntities = btnTable;
|
286 | 3234 | jjdelcerro | } |
287 | 3516 | fdiaz | |
288 | if( btnCheckAll==null ) { |
||
289 | this.btnCheckAll = new JButton(); |
||
290 | } else {
|
||
291 | this.btnCheckAll = btnCheckAll;
|
||
292 | } |
||
293 | if( btnUnCheckAll==null ) { |
||
294 | this.btnUnCheckAll = new JButton(); |
||
295 | } else {
|
||
296 | this.btnUnCheckAll = btnCheckAll;
|
||
297 | } |
||
298 | |||
299 | 3234 | jjdelcerro | this.processing = false; |
300 | 3379 | jjdelcerro | |
301 | 3234 | jjdelcerro | this.initComponents();
|
302 | } |
||
303 | 3379 | jjdelcerro | |
304 | 3234 | jjdelcerro | private void initComponents() { |
305 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
306 | 3379 | jjdelcerro | |
307 | 3234 | jjdelcerro | this.actionListeners = toolsSwingManager.createActionListenerSupport();
|
308 | this.changeListeners = toolsSwingManager.createChangeListenerHelper();
|
||
309 | |||
310 | 3379 | jjdelcerro | this.btnEntities.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
311 | |||
312 | this.treeEntities.getSelectionModel().setSelectionMode(SINGLE_TREE_SELECTION);
|
||
313 | this.treeEntities.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { |
||
314 | @Override
|
||
315 | public void valueChanged(TreeSelectionEvent e) { |
||
316 | fireActionEvent(new ActionEvent(this, 0, ACTION_SELECT)); |
||
317 | 3234 | jjdelcerro | } |
318 | }); |
||
319 | 3379 | jjdelcerro | filteredTree = toolsSwingManager.createFilteredTreeController(treeEntities, txtFilter, btnEntities); |
320 | 3383 | jjdelcerro | filteredTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode())); |
321 | 3381 | fdiaz | EntityCellRenderer cellRenderer = new EntityCellRenderer();
|
322 | this.treeEntities.setCellRenderer(cellRenderer);
|
||
323 | this.treeEntities.setCellEditor(new EntityCellEditor()); |
||
324 | 3383 | jjdelcerro | this.treeEntities.setEditable(true); |
325 | 3516 | fdiaz | |
326 | this.btnCheckAll.addActionListener(new ActionListener() { |
||
327 | @Override
|
||
328 | public void actionPerformed(ActionEvent e) { |
||
329 | checkAll(); |
||
330 | } |
||
331 | }); |
||
332 | this.btnUnCheckAll.addActionListener(new ActionListener() { |
||
333 | @Override
|
||
334 | public void actionPerformed(ActionEvent e) { |
||
335 | clearChecks(); |
||
336 | } |
||
337 | }); |
||
338 | |||
339 | 3234 | jjdelcerro | } |
340 | |||
341 | @Override
|
||
342 | public boolean isProcessing() { |
||
343 | return this.processing; |
||
344 | } |
||
345 | 3379 | jjdelcerro | |
346 | 3234 | jjdelcerro | @Override
|
347 | public VCSGisEntity getSelectedEntity() {
|
||
348 | 3379 | jjdelcerro | if (this.workspace == null) { |
349 | 3234 | jjdelcerro | return null; |
350 | } |
||
351 | 3379 | jjdelcerro | TreePath path = treeEntities.getSelectionPath();
|
352 | if (path == null) { |
||
353 | return null; |
||
354 | } |
||
355 | LabeledValue selected = (LabeledValue) path.getLastPathComponent(); |
||
356 | 3234 | jjdelcerro | if (selected == null) { |
357 | return null; |
||
358 | } |
||
359 | 3319 | fdiaz | VCSGisEntity entity = (VCSGisEntity) selected.getValue(); |
360 | 3379 | jjdelcerro | if (!this.filter.test(entity)) { |
361 | 3319 | fdiaz | return null; |
362 | } |
||
363 | return entity;
|
||
364 | 3234 | jjdelcerro | } |
365 | |||
366 | @Override
|
||
367 | 3319 | fdiaz | public List<VCSGisEntity> getCheckedEntities() { |
368 | List<VCSGisEntity> checkedEntities = new ArrayList<>(); |
||
369 | 3379 | jjdelcerro | for (String entityCode : this.checkedEntityCodes) { |
370 | checkedEntities.add(this.entities.get(entityCode));
|
||
371 | 3319 | fdiaz | } |
372 | 3379 | jjdelcerro | return checkedEntities;
|
373 | 3234 | jjdelcerro | } |
374 | 3379 | jjdelcerro | |
375 | 3234 | jjdelcerro | @Override
|
376 | public void setWorkspace(VCSGisWorkspace workspace) { |
||
377 | 3516 | fdiaz | this.checkedEntityCodes.clear();
|
378 | this.entities.clear();
|
||
379 | 3234 | jjdelcerro | if (workspace == null) { |
380 | 3389 | fdiaz | this.workspace = workspace;
|
381 | 3383 | jjdelcerro | filteredTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode())); |
382 | 3234 | jjdelcerro | return;
|
383 | } |
||
384 | 3389 | fdiaz | if (this.processing && this.workspace == workspace) { |
385 | LOGGER.info("Reentrada");
|
||
386 | return;
|
||
387 | } |
||
388 | |||
389 | this.workspace = workspace;
|
||
390 | 3379 | jjdelcerro | Thread task = new Thread(() -> { |
391 | reloadEntities(this.workspace);
|
||
392 | }, "VCSGisEntitySelectorReloadEntities");
|
||
393 | 3234 | jjdelcerro | task.start(); |
394 | } |
||
395 | |||
396 | @Override
|
||
397 | public VCSGisWorkspace getWorkspace() {
|
||
398 | return this.workspace; |
||
399 | } |
||
400 | 3379 | jjdelcerro | |
401 | private void reloadEntities(VCSGisWorkspace workspace) { |
||
402 | 3234 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
403 | try {
|
||
404 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_BEGIN_PROCESSING_ID, ACTION_BEGIN_PROCESSING)); |
405 | 3234 | jjdelcerro | this.processing = true; |
406 | this.doUpdateComponents();
|
||
407 | workspace.reloadRepositoryEntities(null);
|
||
408 | 3389 | fdiaz | List<VCSGisEntity> allEntities = workspace.getRepositoryEntities();
|
409 | List<VCSGisWorkspaceEntity> localEntities = workspace.getWorkspaceEntities();
|
||
410 | for (VCSGisWorkspaceEntity localEntity : localEntities) {
|
||
411 | if (StringUtils.isBlank(localEntity.getRepositoryRevisionCode())) {
|
||
412 | allEntities.add(localEntity); |
||
413 | } |
||
414 | } |
||
415 | Collections.sort(allEntities, new CompareUtils.NullSafeComparator<VCSGisEntity>() { |
||
416 | @Override
|
||
417 | public int safeCompare(VCSGisEntity o1, VCSGisEntity o2) { |
||
418 | return StringUtils.compare(o1.getEntityName(), o2.getEntityName(), true); |
||
419 | } |
||
420 | }); |
||
421 | 3379 | jjdelcerro | |
422 | 3389 | fdiaz | |
423 | Map<String, VCSGisEntity> theEntities; |
||
424 | Map<String, DefaultMutableTreeNode> theEntityNodes; |
||
425 | 3379 | jjdelcerro | |
426 | 3389 | fdiaz | theEntityNodes = new TreeMap<>((String o1, String o2) -> StringUtils.compare(o1, o2, false)); |
427 | |||
428 | theEntities = new HashMap<>(); |
||
429 | |||
430 | for (VCSGisEntity entity : allEntities) {
|
||
431 | VCSGisWorkspaceEntity lentity = workspace.getWorkspaceEntityByCode(entity.getEntityCode()); |
||
432 | 3303 | jolivas | LabeledValue entry; |
433 | 3389 | fdiaz | String s;
|
434 | if (StringUtils.isBlank(entity.getRepositoryRevisionCode())) { //Es entidad local nueva |
||
435 | s = VCSGisSwingCommons.getHTMLColorTag( |
||
436 | ((VCSGisWorkspaceEntity)entity).getState(), |
||
437 | entity.getEntityName() |
||
438 | ); |
||
439 | } else {
|
||
440 | s = VCSGisSwingCommons.getHTMLColorTag( |
||
441 | lentity == null ? STATE_REPOSITORY_NEW : lentity.getState(),
|
||
442 | entity.getEntityName() |
||
443 | ); |
||
444 | } |
||
445 | 3392 | fdiaz | |
446 | if(lentity != null && this.viewFilter.test(lentity)){ |
||
447 | entity = lentity; |
||
448 | } else {
|
||
449 | if(!this.viewFilter.test(entity)){ |
||
450 | entity = null;
|
||
451 | } |
||
452 | 3319 | fdiaz | } |
453 | 3392 | fdiaz | |
454 | if (entity!=null) { |
||
455 | 3389 | fdiaz | theEntities.put(entity.getEntityCode(), entity); |
456 | 3319 | fdiaz | entry = new LabeledValueImpl(s, entity);
|
457 | 3447 | fdiaz | List<String> categories = entity.getCategoriesAsList(); |
458 | if(categories.size()>0){ |
||
459 | for (String cat : categories) { |
||
460 | String category = StringUtils.trimToNull(cat);
|
||
461 | addNodeEntity(theEntityNodes, category, entry); |
||
462 | } |
||
463 | } else {
|
||
464 | String category = StringUtils.trimToNull(entity.getCategory());
|
||
465 | addNodeEntity(theEntityNodes, category, entry); |
||
466 | 3379 | jjdelcerro | } |
467 | 3319 | fdiaz | } |
468 | 3303 | jolivas | } |
469 | 3389 | fdiaz | |
470 | this.entities = theEntities;
|
||
471 | 3379 | jjdelcerro | |
472 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(); |
||
473 | 3389 | fdiaz | for (DefaultMutableTreeNode node : theEntityNodes.values()) { |
474 | 3379 | jjdelcerro | root.add(node); |
475 | } |
||
476 | postReloadEntities(root); |
||
477 | |||
478 | 3234 | jjdelcerro | } catch (Exception e) { |
479 | LOGGER.warn("_Cant_retrieve_entities_from_repository", e);
|
||
480 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
481 | dialogs.messageDialog( |
||
482 | i18n.getTranslation("_Cant_retrieve_entities_from_repository") + "\n" + e.getMessage(), |
||
483 | i18n.getTranslation("_Checkout"),
|
||
484 | JOptionPane.WARNING_MESSAGE
|
||
485 | ); |
||
486 | } finally {
|
||
487 | this.processing = false; |
||
488 | this.doUpdateComponents();
|
||
489 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_END_PROCESSING_ID, ACTION_END_PROCESSING)); |
490 | 3234 | jjdelcerro | } |
491 | 3379 | jjdelcerro | |
492 | 3234 | jjdelcerro | } |
493 | 3379 | jjdelcerro | |
494 | 3519 | fdiaz | @Override
|
495 | public Collection<VCSGisEntity> getEntities() { |
||
496 | return this.entities.values(); |
||
497 | } |
||
498 | |||
499 | 3447 | fdiaz | private void addNodeEntity(Map<String, DefaultMutableTreeNode> theEntityNodes, String category, LabeledValue entry) { |
500 | DefaultMutableTreeNode branch = theEntityNodes.get(category);
|
||
501 | if (branch == null) { |
||
502 | branch = new DefaultMutableTreeNode(category, true); |
||
503 | theEntityNodes.put(category, branch); |
||
504 | } |
||
505 | branch.add(new DefaultMutableTreeNode(entry, false)); |
||
506 | } |
||
507 | |||
508 | 3379 | jjdelcerro | private void postReloadEntities(DefaultMutableTreeNode entities) { |
509 | if (notInSwingThreadInvokeLater(() -> {
|
||
510 | postReloadEntities(entities); |
||
511 | })) { |
||
512 | 3234 | jjdelcerro | return;
|
513 | } |
||
514 | 3383 | jjdelcerro | filteredTree.setModel(new DefaultTreeModel(entities)); |
515 | this.expandAllNodes(treeEntities);
|
||
516 | 3365 | fdiaz | this.processing = false; |
517 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_RELOAD_ID, ACTION_RELOAD)); |
518 | 3234 | jjdelcerro | } |
519 | 3379 | jjdelcerro | |
520 | 3234 | jjdelcerro | private void doUpdateComponents() { |
521 | 3379 | jjdelcerro | if (notInSwingThreadInvokeLater(this::doUpdateComponents)) { |
522 | 3234 | jjdelcerro | return;
|
523 | } |
||
524 | this.txtFilter.setEnabled(enabled && !processing);
|
||
525 | 3379 | jjdelcerro | this.btnEntities.setEnabled(enabled && !processing);
|
526 | this.treeEntities.setEnabled(enabled && !processing);
|
||
527 | 3234 | jjdelcerro | } |
528 | |||
529 | @Override
|
||
530 | public void setEnabled(boolean enabled) { |
||
531 | this.enabled = enabled;
|
||
532 | doUpdateComponents(); |
||
533 | } |
||
534 | |||
535 | 3379 | jjdelcerro | @Override
|
536 | 3234 | jjdelcerro | public boolean isEnabled() { |
537 | return enabled;
|
||
538 | } |
||
539 | |||
540 | @Override
|
||
541 | public void setChecksEnabled(boolean enabled) { |
||
542 | 3379 | jjdelcerro | this.checksEnabled = enabled;
|
543 | 3234 | jjdelcerro | } |
544 | |||
545 | @Override
|
||
546 | public boolean isChecksEnabled() { |
||
547 | 3379 | jjdelcerro | return this.checksEnabled; |
548 | 3234 | jjdelcerro | } |
549 | |||
550 | @Override
|
||
551 | public void addActionListener(ActionListener al) { |
||
552 | this.actionListeners.addActionListener(al);
|
||
553 | } |
||
554 | |||
555 | @Override
|
||
556 | public ActionListener[] getActionListeners() { |
||
557 | return this.actionListeners.getActionListeners(); |
||
558 | } |
||
559 | |||
560 | @Override
|
||
561 | public void removeActionListener(ActionListener al) { |
||
562 | this.actionListeners.removeActionListener(al);
|
||
563 | } |
||
564 | |||
565 | @Override
|
||
566 | public void removeAllActionListener() { |
||
567 | this.actionListeners.removeAllActionListener();
|
||
568 | } |
||
569 | |||
570 | @Override
|
||
571 | public void fireActionEvent(ActionEvent ae) { |
||
572 | this.actionListeners.fireActionEvent(ae);
|
||
573 | } |
||
574 | |||
575 | @Override
|
||
576 | public boolean hasActionListeners() { |
||
577 | return this.actionListeners.hasActionListeners(); |
||
578 | } |
||
579 | |||
580 | @Override
|
||
581 | public void addChangeListener(ChangeListener cl) { |
||
582 | this.changeListeners.addChangeListener(cl);
|
||
583 | } |
||
584 | |||
585 | @Override
|
||
586 | public ChangeListener[] getChangeListeners() { |
||
587 | return this.changeListeners.getChangeListeners(); |
||
588 | } |
||
589 | |||
590 | @Override
|
||
591 | public void removeChangeListener(ChangeListener cl) { |
||
592 | this.changeListeners.removeChangeListener(cl);
|
||
593 | } |
||
594 | |||
595 | @Override
|
||
596 | public void removeAllChangeListener() { |
||
597 | this.changeListeners.removeAllChangeListener();
|
||
598 | } |
||
599 | |||
600 | @Override
|
||
601 | public boolean hasChangeListeners() { |
||
602 | return this.changeListeners.hasChangeListeners(); |
||
603 | } |
||
604 | 3319 | fdiaz | |
605 | 3379 | jjdelcerro | @Override
|
606 | 3319 | fdiaz | public void setViewFilter(Predicate<VCSGisEntity> viewFilter) { |
607 | this.viewFilter = viewFilter;
|
||
608 | } |
||
609 | |||
610 | 3379 | jjdelcerro | @Override
|
611 | 3319 | fdiaz | public void setFilter(Predicate<VCSGisEntity> filter) { |
612 | this.filter = filter;
|
||
613 | } |
||
614 | 3379 | jjdelcerro | |
615 | @Override
|
||
616 | 3365 | fdiaz | public void check(VCSGisEntity entity) { |
617 | 3379 | jjdelcerro | this.checkedEntityCodes.add(entity.getEntityCode());
|
618 | 3448 | fdiaz | this.treeEntities.repaint();
|
619 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_CHECK_ID, ACTION_CHECK)); |
620 | 3365 | fdiaz | } |
621 | 3319 | fdiaz | |
622 | 3379 | jjdelcerro | @Override
|
623 | 3448 | fdiaz | public void checkAll() { |
624 | for (Map.Entry<String, VCSGisEntity> entry : entities.entrySet()) { |
||
625 | this.checkedEntityCodes.add(entry.getValue().getEntityCode());
|
||
626 | } |
||
627 | this.treeEntities.repaint();
|
||
628 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_CHECK_ID, ACTION_CHECK)); |
629 | 3448 | fdiaz | } |
630 | |||
631 | @Override
|
||
632 | 3379 | jjdelcerro | public void clearChecks() { |
633 | this.checkedEntityCodes.clear();
|
||
634 | 3448 | fdiaz | this.treeEntities.repaint();
|
635 | 3519 | fdiaz | fireActionEvent(new ActionEvent(this, ACTION_CHECK_ID, ACTION_CHECK)); |
636 | 3379 | jjdelcerro | } |
637 | 3383 | jjdelcerro | |
638 | private void expandAllNodes(JTree tree) { |
||
639 | int j = tree.getRowCount();
|
||
640 | int i = 0; |
||
641 | while(i < j) {
|
||
642 | tree.expandRow(i); |
||
643 | i += 1;
|
||
644 | j = tree.getRowCount(); |
||
645 | } |
||
646 | } |
||
647 | 3448 | fdiaz | |
648 | private boolean areSelectedAllEntitiesFromCategory(String category) { |
||
649 | |||
650 | for (Map.Entry<String, VCSGisEntity> entry : this.entities.entrySet()) { |
||
651 | VCSGisEntity entity = entry.getValue(); |
||
652 | if(StringUtils.isBlank(category)){
|
||
653 | if(StringUtils.isBlank(entity.getCategory())){
|
||
654 | if (!this.checkedEntityCodes.contains(entity.getEntityCode())) { |
||
655 | return false; |
||
656 | } |
||
657 | } |
||
658 | } else {
|
||
659 | if (entity.getCategoriesAsList().contains(category)) {
|
||
660 | if (!this.checkedEntityCodes.contains(entity.getEntityCode())) { |
||
661 | return false; |
||
662 | } |
||
663 | } |
||
664 | } |
||
665 | } |
||
666 | return true; |
||
667 | } |
||
668 | |||
669 | private Set<String> getAllEntityCodesFromCategory(String category){ |
||
670 | Set<String> entityCodes = new HashSet<>(); |
||
671 | for (Map.Entry<String, VCSGisEntity> entry : this.entities.entrySet()) { |
||
672 | VCSGisEntity entity = entry.getValue(); |
||
673 | if(StringUtils.isBlank(category)){
|
||
674 | if(StringUtils.isBlank(entity.getCategory())){
|
||
675 | entityCodes.add(entity.getEntityCode()); |
||
676 | } |
||
677 | } else {
|
||
678 | if (entity.getCategoriesAsList().contains(category)) {
|
||
679 | entityCodes.add(entity.getEntityCode()); |
||
680 | } |
||
681 | } |
||
682 | } |
||
683 | return entityCodes;
|
||
684 | } |
||
685 | |||
686 | 3234 | jjdelcerro | } |