Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / LabelingManager.java @ 30144

History | View | Annotate | Download (8.61 KB)

1 10679 jaume
/*
2
 * Created on 01-jun-2004
3
 *
4
 */
5 13003 jaume
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6 10679 jaume
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27 13003 jaume
 *   Av. Blasco Ib??ez, 50
28 10679 jaume
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45 29598 jpiera
package org.gvsig.app.project.documents.view.legend.gui;
46 10679 jaume
47
import java.awt.BorderLayout;
48
import java.awt.Component;
49
import java.awt.FlowLayout;
50
import java.awt.event.ActionEvent;
51
import java.awt.event.ActionListener;
52
import java.util.ArrayList;
53 28414 vcaballero
import java.util.Comparator;
54 10679 jaume
import java.util.Hashtable;
55
import java.util.Iterator;
56 28414 vcaballero
import java.util.TreeMap;
57 10679 jaume
58
import javax.swing.BorderFactory;
59
import javax.swing.JCheckBox;
60
import javax.swing.JComponent;
61
import javax.swing.JLabel;
62
import javax.swing.JPanel;
63
64 29598 jpiera
import org.gvsig.andami.PluginServices;
65
import org.gvsig.andami.messages.NotificationManager;
66 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayer;
67 30011 cordinyana
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
68 20994 jmvivo
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
69 29598 jpiera
import org.gvsig.utils.swing.JComboBox;
70 20904 vcaballero
71 10679 jaume
72 10911 jaume
/**
73
 *
74
 * @author jaume dominguez faus - jaume.dominguez@iver.es
75
 *
76
 */
77 10679 jaume
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
78 13729 jaume
        private static final long serialVersionUID = 856162295985695717L;
79 20904 vcaballero
        private static ArrayList<Class<? extends ILabelingStrategyPanel>> installedPanels = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
80 28414 vcaballero
        private Comparator<Class<?>> comparator=new Comparator<Class<?>>(){
81
82
                public int compare(Class<?> o1, Class<?> o2) {
83
                        return o1.getName().compareTo(o2.getName());
84
                }
85
86
        };
87
        private TreeMap<Class<?>, ILabelingStrategyPanel> strategyPanels = new TreeMap<Class<?>, ILabelingStrategyPanel>(comparator);
88 10679 jaume
        private JCheckBox chkApplyLabels;
89
        private ILabelable layer;
90 10844 jaume
        private JPanel content;
91
        private JPanel pnlNorth;
92 11747 jaume
        private JComboBox cmbStrategy;
93 10679 jaume
94 25750 vcaballero
95 18623 jdominguez
        public LabelingManager() {
96
                super();
97
                initialize();
98 10679 jaume
        }
99
100 11747 jaume
        private class LabelingStrategyItem {
101 18623 jdominguez
                private ILabelingStrategyPanel strategyPanel;
102 25750 vcaballero
103 18623 jdominguez
                private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
104
                        this.strategyPanel = strategyPanel;
105 11747 jaume
                }
106
107
                public String toString() {
108 18623 jdominguez
                        return strategyPanel.getLabelingStrategyName();
109 11747 jaume
                }
110
111
                public boolean equals(Object obj) {
112 18623 jdominguez
                        if (obj instanceof LabelingStrategyItem) {
113
                                LabelingStrategyItem item = (LabelingStrategyItem) obj;
114
                                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
115 11747 jaume
116
                        }
117
                        return super.equals(obj);
118
                }
119
120
        }
121 25750 vcaballero
122 10679 jaume
        private void initialize() {
123 10844 jaume
                setLayout(new BorderLayout());
124 20904 vcaballero
                for (Iterator<Class<? extends ILabelingStrategyPanel>> it = installedPanels.iterator(); it.hasNext();) {
125 18623 jdominguez
                        try {
126
                                ILabelingStrategyPanel pnl = (ILabelingStrategyPanel) it.next().newInstance();
127
                                strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
128 25750 vcaballero
                        } catch (Exception e) {
129 18623 jdominguez
                                /*
130 25750 vcaballero
                                 *  can't happen
131 18623 jdominguez
                                 *  this should never happen since instantiation and access exceptions have been
132
                                 *        controlled in the addLabelingStrategy method
133 20904 vcaballero
                                 */
134
                                NotificationManager.addError(e);
135 18623 jdominguez
                        }
136 25750 vcaballero
137 18623 jdominguez
                }
138 10844 jaume
                content = new JPanel(new BorderLayout());
139
                content.setBorder(BorderFactory.createEtchedBorder());
140
                add(getPnlNorth(), BorderLayout.NORTH);
141
                add(content, BorderLayout.SOUTH);
142 11747 jaume
143 10844 jaume
        }
144 10679 jaume
145 10844 jaume
        private JPanel getPnlNorth() {
146
                if (pnlNorth == null) {
147
                        pnlNorth = new JPanel(new BorderLayout(5,5));
148
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
149 10679 jaume
150 10844 jaume
                        aux.add(getChkApplyLabels());
151
                        pnlNorth.add(aux, BorderLayout.NORTH);
152
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
153
                        aux.add(new JLabel(PluginServices.getText(this, "general")+":"));
154 11747 jaume
                        aux.add(getCmbStrategy());
155 10844 jaume
                        pnlNorth.add(aux, BorderLayout.CENTER);
156 10679 jaume
157 10844 jaume
                }
158
                return pnlNorth;
159
        }
