Java Code Examples for org.eclipse.jdt.internal.ui.javaeditor.EditorUtility#openInEditor()

The following examples show how to use org.eclipse.jdt.internal.ui.javaeditor.EditorUtility#openInEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertyKeyHyperlink.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void open(KeyReference keyReference) {
	if (keyReference == null)
		return;

	try {
		IEditorPart part= keyReference.element != null ? EditorUtility.openInEditor(keyReference.element, true) : EditorUtility.openInEditor(keyReference.resource, true);
		if (part != null)
			EditorUtility.revealInEditor(part, keyReference.offset, keyReference.length);
	} catch (PartInitException x) {

		String message= null;

		IWorkbenchAdapter wbAdapter= (IWorkbenchAdapter)((IAdaptable)keyReference).getAdapter(IWorkbenchAdapter.class);
		if (wbAdapter != null)
			message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_messageArgs,
					new String[] { wbAdapter.getLabel(keyReference), x.getLocalizedMessage() } );

		if (message == null)
			message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_message, x.getLocalizedMessage());

		MessageDialog.openError(fShell,
			PropertiesFileEditorMessages.OpenAction_error_messageProblems,
			message);
	}
}
 
Example 2
Source File: JavaSearchEditorOpener.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IEditorPart showWithoutReuse(Object element) throws PartInitException {
	try {
		return EditorUtility.openInEditor(element, false);
	} catch (PartInitException e) {
		if (e.getStatus().getCode() != IJavaStatusConstants.EDITOR_NO_EDITOR_INPUT) {
			throw e;
		}
	}
	return null;
}
 
Example 3
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		return JavaUI.openInEditor(element);
	}
	
	IPackageFragment packageFragment= (IPackageFragment) element;
	ITypeRoot typeRoot;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	} else {
		typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	}

	// open the package-info file in editor if one exists
	if (typeRoot.exists())
		return JavaUI.openInEditor(typeRoot);

	// open the package.html file in editor if one exists
	Object[] nonJavaResources= packageFragment.getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return EditorUtility.openInEditor(file, true);
			}
		}
	}

	// select the package in the Package Explorer if there is no associated package Javadoc file
	PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
	view.tryToReveal(packageFragment);
	return null;
}
 
Example 4
Source File: AbstractInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void gotoSelectedElement() {
	Object selectedElement= getSelectedElement();
	if (selectedElement != null) {
		try {
			dispose();
			IEditorPart part= EditorUtility.openInEditor(selectedElement, true);
			if (part != null && selectedElement instanceof IJavaElement)
				EditorUtility.revealInEditor(part, (IJavaElement) selectedElement);
		} catch (CoreException ex) {
			JavaPlugin.log(ex);
		}
	}
}
 
Example 5
Source File: PackageExplorerPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void createPartControl(Composite parent) {

	final PerformanceStats stats= PerformanceStats.getStats(PERF_CREATE_PART_CONTROL, this);
	stats.startRun();

	fViewer= createViewer(parent);
	fViewer.setUseHashlookup(true);

	initDragAndDrop();

	setProviders();

	JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);


	MenuManager menuMgr= new MenuManager("#PopupMenu"); //$NON-NLS-1$
	menuMgr.setRemoveAllWhenShown(true);
	menuMgr.addMenuListener(this);
	fContextMenu= menuMgr.createContextMenu(fViewer.getTree());
	fViewer.getTree().setMenu(fContextMenu);

	// Register viewer with site. This must be done before making the actions.
	IWorkbenchPartSite site= getSite();
	site.registerContextMenu(menuMgr, fViewer);
	site.setSelectionProvider(fViewer);

	makeActions(); // call before registering for selection changes

	// Set input after filter and sorter has been set. This avoids resorting and refiltering.
	restoreFilterAndSorter();
	fViewer.setInput(findInputElement());
	initFrameActions();
	initKeyListener();

	fViewer.addDoubleClickListener(new IDoubleClickListener() {
		public void doubleClick(DoubleClickEvent event) {
			fActionSet.handleDoubleClick(event);
		}
	});

	fOpenAndLinkWithEditorHelper= new OpenAndLinkWithEditorHelper(fViewer) {
		@Override
		protected void activate(ISelection selection) {
			try {
				final Object selectedElement= SelectionUtil.getSingleElement(selection);
				if (EditorUtility.isOpenInEditor(selectedElement) != null)
					EditorUtility.openInEditor(selectedElement, true);
			} catch (PartInitException ex) {
				// ignore if no editor input can be found
			}
		}

		@Override
		protected void linkToEditor(ISelection selection) {
			PackageExplorerPart.this.linkToEditor(selection);
		}

		@Override
		protected void open(ISelection selection, boolean activate) {
			fActionSet.handleOpen(selection, activate);
		}

	};

	IStatusLineManager slManager= getViewSite().getActionBars().getStatusLineManager();
	fViewer.addSelectionChangedListener(new StatusBarUpdater(slManager));
	fViewer.addTreeListener(fExpansionListener);

	// Set help for the view
	JavaUIHelp.setHelp(fViewer, IJavaHelpContextIds.PACKAGES_VIEW);

	fillActionBars();

	updateTitle();

	fFilterUpdater= new FilterUpdater(fViewer);
	ResourcesPlugin.getWorkspace().addResourceChangeListener(fFilterUpdater);

	// Sync'ing the package explorer has to be done here. It can't be done
	// when restoring the link state since the package explorers input isn't
	// set yet.
	setLinkingEnabled(isLinkingEnabled());

	stats.endRun();
}
 
