Java Code Examples for org.eclipse.ui.IEditorInput#getAdapter()

The following examples show how to use org.eclipse.ui.IEditorInput#getAdapter() . 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: EditorManager.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void partClosed(IWorkbenchPart part) {
	IEditorPart editorPart = getEditorPart(part);
	if (editorPart != null) {
		IEditorInput editorInput = editorPart.getEditorInput();
		IResource resource = (IResource)editorInput.getAdapter(IResource.class);
		IFileStore fileStore = CoreEditorUtils.editorInputToFileStore(editorInput);
		if (resource == null) {
			if (fileStore != null) {
				IEditableXdsModel editableModel = XdsModelManager.getEditableModel();
				IXdsElement xdsElement = editableModel.getNonWorkspaceXdsElement(fileStore);
				if (xdsElement instanceof IXdsNonWorkspaceCompilationUnit) {
					IXdsNonWorkspaceCompilationUnit compilationUnit = (IXdsNonWorkspaceCompilationUnit) xdsElement;
					editableModel.removeNonWorkspaceXdsElement(compilationUnit);
				}
			}
		}
		ModulaAst modulaAst = ModulaEditorSymbolUtils.getModulaAst(editorInput);
		XdsParserManager.discardModulaAst(modulaAst);
	}
}
 
Example 2
Source File: AbstractResourceSelectionJobCommand.java    From cppcheclipse with Apache License 2.0 6 votes vote down vote up
private IStructuredSelection getEditorFileSelection(IEditorPart editor) {
	if (editor == null) {
		return null;
	}
	IEditorInput input = editor.getEditorInput();
	if (input == null) {
		return null;
	}

	IFileEditorInput fileInput = (IFileEditorInput) input
			.getAdapter(IFileEditorInput.class);
	if (fileInput == null) {
		return null;
	}
	List<IFile> fileList = new LinkedList<IFile>();
	fileList.add(fileInput.getFile());
	return new StructuredSelection(fileList);
}
 
Example 3
Source File: BaseEditor.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return true if the editor passed as a parameter has the same input as this editor.
 */
@Override
public boolean hasSameInput(IBaseEditor edit) {
    IEditorInput thisInput = this.getEditorInput();
    IEditorInput otherInput = (IEditorInput) edit.getEditorInput();
    if (thisInput == null || otherInput == null) {
        return false;
    }

    if (thisInput == otherInput || thisInput.equals(otherInput)) {
        return true;
    }

    IResource r1 = thisInput.getAdapter(IResource.class);
    IResource r2 = otherInput.getAdapter(IResource.class);
    if (r1 == null || r2 == null) {
        return false;
    }
    if (r1.equals(r2)) {
        return true;
    }
    return false;
}
 
Example 4
Source File: DockerLogsLaunchShortcut.java    From doclipser with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
    IEditorInput input = editor.getEditorInput();
    IFile dockerfile = (IFile) input.getAdapter(IFile.class);
    IPath dockerfilePath = null;
    if (dockerfile != null) {
        dockerfilePath = dockerfile.getLocation().removeLastSegments(1);
    }
    if (dockerfilePath == null) {
        ILocationProvider locationProvider = (ILocationProvider) input
                .getAdapter(ILocationProvider.class);
        if (locationProvider != null) {
            dockerfilePath = locationProvider.getPath(input);
        }
    }
    if (dockerfilePath != null) {
        launch(dockerfile, dockerfilePath);
    }
}
 
Example 5
Source File: TexDocumentModel.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks whether all includes exists, if they are outside of the
 * project, add a link to the file to the project 
 * @param includes
 */
private void processIncludes(List<OutlineNode> includes, IEditorInput input) {
    IProject project = getCurrentProject();
    if (project == null) return;
    IFile referFile = (IFile) input.getAdapter(IFile.class);
    if (referFile == null) return;
    for (OutlineNode node : includes) {
    	IFile f = null;
        IFile mainTexFile = TexlipseProperties.getProjectSourceFile(project);
        if (mainTexFile != null) {
        	//Includes are always relative to the main file
        	f = TexProjectParser.findIFile(node.getName(), mainTexFile, project);
        }
        if (f == null) {
        	//Try finding it relative to refering file
        	f = TexProjectParser.findIFile(node.getName(), referFile, project);
        }
        if (f == null) {
            MarkerHandler marker = MarkerHandler.getInstance();
            String errorMsg = MessageFormat.format(
                    TexlipsePlugin.getResourceString("parseErrorIncludeNotFound"),
                    new Object[] { node.getName() });
            marker.createErrorMarker(referFile, errorMsg, node.getBeginLine());
        }
    }
}
 
