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