Fixed: [ Issue 40 ] Add support for import resolution across multiple folders
https://code.google.com/p/protobuf-dt/issues/detail?id=40

Cleaning up UI code.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/InputDialog.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/InputDialog.java
new file mode 100644
index 0000000..3d879e5
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/InputDialog.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.preferences;
+
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * Base class for dialogs that accept user input.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class InputDialog extends Dialog {
+
+  private final String title;
+
+  /**
+   * Creates a new </code>{@link InputDialog}</code>.
+   * @param parent a shell which will be the parent of the new instance.
+   * @param title the title of the dialog.
+   */
+  public InputDialog(Shell parent, String title) {
+    super(parent);
+    setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
+    this.title = title;
+  }
+
+  @Override protected void configureShell(Shell shell) {
+    super.configureShell(shell);
+    if (title != null) shell.setText(title);
+  }
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
index 5b32eb7..d88b0f8 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
@@ -9,8 +9,8 @@
 package com.google.eclipse.protobuf.ui.preferences.paths;
 
 import static com.google.eclipse.protobuf.ui.preferences.paths.Messages.*;
-import static com.google.eclipse.protobuf.ui.swt.SelectDirectoryDialogLauncher.*;
-import static com.google.eclipse.protobuf.ui.swt.Shells.centerShell;
+import static com.google.eclipse.protobuf.ui.preferences.paths.SelectDirectoryDialogs.*;
+import static org.eclipse.jface.dialogs.IDialogConstants.OK_ID;
 import static org.eclipse.xtext.util.Strings.isEmpty;
 
 import org.eclipse.swt.SWT;
@@ -18,17 +18,14 @@
 import org.eclipse.swt.layout.*;
 import org.eclipse.swt.widgets.*;
 
+import com.google.eclipse.protobuf.ui.preferences.InputDialog;
+
 /**
  * Dialog where users can select a path (in the workspace or file system) to be included in resolution of imports.
  *
  * @author alruiz@google.com (Alex Ruiz)
  */
-public class AddDirectoryDialog extends Dialog {
-
-  private final Shell parent;
-
-  private Shell shell;
-  private boolean result;
+public class AddDirectoryDialog extends InputDialog {
 
   private DirectoryPath selectedPath;
 
@@ -36,52 +33,33 @@
   private Button btnWorkspace;
   private Button btnIsWorkspacePath;
   private Button btnFileSystem;
-  private Button btnCancel;
-  private Button btnOk;
 
   /**
    * Creates a new </code>{@link AddDirectoryDialog}</code>.
    * @param parent a shell which will be the parent of the new instance.
-   * @param title the title of this dialog.
    */
-  public AddDirectoryDialog(Shell parent, String title) {
-    super(parent, SWT.NONE);
-    this.parent = parent;
-    getStyle();
-    setText(title);
+  public AddDirectoryDialog(Shell parent) {
+    super(parent, includeDirectoryTitle);
   }
 
-  /**
-   * Opens this dialog.
-   * @return {@code true} if the user made a selection and pressed "OK" or {@code false} if the user pressed "Cancel."
-   */
-  public boolean open() {
-    result = false;
-    shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
-    shell.setSize(503, 254);
-    shell.setText(getText());
-    createAndCenterContent();
-    shell.open();
-    Display display = parent.getDisplay();
-    while (!shell.isDisposed())
-      if (!display.readAndDispatch()) display.sleep();
-    return result;
-  }
+  /** {@inheritDoc} */
+  @Override protected Control createDialogArea(Composite parent) {
+    Composite cmpDialogArea = (Composite) super.createDialogArea(parent);
 
-  private void createAndCenterContent() {
-    shell.setLayout(new GridLayout(2, false));
+    GridLayout gridLayout = (GridLayout) cmpDialogArea.getLayout();
+    gridLayout.numColumns = 2;
 
-    Label label = new Label(shell, SWT.NONE);
+    Label label = new Label(cmpDialogArea, SWT.NONE);
     label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
     label.setText(includeDirectoryPrompt);
 
-    txtPath = new Text(shell, SWT.BORDER);
+    txtPath = new Text(cmpDialogArea, SWT.BORDER);
     txtPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
     txtPath.setEditable(false);
 
-    Composite cmpCheckBox = new Composite(shell, SWT.NONE);
+    Composite cmpCheckBox = new Composite(cmpDialogArea, SWT.NONE);
     cmpCheckBox.setEnabled(false);
-    cmpCheckBox.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
+    cmpCheckBox.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
     cmpCheckBox.setLayout(new GridLayout(1, false));
 
     btnIsWorkspacePath = new Button(cmpCheckBox, SWT.CHECK);
@@ -89,7 +67,8 @@
     btnIsWorkspacePath.setSize(158, 24);
     btnIsWorkspacePath.setText(isWorkspacePathCheck);
 
-    Composite cmpButtons = new Composite(shell, SWT.NONE);
+    Composite cmpButtons = new Composite(cmpDialogArea, SWT.NONE);
+    cmpButtons.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
     cmpButtons.setLayout(new GridLayout(2, true));
     new Label(cmpButtons, SWT.NONE);
 
@@ -98,31 +77,20 @@
     btnWorkspace.setText(browseWorkspace);
     new Label(cmpButtons, SWT.NONE);
 
-        btnFileSystem = new Button(cmpButtons, SWT.NONE);
-        btnFileSystem.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
-        btnFileSystem.setText(browseFileSystem);
-
-    btnOk = new Button(cmpButtons, SWT.NONE);
-    btnOk.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
-    btnOk.setEnabled(false);
-    btnOk.setText(ok);
-
-    btnCancel = new Button(cmpButtons, SWT.NONE);
-    btnCancel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
-    btnCancel.setText(cancel);
+    btnFileSystem = new Button(cmpButtons, SWT.NONE);
+    btnFileSystem.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+    btnFileSystem.setText(browseFileSystem);
 
     addEventListeners();
 
-    shell.setDefaultButton(btnOk);
-    shell.pack();
-
-    centerWindow();
+    applyDialogFont(cmpDialogArea);
+    return cmpDialogArea;
   }
 
   private void addEventListeners() {
     btnWorkspace.addSelectionListener(new SelectionAdapter() {
       @Override public void widgetSelected(SelectionEvent e) {
-        String path = showWorkspaceDirectoryDialog(shell, txtPath.getText(), null);
+        String path = showWorkspaceDirectoryDialog(getShell(), enteredPathText());
         if (path != null) {
           txtPath.setText(path.trim());
           btnIsWorkspacePath.setSelection(true);
@@ -131,35 +99,36 @@
     });
     btnFileSystem.addSelectionListener(new SelectionAdapter() {
       @Override public void widgetSelected(SelectionEvent e) {
-        String path = showFileSystemFolderDialog(shell, txtPath.getText());
+        String path = showFileSystemFolderDialog(getShell(), enteredPathText());
         if (path != null) {
           txtPath.setText(path.trim());
           btnIsWorkspacePath.setSelection(false);
         }
       }
     });
-    btnOk.addSelectionListener(new SelectionAdapter() {
-      @Override public void widgetSelected(SelectionEvent e) {
-        selectedPath = new DirectoryPath(txtPath.getText().trim(), btnIsWorkspacePath.getSelection());
-        result = true;
-        shell.dispose();
-      }
-    });
-    btnCancel.addSelectionListener(new SelectionAdapter() {
-      @Override public void widgetSelected(SelectionEvent e) {
-        shell.dispose();
-      }
-    });
     txtPath.addModifyListener(new ModifyListener() {
       public void modifyText(ModifyEvent e) {
-        boolean hasText = !isEmpty(txtPath.getText().trim());
-        btnOk.setEnabled(hasText);
+        boolean hasText = !isEmpty(enteredPathText());
+        getButton(OK_ID).setEnabled(hasText);
       }
     });
   }
 
-  private void centerWindow() {
-    centerShell(shell, parent);
+  /** {@inheritDoc} */
+  @Override protected void createButtonsForButtonBar(Composite parent) {
+    super.createButtonsForButtonBar(parent);
+    getButton(OK_ID).setEnabled(false);
+    txtPath.setFocus();
+  }
+
+  /** {@inheritDoc} */
+  @Override protected void okPressed() {
+    selectedPath = new DirectoryPath(enteredPathText(), btnIsWorkspacePath.getSelection());
+    super.okPressed();
+  }
+
+  private String enteredPathText() {
+    return txtPath.getText().trim();
   }
 
   /**
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
index c3721b1..e268f64 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
@@ -10,6 +10,7 @@
 
 import static com.google.eclipse.protobuf.ui.preferences.paths.Messages.*;
 import static java.util.Collections.unmodifiableList;
+import static org.eclipse.jface.window.Window.OK;
 
 import java.util.*;
 import java.util.List;
@@ -92,8 +93,8 @@
     });
     btnAdd.addSelectionListener(new SelectionAdapter() {
       @Override public void widgetSelected(SelectionEvent e) {
-        AddDirectoryDialog dialog = new AddDirectoryDialog(getShell(), includeDirectoryTitle);
-        if (dialog.open()) {
+        AddDirectoryDialog dialog = new AddDirectoryDialog(getShell());
+        if (dialog.open() == OK) {
           importPaths.add(dialog.selectedPath());
           updateTable();
           enableButtonsDependingOnTableSelection();
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/swt/SelectDirectoryDialogLauncher.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/SelectDirectoryDialogs.java
similarity index 76%
rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/swt/SelectDirectoryDialogLauncher.java
rename to com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/SelectDirectoryDialogs.java
index 4acbf87..3912eeb 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/swt/SelectDirectoryDialogLauncher.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/SelectDirectoryDialogs.java
@@ -6,7 +6,7 @@
  *
  * http://www.eclipse.org/legal/epl-v10.html
  */
-package com.google.eclipse.protobuf.ui.swt;
+package com.google.eclipse.protobuf.ui.preferences.paths;
 
 import static com.google.eclipse.protobuf.ui.swt.Messages.*;
 import static org.eclipse.core.runtime.IStatus.ERROR;
@@ -17,24 +17,25 @@
 import org.eclipse.core.resources.*;
 import org.eclipse.core.runtime.*;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.DirectoryDialog;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
-import org.eclipse.ui.dialogs.ISelectionStatusValidator;
-import org.eclipse.ui.model.WorkbenchContentProvider;
-import org.eclipse.ui.model.WorkbenchLabelProvider;
+import org.eclipse.swt.widgets.*;
+import org.eclipse.ui.dialogs.*;
+import org.eclipse.ui.model.*;
 import org.eclipse.ui.views.navigator.ResourceComparator;
 
 /**
- * Launches dialog where users can select a directory (either in a workspace or the file system.)
- * 
+ * Launchers for dialogs where users can select a directory (either in a workspace or the file system.)
+ *
  * @author alruiz@google.com (Alex Ruiz)
  */
-public class SelectDirectoryDialogLauncher {
+class SelectDirectoryDialogs {
 
   private static final String PLUGIN_ID = "com.google.eclipse.protobuf.ui";
 
-  public static String showWorkspaceDirectoryDialog(Shell shell, String initialPath, IProject project) {
+  static String showWorkspaceDirectoryDialog(Shell shell, String initialPath) {
+    return showWorkspaceDirectoryDialog(shell, initialPath, null);
+  }
+
+  static String showWorkspaceDirectoryDialog(Shell shell, String initialPath, IProject project) {
     String currentPathText = initialPath.replaceAll("\"", "");
     IPath path = new Path(currentPathText);
     ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(),
@@ -61,8 +62,8 @@
     if (resource == null) return null;
     return resource.getFullPath().toString();
   }
-  
-  public static String showFileSystemFolderDialog(Shell shell, String filterPath) {
+
+  static String showFileSystemFolderDialog(Shell shell, String filterPath) {
     DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN | SWT.APPLICATION_MODAL);
     if (filterPath != null && filterPath.trim().length() != 0) dialog.setFilterPath(filterPath);
     dialog.setMessage(browseFileSystemFolderPrompt);
@@ -73,5 +74,5 @@
     return ResourcesPlugin.getWorkspace().getRoot();
   }
 
-  private SelectDirectoryDialogLauncher() {}
+  private SelectDirectoryDialogs() {}
 }