Example usage for org.eclipse.jdt.core IJavaElement getJavaProject

List of usage examples for org.eclipse.jdt.core IJavaElement getJavaProject

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getJavaProject.

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:edu.buffalo.cse.green.action.AddJavaFileAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *///from w  w  w. j ava 2  s. co m
public void run(IAction action) {
    StructuredSelection ss = (StructuredSelection) _selection;
    DiagramEditor editor = DiagramEditor.getActiveEditor();

    // ensure an editor is open
    if (editor == null) {
        try {
            editor = DiagramEditor.createEditor(ss);
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }

    for (IJavaElement element : new IterableSelection<IJavaElement>(ss)) {
        // If current editor's project is not set, add to it.
        if (editor.getProject() != null) {
            if (!editor.getProject().getHandleIdentifier()
                    .equals(element.getJavaProject().getHandleIdentifier())) {
                // if the editor we found can't hold the current element,
                // see if an editor exists that can hold the element
                editor = DiagramEditor.findProjectEditor(element.getJavaProject());
                // if no such editor exists...
                if (editor == null) {
                    // create one
                    try {
                        editor = DiagramEditor.createEditor(new StructuredSelection(element));
                    } catch (JavaModelException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        try {
            editor.execute(new AddJavaElementCommand(editor, element));
        } catch (GreenException e) {
            MessageDialog.openError(editor.getSite().getShell(), "Error", e.getLocalizedMessage());
        }
    }

    editor.refresh();
}

From source file:edu.buffalo.cse.green.action.IncrementalInNewEditorAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *///from w  ww.  ja  v  a 2s.c  om
public void run(IAction action) {
    StructuredSelection ss = (StructuredSelection) _selection;
    DiagramEditor editor = DiagramEditor.getActiveEditor();

    try {
        editor = DiagramEditor.createEditor(ss);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    for (IJavaElement element1 : new IterableSelection<IJavaElement>(ss)) {
        // If current editor's project is not set, add to it.
        if (editor.getProject() != null) {
            if (!editor.getProject().getHandleIdentifier()
                    .equals(element1.getJavaProject().getHandleIdentifier())) {
                // if the editor we found can't hold the current element,
                // see if an editor exists that can hold the element
                editor = DiagramEditor.findProjectEditor(element1.getJavaProject());
                // if no such editor exists...
                if (editor == null) {
                    // create one
                    try {
                        editor = DiagramEditor.createEditor(new StructuredSelection(element1));
                    } catch (JavaModelException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        try {
            editor.execute(new AddJavaElementCommand(editor, element1));

        } catch (GreenException e) {
            MessageDialog.openError(editor.getSite().getShell(), "Error", e.getLocalizedMessage());
        }

    }
    RootModel root = editor.getRootModel();

    //modelList is a new list so that the iterator on root's children does not
    //throw a ConcurrentModificationException
    List<AbstractModel> modelList = new ArrayList<AbstractModel>(root.getChildren());

    for (AbstractModel model : modelList) {
        if (model instanceof TypeModel) {
            editor.execute(new IncrementalExploreCommand(editor, (TypeModel) model, true));
        }
    }

    editor.refresh();
}

From source file:edu.buffalo.cse.green.action.OpenNewEditorAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *///from ww w. j  av a2  s .  c o m
public void run(IAction action) {
    StructuredSelection ss = (StructuredSelection) _selection;
    IJavaElement element = (IJavaElement) ((StructuredSelection) _selection).getFirstElement();

    if (!(element instanceof IPackageFragment)) {
        element = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    }
    DiagramEditor editor = DiagramEditor.getActiveEditor();

    try {
        editor = DiagramEditor.createEditor(new StructuredSelection(element));
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    for (IJavaElement element1 : new IterableSelection<IJavaElement>(ss)) {
        // If current editor's project is not set, add to it.
        if (editor.getProject() != null) {
            if (!editor.getProject().getHandleIdentifier()
                    .equals(element1.getJavaProject().getHandleIdentifier())) {
                // if the editor we found can't hold the current element,
                // see if an editor exists that can hold the element
                editor = DiagramEditor.findProjectEditor(element1.getJavaProject());
                // if no such editor exists...
                if (editor == null) {
                    // create one
                    try {
                        editor = DiagramEditor.createEditor(new StructuredSelection(element1));
                    } catch (JavaModelException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        try {
            editor.execute(new AddJavaElementCommand(editor, element1));
        } catch (GreenException e) {
            MessageDialog.openError(editor.getSite().getShell(), "Error", e.getLocalizedMessage());
        }
    }

    editor.refresh();
}

From source file:edu.buffalo.cse.green.editor.DiagramEditor.java

License:Open Source License

/**
 * Opens a blank editor.//from   w  w w.j  a  va2 s  .  com
 * 
 * @param element - The element to place the diagram file in.
 * @return a reference to the opened editor, if successful.
 */
private static DiagramEditor createEditor(IJavaElement element) throws JavaModelException {
    IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage workbenchPage = dwindow.getActivePage();
    IPackageFragment packFrag = null;
    IPath elementPath;

    // get the project itself, if desired (for creating DIA)
    if (element.isReadOnly() || PlugIn.getBooleanPreference(P_FORCE_DIA_IN_PROJECT)) {
        element = element.getAncestor(IJavaElement.JAVA_PROJECT);
    }

    if (element instanceof IJavaProject) {
        IJavaProject project = (IJavaProject) element;
        packFrag = project.getPackageFragments()[0];
    } else if (!(element instanceof IPackageFragment)) {
        packFrag = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    } else {
        packFrag = (IPackageFragment) element;
    }

    // create a path to the diagram file with current extension
    elementPath = packFrag.getPath()
            .append(packFrag.getJavaProject().getElementName() + "." + PluginConstants.GREEN_EXTENSION);

    try {
        IFile diaFile = DiagramEditor.getFileNotExist(element.getJavaProject().getProject(), elementPath);
        DiagramEditor editor = (DiagramEditor) IDE.openEditor(workbenchPage, diaFile, true);
        ACTIVE_EDITOR = editor;
        return editor;
    } catch (CoreException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:edu.uci.lighthouse.core.util.ModelUtility.java

License:Open Source License

/**
 * Verify if some given iFile belongs to the Projects that was imported to the database
 * /*ww w  . j a va 2s.c om*/
 * @param iFile
 * @param checkDatabase
 * @return
 */
public static boolean belongsToImportedProjects(IFile iFile, boolean checkDatabase) {
    IJavaElement jFile = JavaCore.create(iFile);
    if (jFile != null) {
        /* When the user is checking out, there is no working copy to compare with. So, we have to compare with the database to guarantee that imported projects existing in database can be checkout. */
        if (checkDatabase) {
            String clazzName = getClassFullyQualifiedName(iFile);
            try {
                LighthouseEntity entity = new LHEntityDAO().get(LHStringUtil.getMD5Hash(clazzName));
                return (entity != null);
            } catch (Exception e) {
                logger.info("Class: " + clazzName + " is not on the database");
            }
        }
        String projectName = jFile.getJavaProject().getElementName();
        LighthouseModel model = LighthouseModel.getInstance();
        if (model.getProjectNames().contains(projectName)) {
            return true;
        }
    }
    return false;
}

From source file:edu.uci.lighthouse.ui.views.actions.LinkWithEditorAction.java

License:Open Source License

private void highlightClassInDiagram(IFile classFile) {
    // highlightClassInDiagram(classFile.getName().replaceAll(".java", ""));
    IJavaElement jFile = JavaCore.create(classFile);
    if (jFile instanceof ICompilationUnit) {
        IType type = ((ICompilationUnit) jFile).findPrimaryType();
        String fqn = jFile.getJavaProject().getElementName() + "." + type.getFullyQualifiedName();
        LighthouseEntity entity = LighthouseModel.getInstance().getEntity(fqn);
        if (entity != null) {
            GraphItem gItem = viewer.findGraphItem(entity);
            if (gItem instanceof GraphNode && !gItem.equals(currentHighlightedNode)) {
                unHighlightNode(currentHighlightedNode);
                highlightNode((GraphNode) gItem);
            }//from w  w  w  .j  av a2  s .c o m
        }
    }
}

From source file:edu.uci.lighthouse.views.filters.ActiveClassFilter.java

License:Open Source License

public LighthouseEntity getLighthouseEntityFromEditor() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage activePage = window.getActivePage();
    if (activePage != null) {
        IEditorPart editor = activePage.getActiveEditor();
        if (editor != null) {
            IJavaElement jFile = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
            if (jFile instanceof ICompilationUnit) {
                IType type = ((ICompilationUnit) jFile).findPrimaryType();
                String fqn = jFile.getJavaProject().getElementName() + "." + type.getFullyQualifiedName();
                LighthouseEntity entity = LighthouseModel.getInstance().getEntity(fqn);
                if (entity instanceof LighthouseClass || entity instanceof LighthouseInterface) {
                    return entity;
                }/* w w w  .  j a va2  s.  co m*/
            }
        }
    }
    return null;
}

From source file:edu.uci.lighthouse.views.filters.OpenEditorFilter.java

License:Open Source License

public static Collection<LighthouseClass> getLighthouseClassesFromEditor() {
    Collection<LighthouseClass> result = new LinkedList<LighthouseClass>();
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage activePage = window.getActivePage();
    if (activePage != null) {
        IEditorReference[] editorReferences = activePage.getEditorReferences();
        for (IEditorReference editorReference : editorReferences) {
            try {
                IJavaElement jFile = JavaUI.getEditorInputJavaElement(editorReference.getEditorInput());
                if (jFile instanceof ICompilationUnit) {
                    IType type = ((ICompilationUnit) jFile).findPrimaryType();
                    String fqn = jFile.getJavaProject().getElementName() + "." + type.getFullyQualifiedName();
                    LighthouseEntity entity = LighthouseModel.getInstance().getEntity(fqn);
                    if (entity instanceof LighthouseClass) {
                        result.add((LighthouseClass) entity);
                    }/*  w ww.  ja v  a  2  s  .  c o m*/
                }
            } catch (Exception e) {
                // TODO: logger
            }
        }
    }
    return result;
}

From source file:es.bsc.servicess.ide.actions.BuildServiceAction.java

License:Apache License

@Override
public void run(IAction arg0) {
    IJavaProject project = null;//from   w  ww  .  j av a  2 s. c o  m
    if (selection != null) {
        IJavaElement el = getInitialJavaElement(selection);
        if (el != null) {
            project = el.getJavaProject();
        }
    }
    if (project == null) {
        IJavaProject[] projects;
        try {
            projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();

            ElementListSelectionDialog dialog = new ElementListSelectionDialog(window.getShell(),
                    new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
            dialog.setIgnoreCase(false);
            dialog.setTitle("Choose Project");
            dialog.setMessage("Choose Project to build");
            dialog.setEmptyListMessage("Empty");
            dialog.setElements(projects);
            dialog.setHelpAvailable(false);
            if (dialog.open() == Window.OK) {
                project = (IJavaProject) dialog.getFirstResult();
            } else {
                ErrorDialog.openError(window.getShell(), "Error", "Getting the Project",
                        new StatusInfo(IStatus.ERROR, "Error getting the project"));
            }
        } catch (JavaModelException e) {
            ErrorDialog.openError(window.getShell(), "Error", e.getMessage(),
                    new StatusInfo(IStatus.ERROR, "Ezception during service building"));
        }
    }
    if (project != null) {
        IProgressMonitor myProgressMonitor = new NullProgressMonitor();
        try {
            //TODO Y3: Add the automatic package grouping.
            IFile file = project.getProject().getFolder(ProjectMetadata.METADATA_FOLDER)
                    .getFile(ProjectMetadata.METADATA_FILENAME);

            ProjectMetadata pr_meta = new ProjectMetadata(new File(file.getRawLocation().toOSString()));
            //TODO Y3: Add progress monitor
            PackagingUtils.buildPackages(project, pr_meta, myProgressMonitor);

        } catch (Exception e) {
            ErrorDialog.openError(window.getShell(), "Error Building Service", e.getMessage(),
                    new StatusInfo(IStatus.ERROR, "Exception during service buildinge"));
        }

    }

}

From source file:es.bsc.servicess.ide.actions.DeployAction.java

License:Apache License

/**
 * The action has been activated. The argument of the method represents the
 * 'real' action sitting in the workbench UI.
 * /*from w  w  w .  j  a  va  2  s .c  o m*/
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    DeployServiceDialog dialog = new DeployServiceDialog(window.getShell());
    if (selection != null) {
        IJavaElement el = getInitialJavaElement(selection);
        if (el != null) {
            dialog.setProject(el.getJavaProject());
        }
    }
    if (dialog.open() == Window.OK) {
        try {
            addLocalhostToProjectFile(dialog.getProject(), dialog.getCoreElementsFolder());
            deployOrchestrations(dialog.getProject(), dialog.getServerLocation());
            deployCoreElements(dialog.getProject(), dialog.getCoreElementsFolder());
            startServer(dialog.getProject(), dialog.getServerLocation());
        } catch (IOException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (CoreException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (ConfigurationException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (SAXException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        } catch (TransformerException e) {
            MessageDialog.openError(window.getShell(), "Error", e.getMessage());
            e.printStackTrace();
        }
    } else {
        MessageDialog.openError(window.getShell(), "Error", "Getting the Project");
    }
}