Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / LabelingManager.java @ 40558

History | View | Annotate | Download (9.54 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 01-jun-2004
26
 *
27
 */
28
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
29
 *
30
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
31
 *
32
 * This program is free software; you can redistribute it and/or
33
 * modify it under the terms of the GNU General Public License
34
 * as published by the Free Software Foundation; either version 2
35
 * of the License, or (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
45
 *
46
 * For more information, contact:
47
 *
48
 *  Generalitat Valenciana
49
 *   Conselleria d'Infraestructures i Transport
50
 *   Av. Blasco Ib??ez, 50
51
 *   46010 VALENCIA
52
 *   SPAIN
53
 *
54
 *      +34 963862235
55
 *   gvsig@gva.es
56
 *      www.gvsig.gva.es
57
 *
58
 *    or
59
 *
60
 *   IVER T.I. S.A
61
 *   Salamanca 50
62
 *   46005 Valencia
63
 *   Spain
64
 *
65
 *   +34 963163400
66
 *   dac@iver.es
67
 */
68
package org.gvsig.app.project.documents.view.legend.gui;
69

    
70
import java.awt.BorderLayout;
71
import java.awt.Component;
72
import java.awt.FlowLayout;
73
import java.awt.event.ActionEvent;
74
import java.awt.event.ActionListener;
75
import java.util.ArrayList;
76
import java.util.Comparator;
77
import java.util.Hashtable;
78
import java.util.Iterator;
79
import java.util.TreeMap;
80

    
81
import javax.swing.BorderFactory;
82
import javax.swing.JCheckBox;
83
import javax.swing.JComponent;
84
import javax.swing.JLabel;
85
import javax.swing.JPanel;
86

    
87
import org.gvsig.andami.PluginServices;
88
import org.gvsig.andami.messages.NotificationManager;
89
import org.gvsig.fmap.mapcontext.layers.FLayer;
90
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
91
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
92
import org.gvsig.utils.swing.JComboBox;
93

    
94

    
95
/**
96
 *
97
 * @author jaume dominguez faus - jaume.dominguez@iver.es
98
 *
99
 */
100
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
101
        private static final long serialVersionUID = 856162295985695717L;
102
        private static ArrayList<Class<? extends ILabelingStrategyPanel>> installedPanels = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
103
        private Comparator<Class<?>> comparator=new Comparator<Class<?>>(){
104

    
105
                public int compare(Class<?> o1, Class<?> o2) {
106
                        return o1.getName().compareTo(o2.getName());
107
                }
108

    
109
        };
110
        private TreeMap<Class<?>, ILabelingStrategyPanel> strategyPanels = new TreeMap<Class<?>, ILabelingStrategyPanel>(comparator);
111
        private JCheckBox chkApplyLabels;
112
        private ILabelable layer;
113
        private JPanel content;
114
        private JPanel pnlNorth;
115
        private JComboBox cmbStrategy;
116

    
117

    
118
        public LabelingManager() {
119
                super();
120
                initialize();
121
        }
122

    
123
        private class LabelingStrategyItem {
124
                private ILabelingStrategyPanel strategyPanel;
125

    
126
                private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
127
                        this.strategyPanel = strategyPanel;
128
                }
129

    
130
                public String toString() {
131
                        return strategyPanel.getLabelingStrategyName();
132
                }
133

    
134
                public boolean equals(Object obj) {
135
                        if (obj instanceof LabelingStrategyItem) {
136
                                LabelingStrategyItem item = (LabelingStrategyItem) obj;
137
                                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
138

    
139
                        }
140
                        return super.equals(obj);
141
                }
142

    
143
        }
144

    
145
        private void initialize() {
146
                setLayout(new BorderLayout());
147
                for (Iterator<Class<? extends ILabelingStrategyPanel>> it = installedPanels.iterator(); it.hasNext();) {
148
                        try {
149
                                ILabelingStrategyPanel pnl = (ILabelingStrategyPanel) it.next().newInstance();
150
                                strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
151
                        } catch (Exception e) {
152
                                /*
153
                                 *  can't happen
154
                                 *  this should never happen since instantiation and access exceptions have been
155
                                 *        controlled in the addLabelingStrategy method
156
                                 */
157
                                NotificationManager.addError(e);
158
                        }
159

    
160
                }
161
                content = new JPanel(new BorderLayout());
162
                content.setBorder(BorderFactory.createEtchedBorder());
163
                add(getPnlNorth(), BorderLayout.NORTH);
164
                add(content, BorderLayout.SOUTH);
165

    
166
        }
167

    
168
        private JPanel getPnlNorth() {
169
                if (pnlNorth == null) {
170
                        pnlNorth = new JPanel(new BorderLayout(5,5));
171
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
172

    
173
                        aux.add(getChkApplyLabels());
174
                        pnlNorth.add(aux, BorderLayout.NORTH);
175
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
176
                        aux.add(new JLabel(PluginServices.getText(this, "general")+":"));
177
                        aux.add(getCmbStrategy());
178
                        pnlNorth.add(aux, BorderLayout.CENTER);
179

    
180
                }
181
                return pnlNorth;
182
        }
183

    
184
        private JComboBox getCmbStrategy() {
185
                if (cmbStrategy == null) {
186
                        Iterator<ILabelingStrategyPanel> it = strategyPanels.values().iterator();
187
                        final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
188
                        while (it.hasNext()) {
189
                                aux.add(new LabelingStrategyItem(it.next()));
190
                        }
191
                        final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
192

    
193
                        cmbStrategy = new JComboBox(items) {
194
                                private static final long serialVersionUID = 7506754097091500846L;
195

    
196
                                @Override
197
                                public void setSelectedItem(Object anObject) {
198
                                        if (anObject == null)
199
                                                return;
200
                                        if (anObject instanceof ILabelingStrategy) {
201
                                                ILabelingStrategy st = (ILabelingStrategy) anObject;
202
                                                for (ILabelingStrategyPanel pnl : strategyPanels.values()) {
203
                                                        if (pnl.getLabelingStrategyClass() != null &&
204
                                                                pnl.getLabelingStrategyClass().equals(st.getClass())) {
205
                                                                super.setSelectedItem(new LabelingStrategyItem(pnl));
206
                                                                return;
207
                                                        }
208
                                                }
209
                                        } else {
210
                                                super.setSelectedItem(anObject);
211
                                        }
212
                                }
213
                        };
214

    
215
                        cmbStrategy.setName("CMBMODE");
216
                        cmbStrategy.addActionListener(this);
217
                }
218

    
219
                return cmbStrategy;
220
        }
221

    
222
        private JCheckBox getChkApplyLabels() {
223
                if (chkApplyLabels == null) {
224
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
225
                        chkApplyLabels.setName("CHKAPPLYLABELS");
226
                        chkApplyLabels.addActionListener(this);
227
                }
228
                return chkApplyLabels;
229
        }
230

    
231
        public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
232
                installedPanels.add(iLabelingStrategyPanelClass);
233
        }