Example 6
Source File: DockerRmLaunchShortcut.java    From doclipser with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
    IEditorInput input = editor.getEditorInput();
    IFile dockerfile = (IFile) input.getAdapter(IFile.class);
    IPath dockerfilePath = null;
    if (dockerfile != null) {
        dockerfilePath = dockerfile.getLocation().removeLastSegments(1);
    }
    if (dockerfilePath == null) {
        ILocationProvider locationProvider = (ILocationProvider) input
                .getAdapter(ILocationProvider.class);
        if (locationProvider != null) {
            dockerfilePath = locationProvider.getPath(input);
        }
    }
    if (dockerfilePath != null) {
        launch(dockerfile, dockerfilePath);
    }
}
 
Example 7
Source File: PythonLinkHelper.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {
    if (aSelection == null || aSelection.isEmpty()) {
        return;
    }

    Object firstElement = aSelection.getFirstElement();

    //if it is a python element, let's first get the actual object for finding the editor
    if (firstElement instanceof IWrappedResource) {
        IWrappedResource resource = (IWrappedResource) firstElement;
        firstElement = resource.getActualObject();
    }

    //and now, if it is really a file...
    if (firstElement instanceof IFile) {

        //ok, let's check if the active editor is already the selection, because although the findEditor(editorInput) method
        //may return an editor for the correct file, we may have multiple editors for the same file, and if the current
        //editor is already correct, we don't want to change it
        //@see bug: https://sourceforge.net/tracker/?func=detail&atid=577329&aid=2037682&group_id=85796
        IEditorPart activeEditor = aPage.getActiveEditor();
        if (activeEditor != null) {
            IEditorInput editorInput = activeEditor.getEditorInput();
            IFile currFile = (IFile) editorInput.getAdapter(IFile.class);
            if (currFile != null && currFile.equals(firstElement)) {
                return; //the current editor is already the active editor.
            }
        }

        //if we got here, the active editor is not a match, so, let's find one and show it.
        IEditorPart editor = null;
        IEditorInput fileInput = new FileEditorInput((IFile) firstElement);
        if ((editor = aPage.findEditor(fileInput)) != null) {
            aPage.bringToTop(editor);
        }
    }

}
 
Example 8
Source File: EditorUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean mustSaveDirtyEditor(IEditorPart ep, IEditorInput input, boolean saveUnknownEditors) {
	/*
	 * Goal: save all editors that could interfere with refactoring operations.
	 *
	 * Always save all editors for compilation units that are not working copies.
	 * (Leaving them dirty would cause problems, since the file buffer could have been
	 * modified but the Java model is not reconciled.)
	 *
	 * If <code>saveUnknownEditors</code> is <code>true</code>, save all editors
	 * whose implementation is probably not based on file buffers.
	 */
	IResource resource= (IResource) input.getAdapter(IResource.class);
	if (resource == null)
		return saveUnknownEditors;

	IJavaElement javaElement= JavaCore.create(resource);
	if (javaElement instanceof ICompilationUnit) {
		ICompilationUnit cu= (ICompilationUnit) javaElement;
		if (!cu.isWorkingCopy()) {
			return true;
		}
	}

	if (! (ep instanceof ITextEditor))
		return saveUnknownEditors;

	ITextEditor textEditor= (ITextEditor) ep;
	IDocumentProvider documentProvider= textEditor.getDocumentProvider();
	if (! (documentProvider instanceof TextFileDocumentProvider))
		return saveUnknownEditors;

	return false;
}
 
Example 9
Source File: AbstractGhidraLaunchShortcut.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
	IEditorInput input = editor.getEditorInput();
	IResource resource = input.getAdapter(IResource.class);
	if (resource != null) {
		launch(JavaCore.create(resource.getProject()), mode);
	}
}
 
