diff options
Diffstat (limited to 'org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java')
-rw-r--r-- | org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java | 196 |
1 files changed, 173 insertions, 23 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java index d9a3e47fd2..142e01ed22 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositorySearchDialog.java @@ -11,13 +11,20 @@ package org.eclipse.egit.ui.internal.repository; import java.io.File; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.egit.core.Activator; import org.eclipse.egit.ui.UIText; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -33,11 +40,14 @@ import org.eclipse.jface.viewers.IColorProvider; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jgit.lib.RepositoryCache; +import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; @@ -47,20 +57,27 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; +import org.osgi.service.prefs.BackingStoreException; /** * Searches for Git directories under a path that can be selected by the user */ public class RepositorySearchDialog extends Dialog { - private final Set<String> existingRepositoryDirs = new HashSet<String>(); + private static final String PREF_DEEP_SEARCH = "RepositorySearchDialogDeepSearch"; //$NON-NLS-1$ + + private static final String PREF_PATH = "RepositorySearchDialogSearchPath"; //$NON-NLS-1$ - private final String myInitialPath; + private final Set<String> existingRepositoryDirs = new HashSet<String>(); private Set<String> result; CheckboxTableViewer tv; + private Button btnToggleSelect; + + private Table tab; + private final class ContentProvider implements IStructuredContentProvider { @SuppressWarnings("unchecked") @@ -104,15 +121,12 @@ public class RepositorySearchDialog extends Dialog { /** * @param parentShell - * @param initialPath - * the initial path * @param existingDirs */ - protected RepositorySearchDialog(Shell parentShell, String initialPath, + protected RepositorySearchDialog(Shell parentShell, Collection<String> existingDirs) { super(parentShell); this.existingRepositoryDirs.addAll(existingDirs); - this.myInitialPath = initialPath; setShellStyle(getShellStyle() | SWT.SHELL_TRIM); } @@ -127,7 +141,8 @@ public class RepositorySearchDialog extends Dialog { @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); - newShell.setText(UIText.RepositorySearchDialog_SearchRepositoriesHeader); + newShell + .setText(UIText.RepositorySearchDialog_SearchRepositoriesHeader); } @Override @@ -143,21 +158,27 @@ public class RepositorySearchDialog extends Dialog { @Override protected Control createDialogArea(Composite parent) { - Composite main = new Composite(parent, SWT.NONE); - main.setLayout(new GridLayout(3, false)); + final IEclipsePreferences prefs = new InstanceScope().getNode(Activator + .getPluginId()); - GridDataFactory.fillDefaults().grab(true, true).applyTo(main); + Composite main = new Composite(parent, SWT.NONE); + main.setLayout(new GridLayout(4, false)); + main.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Label dirLabel = new Label(main, SWT.NONE); dirLabel.setText(UIText.RepositorySearchDialog_DirectoryLabel); - final Text dir = new Text(main, SWT.NONE); - if (myInitialPath != null) - dir.setText(myInitialPath); + final Text dir = new Text(main, SWT.BORDER); + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(2, 1).hint(300, + SWT.DEFAULT).applyTo(dir); - GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, - false).applyTo(dir); + String initialPath = prefs.get(PREF_PATH, ResourcesPlugin + .getWorkspace().getRoot().getLocation().toOSString()); + + dir.setText(initialPath); Button browse = new Button(main, SWT.PUSH); + browse.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, + 1, 1)); browse.setText(UIText.RepositorySearchDialog_BrowseButton); browse.addSelectionListener(new SelectionAdapter() { @@ -168,19 +189,73 @@ public class RepositorySearchDialog extends Dialog { String directory = dd.open(); if (directory != null) { dir.setText(directory); + prefs.put(PREF_PATH, directory); + try { + prefs.flush(); + } catch (BackingStoreException e1) { + // ignore here + } + } + } + + }); + + // we fill the room under the "Directory" label + new Label(main, SWT.NONE); + + final Button btnLookForNested = new Button(main, SWT.CHECK); + btnLookForNested.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, + false, false, 2, 1)); + btnLookForNested + .setSelection(prefs.getBoolean(PREF_DEEP_SEARCH, false)); + btnLookForNested + .setText(UIText.RepositorySearchDialog_DeepSearch_button); + + btnLookForNested.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + prefs.putBoolean(PREF_DEEP_SEARCH, btnLookForNested + .getSelection()); + try { + prefs.flush(); + } catch (BackingStoreException e1) { + // ignore } } }); Button search = new Button(main, SWT.PUSH); + search.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, + 1, 1)); search.setText(UIText.RepositorySearchDialog_SearchButton); - GridDataFactory.fillDefaults().align(SWT.LEAD, SWT.CENTER).span(3, 1) - .applyTo(search); tv = CheckboxTableViewer.newCheckList(main, SWT.NONE); - Table tab = tv.getTable(); - GridDataFactory.fillDefaults().grab(true, true).span(3, 1).applyTo(tab); + tab = tv.getTable(); + tab.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1)); + tab.setEnabled(false); + + btnToggleSelect = new Button(main, SWT.NONE); + btnToggleSelect.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, + false, 1, 1)); + btnToggleSelect + .setText(UIText.RepositorySearchDialog_ToggleSelection_button); + btnToggleSelect.setEnabled(false); + btnToggleSelect.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + + for (int i = 0; i < tab.getItemCount(); i++) { + if (!existingRepositoryDirs.contains(tv.getElementAt(i))) + tv.setChecked(tv.getElementAt(i), !tv.getChecked(tv + .getElementAt(i))); + } + getButton(IDialogConstants.OK_ID).setEnabled( + tv.getCheckedElements().length > 0); + } + }); tv.addCheckStateListener(new ICheckStateListener() { @@ -201,28 +276,64 @@ public class RepositorySearchDialog extends Dialog { public void widgetSelected(SelectionEvent e) { final TreeSet<String> directories = new TreeSet<String>(); final File file = new File(dir.getText()); + final boolean lookForNested = btnLookForNested.getSelection(); if (file.exists()) { + try { + prefs.put(PREF_PATH, file.getCanonicalPath()); + try { + prefs.flush(); + } catch (BackingStoreException e1) { + // ignore here + } + } catch (IOException e2) { + // ignore + } + IRunnableWithProgress action = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { - RepositoriesView.recurseDir(file, directories, - monitor); + + try { + findGitDirsRecursive(file, directories, + monitor, lookForNested); + } catch (Exception ex) { + Activator.getDefault().getLog().log( + new Status(IStatus.ERROR, Activator + .getPluginId(), + ex.getMessage(), ex)); + } } }; try { ProgressMonitorDialog pd = new ProgressMonitorDialog( getShell()); + pd + .getProgressMonitor() + .setTaskName( + UIText.RepositorySearchDialog_ScanningForRepositories_message); pd.run(true, true, action); } catch (InvocationTargetException e1) { - MessageDialog.openError(getShell(), UIText.RepositorySearchDialog_ErrorHeader, e1 - .getCause().getMessage()); + MessageDialog.openError(getShell(), + UIText.RepositorySearchDialog_ErrorHeader, e1 + .getCause().getMessage()); } catch (InterruptedException e1) { // ignore } + boolean foundNew = false; + + for (String foundDir : directories) { + if (!existingRepositoryDirs.contains(foundDir)) { + foundNew = true; + break; + } + } + + btnToggleSelect.setEnabled(foundNew); + tab.setEnabled(directories.size() > 0); tv.setInput(directories); } } @@ -240,4 +351,43 @@ public class RepositorySearchDialog extends Dialog { return bar; } + private void findGitDirsRecursive(File root, TreeSet<String> strings, + IProgressMonitor monitor, boolean lookForNestedRepositories) { + + if (!root.exists() || !root.isDirectory()) { + return; + } + File[] children = root.listFiles(); + // simply ignore null + if (children == null) + return; + + for (File child : children) { + if (monitor.isCanceled()) { + return; + } + + if (child.isDirectory() + && RepositoryCache.FileKey.isGitRepository(child)) { + try { + strings.add(child.getCanonicalPath()); + } catch (IOException e) { + // ignore here + } + monitor + .setTaskName(NLS + .bind( + UIText.RepositorySearchDialog_RepositoriesFound_message, + new Integer(strings.size()))); + if (!lookForNestedRepositories) + return; + } else if (child.isDirectory()) { + monitor.subTask(child.getPath()); + findGitDirsRecursive(child, strings, monitor, + lookForNestedRepositories); + } + } + + } + } |