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

View differences:

LabelingManager.java
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24

  
25 24
package org.gvsig.app.project.documents.view.legend.gui;
26 25

  
27 26
import java.awt.BorderLayout;
......
50 49
import org.gvsig.symbology.swing.SymbologySwingManager;
51 50
import org.gvsig.utils.swing.JComboBox;
52 51

  
53

  
54 52
/**
55 53
 *
56 54
 * @author jaume dominguez faus - jaume.dominguez@iver.es
57 55
 *
58 56
 */
59
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
60
	private static final long serialVersionUID = 856162295985695717L;
57
public class LabelingManager
58
    extends AbstractThemeManagerPage
59
    implements ActionListener {
60

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

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

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

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

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

  
79
	public LabelingManager() {
80
		super();
81
		initialize();
82
	}
84
    private class LabelingStrategyItem {
83 85

  
84
	private class LabelingStrategyItem {
85
		private ILabelingStrategyPanel strategyPanel;
86
        private ILabelingStrategyPanel strategyPanel;
86 87

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

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

  
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());
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());
99 100

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

  
104
	}
105
    }
105 106

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

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

  
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);
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);
124 125

  
125
	}
126
    }
126 127

  
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));
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));
131 132

  
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);
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);
138 139

  
139
		}
140
		return pnlNorth;
141
	}
140
        }
141
        return pnlNorth;
142
    }
142 143

  
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()]);
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()]);
151 152

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

  
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
			};
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
            };
173 175

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

  
178
		return cmbStrategy;
179
	}
180
        return cmbStrategy;
181
    }
180 182

  
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
	}
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
    }
189 191

  
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
	}
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
    }
198 200

  
199
	private void setComponentEnabled(Component c, boolean b) {
200
		c.setEnabled(b);
201
	}
201
    private void setComponentEnabled(Component c, boolean b) {
202
        c.setEnabled(b);
203
    }
202 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
            }
203 221

  
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
			}
222
            setComponentEnabled(this, true);
223
            refreshControls();
211 224

  
212
			setComponentEnabled(this, true);
213
			refreshControls();
225
            ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
226
            evt.setSource(chkApplyLabels);
227
            actionPerformed(evt);
214 228

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

  
216
			ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
217
			evt.setSource(chkApplyLabels);
218
			actionPerformed(evt);
237
    private void refreshControls() {
238
        if( layer == null ) {
239
            return;
240
        }
219 241

  
220
			getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
221
			evt.setSource(getCmbStrategy());
222
			actionPerformed(evt);
223
		} else {
224
			setComponentEnabled(this, false);
225
		}
226
	}
242
        // enables labeling
243
        JCheckBox applyLabels = getChkApplyLabels();
244
        applyLabels.setSelected(layer.isLabeled());
245
    }
227 246

  
247
    public void actionPerformed(ActionEvent e) {
248
        JComponent c = (JComponent) e.getSource();
228 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
            }
229 260

  
230
	private void refreshControls() {
231
		if (layer == null) return;
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);
232 267

  
233
		// enables labeling
234
		JCheckBox applyLabels = getChkApplyLabels();
235
		applyLabels.setSelected(layer.isLabeled());
236
	}
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
    }
237 281

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

  
286
    public void cancelAction() {
239 287

  
288
    }
240 289

  
241
	public void actionPerformed(ActionEvent e) {
242
		JComponent c = (JComponent)e.getSource();
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
    }
243 298

  
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
	    applyAction();
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
	}
299
    public String getName() {
300
        return PluginServices.getText(this, "Etiquetados");
301
    }
296 302
}

Also available in: Unified diff