Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / layerproperties / LabelExpressionEditorPanel.java @ 40911

History | View | Annotate | Download (6.73 KB)

1
package org.gvsig.labeling.gui.layerproperties;
2

    
3
import java.awt.Component;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.Insets;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.MouseEvent;
10
import java.awt.event.MouseListener;
11

    
12
import javax.swing.BorderFactory;
13
import javax.swing.JButton;
14
import javax.swing.JList;
15
import javax.swing.JPanel;
16
import javax.swing.JScrollPane;
17
import javax.swing.JTextArea;
18
import javax.swing.ScrollPaneConstants;
19

    
20
import org.gvsig.andami.ui.mdiManager.IWindow;
21
import org.gvsig.andami.ui.mdiManager.WindowInfo;
22
import org.gvsig.app.ApplicationLocator;
23
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
24
import org.gvsig.i18n.Messages;
25

    
26
public class LabelExpressionEditorPanel extends JPanel implements
27
IWindow, ActionListener, MouseListener {
28
        private static final String CANCEL_ACTION = "CANCEL";
29
        private static final String ACCEPT_ACTION = "ACCEPT";
30
        private static final String ADDFIELD_ACTION = "ADD_FIELD";
31
        private String originalValue;
32
        private String value;
33
        private WindowInfo wInfo = null;
34
        private String[] fieldNames;
35
        private int[] fieldTypes;
36
        private JButton botCancel;
37
        private JButton botAccept;
38
        private JButton botAddField;
39
        private JScrollPane spExpression;
40
        private JTextArea txtExpression;
41
        private JScrollPane spFieldList;
42
        private JList lstFields;
43

    
44
        public LabelExpressionEditorPanel(FeatureAttributeDescriptor[] atts) {
45
                super();
46
                
47
                int n = atts.length;
48
                this.fieldNames = new String[n];
49
                this.fieldTypes = new int[n];
50
                for (int i=0; i<n; i++) {
51
                        this.fieldNames[i] = atts[i].getName();
52
                        this.fieldTypes[i] = atts[i].getType();
53
                }
54
                initialize();
55
        }
56

    
57
        private void initialize() {
58
                this.setLayout(new GridBagLayout());
59

    
60
                GridBagConstraints baseConst = new GridBagConstraints();
61
                baseConst.ipadx = 2;
62
                baseConst.ipady = 2;
63
                baseConst.anchor = baseConst.CENTER;
64
                baseConst.insets = new Insets(2,2,2,2);
65

    
66
                GridBagConstraints con;
67

    
68
                con = (GridBagConstraints) baseConst.clone();
69

    
70
                con.gridx = 0;
71
                con.gridy = 0;
72
                con.gridheight = 3;
73
                con.gridwidth = 2;
74
                con.fill = con.BOTH;
75
                con.weightx = 1;
76
                con.weighty = 1;
77
                this.add(getFieldListComposition(), con);
78

    
79
                con = (GridBagConstraints) baseConst.clone();
80
                con.gridx = 2;
81
                con.gridy = 2;
82
                con.fill = con.NONE;
83
                this.add(getBotAddField(), con);
84

    
85
                con.gridx = 0;
86
                con.gridy = 3;
87
                con.gridheight = 4;
88
                con.gridwidth = 5;
89
                con.fill = con.BOTH;
90
                con.weightx = 1;
91
                con.weighty = 1;
92
                this.add(getExpressionComposition(), con);
93

    
94
                con = (GridBagConstraints) baseConst.clone();
95
                con.gridx = 3;
96
                con.gridy = 8;
97
                con.fill = con.NONE;
98
                this.add(getBotAccept(), con);
99

    
100
                con = (GridBagConstraints) baseConst.clone();
101
                con.gridx = 4;
102
                con.gridy = 8;
103
                con.fill = con.NONE;
104
                this.add(getBotCancel(), con);
105

    
106
        }
107

    
108
        private JButton getBotCancel() {
109
                if (botCancel == null) {
110
                        
111
                        botCancel = new JButton(Messages.getText("cancel"));
112
                        botCancel.setActionCommand(CANCEL_ACTION);
113
                        botCancel.addActionListener(this);
114
                }
115
                return botCancel;
116
        }
117

    
118
        private JButton getBotAccept() {
119
                if (botAccept == null) {
120
                        botAccept = new JButton(Messages.getText("accept"));
121
                        botAccept.setActionCommand(ACCEPT_ACTION);
122
                        botAccept.addActionListener(this);
123
                }
124
                return botAccept;
125

    
126
        }
127

    
128
        private Component getExpressionComposition() {
129
                if (spExpression == null) {
130
                        spExpression = new JScrollPane();
131
                        spExpression.setViewportView(getTxtExpression());
132
                        spExpression.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
133
                        spExpression.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
134
                        spExpression
135
                                        .setBorder(BorderFactory.createTitledBorder(
136
                                                        Messages.getText("expression")));
137
                }
138
                return spExpression;
139
        }
140

    
141
        private JTextArea getTxtExpression() {
142
                if (txtExpression == null){
143
                        txtExpression = new JTextArea();
144
                }
145
                return txtExpression;
146
        }
147

    
148
        public void setExpression(String expr){
149
                this.getTxtExpression().setText(expr);
150
        }
151

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

    
156
        private JButton getBotAddField() {
157
                if (botAddField == null) {
158
                        botAddField = new JButton(Messages.getText("add_field"));
159
                        botAddField.setActionCommand(ADDFIELD_ACTION);
160
                        botAddField.addActionListener(this);
161
                }
162
                return botAddField;
163

    
164
        }
165

    
166
        private Component getFieldListComposition() {
167
                if (spFieldList == null){
168
                        spFieldList = new JScrollPane();
169
                        spFieldList.setViewportView(getLstFields());
170
                        spFieldList.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
171
                        spFieldList.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
172
                        spFieldList
173
                                        .setBorder(BorderFactory.createTitledBorder(
174
                                                        Messages.getText("fields")));
175

    
176
                }
177
                return spFieldList;
178
        }
179

    
180
        private JList getLstFields() {
181
                if (lstFields == null){
182
                        lstFields = new JList(this.fieldNames);
183
                        lstFields.addMouseListener(this);
184
                }
185
                return lstFields;
186
        }
187

    
188
        public WindowInfo getWindowInfo() {
189
                if (wInfo == null) {
190
                        wInfo = new WindowInfo(WindowInfo.RESIZABLE
191
                                        + WindowInfo.MODALDIALOG);
192
                        wInfo.setWidth(500);
193
                        wInfo.setHeight(400);
194
                        wInfo.setTitle(Messages.getText("expression"));
195
                }
196
                return wInfo;
197
        }
198

    
199
        public Object getWindowProfile() {
200
                return WindowInfo.EDITOR_PROFILE;
201
        }
202

    
203
        public void setValue(String value) {
204
                this.originalValue = value;
205
                this.value = value;
206
                this.setExpression(value);
207
        }
208

    
209
        public String getValue() {
210
                return this.value;
211
        }
212

    
213
        public void actionPerformed(ActionEvent e) {
214
                doAction(e.getActionCommand());
215
        }
216

    
217
        private void doAction(String action){
218
                if (action.equals(ADDFIELD_ACTION)){
219
                        JTextArea txt = getTxtExpression();
220
                        StringBuffer strb = new StringBuffer();
221
                        String str = txt.getText();
222
                        int sini = txt.getSelectionStart();
223
                        int send = txt.getSelectionEnd();
224
                        if (sini > 0){
225
                                strb.append(str.substring(0, sini));
226
                                str = str.substring(sini);
227
                        }
228
                        // strb.append('[');
229
                        strb.append((String)getLstFields().getSelectedValue());
230
                        // strb.append(']');
231
                        send = send -sini;
232
                        if (send > 0){
233
                                str = str.substring(send);
234
                        }
235
                        strb.append(str);
236

    
237
                        getTxtExpression().setText(strb.toString());
238

    
239
                } else {
240
                        // Accept or cancel
241
                        if (action.equals(ACCEPT_ACTION)){
242
                                this.value = getExpression();
243
                        } else {
244
                                this.value = this.originalValue;
245
                        }
246
                        ApplicationLocator.getManager().getUIManager().closeWindow(this);
247
                }
248
        }
249

    
250
        public void mouseClicked(MouseEvent e) {
251
                if (e.getSource() == getLstFields()){
252
                        if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() > 1){
253
                                doAction(ADDFIELD_ACTION);
254
                        }
255
                }
256

    
257

    
258
        }
259

    
260
        public void mouseEntered(MouseEvent e) {
261
                // Do nothing
262

    
263
        }
264

    
265
        public void mouseExited(MouseEvent e) {
266
                // Do nothing
267
        }
268

    
269
        public void mousePressed(MouseEvent e) {
270
                // Do nothing
271

    
272
        }
273

    
274
        public void mouseReleased(MouseEvent e) {
275
                // Do nothing
276

    
277
        }
278
}