### Eclipse Workspace Patch 1.0 #P org.eclipse.team.ui Index: .settings/.api_filters =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/.settings/.api_filters,v retrieving revision 1.7 diff -u -r1.7 .api_filters --- .settings/.api_filters 30 Sep 2009 11:54:27 -0000 1.7 +++ .settings/.api_filters 1 Oct 2009 11:22:15 -0000 @@ -27,6 +27,16 @@ + + + + + + + + + + Index: plugin.xml =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/plugin.xml,v retrieving revision 1.201 diff -u -r1.201 plugin.xml --- plugin.xml 15 Sep 2009 08:59:45 -0000 1.201 +++ plugin.xml 1 Oct 2009 11:22:15 -0000 @@ -293,6 +293,9 @@ + + Index: src/org/eclipse/team/internal/ui/TeamUIMessages.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/TeamUIMessages.java,v retrieving revision 1.83 diff -u -r1.83 TeamUIMessages.java --- src/org/eclipse/team/internal/ui/TeamUIMessages.java 26 Mar 2009 10:03:22 -0000 1.83 +++ src/org/eclipse/team/internal/ui/TeamUIMessages.java 1 Oct 2009 11:22:15 -0000 @@ -361,6 +361,9 @@ public static String SynchronizeView_14; public static String SynchronizeView_16; public static String SynchronizeView_statusLine; + public static String SynchronizeView_linkWithEditor; + public static String SynchronizeView_linkWithEditorDescription; + public static String SynchronizeView_linkWithEditorTooltip; public static String StatisticsPanel_outgoing; public static String StatisticsPanel_incoming; Index: src/org/eclipse/team/internal/ui/messages.properties =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/messages.properties,v retrieving revision 1.251 diff -u -r1.251 messages.properties --- src/org/eclipse/team/internal/ui/messages.properties 26 Mar 2009 10:03:22 -0000 1.251 +++ src/org/eclipse/team/internal/ui/messages.properties 1 Oct 2009 11:22:16 -0000 @@ -162,6 +162,9 @@ SynchronizeView_14=Error opening perspective SynchronizeView_16=Error opening view SynchronizeView_statusLine={0} [{1}] +SynchronizeView_LinkWithEditor=Link with Editor +SynchronizeView_LinkWithEditorDescription=Link with active editor +SynchronizeView_LinkWithEditorTooltip=Link with Editor and Selection StatisticsPanel_outgoing=Outgoing StatisticsPanel_conflicting=Conflicting Index: src/org/eclipse/team/internal/ui/synchronize/SynchronizeView.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SynchronizeView.java,v retrieving revision 1.47 diff -u -r1.47 SynchronizeView.java --- src/org/eclipse/team/internal/ui/synchronize/SynchronizeView.java 6 Mar 2009 15:09:40 -0000 1.47 +++ src/org/eclipse/team/internal/ui/synchronize/SynchronizeView.java 1 Oct 2009 11:22:16 -0000 @@ -12,13 +12,15 @@ import java.util.*; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.*; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.dialogs.*; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; -import org.eclipse.jface.viewers.IBasicPropertyConstants; +import org.eclipse.jface.viewers.*; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; @@ -29,13 +31,14 @@ import org.eclipse.team.ui.synchronize.*; import org.eclipse.ui.*; import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.ide.IDE; import org.eclipse.ui.part.*; import org.eclipse.ui.progress.IWorkbenchSiteProgressService; /** * Implements a Synchronize View that contains multiple synchronize participants. */ -public class SynchronizeView extends PageBookView implements ISynchronizeView, ISynchronizeParticipantListener, IPropertyChangeListener, ISaveablesSource, ISaveablePart { +public class SynchronizeView extends PageBookView implements ISynchronizeView, ISynchronizeParticipantListener, IPropertyChangeListener, ISaveablesSource, ISaveablePart, IShowInTarget { /** * Suggested maximum length of participant names when shown in certain menus and dialog. @@ -77,11 +80,16 @@ */ private RemoveSynchronizeParticipantAction fRemoveAllAction; + private ToggleLinkingAction fToggleLinkingAction; + private boolean fLinkingEnabled; + private OpenAndLinkWithEditorHelper fOpenAndLinkWithEditorHelper; + /** * Preference key to save */ private static final String KEY_LAST_ACTIVE_PARTICIPANT_ID = "lastactiveparticipant_id"; //$NON-NLS-1$ private static final String KEY_LAST_ACTIVE_PARTICIPANT_SECONDARY_ID = "lastactiveparticipant_sec_id"; //$NON-NLS-1$ + private static final String KEY_LINK_WITH_EDITOR = "linkWithEditor"; //$NON-NLS-1$ private static final String KEY_SETTINGS_SECTION= "SynchronizeViewSettings"; //$NON-NLS-1$ @@ -232,6 +240,7 @@ if (page instanceof ISynchronizePage) { try { ((ISynchronizePage)page).init(configuration.getSite()); + } catch (PartInitException e) { TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e); } @@ -356,6 +365,7 @@ protected void createActions() { fPageDropDown = new SynchronizePageDropDownAction(this); fPinAction = new PinParticipantAction(); + fToggleLinkingAction = new ToggleLinkingAction(this); fRemoveCurrentAction = new RemoveSynchronizeParticipantAction(this, false); fRemoveAllAction = new RemoveSynchronizeParticipantAction(this, true); updateActionEnablements(); @@ -382,8 +392,10 @@ IToolBarManager mgr = bars.getToolBarManager(); mgr.add(fPageDropDown); mgr.add(fPinAction); + mgr.add(fToggleLinkingAction); IMenuManager menu = bars.getMenuManager(); menu.add(fPinAction); + menu.add(fToggleLinkingAction); menu.add(fRemoveCurrentAction); menu.add(fRemoveAllAction); } @@ -396,6 +408,33 @@ if (part != null) { partActivated(part); fPageDropDown.update(); + //TODO: is this the best place? + IPage page = getPage(part.getParticipant()); + if (page instanceof ISynchronizePage) { + Viewer viewer = ((ISynchronizePage)page).getViewer(); + fOpenAndLinkWithEditorHelper= new OpenAndLinkWithEditorHelper((StructuredViewer) viewer) { + protected void activate(ISelection selection) { + try { + final Object selectedElement= /*SelectionUtil.*/getSingleElement(selection); + if (/*EditorUtility.*/isOpenInEditor(selectedElement) != null) + if (selectedElement instanceof IFile) + /*EditorUtility.*/openInEditor((IFile) selectedElement, true); + } catch (PartInitException ex) { + // ignore if no editor input can be found + } + } + + protected void linkToEditor(ISelection selection) { + SynchronizeView.this.linkToEditor(selection); + } + + protected void open(ISelection selection, boolean activate) { + //TODO: + // fActionSet.handleOpen(selection, activate); + } + + }; + } rememberCurrentParticipant(); PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook().getParent(), participant.getHelpContextId()); } @@ -437,6 +476,7 @@ */ public void createPartControl(Composite parent) { super.createPartControl(parent); + restoreLinkingEnabled(); createActions(); configureToolBar(getViewSite().getActionBars()); updateForExistingParticipants(); @@ -683,4 +723,259 @@ new String[] { description, syncMode })); } } + + private IPartListener2 fLinkWithEditorListener= new IPartListener2() { + public void partVisible(IWorkbenchPartReference partRef) {} + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + public void partClosed(IWorkbenchPartReference partRef) {} + public void partDeactivated(IWorkbenchPartReference partRef) {} + public void partHidden(IWorkbenchPartReference partRef) {} + public void partOpened(IWorkbenchPartReference partRef) {} + public void partInputChanged(IWorkbenchPartReference partRef) { + if (partRef instanceof IEditorReference) { + editorActivated(((IEditorReference) partRef).getEditor(true)); + } + } + + public void partActivated(IWorkbenchPartReference partRef) { + if (partRef instanceof IEditorReference) { + editorActivated(((IEditorReference) partRef).getEditor(true)); + } + } + + }; + + public boolean isLinkingEnabled() { + return fLinkingEnabled; + } + + public void setLinkingEnabled(boolean enabled) { + fLinkingEnabled= enabled; + IDialogSettings dialogSettings = getDialogSettings(); + dialogSettings.put(KEY_LINK_WITH_EDITOR, fLinkingEnabled); + + IWorkbenchPage page= getSite().getPage(); + if (enabled) { + page.addPartListener(fLinkWithEditorListener); + + IEditorPart editor = page.getActiveEditor(); + if (editor != null) + editorActivated(editor); + } else { + page.removePartListener(fLinkWithEditorListener); + } + fOpenAndLinkWithEditorHelper.setLinkWithEditor(enabled); + + } + + // from **************************** PackageExplorer + private void restoreLinkingEnabled() { + fLinkingEnabled = getDialogSettings().getBoolean(KEY_LINK_WITH_EDITOR); + } + + /** + * Links to editor (if option enabled) + * @param selection the selection + */ + private void linkToEditor(ISelection selection) { + Object obj= /*SelectionUtil.*/getSingleElement(selection); + if (obj != null) { + IEditorPart part= isOpenInEditor(obj); + if (part != null) { + IWorkbenchPage page= getSite().getPage(); + page.bringToTop(part); +// if (obj instanceof IJavaElement) +// EditorUtility.revealInEditor(part, (IJavaElement)obj); + } + } + } + + /** + * An editor has been activated. Set the selection in this Packages Viewer + * to be the editor's input, if linking is enabled. + * @param editor the activated editor + */ + private void editorActivated(IEditorPart editor) { + if (!isLinkingEnabled()) + return; + + IEditorInput editorInput= editor.getEditorInput(); + if (editorInput == null) + return; + Object input= getInputFromEditor(editorInput); + if (input == null) + return; + if (!inputIsSelected(editorInput)) + showInput(input); + else + getViewer().getTree().showSelection(); + } + + boolean showInput(Object input) { + Object element= null; + + // TODO: how to select elements in model-enabled participant viewers? +// if (input instanceof IFile && isOnClassPath((IFile)input)) { +// element= JavaCore.create((IFile)input); +// } + + if (element == null) // try a non Java resource + element= input; + + if (element != null) { + ISelection newSelection= new StructuredSelection(element); + if (getViewer().getSelection().equals(newSelection)) { + getViewer().reveal(element); + } else { + getViewer().setSelection(newSelection, true); + + while (element != null && getViewer().getSelection().isEmpty()) { + // Try to select parent in case element is filtered + element= getParent(element); + if (element != null) { + newSelection= new StructuredSelection(element); + getViewer().setSelection(newSelection, true); + } + } + } + return true; + } + return false; + } + + /** + * Returns the element's parent. + * @param element the element + * + * @return the parent or null if there's no parent + */ + private Object getParent(Object element) { + /*if (element instanceof IJavaElement) + return ((IJavaElement)element).getParent(); + else*/ + if (element instanceof IResource) + return ((IResource)element).getParent(); + // else if (element instanceof IJarEntryResource) { + // return ((IJarEntryResource)element).getParent(); + // } + return null; + } + + + private TreeViewer getViewer() { + IPage currentPage = getCurrentPage(); + if (currentPage instanceof ISynchronizePage) { + return (TreeViewer) ((ISynchronizePage)currentPage).getViewer(); + } + // TODO: nobody's expecting null! + return null; + } + + private boolean inputIsSelected(IEditorInput input) { +// IStructuredSelection selection= (IStructuredSelection)fViewer.getSelection(); + IStructuredSelection selection= (IStructuredSelection) getViewer().getSelection(); + if (selection.size() != 1) + return false; + + IEditorInput selectionAsInput= /*EditorUtility.*/getEditorInput(selection.getFirstElement()); + return input.equals(selectionAsInput); + } + + private static IEditorInput getEditorInput(Object input) { +// if (input instanceof IJavaElement) +// return getEditorInput((IJavaElement) input); + //TODO: what about other inputs, from non-workspace model providers? + + if (input instanceof IFile) + return new FileEditorInput((IFile) input); + +// if (JavaModelUtil.isOpenableStorage(input)) +// return new JarEntryEditorInput((IStorage)input); + + return null; + } + + + private Object getInputFromEditor(IEditorInput editorInput) { + Object input = null; +// Object input= JavaUI.getEditorInputJavaElement(editorInput); +// if (input instanceof ICompilationUnit) { +// ICompilationUnit cu= (ICompilationUnit) input; +// if (!cu.getJavaProject().isOnClasspath(cu)) { // test needed for Java files in non-source folders (bug 207839) +// input= cu.getResource(); +// } +// } + if (input == null) { + input= editorInput.getAdapter(IFile.class); + } + if (input == null && editorInput instanceof IStorageEditorInput) { + try { + input= ((IStorageEditorInput) editorInput).getStorage(); + } catch (CoreException e) { + // ignore + } + } + return input; + } + + // **************************** from EditorUtility + + private static IEditorPart isOpenInEditor(Object inputElement) { + IEditorPart editor= findEditor(inputElement, false); + if (editor != null) + return editor; + + IEditorInput input= getEditorInput(inputElement); + + if (input != null) { + IWorkbenchPage p= TeamUIPlugin.getActivePage(); + if (p != null) { + return p.findEditor(input); + } + } + return null; + } + + private static IEditorPart findEditor(Object inputElement, boolean activate) { + //TODO: check compare editors + return null; + } + + private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException { + //TODO: enable these checks +// if (file == null) +// throwPartInitException(JavaEditorMessages.EditorUtility_file_must_not_be_null); + + IWorkbenchPage p= TeamUIPlugin.getActivePage(); +// if (p == null) +// throwPartInitException(JavaEditorMessages.EditorUtility_no_active_WorkbenchPage); + + IEditorPart editorPart = IDE.openEditor(p, file, activate); +// initializeHighlightRange(editorPart); + return editorPart; + } + + //from **************************** SelectionUtil + /** + * Returns the selected element if the selection consists of a single + * element only. + * + * @param s the selection + * @return the selected first element or null + */ + private static Object getSingleElement(ISelection s) { + if (! (s instanceof IStructuredSelection)) + return null; + IStructuredSelection selection= (IStructuredSelection) s; + if (selection.size() != 1) + return null; + + return selection.getFirstElement(); + } + + public boolean show(ShowInContext context) { + // TODO Show In, to implement + return false; + } + } Index: src/org/eclipse/team/internal/ui/synchronize/actions/ToggleLinkingAction.java =================================================================== RCS file: src/org/eclipse/team/internal/ui/synchronize/actions/ToggleLinkingAction.java diff -N src/org/eclipse/team/internal/ui/synchronize/actions/ToggleLinkingAction.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/team/internal/ui/synchronize/actions/ToggleLinkingAction.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2009 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.team.internal.ui.synchronize.actions; + +import org.eclipse.jface.action.Action; +import org.eclipse.team.internal.ui.*; +import org.eclipse.team.ui.synchronize.ISynchronizeView; + +public class ToggleLinkingAction extends Action { + + private ISynchronizeView view; + + public ToggleLinkingAction(ISynchronizeView view) { + super(TeamUIMessages.SynchronizeView_linkWithEditor); + setDescription(TeamUIMessages.SynchronizeView_linkWithEditorDescription); + setToolTipText(TeamUIMessages.SynchronizeView_linkWithEditorTooltip); + setImageDescriptor(TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_LINK_WITH)); + this.view = view; + setChecked(view.isLinkingEnabled()); + } + + public void run() { + view.setLinkingEnabled(isChecked()); + } +} Index: src/org/eclipse/team/ui/synchronize/ISynchronizeView.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.team.ui/src/org/eclipse/team/ui/synchronize/ISynchronizeView.java,v retrieving revision 1.14 diff -u -r1.14 ISynchronizeView.java --- src/org/eclipse/team/ui/synchronize/ISynchronizeView.java 30 Sep 2009 11:54:27 -0000 1.14 +++ src/org/eclipse/team/ui/synchronize/ISynchronizeView.java 1 Oct 2009 11:22:19 -0000 @@ -56,4 +56,18 @@ * or null if none */ public ISynchronizeParticipant getParticipant(); + + /** + * Returns whether this Synchronize view's selection automatically tracks the active editor. + * + * @return true if linking is enabled, false if not + */ + boolean isLinkingEnabled(); + + /** + * Sets whether this Synchronize view's selection automatically tracks the active editor. + * + * @param enabled true to enable, false to disable + */ + void setLinkingEnabled(boolean enabled); }