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 @ 44713
History | View | Annotate | Download (10.6 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.DefaultListModel; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.ListCellRenderer; |
14 |
import javax.swing.event.ListSelectionEvent; |
15 |
import org.apache.commons.io.FilenameUtils; |
16 |
import org.gvsig.fmap.dal.exception.DataException; |
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.fmap.dal.swing.DALSwingLocator; |
21 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
22 |
import org.gvsig.tools.swing.api.FilteredListController; |
23 |
import org.gvsig.tools.swing.api.FilteredListModel; |
24 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
25 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
26 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
27 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel; |
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 |
this.lstAvailableColumns.setCellRenderer(renderer);
|
53 |
this.lstSelectedColumns.setCellRenderer(renderer);
|
54 |
|
55 |
this.lstAvailableColumns.addListSelectionListener((ListSelectionEvent e) -> { |
56 |
if( e.getValueIsAdjusting() ) {
|
57 |
return;
|
58 |
} |
59 |
doSelectAvailableColumn(); |
60 |
}); |
61 |
this.lstSelectedColumns.addListSelectionListener((ListSelectionEvent e) -> { |
62 |
if( e.getValueIsAdjusting() ) {
|
63 |
return;
|
64 |
} |
65 |
doSelectColumn(); |
66 |
}); |
67 |
this.btnColumnAdd.addActionListener((ActionEvent e) -> { |
68 |
doColumnAdd(); |
69 |
}); |
70 |
this.btnColumnRemove.addActionListener((ActionEvent e) -> { |
71 |
doColumnRemove(); |
72 |
}); |
73 |
this.btnColumnDown.addActionListener((ActionEvent e) -> { |
74 |
doColumnDown(); |
75 |
}); |
76 |
this.btnColumnUp.addActionListener((ActionEvent e) -> { |
77 |
doColumnUp(); |
78 |
}); |
79 |
this.availableFieldsController = toolsSwingManager.createFilteredListController(
|
80 |
lstAvailableColumns, |
81 |
txtColumnsFilter, |
82 |
btnColumnsFilter |
83 |
); |
84 |
Dimension sz = this.getPreferredSize(); |
85 |
if( sz.width<450 ) { |
86 |
sz.width = 450;
|
87 |
} |
88 |
if( sz.height < 320 ) { |
89 |
sz.height = 320;
|
90 |
} |
91 |
this.setPreferredSize(sz);
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public List<String> getSelectedNames() { |
96 |
return Collections.unmodifiableList(selecteds); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void setSelectedNames(List<String> names) { |
101 |
this.selecteds.clear();
|
102 |
for (String name : names) { |
103 |
if( this.featureType.getAttributeDescriptor(name)!=null && |
104 |
!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 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
|
137 |
if( attr == null ) { |
138 |
return;
|
139 |
} |
140 |
int n = this.selecteds.indexOf(attr.getName()); |
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 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
|
151 |
if( attr == null ) { |
152 |
return;
|
153 |
} |
154 |
int n = this.selecteds.indexOf(attr.getName()); |
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 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstSelectedColumns.getSelectedValue();
|
165 |
if( attr == null ) { |
166 |
return;
|
167 |
} |
168 |
int n = this.selecteds.indexOf(attr.getName()); |
169 |
if( n<0 ) { |
170 |
return;
|
171 |
} |
172 |
this.selecteds.remove(n);
|
173 |
this.updateControls();
|
174 |
} |
175 |
|
176 |
private void doColumnAdd() { |
177 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAvailableColumns.getSelectedValue();
|
178 |
if( attr == null ) { |
179 |
return;
|
180 |
} |
181 |
int n = this.selecteds.indexOf(attr.getName()); |
182 |
if( n>=0 ) { |
183 |
return;
|
184 |
} |
185 |
this.selecteds.add(attr.getName());
|
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 |
DefaultListModel<FeatureAttributeDescriptor> modelSelecteds = new DefaultListModel<>(); |
252 |
for (FeatureAttributeDescriptor attr : featureType) {
|
253 |
if( !this.selecteds.contains(attr.getName()) ) { |
254 |
modelAvailables.addElement(attr); |
255 |
} |
256 |
} |
257 |
modelAvailables.sort(true);
|
258 |
for (String name : this.selecteds) { |
259 |
modelSelecteds.addElement(featureType.getAttributeDescriptor(name)); |
260 |
} |
261 |
modelAvailables.sort(true);
|
262 |
this.lstAvailableColumns.setModel(modelAvailables);
|
263 |
this.lstSelectedColumns.setModel(modelSelecteds);
|
264 |
if( indexAvailables >= 0 && modelAvailables.getSize()>0 ) { |
265 |
if( indexAvailables >= modelAvailables.getSize() ) {
|
266 |
indexAvailables = modelAvailables.getSize()-1;
|
267 |
} |
268 |
this.lstAvailableColumns.setSelectedIndex(indexAvailables);
|
269 |
this.btnColumnAdd.setEnabled(true); |
270 |
} else {
|
271 |
this.btnColumnAdd.setEnabled(false); |
272 |
} |
273 |
|
274 |
if( indexSelecteds >= 0 && modelSelecteds.getSize()>0 ) { |
275 |
if( indexSelecteds >= modelSelecteds.getSize() ) {
|
276 |
indexSelecteds = modelSelecteds.getSize()-1;
|
277 |
} |
278 |
this.lstSelectedColumns.setSelectedIndex(indexSelecteds);
|
279 |
this.btnColumnRemove.setEnabled(true); |
280 |
} else {
|
281 |
this.btnColumnRemove.setEnabled(false); |
282 |
} |
283 |
} |
284 |
|
285 |
@Override
|
286 |
public ImageIcon loadImage(String imageName) { |
287 |
String name = FilenameUtils.getBaseName(imageName);
|
288 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
289 |
if (theme.exists(name)) {
|
290 |
return theme.get(name);
|
291 |
} |
292 |
URL url = this.getClass().getResource(name + ".png"); |
293 |
if (url == null) { |
294 |
return null; |
295 |
} |
296 |
return new ImageIcon(url); |
297 |
} |
298 |
|
299 |
public static void selfRegister() { |
300 |
String[][] iconNames = new String[][]{ |
301 |
new String[]{"dalswing", "common-filter"}, |
302 |
new String[]{"dalswing", "featuretype-column-add-arrow"}, |
303 |
new String[]{"dalswing", "featuretype-column-remove-arrow"}, |
304 |
new String[]{"dalswing", "featuretype-column-down"}, |
305 |
new String[]{"dalswing", "featuretype-column-up"} |
306 |
}; |
307 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
308 |
for (String[] icon : iconNames) { |
309 |
URL url = DefaultFeatureAttributesSelectionPanel.class.getResource(icon[1] + ".png"); |
310 |
theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
311 |
} |
312 |
} |
313 |
} |