Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / table / TableControlerPanel.java @ 40561

History | View | Annotate | Download (11.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.table;
25

    
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.net.URL;
29

    
30
import javax.swing.ImageIcon;
31
import javax.swing.JButton;
32
import javax.swing.JComboBox;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35

    
36
import org.gvsig.gui.beans.Messages;
37
import org.gvsig.gui.beans.table.listeners.TableListener;
38
import org.gvsig.gui.util.StatusComponent;
39

    
40
/**
41
 * Control para el manejo de tablas. No contiene eventos, estos deben ser
42
 * manejados desde la clase que lo llame.
43
 * @author Nacho Brodin (brodin_ign@gva.es)
44
 */
45
public class TableControlerPanel extends JPanel {
46
        private static final long serialVersionUID = -6442685244347917638L;
47
        private int               HEIGHT_BUTTONS   = 19;                   // 16
48
                                                                                                                                                                                                                                                                                        // estaria
49
                                                                                                                                                                                                                                                                                        // bien
50
        // **********************Vars**********************************
51
        private JButton           bPrev            = null;
52
        private JComboBox         cPoint           = null;
53
        private JButton           bNext            = null;
54
        private JButton           bFirst           = null;
55
        private JButton           bLast            = null;
56
        private JLabel            lPoints          = null;
57
        private JLabel            lNumberOfPoints  = null;
58
        private JButton           bNew             = null;
59
        private JButton           bDelPoint        = null;
60
        private JButton           bClear           = null;
61

    
62
        private String            pathToImages     = "images/";            // "/com/iver/cit/gvsig/gui/panels/images/";
63

    
64
        /**
65
         * Objeto para controlar el estado de los componentes visuales
66
         */
67
        private StatusComponent   statusComponent  = null;
68

    
69
        // **********************End Vars******************************
70

    
71
        // **********************Methods*******************************
72
        /**
73
         * This is the default constructor
74
         */
75
        public TableControlerPanel(TableListener tableListener) {
76
                initialize(tableListener);
77
        }
78

    
79
        /**
80
         * This method initializes this
81
         * @return void
82
         */
83
        private void initialize(TableListener tableListener) {
84
                statusComponent = new StatusComponent(this);
85

    
86
                this.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 0));
87
                this.setAlignmentX(0);
88

    
89
                lPoints = new JLabel();
90
                lPoints.setText(Messages.getText("registro") + " ");
91
                this.add(lPoints);
92

    
93
                this.add(getBFirst());
94
                this.add(getBPrev());
95
                this.add(getCPoint());
96
                this.add(getBNext());
97
                this.add(getBLast());
98
                this.add(getBNew());
99
                this.add(getLNumberOfPoints());
100
                this.add(getBDelPoint());
101
                this.add(getBClear());
102

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

    
113
        /**
114
         * Esta funci?n deshabilita todos los controles y guarda sus valores de
115
         * habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
116
         * se vuelvan a quedar como estaba
117
         */
118
        public void disableAllControls() {
119
                statusComponent.setEnabled(false);
120
        }
121

    
122
        /**
123
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n
124
         * disableAllControls
125
         */
126
        public void restoreControlsValue() {
127
                statusComponent.setEnabled(true);
128
        }
129

    
130
        // **********************Methods*******************************
131
        /**
132
         * This method initializes jButton
133
         * @return javax.swing.JButton
134
         */
135
        public JButton getBFirst() {
136
                if (bFirst == null) {
137
                        bFirst = new JButton();
138
                        bFirst.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
139
                        bFirst.setEnabled(false);
140
                        URL url = getClass().getResource(pathToImages + "first.png");
141
                        if(url != null) {
142
                                bFirst.setIcon(new ImageIcon(url));
143
                        }
144
                        bFirst.setToolTipText(Messages.getText("primero"));
145
                }
146
                return bFirst;
147
        }
148

    
149
        /**
150
         * This method initializes jButton
151
         * @return javax.swing.JButton
152
         */
153
        public JButton getBLast() {
154
                if (bLast == null) {
155
                        bLast = new JButton();
156
                        bLast.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
157
                        bLast.setEnabled(false);
158
                        URL url = getClass().getResource(pathToImages + "last.png");
159
                        if(url != null)
160
                                bLast.setIcon(new ImageIcon(url));
161
                        bLast.setToolTipText(Messages.getText("ultimo"));
162
                }
163
                return bLast;
164
        }
165

    
166
        /**
167
         * This method initializes bBefore
168
         * @return javax.swing.JButton
169
         */
170
        public JButton getBPrev() {
171
                if (bPrev == null) {
172
                        bPrev = new JButton();
173
                        bPrev.setText("");
174
                        bPrev.setEnabled(false);
175
                        bPrev.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
176
                        URL url = getClass().getResource(pathToImages + "prev.png");
177
                        if(url != null)
178
                                bPrev.setIcon(new ImageIcon(url));
179
                        bPrev.setActionCommand("");
180
                        bPrev.setToolTipText(Messages.getText("anterior"));
181
                }
182
                return bPrev;
183
        }
184

    
185
        /**
186
         * This method initializes bNext
187
         * @return javax.swing.JButton
188
         */
189
        public JButton getBNext() {
190
                if (bNext == null) {
191
                        bNext = new JButton("");
192
                        bNext.setEnabled(false);
193
                        bNext.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
194
                        URL url = getClass().getResource(pathToImages + "next.png");
195
                        if(url != null)
196
                                bNext.setIcon(new ImageIcon(url));
197
                        bNext.setActionCommand("");
198
                        bNext.setToolTipText(Messages.getText("siguiente"));
199
                }
200
                return bNext;
201
        }
202

    
203
        /**
204
         * Este m?todo inicializa el combo que contiene el n?mero de puntos.
205
         * @return javax.swing.JComboBox
206
         */
207
        public JComboBox getCPoint() {
208
                if (cPoint == null) {
209
                        cPoint = new JComboBox();
210
                        cPoint.setPreferredSize(new Dimension(64, HEIGHT_BUTTONS));
211
                }
212
                return cPoint;
213
        }
214

    
215
        /**
216
         * @return Returns the lNumberOfPoints.
217
         */
218
        public JLabel getLNumberOfPoints() {
219
                if (lNumberOfPoints == null) {
220
                        lNumberOfPoints = new JLabel();
221
                        lNumberOfPoints.setText(Messages.getText("de") + " 0");
222
                        // lNumberOfPoints.setPreferredSize(new java.awt.Dimension(35,15));
223
                }
224
                return lNumberOfPoints;
225
        }
226

    
227
        /**
228
         * This method initializes jButton
229
         * @return javax.swing.JButton
230
         */
231
        public JButton getBNew() {
232
                if (bNew == null) {
233
                        bNew = new JButton();
234
                        bNew.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
235
                        URL url = getClass().getResource(pathToImages + "newpoint.png");
236
                        if(url != null)
237
                                bNew.setIcon(new ImageIcon(url));
238
                        bNew.setToolTipText(Messages.getText("nuevo"));
239
                }
240
                return bNew;
241
        }
242

    
243
        /**
244
         * Este m?todo inicializa el bot?n del eliminar punto que har? que se elimine
245
         * el punto seleccionado.
246
         * @return javax.swing.JButton
247
         */
248
        public JButton getBDelPoint() {
249
                if (bDelPoint == null) {
250
                        bDelPoint = new JButton();
251
                        bDelPoint.setText("");
252
                        bDelPoint.setEnabled(false);
253
                        bDelPoint.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
254
                        URL url = getClass().getResource(pathToImages + "delone.png");
255
                        if(url != null)
256
                                bDelPoint.setIcon(new ImageIcon(url));
257
                        bDelPoint.setToolTipText(Messages.getText("borrar_uno"));
258
                }
259
                return bDelPoint;
260
        }
261

    
262
        /**
263
         * Este m?todo inicializa el bot?n del clear que har? que se eliminen todos
264
         * los puntos seleccionados.
265
         * @return javax.swing.JButton
266
         */
267
        public JButton getBClear() {
268
                if (bClear == null) {
269
                        bClear = new JButton();
270
                        bClear.setText("");
271
                        bClear.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
272
                        bClear.setEnabled(false);
273
                        URL url = getClass().getResource(pathToImages + "delall.png");
274
                        if(url != null)
275
                                bClear.setIcon(new ImageIcon(url));
276
                        bClear.setActionCommand("");
277
                        bClear.setToolTipText(Messages.getText("borrar_todos"));
278
                }
279
                return bClear;
280
        }
281

    
282
        /**
283
         * Resetea el control al estado inicial. Limpia el combo, pone el n?mero de
284
         * elementos a 0 y desactiva las flechas.
285
         */
286
        public void resetControls() {
287
                getCPoint().removeAllItems();
288
                getLNumberOfPoints().setText(Messages.getText("de 0"));
289
                checkArrows();
290
        }
291

    
292
        /**
293
         * Elimina del control un elemento de una posici?n.
294
         * <UL>
295
         * <LI>Actualiza el combo</LI>
296
         * <LI>Actualiza el texto que dice el n?mero de elementos</LI>
297
         * <LI>Actualiza las flechas</LI>
298
         * </UL>
299
         * @param pos Posici?n del elemento a eliminar.
300
         */
301
        public void setNItems(int n) {
302
                // if(n < 1)
303
                // return;
304
                getCPoint().removeAllItems();
305
                for (int i = 1; i <= n; i++) {
306
                        try {
307
                                getCPoint().addItem(String.valueOf(i));
308
                        } catch (ArrayIndexOutOfBoundsException ex) {
309
                                // No a?adimos nada
310
                        }
311
                }
312
                getLNumberOfPoints().setText(Messages.getText("de") + " " + n);
313
                checkArrows();
314
        }
315

    
316
        /**
317
         * Selecciona un elemento del control
318
         * @param index
319
         */
320
        public void setSelectedIndex(int index) {
321
                try {
322
                        getCPoint().setSelectedIndex(index);
323
                } catch (IllegalArgumentException ex) {
324

    
325
                }
326
                checkArrows();
327
        }
328

    
329
        /**
330
         * Devuelve el punto seleccionado
331
         * @return Punto seleccionado.
332
         */
333
        public int getSelectedIndex() {
334
                return getCPoint().getSelectedIndex();
335
        }
336

    
337
        /**
338
         * Obtiene el n?mero de elementos en la lista.
339
         * @return N?mero de elementos
340
         */
341
        public int getItemCount() {
342
                return getCPoint().getItemCount();
343
        }
344

    
345
        /**
346
         * Obtiene el Objeto seleccionado como cadena de texto.
347
         * @return N?mero seleccionado
348
         */
349
        public String getSelectedItem() {
350
                return getCPoint().getSelectedItem().toString();
351
        }
352

    
353
        /**
354
         * Comprueba la posici?n del combo para ver si tiene que habilitar o
355
         * deshabilitar las flechas de delante y detr?s.
356
         */
357
        public void checkArrows() {
358
                if (!statusComponent.isEnabled())
359
                        return;
360

    
361
                if (getCPoint().getItemCount() <= 0) {
362
                        getBClear().setEnabled(false);
363
                        getBDelPoint().setEnabled(false);
364
                } else {
365
                        getBClear().setEnabled(true);
366
                        getBDelPoint().setEnabled(true);
367
                }
368

    
369
                if (getCPoint().getSelectedIndex() == -1) {
370
                        getBPrev().setEnabled(false);
371
                        getBNext().setEnabled(false);
372
                        getBLast().setEnabled(false);
373
                        getBFirst().setEnabled(false);
374

    
375
                        return;
376
                }
377
                if (getCPoint().getSelectedIndex() == 0) {
378
                        getBPrev().setEnabled(false);
379
                        getBFirst().setEnabled(false);
380
                } else {
381
                        getBPrev().setEnabled(true);
382
                        getBFirst().setEnabled(true);
383
                }
384

    
385
                if (getCPoint().getSelectedIndex() == (getCPoint().getItemCount() - 1)) {
386
                        getBNext().setEnabled(false);
387
                        getBLast().setEnabled(false);
388
                } else {
389
                        getBNext().setEnabled(true);
390
                        getBLast().setEnabled(true);
391
                }
392
        }
393

    
394
        /**
395
         * A?ade un punto al combo y checkea los controles colocandolos en los valores
396
         * correctos.
397
         * @param countPoints N?mero de punto a a?adir al final del combo
398
         */
399
        public void addPointToTable(int countPoints) {
400
                getCPoint().addItem("" + countPoints);
401
                getLNumberOfPoints().setText(Messages.getText("de") + " " + countPoints);
402
                setSelectedIndex(getItemCount() - 1);
403
                checkArrows();
404
        }
405

    
406
        /**
407
         * Asigna la ruta donde est?n las imagenes
408
         * @param pathToImages
409
         */
410
        public void setPathToImages(String pathToImages) {
411
                this.pathToImages = pathToImages;
412
        }
413

    
414
        /**
415
         * Activa o desactiva este panel y todos los que lo componen
416
         * @param enabled variable booleana para la activaci?n y/o desactivaci?n
417
         */
418
        public void setEnabled(boolean enabled) {
419
                this.getBDelPoint().setEnabled(enabled);
420
                this.getBClear().setEnabled(enabled);
421
        }
422

    
423
        /**
424
         * Dice si los controles est?n deshabilitados o no
425
         * @return true si est? habilitado y false si no lo est?
426
         */
427
        public boolean isDisableAllControls() {
428
                return !statusComponent.isEnabled();
429
        }
430
}