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

History | View | Annotate | Download (8.62 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.Iterator;
35
import java.util.List;
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.symbology.swing.SymbologySwingLocator;
50
import org.gvsig.symbology.swing.SymbologySwingManager;
51
import org.gvsig.utils.swing.JComboBox;
52

    
53

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

    
64
                public int compare(Class<?> o1, Class<?> o2) {
65
                        return o1.getSimpleName().compareTo(o2.getSimpleName());
66
                }
67

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

    
78

    
79
        public LabelingManager() {
80
                super();
81
                initialize();
82
        }
83

    
84
        private class LabelingStrategyItem {
85
                private ILabelingStrategyPanel strategyPanel;
86

    
87
                private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
88
                        this.strategyPanel = strategyPanel;
89
                }
90

    
91
                public String toString() {
92
                        return strategyPanel.getLabelingStrategyName();
93
                }
94

    
95
                public boolean equals(Object obj) {
96
                        if (obj instanceof LabelingStrategyItem) {
97
                                LabelingStrategyItem item = (LabelingStrategyItem) obj;
98
                                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
99

    
100
                        }
101
                        return super.equals(obj);
102
                }
103

    
104
        }
105
        
106
         @Override
107
            public int getPriority() {
108
                    return 600;
109
            }
110

    
111
        private void initialize() {
112
                setLayout(new BorderLayout());
113
                SymbologySwingManager symbologySwingManager = SymbologySwingLocator.getSwingManager();
114
    
115
                Iterator<ILabelingStrategyPanel> it = symbologySwingManager.getLabelingEditors().iterator();
116
                while( it.hasNext() ) {
117
                    ILabelingStrategyPanel pnl = it.next();
118
                    strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
119
                }
120
                content = new JPanel(new BorderLayout());
121
                content.setBorder(BorderFactory.createEtchedBorder());
122
                add(getPnlNorth(), BorderLayout.NORTH);
123
                add(content, BorderLayout.SOUTH);
124

    
125
        }
126

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

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

    
139
                }
140
                return pnlNorth;
141
        }
142

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

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

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

    
174
                        cmbStrategy.setName("CMBMODE");
175
                        cmbStrategy.addActionListener(this);
176
                }
177

    
178
                return cmbStrategy;
179
        }
180

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

    
190
        /**
191
         * 
192
         * @deprecated use {#SymbolSwingManger.
193
         */
194
        public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
195
            SymbologySwingManager symbologySwingManager = SymbologySwingLocator.getSwingManager();
196
            symbologySwingManager.registerLabelingEditor(iLabelingStrategyPanelClass);
197
        }
198

    
199
        private void setComponentEnabled(Component c, boolean b) {
200
                c.setEnabled(b);
201
        }
202

    
203

    
204
        public void setModel(FLayer layer) throws IllegalArgumentException {
205
                if (layer instanceof ILabelable) {
206
                        // get the labeling strategy
207
                        this.layer = (ILabelable) layer;
208
                        for (ILabelingStrategyPanel p : strategyPanels.values()) {
209
                                p.setModel(layer,((ILabelable) layer).getLabelingStrategy() );
210
                        }
211

    
212
                        setComponentEnabled(this, true);
213
                        refreshControls();
214

    
215

    
216
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
217
                        evt.setSource(chkApplyLabels);
218
                        actionPerformed(evt);
219

    
220
                        getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
221
                        evt.setSource(getCmbStrategy());
222
                        actionPerformed(evt);
223
                } else {
224
                        setComponentEnabled(this, false);
225
                }
226
        }
227

    
228

    
229

    
230
        private void refreshControls() {
231
                if (layer == null) return;
232

    
233
                // enables labeling
234
                JCheckBox applyLabels = getChkApplyLabels();
235
                applyLabels.setSelected(layer.isLabeled());
236
        }
237

    
238

    
239

    
240

    
241
        public void actionPerformed(ActionEvent e) {
242
                JComponent c = (JComponent)e.getSource();
243

    
244
                if (c.equals(chkApplyLabels)) {
245
                        boolean b = chkApplyLabels.isSelected();
246
                        // enables/disables all components
247
                        getCmbStrategy().setEnabled(b);
248
                        for (int i = 0; i < content.getComponentCount(); i++) {
249
                                Component c1 = content.getComponent(i);
250
                                if (!c1.equals(c))
251
                                        setComponentEnabled(c1, b);
252
                        }
253

    
254
                } else if (c.equals(cmbStrategy)) {
255
                        if (c.equals(cmbStrategy)) {
256
                                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
257
                                if (panel!=null) {
258
                                        try {
259
                                                remove(content);
260

    
261
                                                content.removeAll();
262
                                                content.add((Component) panel);
263
                                                add(content, BorderLayout.CENTER);
264
                                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
265
                                                revalidate();
266
                                                paintImmediately(getBounds());
267
                                        } catch (Exception e1) {
268
                                                e1.printStackTrace();
269
                                        }
270
                                }
271
                        }
272
                }
273
        }
274

    
275
        public void acceptAction() {
276

    
277
        }
278

    
279
        public void cancelAction() {
280

    
281
        }
282

    
283
        public void applyAction() {
284
                if (layer != null) { // in other case the layer is not labelable
285
                        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
286
                        ILabelingStrategy strategy=panel.getLabelingStrategy();
287
                        layer.setLabelingStrategy(strategy);
288
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
289
                }
290
        }
291

    
292

    
293
        public String getName() {
294
                return PluginServices.getText(this,"Etiquetados");
295
        }
296
}