Statistics
| Revision:

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

History | View | Annotate | Download (8.6 KB)

1
/*
2
 * Created on 01-jun-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
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
 *   Av. Blasco Ib??ez, 50
28
 *   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
package org.gvsig.app.project.documents.view.legend.gui;
46

    
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
import java.util.Comparator;
54
import java.util.Hashtable;
55
import java.util.Iterator;
56
import java.util.TreeMap;
57

    
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
import org.gvsig.andami.PluginServices;
65
import org.gvsig.andami.messages.NotificationManager;
66
import org.gvsig.fmap.mapcontext.layers.FLayer;
67
import org.gvsig.fmap.mapcontext.layers.operations.ILabelable;
68
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
69
import org.gvsig.utils.swing.JComboBox;
70

    
71

    
72
/**
73
 *
74
 * @author jaume dominguez faus - jaume.dominguez@iver.es
75
 *
76
 */
77
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
78
        private static final long serialVersionUID = 856162295985695717L;
79
        private static ArrayList<Class<? extends ILabelingStrategyPanel>> installedPanels = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
80
        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
        private JCheckBox chkApplyLabels;
89
        private ILabelable layer;
90
        private JPanel content;
91
        private JPanel pnlNorth;
92
        private JComboBox cmbStrategy;
93

    
94

    
95
        public LabelingManager() {
96
                super();
97
                initialize();
98
        }
99

    
100
        private class LabelingStrategyItem {
101
                private ILabelingStrategyPanel strategyPanel;
102

    
103
                private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
104
                        this.strategyPanel = strategyPanel;
105
                }
106

    
107
                public String toString() {
108
                        return strategyPanel.getLabelingStrategyName();
109
                }
110

    
111
                public boolean equals(Object obj) {
112
                        if (obj instanceof LabelingStrategyItem) {
113
                                LabelingStrategyItem item = (LabelingStrategyItem) obj;
114
                                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
115

    
116
                        }
117
                        return super.equals(obj);
118
                }
119

    
120
        }
121

    
122
        private void initialize() {
123
                setLayout(new BorderLayout());
124
                for (Iterator<Class<? extends ILabelingStrategyPanel>> it = installedPanels.iterator(); it.hasNext();) {
125
                        try {
126
                                ILabelingStrategyPanel pnl = (ILabelingStrategyPanel) it.next().newInstance();
127
                                strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
128
                        } catch (Exception e) {
129
                                /*
130
                                 *  can't happen
131
                                 *  this should never happen since instantiation and access exceptions have been
132
                                 *        controlled in the addLabelingStrategy method
133
                                 */
134
                                NotificationManager.addError(e);
135
                        }
136

    
137
                }
138
                content = new JPanel(new BorderLayout());
139
                content.setBorder(BorderFactory.createEtchedBorder());
140
                add(getPnlNorth(), BorderLayout.NORTH);
141
                add(content, BorderLayout.SOUTH);
142

    
143
        }
144

    
145
        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

    
150
                        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
                        aux.add(getCmbStrategy());
155
                        pnlNorth.add(aux, BorderLayout.CENTER);
156

    
157
                }
158
                return pnlNorth;
159
        }
160

    
161
        private JComboBox getCmbStrategy() {
162
                if (cmbStrategy == null) {
163
                        Iterator<ILabelingStrategyPanel> it = strategyPanels.values().iterator();
164
                        final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
165
                        while (it.hasNext()) {
166
                                aux.add(new LabelingStrategyItem(it.next()));
167
                        }
168
                        final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
169

    
170
                        cmbStrategy = new JComboBox(items) {
171
                                private static final long serialVersionUID = 7506754097091500846L;
172

    
173
                                @Override
174
                                public void setSelectedItem(Object anObject) {
175
                                        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
                                                                return;
184
                                                        }
185
                                                }
186
                                        } else {
187
                                                super.setSelectedItem(anObject);
188
                                        }
189
                                }
190
                        };
191

    
192
                        cmbStrategy.setName("CMBMODE");
193
                        cmbStrategy.addActionListener(this);
194
                }
195

    
196
                return cmbStrategy;
197
        }
198

    
199
        private JCheckBox getChkApplyLabels() {
200
                if (chkApplyLabels == null) {
201
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
202
                        chkApplyLabels.setName("CHKAPPLYLABELS");
203
                        chkApplyLabels.addActionListener(this);
204
                }
205
                return chkApplyLabels;
206
        }
207

    
208
        public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
209
                installedPanels.add(iLabelingStrategyPanelClass);
210
        }
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
                        this.layer = (ILabelable) layer;
221
                        for (ILabelingStrategyPanel p : strategyPanels.values()) {
222
                                p.setModel(layer,((ILabelable) layer).getLabelingStrategy() );
223
                        }
224

    
225
                        setComponentEnabled(this, true);
226
                        refreshControls();
227

    
228

    
229
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
230
                        evt.setSource(chkApplyLabels);
231
                        actionPerformed(evt);
232

    
233
                        getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
234
                        evt.setSource(getCmbStrategy());
235
                        actionPerformed(evt);
236
                } else {
237
                        setComponentEnabled(this, false);
238
                }
239
        }
240

    
241

    
242

    
243
        private void refreshControls() {
244
                if (layer == null) return;
245

    
246
                // enables labeling
247
                JCheckBox applyLabels = getChkApplyLabels();
248
                applyLabels.setSelected(layer.isLabeled());
249
        }
250

    
251

    
252

    
253

    
254
        public void actionPerformed(ActionEvent e) {
255
                JComponent c = (JComponent)e.getSource();
256

    
257
                if (c.equals(chkApplyLabels)) {
258
                        boolean b = chkApplyLabels.isSelected();
259
                        // enables/disables all components
260
                        getCmbStrategy().setEnabled(b);
261
                        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
                } else if (c.equals(cmbStrategy)) {
268
                        if (c.equals(cmbStrategy)) {
269
                                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
270
                                if (panel!=null) {
271
                                        try {
272
                                                remove(content);
273

    
274
                                                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
                                }
284
                        }
285
                }
286
        }
287

    
288
        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
                        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
299
                        ILabelingStrategy strategy=panel.getLabelingStrategy();
300
                        layer.setLabelingStrategy(strategy);
301
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
302
                }
303
        }
304

    
305

    
306
        public String getName() {
307
                return PluginServices.getText(this,"Etiquetados");
308
        }
309
}