Statistics
| Revision:

root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / table / TableControlerPanel.java @ 8026

History | View | Annotate | Download (12.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.table;
20

    
21
import java.awt.FlowLayout;
22
import java.awt.event.ActionListener;
23

    
24
import javax.swing.ImageIcon;
25
import javax.swing.JButton;
26
import javax.swing.JComboBox;
27
import javax.swing.JLabel;
28
import javax.swing.JPanel;
29

    
30
import org.gvsig.i18n.Messages;
31

    
32
/**
33
 * Control para el manejo de tablas. No contiene eventos, estos deben 
34
 * ser manejados desde la clase que lo llame.
35
 * 
36
 * @author Nacho Brodin (brodin_ign@gva.es)
37
 *
38
 */
39
public class TableControlerPanel extends JPanel{
40
                
41
        //**********************Vars**********************************
42
        private JButton                                         bPrev = null;
43
        private JComboBox                                         cPoint = null;
44
        private JButton                                         bNext = null;
45
        private JButton                                         bFirst = null;
46
        private JButton                                         bLast = null;
47
        private JPanel                                                 pTableControl = null;
48
        private JLabel                                                 lPoints = null;
49
        private JLabel                                                 lNumberOfPoints = null;
50
        private JButton                                         bNew = null;
51
        private JButton                                         bDelPoint = null;
52
        private JButton                                         bClear = null;
53
        private String                                                pathToImages = "images/";//"/com/iver/cit/gvsig/gui/panels/images/";
54
        /**
55
         * Estado de los botones cuando se ejecuta disableAllControls
56
         */
57
        private boolean[]                                         buttonsState = new boolean[8];
58
        /**
59
         * DisableAllControls ha sido ejecutada si est? a true esta variabled
60
         */
61
        private boolean                                         disableAllControls = false;
62
        //**********************End Vars******************************
63
        
64
        //**********************Methods*******************************
65
        /**
66
         * This is the default constructor
67
         */
68
        public TableControlerPanel(ActionListener buttonsListener) {
69
                super();
70
                initialize(buttonsListener);
71

    
72
        }
73

    
74
        /**
75
         * This method initializes this
76
         * 
77
         * @return void
78
         */
79
        private void initialize(ActionListener buttonsListener) {                
80
                this.setPreferredSize(new java.awt.Dimension(295,22));
81
                this.setSize(new java.awt.Dimension(295,22));
82
                FlowLayout flowLayout5 = new FlowLayout();
83
                flowLayout5.setHgap(0);
84
                flowLayout5.setVgap(0);
85
        
86
                this.setLayout(flowLayout5);
87
                
88
                lPoints = new JLabel();
89
                lPoints.setText(Messages.getText("puntos"));
90
                lPoints.setPreferredSize(new java.awt.Dimension(46,15));
91
                this.add(lPoints, null);
92
                
93
                this.add(getBFirst(), null);
94
                this.add(getBPrev(), null);
95
                this.add(getCPoint(), null);
96
                this.add(getBNext(), null);
97
                this.add(getBLast(), null);
98
                this.add(getBNew(), null);
99
                this.add(getLNumberOfPoints(), null);
100
                this.add(getBDelPoint(), null);
101
                this.add(getBClear(), null);
102
                
103
                getBFirst().addActionListener(buttonsListener);
104
                getBPrev().addActionListener(buttonsListener);
105
                getCPoint().addActionListener(buttonsListener);
106
                getBNext().addActionListener(buttonsListener);
107
                getBLast().addActionListener(buttonsListener);
108
                getBNew().addActionListener(buttonsListener);
109
                getBDelPoint().addActionListener(buttonsListener);
110
                getBClear().addActionListener(buttonsListener);
111
        }
112
        
113
        /**
114
         * Esta funci?n deshabilita todos los controles y guarda sus valores
115
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
116
         * se vuelvan a quedar como estaba
117
         */
118
        public void disableAllControls(){
119
                if(!disableAllControls){
120
                        disableAllControls = true;
121
                        //Salvamos los estados
122
                        
123
                        buttonsState[0] = getBFirst().isEnabled();
124
                        buttonsState[1] = getBPrev().isEnabled();
125
                        buttonsState[2] = getCPoint().isEnabled();
126
                        buttonsState[3] = getBNext().isEnabled();
127
                        buttonsState[4] = getBLast().isEnabled();
128
                        buttonsState[5] = getBDelPoint().isEnabled();
129
                        buttonsState[6] = getBClear().isEnabled();
130
                        buttonsState[7] = getBNew().isEnabled();
131
                                                
132
                        //Desactivamos controles
133
                        getBFirst().setEnabled(false);
134
                        getBPrev().setEnabled(false);
135
                        getCPoint().setEnabled(false);
136
                        getBNext().setEnabled(false);
137
                        getBLast().setEnabled(false);
138
                        getBDelPoint().setEnabled(false);
139
                        getBClear().setEnabled(false);
140
                        getBNew().setEnabled(false);
141
                }
142
        }
143
        
144
        /**
145
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n 
146
         * disableAllControls
147
         */
148
        public void restoreControlsValue(){
149
                if(disableAllControls){
150
                        disableAllControls = false;
151
                        getBFirst().setEnabled(buttonsState[0]);
152
                        getBPrev().setEnabled(buttonsState[1]);
153
                        getCPoint().setEnabled(buttonsState[2]);
154
                        getBNext().setEnabled(buttonsState[3]);
155
                        getBLast().setEnabled(buttonsState[4]);
156
                        getBDelPoint().setEnabled(buttonsState[5]);
157
                        getBClear().setEnabled(buttonsState[6]);
158
                        getBNew().setEnabled(buttonsState[6]);
159
                }
160
        }
161
        //**********************Methods*******************************
162
        /**
163
         * This method initializes jButton        
164
         *         
165
         * @return javax.swing.JButton        
166
         */    
167
        public JButton getBFirst() {
168
                if (bFirst == null) {
169
                        bFirst = new JButton();
170
                        bFirst.setPreferredSize(new java.awt.Dimension(22,22));
171
                        bFirst.setEnabled(false);
172
                        bFirst.setIcon(new ImageIcon(getClass().getResource(pathToImages + "first.png")));
173
                        bFirst.setToolTipText(Messages.getText("primero"));
174
                }
175
                return bFirst;
176
        }
177
        
178
        /**
179
         * This method initializes jButton        
180
         *         
181
         * @return javax.swing.JButton        
182
         */    
183
        public JButton getBLast() {
184
                if (bLast == null) {
185
                        bLast = new JButton();
186
                        bLast.setPreferredSize(new java.awt.Dimension(22,22));
187
                        bLast.setEnabled(false);
188
                        bLast.setIcon(new ImageIcon(getClass().getResource(pathToImages + "last.png")));
189
                        bLast.setToolTipText(Messages.getText("ultimo"));
190
                }
191
                return bLast;
192
        }
193
        
194
        /**
195
         * This method initializes bBefore        
196
         *         
197
         * @return javax.swing.JButton        
198
         */
199
        public JButton getBPrev() {
200
                if (bPrev == null) {
201
                        bPrev = new JButton();
202
                        bPrev.setText("");
203
                        bPrev.setEnabled(false);
204
                        bPrev.setPreferredSize(new java.awt.Dimension(22,22));
205
                        bPrev.setIcon(new ImageIcon(getClass().getResource(pathToImages + "prev.png")));
206
                        bPrev.setActionCommand("");
207
                        bPrev.setToolTipText(Messages.getText("anterior"));
208
                }
209
                return bPrev;
210
        }
211

    
212
        /**
213
         * This method initializes bNext        
214
         *         
215
         * @return javax.swing.JButton        
216
         */
217
        public JButton getBNext() {
218
                if (bNext == null) {
219
                        bNext = new JButton();
220
                        bNext.setText("");
221
                        bNext.setEnabled(false);
222
                        bNext.setPreferredSize(new java.awt.Dimension(22,22));
223
                        bNext.setIcon(new ImageIcon(getClass().getResource(pathToImages + "next.png")));
224
                        bNext.setActionCommand("");
225
                        bNext.setToolTipText(Messages.getText("siguiente"));
226
                }
227
                return bNext;
228
        }
229
        
230
        /**
231
         * Este m?todo inicializa el combo que contiene el n?mero de puntos.        
232
         *         
233
         * @return javax.swing.JComboBox        
234
         */
235
        public JComboBox getCPoint() {
236
                if (cPoint == null) {
237
                        cPoint = new JComboBox();
238
                        cPoint.setPreferredSize(new java.awt.Dimension(50,22));
239
                        
240
                }
241
                return cPoint;
242
        }
243

    
244
        /**
245
         * @return Returns the lNumberOfPoints.
246
         */
247
        public JLabel getLNumberOfPoints() {
248
                if(lNumberOfPoints == null){
249
                        lNumberOfPoints = new JLabel();
250
                        lNumberOfPoints.setText(Messages.getText("de")+" 0");
251
                        lNumberOfPoints.setPreferredSize(new java.awt.Dimension(35,15));
252
                }
253
                return lNumberOfPoints;
254
        }
255
        
256
        /**
257
         * This method initializes jButton        
258
         *         
259
         * @return javax.swing.JButton        
260
         */    
261
        public JButton getBNew() {
262
                if (bNew == null) {
263
                        bNew = new JButton();
264
                        bNew.setPreferredSize(new java.awt.Dimension(22,22));
265
                        bNew.setIcon(new ImageIcon(getClass().getResource(pathToImages + "newpoint.png")));
266
                        bNew.setToolTipText(Messages.getText("nuevo"));
267
                }
268
                return bNew;
269
        }
270
        
271
        /**
272
         * Este m?todo inicializa el bot?n del eliminar punto que har? que se elimine
273
         * el punto seleccionado.        
274
         *         
275
         * @return javax.swing.JButton        
276
         */
277
        public JButton getBDelPoint() {
278
                if (bDelPoint == null) {
279
                        bDelPoint = new JButton();
280
                        bDelPoint.setText("");
281
                        bDelPoint.setEnabled(false);
282
                        bDelPoint.setPreferredSize(new java.awt.Dimension(22,22));
283
                        bDelPoint.setIcon(new ImageIcon(getClass().getResource(pathToImages + "delone.png")));
284
                        bDelPoint.setToolTipText(Messages.getText("borrar_uno"));
285
                }
286
                return bDelPoint;
287
        }
288
        
289
        /**
290
         * Este m?todo inicializa el bot?n del clear que har? que se eliminen todos los
291
         * puntos seleccionados.        
292
         *         
293
         * @return javax.swing.JButton        
294
         */
295
        public JButton getBClear() {
296
                if (bClear == null) {
297
                        bClear = new JButton();
298
                        bClear.setText("");
299
                        bClear.setPreferredSize(new java.awt.Dimension(22,22));
300
                        bClear.setEnabled(false);
301
                        bClear.setIcon(new ImageIcon(getClass().getResource(pathToImages + "delall.png")));
302
                        bClear.setActionCommand("");
303
                        bClear.setToolTipText(Messages.getText("borrar_todos"));
304
                }
305
                return bClear;
306
        }
307
        
308
        /**
309
         * Resetea el control al estado inicial. Limpia el combo, pone
310
         * el n?mero de elementos a 0 y desactiva las flechas.
311
         *
312
         */
313
        public void resetControls(){
314
                getCPoint().removeAllItems();
315
                getLNumberOfPoints().setText(Messages.getText("de 0"));
316
                checkArrows();
317
        }
318
        
319
        /**
320
         * Elimina del control un elemento de una posici?n.
321
         * <UL>
322
         * <LI>Actualiza el combo</LI>
323
         * <LI>Actualiza el texto que dice el n?mero de elementos</LI>
324
         * <LI>Actualiza las flechas</LI>
325
         * </UL>
326
         * @param pos Posici?n del elemento a eliminar.
327
         */
328
        public void setNItems(int n){
329
                //if(n < 1)
330
                        //return;
331
                getCPoint().removeAllItems();
332
                for(int i=1;i<=n;i++){
333
                        try{
334
                                getCPoint().addItem(String.valueOf(i));
335
                        }catch(ArrayIndexOutOfBoundsException ex){
336
                                //No a?adimos nada
337
                        }
338
                }
339
                getLNumberOfPoints().setText(Messages.getText("de ") + " " + n);
340
                checkArrows();
341
        }
342
        
343
        /**
344
         * Selecciona un elemento del control
345
         * @param index
346
         */
347
        public void setSelectedIndex(int index){
348
                try{
349
                        getCPoint().setSelectedIndex(index);
350
                }catch(IllegalArgumentException ex){
351
                        
352
                }
353
                checkArrows();
354
        }
355
        
356
        /**
357
         * Devuelve el punto seleccionado
358
         * @return Punto seleccionado.
359
         */
360
        public int getSelectedIndex(){
361
                return getCPoint().getSelectedIndex();
362
        }
363
        
364
        /**
365
         * Obtiene el n?mero de elementos en la lista.
366
         * @return N?mero de elementos
367
         */
368
        public int getItemCount(){
369
                return getCPoint().getItemCount();
370
        }
371
        
372
        /**
373
         * Obtiene el Objeto seleccionado como cadena de texto.
374
         * @return N?mero seleccionado
375
         */
376
        public String getSelectedItem(){
377
                return getCPoint().getSelectedItem().toString();
378
        }
379
        
380
        /**
381
         * Comprueba la posici?n del combo para ver si tiene que
382
         * habilitar o deshabilitar las flechas de delante y detr?s.
383
         */
384
        public void checkArrows(){
385
                if(disableAllControls)
386
                        return;
387
                
388
                if(getCPoint().getItemCount() <= 0){
389
                        getBClear().setEnabled(false);
390
                        getBDelPoint().setEnabled(false);
391
                }else{
392
                        getBClear().setEnabled(true);
393
                        getBDelPoint().setEnabled(true);
394
                }
395
                
396
                if(getCPoint().getSelectedIndex() == -1){
397
                        getBPrev().setEnabled(false);
398
                        getBNext().setEnabled(false);
399
                        getBLast().setEnabled(false);
400
                        getBFirst().setEnabled(false);
401
                        
402
                        return;
403
                }
404
                if(getCPoint().getSelectedIndex() == 0){
405
                        getBPrev().setEnabled(false);
406
                        getBFirst().setEnabled(false);
407
                }else{
408
                        getBPrev().setEnabled(true);
409
                        getBFirst().setEnabled(true);
410
                }
411
                
412
                if(getCPoint().getSelectedIndex() == (getCPoint().getItemCount() - 1) ){
413
                        getBNext().setEnabled(false);
414
                        getBLast().setEnabled(false);
415
                }else{
416
                        getBNext().setEnabled(true);
417
                        getBLast().setEnabled(true);
418
                }
419
        }
420
        
421
        /**
422
         * A?ade un punto al combo y checkea los controles colocandolos en los valores correctos.
423
         * @param countPoints N?mero de punto a a?adir al final del combo
424
         */
425
        public void addPointToTable(int countPoints){
426
                getCPoint().addItem("" + countPoints);
427
                getLNumberOfPoints().setText(Messages.getText("de ") + " " + countPoints);
428
                setSelectedIndex(getItemCount() - 1);
429
                checkArrows();
430
        }
431
        
432
        /**
433
         * Asigna la ruta donde est?n las imagenes
434
         * @param pathToImages
435
         */
436
        public void setPathToImages(String pathToImages) {
437
                this.pathToImages = pathToImages;
438
        }
439
        
440
        /**
441
     * Activa o desactiva este panel y todos los que lo componen
442
     * @param enabled variable booleana para la activaci?n y/o desactivaci?n
443
     */
444
    public void setEnabled(boolean enabled){
445
            this.getBDelPoint().setEnabled(enabled);
446
            this.getBClear().setEnabled(enabled);
447
    }
448

    
449
    /**
450
     * Dice si los controles est?n deshabilitados o no
451
     * @return true si est? habilitado y false si no lo est?
452
     */
453
        public boolean isDisableAllControls() {
454
                return disableAllControls;
455
        }
456
 }