Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_912 / libraries / libUI / src / org / gvsig / gui / beans / Pager.java @ 11422

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

    
110
import java.awt.Dimension;
111
import java.awt.event.ActionEvent;
112
import java.awt.event.ActionListener;
113

    
114
import javax.swing.ImageIcon;
115
import javax.swing.JButton;
116
import javax.swing.JPanel;
117
import javax.swing.JSlider;
118
import javax.swing.JTextField;
119

    
120

    
121

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