下面列出了org.eclipse.ui.IWorkbenchPage#openEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void reopenWithGWTJavaEditor(IEditorReference[] openEditors) {
IWorkbenchPage page = JavaPlugin.getActivePage();
for (IEditorReference editorRef : openEditors) {
try {
IEditorPart editor = editorRef.getEditor(false);
IEditorInput input = editorRef.getEditorInput();
// Close the editor, prompting the user to save if document is dirty
if (page.closeEditor(editor, true)) {
// Re-open the .java file in the GWT Java Editor
IEditorPart gwtEditor = page.openEditor(input, GWTJavaEditor.EDITOR_ID);
// Save the file from the new editor if the Java editor's
// auto-format-on-save action screwed up the JSNI formatting
gwtEditor.doSave(null);
}
} catch (PartInitException e) {
GWTPluginLog.logError(e, "Could not open GWT Java editor on {0}", editorRef.getTitleToolTip());
}
}
}
/**
* This operation displays the {@link ReflectivityFormEditor} instead of the
* standard ICEFormEditor.
*/
@Override
public void display() {
// Local Declarations
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
// Create the ICEFormInput for the ReflectivityFormBuilder.
ICEFormInput = new ICEFormInput(widgetForm);
try {
// Use the workbench to open the editor with our input.
IEditorPart formEditor = page.openEditor(ICEFormInput,
GeometryFormEditor.ID);
// Set this editor reference so that listeners can be registered
// later.
ICEFormEditor = (ICEFormEditor) formEditor;
} catch (PartInitException e) {
// Dump the stacktrace if something happens.
e.printStackTrace();
}
return;
}
@Override
protected void doRun() {
final IRepositoryNode node = (IRepositoryNode) ((IStructuredSelection) getSelection()).getFirstElement();
CamelProcessItem processItem = (CamelProcessItem) node.getObject().getProperty().getItem();
IWorkbenchPage page = getActivePage();
try {
CamelProcessEditorInput fileEditorInput = new CamelProcessEditorInput(processItem, true, null, true);
checkUnLoadedNodeForProcess(fileEditorInput);
IEditorPart editorPart = page.findEditor(fileEditorInput);
if (editorPart == null) {
fileEditorInput.setRepositoryNode(node);
page.openEditor(fileEditorInput, CamelMultiPageTalendEditor.ID, true);
} else {
page.activate(editorPart);
}
} catch (PartInitException | PersistenceException e) {
MessageBoxExceptionHandler.process(e);
}
}
/**
* This operation displays the {@link ReflectivityFormEditor} instead of the
* standard ICEFormEditor.
*/
@Override
public void display() {
// Local Declarations
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
// Create the ICEFormInput for the ReflectivityFormBuilder.
ICEFormInput = new ICEFormInput(widgetForm);
try {
// Use the workbench to open the editor with our input.
IEditorPart formEditor = page.openEditor(ICEFormInput,
ReflectivityFormEditor.ID);
// Set this editor reference so that listeners can be registered
// later.
ICEFormEditor = (ICEFormEditor) formEditor;
} catch (PartInitException e) {
// Dump the stacktrace if something happens.
e.printStackTrace();
}
return;
}
protected void showMatch( Match match, int currentOffset
, int currentLength, boolean activate
) throws PartInitException
{
if (match instanceof ModulaSymbolMatch) {
try {
ModulaSymbolMatch em = (ModulaSymbolMatch)match;
IFile f = em.getFile();
IWorkbenchPage page = WorkbenchUtils.getActivePage();
IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(f.getName());
IEditorPart ep = page.openEditor(new FileEditorInput(f), desc.getId());
ITextEditor te = (ITextEditor)ep;
Control ctr = (Control)te.getAdapter(Control.class);
ctr.setFocus();
te.selectAndReveal(em.getOffset(), em.getLength());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
if (inputFile == null) {
return;
}
IWorkbenchPartSite workbenchPartSite = derivedSourceView.getSite();
IWorkbenchPage workbenchPage = workbenchPartSite.getPage();
try {
IEditorPart editorPart = workbenchPage.openEditor(new FileEditorInput(inputFile),
COMPILATION_UNIT_EDITOR_ID, true, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT);
if (selectedRegion != null) {
((ITextEditor) editorPart).selectAndReveal(selectedRegion.getOffset(), selectedRegion.getLength());
}
} catch (PartInitException partInitException) {
throw new WrappedRuntimeException(partInitException);
}
}
/**
* Opens an editor in current workbench window
*
* @param editorId
* @param input
* @return the created or reopened IEditorPart
* @throws PartInitException
*/
public static IEditorPart openEditorUnchecked(String editorId, IEditorInput input, boolean activate) throws PartInitException {
final IWorkbenchPage activePage = getActivePage();
if (activePage != null) {
final IEditorPart openEditor = activePage.openEditor(input, editorId, activate);
// Trigger re-evaluation of the handler enablement state by
// cycling the activepage. Cycling the active page causes an
// event to be fired inside the selection service.
// During this time, there will be no active page!
getActiveWindow().setActivePage(null);
getActiveWindow().setActivePage(activePage);
return openEditor;
}
return null;
}
/**
* Opens an external editor on an IEditorInput containing the file having filePath
* @param editorInput
* @param filePath
* @throws PartInitException
*/
public static IEditorPart openExternalEditor(IEditorInput editorInput, String filePath) throws PartInitException {
//TODO Maybe this method could be improved by omitting filepath which comes from editorInput, but "how?" should be defined here
final IWorkbenchPage page = getPage();
IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(filePath);
if (desc == null) desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(filePath+".txt");
return page.openEditor(editorInput, desc.getId());
}
protected final void openEditor(ProcessItem processItem) throws PersistenceException, PartInitException {
// Set readonly to false since created job will always be editable.
CamelProcessEditorInput fileEditorInput = new CamelProcessEditorInput(processItem, false, true, false);
IRepositoryNode repositoryNode = RepositorySeekerManager.getInstance().searchRepoViewNode(
fileEditorInput.getItem().getProperty().getId(), false);
fileEditorInput.setRepositoryNode(repositoryNode);
IWorkbenchPage page = getActivePage();
page.openEditor(fileEditorInput, getEditorId(), true);
// // use project setting true
// ProjectSettingManager.defaultUseProjectSetting(fileEditorInput.getLoadedProcess());
}
public static void handleOpen ( final IWorkbenchPage page, final String connectionId, final String factoryId, final String configurationId )
{
try
{
page.openEditor ( new ConfigurationEditorInput ( connectionId, factoryId, configurationId ), MultiConfigurationEditor.EDITOR_ID, true );
}
catch ( final PartInitException e )
{
StatusManager.getManager ().handle ( e.getStatus () );
}
}
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
hideIntroView();
hideDashboards();
PlatformUI.getWorkbench().showPerspective(J2EE_PERSPECTIVE_ID, window);
page.openEditor(new NullEditorInput(), DASHBOARD_VIEW_ID);
} catch (Exception e) {
log.error("Cannot open dashboard", e);
}
return true;
}
@Override
protected void openAnotherVersion(RepositoryNode node, boolean readonly) {
final Item item = node.getObject().getProperty().getItem();
final IWorkbenchPage page = getActivePage();
try {
final RepositoryEditorInput fileEditorInput = getEditorInput(item, readonly, page);
page.openEditor(fileEditorInput, CamelMultiPageTalendEditor.ID, readonly);
} catch (Exception e) {
MessageBoxExceptionHandler.process(e);
}
}
/**
* Open an editor anywhere on the file system using Eclipse's default editor registered for the given file.
*
* @param fileToOpen File to open
* @note we must be in the UI thread for this method to work.
* @return Editor opened or created
*/
public static IEditorPart openFile(File fileToOpen, boolean activate) {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null) {
throw new RuntimeException("workbench cannot be null");
}
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
if (activeWorkbenchWindow == null) {
throw new RuntimeException(
"activeWorkbenchWindow cannot be null (we have to be in a ui thread for this to work)");
}
IWorkbenchPage wp = activeWorkbenchWindow.getActivePage();
final IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
try {
if (activate) {
// open the editor on the file
return IDE.openEditorOnFileStore(wp, fileStore);
}
// Workaround when we don't want to activate (as there's no suitable API
// in the core for that).
IEditorInput input = getEditorInput(fileStore);
String editorId = getEditorId(input, null);
return wp.openEditor(input, editorId, activate);
} catch (Exception e) {
Log.log("Editor failed to open", e);
return null;
}
}
private static IEditorPart openInEditor(IEditorInput input,
String editorID, boolean activate) throws CoreException {
Assert.isNotNull(input);
Assert.isNotNull(editorID);
IWorkbenchPage p = WorkbenchUtils.getActivePage();
if (p == null)
ExceptionHelper.throwCoreException(IdeCorePlugin.PLUGIN_ID, "JavaEditorMessages.EditorUtility_no_active_WorkbenchPage"); //$NON-NLS-1$
return p.openEditor(input, editorID, activate);
}
/**
* Open in an editor the given file.
*
* @param file
* @throws PartInitException
*/
public static void openFile(IFile file) throws PartInitException {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
if (desc != null) {
page.openEditor(new FileEditorInput(file), desc.getId());
}
}
/**
* @generated
*/
public void run() {
if (myDiagram == null || myDiagram.eResource() == null) {
return;
}
IEditorInput editorInput = getEditorInput(myDiagram);
IWorkbenchPage page = myViewerSite.getPage();
try {
page.openEditor(editorInput, ProcessDiagramEditor.ID);
} catch (PartInitException e) {
ProcessDiagramEditorPlugin.getInstance().logError("Exception while openning diagram", e); //$NON-NLS-1$
}
}
public static void openEditor( Object adaptable, File target,
String editorId ) throws PartInitException
{
IWorkbench workbench = PlatformUI.getWorkbench( );
IWorkbenchWindow window = workbench == null ? null
: workbench.getActiveWorkbenchWindow( );
IWorkbenchPage page = window == null ? null : window.getActivePage( );
if ( page != null )
{
IEditorInput input = null;
Object adapter = Platform.getAdapterManager( )
.getAdapter( adaptable, IPathEditorInputFactory.class );
if ( adapter instanceof IPathEditorInputFactory )
{
input = ( (IPathEditorInputFactory) adapter ).create( new Path( target.getAbsolutePath( ) ) );
IFile file = (IFile) input.getAdapter( IFile.class );
if ( file != null )
{
try
{
file.refreshLocal( IResource.DEPTH_INFINITE, null );
}
catch ( CoreException e )
{
// do nothing now
}
}
}
if ( input == null )
{
input = new ReportEditorInput( target );
}
page.openEditor( input, editorId, true );
}
}
private void openEditor(IFile file) throws PartInitException {
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
page.openEditor(new FileEditorInput(file), desc.getId());
}
public void run( )
{
FileDialog dialog = new FileDialog( fWindow.getShell( ), SWT.OPEN
| SWT.MULTI );
dialog.setText( DesignerWorkbenchMessages.Dialog_openFile );
dialog.setFilterExtensions( filterExtensions );
dialog.setFilterPath( ResourcesPlugin.getWorkspace( )
.getRoot( )
.getProjectRelativePath( )
.toOSString( ) );
dialog.open( );
String[] names = dialog.getFileNames( );
if ( names != null )
{
String fFilterPath = dialog.getFilterPath( );
int numberOfFilesNotFound = 0;
StringBuffer notFound = new StringBuffer( );
for ( int i = 0; i < names.length; i++ )
{
File file = new File( fFilterPath + File.separator + names[i] );
if ( file.exists( ) )
{
IWorkbenchPage page = fWindow.getActivePage( );
IEditorInput input = new ReportEditorInput( file );
IEditorDescriptor editorDesc = getEditorDescriptor( input,
OpenStrategy.activateOnOpen( ) );
try
{
page.openEditor( input, editorDesc.getId( ) );
}
catch ( Exception e )
{
ExceptionUtil.handle( e );
}
}
else
{
if ( ++numberOfFilesNotFound > 1 )
notFound.append( '\n' );
notFound.append( file.getName( ) );
}
}
if ( numberOfFilesNotFound > 0 )
{
// String msgFmt= numberOfFilesNotFound == 1 ?
// TextEditorMessages.OpenExternalFileAction_message_fileNotFound
// :
// TextEditorMessages.OpenExternalFileAction_message_filesNotFound;
// String msg= MessageFormat.format(msgFmt, new Object[] {
// notFound.toString() });
// MessageDialog.openError(fWindow.getShell(),
// TextEditorMessages.OpenExternalFileAction_title, msg);
}
}
}
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException {
IEditorInput input= new FileEditorInput(file);
IEditorPart editor= page.findEditor(input);
if (editor != null) {
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
IEditorReference reusedEditorRef= fReusedEditor;
if (reusedEditorRef != null) {
boolean isOpen= reusedEditorRef.getEditor(false) != null;
boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
if (canBeReused) {
boolean showsSameInputType= reusedEditorRef.getId().equals(editorId);
if (!showsSameInputType) {
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
fReusedEditor= null;
} else {
editor= reusedEditorRef.getEditor(true);
if (editor instanceof IReusableEditor) {
((IReusableEditor) editor).setInput(input);
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
}
}
}
editor= page.openEditor(input, editorId, activate);
if (editor instanceof IReusableEditor) {
IEditorReference reference= (IEditorReference) page.getReference(editor);
fReusedEditor= reference;
} else {
fReusedEditor= null;
}
return editor;
}