Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

1
/* 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: Pager.java 13136 2007-08-20 08:38:34Z evercher $
45
 * $Log$
46
 * Revision 1.1  2007-08-20 08:34:45  evercher
47
 * He fusionado LibUI con LibUIComponents
48
 *
49
 * Revision 1.1  2006/03/22 11:18:29  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.4  2006/02/28 15:25:14  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.2.2.6  2006/02/23 10:36:30  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2.2.5  2006/02/16 10:36:41  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.1  2006/02/16 09:09:05  caballero
62
 * PAger de Jaume
63
 *
64
 * Revision 1.2.2.4  2006/02/10 13:23:18  jaume
65
 * page value can now be set externally
66
 *
67
 * Revision 1.2.2.3  2006/01/31 16:25:24  jaume
68
 * correcciones de bugs
69
 *
70
 * Revision 1.3  2006/01/26 16:07:14  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.2.2.1  2006/01/26 12:59:32  jaume
74
 * 0.5
75
 *
76
 * Revision 1.2  2006/01/24 14:36:33  jaume
77
 * This is the new version
78
 *
79
 * Revision 1.1.2.5  2006/01/19 16:09:30  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1.2.4  2006/01/17 17:05:39  jaume
83
 * fixed crazy buttons behavior :-P
84
 *
85
 * Revision 1.1.2.3  2006/01/17 17:01:55  jaume
86
 * fixed crazy buttons behavior :-P
87
 *
88
 * Revision 1.1.2.2  2006/01/11 12:20:30  jaume
89
 * *** empty log message ***
90
 *
91
 * Revision 1.1.2.1  2006/01/10 13:11:38  jaume
92
 * *** empty log message ***
93
 *
94
 * Revision 1.1.2.1  2006/01/10 11:33:31  jaume
95
 * Time dimension working against Jet Propulsion Laboratory's WMS server
96
 *
97
 * Revision 1.1.2.3  2006/01/09 18:10:38  jaume
98
 * casi con el time dimension
99
 *
100
 * Revision 1.1.2.2  2006/01/02 18:08:01  jaume
101
 * Tree de estilos
102
 *
103
 * Revision 1.1.2.1  2005/12/30 08:56:19  jaume
104
 * *** empty log message ***
105
 *
106
 *
107
 */
108
/**
109
 *
110
 */
111
package org.gvsig.gui.beans;
112

    
113
import java.awt.Dimension;
114
import java.awt.event.ActionEvent;
115
import java.awt.event.ActionListener;
116

    
117
import javax.swing.ImageIcon;
118
import javax.swing.JButton;
119
import javax.swing.JPanel;
120
import javax.swing.JSlider;
121
import javax.swing.JTextField;
122

    
123

    
124

    
125
/**
126
 * Bean that is useful to browse a very large list of data. It
127
 * includes a set of navigation buttons to step ahead or behind
128
 * by one or going to the first and last element of the list as
129
 * well as an slider and a text field for directly focus on a
130
 * list item.
131
 *
132
 * @author jaume dominguez faus
133
 *
134
 */
135
public class Pager extends DefaultBean {
136
        private JPanel buttonsPanel = null;
137
        private JButton btnFastBackward = null;
138
        private JButton btnBackward = null;
139
        private JTextField txtItemCountDisplay = null;
140
        private JButton btnForward = null;
141
        private JButton btnFastForward = null;
142
        private JPanel sliderPanel = null;
143
        private JSlider slider = null;
144
        private int itemCount;
145
        private int lowLimit;
146
        private int currentValue = -1;
147
        private int orientation;
148
        private boolean refreshing = false;
149
        public static int HORIZONTAL=0;
150
        public static int VERTICAL=1;
151
        /**
152
         * This is the default constructor. Creates a new instance of ItemBrowser with
153
         * zero items.
154
         */
155
        public Pager(int orientation){
156
                super();
157
                this.orientation=orientation;
158
                initialize(0, 0);
159
        }
160
        
161
        /**
162
         * Creates a new instance of ItemBrowser defining its edges
163
         * @param lowIndex, the lowest edge.
164
         * @param itemCount, the highest edge.
165
         */
166
        public Pager(int lowIndex, int itemCount,int orientation) {
167
                super();
168
                this.orientation=orientation;
169
                initialize(lowIndex, itemCount);
170
        }
171
        
172
        /**
173
         * This method initializes this
174
         *
175
         * @return void
176
         */
177
        private void initialize(int lowIndex, int itemCount) {
178
                setItemCount(itemCount);
179
                this.lowLimit = lowIndex;
180
                this.setLayout(null);
181
                if (orientation==VERTICAL){
182
                        this.setSize(45,305);
183
                        this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray,1));
184
                        this.setPreferredSize(new Dimension(40,300));
185
                }else{
186
                        this.setSize(240, 50);
187
                        this.setPreferredSize(new Dimension(190,50));
188
                }
