svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretype / tags / TagController.java @ 46962
History | View | Annotate | Download (5.16 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype.tags; |
---|---|
2 |
|
3 |
import java.awt.event.ItemEvent; |
4 |
import java.util.ArrayList; |
5 |
import java.util.List; |
6 |
import java.util.Objects; |
7 |
import javax.swing.DefaultComboBoxModel; |
8 |
import javax.swing.JComboBox; |
9 |
import javax.swing.JLabel; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.fmap.dal.swing.impl.featuretype.AggregateController; |
12 |
import org.gvsig.fmap.dal.swing.impl.featuretype.AggregateController.AggregateItemController; |
13 |
import org.gvsig.tools.ToolsLocator; |
14 |
import org.gvsig.tools.dynobject.DynField; |
15 |
import org.gvsig.tools.dynobject.DynField_v2; |
16 |
import org.gvsig.tools.dynobject.DynMethod; |
17 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
18 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
19 |
import org.gvsig.tools.util.LabeledValue; |
20 |
import org.gvsig.tools.util.UnmodifiableBasicMap; |
21 |
|
22 |
/**
|
23 |
*
|
24 |
* @author gvSIG Team
|
25 |
*/
|
26 |
@SuppressWarnings("UseSpecificCatch") |
27 |
public class TagController implements AggregateItemController<Tag> { |
28 |
|
29 |
private final JComboBox cboName; |
30 |
private final JComboBox cboValue; |
31 |
private final JLabel lblDescription; |
32 |
private final UnmodifiableBasicMap<String, DynField> tagDefinitions; |
33 |
private AggregateController.AggregateModel tagsModel;
|
34 |
|
35 |
public TagController(
|
36 |
JComboBox cboName,
|
37 |
JComboBox cboValue,
|
38 |
JLabel lblDescription
|
39 |
) { |
40 |
this.cboName = cboName;
|
41 |
this.cboValue = cboValue;
|
42 |
this.lblDescription = lblDescription;
|
43 |
this.tagDefinitions = ToolsLocator.getDynObjectManager().getTags();
|
44 |
this.initComponents();
|
45 |
} |
46 |
|
47 |
private void initComponents() { |
48 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
49 |
swingManager.setDefaultPopupMenu(this.cboName);
|
50 |
swingManager.setDefaultPopupMenu(this.cboValue);
|
51 |
|
52 |
swingManager.addClearButton(this.cboValue);
|
53 |
|
54 |
List<String> tagsNames = new ArrayList<>(); |
55 |
for (String tag : this.tagDefinitions.keySet()) { |
56 |
tagsNames.add(tag); |
57 |
} |
58 |
tagsNames.sort(null);
|
59 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
60 |
model.addElement(" ");
|
61 |
for (String tag : tagsNames) { |
62 |
model.addElement(tag); |
63 |
} |
64 |
this.cboName.setModel(model);
|
65 |
this.cboName.addItemListener((ItemEvent e) -> { |
66 |
if( e.getStateChange()==ItemEvent.SELECTED ) { |
67 |
doChangeTagName(); |
68 |
} |
69 |
}); |
70 |
} |
71 |
|
72 |
private void doChangeTagName() { |
73 |
String tagname = (String) this.cboName.getSelectedItem(); |
74 |
if( StringUtils.isBlank(tagname) ) {
|
75 |
this.lblDescription.setText(""); |
76 |
return;
|
77 |
} |
78 |
for (DynField tagDefinition : tagDefinitions) {
|
79 |
if( tagname.equalsIgnoreCase(tagDefinition.getName()) ) {
|
80 |
this.lblDescription.setText("<html>"+tagDefinition.getDescription()+"<html>"); |
81 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
82 |
LabeledValue[] values;
|
83 |
if( tagDefinition instanceof DynField_v2 ) { |
84 |
DynMethod method = ((DynField_v2)tagDefinition).getAvailableValuesMethod(); |
85 |
try {
|
86 |
values = (LabeledValue[]) method.invoke(null, new Object[] { this.getAggregateModel().getTags() }); |
87 |
} catch(Throwable t) { |
88 |
values = null;
|
89 |
} |
90 |
if( values == null ) { |
91 |
values = tagDefinition.getAvailableValues(); |
92 |
} |
93 |
} else {
|
94 |
values = tagDefinition.getAvailableValues(); |
95 |
} |
96 |
|
97 |
if( values != null ) { |
98 |
for (LabeledValue value : values) {
|
99 |
model.addElement(value); |
100 |
} |
101 |
} |
102 |
this.cboValue.setModel(model);
|
103 |
return;
|
104 |
} |
105 |
} |
106 |
this.lblDescription.setText(""); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public void put(Tag item) { |
111 |
this.cboName.setSelectedItem(item.getName());
|
112 |
this.cboValue.setSelectedItem(Objects.toString(item.getValue(),"")); |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public boolean fetch(Tag item) { |
117 |
String s = Objects.toString(this.cboName.getSelectedItem()); |
118 |
if( StringUtils.isBlank(s) ) {
|
119 |
return false; |
120 |
} |
121 |
item.setName(s); |
122 |
item.setValue(Objects.toString(this.cboValue.getSelectedItem()));
|
123 |
return true; |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void clean() { |
128 |
this.cboName.setSelectedIndex(-1); |
129 |
this.cboValue.setSelectedIndex(-1); |
130 |
this.lblDescription.setText(""); |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public boolean isEditing() { |
135 |
return this.cboName.isEnabled(); |
136 |
} |
137 |
|
138 |
|
139 |
@Override
|
140 |
public void setEnabled(boolean b) { |
141 |
this.cboName.setEnabled(b);
|
142 |
this.cboValue.setEnabled(b);
|
143 |
} |
144 |
|
145 |
@Override
|
146 |
public boolean isEnabled() { |
147 |
return this.cboName.isEnabled(); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void setAggregateModel(AggregateController.AggregateModel model) { |
152 |
this.tagsModel = model;
|
153 |
} |
154 |
|
155 |
public TagsModel getAggregateModel() {
|
156 |
return (TagsModel) this.tagsModel; |
157 |
} |
158 |
|
159 |
} |