/** Restores the object state from the memento. */ public IStatus restoreState() { Assert.isTrue(!isRestored()); IStatus result = Status.OK_STATUS; IMemento memento = this.memento; this.memento = null; String factoryId = memento.getString(IWorkbenchConstants.TAG_FACTORY_ID); if (factoryId == null) { WorkbenchPlugin.log("Unable to restore mru list - no input factory ID."); // $NON-NLS-1$ return result; } IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryId); if (factory == null) { return result; } IMemento persistableMemento = memento.getChild(IWorkbenchConstants.TAG_PERSISTABLE); if (persistableMemento == null) { WorkbenchPlugin.log( "Unable to restore mru list - no input element state: " + factoryId); // $NON-NLS-1$ return result; } IAdaptable adaptable = factory.createElement(persistableMemento); if (adaptable == null || (adaptable instanceof IEditorInput) == false) { return result; } input = (IEditorInput) adaptable; // Get the editor descriptor. String editorId = memento.getString(IWorkbenchConstants.TAG_ID); if (editorId != null) { IEditorRegistry registry = WorkbenchPlugin.getDefault().getEditorRegistry(); descriptor = registry.findEditor(editorId); } return result; }
/* * @see org.eclipse.swt.dnd.DragSourceListener#dragStart */ @Override public void dragStart(DragSourceEvent event) { fEditorInputDatas = new ArrayList<EditorInputData>(); ISelection selection = fProvider.getSelection(); if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext(); ) { Object element = iter.next(); IEditorInput editorInput = EditorUtility.getEditorInput(element); if (editorInput != null && editorInput.getPersistable() != null) { try { String editorId = EditorUtility.getEditorID(editorInput); // see org.eclipse.ui.internal.ide.EditorAreaDropAdapter.openNonExternalEditor(..): IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry(); IEditorDescriptor editorDesc = editorReg.findEditor(editorId); if (editorDesc != null && !editorDesc.isOpenExternal()) { fEditorInputDatas.add( EditorInputTransfer.createEditorInputData(editorId, editorInput)); } } catch (PartInitException e) { JavaPlugin.log(e); } } } } event.doit = fEditorInputDatas.size() > 0; }
public static IEditorDescriptor getEditorDescription(IFile file) { IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry(); IEditorDescriptor selectedDescriptor = null; String defaultEditorID = null; try { defaultEditorID = file.getPersistentProperty(IDE.EDITOR_KEY); } catch (CoreException e) { } if (defaultEditorID != null) selectedDescriptor = editorReg.findEditor(defaultEditorID); if (selectedDescriptor == null) { /* JastAddModel model = JastAddModelProvider.getModel(file); if (model != null) { */ IEditorDescriptor[] descriptors = editorReg.getEditors(file.getName()); for (IEditorDescriptor descriptor : descriptors) if (descriptor.getId().equals(JastAddJEditor.EDITOR_ID)) selectedDescriptor = descriptor; if (selectedDescriptor == null && descriptors.length > 0) selectedDescriptor = descriptors[0]; // } } return selectedDescriptor; }
@Override public IEditorDescriptor getEditorDescriptor(String name, IEditorRegistry editorReg) { IEditorDescriptor editorDesc = null; // next check the OS for in-place editor (OLE on Win32) if (editorReg.isSystemInPlaceEditorAvailable(name)) { editorDesc = editorReg.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); } // next check with the OS for an external editor if (editorDesc == null && editorReg.isSystemExternalEditorAvailable(name)) { editorDesc = editorReg.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); } // next lookup the default text editor if (editorDesc == null) { editorDesc = editorReg.findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); } return editorDesc; }
@Override IWorkbenchPart createErrorPart() { IStatus status = new Status( IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, NLS.bind(WorkbenchMessages.EditorManager_missing_editor_descriptor, descriptorId)); IEditorRegistry registry = getPage().getWorkbenchWindow().getWorkbench().getEditorRegistry(); descriptor = (EditorDescriptor) registry.findEditor(EditorRegistry.EMPTY_EDITOR_ID); return createErrorPart(status); }
/** * Opens the given file in the registered editor for the file type, or in the default text editor * if no editor is registered. This differs from the openInEditor() method in that the system * editor will never be opened. * * @param file the file to open * @return an open editor * @throws PartInitException if the editor could not be opened or the input element is not valid */ public static IEditorPart openInTextEditor(IFile file, boolean activate) throws PartInitException { UIInstrumentationBuilder instrumentation = UIInstrumentation.builder("EditorUtility.openInTextEditor"); try { if (file == null) { instrumentation.metric("Problem", "file is null"); throwPartInitException(DartEditorMessages.EditorUtility_file_must_not_be_null); } instrumentation.data("FileName", file.getName()); instrumentation.data("FilePath", file.getFullPath().toOSString()); IWorkbenchPage p = DartToolsPlugin.getActivePage(); if (p == null) { instrumentation.metric("Problem", "no active workbench page"); throwPartInitException(DartEditorMessages.EditorUtility_no_active_WorkbenchPage); } IEditorDescriptor desc = IDE.getEditorDescriptor(file, true); if (desc.getId() == IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID) { IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry(); desc = editorReg.findEditor(EditorsUI.DEFAULT_TEXT_EDITOR_ID); } IEditorPart editorPart = IDE.openEditor(p, file, maybeSwapDefaultEditorDescriptor(desc.getId()), activate); initializeHighlightRange(editorPart); return editorPart; } catch (RuntimeException e) { instrumentation.metric("Exception", e.getClass().toString()); instrumentation.data("Exception", e.toString()); throw e; } finally { instrumentation.log(); } }
private void initializeTitle(IEditorInput input) { Image oldImage = fTitleImage; fTitleImage = null; String title = ""; // $NON-NLS-1$ if (input != null) { IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry(); IEditorDescriptor editorDesc = editorRegistry.findEditor(getSite().getId()); ImageDescriptor imageDesc = editorDesc != null ? editorDesc.getImageDescriptor() : null; fTitleImage = imageDesc != null ? imageDesc.createImage() : null; title = input.getName(); } setTitleImage(fTitleImage); setPartName(title); firePropertyChange(PROP_DIRTY); if (oldImage != null && !oldImage.isDisposed()) oldImage.dispose(); }
public EditorReference( IEclipseContext windowContext, IWorkbenchPage page, MPart part, IEditorInput input, EditorDescriptor descriptor, IMemento editorState) { super(windowContext, page, part); this.input = input; this.descriptor = descriptor; this.editorState = editorState; if (descriptor == null) { try { String memento = getModel().getPersistedState().get(MEMENTO_KEY); if (memento == null) { descriptorId = EditorRegistry.EMPTY_EDITOR_ID; } else { XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento)); IEditorRegistry registry = getPage().getWorkbenchWindow().getWorkbench().getEditorRegistry(); descriptorId = createReadRoot.getString(IWorkbenchConstants.TAG_ID); this.descriptor = (EditorDescriptor) registry.findEditor(descriptorId); boolean pinnedVal = "true" .equals(createReadRoot.getString(IWorkbenchConstants.TAG_PINNED)); // $NON-NLS-1$ setPinned(pinnedVal); String ttip = createReadRoot.getString(IWorkbenchConstants.TAG_TOOLTIP); part.getTransientData().put(IPresentationEngine.OVERRIDE_TITLE_TOOL_TIP_KEY, ttip); } } catch (WorkbenchException e) { WorkbenchPlugin.log(e); } } else { descriptorId = this.descriptor.getId(); } }
public EditorReference( IEclipseContext windowContext, IWorkbenchPage page, MPart part, IEditorInput input, EditorDescriptor descriptor, IMemento editorState) { super(windowContext, page, part); this.input = input; this.descriptor = descriptor; this.editorState = editorState; if (descriptor == null) { try { String memento = getModel().getPersistedState().get(MEMENTO_KEY); if (memento == null) { descriptorId = EditorRegistry.EMPTY_EDITOR_ID; } else { XMLMemento createReadRoot = XMLMemento.createReadRoot(new StringReader(memento)); IEditorRegistry registry = getPage().getWorkbenchWindow().getWorkbench().getEditorRegistry(); descriptorId = createReadRoot.getString(IWorkbenchConstants.TAG_ID); this.descriptor = (EditorDescriptor) registry.findEditor(descriptorId); } if (this.descriptor == null) { setImageDescriptor(ImageDescriptor.getMissingImageDescriptor()); } else { setImageDescriptor(this.descriptor.getImageDescriptor()); } } catch (WorkbenchException e) { WorkbenchPlugin.log(e); } } else { descriptorId = this.descriptor.getId(); setImageDescriptor(this.descriptor.getImageDescriptor()); } }
public void fill(Menu menu, int index) { final IFileRevision fileRevision = getFileRevision(); if (fileRevision == null) { return; } IEditorDescriptor defaultTextEditor = registry.findEditor("org.eclipse.ui.DefaultTextEditor"); // $NON-NLS-1$ IEditorDescriptor preferredEditor = Utils.getDefaultEditor(fileRevision); Object[] editors = Utils.getEditors(fileRevision); Collections.sort(Arrays.asList(editors), comparer); boolean defaultFound = false; // Check that we don't add it twice. This is possible // if the same editor goes to two mappings. ArrayList alreadyMapped = new ArrayList(); for (int i = 0; i < editors.length; i++) { IEditorDescriptor editor = (IEditorDescriptor) editors[i]; if (!alreadyMapped.contains(editor)) { createMenuItem(menu, editor, preferredEditor); if (defaultTextEditor != null && editor.getId().equals(defaultTextEditor.getId())) { defaultFound = true; } alreadyMapped.add(editor); } } // Only add a separator if there is something to separate if (editors.length > 0) { new MenuItem(menu, SWT.SEPARATOR); } // Add default editor. Check it if it is saved as the preference. if (!defaultFound && defaultTextEditor != null) { createMenuItem(menu, defaultTextEditor, preferredEditor); } // TODO : We might perhaps enable inplace and system external editors menu items /*// Add system editor IEditorDescriptor descriptor = registry .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); final MenuItem systemEditorMenuItem = createMenuItem(menu, descriptor, preferredEditor); systemEditorMenuItem.setEnabled(false); // Add system in-place editor descriptor = registry .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); final MenuItem inPlaceEditorMenuItem = (descriptor != null) ? createMenuItem( menu, descriptor, preferredEditor) : null; if (inPlaceEditorMenuItem != null) inPlaceEditorMenuItem.setEnabled(false); Job job = new Job("updateOpenWithMenu") { //$NON-NLS-1$ protected IStatus run(IProgressMonitor monitor) { try { final boolean isFile = fileRevision.getStorage(monitor) instanceof IFile; Display.getDefault().asyncExec(new Runnable() { public void run() { if (inPlaceEditorMenuItem != null && !inPlaceEditorMenuItem.isDisposed()) inPlaceEditorMenuItem.setEnabled(isFile); if (!systemEditorMenuItem.isDisposed()) systemEditorMenuItem.setEnabled(isFile); } }); return Status.OK_STATUS; } catch (CoreException e) { return new Status(IStatus.WARNING, TeamUIPlugin.ID, null, e); } }; }; job.setSystem(true); job.schedule();*/ createDefaultMenuItem(menu, fileRevision); // add Other... menu item createOtherMenuItem(menu); }