Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / libraries / libUIComponent / src / org / gvsig / gui / beans / coordinatespanel / CoordinatesPanel.java @ 39319

History | View | Annotate | Download (10.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
package org.gvsig.gui.beans.coordinatespanel;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Font;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.Insets;
26
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.EventObject;
29
import java.util.Iterator;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.ImageIcon;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.border.TitledBorder;
36

    
37
import org.gvsig.gui.beans.datainput.DataInputContainer;
38
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
39
/**
40
 * 
41
 * @version 06/09/2007
42
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
43
 */
44
@SuppressWarnings("unchecked")
45
public class CoordinatesPanel extends JPanel implements DataInputContainerListener {
46
        private static final long  serialVersionUID       = 3336324382874763427L;
47
        private String             pathToImages           = "images/";
48
        private ArrayList          actionChangedListeners = new ArrayList();
49

    
50
        private JPanel             pCoord                 = null;
51
        private JLabel             lTitulo1               = null;
52
        private JLabel             lTitulo2               = null;
53
        private DataInputContainer dataInputContainer11   = null;
54
        private DataInputContainer dataInputContainer12   = null;
55
        private DataInputContainer dataInputContainer21   = null;
56
        private DataInputContainer dataInputContainer22   = null;
57
        private int                maximumFractionDigits  = 3;
58

    
59
        /**
60
         * This is the default constructor
61
         */
62
        public CoordinatesPanel(int maximumFractionDigits) {
63
                this.maximumFractionDigits = maximumFractionDigits;
64
                initialize();
65
        }
66
        
67
        /**
68
         * This is the default constructor
69
         */
70
        public CoordinatesPanel() {
71
                initialize();
72
        }
73

    
74
        /**
75
         * This method initializes this
76
         *
77
         * @return void
78
         */
79
        private void initialize() {
80
                setLayout(new BorderLayout());
81
                add(getPCoord(), BorderLayout.CENTER);
82
        }
83

    
84
        /**
85
         * This method initializes jPanel
86
         *
87
         * @return javax.swing.JPanel
88
         */
89
        private JPanel getPCoord() {
90
                if (pCoord == null) {
91
                        GridBagConstraints gridBagConstraints;
92
                        pCoord = new JPanel();
93
                        pCoord.setLayout(new GridBagLayout());
94

    
95
                        int y = 0;
96
                        gridBagConstraints = new GridBagConstraints();
97
                        gridBagConstraints.gridx = 0;
98
                        gridBagConstraints.gridy = y;
99
                        gridBagConstraints.insets = new Insets(5, 5, 2, 2);
100
                        pCoord.add(getLTitulo1(), gridBagConstraints);
101

    
102
                        gridBagConstraints = new GridBagConstraints();
103
                        gridBagConstraints.gridx = 1;
104
                        gridBagConstraints.gridy = y;
105
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
106
                        gridBagConstraints.weightx = 1.0;
107
                        gridBagConstraints.insets = new Insets(5, 2, 2, 2);
108
                        pCoord.add(getDataInputContainer11(), gridBagConstraints);
109

    
110
                        gridBagConstraints = new GridBagConstraints();
111
                        gridBagConstraints.gridx = 2;
112
                        gridBagConstraints.gridy = y;
113
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
114
                        gridBagConstraints.weightx = 1.0;
115
                        gridBagConstraints.insets = new Insets(5, 2, 2, 5);
116
                        pCoord.add(getDataInputContainer12(), gridBagConstraints);
117

    
118
                        y++;
119
                        gridBagConstraints = new GridBagConstraints();
120
                        gridBagConstraints.gridx = 0;
121
                        gridBagConstraints.gridy = y;
122
                        gridBagConstraints.insets = new Insets(2, 5, 5, 2);
123
                        pCoord.add(getLTitulo2(), gridBagConstraints);
124

    
125
                        gridBagConstraints = new GridBagConstraints();
126
                        gridBagConstraints.gridx = 1;
127
                        gridBagConstraints.gridy = y;
128
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
129
                        gridBagConstraints.weightx = 1.0;
130
                        gridBagConstraints.insets = new Insets(2, 2, 5, 2);
131
                        pCoord.add(getDataInputContainer21(), gridBagConstraints);
132

    
133

    
134
                        gridBagConstraints = new GridBagConstraints();
135
                        gridBagConstraints.gridx = 2;
136
                        gridBagConstraints.gridy = y;
137
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
138
                        gridBagConstraints.weightx = 1.0;
139
                        gridBagConstraints.insets = new Insets(2, 2, 5, 5);
140
                        pCoord.add(getDataInputContainer22(), gridBagConstraints);                
141
                }
142
                return pCoord;
143
        }
144

    
145
        private JLabel getLTitulo1(){
146
                if (lTitulo1 == null){
147
                        URL resource = getClass().getResource(pathToImages + "upleft.png");
148
                        if(resource != null) {
149
                                ImageIcon icon = new ImageIcon(resource);
150
                                lTitulo1 = new JLabel(icon);
151
                        } else
152
                                lTitulo1 = new JLabel();
153
                }
154
                return lTitulo1;
155
        }
156

    
157
        private JLabel getLTitulo2() {
158
                if (lTitulo2 == null){
159
                        if (lTitulo2 == null){
160
                                URL resource = getClass().getResource(pathToImages + "downright.png");
161
                                if(resource != null) {
162
                                        ImageIcon icon = new ImageIcon(resource);
163
                                        lTitulo2 = new JLabel(icon);
164
                                } else
165
                                        lTitulo2 = new JLabel();
166
                        }
167
                }
168
                return lTitulo2;
169
        }
170

    
171
        /**
172
         * This method initializes jPanel
173
         *
174
         * @return javax.swing.JPanel
175
         */
176
        public DataInputContainer getDataInputContainer11() {
177
                if (dataInputContainer11 == null) {
178
                        dataInputContainer11 = new DataInputContainer(maximumFractionDigits);
179
                        dataInputContainer11.setLabelText("X");
180
                        dataInputContainer11.addValueChangedListener(this);
181
                }
182
                return dataInputContainer11;
183
        }
184

    
185
        /**
186
         * This method initializes jPanel1
187
         *
188
         * @return javax.swing.JPanel
189
         */
190
        public DataInputContainer getDataInputContainer12() {
191
                if (dataInputContainer12 == null) {
192
                        dataInputContainer12 = new DataInputContainer(maximumFractionDigits);
193
                        dataInputContainer12.setLabelText("Y");
194
                        dataInputContainer12.addValueChangedListener(this);
195
                }
196
                return dataInputContainer12;
197
        }
198

    
199
        /**
200
         * This method initializes jPanel2
201
         *
202
         * @return javax.swing.JPanel
203
         */
204
        public DataInputContainer getDataInputContainer21() {
205
                if (dataInputContainer21 == null) {
206
                        dataInputContainer21 = new DataInputContainer(maximumFractionDigits);
207
                        dataInputContainer21.setLabelText("X");
208
                        dataInputContainer21.addValueChangedListener(this);
209
                }
210
                return dataInputContainer21;
211
        }
212

    
213
        /**
214
         * This method initializes jPanel3
215
         *
216
         * @return javax.swing.JPanel
217
         */
218
        public DataInputContainer getDataInputContainer22() {
219
                if (dataInputContainer22 == null) {
220
                        dataInputContainer22 = new DataInputContainer(maximumFractionDigits);
221
                        dataInputContainer22.setLabelText("Y");
222
                        dataInputContainer22.addValueChangedListener(this);
223
                }
224
                return dataInputContainer22;
225
        }
226

    
227
        /**
228
         * M?todo para poner un titulo al panel.
229
         * 
230
         * @param titlePanel
231
         */
232
        public void setTitlePanel(String titlePanel) {
233
                getPCoord().setBorder(BorderFactory.createTitledBorder(null, titlePanel, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.PLAIN, 12), null));
234
        }
235

    
236
        /**
237
         * Devuelve los valores del panel por orden de izquierda a derecha
238
         * y de arriba a bajo.
239
         * @return
240
         */
241
        public String[] getValues(){
242
                String[] values = {
243
                        getDataInputContainer11().getValue(),
244
                        getDataInputContainer12().getValue(),
245
                        getDataInputContainer21().getValue(),
246
                        getDataInputContainer22().getValue()
247
                };
248
                return values;
249
        }
250

    
251
        /**
252
         * Asigna los valores del panel por orden de izquierda a derecha
253
         * y de arriba a bajo.
254
         * @return
255
         */
256
        public void setValues(String[] values){
257
                getDataInputContainer11().setValue(values[0]);
258
                getDataInputContainer12().setValue(values[1]);
259
                getDataInputContainer21().setValue(values[2]);
260
                getDataInputContainer22().setValue(values[3]);
261
        }
262
        
263
        /**
264
         * A?adir un listener a la lista de eventos
265
         * @param listener
266
         */
267
        public void addValueChangedListener(CoordinatesListener listener) {
268
                if (!actionChangedListeners.contains(listener))
269
                        actionChangedListeners.add(listener);
270
        }
271

    
272
        /**
273
         * Borrar un listener de la lista de eventos
274
         * @param listener
275
         */
276
        public void removeValueChangedListener(CoordinatesListener listener) {
277
                actionChangedListeners.remove(listener);
278
        }
279
        
280
        /**
281
         * Invocar a los eventos asociados al componente
282
         */
283
        private void callValueChangedListeners(String component) {
284
                Iterator acIterator = actionChangedListeners.iterator();
285
                while (acIterator.hasNext()) {
286
                        CoordinatesListener listener = (CoordinatesListener) acIterator.next();
287
                        listener.actionValueChanged(new CoordinatesEvent(this, component));
288
                }
289
        }
290

    
291
        public void actionValueChanged(EventObject e) {
292
                if (e.getSource() == getDataInputContainer11().getDataInputField()) {
293
                        callValueChangedListeners("11");
294
                        return;
295
                }
296
                if (e.getSource() == getDataInputContainer12().getDataInputField()) {
297
                        callValueChangedListeners("12");
298
                        return;
299
                }
300
                if (e.getSource() == getDataInputContainer21().getDataInputField()) {
301
                        callValueChangedListeners("21");
302
                        return;
303
                }
304
                if (e.getSource() == getDataInputContainer22().getDataInputField()) {
305
                        callValueChangedListeners("22");
306
                        return;
307
                }
308
  }
309
        
310
        public String getValue11() {
311
                return getDataInputContainer11().getValue();
312
        }
313
        
314
        public String getValue12() {
315
                return getDataInputContainer12().getValue();
316
        }
317
        
318
        public String getValue21() {
319
                return getDataInputContainer21().getValue();
320
        }
321
        
322
        public String getValue22() {
323
                return getDataInputContainer22().getValue();
324
        }
325
        
326
        public void setValue11(String value) {
327
                getDataInputContainer11().setValue(value);
328
        }
329
        
330
        public void setValue12(String value) {
331
                getDataInputContainer12().setValue(value);
332
        }
333
        
334
        public void setValue21(String value) {
335
                getDataInputContainer21().setValue(value);
336
        }
337
        
338
        public void setValue22(String value) {
339
                getDataInputContainer22().setValue(value);
340
        }
341

    
342
        /*
343
         * (non-Javadoc)
344
         * @see javax.swing.JComponent#setEnabled(boolean)
345
         */
346
        public void setEnabled(boolean enabled) {
347
          super.setEnabled(enabled);
348
          getPCoord().setEnabled(enabled);
349
          getLTitulo1().setEnabled(enabled);
350
          getLTitulo2().setEnabled(enabled);
351
          getDataInputContainer11().setControlEnabled(enabled);
352
          getDataInputContainer12().setControlEnabled(enabled);
353
          getDataInputContainer21().setControlEnabled(enabled);
354
          getDataInputContainer22().setControlEnabled(enabled);
355
  }
356
}