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 @ 40596

History | View | Annotate | Download (8.32 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
package org.gvsig.app.project.documents.view.legend.gui;
26

    
27
import java.awt.BorderLayout;
28
import java.awt.Component;
29
import java.awt.FlowLayout;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.ArrayList;
33
import java.util.Comparator;
34
import java.util.Hashtable;
35
import java.util.Iterator;
36
import java.util.TreeMap;
37

    
38
import javax.swing.BorderFactory;
39
import javax.swing.JCheckBox;
40
import javax.swing.JComponent;
41
import javax.swing.JLabel;
42
import javax.swing.JPanel;
43

    
44
import org.gvsig.andami.PluginServices;
45
import org.gvsig.andami.messages.NotificationManager;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
48
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
49
import org.gvsig.utils.swing.JComboBox;
50

    
51

    
52
/**
53
 *
54
 * @author jaume dominguez faus - jaume.dominguez@iver.es
55
 *
56
 */
57
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
58
        private static final long serialVersionUID = 856162295985695717L;
59
        private static ArrayList<Class<? extends ILabelingStrategyPanel>> installedPanels = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
60
        private Comparator<Class<?>> comparator=new Comparator<Class<?>>(){
61

    
62
                public int compare(Class<?> o1, Class<?> o2) {
63
                        return o1.getName().compareTo(o2.getName());
64
                }
65

    
66
        };
67
        private TreeMap<Class<?>, ILabelingStrategyPanel> strategyPanels = new TreeMap<Class<?>, ILabelingStrategyPanel>(comparator);
68
        private JCheckBox chkApplyLabels;
69
        private ILabelable layer;
70
        private JPanel content;
71
        private JPanel pnlNorth;
72
        private JComboBox cmbStrategy;
73

    
74

    
75
        public LabelingManager() {
76
                super();
77
                initialize();
78
        }
79

    
80
        private class LabelingStrategyItem {
81
                private ILabelingStrategyPanel strategyPanel;
82

    
83
                private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
84
                        this.strategyPanel = strategyPanel;
85
                }
86

    
87
                public String toString() {
88
                        return strategyPanel.getLabelingStrategyName();
89
                }
90

    
91
                public boolean equals(Object obj) {
92
                        if (obj instanceof LabelingStrategyItem) {
93
                                LabelingStrategyItem item = (LabelingStrategyItem) obj;
94
                                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
95

    
96
                        }
97
                        return super.equals(obj);
98
                }
99

    
100
        }
101

    
102
        private void initialize() {
103
                setLayout(new BorderLayout());
104
                for (Iterator<Class<? extends ILabelingStrategyPanel>> it = installedPanels.iterator(); it.hasNext();) {
105
                        try {
106
                                ILabelingStrategyPanel pnl = (ILabelingStrategyPanel) it.next().newInstance();
107
                                strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
108
                        } catch (Exception e) {
109
                                /*
110
                                 *  can't happen
111
                                 *  this should never happen since instantiation and access exceptions have been
112
                                 *        controlled in the addLabelingStrategy method
113
                                 */
114
                                NotificationManager.addError(e);
115
                        }
116

    
117
                }
118
                content = new JPanel(new BorderLayout());
119
                content.setBorder(BorderFactory.createEtchedBorder());
120
                add(getPnlNorth(), BorderLayout.NORTH);
121
                add(content, BorderLayout.SOUTH);
122

    
123
        }
124

    
125
        private JPanel getPnlNorth() {
126
                if (pnlNorth == null) {
127
                        pnlNorth = new JPanel(new BorderLayout(5,5));
128
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
129

    
130
                        aux.add(getChkApplyLabels());
131
                        pnlNorth.add(aux, BorderLayout.NORTH);
132
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
133
                        aux.add(new JLabel(PluginServices.getText(this, "general")+":"));
134
                        aux.add(getCmbStrategy());
135
                        pnlNorth.add(aux, BorderLayout.CENTER);
136

    
137
                }
138
                return pnlNorth;
139
        }