Example 6
Source File: TypeHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void setHierarchyMode(int viewerIndex) {
	Assert.isNotNull(fAllViewers);
	if (viewerIndex < fAllViewers.length && fCurrentViewerIndex != viewerIndex) {

		fCurrentViewerIndex= viewerIndex;

		updateHierarchyViewer(true);
		if (fInputElements != null) {
			ISelection currSelection= getCurrentViewer().getSelection();
			if (currSelection == null || currSelection.isEmpty()) {
				internalSelectType(getSelectableType(fInputElements), false);
				currSelection= getCurrentViewer().getSelection();
			}
			if (!fIsEnableMemberFilter) {
				typeSelectionChanged(currSelection);
			}
		}
		updateToolTipAndDescription();

		fDialogSettings.put(DIALOGSTORE_HIERARCHYVIEW, viewerIndex);
		getCurrentViewer().getTree().setFocus();

		if (fTypeOpenAndLinkWithEditorHelper != null)
			fTypeOpenAndLinkWithEditorHelper.dispose();
		fTypeOpenAndLinkWithEditorHelper= new OpenAndLinkWithEditorHelper(getCurrentViewer()) {
			@Override
			protected void activate(ISelection selection) {
				try {
					final Object selectedElement= SelectionUtil.getSingleElement(selection);
					if (EditorUtility.isOpenInEditor(selectedElement) != null)
						EditorUtility.openInEditor(selectedElement, true);
				} catch (PartInitException ex) {
					// ignore if no editor input can be found
				}
			}

			@Override
			protected void linkToEditor(ISelection selection) {
				// do nothing: this is handled in more detailed by the part itself
			}

			@Override
			protected void open(ISelection selection, boolean activate) {
				if (selection instanceof IStructuredSelection)
					fOpenAction.run((IStructuredSelection)selection);
			}
		};

	}
	for (int i= 0; i < fViewActions.length; i++) {
		ToggleViewAction action= fViewActions[i];
		action.setChecked(fCurrentViewerIndex == action.getViewerIndex());
	}
}
 
Example 7
Source File: JavaBrowsingPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds additional listeners to this view.
 * This method can be overridden but should
 * call super.
 */
protected void hookViewerListeners() {


	fOpenAndLinkWithEditorHelper= new OpenAndLinkWithEditorHelper(fViewer) {
		@Override
		protected void activate(ISelection selection) {
			try {
				final Object selectedElement= SelectionUtil.getSingleElement(selection);
				if (EditorUtility.isOpenInEditor(selectedElement) != null)
					EditorUtility.openInEditor(selectedElement, true);
			} catch (PartInitException ex) {
				// ignore if no editor input can be found
			}
		}

		@Override
		protected void linkToEditor(ISelection selection) {
			if (!fProcessSelectionEvents)
				return;

			fPreviousSelectedElement= getSingleElementFromSelection(selection);

			IWorkbenchPage page= getSite().getPage();
			if (page == null)
				return;

			if (page.equals(JavaPlugin.getActivePage()) && JavaBrowsingPart.this.equals(page.getActivePart())) {
				JavaBrowsingPart.this.linkToEditor(selection);
			}
		}

		@Override
		protected void open(ISelection selection, boolean activate) {
			IAction open= fOpenEditorGroup.getOpenAction();
			if (open.isEnabled()) {
				open.run();
				restoreSelection();
			}
		}

	};
	fOpenAndLinkWithEditorHelper.setLinkWithEditor(fLinkingEnabled);

}
 
Example 8
Source File: JavaUI.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Opens an editor on the given Java element in the active page. Valid elements are all Java elements that are {@link ISourceReference}.
	 * For elements inside a compilation unit or class file, the parent is opened in the editor is opened.
	 * If there already is an open Java editor for the given element, it is returned.
 *
 * @param element the input element; either a compilation unit
 *   (<code>ICompilationUnit</code>) or a class file (<code>IClassFile</code>) or source references inside.
 * @param activate if set, the editor will be activated.
 * @param reveal if set, the element will be revealed.
 * @return returns the editor part of the opened editor or <code>null</code> if the element is not a {@link ISourceReference} or the
 * file was opened in an external editor.
 * @exception PartInitException if the editor could not be initialized or no workbench page is active
 * @exception JavaModelException if this element does not exist or if an exception occurs while accessing its underlying resource
 * @since 3.3
 */
public static IEditorPart openInEditor(IJavaElement element, boolean activate, boolean reveal) throws JavaModelException, PartInitException {
	if (!(element instanceof ISourceReference)) {
		return null;
	}
	IEditorPart part= EditorUtility.openInEditor(element, activate);
	if (reveal && part != null) {
		EditorUtility.revealInEditor(part, element);
	}
	return part;
}