Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / LabelClassRenderingProperties.java @ 20768

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.gui.styling;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.Font;
48
import java.awt.GridLayout;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.util.ArrayList;
52

    
53
import javax.swing.BorderFactory;
54
import javax.swing.JLabel;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57
import javax.swing.JTextField;
58

    
59
import org.gvsig.gui.beans.AcceptCancelPanel;
60
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
61
import org.gvsig.gui.beans.swing.JButton;
62

    
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.messages.NotificationManager;
65
import com.iver.andami.ui.mdiManager.IWindow;
66
import com.iver.andami.ui.mdiManager.WindowInfo;
67
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
68
import com.iver.cit.gvsig.fmap.core.FShape;
69
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
70
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
71
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
72
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
73
import com.iver.cit.gvsig.gui.styling.SelectorFilter;
74
import com.iver.cit.gvsig.gui.styling.StyleSelector;
75
import com.iver.cit.gvsig.gui.styling.SymbolPreviewer;
76
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
77
import com.iver.cit.gvsig.gui.utils.FontChooser;
78
import com.iver.cit.gvsig.project.documents.view.legend.gui.ISymbolSelector;
79
import com.iver.utiles.swing.JComboBox;
80

    
81
/**
82
 * 
83
 * LabelClassRenderingProperties.java
84
 *
85
 * 
86
 * @author jaume dominguez faus - jaume.dominguez@iver.es Feb 29, 2008
87
 *
88
 */
