001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/plugins/ViewingPlatformWarpPlugin.java,v 1.1 2005/04/20 21:04:49 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.plugins;
019:
020: import org.jdesktop.j3dfly.J3dFlyContext;
021: import javax.media.j3d.Transform3D;
022: import javax.media.j3d.TransformGroup;
023: import javax.vecmath.Matrix4d;
024: import java.util.LinkedList;
025: import java.util.Iterator;
026: import javax.swing.JMenuItem;
027: import javax.swing.JDialog;
028: import java.awt.event.ActionListener;
029:
030: import org.jdesktop.j3dfly.event.UniverseClearEvent;
031: import org.jdesktop.j3dfly.event.FlyEventListener;
032:
033: /**
034: * Provides controls to 'Warp' the viewing platform between user defined
035: * locations in the universe
036: *
037: * @author Paul Byrne
038: * @version $Id: ViewingPlatformWarpPlugin.java,v 1.1 2005/04/20 21:04:49 paulby Exp $
039: */
040: public class ViewingPlatformWarpPlugin extends J3dFlyPlugin implements
041: FlyEventListener {
042:
043: private java.lang.ref.SoftReference panelReference = null;
044: private java.lang.ref.SoftReference dialogReference = null;
045:
046: /** Creates new form PluginTemplate */
047: public ViewingPlatformWarpPlugin() {
048: super ();
049: }
050:
051: /** This method is called from within the constructor to
052: * initialize the form.
053: * WARNING: Do NOT modify this code. The content of this method is
054: * always regenerated by the Form Editor.
055: */
056: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
057: private void initComponents() {
058: menuBar = new javax.swing.JMenuBar();
059: pluginMenu = new javax.swing.JMenu();
060: editMI = new javax.swing.JMenuItem();
061: jSeparator1 = new javax.swing.JSeparator();
062:
063: pluginMenu.setText("Warp");
064: pluginMenu
065: .setToolTipText("Warp to predefined locations in Universe");
066: editMI.setText("Edit...");
067: editMI.addActionListener(new java.awt.event.ActionListener() {
068: public void actionPerformed(java.awt.event.ActionEvent evt) {
069: editMIActionPerformed(evt);
070: }
071: });
072:
073: pluginMenu.add(editMI);
074:
075: pluginMenu.add(jSeparator1);
076:
077: menuBar.add(pluginMenu);
078:
079: }
080:
081: // </editor-fold>//GEN-END:initComponents
082:
083: private void editMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editMIActionPerformed
084: if (dialogReference == null || dialogReference.get() == null) {
085: JDialog dialog = new JDialog((javax.swing.JFrame) null,
086: "Warp Settings");
087: dialog.getContentPane().add(getControlPanel());
088: dialog.pack();
089: dialogReference = new java.lang.ref.SoftReference(dialog);
090: }
091:
092: ((JDialog) dialogReference.get()).setVisible(true);
093: }//GEN-LAST:event_editMIActionPerformed
094:
095: /**
096: * Add the warp to the plugins menu and register a listener for
097: * the menu item
098: */
099: void addWarpToMenu(final WarpLocation warp) {
100: WarpMenuItem m = new WarpMenuItem(warp.getName(), warp);
101: m.addActionListener(new ActionListener() {
102: public void actionPerformed(
103: java.awt.event.ActionEvent actionEvent) {
104: TransformGroup tg = j3dflyContext.getUniverse()
105: .getViewingPlatform()
106: .getViewPlatformTransform();
107: tg.setTransform(warp.getTransform3D());
108: }
109: });
110: pluginMenu.add(m);
111: }
112:
113: /**
114: * Remove a warp from the menu
115: */
116: void removeWarpFromMenu(WarpLocation warp) {
117: for (int i = 0; i < pluginMenu.getItemCount(); i++) {
118: JMenuItem m = pluginMenu.getItem(i);
119: if (m instanceof WarpMenuItem
120: && ((WarpMenuItem) m).getWarpLocation() == warp) {
121: pluginMenu.remove((WarpMenuItem) m);
122: return;
123: }
124: }
125: }
126:
127: /**
128: * Update the menu item for this warp. Called when the warps
129: * name is changed
130: */
131: void updateWarpMenu(WarpLocation warp) {
132: for (int i = 0; i < pluginMenu.getItemCount(); i++) {
133: JMenuItem m = pluginMenu.getItem(i);
134: if (m instanceof WarpMenuItem
135: && ((WarpMenuItem) m).getWarpLocation() == warp) {
136: ((WarpMenuItem) m).setText(warp.getName());
137: return;
138: }
139: }
140: }
141:
142: /**
143: * Add a legacy WarpSet to the list of warps in the plugin
144: */
145: public void addWarpSet(org.jdesktop.j3dfly.warp.WarpSet set) {
146: org.jdesktop.j3dfly.warp.WarpPosition[] warps = set.getWarps();
147:
148: if (warps == null)
149: return;
150:
151: String defaultWarp = set.getDefaultWarp();
152:
153: for (int i = 0; i < warps.length; i++) {
154: WarpLocation warp = new WarpLocation(warps[i].getName(),
155: warps[i].getDescription(), warps[i].getPosition());
156: ((ViewingPlatformWarpPluginPreference) pluginPref)
157: .addWarp(warp);
158: if (warps[i].getName().equals(defaultWarp)) {
159: j3dflyContext.getUniverse().getViewingPlatform()
160: .getViewPlatformTransform().setTransform(
161: new Transform3D(warp.getLocation()));
162: ((ViewingPlatformWarpPluginPreference) pluginPref)
163: .setDefaultWarp(warp);
164: }
165: }
166: }
167:
168: /**
169: * Get the title of the plugin
170: */
171: public String getPluginTitle() {
172: // Change this to return the name of the plugin
173: return getClass().getName();
174: }
175:
176: /**
177: * Returns the control panel for this plugin, or null if there
178: * is no control panel
179: */
180: public javax.swing.JPanel getControlPanel() {
181:
182: if (panelReference == null || panelReference.get() == null)
183: panelReference = new java.lang.ref.SoftReference(
184: new ViewingPlatformWarpPanel(this ));
185:
186: return (javax.swing.JPanel) panelReference.get();
187:
188: }
189:
190: /**
191: * Install the plugin and add it's menu item to the menu
192: */
193: public void installPlugin(PluginPreference pluginPref,
194: J3dFlyContext j3dflyContext) {
195: super .installPlugin(pluginPref, j3dflyContext);
196: initComponents();
197:
198: menuBar.removeAll();
199: menuBar = null;
200:
201: menu = pluginMenu;
202: menu.setText(pluginPref.getInstalledFromStr() + " Warps");
203: if (pluginPref.isInstallInMenu())
204: getMenu("Viewing Platform").add(menu);
205:
206: LinkedList warps = ((ViewingPlatformWarpPluginPreference) pluginPref)
207: .getWarps();
208: if (warps != null) {
209: Iterator it = warps.iterator();
210: while (it.hasNext())
211: addWarpToMenu((WarpLocation) it.next());
212: }
213:
214: // If this warp was installed by the data file, then listen for
215: // clear events
216: if (pluginPref.getInstalledFrom() == PluginPreference.FILE)
217: pluginPref.getContext().getEventProcessor().addListener(
218: this , UniverseClearEvent.class);
219:
220: WarpLocation warp = ((ViewingPlatformWarpPluginPreference) pluginPref)
221: .getDefaultWarp();
222: if (warp != null)
223: j3dflyContext.getUniverse().getViewingPlatform()
224: .getViewPlatformTransform().setTransform(
225: new Transform3D(warp.getLocation()));
226: }
227:
228: /**
229: * Uninstall this plugin
230: */
231: public void uninstallPlugin() {
232: super .uninstallPlugin();
233: if (pluginPref.isInstallInMenu())
234: getMenu("Viewing Platform").remove(menu);
235: menu = null;
236:
237: // If this warp was installed by the data file, then listen for
238: // clear events
239: if (pluginPref.getInstalledFrom() == PluginPreference.FILE)
240: pluginPref.getContext().getEventProcessor().removeListener(
241: this , UniverseClearEvent.class);
242: }
243:
244: /**
245: * Returns the class of the plugin preference.
246: *
247: * Plugins that require more preference information should provide a
248: * subclass of PluginPrefernece that contains all the extra preference
249: * data. This class must be Serializable.
250: */
251: public Class getPluginPreferenceClass() {
252: return ViewingPlatformWarpPluginPreference.class;
253: }
254:
255: /**
256: * Called by EventProcessor when an event occurs for which this
257: * listener has registered interest
258: */
259: public void processFlyEvent(org.jdesktop.j3dfly.event.FlyEvent evt) {
260: if (evt instanceof UniverseClearEvent)
261: uninstallPlugin();
262: }
263:
264: public static class ViewingPlatformWarpPluginPreference extends
265: PluginPreference {
266:
267: private LinkedList warps = null;
268: private WarpLocation defaultWarp = null;
269:
270: public ViewingPlatformWarpPluginPreference() {
271: super ();
272: }
273:
274: public ViewingPlatformWarpPluginPreference(boolean enabled,
275: boolean installed) {
276: super (enabled, installed);
277: }
278:
279: public J3dFlyPlugin instantiatePlugin() {
280: plugin = new ViewingPlatformWarpPlugin();
281: return plugin;
282: }
283:
284: /**
285: * Return a description of this plugin
286: */
287: public String getDescription() {
288: return "ViewingPlatform Warp capability";
289: }
290:
291: /**
292: * Return the name of the Plugin for this prefernece.
293: * This is the name that will appear in the list of plugins
294: */
295: public String getName() {
296: return "Warp";
297: }
298:
299: /**
300: * Get the list of warps. This returns the structure used by
301: * this class. If you make any changes to the warp objects you
302: * should call updateWarp.
303: *
304: * You should not make structual changes to the LinkedList, instead
305: * call addWarp or removeWarp in this class
306: */
307: public LinkedList getWarps() {
308: return warps;
309: }
310:
311: public void setWarps(LinkedList list) {
312: this .warps = list;
313: }
314:
315: public void addWarp(WarpLocation warp) {
316: if (warps == null)
317: warps = new LinkedList();
318:
319: warps.add(warp);
320: ((ViewingPlatformWarpPlugin) plugin).addWarpToMenu(warp);
321: }
322:
323: public void removeWarp(WarpLocation warp) {
324: if (warps != null) {
325: ((ViewingPlatformWarpPlugin) plugin)
326: .removeWarpFromMenu(warp);
327: warps.remove(warp);
328: }
329: }
330:
331: /**
332: * Called when the warp object is changed. The warp should already
333: * exist in the LinkedList.
334: *
335: * This method will ensure the menu is correct etc
336: */
337: public void updateWarp(WarpLocation warp) {
338: ((ViewingPlatformWarpPlugin) plugin).updateWarpMenu(warp);
339: }
340:
341: public void setDefaultWarp(WarpLocation warp) {
342: defaultWarp = warp;
343: }
344:
345: public WarpLocation getDefaultWarp() {
346: return defaultWarp;
347: }
348: }
349:
350: public static class WarpLocation implements java.io.Serializable {
351:
352: /** Holds value of property name. */
353: private String name;
354:
355: /** Holds value of property description. */
356: private String description;
357:
358: /** Holds location Matrix values */
359: private double[] location = new double[16];
360:
361: public WarpLocation() {
362: }
363:
364: public WarpLocation(String name, String description,
365: Transform3D location) {
366: this .name = name;
367: this .description = description;
368: location.get(this .location);
369: }
370:
371: /** Getter for property name.
372: * @return Value of property name.
373: */
374: public String getName() {
375: return name;
376: }
377:
378: /** Setter for property name.
379: * @param name New value of property name.
380: */
381: public void setName(String name) {
382: this .name = name;
383: }
384:
385: /** Getter for property description.
386: * @return Value of property description.
387: */
388: public String getDescription() {
389: return description;
390: }
391:
392: /** Setter for property description.
393: * @param description New value of property description.
394: */
395: public void setDescription(String description) {
396: this .description = description;
397: }
398:
399: /** Getter for property location.
400: * @return Value of property location.
401: */
402: public double[] getLocation() {
403: return location;
404: }
405:
406: /** Setter for property location.
407: * @param location New value of property location.
408: */
409: public void setLocation(double[] location) {
410: this .location = location;
411: }
412:
413: /**
414: * Create and return a Transform3D set to the location
415: * of this warp
416: */
417: public Transform3D getTransform3D() {
418: return new Transform3D(location);
419: }
420:
421: }
422:
423: class WarpMenuItem extends javax.swing.JMenuItem {
424: private WarpLocation warp;
425:
426: public WarpMenuItem(String str, WarpLocation warp) {
427: super (str);
428: this .warp = warp;
429: }
430:
431: public WarpLocation getWarpLocation() {
432: return warp;
433: }
434: }
435:
436: // Variables declaration - do not modify//GEN-BEGIN:variables
437: private javax.swing.JMenuItem editMI;
438: private javax.swing.JSeparator jSeparator1;
439: private javax.swing.JMenuBar menuBar;
440: private javax.swing.JMenu pluginMenu;
441: // End of variables declaration//GEN-END:variables
442:
443: }
|