gvsig-3d / 2.1 / branches / org.gvsig.view3d_vector_and_extrusion_2.3 / org.gvsig.view3d / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / DefaultView3DPanel.java @ 708
History | View | Annotate | Download (9.28 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2015 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 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., 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 |
|
25 |
package org.gvsig.view3d.swing.impl; |
26 |
|
27 |
import java.awt.BorderLayout; |
28 |
import java.awt.Component; |
29 |
import java.awt.Dimension; |
30 |
import java.awt.Toolkit; |
31 |
import java.awt.Window; |
32 |
import java.awt.event.ActionEvent; |
33 |
import java.awt.event.ComponentEvent; |
34 |
import java.awt.event.ComponentListener; |
35 |
import java.awt.event.HierarchyEvent; |
36 |
import java.awt.event.HierarchyListener; |
37 |
import java.awt.event.KeyEvent; |
38 |
import java.awt.event.WindowEvent; |
39 |
import java.awt.event.WindowFocusListener; |
40 |
|
41 |
import javax.swing.AbstractAction; |
42 |
import javax.swing.Action; |
43 |
import javax.swing.JComponent; |
44 |
import javax.swing.JFrame; |
45 |
import javax.swing.JPanel; |
46 |
import javax.swing.KeyStroke; |
47 |
import javax.swing.SwingUtilities; |
48 |
|
49 |
import org.gvsig.fmap.mapcontext.MapContext; |
50 |
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesSupport; |
51 |
import org.gvsig.tools.ToolsLocator; |
52 |
import org.gvsig.tools.i18n.I18nManager; |
53 |
import org.gvsig.tools.observer.ObservableHelper; |
54 |
import org.gvsig.tools.observer.Observer; |
55 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
56 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
57 |
import org.gvsig.view3d.swing.api.MapControl3D; |
58 |
import org.gvsig.view3d.swing.api.View3DPanel; |
59 |
import org.gvsig.view3d.swing.api.View3DSwingLocator; |
60 |
import org.gvsig.view3d.swing.api.View3DSwingManager; |
61 |
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE; |
62 |
import org.gvsig.view3d.swing.api.properties.GeneralProperties3D; |
63 |
|
64 |
/**
|
65 |
* Default implementaion of {@link View3DPanel}.
|
66 |
*
|
67 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
68 |
*/
|
69 |
public class DefaultView3DPanel extends JPanel implements View3DPanel { |
70 |
|
71 |
private static final long serialVersionUID = -87105248886531868L; |
72 |
|
73 |
private ObservableHelper observableHelper;
|
74 |
|
75 |
private ComponentListener disposeComponentListener; |
76 |
|
77 |
private String title; |
78 |
|
79 |
private MapControl3D mapControl3D;
|
80 |
|
81 |
private JFrame fullScreenFrame; |
82 |
|
83 |
/**
|
84 |
* Default constructor of {@link DefaultView3DPanel}. Creates an
|
85 |
* {@link MapControl3D}, initialize local fileds and register listeners.
|
86 |
*
|
87 |
* @param theMapContext
|
88 |
* associated <code>MapContext</code>
|
89 |
* @param type
|
90 |
* Type of this {@link DefaultMapControl3D}. See {@link TYPE}.
|
91 |
* @param viewProperties
|
92 |
* View properties to register a <code>MapControl3D</code>
|
93 |
*/
|
94 |
public DefaultView3DPanel(MapContext theMapContext, String title, |
95 |
TYPE type, ExtendedPropertiesSupport viewProperties) { |
96 |
|
97 |
View3DSwingManager swingManager = View3DSwingLocator.getManager(); |
98 |
this.mapControl3D =
|
99 |
swingManager |
100 |
.createMapControl3D(theMapContext, type, viewProperties); |
101 |
|
102 |
initialize(title); |
103 |
} |
104 |
|
105 |
/**
|
106 |
*
|
107 |
* @param mapControl3D
|
108 |
* @param title
|
109 |
*/
|
110 |
public DefaultView3DPanel(MapControl3D mapControl3D, String title) { |
111 |
|
112 |
this.mapControl3D = mapControl3D;
|
113 |
|
114 |
initialize(title); |
115 |
} |
116 |
|
117 |
private void initialize(String title) { |
118 |
|
119 |
this.setLayout(new BorderLayout()); |
120 |
|
121 |
View3DSwingManager swingManager = View3DSwingLocator.getManager(); |
122 |
GeneralProperties3D general3dProperties = |
123 |
swingManager.getGeneral3DProperties(); |
124 |
|
125 |
this.title = title;
|
126 |
|
127 |
this.observableHelper = new ObservableHelper(); |
128 |
|
129 |
this.add(mapControl3D.asJComponent(), BorderLayout.CENTER); |
130 |
|
131 |
addComponentListener(getDisposeComponentListener()); |
132 |
|
133 |
this.setPreferredSize(new Dimension(general3dProperties |
134 |
.getDefaultViewWidth(), general3dProperties.getDefaultViewHeight())); |
135 |
} |
136 |
|
137 |
private String composeTitle(String title) { |
138 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
139 |
StringBuilder stb = new StringBuilder(); |
140 |
stb.append(i18nManager.getTranslation("view_3d"));
|
141 |
stb.append(" ");
|
142 |
stb.append(":");
|
143 |
stb.append(" ");
|
144 |
stb.append(title); |
145 |
return stb.toString();
|
146 |
} |
147 |
|
148 |
public JComponent asJComponent() { |
149 |
return this; |
150 |
} |
151 |
|
152 |
public MapControl3D getMapControl3D() {
|
153 |
return this.mapControl3D; |
154 |
} |
155 |
|
156 |
public void show() { |
157 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
158 |
wm.showWindow(this, composeTitle(title), WindowManager.MODE.WINDOW);
|
159 |
} |
160 |
|
161 |
public void addObserver(Observer o) { |
162 |
if (observableHelper != null) { |
163 |
observableHelper.addObserver(o); |
164 |
} |
165 |
} |
166 |
|
167 |
public void deleteObserver(Observer o) { |
168 |
if (observableHelper != null) { |
169 |
observableHelper.deleteObserver(o); |
170 |
} |
171 |
|
172 |
} |
173 |
|
174 |
public void deleteObservers() { |
175 |
if (observableHelper != null) { |
176 |
observableHelper.deleteObservers(); |
177 |
} |
178 |
} |
179 |
|
180 |
private JFrame getFullScreenFrame() { |
181 |
if (this.fullScreenFrame == null) { |
182 |
this.fullScreenFrame = new JFrame(); |
183 |
this.fullScreenFrame.getContentPane().setPreferredSize(
|
184 |
Toolkit.getDefaultToolkit().getScreenSize());
|
185 |
this.fullScreenFrame.getContentPane().setLayout(new BorderLayout()); |
186 |
this.fullScreenFrame.getContentPane().add(
|
187 |
this.mapControl3D.asJComponent(), BorderLayout.CENTER); |
188 |
this.fullScreenFrame.setLocation(0, 0); |
189 |
this.fullScreenFrame.setUndecorated(true); |
190 |
this.fullScreenFrame.setAlwaysOnTop(true); |
191 |
this.fullScreenFrame.pack();
|
192 |
addCloseListener(); |
193 |
addHierarchyListener(); |
194 |
} |
195 |
return this.fullScreenFrame; |
196 |
} |
197 |
|
198 |
private void addHierarchyListener() { |
199 |
|
200 |
this.mapControl3D.asJComponent().addHierarchyListener(
|
201 |
new HierarchyListener() { |
202 |
|
203 |
public void hierarchyChanged(HierarchyEvent e) { |
204 |
final Component component = e.getComponent(); |
205 |
if (component.isShowing()
|
206 |
&& (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { |
207 |
|
208 |
final Window toplevel = |
209 |
SwingUtilities.getWindowAncestor(component);
|
210 |
toplevel |
211 |
.addWindowFocusListener(new WindowFocusListener() { |
212 |
|
213 |
public void windowLostFocus(WindowEvent e) { |
214 |
} |
215 |
|
216 |
public void windowGainedFocus(WindowEvent e) { |
217 |
component.requestFocus(); |
218 |
toplevel.removeWindowFocusListener(this);
|
219 |
} |
220 |
}); |
221 |
component.removeHierarchyListener(this);
|
222 |
} |
223 |
}; |
224 |
}); |
225 |
} |
226 |
|
227 |
public void fullScreen() { |
228 |
|
229 |
// NOTE: We have to remove dispose component listener because the
|
230 |
// component will be re-used at full screen frame.
|
231 |
removeComponentListener(getDisposeComponentListener()); |
232 |
this.setVisible(false); |
233 |
|
234 |
getFullScreenFrame().setVisible(true);
|
235 |
} |
236 |
|
237 |
private void addCloseListener() { |
238 |
|
239 |
// Key bindings to close JFrame with ESC key
|
240 |
getFullScreenFrame().getRootPane() |
241 |
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
242 |
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit"); |
243 |
getFullScreenFrame().getRootPane().getActionMap() |
244 |
.put("exit", getCloseAction());
|
245 |
} |
246 |
|
247 |
private Action getCloseAction() { |
248 |
return new AbstractAction() { |
249 |
|
250 |
private static final long serialVersionUID = -8538252474573633943L; |
251 |
|
252 |
public void actionPerformed(ActionEvent e) { |
253 |
getFullScreenFrame().dispose(); |
254 |
|
255 |
View3DSwingManager swingManager = |
256 |
View3DSwingLocator.getManager(); |
257 |
View3DPanel newPanel = |
258 |
swingManager.createView3DPanel(mapControl3D, title); |
259 |
newPanel.show(); |
260 |
} |
261 |
}; |
262 |
} |
263 |
|
264 |
private ComponentListener getDisposeComponentListener() { |
265 |
if (disposeComponentListener == null) { |
266 |
disposeComponentListener = new ComponentListener() { |
267 |
|
268 |
public void componentShown(ComponentEvent e) { |
269 |
} |
270 |
|
271 |
public void componentResized(ComponentEvent e) { |
272 |
} |
273 |
|
274 |
public void componentMoved(ComponentEvent e) { |
275 |
} |
276 |
|
277 |
public void componentHidden(ComponentEvent e) { |
278 |
getMapControl3D().dispose(); |
279 |
} |
280 |
}; |
281 |
} |
282 |
return disposeComponentListener;
|
283 |
} |
284 |
} |