Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_19_ext3D_osgVP_2_2_0 / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / layer / TransparencyPanel.java @ 31641

History | View | Annotate | Download (8.5 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.gvsig3dgui.layer;
42

    
43
import java.awt.Dimension;
44
import java.awt.GridBagConstraints;
45
import java.awt.Insets;
46
import java.awt.event.ActionListener;
47
import java.util.Dictionary;
48
import java.util.Hashtable;
49

    
50
import javax.swing.JLabel;
51
import javax.swing.JSlider;
52
import javax.swing.event.ChangeEvent;
53
import javax.swing.event.ChangeListener;
54

    
55
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
56
import org.gvsig.gui.beans.swing.JButton;
57
import org.gvsig.gvsig3dgui.ProjectView3D;
58

    
59
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.ui.mdiManager.SingletonWindow;
62
import com.iver.andami.ui.mdiManager.WindowInfo;
63
import com.iver.cit.gvsig.fmap.MapContext;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
66
import com.iver.cit.gvsig.project.documents.view.IProjectView;
67

    
68
/**
69
 * Dialogo donde se muestran las propiedades de una vista
70
 * 
71
 * @author Fernando Gonz�lez Cort�s
72
 */
73
public class TransparencyPanel extends GridBagLayoutPanel implements
74
                SingletonWindow, ChangeListener {
75

    
76
        /**
77
         * 
78
         */
79
        private static final long serialVersionUID = 1L;
80

    
81
        private ProjectView3D                 _view3D;
82
        private int                                 _width = 250;
83
        private int                                 _height = 170;
84
        private int                                 _order;
85
        private Layer3DProps                 _layer3DProps;
86
        private float                                 _opacity;
87
        private FLayer                                 _lyr3D;
88

    
89
        private GridBagLayoutPanel         okCancelPanel;
90
        private JButton                         okButton;
91
        private JSlider                         sliderTrans;
92
        private SliderUpdate                 runnable;
93

    
94
        /**
95
         * This is the default constructor
96
         * 
97
         * @param f
98
         *            Frame padre del dialogo
99
         * @param v
100
         *            Vista que se representa
101
         */
102
        public TransparencyPanel(IProjectView v) {
103

    
104
                _view3D = (ProjectView3D) v;
105

    
106
                // cogemos la capa q esta seleccionada
107

    
108
                MapContext mapa = _view3D.getMapContext();
109

    
110
                // Getting all selected Extent
111
                FLayer[] selectedExtent = mapa.getLayers().getActives();
112

    
113
                if (selectedExtent.length == 1 && selectedExtent[0].isAvailable()) {
114

    
115
                        _lyr3D = (FLayer) selectedExtent[0];
116
                        _layer3DProps = Layer3DProps.getLayer3DProps(_lyr3D);
117

    
118
                        _order = _layer3DProps.getTerrainOrder();
119
                        _opacity = _layer3DProps.getOpacity();
120

    
121
                }
122
                initialize();
123

    
124
        }
125

    
126
        private void UpdateOrder() {
127
                // cogemos la capa q esta seleccionada
128

    
129
                MapContext mapa = _view3D.getMapContext();
130

    
131
                // Getting all selected Extent
132
                FLayer[] selectedExtent = mapa.getLayers().getActives();
133
                FLayer lyr3DAux = null;
134

    
135
                if (selectedExtent.length == 1 && selectedExtent[0].isAvailable()) {
136

    
137
                        lyr3DAux = (FLayer) selectedExtent[0];
138

    
139
                        _lyr3D = lyr3DAux;
140
                        _layer3DProps = Layer3DProps.getLayer3DProps(_lyr3D);
141
                        
142
                        _order = _layer3DProps.getTerrainOrder();
143
                        _opacity = (((FLyrDefault) _lyr3D).getTransparency())
144
                        / (float) 255;
145
                }
146

    
147
        }
148

    
149
        /**
150
         * This method initializes this
151
         */
152
        private void initialize() {
153

    
154
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
155
                gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
156
                gridBagConstraints1.gridy = 2;
157
                gridBagConstraints1.weightx = 1.0;
158
                gridBagConstraints1.gridx = 1;
159
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
160
                gridBagConstraints.gridx = 1;
161
                gridBagConstraints.gridheight = 2;
162
                gridBagConstraints.gridwidth = 2;
163
                gridBagConstraints.insets = new Insets(15, 0, 0, 0);
164
                gridBagConstraints.gridy = 3;
165

    
166
                this.add(getSliderTrans(), gridBagConstraints1);
167
                this.add(getOkCancelPanel(), gridBagConstraints);
168

    
169
                // Initialize component
170
                setName(PluginServices.getText(this, "Transparency"));
171
                this.setPreferredSize(new Dimension(_width, _height));
172
                this.setSize(new Dimension(_width, _height));
173

    
174
                // ADDING COMPONENTS
175

    
176
                // Launching slider update
177
                // Create the object with the run() method
178
                runnable = new SliderUpdate();
179

    
180
                // Create the thread supplying it with the runnable object
181
                Thread thread = new Thread(runnable);
182

    
183
                // Start the thread
184
                thread.start();
185

    
186
        }
187

    
188
        private javax.swing.JSlider getSliderTrans() {
189
                if (sliderTrans == null) {
190
                        sliderTrans = new JSlider(JSlider.HORIZONTAL, 0, 255,
191
                                        (int) _opacity * 255);
192
                        // Create the label table
193
                        Dictionary labelTable = new Hashtable();
194
                        labelTable.put(new Integer(50), new JLabel("Transparente"));
195
                        labelTable.put(new Integer(215), new JLabel("Opaco"));
196
                        // Adding table to slider
197
                        sliderTrans.setLabelTable(labelTable);
198
                        // Activate the lables
199
                        sliderTrans.setPaintLabels(true);
200
                        // Add Change linstener
201
                        sliderTrans.addChangeListener(this);
202

    
203
                }
204
                int a = (int) (_opacity * 255);
205
                sliderTrans.setValue(a);
206
                return sliderTrans;
207
        }
208

    
209
        public void stateChanged(ChangeEvent e) {
210
                JSlider source = (JSlider) e.getSource();
211
                int trans = (int) source.getValue();
212
                UpdateOrder();
213
                _opacity = (float) trans / 255f;
214
                ((FLyrDefault) _lyr3D).setTransparency(trans);
215
                // Notify the opacity to planet
216
                //@TODO: Fix this ...
217
                //view3D.getPlanet().setTextureOpacityLayer(order, opacity);
218
                // Repainting canvas
219
                //_view3D.getCanvas3d().repaint();
220

    
221
                this.repaint();
222
        }
223

    
224
        private GridBagLayoutPanel getOkCancelPanel() {
225
                if (okCancelPanel == null) {
226
                        ActionListener okAction, cancelAction, applyAction;
227
                        okAction = new java.awt.event.ActionListener() {
228
                                public void actionPerformed(java.awt.event.ActionEvent e) {
229
                                        UpdateOrder();
230
                                        // Alpha value
231
                                        float a = _opacity;
232
                                        // Setting opaciti in layer properties
233
                                        _layer3DProps.setOpacity(a);
234
                                        // Notify the opacity to planet
235
                                        //@TODO: Fix this ...
236
                                        //view3D.getPlanet().setTextureOpacityLayer(order, a);
237
                                        // Repainting canvas
238
                                        //_view3D.getCanvas3d().repaint();
239
                                        // Stop thread
240
                                        runnable.end();
241
                                        PluginServices.getMDIManager().closeWindow(
242
                                                        TransparencyPanel.this);
243
                                }
244
                        };
245
                        okCancelPanel = new GridBagLayoutPanel();
246
                        okCancelPanel.setAlignmentX(GridBagLayoutPanel.RIGHT_ALIGNMENT);
247
                        okButton = new JButton();
248
                        okButton.setLabel(PluginServices.getText(this, "Close"));
249
                        okButton.addActionListener(okAction);
250

    
251
                        okCancelPanel.addComponent(okButton);
252
                }
253
                return okCancelPanel;
254
        }
255

    
256
        public Object getWindowModel() {
257
                // TODO Auto-generated method stub
258
                return _view3D;
259
        }
260

    
261
        public WindowInfo getWindowInfo() {
262
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODELESSDIALOG);
263
                m_viewinfo.setTitle(PluginServices.getText(this, "Transparency"));
264
                m_viewinfo.setHeight(_height);
265
                m_viewinfo.setWidth(_width);
266
                m_viewinfo.setX(100);
267
                m_viewinfo.setY(100);
268
                return m_viewinfo;
269
        }
270

    
271
        public class SliderUpdate implements Runnable {
272

    
273
                private boolean finish = false;
274

    
275
                ProjectView3D view;
276

    
277
                JSlider slider;
278

    
279
                public SliderUpdate() {
280
                }
281

    
282
                // This method is called when the thread runs
283
                public void run() {
284
                        while (true) {
285
                                try {
286
                                        Thread.sleep(250);
287
                                        synchronized (this) {
288
                                                if (finish) {
289
                                                        break;
290
                                                }
291
                                        }
292
                                } catch (InterruptedException e) {
293

    
294
                                        e.printStackTrace();
295
                                }
296
                                // aki codigo de comprobacion
297
                                UpdateOrder();
298
                                sliderTrans.setValue((int) (_opacity * 255));
299
                                sliderTrans.repaint();
300
                        }
301
                }
302

    
303
                public synchronized void end() {
304
                        finish = true;
305
                }
306

    
307
        }
308
        public Object getWindowProfile() {
309
                return WindowInfo.PROPERTIES_PROFILE;
310
        }
311
}