Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / pagedtable / TableEntryControllerPanel.java @ 1556

History | View | Annotate | Download (12.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 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 2
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.raster.swing.impl.pagedtable;
25

    
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.util.Observable;
29
import java.util.Observer;
30

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

    
37
import org.gvsig.andami.IconThemeHelper;
38
import org.gvsig.gui.util.StatusComponent;
39
import org.gvsig.i18n.Messages;
40

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

    
63
        private String            pathToImagesForTest = "/src/main/resources/images/";
64

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

    
70
        // **********************End Vars******************************
71

    
72
        // **********************Methods*******************************
73
        /**
74
         * This is the default constructor
75
         */
76
        public TableEntryControllerPanel(TableControllerListener tableListener) {
77
                tableListener.setControlPanel(this);
78
                initialize(tableListener);
79
        }
80

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

    
88
                this.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 0));
89
                this.setAlignmentX(0);
90

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

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

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

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

    
124
        /**
125
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n
126
         * disableAllControls
127
         */
128
        public void restoreControlsValue() {
129
                statusComponent.setEnabled(true);
130
        }
131
        
132
        private ImageIcon loadIcon(String iconName) {
133
                ImageIcon icon = null;
134
                try {
135
                        icon = IconThemeHelper.getImageIcon(iconName);
136
                } catch(NullPointerException e) {}
137
                if(icon == null) 
138
                        icon = new ImageIcon(System.getProperty("user.dir") + pathToImagesForTest + iconName + ".png", "");
139
                if(icon == null) 
140
                        icon = new ImageIcon(System.getProperty("user.dir") + pathToImagesForTest + iconName + ".gif", "");
141
                return icon;
142
        }
143

    
144
        // **********************Methods*******************************
145
        /**
146
         * This method initializes jButton
147
         * @return javax.swing.JButton
148
         */
149
        public JButton getBFirst() {
150
                if (bFirst == null) {
151
                        bFirst = new JButton();
152
                        bFirst.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
153
                        bFirst.setEnabled(false);
154
                        bFirst.setIcon(loadIcon("first"));
155
                        bFirst.setToolTipText(Messages.getText("primero"));
156
                }
157
                return bFirst;
158
        }
159

    
160
        /**
161
         * This method initializes jButton
162
         * @return javax.swing.JButton
163
         */
164
        public JButton getBLast() {
165
                if (bLast == null) {
166
                        bLast = new JButton();
167
                        bLast.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
168
                        bLast.setEnabled(false);
169
                        bLast.setIcon(loadIcon("last"));
170
                        bLast.setToolTipText(Messages.getText("ultimo"));
171
                }
172
                return bLast;
173
        }
174

    
175
        /**
176
         * This method initializes bBefore
177
         * @return javax.swing.JButton
178
         */
179
        public JButton getBPrev() {
180
                if (bPrev == null) {
181
                        bPrev = new JButton();
182
                        bPrev.setText("");
183
                        bPrev.setEnabled(false);
184
                        bPrev.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
185
                        bPrev.setIcon(loadIcon("prev"));
186
                        bPrev.setActionCommand("");
187
                        bPrev.setToolTipText(Messages.getText("anterior"));
188
                }
189
                return bPrev;
190
        }
191

    
192
        /**
193
         * This method initializes bNext
194
         * @return javax.swing.JButton
195
         */
196
        public JButton getBNext() {
197
                if (bNext == null) {
198
                        bNext = new JButton("");
199
                        bNext.setEnabled(false);
200
                        bNext.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
201
                        bNext.setIcon(loadIcon("next"));
202
                        bNext.setActionCommand("");
203
                        bNext.setToolTipText(Messages.getText("siguiente"));
204
                }
205
                return bNext;
206
        }
207

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

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

    
232
        /**
233
         * This method initializes jButton
234
         * @return javax.swing.JButton
235
         */
236
        public JButton getBNew() {
237
                if (bNew == null) {
238
                        bNew = new JButton();
239
                        bNew.setPreferredSize(new Dimension(22, HEIGHT_BUTTONS));
240
                        bNew.setIcon(loadIcon("newpoint"));
241
                        bNew.setToolTipText(Messages.getText("nuevo"));
242
                }
243
                return bNew;
244
        }
245

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

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

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

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

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

    
324
                }
325
                checkArrows();
326
        }
327

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

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

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

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

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

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

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

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

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

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

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

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

    
430
        /**
431
         * Receives the event when the rows selected are updated
432
         */
433
        public void update(Observable o, Object arg) {
434
                Pager pager = (Pager)o;
435
                if(o instanceof Pager && arg instanceof Integer) {
436
                        int option = ((Integer)arg).intValue();
437
                        if(option == Pager.CHANGE_SELECTED_ROWS ) {
438
                                int[] selectedRows = pager.getSelectedRows();
439
                                if(selectedRows != null && selectedRows.length > 0)
440
                                        setSelectedIndex(selectedRows[0]);
441
                        }
442
                        
443
                        if(option == Pager.CHANGE_ADD_ENTRY ) {
444
                                addPointToTable(pager.getEntriesCount());
445
                        }
446
                        
447
                        if(option == Pager.CHANGE_REMOVE_ENTRY ) {
448
                                setNItems(pager.getEntriesCount());
449
                                int[] selectedRows = pager.getSelectedRows();
450
                                if(selectedRows != null && selectedRows.length >= 1) {
451
                                        setSelectedIndex(selectedRows[0] % pager.getEntriesPerPage());
452
                                }
453
                        }
454
                        
455
                        if(option == Pager.CHANGE_REMOVE_ALL ) {
456
                                setNItems(0);
457
                        }
458
                }
459
                
460
        }
461
}