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 / DefaultFeatureAttributesSelectionPanel.java @ 44747
History | View | Annotate | Download (10.7 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.net.URL; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Collections; |
9 |
import java.util.List; |
10 |
import javax.swing.DefaultListCellRenderer; |
11 |
import javax.swing.DefaultListModel; |
12 |
import javax.swing.ImageIcon; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.ListCellRenderer; |
15 |
import javax.swing.event.ListSelectionEvent; |
16 |
import org.apache.commons.io.FilenameUtils; |
17 |
import org.gvsig.fmap.dal.exception.DataException; |
18 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
19 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
22 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
23 |
import org.gvsig.tools.swing.api.FilteredListController; |
24 |
import org.gvsig.tools.swing.api.FilteredListModel; |
25 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
26 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
27 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
28 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel; |
29 |
import org.gvsig.tools.util.LabeledValue; |
30 |
import org.gvsig.tools.util.LabeledValueImpl; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author jjdelcerro
|
35 |
*/
|
36 |
public class DefaultFeatureAttributesSelectionPanel |
37 |
extends DefaultFeatureAttributesSelectionPanelView
|
38 |
implements FeatureAttributesSelectionPanel
|
39 |
{ |
40 |
|
41 |
private FeatureType featureType;
|
42 |
private final ActionListenerSupport actionListenerSupport; |
43 |
private FilteredListController availableFieldsController;
|
44 |
private final List<String> selecteds; |
45 |
|
46 |
public DefaultFeatureAttributesSelectionPanel() {
|
47 |
this.actionListenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
48 |
this.selecteds = new ArrayList<>(); |
49 |
this.initComponents();
|
50 |
} |
51 |
|
52 |
private void initComponents() { |
53 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
54 |
// ListCellRenderer renderer = DALSwingLocator.getDataSwingManager().createDefaultFeatureAttributeListCellRenderer();
|
55 |
DefaultListCellRenderer renderer = new DefaultListCellRenderer(); |
56 |
this.lstAvailableColumns.setCellRenderer(renderer);
|
57 |
this.lstSelectedColumns.setCellRenderer(renderer);
|
58 |
|
59 |
this.lstAvailableColumns.addListSelectionListener((ListSelectionEvent e) -> { |
60 |
if( e.getValueIsAdjusting() ) {
|
61 |
return;
|
62 |
} |
63 |
doSelectAvailableColumn(); |
64 |
}); |
65 |
this.lstSelectedColumns.addListSelectionListener((ListSelectionEvent e) -> { |
66 |
if( e.getValueIsAdjusting() ) {
|
67 |
return;
|
68 |
} |
69 |
doSelectColumn(); |
70 |
}); |
71 |
this.btnColumnAdd.addActionListener((ActionEvent e) -> { |
72 |
doColumnAdd(); |
73 |
}); |
74 |
this.btnColumnRemove.addActionListener((ActionEvent e) -> { |
75 |
doColumnRemove(); |
76 |
}); |
77 |
this.btnColumnDown.addActionListener((ActionEvent e) -> { |
78 |
doColumnDown(); |
79 |
}); |
80 |
this.btnColumnUp.addActionListener((ActionEvent e) -> { |
81 |
doColumnUp(); |
82 |
}); |
83 |
this.availableFieldsController = toolsSwingManager.createFilteredListController(
|
84 |
lstAvailableColumns, |
85 |
txtColumnsFilter, |
86 |
btnColumnsFilter |
87 |
); |
88 |
Dimension sz = this.getPreferredSize(); |
89 |
if( sz.width<450 ) { |
90 |
sz.width = 450;
|
91 |
} |
92 |
if( sz.height < 320 ) { |
93 |
sz.height = 320;
|
94 |
} |
95 |
this.setPreferredSize(sz);
|
96 |
} |
97 |
|
98 |
@Override
|
99 |
public List<String> getSelectedNames() { |
100 |
return Collections.unmodifiableList(selecteds); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void setSelectedNames(List<String> names) { |
105 |
this.selecteds.clear();
|
106 |
for (String name : names) { |
107 |
if(!this.selecteds.contains(name)) { |
108 |
this.selecteds.add(name);
|
109 |
} |
110 |
} |
111 |
updateControls(); |
112 |
} |
113 |
|
114 |
private void doSelectAvailableColumn() { |
115 |
if( this.lstAvailableColumns.getSelectedIndex()>=0 ) { |
116 |
this.btnColumnAdd.setEnabled(true); |
117 |
return;
|
118 |
} |
119 |
this.btnColumnAdd.setEnabled(false); |
120 |
} |
121 |
|
122 |
private void doSelectColumn() { |
123 |
if( this.lstSelectedColumns.getSelectedIndex()>=0 ) { |
124 |
// FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
|
125 |
// if( attr != null ) {
|
126 |
// this.selecteds.add(attr.getName());
|
127 |
this.btnColumnRemove.setEnabled(true); |
128 |
this.btnColumnDown.setEnabled(true); |
129 |
this.btnColumnUp.setEnabled(true); |
130 |
return;
|
131 |
// }
|
132 |
} |
133 |
this.btnColumnRemove.setEnabled(false); |
134 |
this.btnColumnDown.setEnabled(false); |
135 |
this.btnColumnUp.setEnabled(false); |
136 |
} |
137 |
|
138 |
private void doColumnUp() { |
139 |
LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
|
140 |
if( attr == null ) { |
141 |
return;
|
142 |
} |
143 |
int n = this.selecteds.indexOf(attr.getLabel()); |
144 |
if( n<1 ) { |
145 |
return;
|
146 |
} |
147 |
Collections.swap(selecteds, n, n-1); |
148 |
this.lstSelectedColumns.setSelectedIndex(n-1); |
149 |
this.updateControls();
|
150 |
} |
151 |
|
152 |
private void doColumnDown() { |
153 |
LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
|
154 |
if( attr == null ) { |
155 |
return;
|
156 |
} |
157 |
int n = this.selecteds.indexOf(attr.getLabel()); |
158 |
if( n<1 || n>=this.selecteds.size()-1 ) { |
159 |
return;
|
160 |
} |
161 |
Collections.swap(selecteds, n, n+1); |
162 |
this.lstSelectedColumns.setSelectedIndex(n+1); |
163 |
this.updateControls();
|
164 |
} |
165 |
|
166 |
private void doColumnRemove() { |
167 |
LabeledValue attr = (LabeledValue) this.lstSelectedColumns.getSelectedValue();
|
168 |
if( attr == null ) { |
169 |
return;
|
170 |
} |
171 |
int n = this.selecteds.indexOf(attr.getLabel()); |
172 |
if( n<0 ) { |
173 |
return;
|
174 |
} |
175 |
this.selecteds.remove(n);
|
176 |
this.updateControls();
|
177 |
} |
178 |
|
179 |
private void doColumnAdd() { |
180 |
LabeledValue attr = (LabeledValue) this.lstAvailableColumns.getSelectedValue();
|
181 |
if( attr == null ) { |
182 |
return;
|
183 |
} |
184 |
int n = this.selecteds.indexOf(attr); |
185 |
if( n>=0 ) { |
186 |
return;
|
187 |
} |
188 |
this.selecteds.add(attr.getLabel());
|
189 |
this.updateControls();
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void setStore(FeatureStore store) { |
194 |
try {
|
195 |
this.setFeatureType(store.getDefaultFeatureType());
|
196 |
} catch (DataException ex) {
|
197 |
throw new RuntimeException("Can't assign store", ex); |
198 |
} |
199 |
} |
200 |
|
201 |
@Override
|
202 |
public void setFeatureType(FeatureType featureType) { |
203 |
this.featureType = featureType;
|
204 |
if( !this.selecteds.isEmpty() ) { |
205 |
return;
|
206 |
} |
207 |
this.updateControls();
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public FeatureType getFeatureType() {
|
212 |
return this.featureType; |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public JComponent asJComponent() { |
217 |
return this; |
218 |
} |
219 |
|
220 |
@Override
|
221 |
public void addActionListener(ActionListener al) { |
222 |
this.actionListenerSupport.addActionListener(al);
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public ActionListener[] getActionListeners() { |
227 |
return this.actionListenerSupport.getActionListeners(); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public void removeActionListener(ActionListener al) { |
232 |
this.actionListenerSupport.removeActionListener(al);
|
233 |
} |
234 |
|
235 |
@Override
|
236 |
public void removeAllActionListener() { |
237 |
this.actionListenerSupport.removeAllActionListener();
|
238 |
} |
239 |
|
240 |
@Override
|
241 |
public void fireActionEvent(ActionEvent ae) { |
242 |
this.actionListenerSupport.fireActionEvent(ae);
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public boolean hasActionListeners() { |
247 |
return this.actionListenerSupport.hasActionListeners(); |
248 |
} |
249 |
|
250 |
private void updateControls() { |
251 |
int indexAvailables = this.lstAvailableColumns.getSelectedIndex(); |
252 |
int indexSelecteds = this.lstSelectedColumns.getSelectedIndex(); |
253 |
FilteredListModel modelAvailables = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel(); |
254 |
FilteredListModel modelSelecteds = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel(); |
255 |
for (FeatureAttributeDescriptor attr : featureType) {
|
256 |
if( !this.selecteds.contains(attr.getName()) ) { |
257 |
modelAvailables.addElement(attr.getName()); |
258 |
} |
259 |
} |
260 |
modelAvailables.setFilter(this.txtColumnsFilter.getText());
|
261 |
modelAvailables.sort(true);
|
262 |
for (String name : this.selecteds) { |
263 |
modelSelecteds.addElement(name); |
264 |
} |
265 |
modelAvailables.sort(true);
|
266 |
this.lstAvailableColumns.setModel(modelAvailables);
|
267 |
this.lstSelectedColumns.setModel(modelSelecteds);
|
268 |
if( indexAvailables >= 0 && modelAvailables.getSize()>0 ) { |
269 |
if( indexAvailables >= modelAvailables.getSize() ) {
|
270 |
indexAvailables = modelAvailables.getSize()-1;
|
271 |
} |
272 |
this.lstAvailableColumns.setSelectedIndex(indexAvailables);
|
273 |
this.btnColumnAdd.setEnabled(true); |
274 |
} else {
|
275 |
this.btnColumnAdd.setEnabled(false); |
276 |
} |
277 |
|
278 |
if( indexSelecteds >= 0 && modelSelecteds.getSize()>0 ) { |
279 |
if( indexSelecteds >= modelSelecteds.getSize() ) {
|
280 |
indexSelecteds = modelSelecteds.getSize()-1;
|
281 |
} |
282 |
this.lstSelectedColumns.setSelectedIndex(indexSelecteds);
|
283 |
this.btnColumnRemove.setEnabled(true); |
284 |
} else {
|
285 |
this.btnColumnRemove.setEnabled(false); |
286 |
} |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public ImageIcon loadImage(String imageName) { |
291 |
String name = FilenameUtils.getBaseName(imageName);
|
292 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
293 |
if (theme.exists(name)) {
|
294 |
return theme.get(name);
|
295 |
} |
296 |
URL url = this.getClass().getResource(name + ".png"); |
297 |
if (url == null) { |
298 |
return null; |
299 |
} |
300 |
return new ImageIcon(url); |
301 |
} |
302 |
|
303 |
public static void selfRegister() { |
304 |
String[][] iconNames = new String[][]{ |
305 |
new String[]{"dalswing", "common-filter"}, |
306 |
new String[]{"dalswing", "featuretype-column-add-arrow"}, |
307 |
new String[]{"dalswing", "featuretype-column-remove-arrow"}, |
308 |
new String[]{"dalswing", "featuretype-column-down"}, |
309 |
new String[]{"dalswing", "featuretype-column-up"} |
310 |
}; |
311 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
312 |
for (String[] icon : iconNames) { |
313 |
URL url = DefaultFeatureAttributesSelectionPanel.class.getResource(icon[1] + ".png"); |
314 |
theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
315 |
} |
316 |
} |
317 |
} |