Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / Pager.java @ 14365

History | View | Annotate | Download (13.4 KB)

1 13136 evercher
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
42
/* CVS MESSAGES:
43
 *
44
 * $Id$
45
 * $Log$
46 13655 bsanchez
 * Revision 1.2  2007-09-12 16:28:23  bsanchez
47
 * *** empty log message ***
48
 *
49
 * Revision 1.1  2007/08/20 08:34:45  evercher
50 13136 evercher
 * He fusionado LibUI con LibUIComponents
51
 *
52
 * Revision 1.1  2006/03/22 11:18:29  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.4  2006/02/28 15:25:14  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2.2.6  2006/02/23 10:36:30  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.2.2.5  2006/02/16 10:36:41  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.1  2006/02/16 09:09:05  caballero
65
 * PAger de Jaume
66
 *
67
 * Revision 1.2.2.4  2006/02/10 13:23:18  jaume
68
 * page value can now be set externally
69
 *
70
 * Revision 1.2.2.3  2006/01/31 16:25:24  jaume
71
 * correcciones de bugs
72
 *
73
 * Revision 1.3  2006/01/26 16:07:14  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.2.2.1  2006/01/26 12:59:32  jaume
77
 * 0.5
78
 *
79
 * Revision 1.2  2006/01/24 14:36:33  jaume
80
 * This is the new version
81
 *
82
 * Revision 1.1.2.5  2006/01/19 16:09:30  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1.2.4  2006/01/17 17:05:39  jaume
86
 * fixed crazy buttons behavior :-P
87
 *
88
 * Revision 1.1.2.3  2006/01/17 17:01:55  jaume
89
 * fixed crazy buttons behavior :-P
90
 *
91
 * Revision 1.1.2.2  2006/01/11 12:20:30  jaume
92
 * *** empty log message ***
93
 *
94
 * Revision 1.1.2.1  2006/01/10 13:11:38  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.1.2.1  2006/01/10 11:33:31  jaume
98
 * Time dimension working against Jet Propulsion Laboratory's WMS server
99
 *
100
 * Revision 1.1.2.3  2006/01/09 18:10:38  jaume
101
 * casi con el time dimension
102
 *
103
 * Revision 1.1.2.2  2006/01/02 18:08:01  jaume
104
 * Tree de estilos
105
 *
106
 * Revision 1.1.2.1  2005/12/30 08:56:19  jaume
107
 * *** empty log message ***
108
 *
109
 *
110
 */
111
/**
112
 *
113
 */
114
package org.gvsig.gui.beans;
115
116
import java.awt.Dimension;
117
import java.awt.event.ActionEvent;
118
import java.awt.event.ActionListener;
119
120
import javax.swing.ImageIcon;
121
import javax.swing.JButton;
122
import javax.swing.JPanel;
123
import javax.swing.JSlider;
124
import javax.swing.JTextField;
125
126
127
128
/**
129
 * Bean that is useful to browse a very large list of data. It
130
 * includes a set of navigation buttons to step ahead or behind
131
 * by one or going to the first and last element of the list as
132
 * well as an slider and a text field for directly focus on a
133
 * list item.
134
 *
135
 * @author jaume dominguez faus
136
 *
137
 */
138
public class Pager extends DefaultBean {
139 13655 bsanchez
  private static final long serialVersionUID = 1997136221292929382L;
140 13136 evercher
        private JPanel buttonsPanel = null;
141
        private JButton btnFastBackward = null;
142
        private JButton btnBackward = null;
143
        private JTextField txtItemCountDisplay = null;
144
        private JButton btnForward = null;
145
        private JButton btnFastForward = null;
146
        private JPanel sliderPanel = null;
147
        private JSlider slider = null;
148
        private int itemCount;
149
        private int lowLimit;
150
        private int currentValue = -1;
151
        private int orientation;
152
        private boolean refreshing = false;
153
        public static int HORIZONTAL=0;
154
        public static int VERTICAL=1;
155
        /**
156
         * This is the default constructor. Creates a new instance of ItemBrowser with
157
         * zero items.
158
         */
159
        public Pager(int orientation){
160
                super();
161
                this.orientation=orientation;
162
                initialize(0, 0);
163
        }
164
165
        /**
166
         * Creates a new instance of ItemBrowser defining its edges
167
         * @param lowIndex, the lowest edge.
168
         * @param itemCount, the highest edge.
169
         */
170
        public Pager(int lowIndex, int itemCount,int orientation) {
171
                super();
172
                this.orientation=orientation;
173
                initialize(lowIndex, itemCount);
174
        }
175
176
        /**
177
         * This method initializes this
178
         *
179
         * @return void
180
         */
181
        private void initialize(int lowIndex, int itemCount) {
182
                setItemCount(itemCount);
183
                this.lowLimit = lowIndex;
184
                this.setLayout(null);
185
                if (orientation==VERTICAL){
186
                        this.setSize(45,305);
187
                        this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray,1));
188
                        this.setPreferredSize(new Dimension(40,300));
189
                }else{
190
                        this.setSize(240, 50);
191
                        this.setPreferredSize(new Dimension(190,50));
192
                }
