001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/plugins/ViewingPlatformControlPlugin.java,v 1.1 2005/04/20 21:04:48 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 java.awt.event.KeyEvent;
021: import javax.swing.ButtonGroup;
022: import org.jdesktop.j3dfly.event.CollisionEnableEvent;
023: import org.jdesktop.j3dfly.event.VPBehaviorPluginEvent;
024: import org.jdesktop.j3dfly.event.EventProcessor;
025: import org.jdesktop.j3dfly.event.ViewClipDistanceChangedEvent;
026: import org.jdesktop.j3dfly.J3dFlyContext;
027: import javax.media.j3d.BranchGroup;
028: import javax.media.j3d.View;
029:
030: import org.jdesktop.j3dfly.plugins.VPBehaviorPlugin;
031: import org.jdesktop.j3dfly.utils.vpbehaviors.ViewUtils;
032:
033: /**
034: *
035: * @author Paul Byrne
036: * @version $Revision: 1.1 $
037: */
038: public class ViewingPlatformControlPlugin extends J3dFlyPlugin
039: implements org.jdesktop.j3dfly.event.FlyEventListener,
040: java.awt.event.ActionListener {
041:
042: /**
043: * Button Group for the menu items
044: */
045: private ButtonGroup behaviorMenuButtonGroup = new ButtonGroup();
046:
047: /**
048: * Button Group for the toolbar
049: */
050: private ButtonGroup behaviorTBButtonGroup = new ButtonGroup();
051:
052: public ViewingPlatformControlPlugin() {
053: super ();
054:
055: javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(false);
056: initComponents();
057: menuBar.remove(viewBehaviorMenu);
058: menuBar = null;
059: dummyPanel.remove(vpBehaviorToolBar);
060: dummyPanel = null;
061:
062: }
063:
064: /**
065: * Returns the class of the plugin preference.
066: *
067: * Plugins that require more preference information should provide a
068: * subclass of PluginPrefernece that contains all the extra preference
069: * data. This class must be Serializable.
070: */
071: public Class getPluginPreferenceClass() {
072: return ViewingPlatformControlPluginPreference.class;
073: }
074:
075: public void installPlugin(PluginPreference pluginPref,
076: J3dFlyContext j3dflyContext) {
077: super .installPlugin(pluginPref, j3dflyContext);
078: if (pluginPref.isInstallInMenu())
079: j3dflyContext.getMainMenuBar().add(viewBehaviorMenu);
080: menu = viewBehaviorMenu;
081:
082: if (j3dflyContext.getToolBarPanel() != null) {
083: j3dflyContext.getToolBarPanel().add(vpBehaviorToolBar);
084: }
085:
086: j3dflyContext.getEventProcessor().addListener(this ,
087: CollisionEnableEvent.class);
088: //j3dflyContext.getEventProcessor().addListener( this, VPBehaviorChangeEvent.class );
089: j3dflyContext.getEventProcessor().addListener(this ,
090: VPBehaviorPluginEvent.class);
091: }
092:
093: public void uninstallPlugin() {
094: super .uninstallPlugin();
095: if (pluginPref.isInstallInMenu())
096: j3dflyContext.getMainMenuBar().remove(viewBehaviorMenu);
097: }
098:
099: /**
100: * Dummy method. Netbeans thinks this class is a JPanel
101: */
102: private void add(java.awt.Component comp) {
103: }
104:
105: /** This method is called from within the constructor to
106: * initialize the form.
107: * WARNING: Do NOT modify this code. The content of this method is
108: * always regenerated by the FormEditor.
109: */
110: private void initComponents() {//GEN-BEGIN:initComponents
111: menuBar = new javax.swing.JPopupMenu();
112: viewBehaviorMenu = new javax.swing.JMenu();
113: jSeparator3 = new javax.swing.JSeparator();
114: collisionEnabledMI = new javax.swing.JCheckBoxMenuItem();
115: jSeparator4 = new javax.swing.JSeparator();
116: showAllMI = new javax.swing.JMenu();
117: showAllXMI = new javax.swing.JMenuItem();
118: showAllYMI = new javax.swing.JMenuItem();
119: showAllZMI = new javax.swing.JMenuItem();
120: showAllnveXMI = new javax.swing.JMenuItem();
121: showAllnveYMI = new javax.swing.JMenuItem();
122: showAllnveZMI = new javax.swing.JMenuItem();
123: dummyPanel = new javax.swing.JPanel();
124: vpBehaviorToolBar = new javax.swing.JToolBar();
125:
126: viewBehaviorMenu.setText("Viewing Platform");
127: viewBehaviorMenu.add(jSeparator3);
128: collisionEnabledMI.setText("Collision Enabled");
129: collisionEnabledMI.setToolTipText("Enable Collision Detection");
130: collisionEnabledMI
131: .addActionListener(new java.awt.event.ActionListener() {
132: public void actionPerformed(
133: java.awt.event.ActionEvent evt) {
134: collisionEnabledMIActionPerformed(evt);
135: }
136: });
137:
138: viewBehaviorMenu.add(collisionEnabledMI);
139: viewBehaviorMenu.add(jSeparator4);
140: showAllMI.setText("Show All");
141: showAllXMI.setText("X");
142: showAllXMI
143: .addActionListener(new java.awt.event.ActionListener() {
144: public void actionPerformed(
145: java.awt.event.ActionEvent evt) {
146: showAllActionPerformed(evt);
147: }
148: });
149:
150: showAllMI.add(showAllXMI);
151: showAllYMI.setText("Y");
152: showAllYMI
153: .addActionListener(new java.awt.event.ActionListener() {
154: public void actionPerformed(
155: java.awt.event.ActionEvent evt) {
156: showAllActionPerformed(evt);
157: }
158: });
159:
160: showAllMI.add(showAllYMI);
161: showAllZMI.setText("Z");
162: showAllZMI
163: .addActionListener(new java.awt.event.ActionListener() {
164: public void actionPerformed(
165: java.awt.event.ActionEvent evt) {
166: showAllActionPerformed(evt);
167: }
168: });
169:
170: showAllMI.add(showAllZMI);
171: showAllnveXMI.setText("-X");
172: showAllnveXMI
173: .addActionListener(new java.awt.event.ActionListener() {
174: public void actionPerformed(
175: java.awt.event.ActionEvent evt) {
176: showAllActionPerformed(evt);
177: }
178: });
179:
180: showAllMI.add(showAllnveXMI);
181: showAllnveYMI.setText("-Y");
182: showAllnveYMI
183: .addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(
185: java.awt.event.ActionEvent evt) {
186: showAllActionPerformed(evt);
187: }
188: });
189:
190: showAllMI.add(showAllnveYMI);
191: showAllnveZMI.setText("-Z");
192: showAllnveZMI
193: .addActionListener(new java.awt.event.ActionListener() {
194: public void actionPerformed(
195: java.awt.event.ActionEvent evt) {
196: showAllActionPerformed(evt);
197: }
198: });
199:
200: showAllMI.add(showAllnveZMI);
201: viewBehaviorMenu.add(showAllMI);
202: menuBar.add(viewBehaviorMenu);
203:
204: dummyPanel.setToolTipText("Drive Behavior");
205: dummyPanel.setMinimumSize(new java.awt.Dimension(28, 28));
206: dummyPanel.setPreferredSize(new java.awt.Dimension(28, 28));
207: dummyPanel.add(vpBehaviorToolBar);
208:
209: add(dummyPanel);
210:
211: }//GEN-END:initComponents
212:
213: private void showAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllActionPerformed
214: // Add your handling code here:
215: double distance = 0;
216:
217: if (evt.getSource() == showAllXMI)
218: distance = j3dflyContext.getJ3dFly().getController()
219: .showAll(ViewUtils.POSITIVE_X_AXIS);
220: else if (evt.getSource() == showAllYMI)
221: distance = j3dflyContext.getJ3dFly().getController()
222: .showAll(ViewUtils.POSITIVE_Y_AXIS);
223: else if (evt.getSource() == showAllZMI)
224: distance = j3dflyContext.getJ3dFly().getController()
225: .showAll(ViewUtils.POSITIVE_Z_AXIS);
226: else if (evt.getSource() == showAllnveXMI)
227: distance = j3dflyContext.getJ3dFly().getController()
228: .showAll(ViewUtils.NEGATIVE_X_AXIS);
229: else if (evt.getSource() == showAllnveYMI)
230: distance = j3dflyContext.getJ3dFly().getController()
231: .showAll(ViewUtils.NEGATIVE_Y_AXIS);
232: else if (evt.getSource() == showAllnveZMI)
233: distance = j3dflyContext.getJ3dFly().getController()
234: .showAll(ViewUtils.NEGATIVE_Z_AXIS);
235:
236: View view = j3dflyContext.getUniverse().getViewer().getView();
237: double backClip = view.getBackClipDistance();
238:
239: if (distance > backClip) {
240: view.setBackClipDistance(distance);
241: j3dflyContext.getEventProcessor().postEvent(
242: new ViewClipDistanceChangedEvent(view
243: .getFrontClipDistance(), distance));
244: }
245:
246: }//GEN-LAST:event_showAllActionPerformed
247:
248: private void collisionEnabledMIActionPerformed(
249: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_collisionEnabledMIActionPerformed
250: // Add your handling code here:
251: j3dflyContext.getJ3dFly().getController().enableCollision(
252: collisionEnabledMI.getState());
253: }//GEN-LAST:event_collisionEnabledMIActionPerformed
254:
255: /**
256: * Returns the control panel for this plugin, or null if there
257: * is no control panel
258: */
259: public javax.swing.JPanel getControlPanel() {
260: return null;
261: }
262:
263: /**
264: * Forte thinks this class is a subclass of JFrame, it's not so add
265: * this NOP method to allow compilation in Forte
266: */
267: private void setJMenuBar(javax.swing.JMenuBar mb) {
268: }
269:
270: /**
271: * Called for VP Behavior selections
272: */
273: public void actionPerformed(java.awt.event.ActionEvent evt) {
274: j3dflyContext.getJ3dFly().getController().setVPBehavior(
275: ((VPBehaviorAction) evt.getSource()).getPlugin());
276: }
277:
278: /**
279: * Called by EventProcessor when an event occurs for which this
280: * listener has registered interest
281: */
282: public void processFlyEvent(
283: final org.jdesktop.j3dfly.event.FlyEvent evt) {
284: if (evt instanceof org.jdesktop.j3dfly.event.CollisionEnableEvent) {
285: collisionEnabledMI.setState(((CollisionEnableEvent) evt)
286: .isCollisionEnabled());
287: } else if (evt instanceof org.jdesktop.j3dfly.event.VPBehaviorPluginEvent) {
288: switch (((VPBehaviorPluginEvent) evt).getStatus()) {
289: case VPBehaviorPluginEvent.INSTALLED:
290: BehaviorMenuItem item = new BehaviorMenuItem(
291: ((VPBehaviorPluginEvent) evt)
292: .getBehaviorPlugin());
293: viewBehaviorMenu.insert(item, 0);
294: behaviorMenuButtonGroup.add(item);
295: item.addActionListener(this );
296: if (((VPBehaviorPluginEvent) evt).getBehaviorPlugin()
297: .getToolbarIcon() != null) {
298: BehaviorTBButton tbButton = new BehaviorTBButton(
299: ((VPBehaviorPluginEvent) evt)
300: .getBehaviorPlugin());
301: vpBehaviorToolBar.add(tbButton);
302: tbButton.addActionListener(this );
303: behaviorTBButtonGroup.add(tbButton);
304:
305: item.addChangeListener(tbButton);
306: tbButton.addChangeListener(item);
307: }
308: break;
309: case VPBehaviorPluginEvent.UNINSTALLED:
310: VPBehaviorPlugin plugin = ((VPBehaviorPluginEvent) evt)
311: .getBehaviorPlugin();
312: for (int i = 0; i < viewBehaviorMenu.getItemCount(); i++) {
313: javax.swing.JMenuItem m = viewBehaviorMenu
314: .getItem(i);
315: if (m instanceof BehaviorMenuItem
316: && plugin == ((BehaviorMenuItem) m)
317: .getPlugin()) {
318: behaviorMenuButtonGroup.remove(m);
319: m.removeActionListener(this );
320: viewBehaviorMenu.remove(m);
321: i = viewBehaviorMenu.getItemCount(); // Terminate the loop
322: }
323: }
324:
325: for (int i = 0; i < vpBehaviorToolBar
326: .getComponentCount(); i++) {
327: java.awt.Component c = vpBehaviorToolBar
328: .getComponent(i);
329: if (c instanceof BehaviorTBButton
330: && plugin == ((BehaviorTBButton) c)
331: .getPlugin()) {
332: behaviorTBButtonGroup
333: .remove((BehaviorTBButton) c);
334: ((BehaviorTBButton) c)
335: .removeActionListener(this );
336: vpBehaviorToolBar.remove((BehaviorTBButton) c);
337: i = vpBehaviorToolBar.getComponentCount(); // Terminate the loop
338: }
339: }
340:
341: vpBehaviorToolBar.repaint();
342: break;
343: case VPBehaviorPluginEvent.ACTIVATED:
344: break;
345: case VPBehaviorPluginEvent.DEACTIVATED:
346: break;
347: }
348: }
349: }
350:
351: public static class ViewingPlatformControlPluginPreference extends
352: PluginPreference {
353: public ViewingPlatformControlPluginPreference() {
354: super ();
355: }
356:
357: public ViewingPlatformControlPluginPreference(boolean enabled,
358: boolean installed) {
359: super (enabled, installed);
360: }
361:
362: public J3dFlyPlugin instantiatePlugin() {
363: plugin = new ViewingPlatformControlPlugin();
364: return plugin;
365: }
366:
367: /**
368: * Return a description of this plugin
369: */
370: public String getDescription() {
371: return "ViewingPlatfrom Controls, mainly behaviors";
372: }
373:
374: /**
375: * Return the name of the Plugin for this prefernece.
376: * This is the name that will appear in the list of plugins
377: */
378: public String getName() {
379: return "ViewingPlatform Control";
380: }
381:
382: }
383:
384: interface VPBehaviorAction {
385: public VPBehaviorPlugin getPlugin();
386: }
387:
388: class BehaviorMenuItem extends javax.swing.JRadioButtonMenuItem
389: implements VPBehaviorAction,
390: javax.swing.event.ChangeListener {
391: private VPBehaviorPlugin plugin;
392:
393: public BehaviorMenuItem(VPBehaviorPlugin plugin) {
394: super (plugin.getMenuString());
395: this .plugin = plugin;
396: }
397:
398: public VPBehaviorPlugin getPlugin() {
399: return plugin;
400: }
401:
402: // Use state change event from toolbar button to correctly set this state
403: public void stateChanged(javax.swing.event.ChangeEvent evt) {
404: setSelected(((javax.swing.AbstractButton) evt.getSource())
405: .isSelected());
406: }
407: }
408:
409: class BehaviorTBButton extends javax.swing.JToggleButton implements
410: VPBehaviorAction, javax.swing.event.ChangeListener {
411: private VPBehaviorPlugin plugin;
412:
413: public BehaviorTBButton(VPBehaviorPlugin plugin) {
414: super (plugin.getToolbarIcon());
415: this .plugin = plugin;
416: this .setPreferredSize(new java.awt.Dimension(28, 28));
417: this .setMaximumSize(new java.awt.Dimension(28, 28));
418: this .setMargin(new java.awt.Insets(2, 2, 2, 2));
419: this .setMinimumSize(new java.awt.Dimension(28, 28));
420: this .setToolTipText(plugin.getTooltipText());
421: }
422:
423: public VPBehaviorPlugin getPlugin() {
424: return plugin;
425: }
426:
427: // Use state change event from menu button to correctly set this state
428: public void stateChanged(javax.swing.event.ChangeEvent evt) {
429: setSelected(((javax.swing.AbstractButton) evt.getSource())
430: .isSelected());
431: }
432: }
433:
434: /** Exit the Application */
435: // Variables declaration - do not modify//GEN-BEGIN:variables
436: private javax.swing.JPanel dummyPanel;
437: private javax.swing.JSeparator jSeparator4;
438: private javax.swing.JSeparator jSeparator3;
439: private javax.swing.JPopupMenu menuBar;
440: private javax.swing.JMenuItem showAllnveZMI;
441: private javax.swing.JMenuItem showAllnveYMI;
442: private javax.swing.JMenuItem showAllnveXMI;
443: private javax.swing.JMenu viewBehaviorMenu;
444: private javax.swing.JMenuItem showAllZMI;
445: private javax.swing.JMenuItem showAllYMI;
446: private javax.swing.JMenuItem showAllXMI;
447: private javax.swing.JToolBar vpBehaviorToolBar;
448: private javax.swing.JCheckBoxMenuItem collisionEnabledMI;
449: private javax.swing.JMenu showAllMI;
450: // End of variables declaration//GEN-END:variables
451:
452: }
|