140

    
141
        private JComboBox getCmbStrategy() {
142
                if (cmbStrategy == null) {
143
                        Iterator<ILabelingStrategyPanel> it = strategyPanels.values().iterator();
144
                        final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
145
                        while (it.hasNext()) {
146
                                aux.add(new LabelingStrategyItem(it.next()));
147
                        }
148
                        final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
149

    
150
                        cmbStrategy = new JComboBox(items) {
151
                                private static final long serialVersionUID = 7506754097091500846L;
152

    
153
                                @Override
154
                                public void setSelectedItem(Object anObject) {
155
                                        if (anObject == null)
156
                                                return;
157
                                        if (anObject instanceof ILabelingStrategy) {
158
                                                ILabelingStrategy st = (ILabelingStrategy) anObject;
159
                                                for (ILabelingStrategyPanel pnl : strategyPanels.values()) {
160
                                                        if (pnl.getLabelingStrategyClass() != null &&
161
                                                                pnl.getLabelingStrategyClass().equals(st.getClass())) {
162
                                                                super.setSelectedItem(new LabelingStrategyItem(pnl));
163
                                                                return;
164
                                                        }
165
                                                }
166
                                        } else {
167
                                                super.setSelectedItem(anObject);
168
                                        }
169
                                }
170
                        };
171

    
172
                        cmbStrategy.setName("CMBMODE");
173
                        cmbStrategy.addActionListener(this);
174
                }
175

    
176
                return cmbStrategy;
177
        }
178

    
179
        private JCheckBox getChkApplyLabels() {
180
                if (chkApplyLabels == null) {
181
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
182
                        chkApplyLabels.setName("CHKAPPLYLABELS");
183
                        chkApplyLabels.addActionListener(this);
184
                }
185
                return chkApplyLabels;
186
        }
187

    
188
        public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
189
                installedPanels.add(iLabelingStrategyPanelClass);
190
        }
191

    
192
        private void setComponentEnabled(Component c, boolean b) {
193
                c.setEnabled(b);
194
        }
195

    
196

    
197
        public void setModel(FLayer layer) throws IllegalArgumentException {
198
                if (layer instanceof ILabelable) {
199
                        // get the labeling strategy
200
                        this.layer = (ILabelable) layer;
201
                        for (ILabelingStrategyPanel p : strategyPanels.values()) {
202
                                p.setModel(layer,((ILabelable) layer).getLabelingStrategy() );
203
                        }
204

    
205
                        setComponentEnabled(this, true);
206
                        refreshControls();
207

    
208

    
209
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
210
                        evt.setSource(chkApplyLabels);
211
                        actionPerformed(evt);
212

    
213
                        getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
214
                        evt.setSource(getCmbStrategy());
215
                        actionPerformed(evt);
216
                } else {
217
                        setComponentEnabled(this, false);
218
                }
219
        }
220

    
221

    
222

    
223
        private void refreshControls() {
224
                if (layer == null) return;
225

    
226
                // enables labeling
227
                JCheckBox applyLabels = getChkApplyLabels();
228
                applyLabels.setSelected(layer.isLabeled());
229
        }
230

    
231

    
232

    
233

    
234
        public void actionPerformed(ActionEvent e) {
235
                JComponent c = (JComponent)e.getSource();
236

    
237
                if (c.equals(chkApplyLabels)) {
238
                        boolean b = chkApplyLabels.isSelected();
239
                        // enables/disables all components
240
                        getCmbStrategy().setEnabled(b);
241
                        for (int i = 0; i < content.getComponentCount(); i++) {
242
                                Component c1 = content.getComponent(i);
243
                                if (!c1.equals(c))
244
                                        setComponentEnabled(c1, b);
245
                        }
246

    
247
                } else if (c.equals(cmbStrategy)) {
248
                        if (c.equals(cmbStrategy)) {
249
                                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
250
                                if (panel!=null) {
251
                                        try {
252
                                                remove(content);
253

    
254
                                                content.removeAll();
255
                                                content.add((Component) panel);
256
                                                add(content, BorderLayout.CENTER);
257
                                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
258
                                                revalidate();
259
                                                paintImmediately(getBounds());
260
                                        } catch (Exception e1) {
261
                                                e1.printStackTrace();
262
                                        }
263
                                }
264
                        }
265
                }
266
        }
267

    
268
        public void acceptAction() {
269

    
270
        }
271

    
272
        public void cancelAction() {
273

    
274
        }
275

    
276
        public void applyAction() {
277
                if (layer != null) { // in other case the layer is not labelable
278
                        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
279
                        ILabelingStrategy strategy=panel.getLabelingStrategy();
280
                        layer.setLabelingStrategy(strategy);
281
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
282
                }
283
        }
284

    
285

    
286
        public String getName() {
287
                return PluginServices.getText(this,"Etiquetados");
288
        }
289
}