89
public class LabelClassRenderingProperties extends GridBagLayoutPanel implements ActionListener{
90
        private SymbolPreviewer previewer;
91
        private JComboBox cmbExpressions;
92
        private JButton btnFont;
93
        private JButton btnSymbol;
94
        private JButton btnLabelStyles;
95
        private JButton btnEditExpression;
96
        private LabelClass lc;
97
        private ArrayList<String> expressions;
98
//        private ILabelingMethod method;
99
        private String[] fieldNames;
100
        
101
        private class ExprEditorPanel extends JPanel implements IWindow {
102
                private static final long serialVersionUID = -3867224882591879900L;
103
                private ActionListener action = new ActionListener() {
104
                        public void actionPerformed(ActionEvent e) {
105
                                if ("OK".equals(e.getActionCommand())) {
106
                                        if (validateExpr())
107
                                                PluginServices.getMDIManager().closeWindow(
108
                                                                ExprEditorPanel.this);
109
                                        else
110
                                                JOptionPane.showMessageDialog(ExprEditorPanel.this,
111
                                                                PluginServices.getText(this, "syntax_error"),
112
                                                                PluginServices.getText(this, "error"),
113
                                                                JOptionPane.ERROR_MESSAGE);
114
                                } else {
115
                                        getTxtExpression().setText(lastExtression);
116
                                        PluginServices.getMDIManager().closeWindow(
117
                                                        ExprEditorPanel.this);
118
                                }
119
                        }
120
                };
121
                private JTextField txtExpression;
122
                private String lastExtression;
123

    
124
                public ExprEditorPanel(String currentExpression) {
125
                        super();
126
                        initialize();
127
                        lastExtression = currentExpression;
128
                        getTxtExpression().setText(currentExpression);
129

    
130
                }
131

    
132
                private void initialize() {
133
                        GridBagLayoutPanel content = new GridBagLayoutPanel();
134
                        content.addComponent(new JLabel(" "+PluginServices.getText(this, "label_expression_help")));
135
                        content.addComponent(
136
                                        " "+PluginServices.getText(this, "expression")+":",
137
                                        getTxtExpression());
138
                        setLayout(new BorderLayout(5, 5));
139

    
140
                        add(content, BorderLayout.CENTER);
141
                        add(new AcceptCancelPanel(action, action), BorderLayout.SOUTH);
142
                        setSize(new Dimension(300, 80));
143
                }
144

    
145
                private JTextField getTxtExpression() {
146
                        if (txtExpression == null) {
147
                                txtExpression = new JTextField(25);
148
                        }
149
                        return txtExpression;
150
                }
151

    
152
                public String getExpression() {
153
                        return getTxtExpression().getText();
154
                }
155

    
156
                public WindowInfo getWindowInfo() {
157
                        WindowInfo viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
158
                        viewInfo.setWidth(getWidth());
159
                        viewInfo.setHeight(getHeight());
160
                        viewInfo.setTitle(PluginServices.getText(this,"label_expression_editor"));
161
                        return viewInfo;
162
                }
163

    
164
                private boolean validateExpr() {
165
                        // TODO : implement it
166
                        return true;
167
                }
168
        }
169
        
170
        public LabelClassRenderingProperties() {
171
                initialize();
172
        }
173
        
174
        private void initialize() {
175
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
176
                aux.setBorder(BorderFactory.createTitledBorder(
177
                                null, PluginServices.getText(this, "text_string")));
178
                aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
179
                aux.add(getCmbExpressions());
180
                aux.add(getBtnEditExpression());
181
                aux.setPreferredSize(new Dimension(612, 60));
182
                addComponent(aux);
183

    
184

    
185
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
186
                aux.setBorder(BorderFactory.createTitledBorder(
187
                                null, PluginServices.getText(this, "text_symbol")));
188
                aux.add(getSymbolPreviewer());
189
                
190
                JPanel aux2 = new JPanel();
191
                aux2.add(getBtnFont());
192
                aux2.add(getBtnSymbol());
193
                aux2.add(getBtnLabelStyles());
194

    
195
                aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
196
                aux.add(aux2);
197
                aux.setPreferredSize(new Dimension(612, 100));
198
                addComponent(aux);
199
        }
200
        
201
        
202
        public void setFieldNames(String[] fieldNames) {
203
                this.fieldNames = fieldNames;
204
        }
205

    
206
        private void refreshCmbExpressions() {
207

    
208
                ArrayList<String> exp = getExpressions();
209

    
210
//                noEvent = true;
211
                if ( lc != null && 
212
                                lc.getLabelExpression()!=null && 
213
                                !lc.getLabelExpression().equals("") && 
214
                                !exp.contains(lc.getLabelExpression())) {
215
                        exp.add(0, lc.getLabelExpression());
216
                }
217

    
218
                getCmbExpressions().removeAllItems();
219
                for (int i = 0; i < exp.size(); i++) {
220
                        getCmbExpressions().addItem(exp.get(i));
221
                }
222
                if (lc != null) {
223
                        getCmbExpressions().setSelectedItem(lc.getLabelExpression());
224
                } else {
225
                        getCmbExpressions().setSelectedItem(exp.get(0));
226
                }
227
//                noEvent = false;
228
        }
229
        
230
        private JButton getBtnEditExpression() {
231
                if (btnEditExpression == null) {
232
                        btnEditExpression = new JButton(
233
                                        PluginServices.getText(this, "edit_expression")+"...");
234
                        btnEditExpression.setName("BTNEDITEXPRESSION");
235
                        btnEditExpression.addActionListener(this);
236
                }
237
                return btnEditExpression;
238
        }
239

    
240
        private JButton getBtnSymbol() {
241
                if (btnSymbol == null) {
242
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
243
                        btnSymbol.setName("BTNSYMBOL");
244
                        btnSymbol.addActionListener(this);
245
                }
246

    
247
                return btnSymbol;
248
        }
249
        
250
        private ArrayList<String> getExpressions() {
251
                if (expressions == null) {
252
                        expressions = new ArrayList<String>();
253
                        try {
254
                                for (int i = 0; i < fieldNames.length; i++) {
255
                                        expressions.add("["+fieldNames[i]+"];");
256
                                }
257
                        } catch (Exception e) {
258
                                NotificationManager.addError(
259
                                                PluginServices.getText(
260
                                                                this, "could_not_retreive_layer_field_names"), e);
261
                        }
262
                }
263
                return expressions;
264
        }
265
        private JComboBox getCmbExpressions() {
266
                if (cmbExpressions == null) {
267
                        cmbExpressions = new JComboBox();
268
                        cmbExpressions.setPreferredSize(new Dimension(150, 20));
269
                        cmbExpressions.setName("CMBEXPRESSIONS");
270
                        cmbExpressions.addActionListener(this);
271
                }
272
                return cmbExpressions;
273

    
274
        }
275

    
276
        private JButton getBtnFont() {
277
                if (btnFont == null) {
278
                        btnFont = new JButton(
279
                                        PluginServices.getText(this,"fuente")+"...");
280
                        btnFont.setName("BTNFONT");
281
                        btnFont.addActionListener(this);
282
                }
283
                return btnFont;
284
        }
285
        
286
        private SymbolPreviewer getSymbolPreviewer() {
287
                if (previewer == null) {
288
                        previewer = new SymbolPreviewer();
289
                        previewer.setPreferredSize(new Dimension(420, 65));
290
                }
291
                return previewer;
292
        }
293
        
294
        
295
        public void setModel(LabelClass lc) {
296
                this.lc = lc;
297
                refreshCmbExpressions();
298
                refreshTextSymbolPreviewer();
299
        }
300
        
301
        public void actionPerformed(ActionEvent e) {
302
                Component c = (Component) e.getSource();
303
                if (c.equals(btnEditExpression)) {
304
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
305
                                        getSelectedItem());
306
                        PluginServices.getMDIManager().addWindow( ep );
307
                        getCmbExpressions().addItem(ep.getExpression());
308
                        getCmbExpressions().setSelectedItem(ep.getExpression());
309
                        lc.setLabelExpression(ep.getExpression());
310
                } else if (c.equals(btnFont)) {
311
                        Font labelFont = lc.getTextSymbol().getFont();
312
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
313
                        if (newFont != null) {
314
                                lc.getTextSymbol().setFont(newFont);
315
                        }
316
                        refreshTextSymbolPreviewer();
317
                } else if (c.equals(btnLabelStyles)) {
318
                        // here open symbol selector
319

    
320
                        StyleSelector stySel = new StyleSelector(
321
                                        lc.getLabelStyle(),
322
                                        FShape.TEXT );
323
                        stySel.setUnit(lc.getUnit());
324
                        stySel.setReferenceSystem(lc.getReferenceSystem());
325
                        PluginServices.getMDIManager().addWindow(stySel);
326
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
327
                        if (sty != null) {
328
                                // gather the style and apply to the class
329
                                lc.setLabelStyle(sty);
330
                                lc.setUnit(stySel.getUnit());
331
                                lc.setReferenceSystem(stySel.getReferenceSystem());
332

    
333
                        }
334
                } else if (c.equals(cmbExpressions)) {
335
                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
336
                } if (c.equals(btnSymbol)) {
337
                        
338
                        // here open symbol selector
339

    
340
                        ISymbolSelector symSel = SymbolSelector.createSymbolSelector(lc.getTextSymbol(),
341
                                        FShape.TEXT,
342
                                        new SelectorFilter() {
343
                                                public boolean accepts(Object obj) {
344
                                                        if (obj instanceof ISymbol) {
345
                                                                ISymbol sym = (ISymbol) obj;
346
                                                                return sym.getSymbolType() == FShape.TEXT;
347
                                                        }
348
                                                        return false;
349
                                                }
350
                                        });
351
                        
352
                        PluginServices.getMDIManager().addWindow(symSel);
353
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
354
                        if (sym != null) {
355
                                // gather the symbol and apply to the class
356
                                if (sym instanceof CartographicSupport) {
357
                                        CartographicSupport csSty = (CartographicSupport) sym;
358
                                        lc.setUnit(csSty.getUnit());
359
                                        lc.setReferenceSystem(csSty.getReferenceSystem());
360
                                }
361
                                lc.setTextSymbol((ITextSymbol) sym);
362
                                
363
                        }
364
                        
365
                        refreshTextSymbolPreviewer();
366
                } 
367
        }
368

    
369
        private JButton getBtnLabelStyles() {
370
                if (btnLabelStyles == null) {
371
                        btnLabelStyles = new JButton(
372
                                        PluginServices.getText(this, "label_styles")+"...");
373
                        btnLabelStyles.setName("BTNLABELSTYLES");
374
                        btnLabelStyles.addActionListener(this);
375
                }
376
                return btnLabelStyles;
377
        }
378

    
379
        private void refreshTextSymbolPreviewer() {
380
                ITextSymbol textSym = lc.getTextSymbol();
381
                textSym.setText(PluginServices.getText(this, "GeneralLabeling.sample_text"));
382
                getSymbolPreviewer().setSymbol(textSym);
383
        
384
        }
385

    
386
}