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