193
194
                this.add(getSliderPanel(), null);
195
                this.add(getButtonsPanel(), null);
196
        }
197
198
        /**
199
         * This method initializes buttonsPanel
200
         *
201
         * @return javax.swing.JPanel
202
         */
203
        private JPanel getButtonsPanel() {
204
                if (buttonsPanel == null) {
205
                        buttonsPanel = new JPanel();
206
                        buttonsPanel.setLayout(null);
207
                        buttonsPanel.setName("buttonsPanel");
208
                        buttonsPanel.setPreferredSize(new java.awt.Dimension(173,50));
209
                        if (orientation==VERTICAL){
210
                                buttonsPanel.setBounds(3, 182, 35,115);
211
                        }else{
212
                                buttonsPanel.setBounds(5, 25, 240, 25);
213
                        }
214
215
                        buttonsPanel.add(getBtnFastBackward(), null);
216
                        buttonsPanel.add(getBtnBackward(), null);
217
                        buttonsPanel.add(getTxtItemCountDisplay(), null);
218
                        buttonsPanel.add(getBtnForward(), null);
219
                        buttonsPanel.add(getBtnFastForward(), null);
220
                }
221
                return buttonsPanel;
222
        }
223
224
        /**
225
         * This method initializes btnFastBackWard
226
         *
227
         * @return javax.swing.JButton
228
         */
229
        private JButton getBtnFastBackward() {
230
                if (btnFastBackward == null) {
231
                        btnFastBackward = new JButton();
232
                        if (orientation==VERTICAL){
233
                                btnFastBackward.setBounds(7, 1, 20, 24);
234
                        }else{
235
                                btnFastBackward.setBounds(2, 1, 20, 24);
236
                        }
237
238
                        btnFastBackward.setEnabled(itemCount!=0);
239
                        btnFastBackward.addActionListener(new ActionListener() {
240
                                public void actionPerformed(ActionEvent e) {
241
                                        if (currentValue != lowLimit){
242
                                                setValue(lowLimit, true);
243
                                        }
244
                                }});
245
                        btnFastBackward.setIcon(new ImageIcon(getClass().getResource("images/fastbackward.png")));
246
                }
247
                return btnFastBackward;
248
        }
249
250
        /**
251
         * This method initializes btnBackward
252
         *
253
         * @return javax.swing.JButton
254
         */
255
        private JButton getBtnBackward() {
256
                if (btnBackward == null) {
257
                        btnBackward = new JButton();
258
                        if (orientation==VERTICAL){
259
                                btnBackward.setBounds(7, 21, 20, 24);
260
                        }else{
261
                                btnBackward.setBounds(21, 1, 20, 24);
262
                        }
263
264
                        btnBackward.setEnabled(itemCount!=0);
265
                        btnBackward.addActionListener(new ActionListener() {
266
                                public void actionPerformed(ActionEvent e) {
267
                                        if (currentValue > lowLimit ){
268
                                                setValue(currentValue-1, true);
269
                                        }
270
                                }});
271
                        btnBackward.setIcon(new ImageIcon(getClass().getResource("images/backward.png")));
272
                }
273
                return btnBackward;
274
        }
275
276
        /**
277
         * This method initializes txtItemCountDisplay
278
         *
279
         * @return javax.swing.JTextField
280
         */