189
                
190
                this.add(getSliderPanel(), null);
191
                this.add(getButtonsPanel(), null);
192
        }
193
        
194
        /**
195
         * This method initializes buttonsPanel
196
         *
197
         * @return javax.swing.JPanel
198
         */
199
        private JPanel getButtonsPanel() {
200
                if (buttonsPanel == null) {
201
                        buttonsPanel = new JPanel();
202
                        buttonsPanel.setLayout(null);
203
                        buttonsPanel.setName("buttonsPanel");
204
                        buttonsPanel.setPreferredSize(new java.awt.Dimension(173,50));
205
                        if (orientation==VERTICAL){
206
                                buttonsPanel.setBounds(3, 182, 35,115);
207
                        }else{
208
                                buttonsPanel.setBounds(5, 25, 240, 25);
209
                        }
210
                        
211
                        buttonsPanel.add(getBtnFastBackward(), null);
212
                        buttonsPanel.add(getBtnBackward(), null);
213
                        buttonsPanel.add(getTxtItemCountDisplay(), null);
214
                        buttonsPanel.add(getBtnForward(), null);
215
                        buttonsPanel.add(getBtnFastForward(), null);
216
                }
217
                return buttonsPanel;
218
        }
219
        
220
        /**
221
         * This method initializes btnFastBackWard
222
         *
223
         * @return javax.swing.JButton
224
         */
225
        private JButton getBtnFastBackward() {
226
                if (btnFastBackward == null) {
227
                        btnFastBackward = new JButton();
228
                        if (orientation==VERTICAL){
229
                                btnFastBackward.setBounds(7, 1, 20, 24);
230
                        }else{
231
                                btnFastBackward.setBounds(2, 1, 20, 24);
232
                        }
233
                        
234
                        btnFastBackward.setEnabled(itemCount!=0);
235
                        btnFastBackward.addActionListener(new ActionListener() {
236
                                public void actionPerformed(ActionEvent e) {
237
                                        if (currentValue != lowLimit){
238
                                                setValue(lowLimit, true);
239
                                        }
240
                                }});
241
                        btnFastBackward.setIcon(new ImageIcon(getClass().getResource("images/fastbackward.png")));
242
                }
243
                return btnFastBackward;
244
        }
245
        
246
        /**
247
         * This method initializes btnBackward
248
         *
249
         * @return javax.swing.JButton
250
         */
251
        private JButton getBtnBackward() {
252
                if (btnBackward == null) {
253
                        btnBackward = new JButton();
254
                        if (orientation==VERTICAL){
255
                                btnBackward.setBounds(7, 21, 20, 24);
256
                        }else{
257
                                btnBackward.setBounds(21, 1, 20, 24);
258
                        }
259
                        
260
                        btnBackward.setEnabled(itemCount!=0);
261
                        btnBackward.addActionListener(new ActionListener() {
262
                                public void actionPerformed(ActionEvent e) {
263
                                        if (currentValue > lowLimit ){
264
                                                setValue(currentValue-1, true);
265
                                        }
266
                                }});
267
                        btnBackward.setIcon(new ImageIcon(getClass().getResource("images/backward.png")));
268
                }
269
                return btnBackward;
270
        }
271
        
272
        /**
273
         * This method initializes txtItemCountDisplay
274
         *
275
         * @return javax.swing.JTextField
276
         */