160 10679 jaume
161 11747 jaume
        private JComboBox getCmbStrategy() {
162
                if (cmbStrategy == null) {
163 18623 jdominguez
                        Iterator<ILabelingStrategyPanel> it = strategyPanels.values().iterator();
164 13729 jaume
                        final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
165 11747 jaume
                        while (it.hasNext()) {
166 18623 jdominguez
                                aux.add(new LabelingStrategyItem(it.next()));
167 11747 jaume
                        }
168 13729 jaume
                        final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
169 14833 vcaballero
170 13729 jaume
                        cmbStrategy = new JComboBox(items) {
171 18623 jdominguez
                                private static final long serialVersionUID = 7506754097091500846L;
172 14833 vcaballero
173 13729 jaume
                                @Override
174
                                public void setSelectedItem(Object anObject) {
175 18623 jdominguez
                                        if (anObject == null)
176
                                                return;
177
                                        if (anObject instanceof ILabelingStrategy) {
178
                                                ILabelingStrategy st = (ILabelingStrategy) anObject;
179
                                                for (ILabelingStrategyPanel pnl : strategyPanels.values()) {
180
                                                        if (pnl.getLabelingStrategyClass() != null &&
181
                                                                pnl.getLabelingStrategyClass().equals(st.getClass())) {
182
                                                                super.setSelectedItem(new LabelingStrategyItem(pnl));
183 13729 jaume
                                                                return;
184
                                                        }
185
                                                }
186 18623 jdominguez
                                        } else {
187
                                                super.setSelectedItem(anObject);
188 13729 jaume
                                        }
189
                                }
190
                        };
191 25750 vcaballero
192 11747 jaume
                        cmbStrategy.setName("CMBMODE");
193
                        cmbStrategy.addActionListener(this);
194 10844 jaume
                }
195 10679 jaume
196 11747 jaume
                return cmbStrategy;
197 10844 jaume
        }
198 10679 jaume
199
        private JCheckBox getChkApplyLabels() {
200
                if (chkApplyLabels == null) {
201 13522 jaume
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
202 11271 jaume
                        chkApplyLabels.setName("CHKAPPLYLABELS");
203 10679 jaume
                        chkApplyLabels.addActionListener(this);
204
                }
205
                return chkApplyLabels;
206
        }
207
208 20904 vcaballero
        public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
209
                installedPanels.add(iLabelingStrategyPanelClass);
210 10679 jaume
        }
211
212
        private void setComponentEnabled(Component c, boolean b) {
213
                c.setEnabled(b);
214
        }
215
216
217
        public void setModel(FLayer layer) throws IllegalArgumentException {
218
                if (layer instanceof ILabelable) {
219
                        // get the labeling strategy
220 18623 jdominguez
                        this.layer = (ILabelable) layer;
221
                        for (ILabelingStrategyPanel p : strategyPanels.values()) {
222
                                p.setModel(layer,((ILabelable) layer).getLabelingStrategy() );
223 10679 jaume
                        }
224 25750 vcaballero
225 10679 jaume
                        setComponentEnabled(this, true);
226
                        refreshControls();
227 25750 vcaballero
228
229 10844 jaume
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
230
                        evt.setSource(chkApplyLabels);
231
                        actionPerformed(evt);
232 25750 vcaballero
233 18623 jdominguez
                        getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
234
                        evt.setSource(getCmbStrategy());
235
                        actionPerformed(evt);
236 10679 jaume
                } else {
237
                        setComponentEnabled(this, false);
238
                }
239
        }
240
241
242 25750 vcaballero
243 10679 jaume
        private void refreshControls() {
244
                if (layer == null) return;
245 25750 vcaballero
246 10679 jaume
                // enables labeling
247 25750 vcaballero
                JCheckBox applyLabels = getChkApplyLabels();
248
                applyLabels.setSelected(layer.isLabeled());
249 10679 jaume
        }
250
251 10914 jaume
252 25750 vcaballero
253
254 10679 jaume
        public void actionPerformed(ActionEvent e) {
255
                JComponent c = (JComponent)e.getSource();
256 13513 caballero
257 10679 jaume
                if (c.equals(chkApplyLabels)) {
258
                        boolean b = chkApplyLabels.isSelected();
259
                        // enables/disables all components
260 25750 vcaballero
                        getCmbStrategy().setEnabled(b);
261 10679 jaume
                        for (int i = 0; i < content.getComponentCount(); i++) {
262
                                Component c1 = content.getComponent(i);
263
                                if (!c1.equals(c))
264
                                        setComponentEnabled(c1, b);
265
                        }
266
267 11747 jaume
                } else if (c.equals(cmbStrategy)) {
268 18623 jdominguez
                        if (c.equals(cmbStrategy)) {
269
                                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
270
                                if (panel!=null) {
271
                                        try {
272
                                                remove(content);
273 25750 vcaballero
274 18623 jdominguez
                                                content.removeAll();
275
                                                content.add((Component) panel);
276
                                                add(content, BorderLayout.CENTER);
277
                                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
278
                                                revalidate();
279
                                                paintImmediately(getBounds());
280
                                        } catch (Exception e1) {
281
                                                e1.printStackTrace();
282
                                        }
283 11747 jaume
                                }
284 10844 jaume
                        }
285 10679 jaume
                }
286
        }
287 25750 vcaballero
288 10679 jaume
        public void acceptAction() {
289
290
        }
291
292
        public void cancelAction() {
293
294
        }
295
296
        public void applyAction() {
297
                if (layer != null) { // in other case the layer is not labelable
298 18623 jdominguez
                        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
299 25750 vcaballero
                        ILabelingStrategy strategy=panel.getLabelingStrategy();
300
                        layer.setLabelingStrategy(strategy);
301 10679 jaume
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
302
                }
303
        }
304
305 25750 vcaballero
306 10679 jaume
        public String getName() {
307
                return PluginServices.getText(this,"Etiquetados");
308
        }
309
}