234

    
235
        private void setComponentEnabled(Component c, boolean b) {
236
                c.setEnabled(b);
237
        }
238

    
239

    
240
        public void setModel(FLayer layer) throws IllegalArgumentException {
241
                if (layer instanceof ILabelable) {
242
                        // get the labeling strategy
243
                        this.layer = (ILabelable) layer;
244
                        for (ILabelingStrategyPanel p : strategyPanels.values()) {
245
                                p.setModel(layer,((ILabelable) layer).getLabelingStrategy() );
246
                        }
247

    
248
                        setComponentEnabled(this, true);
249
                        refreshControls();
250

    
251

    
252
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
253
                        evt.setSource(chkApplyLabels);
254
                        actionPerformed(evt);
255

    
256
                        getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
257
                        evt.setSource(getCmbStrategy());
258
                        actionPerformed(evt);
259
                } else {
260
                        setComponentEnabled(this, false);
261
                }
262
        }
263

    
264

    
265

    
266
        private void refreshControls() {
267
                if (layer == null) return;
268

    
269
                // enables labeling
270
                JCheckBox applyLabels = getChkApplyLabels();
271
                applyLabels.setSelected(layer.isLabeled());
272
        }
273

    
274

    
275

    
276

    
277
        public void actionPerformed(ActionEvent e) {
278
                JComponent c = (JComponent)e.getSource();
279

    
280
                if (c.equals(chkApplyLabels)) {
281
                        boolean b = chkApplyLabels.isSelected();
282
                        // enables/disables all components
283
                        getCmbStrategy().setEnabled(b);
284
                        for (int i = 0; i < content.getComponentCount(); i++) {
285
                                Component c1 = content.getComponent(i);
286
                                if (!c1.equals(c))
287
                                        setComponentEnabled(c1, b);
288
                        }
289

    
290
                } else if (c.equals(cmbStrategy)) {
291
                        if (c.equals(cmbStrategy)) {
292
                                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
293
                                if (panel!=null) {
294
                                        try {
295
                                                remove(content);
296

    
297
                                                content.removeAll();
298
                                                content.add((Component) panel);
299
                                                add(content, BorderLayout.CENTER);
300
                                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
301
                                                revalidate();
302
                                                paintImmediately(getBounds());
303
                                        } catch (Exception e1) {
304
                                                e1.printStackTrace();
305
                                        }
306
                                }
307
                        }
308
                }
309
        }
310

    
311
        public void acceptAction() {
312

    
313
        }
314

    
315
        public void cancelAction() {
316

    
317
        }
318

    
319
        public void applyAction() {
320
                if (layer != null) { // in other case the layer is not labelable
321
                        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
322
                        ILabelingStrategy strategy=panel.getLabelingStrategy();
323
                        layer.setLabelingStrategy(strategy);
324
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
325
                }
326
        }
327

    
328

    
329
        public String getName() {
330
                return PluginServices.getText(this,"Etiquetados");
331
        }
332
}