277
        private JTextField getTxtItemCountDisplay() {
278
                if (txtItemCountDisplay == null) {
279
                        txtItemCountDisplay = new JTextField();
280
                        txtItemCountDisplay.setEnabled(itemCount!=0);
281
                        txtItemCountDisplay.setHorizontalAlignment(javax.swing.JTextField.CENTER);
282
                        if (orientation==VERTICAL){
283
                                txtItemCountDisplay.setBounds(2,43, 33, 23);
284
                        } else {
285
                                txtItemCountDisplay.setBounds(43, 2, 144, 23);
286
                        }
287
                        
288
                        txtItemCountDisplay.setText(lowLimit+" / "+itemCount);
289
                        txtItemCountDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
290
                                public void mouseClicked(java.awt.event.MouseEvent e) {
291
                                        txtItemCountDisplay.setText(currentValue+"");
292
                                        txtItemCountDisplay.setSelectionStart(0);
293
                                        txtItemCountDisplay.setSelectionEnd(txtItemCountDisplay.getText().length());
294
                                }
295
                        });
296
                        txtItemCountDisplay.addActionListener(new java.awt.event.ActionListener() {
297
                                public void actionPerformed(java.awt.event.ActionEvent e) {
298
                                        try {
299
                                                int v = Integer.parseInt(txtItemCountDisplay.getText());
300
                                                v = (v>itemCount) ? itemCount : v;
301
                                                setValue(v, true);
302
                                        } catch (Exception ex){
303
                                                refreshText(currentValue);
304
                                        }
305
                                        txtItemCountDisplay.transferFocusDownCycle();
306
                                }
307
                        });
308
                        txtItemCountDisplay.setEnabled(false);
309
                }
310
                return txtItemCountDisplay;
311
        }
312
        
313
        /**
314
         * This, sets the bean value and triggers an event that can be captured
315
         * by a listener.
316
         * @param number
317
         * @param fireEvent, if true then this method will fire the event. If false,
318
         * then the value will be changed silently.
319
         *
320
         */
321
        public void setValue(int number, boolean fireEvent) {
322
                if (number < lowLimit)
323
                        number = lowLimit;
324
                if (number > itemCount-1)
325
                        number = itemCount;
326
                if (number != currentValue) {
327
                        currentValue = number;
328
                        refreshControls();
329
                        if (fireEvent)
330
                                callValueChanged(new Integer(currentValue));
331
                }
332
        }
333
        
334
        /**
335
         * Refreshes all the mutable controls in this component.
336
         */
337
        private void refreshControls() {
338
                int normalizedValue = (int) ((currentValue / (float) itemCount)*100);
339
                refreshSlider(normalizedValue);
340
                refreshText(currentValue);
341
        }
342
        
343
        /**
344
         * Sets the slider to the correct (scaled) position.
345
         * @param normalizedValue
346
         */
347
        private void refreshSlider(int normalizedValue) {
348
                refreshing = true;
349
                getSlider().setValue(normalizedValue);
350
                refreshing = false;
351
        }
352
        
353
        /**
354
         * @param string
355
         */
356
        private void refreshText(int value) {
357
                String newText = (value+1) +" / "+itemCount;
358
                
359
                if (!getTxtItemCountDisplay().getText().equals(newText))
360
                        getTxtItemCountDisplay().setText(newText);
361
        }
362
        
363
        /**
364
         * This method initializes btnForward
365
         *
366
         * @return javax.swing.JButton
367
         */
368
        private JButton getBtnForward() {
369
                if (btnForward == null) {
370
                        btnForward = new JButton();
371
                        if (orientation==VERTICAL){
372
                                btnForward.setBounds(7, 67, 20, 24);
373
                        }else{
374
                                btnForward.setBounds(189, 1, 20, 24);
375
                        }
376
                        
377
                        btnForward.setEnabled(itemCount!=0);
378
                        btnForward.addActionListener(new ActionListener() {
379
                                public void actionPerformed(ActionEvent e) {
380
                                        if (currentValue < itemCount-1){
381
                                                setValue(currentValue+1, true);
382
                                        }
383
                                }});
384
                        btnForward.setIcon(new ImageIcon(getClass().getResource("images/forward.png")));
385
                }
386
                return btnForward;
387
        }
388
        
389
        /**
390
         * This method initializes btnFastForward
391
         *
392
         * @return javax.swing.JButton
393
         */
