Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / LabelingManager.java @ 43888

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

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

    
37
import javax.swing.BorderFactory;
38
import javax.swing.ComboBoxModel;
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.configurableactions.ConfigurableActionsMamager;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
49
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
50
import org.gvsig.symbology.swing.SymbologySwingLocator;
51
import org.gvsig.symbology.swing.SymbologySwingManager;
52
import org.gvsig.tools.util.ToolsUtilLocator;
53
import org.gvsig.utils.swing.JComboBox;
54

    
55
/**
56
 *
57
 * @author jaume dominguez faus - jaume.dominguez@iver.es
58
 *
59
 */
60
public class LabelingManager
61
    extends AbstractThemeManagerPage
62
    implements ActionListener, LabelingPanel {
63

    
64
    private static final long serialVersionUID = 856162295985695717L;
65
//        private static ArrayList<Class<? extends ILabelingStrategyPanel>> installedPanels = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
66
    private Comparator<Class<?>> comparator = new Comparator<Class<?>>() {
67

    
68
        public int compare(Class<?> o1, Class<?> o2) {
69
            return o1.getSimpleName().compareTo(o2.getSimpleName());
70
        }
71

    
72
    };
73
    private TreeMap<Class<?>, ILabelingStrategyPanel> strategyPanels
74
        = new TreeMap<Class<?>, ILabelingStrategyPanel>(comparator);
75

    
76
    private JCheckBox chkApplyLabels;
77
    private ILabelable layer;
78
    private JPanel content;
79
    private JPanel pnlNorth;
80
    private JComboBox cmbStrategy;
81

    
82
    public LabelingManager() {
83
        super();
84
        initialize();
85
    }
86

    
87
    @Override
88
    public boolean isLabelingEnabled() {
89
        return getChkApplyLabels().isSelected();
90
    }
91

    
92
    @Override
93
    public void setLabelingEnabled(boolean enabled) {
94
        getChkApplyLabels().setSelected(enabled);
95
        getCmbStrategy().setEnabled(enabled);
96
        ILabelingStrategyPanel panel = getCurrentLabelingStrategy();
97
        ((Component)panel).setEnabled(enabled);
98
    }
99

    
100
    @Override
101
    public void setCurrentLabelingStrategy(ILabelingStrategyPanel panel) {
102
        ComboBoxModel model = getCmbStrategy().getModel();
103
        model.setSelectedItem(panel);
104
    }
105
  
106
    @Override
107
    public ILabelingStrategyPanel getCurrentLabelingStrategy() {
108
        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
109
        return panel;
110
    }
111
  
112
    @Override
113
    public FLayer getLayer() {
114
        return (FLayer) this.layer;
115
    }
116

    
117
    @Override
118
    public void setLayer(FLayer layer) {
119
        this.setModel(layer);
120
    }
121

    
122
    @Override
123
    public ILabelingStrategy getLabelingStrategy() {
124
        ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
125
        ILabelingStrategy strategy = panel.getLabelingStrategy();
126
        return strategy;
127
    }
128

    
129
    
130
    private class LabelingStrategyItem {
131

    
132
        private ILabelingStrategyPanel strategyPanel;
133

    
134
        private LabelingStrategyItem(ILabelingStrategyPanel strategyPanel) {
135
            this.strategyPanel = strategyPanel;
136
        }
137

    
138
        public String toString() {
139
            return strategyPanel.getLabelingStrategyName();
140
        }
141

    
142
        public boolean equals(Object obj) {
143
            if( obj instanceof LabelingStrategyItem ) {
144
                LabelingStrategyItem item = (LabelingStrategyItem) obj;
145
                return this.strategyPanel.getClass().equals(item.strategyPanel.getClass());
146

    
147
            }
148
            return super.equals(obj);
149
        }
150

    
151
    }
152

    
153
    @Override
154
    public int getPriority() {
155
        return 600;
156
    }
157

    
158
    private void initialize() {
159
        setLayout(new BorderLayout());
160
        SymbologySwingManager symbologySwingManager = SymbologySwingLocator.getSwingManager();
161

    
162
        Iterator<ILabelingStrategyPanel> it = symbologySwingManager.getLabelingEditors().iterator();
163
        while( it.hasNext() ) {
164
            ILabelingStrategyPanel pnl = it.next();
165
            strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
166
        }
167
        content = new JPanel(new BorderLayout());
168
        content.setBorder(BorderFactory.createEtchedBorder());
169
        add(getPnlNorth(), BorderLayout.NORTH);
170
        add(content, BorderLayout.SOUTH);
171

    
172
    }
173

    
174
    private JPanel getPnlNorth() {
175
        if( pnlNorth == null ) {
176
            pnlNorth = new JPanel(new BorderLayout(5, 5));
177
            JPanel aux = new JPanel(new BorderLayout());
178

    
179
            aux.add(getChkApplyLabels(), BorderLayout.LINE_START);
180
            
181
            ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager();
182
            JComponent c = cfgActionsManager.getConfigurableActionsComponent("labelingPropertiesPage", this);
183
            aux.add(c, BorderLayout.LINE_END);
184
            
185
            pnlNorth.add(aux, BorderLayout.NORTH);
186
            
187
            aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
188
            aux.add(new JLabel(PluginServices.getText(this, "general") + ":"));
189
            aux.add(getCmbStrategy());
190
            pnlNorth.add(aux, BorderLayout.CENTER);
191

    
192
        }
193
        return pnlNorth;
194
    }
195

    
196
    private JComboBox getCmbStrategy() {
197
        if( cmbStrategy == null ) {
198
            Iterator<ILabelingStrategyPanel> it = strategyPanels.values().iterator();
199
            final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
200
            while( it.hasNext() ) {
201
                aux.add(new LabelingStrategyItem(it.next()));
202
            }
203
            final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
204

    
205
            cmbStrategy = new JComboBox(items) {
206
                private static final long serialVersionUID = 7506754097091500846L;
207

    
208
                @Override
209
                public void setSelectedItem(Object anObject) {
210
                    if( anObject == null ) {
211
                        return;
212
                    }
213
                    if( anObject instanceof ILabelingStrategy ) {
214
                        ILabelingStrategy st = (ILabelingStrategy) anObject;
215
                        for( ILabelingStrategyPanel pnl : strategyPanels.values() ) {
216
                            if( pnl.getLabelingStrategyClass() != null
217
                                && pnl.getLabelingStrategyClass().equals(st.getClass()) ) {
218
                                super.setSelectedItem(new LabelingStrategyItem(pnl));
219
                                return;
220
                            }
221
                        }
222
                    } else {
223
                        super.setSelectedItem(anObject);
224
                    }
225
                }
226
            };
227

    
228
            cmbStrategy.setName("CMBMODE");
229
            cmbStrategy.addActionListener(this);
230
        }
231

    
232
        return cmbStrategy;
233
    }
234

    
235
    private JCheckBox getChkApplyLabels() {
236
        if( chkApplyLabels == null ) {
237
            chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
238
            chkApplyLabels.setName("CHKAPPLYLABELS");
239
            chkApplyLabels.addActionListener(this);
240
        }
241
        return chkApplyLabels;
242
    }
243

    
244
    /**
245
     *
246
     * @deprecated use {#SymbolSwingManger.
247
     */
248
    public static void addLabelingStrategy(Class<? extends ILabelingStrategyPanel> iLabelingStrategyPanelClass) {
249
        SymbologySwingManager symbologySwingManager = SymbologySwingLocator.getSwingManager();
250
        symbologySwingManager.registerLabelingEditor(iLabelingStrategyPanelClass);
251
    }
252

    
253
    private void setComponentEnabled(Component c, boolean b) {
254
        c.setEnabled(b);
255
    }
256

    
257
    public void setModel(FLayer layer) throws IllegalArgumentException {
258
        if( !layer.isAvailable() ) {
259
            setComponentEnabled(this, false);
260
            getChkApplyLabels().setSelected(false);
261
            getChkApplyLabels().setEnabled(false);
262
            getCmbStrategy().setEnabled(false);
263
            return;
264
        }
265
        if( layer instanceof ILabelable ) {
266
            // get the labeling strategy
267
            this.layer = (ILabelable) layer;
268
            getChkApplyLabels().setEnabled(true);
269
            getCmbStrategy().setEnabled(true);
270
            for( ILabelingStrategyPanel p : strategyPanels.values() ) {
271
                p.setModel(layer, ((ILabelable) layer).getLabelingStrategy());
272
            }
273

    
274
            setComponentEnabled(this, true);
275
            refreshControls();
276

    
277
            ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
278
            evt.setSource(chkApplyLabels);
279
            actionPerformed(evt);
280

    
281
            getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
282
            evt.setSource(getCmbStrategy());
283
            actionPerformed(evt);        
284
        } else {
285
            setComponentEnabled(this, false);
286
        }
287
    }
288

    
289
    private void refreshControls() {
290
        if( layer == null ) {
291
            return;
292
        }
293

    
294
        // enables labeling
295
        JCheckBox applyLabels = getChkApplyLabels();
296
        applyLabels.setSelected(layer.isLabeled());
297
    }
298

    
299
    public void actionPerformed(ActionEvent e) {
300
        JComponent c = (JComponent) e.getSource();
301

    
302
        if( c.equals(chkApplyLabels) ) {
303
            boolean b = chkApplyLabels.isSelected();
304
            // enables/disables all components
305
            getCmbStrategy().setEnabled(b);
306
            for( int i = 0; i < content.getComponentCount(); i++ ) {
307
                Component c1 = content.getComponent(i);
308
                if( !c1.equals(c) ) {
309
                    setComponentEnabled(c1, b);
310
                }
311
            }
312

    
313
        } else if( c.equals(cmbStrategy) ) {
314
            if( c.equals(cmbStrategy) ) {
315
                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
316
                if( panel != null ) {
317
                    try {
318
                        remove(content);
319

    
320
                        content.removeAll();
321
                        content.add((Component) panel);
322
                        add(content, BorderLayout.CENTER);
323
                        actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
324
                        revalidate();
325
                        paintImmediately(getBounds());
326
                    } catch (Exception e1) {
327
                        e1.printStackTrace();
328
                    }
329
                }
330
            }
331
        }
332
    }
333
  
334
    public void acceptAction() {
335
        applyAction();
336
    }
337

    
338
    public void cancelAction() {
339

    
340
    }
341

    
342
    public void applyAction() {
343
        if( layer != null ) { // in other case the layer is not labelable
344
            ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
345
            ILabelingStrategy strategy = panel.getLabelingStrategy();
346
            layer.setLabelingStrategy(strategy);
347
            layer.setIsLabeled(getChkApplyLabels().isSelected());
348
        }
349
    }
350

    
351
    public String getName() {
352
        return PluginServices.getText(this, "Etiquetados");
353
    }
354
}