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

History | View | Annotate | Download (10.4 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.JCheckBox;
39
import javax.swing.JComponent;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
42

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

    
52
/**
53
 *
54
 * @author jaume dominguez faus - jaume.dominguez@iver.es
55
 *
56
 */
57
public class LabelingManager
58
    extends AbstractThemeManagerPage
59
    implements ActionListener {
60

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

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

    
69
    };
70
    private TreeMap<Class<?>, ILabelingStrategyPanel> strategyPanels
71
        = new TreeMap<Class<?>, ILabelingStrategyPanel>(comparator);
72

    
73
    private JCheckBox chkApplyLabels;
74
    private ILabelable layer;
75
    private JPanel content;
76
    private JPanel pnlNorth;
77
    private JComboBox cmbStrategy;
78

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

    
84
    private class LabelingStrategyItem {
85

    
86
        private ILabelingStrategyPanel strategyPanel;
87

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

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

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

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

    
105
    }
106

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

    
112
    private void initialize() {
113
        setLayout(new BorderLayout());
114
        SymbologySwingManager symbologySwingManager = SymbologySwingLocator.getSwingManager();
115

    
116
        Iterator<ILabelingStrategyPanel> it = symbologySwingManager.getLabelingEditors().iterator();
117
        while( it.hasNext() ) {
118
            ILabelingStrategyPanel pnl = it.next();
119
            strategyPanels.put(pnl.getLabelingStrategyClass(), pnl);
120
        }
121
        content = new JPanel(new BorderLayout());
122
        content.setBorder(BorderFactory.createEtchedBorder());
123
        add(getPnlNorth(), BorderLayout.NORTH);
124
        add(content, BorderLayout.SOUTH);
125

    
126
    }
127

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

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

    
140
        }
141
        return pnlNorth;
142
    }
143

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

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

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

    
176
            cmbStrategy.setName("CMBMODE");
177
            cmbStrategy.addActionListener(this);
178
        }
179

    
180
        return cmbStrategy;
181
    }
182

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

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

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

    
205
    public void setModel(FLayer layer) throws IllegalArgumentException {
206
        if( !layer.isAvailable() ) {
207
            setComponentEnabled(this, false);
208
            getChkApplyLabels().setSelected(false);
209
            getChkApplyLabels().setEnabled(false);
210
            getCmbStrategy().setEnabled(false);
211
            return;
212
        }
213
        if( layer instanceof ILabelable ) {
214
            // get the labeling strategy
215
            this.layer = (ILabelable) layer;
216
            getChkApplyLabels().setEnabled(true);
217
            getCmbStrategy().setEnabled(true);
218
            for( ILabelingStrategyPanel p : strategyPanels.values() ) {
219
                p.setModel(layer, ((ILabelable) layer).getLabelingStrategy());
220
            }
221

    
222
            setComponentEnabled(this, true);
223
            refreshControls();
224

    
225
            ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
226
            evt.setSource(chkApplyLabels);
227
            actionPerformed(evt);
228

    
229
            getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
230
            evt.setSource(getCmbStrategy());
231
            actionPerformed(evt);
232
        } else {
233
            setComponentEnabled(this, false);
234
        }
235
    }
236

    
237
    private void refreshControls() {
238
        if( layer == null ) {
239
            return;
240
        }
241

    
242
        // enables labeling
243
        JCheckBox applyLabels = getChkApplyLabels();
244
        applyLabels.setSelected(layer.isLabeled());
245
    }
246

    
247
    public void actionPerformed(ActionEvent e) {
248
        JComponent c = (JComponent) e.getSource();
249

    
250
        if( c.equals(chkApplyLabels) ) {
251
            boolean b = chkApplyLabels.isSelected();
252
            // enables/disables all components
253
            getCmbStrategy().setEnabled(b);
254
            for( int i = 0; i < content.getComponentCount(); i++ ) {
255
                Component c1 = content.getComponent(i);
256
                if( !c1.equals(c) ) {
257
                    setComponentEnabled(c1, b);
258
                }
259
            }
260

    
261
        } else if( c.equals(cmbStrategy) ) {
262
            if( c.equals(cmbStrategy) ) {
263
                ILabelingStrategyPanel panel = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategyPanel;
264
                if( panel != null ) {
265
                    try {
266
                        remove(content);
267

    
268
                        content.removeAll();
269
                        content.add((Component) panel);
270
                        add(content, BorderLayout.CENTER);
271
                        actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
272
                        revalidate();
273
                        paintImmediately(getBounds());
274
                    } catch (Exception e1) {
275
                        e1.printStackTrace();
276
                    }
277
                }
278
            }
279
        }
280
    }
281

    
282
    public void acceptAction() {
283
        applyAction();
284
    }
285

    
286
    public void cancelAction() {
287

    
288
    }
289

    
290
    public void applyAction() {
291
        if( layer != null ) { // in other case the layer is not labelable
292
            ILabelingStrategyPanel panel = ((LabelingStrategyItem) getCmbStrategy().getSelectedItem()).strategyPanel;
293
            ILabelingStrategy strategy = panel.getLabelingStrategy();
294
            layer.setLabelingStrategy(strategy);
295
            layer.setIsLabeled(getChkApplyLabels().isSelected());
296
        }
297
    }
298

    
299
    public String getName() {
300
        return PluginServices.getText(this, "Etiquetados");
301
    }
302
}