root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / table / models / JValueSelector.java @ 8026
History | View | Annotate | Download (7.31 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.table.models; |
20 |
|
21 |
import java.awt.Component; |
22 |
import java.awt.Container; |
23 |
import java.awt.FlowLayout; |
24 |
import java.awt.GridBagConstraints; |
25 |
import java.awt.GridBagLayout; |
26 |
import java.awt.HeadlessException; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
|
30 |
import javax.swing.JButton; |
31 |
import javax.swing.JComponent; |
32 |
import javax.swing.JDialog; |
33 |
import javax.swing.JOptionPane; |
34 |
import javax.swing.JPanel; |
35 |
|
36 |
import org.gvsig.gui.beans.slidertext.SliderTextContainer; |
37 |
|
38 |
/**
|
39 |
* Ventana con el slider para la selecci?n de Alpha en una entrada de la tabla que representa
|
40 |
* a la paleta de color.
|
41 |
*
|
42 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
43 |
*
|
44 |
*/
|
45 |
|
46 |
public class JValueSelector extends JComponent { |
47 |
/**
|
48 |
* Creates and returns a new dialog containing the specified
|
49 |
* <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
|
50 |
* buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is
|
51 |
* automatically hidden (but not disposed). If the "Reset"
|
52 |
* button is pressed, the color-chooser's color will be reset to the
|
53 |
* color which was set the last time <code>show</code> was invoked on the
|
54 |
* dialog and the dialog will remain showing.
|
55 |
*
|
56 |
* @param c the parent component for the dialog
|
57 |
* @param title the title for the dialog
|
58 |
* @param modal a boolean. When true, the remainder of the program
|
59 |
* is inactive until the dialog is closed.
|
60 |
* @param valueSelector the value-selector to be placed inside the dialog
|
61 |
* @param okListener the ActionListener invoked when "OK" is pressed
|
62 |
* @param cancelListener the ActionListener invoked when "Cancel" is pressed
|
63 |
* @return a new dialog containing the color-chooser pane
|
64 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
|
65 |
* returns true.
|
66 |
* @see java.awt.GraphicsEnvironment#isHeadless
|
67 |
*/
|
68 |
public static JDialog createDialog(Component c, String title, boolean modal, |
69 |
JValueSelector valueSelector, ActionListener okListener,
|
70 |
ActionListener cancelListener, String initValue) throws HeadlessException { |
71 |
|
72 |
return new ValueSelector(c, title, modal, valueSelector, |
73 |
okListener, cancelListener, initValue); |
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
class ValueSelector extends JDialog implements ActionListener { |
79 |
|
80 |
private JValueSelector valueSelector;
|
81 |
private int initValue; |
82 |
private ActionListener okListener; |
83 |
private ActionListener cancelListener; |
84 |
|
85 |
private int MARGIN = 0; |
86 |
private int HBUTTONS = 30; |
87 |
private int HSLIDER = 50; |
88 |
private JPanel buttons = null; |
89 |
private JPanel pMain = null; |
90 |
private SliderTextContainer pSlider = null; |
91 |
private int width = 315, height = 115; |
92 |
private int widthMargin = 300; |
93 |
private JButton bAccept = null; |
94 |
private JButton bCancel = null; |
95 |
|
96 |
/**
|
97 |
* Constructor
|
98 |
* @param pp PalettePanel
|
99 |
*/
|
100 |
public ValueSelector(Component c, String title, boolean modal, |
101 |
JValueSelector valueSelector, ActionListener okListener,
|
102 |
ActionListener cancelListener, String initValue)throws HeadlessException { |
103 |
|
104 |
super(JOptionPane.getFrameForComponent(c), title, modal); |
105 |
this.okListener = okListener;
|
106 |
this.cancelListener = cancelListener;
|
107 |
|
108 |
this.valueSelector = valueSelector;
|
109 |
|
110 |
try{
|
111 |
init(Integer.valueOf(initValue).intValue());
|
112 |
this.initValue = Integer.valueOf(initValue).intValue(); |
113 |
}catch(NumberFormatException ex){ |
114 |
init(0);
|
115 |
} |
116 |
|
117 |
setLocationRelativeTo(c); |
118 |
} |
119 |
|
120 |
public void init(int initValue){ |
121 |
getPSlider().setValue(initValue); |
122 |
|
123 |
Container contentPane = getContentPane();
|
124 |
FlowLayout layout = new FlowLayout(); |
125 |
layout.setHgap(0);
|
126 |
layout.setVgap(0);
|
127 |
contentPane.setLayout(layout); |
128 |
setSize(width, height + 10);
|
129 |
contentPane.add(getPMain(), null);
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Asigna el valor de inicializaci?n para el Slider
|
134 |
* @param value
|
135 |
*/
|
136 |
public void setValue(int value){ |
137 |
this.initValue = value;
|
138 |
this.getPSlider().setValue((double)value); |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Asigna el valor de inicializaci?n para el Slider
|
143 |
* @param value
|
144 |
*/
|
145 |
public double getValue(){ |
146 |
return initValue;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* Obtiene el panel principal que contiene el panel de botones y el panel
|
151 |
* con el slider
|
152 |
* @return JPanel
|
153 |
*/
|
154 |
private JPanel getPMain(){ |
155 |
if(pMain == null){ |
156 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
157 |
gridBagConstraints.gridy = 0;
|
158 |
gridBagConstraints.gridx = 0;
|
159 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
160 |
gridBagConstraints1.gridy = 1;
|
161 |
gridBagConstraints1.gridx = 0;
|
162 |
pMain = new JPanel(); |
163 |
pMain.setLayout(new GridBagLayout()); |
164 |
pMain.setPreferredSize(new java.awt.Dimension(widthMargin, height));
|
165 |
pMain.add(getPSlider(), gridBagConstraints); |
166 |
pMain.add(getPButtons(), gridBagConstraints1); |
167 |
} |
168 |
return pMain;
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Obtiene el panel de botones
|
173 |
* @return JPanel
|
174 |
*/
|
175 |
private JPanel getPButtons(){ |
176 |
if(buttons == null){ |
177 |
buttons = new JPanel(); |
178 |
buttons.setPreferredSize(new java.awt.Dimension(widthMargin, HBUTTONS));
|
179 |
FlowLayout flowLayout = new FlowLayout(); |
180 |
flowLayout.setAlignment(java.awt.FlowLayout.RIGHT); |
181 |
flowLayout.setVgap(0);
|
182 |
flowLayout.setHgap(2);
|
183 |
buttons.setLayout(flowLayout); |
184 |
buttons.add(getBAccept(), null);
|
185 |
buttons.add(getBCancel(), null);
|
186 |
} |
187 |
return buttons;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Obtiene el panel con el slider
|
192 |
* @return
|
193 |
*/
|
194 |
public SliderTextContainer getPSlider(){
|
195 |
if(pSlider == null){ |
196 |
pSlider = new SliderTextContainer(widthMargin, HSLIDER, 0, 255, this.initValue); |
197 |
pSlider.setPreferredSize(new java.awt.Dimension(widthMargin, HSLIDER));
|
198 |
} |
199 |
return pSlider;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* Obtiene el bot?n de aceptar
|
204 |
* @return JButton
|
205 |
*/
|
206 |
public JButton getBAccept(){ |
207 |
if(bAccept == null){ |
208 |
bAccept = new JButton("aceptar"); |
209 |
bAccept.addActionListener(okListener); |
210 |
bAccept.addActionListener(this);
|
211 |
} |
212 |
return bAccept;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Obtiene el bot?n de cancelar
|
217 |
* @return JButton
|
218 |
*/
|
219 |
public JButton getBCancel(){ |
220 |
if(bCancel == null){ |
221 |
bCancel = new JButton("cancelar"); |
222 |
bAccept.addActionListener(cancelListener); |
223 |
bCancel.addActionListener(this);
|
224 |
} |
225 |
return bCancel;
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* Al aceptar asignamos el valor del slider al bot?n de alpha y al cancelar cerramos la ventana
|
230 |
*/
|
231 |
public void actionPerformed(ActionEvent e) { |
232 |
if(e.getSource() == this.getBAccept()){ |
233 |
initValue = (int)getPSlider().getValue();
|
234 |
} |
235 |
this.setVisible(false); |
236 |
} |
237 |
|
238 |
} |