package gxtbeans.actions;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
/**
* Our sample action implements workbench action delegate.
* The action proxy will be created by the workbench and
* shown in the UI. When the user tries to use the action,
* this delegate will be created and execution will be
* delegated to it.
* @see IWorkbenchWindowActionDelegate
*/
public class GenerateBaseModelAction implements IWorkbenchWindowActionDelegate {
public static final String GXT_PATH = "gxt";
private IWorkbenchWindow window;
/**
* The constructor.
*/
private TreeSelection selection;
public GenerateBaseModelAction() {
}
/**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
if (!(selection.getFirstElement() instanceof ICompilationUnit)){
return;
}
IProgressMonitor progress = new NullProgressMonitor();
List items = selection.toList();
StringBuffer message = new StringBuffer();
for (Object item : items){
if (!(item instanceof ICompilationUnit))
continue;
/*
* Find or create a sub package called 'gxt'
*
* Find or create compilation in the new package
*
* Find or create types in the compilation unit
*/
IFile file;
try {
ICompilationUnit original = (ICompilationUnit)item;
ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setSource(original);
CompilationUnit compilationUnit = parse(original);
IProject project = original.getResource().getProject();
IPackageFragment parentPackage = (IPackageFragment) original.getParent();
/*
* create the sub package
*/
IFolder folder = (IFolder) parentPackage.getResource();
IPath newPath = folder.getProjectRelativePath();
newPath = newPath.append(GXT_PATH);
IFolder newFolder = project.getFolder(newPath);
if (!newFolder.exists()) {
newFolder.create(false, true, null);
}
IPackageFragment newPackage = (IPackageFragment) JavaCore.create(newFolder);
if (!newPackage.exists())
newPackage.save(progress, true);
/*
* Create compilation unit
*/
ICompilationUnit newCompilationUnit = newPackage.createCompilationUnit(original.getElementName(), "", true, progress);
/*
* create the package
*/
newCompilationUnit.createPackageDeclaration(newPackage.getElementName(), progress);
/*
* copy the imports
*/
IImportDeclaration[] imports = original.getImports();
for (IImportDeclaration importt : imports ){
newCompilationUnit.createImport(importt.getElementName(), null, progress);
}
// add the BaseModle import
newCompilationUnit.createImport("com.extjs.gxt.ui.client.data.BaseModel", null, progress);
NewClassGenerator generator = new NewClassGenerator(original, newCompilationUnit);
compilationUnit.accept(generator);
// Commit changes
original.save(progress, true);
} catch (JavaModelException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
}
}
private static CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
return (CompilationUnit) parser.createAST(null); // parse
}
/**
* Selection in the workbench has been changed. We
* can change the state of the 'real' action here
* if we want, but this can only happen after
* the delegate has been created.
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof ITreeSelection){
this.selection = (TreeSelection) selection;
}
}
/**
* We can use this method to dispose of any system
* resources we previously allocated.
* @see IWorkbenchWindowActionDelegate#dispose
*/
public void dispose() {
}
/**
* We will cache window object in order to
* be able to provide parent shell for the message dialog.
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}
}