001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/plugins/J3dFlyPlugin.java,v 1.1 2005/04/20 21:04:40 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 javax.media.j3d.BranchGroup;
021: import org.jdesktop.j3dfly.J3dFlyContext;
022: import javax.swing.JDialog;
023: import javax.swing.JPanel;
024: import javax.swing.JButton;
025: import javax.swing.JMenu;
026: import javax.swing.JTabbedPane;
027:
028: /**
029: * The interface for all J3dFly PlugIn's
030: *
031: * @author Paul Byrne
032: * @version $Revision: 1.1 $
033: */
034: public abstract class J3dFlyPlugin {
035:
036: protected J3dFlyContext j3dflyContext;
037: /**
038: * The tool wide preferences for this plugin
039: */
040: protected PluginPreference pluginPref;
041:
042: protected javax.swing.JMenuItem menu = null;
043:
044: /**
045: * TODO remove this debug support once we've implemented uninstall
046: * in all the plugins
047: */
048: //protected boolean uninstallDone = false;
049: /**
050: * Create a new instance of the plugin.
051: *
052: * Once the Plugin is create it must be installed by calling
053: * installPlugin
054: */
055: public J3dFlyPlugin() {
056: }
057:
058: /**
059: * Install the plugin into j3dfly with the supplied preferences
060: *
061: * Access this functionality through the preference object for this
062: * plugin
063: */
064: public void installPlugin(PluginPreference pluginPref,
065: J3dFlyContext j3dflyContext) {
066: this .pluginPref = pluginPref;
067: this .j3dflyContext = j3dflyContext;
068: j3dflyContext.getPluginPreferenceControl().addInstalledPlugin(
069: this );
070: }
071:
072: /**
073: * Uninstall the plugin
074: *
075: * Access this functionality through the preference object for this
076: * plugin
077: */
078: public void uninstallPlugin() {
079: //if (!uninstallDone)
080: // System.out.println("TODO IMplenent uninstallPlugin for "+this.getClass().getName() );
081: j3dflyContext.getPluginPreferenceControl()
082: .removeInstalledPlugin(this );
083: }
084:
085: /**
086: * Returns the class of the plugin preference used for the Tool wide
087: * preferences.
088: *
089: * Plugins that require more preference information should provide a
090: * subclass of PluginPrefernece that contains all the extra preference
091: * data. This class must be Serializable.
092: */
093: public abstract Class getPluginPreferenceClass();
094:
095: /**
096: * Return the preference object for this plugin
097: */
098: public PluginPreference getPluginPreference() {
099: return pluginPref;
100: }
101:
102: public void setPluginPreference(PluginPreference pluginPref) {
103: this .pluginPref = pluginPref;
104: }
105:
106: /**
107: * Enable/disable the plugin
108: *
109: * Access this functionality through the preference object for this
110: * plugin
111: */
112: protected void setEnabled(boolean enable) {
113: if (menu != null)
114: menu.setEnabled(enable);
115: }
116:
117: /**
118: * Return true if the plugin is enabled
119: *
120: * Access this functionality through the preference object for this
121: * plugin
122: */
123: protected boolean isEnabled() {
124: return pluginPref.isEnabled();
125: }
126:
127: /**
128: * Returns the control panel for this plugin, or null if there
129: * is no control panel
130: */
131: public abstract javax.swing.JPanel getControlPanel();
132:
133: /**
134: * Show the controls for this plugin in a non-modal dialog
135: */
136: protected void showControls() {
137: /*
138: if (getControlPanel().getParent()!=null) {
139: if (getControlPanel().getParent() instanceof JTabbedPane ) {
140: ((JTabbedPane)getControlPanel().getParent()).setSelectedComponent( getControlPanel() );
141: } else
142: throw new RuntimeException("Panel already has a parent");
143: } else {
144: */
145: java.awt.Container parent = j3dflyContext.getJ3dFly()
146: .getContainer();
147:
148: if (!(parent instanceof java.awt.Frame))
149: parent = null;
150:
151: final JDialog dialog = new JDialog(j3dflyContext.getJ3dFly()
152: .getDialogParent(), pluginPref.getName(), false);
153: final JPanel p = new JPanel();
154: dialog.setLocationRelativeTo(j3dflyContext.getJ3dFly()
155: .getContainer());
156: JPanel p2 = new JPanel();
157: JButton okButton = new JButton("OK");
158: p2.add(okButton);
159: okButton.addActionListener(new java.awt.event.ActionListener() {
160: public void actionPerformed(java.awt.event.ActionEvent evt) {
161: dialog.setVisible(false);
162: p.remove(getControlPanel());
163: dialog.dispose();
164: }
165: });
166: p.setLayout(new java.awt.BorderLayout());
167:
168: p.add(getControlPanel(), java.awt.BorderLayout.CENTER);
169:
170: p.add(p2, java.awt.BorderLayout.SOUTH);
171: dialog.getContentPane().add(p);
172: dialog.pack();
173: dialog.setVisible(true);
174: }
175:
176: /**
177: * Return the named menu from the J3dFly main Menu bar. If the menu
178: * does not exist it will be created.
179: *
180: * Using this mechanism Plugin's can add menu items to existing Menus
181: * in the MenuBar. See <TODO> for a list of default menu names
182: */
183: protected JMenu getMenu(String menuName) {
184: JMenu ret = null;
185:
186: int i = 0;
187: while (ret == null
188: && i < j3dflyContext.getMainMenuBar().getMenuCount()) {
189: if (j3dflyContext.getMainMenuBar().getMenu(i).getText()
190: .equals(menuName))
191: ret = j3dflyContext.getMainMenuBar().getMenu(i);
192: i++;
193: }
194:
195: if (ret == null) {
196: ret = new JMenu(menuName);
197: j3dflyContext.getMainMenuBar().add(ret);
198: }
199:
200: return ret;
201: }
202:
203: /**
204: * Returns the J3dFlyContext in which this plugin is installed
205: */
206: public J3dFlyContext getJ3dFlyContext() {
207: return j3dflyContext;
208: }
209: }
|