Example 10
Source File: ImageViewer.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Load the image data from the current editor input. This operation can take time and should not be called on the
 * ui thread.
 */
void loadImageData() throws CoreException {
	final IEditorInput input = getEditorInput();
	final Object o = input.getAdapter(ImageData.class);
	if (o instanceof ImageData) {
		imageData = (ImageData) o;
	} else if (input instanceof IStorageEditorInput) {
		final IFile file = getFileFor(input);
		imageData = ImageDataLoader.getImageData(file);
	}
	// save this away so we don't compute it all the time
	this.maxZoomFactor = determineMaxZoomFactor();
}
 
Example 11
Source File: WorkbenchUtils.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tries to get IFile from editor input - i.e. the IFile which can be modified by this editor
 * @param input
 * @return
 */
public static IFile getIFileFrom(IEditorInput input) {
	if (input == null) {
		return null;
	}
	
	return (IFile) input.getAdapter(IFile.class);
}
 
Example 12
Source File: JavaUI.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the {@link ITypeRoot} wrapped by the given editor input.
 *
 * @param editorInput the editor input
 * @return the {@link ITypeRoot} wrapped by <code>editorInput</code> or <code>null</code> if the editor input
 * does not stand for a ITypeRoot
 *
 * @since 3.4
 */
public static ITypeRoot getEditorInputTypeRoot(IEditorInput editorInput) {
	// Performance: check working copy manager first: this is faster
	ICompilationUnit cu= JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
	if (cu != null)
		return cu;

	IJavaElement je= (IJavaElement) editorInput.getAdapter(IJavaElement.class);
	if (je instanceof ITypeRoot)
		return (ITypeRoot) je;

	return null;
}
 
Example 13
Source File: ProblemTickUpdater.java    From typescript.java with MIT License 5 votes vote down vote up
private IResource getEditorInputResource() {
	IEditorInput input = editor.getEditorInput();
	if (input == null) {
		return null;
	}
	return input.getAdapter(IResource.class);
}
 
Example 14
Source File: AbstractStatsView.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * *** * Copied from ResourceUtil.getFile() since ResourceUtil is only available since Eclipse 3.1
 * *** Returns the file corresponding to the given editor input, or <code>null</code> if there is
 * no applicable file. Returns <code>null</code> if the given editor input is <code>null</code>.
 *
 * @param editorInput
 *          the editor input, or <code>null</code>
 * @return the file corresponding to the editor input, or <code>null</code>
 */
public static IFile getFile(IEditorInput editorInput) {
  if (editorInput == null) {
    return null;
  }
  // Note: do not treat IFileEditorInput as a special case. Use the
  // adapter mechanism instead.
  // See Bug 87288 [IDE] [EditorMgmt] Should avoid explicit checks for
  // [I]FileEditorInput
  Object o = editorInput.getAdapter(IFile.class);
  if (o instanceof IFile) {
    return (IFile) o;
  }
  return null;
}
 
Example 15
Source File: LapseView.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
public static IOpenable getJavaInput(IEditorPart part) {
    IEditorInput editorInput= part.getEditorInput();
    if (editorInput != null) {
      IJavaElement input= (IJavaElement)editorInput.getAdapter(IJavaElement.class);
      if (input instanceof IOpenable) {
        return (IOpenable) input;
      }
    }
    return null;  
}
 
Example 16
Source File: XbaseEditorInputRedirector.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ITypeRoot getTypeRoot(IEditorInput editorInput) {
	IJavaElement adapter = editorInput.getAdapter(IJavaElement.class);
	return adapter instanceof ITypeRoot ? (ITypeRoot) adapter : null;
}
 
