From 7b8a1c1f0462dabc5534b33e7afe61045bede366 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Thu, 6 May 2010 11:14:37 +0200 Subject: [PATCH] Git Repositories View: Copy/Paste support This adds copy/paste support for the keyboard and introduces some error handling/user feedback for the paste action Bug: 311486 Change-Id: Ie49f9ebfeb32d5665a0e424a13c55169f824027d Signed-off-by: Mathias Kinzler --- .../src/org/eclipse/egit/ui/UIText.java | 18 +++ .../ui/internal/repository/RepositoriesView.java | 131 +++++++++++++++++++++ .../src/org/eclipse/egit/ui/uitext.properties | 6 + 3 files changed, 155 insertions(+) diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index 12b71e57..2ce6989a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -1576,6 +1576,12 @@ public class UIText extends NLS { public static String RepositoriesView_CheckOut_MenuItem; /** */ + public static String RepositoriesView_ClipboardContentNoGitRepoMessage; + + /** */ + public static String RepositoriesView_ClipboardContentNotDirectoryMessage; + + /** */ public static String RepositoriesView_Clone_Tooltip; /** */ @@ -1657,12 +1663,24 @@ public class UIText extends NLS { public static String RepositoriesView_NewRemoteMenu; /** */ + public static String RepositoriesView_NothingToPasteMessage; + + /** */ public static String RepositoriesView_OpenInTextEditor_menu; /** */ public static String RepositoriesView_OpenPropertiesMenu; /** */ + public static String RepositoriesView_PasteFailureTitle; + + /** */ + public static String RepositoriesView_PasteMenu; + + /** */ + public static String RepositoriesView_PasteRepoAlreadyThere; + + /** */ public static String RepositoriesView_Refresh_Button; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java index da2e7c1a..7486aa55 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java @@ -76,6 +76,7 @@ import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryConfig; import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.osgi.util.NLS; @@ -103,6 +104,7 @@ import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.editors.text.EditorsUI; import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.ide.IDE; @@ -167,6 +169,10 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, private IAction linkWithSelectionAction; + private IAction copyAction; + + private IAction pasteAction; + /** * TODO move to utility class * @@ -263,9 +269,21 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, public void selectionChanged(SelectionChangedEvent event) { + copyAction.setEnabled(false); + IStructuredSelection ssel = (IStructuredSelection) event .getSelection(); if (ssel.size() == 1) { + RepositoryTreeNode node = (RepositoryTreeNode) ssel + .getFirstElement(); + // allow copy on repository, file, or folder (copying the + // directory) + if (node.getType() == RepositoryTreeNodeType.REPO + || node.getType() == RepositoryTreeNodeType.WORKINGDIR + || node.getType() == RepositoryTreeNodeType.FOLDER + || node.getType() == RepositoryTreeNodeType.FILE) { + copyAction.setEnabled(true); + } setSelection(new StructuredSelection(ssel.getFirstElement())); } else { setSelection(new StructuredSelection()); @@ -412,6 +430,17 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, }); + MenuItem pasteItem = new MenuItem(men, SWT.PUSH); + pasteItem.setText(UIText.RepositoriesView_PasteMenu); + pasteItem.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + pasteAction.run(); + } + + }); + MenuItem refreshItem = new MenuItem(men, SWT.PUSH); refreshItem.setText(refreshAction.getText()); refreshItem.addSelectionListener(new SelectionAdapter() { @@ -1371,6 +1400,104 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, public void run() { tv.collapseAll(); } + }; + + // copy and paste are global actions; we just implement them + // and register them with the global action handler + // we enable/disable them upon tree selection changes + + copyAction = new Action("") { //$NON-NLS-1$ + + @Override + public void run() { + // for REPO, WORKINGDIR, FILE, FOLDER: copy directory + IStructuredSelection sel = (IStructuredSelection) tv + .getSelection(); + if (sel.size() == 1) { + RepositoryTreeNode node = (RepositoryTreeNode) sel + .getFirstElement(); + String dir = null; + if (node.getType() == RepositoryTreeNodeType.REPO) { + dir = node.getRepository().getDirectory().getPath(); + } else if (node.getType() == RepositoryTreeNodeType.FILE + || node.getType() == RepositoryTreeNodeType.FOLDER) { + dir = ((File) node.getObject()).getPath(); + } else if (node.getType() == RepositoryTreeNodeType.WORKINGDIR) { + dir = node.getRepository().getWorkDir().getPath(); + } + if (dir != null) { + Clipboard clip = null; + try { + clip = new Clipboard(getSite().getShell() + .getDisplay()); + clip + .setContents(new Object[] { dir }, + new Transfer[] { TextTransfer + .getInstance() }); + } finally { + if (clip != null) + // we must dispose ourselves + clip.dispose(); + } + } + } + } + + }; + copyAction.setEnabled(false); + + getViewSite().getActionBars().setGlobalActionHandler( + ActionFactory.COPY.getId(), copyAction); + + pasteAction = new Action("") { //$NON-NLS-1$ + + @Override + public void run() { + // we check if the pasted content is a directory + // repository location and try to add this + String errorMessage = null; + + Clipboard clip = null; + try { + clip = new Clipboard(getSite().getShell().getDisplay()); + String content = (String) clip.getContents(TextTransfer + .getInstance()); + if (content == null) { + errorMessage = UIText.RepositoriesView_NothingToPasteMessage; + return; + } + + File file = new File(content); + if (!file.exists() || !file.isDirectory()) { + errorMessage = UIText.RepositoriesView_ClipboardContentNotDirectoryMessage; + return; + } + + if (!RepositoryCache.FileKey.isGitRepository(file)) { + errorMessage = NLS + .bind( + UIText.RepositoriesView_ClipboardContentNoGitRepoMessage, + content); + return; + } + + if (addDir(file)) + scheduleRefresh(); + else + errorMessage = NLS.bind( + UIText.RepositoriesView_PasteRepoAlreadyThere, + content); + } finally { + if (clip != null) + // we must dispose ourselves + clip.dispose(); + if (errorMessage != null) + // TODO String ext + MessageDialog.openWarning(getSite().getShell(), + UIText.RepositoriesView_PasteFailureTitle, + errorMessage); + } + } }; @@ -1378,6 +1505,10 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, getViewSite().getActionBars().getToolBarManager() .add(collapseAllAction); + + getViewSite().getActionBars().setGlobalActionHandler( + ActionFactory.PASTE.getId(), pasteAction); + } /** diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 3a6cb809..f91a8481 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -565,6 +565,8 @@ RepositoriesView_Branches_Nodetext=Branches RepositoriesView_Checking_Message=Checking: {0} RepositoriesView_CheckingOutMessage=Checking out {0} RepositoriesView_CheckOut_MenuItem=Check out +RepositoriesView_ClipboardContentNoGitRepoMessage=Path {0} does not appear to be a Git Repository location +RepositoriesView_ClipboardContentNotDirectoryMessage=Clipboard content is not a directory path RepositoriesView_Clone_Tooltip=Import (clone) a Git Repository RepositoriesView_CollapseAllMenu=Collapse all RepositoriesView_ConfigureFetchMenu=Configure Fetch... @@ -593,8 +595,12 @@ RepositoriesView_NewBranchTitle=Create Branch RepositoriesView_NewLocalBranchMenu=Create a new local branch... RepositoriesView_NewRemoteBranchMenu=Create a new remote branch... RepositoriesView_NewRemoteMenu=New remote... +RepositoriesView_NothingToPasteMessage=Clipboard contains no data to paste RepositoriesView_OpenInTextEditor_menu=Open in text editor RepositoriesView_OpenPropertiesMenu=Open Properties view +RepositoriesView_PasteFailureTitle=Paste failure +RepositoriesView_PasteMenu=Paste a Git Repository Location +RepositoriesView_PasteRepoAlreadyThere=Repository at location {0} is already in the list RepositoriesView_Refresh_Button=Refresh RepositoriesView_RemotesNodeText=Remotes RepositoriesView_Remove_MenuItem=Remove -- 2.11.4.GIT