Copy all of the following code and paste
it into the HelloX.java
file:
// Copyright (c) 2001, 2003,
Oracle. All rights reserved.
package oracle.jdev.hellox;
import oracle.ide.WizardManager;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.Icon;
import java.lang.reflect.Modifier;
import oracle.ide.Ide;
import oracle.ide.cmd.buffer.Edit;
import oracle.ide.editor.Editor;
import oracle.ide.editor.EditorUtil;
import oracle.ide.editor.EditorManager;
import oracle.ide.util.GraphicsUtils;
import oracle.ide.util.MenuSpec;
import oracle.ide.addin.Context;
import oracle.ide.addin.Wizard;
import oracle.ide.addin.Addin;
import oracle.ide.model.Project;
import oracle.ide.model.NodeFactory;
import oracle.ide.net.URLFactory;
import oracle.ide.net.URLPath;
import oracle.jdeveloper.model.JProject;
import oracle.jdeveloper.model.JavaSourceNode;
import oracle.jdeveloper.jot.*;
/**
* This sample demonstrates
a JDeveloper extension that can be invoked to create
* a Java file and add it
to the currently selected project.
*
* This wizard can be installed
in the gallery and invoked from there.
* When invoked, the wizard
opens a Class Wizard dialog, which requests a name
* for the class. When it
is provided the wizard creates a Java program.
*/
public class HelloX implements
Wizard, Addin
{
//
// Fields
//
protected String imageName
= "HelloX.gif";
protected String wizName
= "Hello X Wizard";
protected String prompt
= "Greetee:";
private Icon image;
private HelloX instance;
private MenuSpec menuSpec;
//
// Constructors
//
/**
* Default constructor.
*
* A singleton instance is
created when the wizard is loaded at startup.
*
* This wizard is registered
here so that it can be accessed from the
* Tools menu.
*/
public HelloX()
{
if ( instance == null )
{
instance = this;
}
}
//
// Methods required by the
Wizard interface
//
/**
* Performs the wizard's
function. This method is called by the IDE when
* the user selects one of
the wizard's UI elements.
*
* This wizard creates a
Java file, adds it to the current project, and opens
* it for editing.
*
* @param context the context
of the invocation, which names the current
* project.
* @param params optional
arguments.
*
* @return true if the invocation
was successful, false otherwise.
*/
public boolean invoke(oracle.ide.addin.Context
context, java.lang.String[] params)
{
if ( !this.isAvailable(context)
)
return false;
String greetee = null;
JProject project = (JProject)
context.getProject();
// Get the parameter from
the user.
greetee = JOptionPane.showInputDialog
(new JDialog(), prompt,
wizName, JOptionPane.OK_CANCEL_OPTION);
if ( greetee == null )
return false;
// Create the document and
the node that represents it.
if ( !createNode(project,
greetee) )
return false;
return true;
}
/**
* Returns the human-readable
name of this wizard.
*/
public java.lang.String
getName()
{
return wizName;
}
/**
* Returns the wizard's menu
label.
*/
public java.lang.String
getMenuLabel()
{
return wizName;
}
/**
* Returns the menu presentation
for this wizard.
*/
public MenuSpec getMenuSpecification()
{
if ( menuSpec == null)
{
Icon icon = getIcon();
menuSpec = new MenuSpec(wizName,
new Integer((int)'X'), (KeyStroke)null, icon);
}
return menuSpec;
}
/**
* Determines the active
state of this wizard.
* @param context a record
of the current state of the IDE.
* @return true if this wizard
is to be activated; false if it is to be
* inactivated.
*/
public boolean isAvailable(oracle.ide.addin.Context
context)
{
Project p = context.getProject();
if ( (p != null) &&
(p instanceof JProject) )
return true;
return false;
}
/**
* Returns a string representation
of the wizard. Usually, this should be
* the same as getName().
*/
public String toString()
{
return wizName;
}
/**
* Returns the icon to be
used for this wizard in the Object Gallery and
* Navigator.
* @return an icon if this
wizard is to be accessable from the Object Gallery,
* or null if not.
*/
public Icon getIcon()
{
if (image == null)
{
image = GraphicsUtils.createImageIcon(
GraphicsUtils.loadFromResource(imageName,
this.getClass()));
}
return image;
}
//
// Methods required by the
Addin interface
//
public void initialize()
{
WizardManager wizardMgr
= WizardManager.getInstance();
wizardMgr.registerWizard((Wizard)this,
true);
}
public void shutdown()
{
}
public float version()
{
return 1.0f;
}
public float ideVersion()
{
return Ide.getVersion();
}
public boolean canShutdown()
{
return true;
}
//
// Other methods
//
/**
* Creates a node representing
a Java file.
* @param project the file's
project.
* @param greetee the parameterized
part of the class.
* @return true if the node
was successfully created; false otherwise.
*/
protected boolean createNode
(
JProject project,
String greetee
)
{
String className = "Hello"
+ greetee;
JavaSourceNode node = null;
// Construct a URL for the
class
URL dirURL = getProjectSourceDirectory(project);
if ( dirURL == null )
return false;
URL classURL = URLFactory.newURL(dirURL,
className + ".java");
if ( classURL == null )
return false;
// Make sure the node doesn't
already exist
if ( NodeFactory.find(classURL)
!= null )
return false;
// Create the node
try
{
node = (JavaSourceNode)
NodeFactory.findOrCreate(JavaSourceNode.class, classURL);
project.add(node, true);
node.open();
}
catch (Throwable t)
{
return false;
}
// Create the content
boolean success = createContent(node,
project, className, greetee);
if ( !success )
return false;
// Open the document for
editing.
EditorUtil.openDefaultEditorInFrame(node);
return true;
}
/**
* Creates a Java source
file containing the skeleton of a class
* or interface definition.
* @param node the node that
will represent the file.
* @param project the file's
project.
* @param className the name
of the class or interface.
* @param greetee the parameterized
part of the output text.
* @param packageName the
body of the file's package statement.
* @return true if the file
was successfully created; false otherwise.
*/
protected boolean createContent
(
JavaSourceNode node,
JProject project,
String className,
String greetee
)
{
JotManager mgr = JotManager.getJotManager(project);
JotFile jotFile = mgr.getFile(node.getURL());
String greeting = "\"Hello,
" + greetee + "!\"";
if ( jotFile == null )
return false;
// Describe the class
JotClass helloxClass = jotFile.addClass(className);
helloxClass.setModifiers(Modifier.PUBLIC);
helloxClass.setIsInterface(false);
// Create the main method
JotMethod mainMethod = helloxClass.addMethod(null,
true, "void", "main");
mainMethod.setModifiers(Modifier.PUBLIC
+ Modifier.STATIC);
mainMethod.addParameter("String[]",
"args");
JotCodeBlock mainCodeBlock
= mainMethod.getCodeBlock();
JotMethodCall printlnCall
= mainCodeBlock.createMethodCall("System.out", "println");
printlnCall.addArgument(greeting);
mainCodeBlock.addExpressionStatement((JotStatementExpression)printlnCall);
// Write the JOT representation
to the file
jotFile.commitFile();
return true;
}
/**
* Constructs a URL for a
project's source files.
* @param project A project
(.jpr) node.
* @return a URL constructed
from the project's source path and default
* directory.
*/
protected URL getProjectSourceDirectory(JProject
project)
{
if ( project == null )
return null;
URLPath prjSrcPath = project.getProjectSourcePath();
URL base = prjSrcPath.getFirstEntry();
String defaultPkg = project.getDefaultPackage();
if ( (base == null) || (defaultPkg
== null) )
return null;
String defaultPkgDir = defaultPkg.replace('.',
'/');
URL dirURL = URLFactory.newDirURL(base,
defaultPkgDir);
return dirURL;
}
}
Notes:
When the wizard's New Gallery page or menu is opened, before
the wizard is actually selected, the Wizard Manager calls the wizard's
getIcon()
method to obtain its graphic and call isAvailable()
to determine if the icon and name should be displayed as selectable or
not. In this case, the generated class file must be added to an existing
project, so the current context — the currently selected object — must
be a JProject.
When the user selects the wizard's item the Wizard Manager calls
its invoke() method. As
a typical wizard, Hello X opens a modal dialog to obtain user input — the
string greetee — and passes it to the method that will create the class
and its node.
To construct the node, the createNode()
method creates a URL from the class name (a concatenation of "World" and
the greetee parameter supplied
by the user), and the project's URL. The class file will be created in
the project's directory.
The content of the class file is Java code, created in the
createContent() method using JOT (Java Object Tool). JOT classes
represent Java's syntactic elements, such as classes, methods, parameters,
and expressions.
|