Statistics
| Revision:

root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / slidertext / SliderTextContainer.java @ 8026

History | View | Annotate | Download (4.97 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
package org.gvsig.gui.beans.slidertext;
20

    
21
import java.awt.GridBagConstraints;
22
import java.awt.GridBagLayout;
23

    
24
import javax.swing.JPanel;
25
import javax.swing.JSlider;
26
import javax.swing.JTextField;
27

    
28
import org.gvsig.gui.beans.BaseComponent;
29
import org.gvsig.gui.beans.slidertext.listeners.SliderTextListener;
30

    
31
/**
32
 * Barra de deslizamiento con una ventana de texto que tiene el valor de la posici?n de
33
 * la barra. En este control podr? controlarse mediante la entrada de datos por la caja de
34
 * texto la posibilidad de introducir valores decimales.
35
 *  
36
 * Nacho Brodin (brodin_ign@gva.es)
37
 */
38

    
39
public class SliderTextContainer extends BaseComponent{
40

    
41
        private int         MARGIN = 8;
42
        private int         WTEXT = 50;
43
        private JPanel pSlider = null;
44
        private JPanel pText = null;
45
        private JSlider slider = null;
46
        private JTextField text = null;
47
        private int        width = 260;
48
        private int        height = 35;
49
        private int min = 0;
50
        private int max = 255;
51
        private int defaultPos = 0;
52
        private SliderTextListener listener = null;
53
        
54
        /**
55
         * Contructor
56
         * @param width Ancho del componente
57
         * @param height Alto del componente
58
         * @param min Valor m?nimo de la barra
59
         * @param max Valor m?ximo de la barra
60
         * @param defaultPos Posici?n por defecto 
61
         */
62
        public SliderTextContainer(int width, int height, int min, int max, int defaultPos) {
63
                super();
64
                this.width = width;
65
                this.height = height;
66
                this.min = min;
67
                this.max = max;
68
                this.defaultPos = defaultPos;
69
        
70
                initialize();
71
        }
72

    
73
        /**
74
         * This method initializes this
75
         * 
76
         */
77
        private void initialize() {
78
                listener = new SliderTextListener(this);
79
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
80
        gridBagConstraints1.gridx = 1;
81
        gridBagConstraints1.gridy = 0;
82
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
83
        gridBagConstraints.gridx = 0;
84
        gridBagConstraints.gridy = 0;
85
        this.setLayout(new GridBagLayout());
86
        this.add(getPSlider(), gridBagConstraints);
87
        this.add(getPText(), gridBagConstraints1);
88
        this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
89
        setComponentSize(width, height);
90
        }
91

    
92
        /**
93
         * This method initializes jPanel        
94
         *         
95
         * @return javax.swing.JPanel        
96
         */
97
        private JPanel getPSlider() {
98
                if (pSlider == null) {
99
                        pSlider = new JPanel();
100
                        pSlider.add(getSlider(), null);
101
                }
102
                return pSlider;
103
        }
104

    
105
        /**
106
         * This method initializes jPanel1        
107
         *         
108
         * @return javax.swing.JPanel        
109
         */
110
        private JPanel getPText() {
111
                if (pText == null) {
112
                        pText = new JPanel();
113
                        pText.setPreferredSize(new java.awt.Dimension(WTEXT,35));
114
                        pText.add(getText(), null);
115
                }
116
                return pText;
117
        }
118

    
119
        /**
120
         * This method initializes jSlider        
121
         *         
122
         * @return javax.swing.JSlider        
123
         */
124
        public JSlider getSlider() {
125
                if (slider == null) {
126
                        slider = new JSlider();
127
                        slider.setMajorTickSpacing(50);
128
                        slider.setPaintTicks(true);
129
                        slider.setMinorTickSpacing(10);
130
                        slider.setMinimum(min);
131
                        slider.setMaximum(max);
132
                        slider.setValue(defaultPos);
133
                        slider.addChangeListener(listener);
134
                }
135
                return slider;
136
        }
137

    
138
        /**
139
         * This method initializes jTextField        
140
         *         
141
         * @return javax.swing.JTextField        
142
         */
143
        public JTextField getText() {
144
                if (text == null) {
145
                        text = new JTextField();
146
                        text.setText(defaultPos+"");
147
                        text.addFocusListener(listener);
148
                        text.addKeyListener(listener);
149
                }
150
                return text;
151
        }
152
        
153
        public void setComponentSize(int w, int h){
154
                this.width = w;
155
                this.height = h;
156
        this.setPreferredSize(new java.awt.Dimension(width, height));
157
                pSlider.setPreferredSize(new java.awt.Dimension(width - MARGIN - WTEXT, 27));
158
                pText.setPreferredSize(new java.awt.Dimension(WTEXT,35));
159
                slider.setPreferredSize(new java.awt.Dimension(width - WTEXT - MARGIN,27));
160
                text.setPreferredSize(new java.awt.Dimension(WTEXT - 10, 19));
161
        }
162
        
163
        /**
164
         * Obtiene el valor del control.
165
         * @return Valor del control en formato double.
166
         */
167
        public double getValue(){
168
                return Double.valueOf(getText().getText()).doubleValue();
169
        }
170

    
171
        /**
172
         * Asigna el valor del control.
173
         * @return Valor del control en formato double.
174
         */
175
        public void setValue(double value){
176
                getText().setText(String.valueOf(value));
177
                getSlider().setValue((int)value);
178
        }
179
}