394
        private JButton getBtnFastForward() {
395
                if (btnFastForward == null) {
396
                        btnFastForward = new JButton();
397
                        if (orientation==VERTICAL){
398
                                btnFastForward.setBounds(7, 91, 20, 24);
399
                        }else{
400
                                btnFastForward.setBounds(208, 1, 20, 24);
401
                        }
402
                        
403
                        btnFastForward.setEnabled(itemCount!=0);
404
                        btnFastForward.addActionListener(new ActionListener() {
405
                                public void actionPerformed(ActionEvent e) {
406
                                        if (currentValue < itemCount-1){
407
                                                setValue(itemCount-1, true);
408
                                        }
409
                                }});
410
                        btnFastForward.setIcon(new ImageIcon(getClass().getResource("images/fastforward.png")));
411
                }
412
                return btnFastForward;
413
        }
414
        
415
        /**
416
         * This method initializes sliderPanel
417
         *
418
         * @return javax.swing.JPanel
419
         */
420
        private JPanel getSliderPanel() {
421
                if (sliderPanel == null) {
422
                        sliderPanel = new JPanel();
423
                        sliderPanel.setLayout(null);
424
                        sliderPanel.setName("sliderPanel");
425
                        if (orientation==VERTICAL){
426
                                sliderPanel.setBounds(3, 0, 35, 181);
427
                        }else{
428
                                sliderPanel.setBounds(5, 0, 300, 26);
429
                        }
430
                        sliderPanel.setEnabled(false);
431
                        sliderPanel.add(getSlider(), null);
432
                }
433
                return sliderPanel;
434
        }
435
        
436
        /**
437
         * This method initializes slider
438
         *
439
         * @return javax.swing.JSlider
440
         */
441
        private JSlider getSlider() {
442
                if (slider == null) {
443
                        slider = new JSlider();
444
                        slider.setValue(0);
445
                        if (orientation==VERTICAL){
446
                                slider.setOrientation(JSlider.VERTICAL);
447
                                slider.setSize(24, 230);
448
                        }else{
449
                                slider.setOrientation(JSlider.HORIZONTAL);
450
                                slider.setSize(230, 24);
451
                        }
452
                        
453
                        slider.setLocation(0, 1);
454
                        slider.setEnabled(itemCount!=0);
455
                        slider.addMouseListener(new java.awt.event.MouseAdapter() {
456
                                public void mouseReleased(java.awt.event.MouseEvent e) {
457
                                        int value = (int) (getSlider().getValue() * itemCount * 0.01);
458
                                        if (value >= itemCount)
459
                                                value = itemCount - 1;
460
                                        refreshText(value);
461
                                        setValue(value, false);
462
                                }
463
                        });
464
                        slider.addChangeListener(new javax.swing.event.ChangeListener() {
465
                                public void stateChanged(javax.swing.event.ChangeEvent e) {
466
                                        int value = (int) (getSlider().getValue() * itemCount * 0.01);
467
                                        if (value >= itemCount)
468
                                                value = itemCount - 1;
469
                                        refreshText(value);
470
                                        if (!refreshing)
471
                                                callValueChanged(new Integer(value));
472
                                }
473
                        });
474
                }
475
                return slider;
476
        }
477
        
478
        /**
479
         * Sets the amount of items that this component will handle.
480
         * @param count
481
         */
482
        public void setItemCount(int count){
483
                itemCount = count;
484
                getSlider().setEnabled(count != 0);
485
                getBtnFastBackward().setEnabled(count != 0);
486
                getBtnBackward().setEnabled(count != 0);
487
                getTxtItemCountDisplay().setEnabled(count != 0);
488
                getBtnForward().setEnabled(count != 0);
489
                getBtnFastForward().setEnabled(count != 0);
490
        }
491
        
492
        /**
493
         * Sets the starting point if none is defined the pager will start from 0.
494
         * @param initial position
495
         */
496
        public void setStartingPosition(int initialPosition) {
497
                lowLimit = initialPosition;
498
        }
499
        
500
        public void setCurrentPosition(int pos) {
501
                setValue(pos, true);
502
        }
503
}  //  @jve:decl-index=0:visual-constraint="10,15"