281
        private JTextField getTxtItemCountDisplay() {
282
                if (txtItemCountDisplay == null) {
283
                        txtItemCountDisplay = new JTextField();
284
                        txtItemCountDisplay.setEnabled(itemCount!=0);
285
                        txtItemCountDisplay.setHorizontalAlignment(javax.swing.JTextField.CENTER);
286
                        if (orientation==VERTICAL){
287
                                txtItemCountDisplay.setBounds(2,43, 33, 23);
288
                        } else {
289
                                txtItemCountDisplay.setBounds(43, 2, 144, 23);
290
                        }
291
292
                        txtItemCountDisplay.setText(lowLimit+" / "+itemCount);
293
                        txtItemCountDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
294
                                public void mouseClicked(java.awt.event.MouseEvent e) {
295
                                        txtItemCountDisplay.setText(currentValue+"");
296
                                        txtItemCountDisplay.setSelectionStart(0);
297
                                        txtItemCountDisplay.setSelectionEnd(txtItemCountDisplay.getText().length());
298
                                }
299
                        });
300
                        txtItemCountDisplay.addActionListener(new java.awt.event.ActionListener() {
301
                                public void actionPerformed(java.awt.event.ActionEvent e) {
302
                                        try {
303
                                                int v = Integer.parseInt(txtItemCountDisplay.getText());
304
                                                v = (v>itemCount) ? itemCount : v;
305
                                                setValue(v, true);
306
                                        } catch (Exception ex){
307
                                                refreshText(currentValue);
308
                                        }
309
                                        txtItemCountDisplay.transferFocusDownCycle();
310
                                }
311
                        });
312
                        txtItemCountDisplay.setEnabled(false);
313
                }
314
                return txtItemCountDisplay;
315
        }
316
317
        /**
318
         * This, sets the bean value and triggers an event that can be captured
319
         * by a listener.
320
         * @param number
321
         * @param fireEvent, if true then this method will fire the event. If false,
322
         * then the value will be changed silently.
323
         *
324
         */
325
        public void setValue(int number, boolean fireEvent) {
326
                if (number < lowLimit)
327
                        number = lowLimit;
328
                if (number > itemCount-1)
329
                        number = itemCount;
330
                if (number != currentValue) {
331
                        currentValue = number;
332
                        refreshControls();
333
                        if (fireEvent)
334
                                callValueChanged(new Integer(currentValue));
335
                }
336
        }
337
338
        /**
339
         * Refreshes all the mutable controls in this component.
340
         */
341
        private void refreshControls() {
342
                int normalizedValue = (int) ((currentValue / (float) itemCount)*100);
343
                refreshSlider(normalizedValue);
344
                refreshText(currentValue);
345
        }
346
347
        /**
348
         * Sets the slider to the correct (scaled) position.
349
         * @param normalizedValue
350
         */
351
        private void refreshSlider(int normalizedValue) {
352
                refreshing = true;
353
                getSlider().setValue(normalizedValue);
354
                refreshing = false;
355
        }
356
357
        /**
358
         * @param string
359
         */
360
        private void refreshText(int value) {
361
                String newText = (value+1) +" / "+itemCount;
362
363
                if (!getTxtItemCountDisplay().getText().equals(newText))
364
                        getTxtItemCountDisplay().setText(newText);
365
        }
366
367
        /**
368
         * This method initializes btnForward
369
         *
370
         * @return javax.swing.JButton
371
         */
372
        private JButton getBtnForward() {
373
                if (btnForward == null) {
374
                        btnForward = new JButton();
375
                        if (orientation==VERTICAL){
376
                                btnForward.setBounds(7, 67, 20, 24);
377
                        }else{
378
                                btnForward.setBounds(189, 1, 20, 24);
379
                        }
380
381
                        btnForward.setEnabled(itemCount!=0);
382
                        btnForward.addActionListener(new ActionListener() {
383
                                public void actionPerformed(ActionEvent e) {
384
                                        if (currentValue < itemCount-1){
385
                                                setValue(currentValue+1, true);
386
                                        }
387
                                }});
388
                        btnForward.setIcon(new ImageIcon(getClass().getResource("images/forward.png")));
389
                }
390
                return btnForward;
391
        }