Example 17
Source File: ImportExternal.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	XLIFFEditorImplWithNatTable xliffEditor = null;
	final Shell shell = HandlerUtil.getActiveShell(event);
	String partId = HandlerUtil.getActivePartId(event);
	IFile file = null;
	IEditorPart editor = HandlerUtil.getActiveEditor(event);
	if (editor instanceof XLIFFEditorImplWithNatTable) {
		xliffEditor = (XLIFFEditorImplWithNatTable) editor;
	}
	if (partId.equals("net.heartsome.cat.common.ui.navigator.view")) {
		// 导航视图处于激活状态
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewPart viewPart = page.findView("net.heartsome.cat.common.ui.navigator.view");
		StructuredSelection selection = (StructuredSelection) viewPart.getSite().getSelectionProvider()
				.getSelection();
		if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
			List<?> lstObj = ((IStructuredSelection) selection).toList();
			ArrayList<IFile> lstXliff = new ArrayList<IFile>();
			for (Object obj : lstObj) {
				if (obj instanceof IFile) {
					IFile tempFile = (IFile) obj;
					// Linux 下的文本文件无扩展名,因此要先判断扩展名是否为空
					if (tempFile.getFileExtension() != null
							&& CommonFunction.validXlfExtension(tempFile.getFileExtension())) {
						lstXliff.add(tempFile);
					}
				}
			}
			if (lstXliff.size() > 1) {
				MessageDialog.openInformation(shell, Messages.getString("all.dialog.ok.title"),
						Messages.getString("ImportDocxHandler.msg1"));
				return null;
			}
			if (lstXliff.size() == 1) {
				file = lstXliff.get(0);
			}
		}
	} else if (partId.equals("net.heartsome.cat.ts.ui.xliffeditor.nattable.editor")) {
		// nattable 处于激活状态
		IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
		IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
		IFile iFile = (IFile) editorInput.getAdapter(IFile.class);

		if (xliffEditor.isMultiFile()) {
			MessageDialog.openInformation(shell, Messages.getString("all.dialog.ok.title"),
					Messages.getString("ImportDocxHandler.msg2"));
			return null;
		} else if (iFile.getFileExtension() != null && CommonFunction.validXlfExtension(iFile.getFileExtension())) {
			file = iFile;
		}
	}
	if (file != null) {
		XLFValidator.resetFlag();
		if (!XLFValidator.validateXliffFile(file)) {
			return null;
		}
		XLFValidator.resetFlag();
	}

	final ImportConfig config = new ImportConfig();
	config.setShell(shell);
	config.set_xliff(file == null ? "" : file.getFullPath().toOSString());
	config.setXliffEditor(xliffEditor);
	config.setXliffFile(file == null ? "" : ResourceUtils.iFileToOSPath(file));
	HsMultiActiveCellEditor.commit(true);
	ImportExternalDialog dialog = new ImportExternalDialog(shell, xliffEditor, config);
	if (Dialog.OK == dialog.open()) {
		config.doImport();
		if (xliffEditor != null) {
			// reopen if need
			if (xliffEditor.getXLFHandler().getVnMap().get(config.getXliffFile()) != null) {
				Map<String, Object> resultMap = xliffEditor.getXLFHandler().openFile(config.getXliffFile());
				if (resultMap == null
						|| Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap
								.get(Constant.RETURNVALUE_RESULT)) {
					// 打开文件失败。
					MessageDialog.openWarning(
							shell,
							Messages.getString("all.dialog.warning"),
							MessageFormat.format(Messages.getString("ImportDocxDialog.ok.parseError"),
									config.get_xliff()));
					LOGGER.error(MessageFormat.format(Messages.getString("ImportDocxDialog.ok.parseError"),
							config.get_xliff()));
					return null;
				}
				xliffEditor.reloadData();
				HsMultiCellEditorControl.activeSourceAndTargetCell(xliffEditor);
			}
		}
	}
	return null;
}
 
