root / trunk / libraries / libCq_CMS_praster / src / org / cresques / ui / filter / RasterTransparencyPanel.java @ 8026
History | View | Annotate | Download (13.8 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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 2
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.ui.filter; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.ComponentEvent; |
32 |
import java.awt.event.ComponentListener; |
33 |
import java.awt.event.FocusEvent; |
34 |
import java.awt.event.FocusListener; |
35 |
|
36 |
import javax.swing.JCheckBox; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JSlider; |
39 |
import javax.swing.JTextField; |
40 |
import javax.swing.event.ChangeEvent; |
41 |
import javax.swing.event.ChangeListener; |
42 |
|
43 |
import org.cresques.ui.raster.RGBInputPanel; |
44 |
|
45 |
import java.awt.FlowLayout; |
46 |
import java.awt.Insets; |
47 |
|
48 |
|
49 |
/**
|
50 |
* Dialogo para asignar la transparencia por pixel y global al raster.
|
51 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
52 |
*/
|
53 |
public class RasterTransparencyPanel extends JPanel implements ComponentListener, ActionListener, FocusListener, ChangeListener{ |
54 |
final private static long serialVersionUID = -3370601314380922368L; |
55 |
|
56 |
/**
|
57 |
* Nombre del panel
|
58 |
*/
|
59 |
private String nom = "Transparencia"; |
60 |
FilterRasterDialogPanel parent = null;
|
61 |
|
62 |
/**
|
63 |
* N?mero de bandas del raster
|
64 |
*/
|
65 |
public int nBands = 3; |
66 |
private JPanel jPanel1 = null; |
67 |
private JCheckBox cbTransparencia = null; |
68 |
|
69 |
/**
|
70 |
* Variable p?blica para la traducci?n
|
71 |
*/
|
72 |
public JPanel pGeneralTrans = null; |
73 |
private JCheckBox cbOpacidad = null; |
74 |
private JSlider slOpacidad = null; |
75 |
private JTextField tOpacidad = null; |
76 |
private TransparencyByPixelPanel pTranspByPixel = null; |
77 |
private JPanel pTransparencyByPixel = null; |
78 |
|
79 |
/**
|
80 |
* Constructor. Asigna la variable panel que contiene este
|
81 |
* @param parent FilterRasterDialogPanel
|
82 |
*/
|
83 |
public RasterTransparencyPanel(FilterRasterDialogPanel parent) {
|
84 |
this.parent = parent;
|
85 |
initialize(); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* This method initializes this
|
90 |
*
|
91 |
* @return void
|
92 |
*/
|
93 |
private void initialize() { |
94 |
GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
95 |
gridBagConstraints4.insets = new java.awt.Insets(0,0,0,0); |
96 |
gridBagConstraints4.gridx = 0;
|
97 |
gridBagConstraints4.gridy = 1;
|
98 |
gridBagConstraints4.gridwidth = 1;
|
99 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
100 |
gridBagConstraints1.gridx = 0;
|
101 |
gridBagConstraints1.gridy = 0;
|
102 |
this.setLayout(new GridBagLayout()); |
103 |
this.setBounds(0, 0, 445, 239); |
104 |
this.setPreferredSize(new java.awt.Dimension(445, 239)); |
105 |
this.add(getPTransparencyByPixel(), gridBagConstraints1);
|
106 |
this.add(getOpacityPanel(), gridBagConstraints4);
|
107 |
initControls(); |
108 |
this.addComponentListener(this); |
109 |
} |
110 |
|
111 |
/**
|
112 |
* Obtiene el nombre del panel
|
113 |
* @return Cadena con el nombre del panel
|
114 |
*/
|
115 |
public String getName(){ |
116 |
return this.nom; |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Asigna el n?mero de bandas de la imagen
|
121 |
* @param nBands
|
122 |
*/
|
123 |
public void setBands(int nBands) { |
124 |
this.nBands = nBands;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Inicializa controles a sus valores por defecto
|
129 |
*/
|
130 |
public void initControls() { |
131 |
this.getOpacityText().setText("100"); |
132 |
this.setActiveOpacityControl(false); |
133 |
this.setActiveTransparencyControl(false); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* This method initializes jPanel1
|
138 |
*
|
139 |
* @return javax.swing.JPanel
|
140 |
*/
|
141 |
private JPanel getJPanel1() { |
142 |
if (jPanel1 == null) { |
143 |
jPanel1 = new JPanel(); |
144 |
jPanel1.setLayout(new BorderLayout()); |
145 |
jPanel1.setPreferredSize(new java.awt.Dimension(415,25)); |
146 |
jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0)); |
147 |
jPanel1.add(getTransparencyCheck(), java.awt.BorderLayout.NORTH); |
148 |
} |
149 |
|
150 |
return jPanel1;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* This method initializes jCheckBox
|
155 |
*
|
156 |
* @return javax.swing.JCheckBox
|
157 |
*/
|
158 |
public JCheckBox getTransparencyCheck() { |
159 |
if (cbTransparencia == null) { |
160 |
cbTransparencia = new JCheckBox(); |
161 |
cbTransparencia.setText("Activar");
|
162 |
cbTransparencia.addActionListener(this);
|
163 |
} |
164 |
|
165 |
return cbTransparencia;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* This method initializes jPanel4
|
170 |
*
|
171 |
* @return javax.swing.JPanel
|
172 |
*/
|
173 |
public JPanel getOpacityPanel() { |
174 |
if (pGeneralTrans == null) { |
175 |
pGeneralTrans = new JPanel(); |
176 |
pGeneralTrans.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opacidad", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
177 |
pGeneralTrans.setPreferredSize(new java.awt.Dimension(430,55)); |
178 |
pGeneralTrans.add(getOpacityCheck(), null);
|
179 |
pGeneralTrans.add(getOpacitySlider(), null);
|
180 |
pGeneralTrans.add(getOpacityText(), null);
|
181 |
} |
182 |
|
183 |
return pGeneralTrans;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* This method initializes jCheckBox1
|
188 |
*
|
189 |
* @return javax.swing.JCheckBox
|
190 |
*/
|
191 |
public JCheckBox getOpacityCheck() { |
192 |
if (cbOpacidad == null) { |
193 |
cbOpacidad = new JCheckBox(); |
194 |
cbOpacidad.setText("Activar");
|
195 |
cbOpacidad.addActionListener(this);
|
196 |
cbOpacidad.addFocusListener(this);
|
197 |
} |
198 |
|
199 |
return cbOpacidad;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* This method initializes jSlider
|
204 |
*
|
205 |
* @return javax.swing.JSlider
|
206 |
*/
|
207 |
public JSlider getOpacitySlider() { |
208 |
if (slOpacidad == null) { |
209 |
slOpacidad = new JSlider(); |
210 |
slOpacidad.setPreferredSize(new java.awt.Dimension(270, 16)); |
211 |
slOpacidad.addChangeListener(this);
|
212 |
} |
213 |
|
214 |
return slOpacidad;
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* This method initializes jTextField3
|
219 |
*
|
220 |
* @return javax.swing.JTextField
|
221 |
*/
|
222 |
public JTextField getOpacityText() { |
223 |
if (tOpacidad == null) { |
224 |
tOpacidad = new JTextField(); |
225 |
tOpacidad.setPreferredSize(new java.awt.Dimension(30, 19)); |
226 |
tOpacidad.addActionListener(this);
|
227 |
} |
228 |
|
229 |
return tOpacidad;
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* Activa/Desactiva los controles de opacidad
|
234 |
* @param active
|
235 |
*/
|
236 |
public void setActiveOpacityControl(boolean active) { |
237 |
this.getOpacityCheck().setSelected(active);
|
238 |
this.getOpacitySlider().setEnabled(active);
|
239 |
this.getOpacityText().setEnabled(active);
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* Activa/Desactiva los controles de transparencia
|
244 |
* @param active
|
245 |
*/
|
246 |
public void setActiveTransparencyControl(boolean active) { |
247 |
this.getTransparencyCheck().setSelected(active);
|
248 |
RGBInputPanel rgbPanel = this.getPTranspByPixel().getPRGBInput();
|
249 |
rgbPanel.getTRed().setEnabled(active); |
250 |
|
251 |
if (parent.nbands == 2) { |
252 |
rgbPanel.getTGreen().setEnabled(active); |
253 |
rgbPanel.getTBlue().setEnabled(false);
|
254 |
} |
255 |
|
256 |
if (parent.nbands == 3) { |
257 |
rgbPanel.getTGreen().setEnabled(active); |
258 |
rgbPanel.getTBlue().setEnabled(active); |
259 |
} |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Asigna el valor de opacidad a los controles de la ventana
|
264 |
* para que cuando esta se abra tenga los valores seleccionados en la
|
265 |
* imagen.
|
266 |
* @param alpha
|
267 |
*/
|
268 |
public void setOpacity(int alpha) { |
269 |
int opacityPercent = (int) ((alpha * 100) / 255); |
270 |
this.getOpacityText().setText(String.valueOf(opacityPercent)); |
271 |
this.getOpacitySlider().setValue(opacityPercent);
|
272 |
this.setActiveOpacityControl(true); |
273 |
|
274 |
if (opacityPercent == 100) { |
275 |
this.setActiveOpacityControl(false); |
276 |
} |
277 |
} |
278 |
|
279 |
/**
|
280 |
* This method initializes jPanel2
|
281 |
*
|
282 |
* @return javax.swing.JPanel
|
283 |
*/
|
284 |
public TransparencyByPixelPanel getPTranspByPixel() {
|
285 |
if (pTranspByPixel == null) { |
286 |
pTranspByPixel = new TransparencyByPixelPanel();
|
287 |
pTranspByPixel.setPreferredSize(new java.awt.Dimension(410,120)); |
288 |
pTranspByPixel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0)); |
289 |
pTranspByPixel.setControlEnabled(false);
|
290 |
} |
291 |
return pTranspByPixel;
|
292 |
} |
293 |
|
294 |
/**
|
295 |
* This method initializes jPanel2
|
296 |
*
|
297 |
* @return javax.swing.JPanel
|
298 |
*/
|
299 |
public JPanel getPTransparencyByPixel() { |
300 |
if (pTransparencyByPixel == null) { |
301 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
302 |
gridBagConstraints.gridx = 0;
|
303 |
gridBagConstraints.gridy = 1;
|
304 |
GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
305 |
gridBagConstraints2.anchor = GridBagConstraints.WEST;
|
306 |
gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0); |
307 |
gridBagConstraints2.gridheight = 1;
|
308 |
gridBagConstraints2.gridwidth = 1;
|
309 |
gridBagConstraints2.gridx = 0;
|
310 |
gridBagConstraints2.gridy = 0;
|
311 |
gridBagConstraints2.ipady = 0;
|
312 |
gridBagConstraints2.fill = GridBagConstraints.NONE;
|
313 |
pTransparencyByPixel = new JPanel(); |
314 |
pTransparencyByPixel.setLayout(new GridBagLayout()); |
315 |
pTransparencyByPixel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transparencia por pixel", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
316 |
pTransparencyByPixel.setPreferredSize(new java.awt.Dimension(430,180)); |
317 |
pTransparencyByPixel.add(getJPanel1(), gridBagConstraints2); |
318 |
pTransparencyByPixel.add(getPTranspByPixel(), gridBagConstraints); |
319 |
} |
320 |
return pTransparencyByPixel;
|
321 |
} |
322 |
|
323 |
public void componentHidden(ComponentEvent e) { |
324 |
// TODO Auto-generated method stub
|
325 |
|
326 |
} |
327 |
|
328 |
public void componentMoved(ComponentEvent e) { |
329 |
// TODO Auto-generated method stub
|
330 |
|
331 |
} |
332 |
/**
|
333 |
* Redimensiona el panel cuando se redimensiona el contenedor de ?ste
|
334 |
*/
|
335 |
public void componentResized(ComponentEvent e) { |
336 |
/*if (e.getSource() == this){
|
337 |
int nWidth = this.getSize().width;
|
338 |
int nHeight = this.getSize().height;
|
339 |
int difWidth = nWidth - 445;
|
340 |
int difHeight = nHeight - 239;
|
341 |
this.pTransparencyByPixel.setSize(this.pTransparencyByPixel.getSize().width + difWidth, this.pTransparencyByPixel.getSize().height + difHeight);
|
342 |
this.pTransparencyByPixel.setLocation(14, 19);
|
343 |
this.pGeneralTrans.setSize(this.pGeneralTrans.getSize().width + difWidth, this.pGeneralTrans.getSize().height);
|
344 |
this.pGeneralTrans.setLocation(14, this.pGeneralTrans.getLocation().y + difHeight/2);
|
345 |
this.slOpacidad.setSize(this.slOpacidad.getSize().width + difWidth, this.slOpacidad.getSize().height);
|
346 |
this.tOpacidad.setLocation(this.tOpacidad.getLocation().x + difWidth, this.tOpacidad.getLocation().y);
|
347 |
this.pTranspByPixel.setLocation(this.pTranspByPixel.getLocation().x + difWidth/2, this.pTranspByPixel.getLocation().y + difHeight/2);
|
348 |
}*/
|
349 |
|
350 |
} |
351 |
|
352 |
public void componentShown(ComponentEvent e) { |
353 |
// TODO Auto-generated method stub
|
354 |
|
355 |
} |
356 |
|
357 |
/**
|
358 |
* Eventos sobre TextField y CheckBox. Controla eventos de checkbox de opacidad, transparencia,
|
359 |
* recorte de colas y los textfield de opacidad, valores de transparencia por banda y
|
360 |
* porcentaje de recorte.
|
361 |
*/
|
362 |
public void actionPerformed(ActionEvent e) { |
363 |
//Evento sobre el checkbox de opacidad
|
364 |
if (e.getSource().equals(getOpacityCheck())) {
|
365 |
//Check de opacidad activado -> Activar controles de opacidad
|
366 |
if (getOpacityCheck().isSelected()) {
|
367 |
getOpacitySlider().setEnabled(true);
|
368 |
getOpacityText().setEnabled(true);
|
369 |
} else {
|
370 |
getOpacitySlider().setEnabled(false);
|
371 |
getOpacityText().setEnabled(false);
|
372 |
} |
373 |
} |
374 |
|
375 |
//Evento sobre el checkbox de transparencia
|
376 |
if (e.getSource().equals(getTransparencyCheck())) {
|
377 |
//Check de opacidad activado -> Activar controles de opacidad
|
378 |
if (getTransparencyCheck().isSelected()) {
|
379 |
getPTranspByPixel().setControlEnabled(true);
|
380 |
getPTranspByPixel().getPRGBInput().setActiveBands(nBands); |
381 |
}else
|
382 |
getPTranspByPixel().setControlEnabled(false);
|
383 |
} |
384 |
|
385 |
//Evento sobre el textfield de opacidad
|
386 |
if (e.getSource().equals(getOpacityText()))
|
387 |
checkOpacityText(); |
388 |
} |
389 |
|
390 |
public void focusGained(FocusEvent e) { |
391 |
//pTrans.updateTextBox();
|
392 |
} |
393 |
|
394 |
public void focusLost(FocusEvent e) { |
395 |
checkOpacityText(); |
396 |
} |
397 |
|
398 |
/*
|
399 |
*
|
400 |
*/
|
401 |
public void stateChanged(ChangeEvent e) { |
402 |
//Ponemos el valor del texto de la opacidad de pendiendo de la posici?n del Slider
|
403 |
if (e.getSource().equals(getOpacitySlider())) {
|
404 |
getOpacityText().setText( |
405 |
String.valueOf(getOpacitySlider().getValue()));
|
406 |
} |
407 |
} |
408 |
|
409 |
/**
|
410 |
* Controla que si el formato introducido en el textfield
|
411 |
* de opacidad es numerico se actualiza la posici?n del
|
412 |
* slider.
|
413 |
*/
|
414 |
private void checkOpacityText() { |
415 |
String op = getOpacityText().getText();
|
416 |
int value = 0; |
417 |
|
418 |
try {
|
419 |
if (!op.equals("")) { |
420 |
value = Integer.parseInt(op);
|
421 |
} |
422 |
|
423 |
getOpacitySlider().setValue(value); |
424 |
} catch (NumberFormatException exc) { |
425 |
System.err.println("Formato no numerico"); |
426 |
getOpacityText().setText("100");
|
427 |
getOpacitySlider().setValue(100);
|
428 |
} |
429 |
} |
430 |
|
431 |
} // @jve:decl-index=0:visual-constraint="36,15"
|