Statistics
| Revision:

svn-gvsig-desktop / import / ext3D / branches / ext3D_v1.1 / ext3Dgui / src / com / iver / ai2 / gvsig3dgui / layer / TransparencyPanel.java @ 15493

History | View | Annotate | Download (9.13 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 com.iver.ai2.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

    
58
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
59
import com.iver.ai2.gvsig3dgui.ProjectView3D;
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
        private ProjectView3D view3D;
77

    
78
        private int width = 250;
79

    
80
        private int height = 100;
81

    
82
        private GridBagLayoutPanel okCancelPanel;
83

    
84
        private JButton okButton;
85

    
86
        private JSlider sliderTrans;
87

    
88
        private int order;
89

    
90
        private Layer3DProps layer3DProps;
91

    
92
        private float opacity;
93

    
94
        private SliderUpdate runnable;
95

    
96
        private FLayer lyr3D;
97

    
98
        /**
99
         * This is the default constructor
100
         * 
101
         * @param f
102
         *            Frame padre del dialogo
103
         * @param v
104
         *            Vista que se representa
105
         */
106
        public TransparencyPanel(IProjectView v) {
107

    
108
                view3D = (ProjectView3D) v;
109

    
110
                // UpdateOrder();
111
                // cogemos la capa q esta seleccionada
112

    
113
                MapContext mapa = view3D.getMapContext();
114

    
115
                // Getting all selected Extent
116
                FLayer[] selectedExtent = mapa.getLayers().getActives();
117

    
118
                if (selectedExtent.length == 1 && selectedExtent[0].isAvailable()) {
119

    
120
                        lyr3D = (FLayer) selectedExtent[0];
121
                        layer3DProps = Layer3DProps.getLayer3DProps(lyr3D);
122

    
123
                        order = layer3DProps.getPlanetOrder();
124
                        opacity = layer3DProps.getOpacity();
125

    
126
                }
127
                initialize();
128

    
129
        }
130

    
131
        private void UpdateOrder() {
132

    
133
                // cogemos la capa q esta seleccionada
134

    
135
                MapContext mapa = view3D.getMapContext();
136

    
137
                // Getting all selected Extent
138
                FLayer[] selectedExtent = mapa.getLayers().getActives();
139
                FLayer lyr3DAux = null;
140

    
141
                if (selectedExtent.length == 1 && selectedExtent[0].isAvailable()) {
142

    
143
                        lyr3DAux = (FLayer) selectedExtent[0];
144
                        if (!(lyr3DAux.getName().equals(lyr3D.getName()))){
145
                                lyr3D = lyr3DAux;
146
                                layer3DProps = Layer3DProps.getLayer3DProps(lyr3D);
147
                                
148
                                order = layer3DProps.getPlanetOrder();
149
//                                opacity = layer3DProps.getOpacity();
150
                                opacity = ((FLyrDefault)lyr3D).getTransparency() / 255;
151
                                // sliderTrans.setValue((int) opacity * 255);
152
                                
153
                        }
154
                }
155

    
156
        }
157

    
158
        /**
159
         * This method initializes this
160
         */
161
        private void initialize() {
162
                
163
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
164
                gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
165
                gridBagConstraints1.gridy = 2;
166
                gridBagConstraints1.weightx = 1.0;
167
                gridBagConstraints1.gridx = 1;
168
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
169
                gridBagConstraints.gridx = 1;
170
                gridBagConstraints.gridheight = 2;
171
                gridBagConstraints.gridwidth = 2;
172
                gridBagConstraints.insets = new Insets(15, 0, 0, 0);
173
                gridBagConstraints.gridy = 3;
174
                
175
                this.add(getSliderTrans(),gridBagConstraints1);
176
                this.add(getOkCancelPanel(),gridBagConstraints);
177
//                jContentPane = new JPanel();
178
//                jContentPane.setLayout(new GridBagLayout());
179
//                jContentPane.add(getJButton(), gridBagConstraints);
180
//                jContentPane.add(getJSlider(), gridBagConstraints1);
181

    
182
                
183
                
184
                // Inicialize component
185
                setName(PluginServices.getText(this, "Transparency"));
186
                // Introducing the margin
187
//                setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
188
                // Dimension of the panel
189
                // setSize(new Dimension(width, height));
190
                this.setPreferredSize(new Dimension(width, height));
191
                this.setSize(new Dimension(width, height));
192

    
193
                // ADDING COMPONENTS
194

    
195
//                this.addComponent(getSliderTrans(), new Insets(2, 5, 2, 5));
196
//
197
//                // Accept buton
198
//                this.addComponent(getOkCancelPanel(), new Insets(2, 5, 2, 5));
199

    
200
                // Launching slider update
201
                // Create the object with the run() method
202
                runnable = new SliderUpdate();
203

    
204
                // Create the thread supplying it with the runnable object
205
                Thread thread = new Thread(runnable);
206

    
207
                // Start the thread
208
                thread.start();
209

    
210
        }
211

    
212
        private javax.swing.JSlider getSliderTrans() {
213
                if (sliderTrans == null) {
214
                        sliderTrans = new JSlider(JSlider.HORIZONTAL, 0, 255,
215
                                        (int) opacity * 255);
216
                        // Create the label table
217
                        Dictionary labelTable = new Hashtable();
218
                        labelTable.put(new Integer(50), new JLabel("Transparente"));
219
                        labelTable.put(new Integer(215), new JLabel("Opaco"));
220
                        // Adding table to slider
221
                        sliderTrans.setLabelTable(labelTable);
222
                        // Activate the lables
223
                        sliderTrans.setPaintLabels(true);
224
                        // Add Change linstener
225
                        sliderTrans.addChangeListener(this);
226

    
227
                }
228
                int a = (int) (opacity * 255);
229
                sliderTrans.setValue(a);
230
                return sliderTrans;
231
        }
232

    
233
        public void stateChanged(ChangeEvent e) {
234
//                this.UpdateOrder();
235
                JSlider source = (JSlider) e.getSource();
236
                int trans = (int) source.getValue();
237
                // System.out.println("valor de la transparencia capturada " + trans);
238
                UpdateOrder();
239
                // System.out.println("Activado refresco automatico");
240
                // Alpha value
241
                opacity = (float) trans / 255f;
242
                // Setting opaciti in layer properties
243
                //layer3DProps.setOpacity(opacity);
244
                // Setting opacity in layer 
245
                ((FLyrDefault)lyr3D).setTransparency(trans);
246
                // Notify the opacity to planet
247
                view3D.getPlanet().setTextureOpacityLayer(order, opacity);
248
                // Repainting canvas
249
                view3D.getCanvas3d().repaint();
250

    
251
                this.repaint();
252
        }
253

    
254
        private GridBagLayoutPanel getOkCancelPanel() {
255
                if (okCancelPanel == null) {
256
                        ActionListener okAction, cancelAction, applyAction;
257
                        okAction = new java.awt.event.ActionListener() {
258
                                public void actionPerformed(java.awt.event.ActionEvent e) {
259
                                        UpdateOrder();
260
                                        // Alpha value
261
                                        float a = opacity;
262
                                        // Setting opaciti in layer properties
263
                                        layer3DProps.setOpacity(a);
264
                                        // Notify the opacity to planet
265
                                        view3D.getPlanet().setTextureOpacityLayer(order, a);
266
                                        // Repainting canvas
267
                                        view3D.getCanvas3d().repaint();
268
                                         // Stop thread
269
                                         runnable.end();
270
                                        PluginServices.getMDIManager().closeWindow(
271
                                                        TransparencyPanel.this);
272
                                }
273
                        };
274
                        okCancelPanel = new GridBagLayoutPanel();
275
                        okCancelPanel.setAlignmentX(GridBagLayoutPanel.RIGHT_ALIGNMENT);
276
                        okButton = new JButton();
277
                        okButton.setLabel(PluginServices.getText(this, "Close"));
278
                        okButton.addActionListener(okAction);
279

    
280
                        okCancelPanel.addComponent(okButton);
281
                }
282
                return okCancelPanel;
283
        }
284

    
285
        public Object getWindowModel() {
286
                // TODO Auto-generated method stub
287
                return view3D;
288
        }
289

    
290
        public WindowInfo getWindowInfo() {
291
                WindowInfo m_viewinfo = new WindowInfo();
292
                m_viewinfo.setTitle(PluginServices.getText(this, "Transparency"));
293
                m_viewinfo.setHeight(height);
294
                m_viewinfo.setWidth(width);
295
                return m_viewinfo;
296
        }
297

    
298
        public class SliderUpdate implements Runnable {
299

    
300
                private boolean finish = false;
301

    
302
                ProjectView3D view;
303

    
304
                JSlider slider;
305

    
306
                public SliderUpdate() {
307
                }
308

    
309
                // This method is called when the thread runs
310
                public void run() {
311
                        while (true) {
312
                                try {
313
                                        Thread.sleep(500);
314
                                        synchronized (this) {
315
                                                if (finish) {
316
                                                        break;
317
                                                }
318
                                        }
319
                                } catch (InterruptedException e) {
320

    
321
                                        e.printStackTrace();
322
                                }
323
                                // aki codigo de comprobacion
324
                                UpdateOrder();
325
                                sliderTrans.setValue((int)(opacity * 255));
326
                                sliderTrans.repaint();
327
                        }
328
                }
329

    
330
                public synchronized void end() {
331
                        finish = true;
332
                }
333

    
334
        }
335

    
336
}