Example 18
Source File: AbstractExportHandler.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
public boolean initExportConfig(ExecutionEvent event) throws ExecutionException {
	config = new ExportConfig();
	Shell shell = HandlerUtil.getActiveShell(event);
	String partId = HandlerUtil.getActivePartId(event);

	if (partId.equals("net.heartsome.cat.common.ui.navigator.view")) {// 导航视图处于激活状态
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewPart viewPart = page.findView("net.heartsome.cat.common.ui.navigator.view");
		StructuredSelection selection = (StructuredSelection) viewPart.getSite().getSelectionProvider()
				.getSelection();
		if (selection != null && !selection.isEmpty()) {
			for (Object obj : selection.toList()) {
				if (obj instanceof IFile) {
					addXLFFile((IFile) obj);
				} else if (obj instanceof IFolder) {
					traversalFile((IFolder) obj);
				} else if (obj instanceof IProject) {
					IProject proj = (IProject) obj;
					traversalFile(proj.getFolder(XLF));
				}
			}
			if (config.getProjects() == null || config.getProjects().size() < 1) {
				MessageDialog.openInformation(shell, Messages.getString("all.dialog.ok.title"),
						Messages.getString("xlf2tmx.info.notfoundxlf"));
				return false;
			}
		}
	} else if (partId.equals("net.heartsome.cat.ts.ui.xliffeditor.nattable.editor")) {// nattable 处于激活状态
		IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
		IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
		IFile iFile = (IFile) editorInput.getAdapter(IFile.class);
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
		IXliffEditor xliffEditor = (IXliffEditor) editor;

		if (xliffEditor.isMultiFile()) {
			MessageDialog.openInformation(shell, Messages.getString("all.dialog.ok.title"),
					Messages.getString("ExportDocxHandler.msg2"));
			return false;
		} else if (iFile.getFileExtension() != null && CommonFunction.validXlfExtension(iFile.getFileExtension())) {
			addXLFFile(iFile);
		}
	}

	return true;
}
 
Example 19
Source File: ExportRTFHandler.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException {
	String tshelp = System.getProperties().getProperty("TSHelp");
	String tsstate = System.getProperties().getProperty("TSState");
	if (tshelp == null || !"true".equals(tshelp) || tsstate == null || !"true".equals(tsstate)) {
		LoggerFactory.getLogger(ExportRTFHandler.class).error("Exception:key hs008 is lost.(Can't find the key)");
		System.exit(0);
	}
	Shell shell = HandlerUtil.getActiveShell(event);
	String partId = HandlerUtil.getActivePartId(event);
	IFile file = null;
	if (partId.equals("net.heartsome.cat.common.ui.navigator.view")) {
		// 导航视图处于激活状态
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewPart viewPart = page.findView("net.heartsome.cat.common.ui.navigator.view");
		StructuredSelection selection = (StructuredSelection) viewPart.getSite().getSelectionProvider()
				.getSelection();
		// ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
			List<?> lstObj = ((IStructuredSelection) selection).toList();
			ArrayList<IFile> lstXliff = new ArrayList<IFile>();
			for (Object obj : lstObj) {
				if (obj instanceof IFile) {
					IFile tempFile = (IFile) obj;
					// Linux 下的文本文件无扩展名,因此要先判断扩展名是否为空
					if (tempFile.getFileExtension() != null && CommonFunction.validXlfExtension(tempFile.getFileExtension())) {
						lstXliff.add(tempFile);
					}
				}
			}
			if (lstXliff.size() > 1) {
				MessageDialog.openInformation(shell, Messages.getString("handler.ExportRTFHandler.msg.title"),
						Messages.getString("handler.ExportRTFHandler.msg1"));
				return null;
			}
			if (lstXliff.size() == 1) {
				file = lstXliff.get(0);
			}
		}
	} else if (partId.equals("net.heartsome.cat.ts.ui.xliffeditor.nattable.editor")) {
		// nattable 处于激活状态
		IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
		IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
		IFile iFile = (IFile) editorInput.getAdapter(IFile.class);
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
		IXliffEditor xliffEditor = (IXliffEditor) editor;

		if (xliffEditor.isMultiFile()) {
			MessageDialog.openInformation(shell, Messages.getString("handler.ExportRTFHandler.msg.title"),
					Messages.getString("handler.ExportRTFHandler.msg2"));
			return null;
		} else if (iFile.getFileExtension() != null && CommonFunction.validXlfExtension(iFile.getFileExtension())) {
			file = iFile;
		}
	}

	if (file != null) {
		XLFValidator.resetFlag();
		if (!XLFValidator.validateXliffFile(file)) {
			return null;
		}
		XLFValidator.resetFlag();
	}

	ExportRTFDilaog dialog = new ExportRTFDilaog(shell, file == null ? "" : file.getFullPath().toOSString(),
			file == null ? "" : ResourceUtils.iFileToOSPath(file));
	dialog.open();
	return null;
}
 
Example 20
Source File: EditorUtils.java    From typescript.java with MIT License 4 votes vote down vote up
public static IResource getResource(IEditorInput input) {
	return (IResource) input.getAdapter(IResource.class);
}