diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/internal/example/collab/EditorCompoundContributionItem.java | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/internal/example/collab/EditorCompoundContributionItem.java b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/internal/example/collab/EditorCompoundContributionItem.java index e02523722..19a389933 100644 --- a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/internal/example/collab/EditorCompoundContributionItem.java +++ b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/internal/example/collab/EditorCompoundContributionItem.java @@ -12,15 +12,21 @@ import org.eclipse.jface.action.Separator; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchPartSite; +import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.CompoundContributionItem; import org.eclipse.ui.part.FileEditorInput; -import org.eclipse.ui.texteditor.ITextEditor; public class EditorCompoundContributionItem extends CompoundContributionItem { + private static final IContributionItem[] EMPTY = new IContributionItem[] {}; + public EditorCompoundContributionItem() { } @@ -28,8 +34,8 @@ public class EditorCompoundContributionItem extends CompoundContributionItem { super(id); } - protected IFile getFileForPart(ITextEditor editor) { - final IEditorInput input = editor.getEditorInput(); + protected IFile getFileForPart(IEditorPart editorPart) { + final IEditorInput input = editorPart.getEditorInput(); if (input instanceof FileEditorInput) { final FileEditorInput fei = (FileEditorInput) input; return fei.getFile(); @@ -37,10 +43,6 @@ public class EditorCompoundContributionItem extends CompoundContributionItem { return null; } - protected IWorkbench getWorkbench() { - return PlatformUI.getWorkbench(); - } - protected ClientEntry isConnected(IResource res) { if (res == null) return null; @@ -49,31 +51,59 @@ public class EditorCompoundContributionItem extends CompoundContributionItem { return entry; } - protected IContributionItem[] getContributionItems() { - final ITextEditor editor = (ITextEditor) getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); - if (editor == null) + protected IEditorPart getEditorPart() { + final IWorkbench workbench = PlatformUI.getWorkbench(); + if (workbench == null) return null; - final ISelection s = editor.getSelectionProvider().getSelection(); - final ITextSelection textSelection = (s instanceof ITextSelection) ? ((ITextSelection) s) : null; - if (textSelection == null) + final IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow(); + if (ww == null) return null; - final IFile file = getFileForPart(editor); - if (file == null) + final IWorkbenchPage wp = ww.getActivePage(); + if (wp == null) + return null; + return wp.getActiveEditor(); + } + + protected ITextSelection getSelection() { + final IEditorPart ep = getEditorPart(); + if (ep == null) return null; + final IWorkbenchPartSite ws = ep.getEditorSite(); + if (ws == null) + return null; + final ISelectionProvider sp = ws.getSelectionProvider(); + if (sp == null) + return null; + final ISelection sel = sp.getSelection(); + if (sel == null || !(sel instanceof ITextSelection)) + return null; + return (ITextSelection) sel; + } + + protected IContributionItem[] getContributionItems() { + final ITextSelection textSelection = getSelection(); + if (textSelection == null) + return EMPTY; + final IEditorPart editorPart = getEditorPart(); + if (editorPart == null) + return EMPTY; + final IFile file = getFileForPart(editorPart); + if (file == null) + return EMPTY; final IProject project = file.getProject(); if (isConnected(project.getWorkspace().getRoot()) == null) - return null; + return EMPTY; final IAction action = new Action() { public void run() { final ClientEntry entry = isConnected(project.getWorkspace().getRoot()); if (entry == null) { - MessageDialog.openInformation(getWorkbench().getDisplay().getActiveShell(), Messages.EditorCompoundContributionItem_EXCEPTION_NOT_CONNECTED_TITLE, Messages.EditorCompoundContributionItem_EXCEPTION_NOT_CONNECTED_MESSAGE); + MessageDialog.openInformation(PlatformUI.getWorkbench().getDisplay().getActiveShell(), Messages.EditorCompoundContributionItem_EXCEPTION_NOT_CONNECTED_TITLE, Messages.EditorCompoundContributionItem_EXCEPTION_NOT_CONNECTED_MESSAGE); return; } final EclipseCollabSharedObject collabsharedobject = entry.getSharedObject(); if (collabsharedobject != null) { - collabsharedobject.sendOpenAndSelectForFile(null, project.getName() + "/" + file.getProjectRelativePath().toString(), textSelection.getOffset(), textSelection.getLength()); //$NON-NLS-1$ + collabsharedobject.sendOpenAndSelectForFile(null, project.getName() + "/" + file.getProjectRelativePath().toString(), textSelection.getOffset(), textSelection.getLength()); } } }; |