root / org.gvsig.complexlegend / trunk / org.gvsig.complexlegend / org.gvsig.complexlegend.swing / org.gvsig.complexlegend.swing.impl / src / main / java / org / gvsig / complexlegend / swing / impl / DefaultComplexLegendPanel.java @ 86
History | View | Annotate | Download (10.1 KB)
1 |
package org.gvsig.complexlegend.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.FlowLayout; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
|
11 |
import javax.swing.BorderFactory; |
12 |
import javax.swing.DefaultComboBoxModel; |
13 |
import javax.swing.ImageIcon; |
14 |
import javax.swing.JButton; |
15 |
import javax.swing.JComboBox; |
16 |
import javax.swing.JDialog; |
17 |
import javax.swing.JLabel; |
18 |
import javax.swing.JPanel; |
19 |
import javax.swing.event.ListDataEvent; |
20 |
import javax.swing.event.ListDataListener; |
21 |
|
22 |
import org.gvsig.andami.IconThemeHelper; |
23 |
import org.gvsig.andami.PluginServices; |
24 |
import org.gvsig.app.project.documents.view.legend.gui.AbstractParentPanel; |
25 |
import org.gvsig.app.project.documents.view.legend.gui.Features; |
26 |
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel; |
27 |
import org.gvsig.complexlegend.ComplexLegendItem; |
28 |
import org.gvsig.complexlegend.VectorComplexLegend; |
29 |
import org.gvsig.complexlegend.impl.DefaultVectorComplexLegend; |
30 |
import org.gvsig.complexlegend.swing.ComplexLegendPanel; |
31 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
32 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
33 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
34 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
35 |
import org.gvsig.symbology.swing.SymbologySwingLocator; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.i18n.I18nManager; |
38 |
import org.gvsig.tools.observer.Observable; |
39 |
import org.gvsig.tools.observer.Observer; |
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
public class DefaultComplexLegendPanel extends JPanel implements ComplexLegendPanel, Observer{ |
44 |
|
45 |
/**
|
46 |
*
|
47 |
*/
|
48 |
private static final long serialVersionUID = -312336406359200569L; |
49 |
private static Logger logger = LoggerFactory.getLogger(DefaultComplexLegendPanel.class); |
50 |
private VectorComplexLegend auxLegend;
|
51 |
private FLayer layer;
|
52 |
private JComboBox cmbValue; |
53 |
private JPanel symbolPanel; |
54 |
private JButton addButton; |
55 |
private JButton removeButton; |
56 |
private DefaultComboBoxModel model; |
57 |
private ILegendPanel legendPanel;
|
58 |
private JDialog dlg; |
59 |
private ComplexLegendItem currentItem;
|
60 |
private I18nManager i18nManager;
|
61 |
private int currentIndex; |
62 |
|
63 |
public DefaultComplexLegendPanel(){
|
64 |
super();
|
65 |
initialize(); |
66 |
refreshButtons(); |
67 |
} |
68 |
|
69 |
private void initialize() { |
70 |
this.i18nManager = ToolsLocator.getI18nManager();
|
71 |
|
72 |
setLayout(new BorderLayout()); |
73 |
JPanel aux = new JPanel(new BorderLayout()); |
74 |
|
75 |
JPanel fieldsPanel = new JPanel(new FlowLayout()); |
76 |
fieldsPanel.setBorder(BorderFactory.createTitledBorder(null, |
77 |
i18nManager.getTranslation("select_scale")));
|
78 |
fieldsPanel.setPreferredSize(new Dimension(300,60)); |
79 |
|
80 |
fieldsPanel.add(new JLabel()); |
81 |
fieldsPanel.add(getComboBox()); |
82 |
fieldsPanel.add(getAddButton()); |
83 |
fieldsPanel.add(getRemoveButton()); |
84 |
|
85 |
symbolPanel = new JPanel(); |
86 |
symbolPanel.setBorder(BorderFactory.createTitledBorder(null, |
87 |
i18nManager.getTranslation("legend_definition")));
|
88 |
|
89 |
|
90 |
aux.add(fieldsPanel, BorderLayout.NORTH);
|
91 |
aux.add(symbolPanel, BorderLayout.CENTER);
|
92 |
|
93 |
this.add(aux, BorderLayout.CENTER); |
94 |
} |
95 |
|
96 |
private JComboBox getComboBox() { |
97 |
if(cmbValue == null){ |
98 |
cmbValue = new JComboBox(getComboBoxModel()); |
99 |
cmbValue.setPreferredSize(new Dimension(350, 20)); |
100 |
cmbValue.addActionListener(new ActionListener() { |
101 |
public void actionPerformed(ActionEvent e) { |
102 |
refreshLegendPanel(); |
103 |
} |
104 |
}); |
105 |
} |
106 |
return cmbValue;
|
107 |
} |
108 |
|
109 |
private DefaultComboBoxModel getComboBoxModel(){ |
110 |
if(this.model == null){ |
111 |
this.model = new DefaultComboBoxModel(); |
112 |
this.model.addListDataListener(new ListDataListener() { |
113 |
public void intervalRemoved(ListDataEvent arg0) { |
114 |
} |
115 |
|
116 |
public void intervalAdded(ListDataEvent arg0) { |
117 |
} |
118 |
|
119 |
public void contentsChanged(ListDataEvent arg0) { |
120 |
refreshButtons(); |
121 |
} |
122 |
}); |
123 |
} |
124 |
return this.model; |
125 |
} |
126 |
|
127 |
protected void refreshButtons() { |
128 |
boolean status = (getComboBoxModel().getSize() != 0); |
129 |
getComboBox().setEnabled(status); |
130 |
getRemoveButton().setEnabled(status); |
131 |
if(legendPanel != null){ |
132 |
legendPanel.getPanel().setVisible(status); |
133 |
} |
134 |
} |
135 |
|
136 |
private JButton getAddButton() { |
137 |
if(addButton == null){ |
138 |
addButton = new JButton(); |
139 |
addButton.setIcon(PluginServices.getIconTheme().get("legend-add"));
|
140 |
addButton.addActionListener(new ActionListener() { |
141 |
public void actionPerformed(ActionEvent arg0) { |
142 |
createComplexLegendItemWindow(); |
143 |
} |
144 |
}); |
145 |
} |
146 |
return addButton;
|
147 |
} |
148 |
|
149 |
private JButton getRemoveButton() { |
150 |
if(removeButton == null){ |
151 |
removeButton = new JButton(); |
152 |
removeButton.setIcon(PluginServices.getIconTheme().get("legend-delete"));
|
153 |
removeButton.addActionListener(new ActionListener() { |
154 |
public void actionPerformed(ActionEvent arg0) { |
155 |
removeComplexLegendItem(); |
156 |
} |
157 |
}); |
158 |
} |
159 |
return removeButton;
|
160 |
} |
161 |
|
162 |
protected void removeComplexLegendItem() { |
163 |
ComplexLegendItem item = (ComplexLegendItem) getComboBox().getSelectedItem(); |
164 |
int index = auxLegend.getLegends().indexOf(item);
|
165 |
if(index != -1){ |
166 |
auxLegend.remove(item); |
167 |
getComboBoxModel().removeElementAt(index); |
168 |
} |
169 |
refreshButtons(); |
170 |
} |
171 |
|
172 |
protected void createComplexLegendItemWindow() { |
173 |
if(layer != null){ |
174 |
ComplexLegendItemPanel legendItemPanel = |
175 |
new ComplexLegendItemPanel(layer, (VectorComplexLegend) getLegend());
|
176 |
legendItemPanel.addObserver(this);
|
177 |
|
178 |
dlg = new JDialog(); |
179 |
dlg.setTitle( i18nManager.getTranslation("SelectItem"));
|
180 |
dlg.setResizable(true);
|
181 |
//legendItemPanel.setParentDialog(dlg);
|
182 |
dlg.getContentPane().add(legendItemPanel); |
183 |
dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
184 |
|
185 |
dlg.setSize(510, 510); |
186 |
dlg.setModal(true);
|
187 |
dlg.pack(); |
188 |
dlg.setLocationRelativeTo(null);
|
189 |
dlg.setVisible(true);
|
190 |
} |
191 |
} |
192 |
|
193 |
public String getDescription() { |
194 |
return i18nManager.getTranslation(
|
195 |
"legend_by_scale");
|
196 |
} |
197 |
|
198 |
public ImageIcon getIcon() { |
199 |
return IconThemeHelper.getImageIcon(
|
200 |
"scaled-symbols");
|
201 |
} |
202 |
|
203 |
public ILegend getLegend() {
|
204 |
if (auxLegend == null) { |
205 |
auxLegend = (DefaultVectorComplexLegend) MapContextLocator.getMapContextManager() |
206 |
.createLegend(DefaultVectorComplexLegend.COMPLEX_LEGEND_NAME); |
207 |
} |
208 |
saveCurrentPanel(); |
209 |
|
210 |
// DefaultComplexLegendItem scaleLegend1 = new DefaultComplexLegendItem();
|
211 |
//
|
212 |
// SimpleFillSymbol fillsymbol1 = new SimpleFillSymbol();
|
213 |
// fillsymbol1.setFillColor(Color.WHITE);
|
214 |
// SingleSymbolLegend legend1 = new SingleSymbolLegend(fillsymbol1);
|
215 |
// scaleLegend1.setLegend(legend1);
|
216 |
// scaleLegend1.setMinScale(10000000);
|
217 |
// scaleLegend1.setMaxScale(20000000);
|
218 |
//
|
219 |
// DefaultComplexLegendItem scaleLegend2 = new DefaultComplexLegendItem();
|
220 |
// SimpleFillSymbol fillsymbol2 = new SimpleFillSymbol();
|
221 |
// fillsymbol2.setFillColor(Color.BLACK);
|
222 |
// SingleSymbolLegend legend2 = new SingleSymbolLegend(fillsymbol2);
|
223 |
// scaleLegend2.setLegend(legend2);
|
224 |
// scaleLegend2.setMinScale(0);
|
225 |
// scaleLegend2.setMaxScale(9999999);
|
226 |
//
|
227 |
// auxLegend.add(scaleLegend2);
|
228 |
// auxLegend.add(scaleLegend1);
|
229 |
|
230 |
return auxLegend;
|
231 |
} |
232 |
|
233 |
public Class getLegendClass() { |
234 |
return VectorComplexLegend.class;
|
235 |
} |
236 |
|
237 |
public JPanel getPanel() { |
238 |
return this; |
239 |
} |
240 |
|
241 |
public Class getParentClass() { |
242 |
return Features.class;
|
243 |
} |
244 |
|
245 |
public String getTitle() { |
246 |
return i18nManager.getTranslation("complex_symbology"); |
247 |
} |
248 |
|
249 |
public boolean isSuitableFor(FLayer layer) { |
250 |
return (layer instanceof FLyrVect); |
251 |
} |
252 |
|
253 |
public void setData(FLayer layer, ILegend data) { |
254 |
this.layer = layer;
|
255 |
if(data instanceof DefaultVectorComplexLegend){ |
256 |
auxLegend = (DefaultVectorComplexLegend) data; |
257 |
updateComboModel(); |
258 |
} |
259 |
} |
260 |
|
261 |
private void saveCurrentPanel(){ |
262 |
if(legendPanel != null && currentItem != null){ |
263 |
currentItem.setLegend(legendPanel.getLegend()); |
264 |
} |
265 |
} |
266 |
|
267 |
|
268 |
public void refreshLegendPanel(){ |
269 |
ComplexLegendItem item = (ComplexLegendItem) getComboBox().getSelectedItem(); |
270 |
if(item != null){ |
271 |
ILegendPanel aux = getLegendPanel(item.getLegend()); |
272 |
if(aux != null){ |
273 |
saveCurrentPanel(); |
274 |
|
275 |
symbolPanel.removeAll(); |
276 |
legendPanel = aux; |
277 |
legendPanel.setData(layer, item.getLegend()); |
278 |
symbolPanel.add(legendPanel.getPanel()); |
279 |
symbolPanel.repaint(); |
280 |
symbolPanel.revalidate(); |
281 |
|
282 |
currentItem = item; |
283 |
currentIndex = getComboBox().getSelectedIndex(); |
284 |
} |
285 |
} |
286 |
} |
287 |
|
288 |
private ILegendPanel getLegendPanel(ILegend legend){
|
289 |
List panels = SymbologySwingLocator.getSwingManager().getLegendEditorClasses();
|
290 |
Iterator it = panels.iterator();
|
291 |
while(it.hasNext()){
|
292 |
Class panelClass = (Class) it.next(); |
293 |
ILegendPanel panel; |
294 |
try {
|
295 |
panel = (ILegendPanel) panelClass.newInstance(); |
296 |
if(!(panel instanceof AbstractParentPanel)){ |
297 |
if(panel.getLegendClass().equals(legend.getClass())){
|
298 |
panel.setData(layer, legend); |
299 |
return panel;
|
300 |
} |
301 |
} |
302 |
} catch (Exception e) { |
303 |
logger.warn("Error creating ILegendPanel. ", e);
|
304 |
return null; |
305 |
} |
306 |
} |
307 |
return null; |
308 |
} |
309 |
|
310 |
|
311 |
|
312 |
public void update(Observable observable, Object isAccepted) { |
313 |
if(observable instanceof ComplexLegendItemPanel && isAccepted instanceof Boolean){ |
314 |
ComplexLegendItemPanel itemPanel = (ComplexLegendItemPanel) observable; |
315 |
boolean acceptForm = (Boolean) isAccepted; |
316 |
if(!acceptForm){
|
317 |
/* Si se ha pinchado en cancelar, se cierra sin m?s */
|
318 |
dlg.dispose(); |
319 |
}else{
|
320 |
if(itemPanel.validateValues()){
|
321 |
ComplexLegendItem item = itemPanel.getLegendItem(); |
322 |
|
323 |
/* Guardo el panel actual*/
|
324 |
saveCurrentPanel(); |
325 |
|
326 |
/* A?ado y ordeno la nueva entrada */
|
327 |
auxLegend.add(item); |
328 |
updateComboModel(); |
329 |
|
330 |
/* Cargo el nuevo panel */
|
331 |
symbolPanel.removeAll(); |
332 |
legendPanel = itemPanel.getLegendPanel(); |
333 |
legendPanel.setData(layer, item.getLegend()); |
334 |
symbolPanel.add(legendPanel.getPanel()); |
335 |
symbolPanel.repaint(); |
336 |
|
337 |
/* Selecciono la nueva entrada */
|
338 |
currentItem = item; |
339 |
getComboBox().setSelectedItem(item); |
340 |
currentIndex = getComboBox().getSelectedIndex(); |
341 |
|
342 |
/* Cierro el di?logo */
|
343 |
dlg.dispose(); |
344 |
} |
345 |
} |
346 |
} |
347 |
} |
348 |
|
349 |
private void updateComboModel() { |
350 |
getComboBoxModel().removeAllElements(); |
351 |
if(auxLegend != null){ |
352 |
Iterator<ComplexLegendItem> it = auxLegend.getLegends().iterator();
|
353 |
while(it.hasNext()){
|
354 |
ComplexLegendItem item = (ComplexLegendItem) it.next(); |
355 |
getComboBoxModel().addElement(item); |
356 |
} |
357 |
|
358 |
} |
359 |
} |
360 |
|
361 |
} |