Statistics
| Revision:

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

History | View | Annotate | Download (7.19 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
package org.gvsig.gui.beans.table.models;
25

    
26
import java.awt.Component;
27
import java.awt.Container;
28
import java.awt.FlowLayout;
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.HeadlessException;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34

    
35
import javax.swing.JButton;
36
import javax.swing.JDialog;
37
import javax.swing.JOptionPane;
38
import javax.swing.JPanel;
39

    
40
import org.gvsig.gui.beans.slidertext.SliderTextContainer;
41

    
42
/**
43
 * Ventana con el slider para la selecci?n de Alpha en una entrada de la tabla que representa
44
 * a la paleta de color.
45
 *
46
 * @author Nacho Brodin (brodin_ign@gva.es)
47
 *
48
 */
49

    
50
public class JValueSelector extends JPanel {
51
        private static final long serialVersionUID = -427287107697905904L;
52

    
53
        /**
54
                 * Creates and returns a new dialog containing the specified
55
                 * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
56
                 * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
57
                 * automatically hidden (but not disposed).  If the "Reset"
58
                 * button is pressed, the color-chooser's color will be reset to the
59
                 * color which was set the last time <code>show</code> was invoked on the
60
                 * dialog and the dialog will remain showing.
61
                 *
62
                 * @param c              the parent component for the dialog
63
                 * @param title          the title for the dialog
64
                 * @param modal          a boolean. When true, the remainder of the program
65
                 *                       is inactive until the dialog is closed.
66
                 * @param valueSelector  the value-selector to be placed inside the dialog
67
                 * @param okListener     the ActionListener invoked when "OK" is pressed
68
                 * @param cancelListener the ActionListener invoked when "Cancel" is pressed
69
                 * @return a new dialog containing the color-chooser pane
70
                 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
71
                 * returns true.
72
                 * @see java.awt.GraphicsEnvironment#isHeadless
73
                 */
74
                public static JDialog createDialog(Component c, String title, boolean modal,
75
                                JValueSelector valueSelector, ActionListener okListener,
76
                                ActionListener cancelListener, String initValue) throws HeadlessException {
77

    
78
                                return new ValueSelector(c, title, modal, valueSelector,
79
                                                                                                                                                                                                okListener, cancelListener, initValue);
80
                }
81

    
82
}
83

    
84
class ValueSelector extends JDialog implements ActionListener {
85
        private static final long serialVersionUID = -1510744508318912657L;
86
        private int                                         initValue;
87
        private ActionListener                        okListener;
88
        private ActionListener                        cancelListener;
89

    
90
        private int                                         HBUTTONS = 30;
91
        private int                                         HSLIDER = 50;
92
        private JPanel                                        buttons = null;
93
        private JPanel                                        pMain = null;
94
        private SliderTextContainer         pSlider = null;
95
        private int                                         width = 315, height = 115;
96
        private int                                         widthMargin = 300;
97
        private JButton                                bAccept = null;
98
        private JButton                                bCancel = null;
99

    
100
        /**
101
         * Constructor
102
         * @param pp PalettePanel
103
         */
104
        public ValueSelector(Component c, String title, boolean modal,
105
                                        JValueSelector valueSelector, ActionListener okListener,
106
                                        ActionListener cancelListener, String initValue)throws HeadlessException {
107

    
108
                                super(JOptionPane.getFrameForComponent(c), title, modal);
109
                                this.okListener = okListener;
110
                                this.cancelListener = cancelListener;
111

    
112
                                try{
113
                                        init(Integer.valueOf(initValue).intValue());
114
                                        this.initValue = Integer.valueOf(initValue).intValue();
115
                                }catch(NumberFormatException ex){
116
                                        init(0);
117
                                }
118

    
119
                                setLocationRelativeTo(c);
120
        }
121

    
122
        public void init(int initValue){
123
                getPSlider().setValue(initValue);
124

    
125
                Container contentPane = getContentPane();
126
                FlowLayout layout = new FlowLayout();
127
                layout.setHgap(0);
128
                layout.setVgap(0);
129
                                contentPane.setLayout(layout);
130
                                setSize(width, height + 10);
131
                                contentPane.add(getPMain(), null);
132
        }
133

    
134
        /**
135
         * Asigna el valor de inicializaci?n para el Slider
136
         * @param value
137
         */
138
        public void setValue(int value){
139
                        this.initValue = value;
140
                        this.getPSlider().setValue((double)value);
141
                }
142

    
143
        /**
144
         * Asigna el valor de inicializaci?n para el Slider
145
         * @param value
146
         */
147
        public double getValue(){
148
                 return initValue;
149
                }
150

    
151
        /**
152
         * Obtiene el panel principal que contiene el panel de botones y el panel
153
         * con el slider
154
         * @return JPanel
155
         */
156
        private JPanel getPMain(){
157
                if(pMain == null){
158
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
159
                        gridBagConstraints.gridy = 0;
160
                        gridBagConstraints.gridx = 0;
161
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
162
                        gridBagConstraints1.gridy = 1;
163
                        gridBagConstraints1.gridx = 0;
164
                        pMain = new JPanel();
165
                        pMain.setLayout(new GridBagLayout());
166
                        pMain.setPreferredSize(new java.awt.Dimension(widthMargin, height));
167
                        pMain.add(getPSlider(), gridBagConstraints);
168
                        pMain.add(getPButtons(), gridBagConstraints1);
169
                }
170
                return pMain;
171
        }
172

    
173
        /**
174
         * Obtiene el panel de botones
175
         * @return JPanel
176
         */
177
        private JPanel getPButtons(){
178
                if(buttons == null){
179
                        buttons = new JPanel();
180
                        buttons.setPreferredSize(new java.awt.Dimension(widthMargin, HBUTTONS));
181
                        FlowLayout flowLayout = new FlowLayout();
182
                        flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
183
                        flowLayout.setVgap(0);
184
                        flowLayout.setHgap(2);
185
                        buttons.setLayout(flowLayout);
186
                        buttons.add(getBAccept(), null);
187
                        buttons.add(getBCancel(), null);
188
                }
189
                return buttons;
190
        }
191

    
192
        /**
193
         * Obtiene el panel con el slider
194
         * @return
195
         */
196
        public SliderTextContainer getPSlider(){
197
                if(pSlider == null){
198
                        pSlider = new SliderTextContainer(0, 255, this.initValue);
199
                        pSlider.setPreferredSize(new java.awt.Dimension(widthMargin, HSLIDER));
200
                }
201
                return pSlider;
202
        }
203

    
204
        /**
205
         * Obtiene el bot?n de aceptar
206
         * @return JButton
207
         */
208
        public JButton getBAccept(){
209
                if(bAccept == null){
210
                        bAccept = new JButton("aceptar");
211
                        bAccept.addActionListener(okListener);
212
                        bAccept.addActionListener(this);
213
                }
214
                return bAccept;
215
        }
216

    
217
        /**
218
         * Obtiene el bot?n de cancelar
219
         * @return JButton
220
         */
221
        public JButton getBCancel(){
222
                if(bCancel == null){
223
                        bCancel = new JButton("cancelar");
224
                        bAccept.addActionListener(cancelListener);
225
                        bCancel.addActionListener(this);
226
                }
227
                return bCancel;
228
        }
229

    
230
        /**
231
         * Al aceptar asignamos el valor del slider al bot?n de alpha y al cancelar cerramos la ventana
232
         */
233
        public void actionPerformed(ActionEvent e) {
234
                if(e.getSource() == this.getBAccept()){
235
                        initValue = (int)getPSlider().getValue();
236
                }
237
                this.setVisible(false);
238
        }
239

    
240
}