下面列出了org.eclipse.ui.IEditorPart#getAdapter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns {@link IDocument} in the open editor, or null if the editor
* is not open.
*/
static IDocument getCurrentDocument(IFile file) {
try {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
IEditorPart editorPart = ResourceUtil.findEditor(activePage, file);
if (editorPart != null) {
IDocument document = editorPart.getAdapter(IDocument.class);
return document;
}
return null;
} catch (IllegalStateException ex) {
//If workbench does not exist
return null;
}
}
public static void findPrevious(IEditorPart editorPart) {
IFindReplaceTarget target = (IFindReplaceTarget) editorPart.getAdapter(IFindReplaceTarget.class);
if (target == null)
return;
StatusLine statusLine = new StatusLine(editorPart.getEditorSite());
statusLine.cleanStatusLine();
SearchRegion region = getSearchRegion(target, editorPart);
if (region == null)
return;
int offset = Math.max(region.offset - 1, 0);
offset = findAndSelect(target, offset, region.text, false);
if (offset < 0) {
String message = String.format(Messages.QuickFind_Status_FirstOccurence, region.text);
statusLine.showMessage(message, ImageUtils.getImage(ImageUtils.FIND_STATUS));
}
}
@Override
public boolean isEnabled() {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor() ;
if(editor != null){
final IOperationHistory history = (IOperationHistory) editor.getAdapter(IOperationHistory.class);
final IUndoContext context = (IUndoContext) editor.getAdapter(IUndoContext.class);
if(history != null && context != null){
final IUndoableOperation[] undoHistory = history.getUndoHistory(context);
if (undoHistory != null && undoHistory.length != 0) {
final IUndoableOperation ctxt = undoHistory[undoHistory.length - 1];
final String ctxtLabel = ctxt.getLabel();
if(ctxtLabel != null && ctxtLabel.contains("Lane")){//Avoid Exception on undo //$NON-NLS-1$
return false ;
} else {
return ctxt.canUndo();
}
}
} else {
return false;
}
}
return false;
}
public void open()
{
try
{
final IFileStore store = EFS.getStore(document);
// Now open an editor to this file (and highlight the occurrence if possible)
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = IDE.openEditorOnFileStore(page, store);
// Now select the occurrence if we can
IFindReplaceTarget target = (IFindReplaceTarget) part.getAdapter(IFindReplaceTarget.class);
if (target != null && target.canPerformFind())
{
target.findAndSelect(0, searchString, true, caseSensitive, wholeWord);
}
}
catch (Exception e)
{
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
}
/**
* Obtains the source viewer of an editor.
* @param editor the editor
* @return the source viewer of the editor
*/
public static ISourceViewer getSourceViewer(IEditorPart editor) {
if (editor == null) {
return null;
}
ISourceViewer viewer = (ISourceViewer)editor.getAdapter(ITextOperationTarget.class);
return viewer;
}
public static TMPresentationReconciler getTMPresentationReconciler(IEditorPart editorPart) {
if (editorPart == null) {
return null;
}
@Nullable ITextOperationTarget target = editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = ((ITextViewer) target);
return TMPresentationReconciler.getTMPresentationReconciler(textViewer);
}
return null;
}
private void installViewer() {
if (viewer == null) {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = page.getActiveEditor();
viewer = editorPart.getAdapter(ITextViewer.class);
}
}
static ITextViewer getViewer(IFile file) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
IEditorPart editorPart = ResourceUtil.findEditor(activePage, file);
ITextOperationTarget target = editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
return (ITextViewer) target;
}
return null;
}
@Override
public void launch(IEditorPart editor, String mode) {
final SarlJavaElementDelegateMainLaunch javaElementDelegate = editor.getAdapter(SarlJavaElementDelegateMainLaunch.class);
if (javaElementDelegate != null) {
super.launch(new StructuredSelection(javaElementDelegate), mode);
} else {
super.launch(editor, mode);
}
}
public static IModelEventManager getModelEventManager( )
{
IEditorPart input = null;
for ( int i = 0; i < EDITOR_IDS.length; i++ )
{
input = getActiveEditor( EDITOR_IDS[i] );
if ( input != null )
{
break;
}
}
if ( input == null )
{
IEditorPart part = getActiveEditor( true );
if ( part instanceof IReportEditor )
{
input = part;
}
}
if ( input == null )
{
return null;
}
Object adapter = input.getAdapter( IModelEventManager.class );
if ( adapter instanceof IModelEventManager )
{
return (IModelEventManager) adapter;
}
return null;
}
/**
* @return the active structured text viewer, or null
*/
public static StructuredTextViewer getActiveTextViewer() {
// Need to get the workbench window from the UI thread
final IWorkbenchWindow[] windowHolder = new IWorkbenchWindow[1];
Display.getDefault().syncExec(new Runnable() {
public void run() {
windowHolder[0] = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
});
IWorkbenchWindow window = windowHolder[0];
if (window == null) {
return null;
}
IWorkbenchPage page = window.getActivePage();
if (page == null) {
return null;
}
IEditorPart editor = page.getActiveEditor();
if (editor == null) {
return null;
}
/*
* TODO: change the following to use AdapterUtilities.getAdapter()
* and either a) have GWTD register an adapter factory or b) add a direct
* IAdaptable.getAdapter() call to AdapterUtilities.getAdapter().
*/
StructuredTextEditor structuredEditor = (StructuredTextEditor) editor.getAdapter(StructuredTextEditor.class);
if (structuredEditor == null) {
return null;
}
return structuredEditor.getTextViewer();
}
private static void findAndLogDocumentProviderIssues(final IEditorPart editorPart) {
final IDocumentProvider defaultDocumentProvider =
EditorAPI.getDocumentProvider(editorPart.getEditorInput());
if (!(defaultDocumentProvider instanceof TextFileDocumentProvider)) {
log.warn(
"The default document provider "
+ defaultDocumentProvider
+ " for editor with title '"
+ editorPart.getTitle()
+ "' might not support shared access. It is likely that the editor content is not properly synchronized!");
return;
}
final ITextEditor textEditor = editorPart.getAdapter(ITextEditor.class);
if (textEditor == null) return;
final IDocumentProvider editorDocumentProvider = textEditor.getDocumentProvider();
if (!(editorDocumentProvider instanceof TextFileDocumentProvider)) {
log.warn(
"The document provider "
+ editorDocumentProvider
+ " for editor with title '"
+ editorPart.getTitle()
+ "' might not support shared access. It is likely that the editor content is not properly synchronized!");
}
}
private int retrieveLimit(SWTBotEditor botEditor) {
IEditorPart editor = botEditor.getReference().getEditor(false);
IOperationHistory history = editor.getAdapter(IOperationHistory.class);
IUndoContext context = editor.getAdapter(IUndoContext.class);
if (history != null && context != null) {
return history.getLimit(context);
}
return -1;
}
protected boolean shouldInstall(IEditorPart part) {
if (part instanceof StatechartDiagramEditor) {
issueStore = (IValidationIssueStore) part.getAdapter(IValidationIssueStore.class);
return true;
}
// This is required for OffscreenEditPartFactory to render problem markers into
// the preview image
if (part == null) {
issueStore = new IValidationIssueStore.NullImpl();
return true;
}
return false;
}
@Override
protected void createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
BaseSelectionAction action = new OpenInNewEditor(this) {
@Override
public void run() {
if (getSelectedObjects().size() > 0) {
Object o = getSelectedObjects().get(0);
// should make this generic and be able to get the owner from a facade object
if (o instanceof WSDLBaseAdapter) {
WSDLBaseAdapter baseAdapter = (WSDLBaseAdapter) o;
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor();
Object object = editorPart.getAdapter(org.eclipse.wst.wsdl.Definition.class);
if (object instanceof org.eclipse.wst.wsdl.Definition) {
EObject eObject = (EObject) baseAdapter.getTarget();
OpenOnSelectionHelper openHelper = new OpenOnSelectionHelper(
(org.eclipse.wst.wsdl.Definition) object);
openHelper.openEditor(eObject);
}
}
}
}
};
action.setSelectionProvider(getSelectionManager());
registry.registerAction(action);
}
/**
* {@inheritDoc}
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
ITextSelection selection = WorkbenchUtils.getActiveTextSelection();
IDocument doc = WorkbenchUtils.getActiveDocument();
IEditorInput input = WorkbenchUtils.getActiveInput();
IEditorPart editor = WorkbenchUtils.getActiveEditor(false);
boolean isTextOperationAllowed = (selection != null) && (doc != null)
&& (input != null) && (editor != null)
&& (editor instanceof ModulaEditor);
if (isTextOperationAllowed) {
ITextEditor iTextEditor = (ITextEditor)editor;
final ITextOperationTarget operationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
String commentPrefix = ((SourceCodeTextEditor)editor).getEOLCommentPrefix();
isTextOperationAllowed = (operationTarget != null)
&& (operationTarget instanceof TextViewer)
&& (validateEditorInputState(iTextEditor))
&& (commentPrefix != null);
if ((isTextOperationAllowed)) {
int startLine = selection.getStartLine();
int endLine = selection.getEndLine();
int selOffset = selection.getOffset();
int selLen = selection.getLength();
int realEndLine = doc.getLineOfOffset(selOffset + selLen); // for selection end at pos=0 (endLine is line before here)
// Are cursor and anchor at 0 positions?
boolean is0pos = false;
if (doc.getLineOffset(startLine) == selOffset) {
if ((doc.getLineOffset(endLine) + doc.getLineLength(endLine) == selOffset + selLen)) {
is0pos = true;
}
}
ArrayList<ReplaceEdit> edits = null;
int offsAfter[] = {0};
if (is0pos || selLen == 0) {
edits = commentWholeLines(startLine, (selLen == 0) ? startLine : endLine, realEndLine, doc, offsAfter);
} else {
edits = commentRange(selOffset, selLen, "(*", "*)", offsAfter); //$NON-NLS-1$ //$NON-NLS-2$
}
if (edits != null && !edits.isEmpty()) {
DocumentRewriteSession drws = null;
try {
if (doc instanceof IDocumentExtension4) {
drws = ((IDocumentExtension4)doc).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
}
MultiTextEdit edit= new MultiTextEdit(0, doc.getLength());
edit.addChildren((TextEdit[]) edits.toArray(new TextEdit[edits.size()]));
edit.apply(doc, TextEdit.CREATE_UNDO);
iTextEditor.getSelectionProvider().setSelection(new TextSelection(offsAfter[0], 0));
}
finally {
if (doc instanceof IDocumentExtension4 && drws != null) {
((IDocumentExtension4)doc).stopRewriteSession(drws);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public XFindPanel(final Composite parent, IEditorPart editorPart) {
super(parent, SWT.NONE);
statusLine = new StatusLine(editorPart.getEditorSite());
setVisible(false);
if (editorPart != null) {
target = (IFindReplaceTarget) editorPart.getAdapter(IFindReplaceTarget.class);
isRegExSupported = (target instanceof IFindReplaceTargetExtension3);
}
final IPreferenceStore store = XFindPlugin.getDefault().getPreferenceStore();
if (!store.contains(XFIND_PANEL_PLACEMENT)) {
store.setDefault(XFIND_PANEL_PLACEMENT, XFIND_PANEL_PLACEMENT_TOP);
}
if (store.getInt(XFIND_PANEL_PLACEMENT) == XFIND_PANEL_PLACEMENT_BOTTOM) {
moveBelow(null);
} else {
moveAbove(null);
}
createContents();
loadHistory(store);
store.addPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(final PropertyChangeEvent event) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (XFIND_PANEL_PLACEMENT.equals(event.getProperty())) {
if (store.getInt(XFIND_PANEL_PLACEMENT) == XFIND_PANEL_PLACEMENT_BOTTOM) {
moveBelow(null);
} else {
moveAbove(null);
}
parent.layout();
}
}
});
}
});
parent.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
saveHistory(store);
}
});
}
@Test
public void testHDF5FileMonitoring() throws Exception {
try {
testIsRunning = true;
final File h5File = startHDF5WritingThread(); // This is an HDF5 file which is growing as a thread writes it.
Thread.sleep(2000);
IRemoteDatasetService service = new RemoteDatasetServiceImpl();
final IDatasetConnector data = service.createRemoteDataset("localhost", 8080);
data.setPath(h5File.getAbsolutePath());
data.setDatasetName("/entry/data/image"); // We just get the first image in the PNG file.
data.connect();
try {
// We open a random part then
// 1. copy in the remote dataset which we are currently writing to
// 2. plot data from it
IEditorPart editor = TestUtils.openExternalEditor(testDir+"/export.h5");
ITransferableDataManager man = (ITransferableDataManager)editor.getAdapter(ITransferableDataManager.class);
TestUtils.delay(1000); // Wait while plot sorts itself out
TransferableLazyDataset trans = new TransferableLazyDataset(data.getDataset());
trans.setChecked(true);
man.addData(trans);
final ISliceSystem system = (ISliceSystem)editor.getAdapter(ISliceSystem.class);
for (int i = 0; i < 20; i++) {
TestUtils.delay(1000);
// We set the slice to the current end, so that the image refreshes.
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
system.setSliceIndex(0, data.getDataset().getShape()[0]-1, true);
}
});
}
} finally {
data.disconnect();
}
} finally {
testIsRunning = false;
}
}
/**
* Gets a {@link ITextViewer} instance for the given editor part.
*
* @param editorPart for which we want a {@link TextViewer}.
* @return {@link ITextViewer} or <code>null</code> if there is no {@link TextViewer} for the
* editorPart.
*/
public static ITextViewer getViewer(IEditorPart editorPart) {
Object viewer = editorPart.getAdapter(ITextOperationTarget.class);
return (viewer instanceof ITextViewer) ? (ITextViewer) viewer : null;
}
/**
* Opens the quick type hierarchy for the given editor.
*
* @param editor the editor for which to open the quick hierarchy
*/
private static void openQuickHierarchy(IEditorPart editor) {
ITextOperationTarget textOperationTarget= (ITextOperationTarget)editor.getAdapter(ITextOperationTarget.class);
textOperationTarget.doOperation(JavaSourceViewer.SHOW_HIERARCHY);
}