Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / table / TableControlerPanel.java @ 13414

History | View | Annotate | Download (12.7 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.Dimension;
22
import java.awt.FlowLayout;
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.gui.beans.messages.Messages;
31
import org.gvsig.gui.beans.table.listeners.TableListener;
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
public class TableControlerPanel extends JPanel {
39
        private static final long serialVersionUID = -6442685244347917638L;
40
        private int       HEIGHT_CONTROL     = 21;
41
        private int       HEIGHT_BUTTONS     = 19; // 16 estaria bien
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

    
63
        //**********************End Vars******************************
64

    
65
        //**********************Methods*******************************
66
        /**
67
         * This is the default constructor
68
         */
69
        public TableControlerPanel(TableListener tableListener) {
70
                super();
71
                initialize(tableListener);
72

    
73
        }
74

    
75
        /**
76
         * This method initializes this
77
         *
78
         * @return void
79
         */
80
        private void initialize(TableListener tableListener) {
81
                FlowLayout flowLayout5 = new FlowLayout();
82
                flowLayout5.setHgap(1);
83
                flowLayout5.setVgap(0);
84
                flowLayout5.setAlignment(java.awt.FlowLayout.LEFT);
85

    
86
                this.setLayout(flowLayout5);
87
                this.setAlignmentX(0);
88

    
89
                lPoints = new JLabel();
90
                lPoints.setText(Messages.getText("registro") + " ");
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
                this.setPreferredSize(new Dimension(0, HEIGHT_CONTROL));
103

    
104
                getBFirst().addActionListener(tableListener);
105
                getBPrev().addActionListener(tableListener);
106
                getCPoint().addActionListener(tableListener);
107
                getBNext().addActionListener(tableListener);
108
                getBLast().addActionListener(tableListener);
109
                getBNew().addActionListener(tableListener);
110
                getBDelPoint().addActionListener(tableListener);
111
                getBClear().addActionListener(tableListener);
112
        }
113

    
114
        /**
115
         * Esta funci?n deshabilita todos los controles y guarda sus valores
116
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
117
         * se vuelvan a quedar como estaba
118
         */
119
        public void disableAllControls(){
120
                if(!disableAllControls){
121
                        disableAllControls = true;
122
                        //Salvamos los estados
123

    
124
                        buttonsState[0] = getBFirst().isEnabled();
125
                        buttonsState[1] = getBPrev().isEnabled();
126
                        buttonsState[2] = getCPoint().isEnabled();
127
                        buttonsState[3] = getBNext().isEnabled();
128
                        buttonsState[4] = getBLast().isEnabled();
129
                        buttonsState[5] = getBDelPoint().isEnabled();
130
                        buttonsState[6] = getBClear().isEnabled();
131
                        buttonsState[7] = getBNew().isEnabled();
132

    
133
                        //Desactivamos controles
134
                        getBFirst().setEnabled(false);
135
                        getBPrev().setEnabled(false);
136
                        getCPoint().setEnabled(false);
137
                        getBNext().setEnabled(false);
138
                        getBLast().setEnabled(false);
139
                        getBDelPoint().setEnabled(false);
140
                        getBClear().setEnabled(false);
141
                        getBNew().setEnabled(false);
142
                }
143
        }
144

    
145
        /**
146
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n
147
         * disableAllControls
148
         */
149
        public void restoreControlsValue(){
150
                if(disableAllControls){
151
                        disableAllControls = false;
152
                        getBFirst().setEnabled(buttonsState[0]);
153
                        getBPrev().setEnabled(buttonsState[1]);
154
                        getCPoint().setEnabled(buttonsState[2]);
155
                        getBNext().setEnabled(buttonsState[3]);
156
                        getBLast().setEnabled(buttonsState[4]);
157
                        getBDelPoint().setEnabled(buttonsState[5]);
158
                        getBClear().setEnabled(buttonsState[6]);
159
                        getBNew().setEnabled(buttonsState[6]);
160
                }
161
        }
162
        //**********************Methods*******************************
163
        /**
164
         * This method initializes jButton
165
         *
166
         * @return javax.swing.JButton
167
         */
168
        public JButton getBFirst() {
169
                if (bFirst == null) {
170
                        bFirst = new JButton();
171
                        bFirst.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
172
                        bFirst.setEnabled(false);
173
                        bFirst.setIcon(new ImageIcon(getClass().getResource(pathToImages + "first.png")));
174
                        bFirst.setToolTipText(Messages.getText("primero"));
175
                }
176
                return bFirst;
177
        }
178

    
179
        /**
180
         * This method initializes jButton
181
         *
182
         * @return javax.swing.JButton
183
         */
184
        public JButton getBLast() {
185
                if (bLast == null) {
186
                        bLast = new JButton();
187
                        bLast.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
188
                        bLast.setEnabled(false);
189
                        bLast.setIcon(new ImageIcon(getClass().getResource(pathToImages + "last.png")));
190
                        bLast.setToolTipText(Messages.getText("ultimo"));
191
                }
192
                return bLast;
193
        }
194

    
195
        /**
196
         * This method initializes bBefore
197
         *
198
         * @return javax.swing.JButton
199
         */
200
        public JButton getBPrev() {
201
                if (bPrev == null) {
202
                        bPrev = new JButton();
203
                        bPrev.setText("");
204
                        bPrev.setEnabled(false);
205
                        bPrev.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
206
                        bPrev.setIcon(new ImageIcon(getClass().getResource(pathToImages + "prev.png")));
207
                        bPrev.setActionCommand("");
208
                        bPrev.setToolTipText(Messages.getText("anterior"));
209
                }
210
                return bPrev;
211
        }
212

    
213
        /**
214
         * This method initializes bNext
215
         *
216
         * @return javax.swing.JButton
217
         */
218
        public JButton getBNext() {
219
                if (bNext == null) {
220
                        bNext = new JButton();
221
                        bNext.setText("");
222
                        bNext.setEnabled(false);
223
                        bNext.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
224
                        bNext.setIcon(new ImageIcon(getClass().getResource(pathToImages + "next.png")));
225
                        bNext.setActionCommand("");
226
                        bNext.setToolTipText(Messages.getText("siguiente"));
227
                }
228
                return bNext;
229
        }
230

    
231
        /**
232
         * Este m?todo inicializa el combo que contiene el n?mero de puntos.
233
         *
234
         * @return javax.swing.JComboBox
235
         */
236
        public JComboBox getCPoint() {
237
                if (cPoint == null) {
238
                        cPoint = new JComboBox();
239
                        cPoint.setPreferredSize(new Dimension(64, HEIGHT_BUTTONS));
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 Dimension(22, HEIGHT_BUTTONS));
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 Dimension(22, HEIGHT_BUTTONS));
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 Dimension(22, HEIGHT_BUTTONS));
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
 }