392
393
        /**
394
         * This method initializes btnFastForward
395
         *
396
         * @return javax.swing.JButton
397
         */
398
        private JButton getBtnFastForward() {
399
                if (btnFastForward == null) {
400
                        btnFastForward = new JButton();
401
                        if (orientation==VERTICAL){
402
                                btnFastForward.setBounds(7, 91, 20, 24);
403
                        }else{
404
                                btnFastForward.setBounds(208, 1, 20, 24);
405
                        }
406
407
                        btnFastForward.setEnabled(itemCount!=0);
408
                        btnFastForward.addActionListener(new ActionListener() {
409
                                public void actionPerformed(ActionEvent e) {
410
                                        if (currentValue < itemCount-1){
411
                                                setValue(itemCount-1, true);
412
                                        }
413
                                }});
414
                        btnFastForward.setIcon(new ImageIcon(getClass().getResource("images/fastforward.png")));
415
                }
416
                return btnFastForward;
417
        }
418
419
        /**
420
         * This method initializes sliderPanel
421
         *
422
         * @return javax.swing.JPanel
423
         */
424
        private JPanel getSliderPanel() {
425
                if (sliderPanel == null) {
426
                        sliderPanel = new JPanel();
427
                        sliderPanel.setLayout(null);
428
                        sliderPanel.setName("sliderPanel");
429
                        if (orientation==VERTICAL){
430
                                sliderPanel.setBounds(3, 0, 35, 181);
431
                        }else{
432
                                sliderPanel.setBounds(5, 0, 300, 26);
433
                        }
434
                        sliderPanel.setEnabled(false);
435
                        sliderPanel.add(getSlider(), null);
436
                }
437
                return sliderPanel;
438
        }
439
440
        /**
441
         * This method initializes slider
442
         *
443
         * @return javax.swing.JSlider
444
         */
445
        private JSlider getSlider() {
446
                if (slider == null) {
447
                        slider = new JSlider();
448
                        slider.setValue(0);
449
                        if (orientation==VERTICAL){
450
                                slider.setOrientation(JSlider.VERTICAL);
451
                                slider.setSize(24, 230);
452
                        }else{
453
                                slider.setOrientation(JSlider.HORIZONTAL);
454
                                slider.setSize(230, 24);
455
                        }
456
457
                        slider.setLocation(0, 1);
458
                        slider.setEnabled(itemCount!=0);
459
                        slider.addMouseListener(new java.awt.event.MouseAdapter() {
460
                                public void mouseReleased(java.awt.event.MouseEvent e) {
461
                                        int value = (int) (getSlider().getValue() * itemCount * 0.01);
462
                                        if (value >= itemCount)
463
                                                value = itemCount - 1;
464
                                        refreshText(value);
465
                                        setValue(value, false);
466
                                }
467
                        });
468
                        slider.addChangeListener(new javax.swing.event.ChangeListener() {
469
                                public void stateChanged(javax.swing.event.ChangeEvent e) {
470
                                        int value = (int) (getSlider().getValue() * itemCount * 0.01);
471
                                        if (value >= itemCount)
472
                                                value = itemCount - 1;
473
                                        refreshText(value);
474
                                        if (!refreshing)
475
                                                callValueChanged(new Integer(value));
476
                                }
477
                        });
478
                }
479
                return slider;
480
        }
481
482
        /**
483
         * Sets the amount of items that this component will handle.
484
         * @param count
485
         */
486
        public void setItemCount(int count){
487
                itemCount = count;
488
                getSlider().setEnabled(count != 0);
489
                getBtnFastBackward().setEnabled(count != 0);
490
                getBtnBackward().setEnabled(count != 0);
491
                getTxtItemCountDisplay().setEnabled(count != 0);
492
                getBtnForward().setEnabled(count != 0);
493
                getBtnFastForward().setEnabled(count != 0);
494
        }
495
496
        /**
497
         * Sets the starting point if none is defined the pager will start from 0.
498
         * @param initial position
499
         */
500
        public void setStartingPosition(int initialPosition) {
501
                lowLimit = initialPosition;
502
        }
503
504
        public void setCurrentPosition(int pos) {
505
                setValue(pos, true);
506
        }
507
}  //  @jve:decl-index=0:visual-constraint="10,15"