Statistics
| Revision:

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

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

    
98
import java.awt.Dimension;
99
import java.awt.event.ActionEvent;
100
import java.awt.event.ActionListener;
101

    
102
import javax.swing.ImageIcon;
103
import javax.swing.JButton;
104
import javax.swing.JPanel;
105
import javax.swing.JSlider;
106
import javax.swing.JTextField;
107

    
108

    
109

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