Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / table / TableControlerPanel.java @ 10885

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

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

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

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