core/com.nokia.cpp.utils.ui/src/com/nokia/cpp/internal/api/utils/ui/BrowseDialogUtils.java
changeset 677 d13345e153e8
parent 676 7a49747f237a
equal deleted inserted replaced
676:7a49747f237a 677:d13345e153e8
    23 import org.eclipse.swt.widgets.DirectoryDialog;
    23 import org.eclipse.swt.widgets.DirectoryDialog;
    24 import org.eclipse.swt.widgets.FileDialog;
    24 import org.eclipse.swt.widgets.FileDialog;
    25 import org.eclipse.swt.widgets.Text;
    25 import org.eclipse.swt.widgets.Text;
    26 
    26 
    27 /**
    27 /**
    28  * Utilities to make it easier to use file dialogs.
    28  * Utilities to make it easier to use file and directory dialogs which are associated
       
    29  * with text entry fields.
       
    30  * <p>
       
    31  * First, promote UI where Browse... starts from the current textual entry rather than
       
    32  * some random place.
       
    33  * <p>
       
    34  * Second, overcome terrible SWT behavior:  even if you do set the filter path, then if it 
       
    35  * is not 100% correct, it will again revert to the home directory.  So, find the nearest
       
    36  * directory that <i>does</i> exist.
    29  */
    37  */
    30 public class BrowseDialogUtils {
    38 public class BrowseDialogUtils {
    31 	/**
    39 	/**
    32 	 * When issuing a "Browse..." command next to a text entry field, initialize
    40 	 * When issuing a "Browse..." command next to a text entry field, initialize
    33 	 * the dialog with the existing path in that text field.
    41 	 * the dialog with the nearest existing file or directory in that text field.
       
    42 	 * @param dialog
       
    43 	 * @param textEntry
       
    44 	 * @pathm defaultPath the path to use when the text entry is empty 
       
    45 	 */
       
    46 	public static void initializeFrom(FileDialog dialog, Text textEntry, IPath defaultPath) {
       
    47 		if (textEntry != null) {
       
    48 			String existing = textEntry.getText().trim();
       
    49 			if (existing.length() > 0) {
       
    50 				initializeFrom(dialog, existing);
       
    51 				return;
       
    52 			}
       
    53 		}
       
    54 		if (defaultPath != null) {
       
    55 			initializeFrom(dialog, defaultPath);
       
    56 		}
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * When issuing a "Browse..." command next to a text entry field, initialize
       
    61 	 * the dialog with the nearest existing file or directory in that text field.
    34 	 * @param dialog
    62 	 * @param dialog
    35 	 * @param textEntry
    63 	 * @param textEntry
    36 	 */
    64 	 */
    37 	public static void initializeFrom(FileDialog dialog, Text textEntry) {
    65 	public static void initializeFrom(FileDialog dialog, Text textEntry) {
    38 		if (textEntry == null)
    66 		if (textEntry == null)
    39 			return;
    67 			return;
    40 		String existing = textEntry.getText();
    68 		String existing = textEntry.getText().trim();
    41 		initializeFrom(dialog, existing);
    69 		initializeFrom(dialog, existing);
    42 	}
    70 	}
    43 
    71 
    44 	/**
    72 	/**
    45 	 * When issuing a "Browse..." command with an expected path, initialize
    73 	 * When issuing a "Browse..." command with an expected file, initialize
    46 	 * the dialog with the existing path.
    74 	 * the dialog with the nearest existing file or directory.
    47 	 * @param dialog
    75 	 * @param dialog
    48 	 * @param path
    76 	 * @param path
    49 	 */
    77 	 */
    50 	public static void initializeFrom(FileDialog dialog, IPath path) {
    78 	public static void initializeFrom(FileDialog dialog, IPath path) {
    51 		if (path != null) {
    79 		if (path != null) {
    52 			initializeFrom(dialog, path.toOSString());
    80 			initializeFrom(dialog, path.toOSString());
    53 		}
    81 		}
    54 	}
    82 	}
    55 	
    83 	
    56 	/**
    84 	/**
    57 	 * When issuing a "Browse..." command with an expected path, initialize
    85 	 * When issuing a "Browse..." command with an expected file, initialize
    58 	 * the dialog with the existing path.
    86 	 * the dialog with the nearest existing file or directory.
    59 	 * @param dialog
    87 	 * @param dialog
    60 	 * @param path
    88 	 * @param path
    61 	 */
    89 	 */
    62 	public static void initializeFrom(FileDialog dialog, String path) {
    90 	public static void initializeFrom(FileDialog dialog, String path) {
    63 		if (path != null && path.length() > 0) {
    91 		if (path != null && path.length() > 0) {
       
    92 			boolean isDirectory = path.endsWith("/") || path.endsWith("\\");
    64 			File file = new File(path);
    93 			File file = new File(path);
    65 			if (file.exists()) {
    94 			boolean exists = file.exists();
       
    95 			if (exists) {
       
    96 				isDirectory = file.isDirectory();
       
    97 			}
       
    98 			if (!isDirectory) {
       
    99 				dialog.setFileName(file.getName());
       
   100 			}
       
   101 			if (exists) {
    66 				if (file.isAbsolute()) {
   102 				if (file.isAbsolute()) {
    67 					dialog.setFilterPath(file.getParent());
   103 					dialog.setFilterPath(isDirectory ? file.getAbsolutePath() : file.getParent());
    68 				}
   104 				}
    69 				dialog.setFileName(file.getName());
       
    70 			} else {
   105 			} else {
    71 				if (!file.isAbsolute())
   106 				if (!file.isAbsolute())
    72 					return;
   107 					return;
    73 				File dir = file.getParentFile();
   108 				File dir = file.getParentFile();
    74 				while (dir != null && !dir.exists()) {
   109 				while (dir != null && !dir.exists()) {
    81 		}
   116 		}
    82 	}
   117 	}
    83 	
   118 	
    84 	/**
   119 	/**
    85 	 * When issuing a "Browse..." command next to a text entry field, initialize
   120 	 * When issuing a "Browse..." command next to a text entry field, initialize
    86 	 * the dialog with the existing path in that text field.
   121 	 * the dialog with the nearest existing directory in that text field.
       
   122 	 * @param dialog
       
   123 	 * @param textEntry
       
   124 	 * @param defaultPath the default path if the text entry is empty
       
   125 	 */
       
   126 	public static void initializeFrom(DirectoryDialog dialog, Text textEntry, IPath defaultPath) {
       
   127 		if (textEntry != null) {
       
   128 			String existing = textEntry.getText().trim();
       
   129 			if (existing.length() > 0) {
       
   130 				initializeFrom(dialog, existing);
       
   131 				return;
       
   132 			}
       
   133 		}
       
   134 		if (defaultPath != null) {
       
   135 			initializeFrom(dialog, defaultPath);
       
   136 		}
       
   137 	}
       
   138 
       
   139 	/**
       
   140 	 * When issuing a "Browse..." command next to a text entry field, initialize
       
   141 	 * the dialog with the nearest existing directory in that text field.
    87 	 * @param dialog
   142 	 * @param dialog
    88 	 * @param textEntry
   143 	 * @param textEntry
    89 	 */
   144 	 */
    90 	public static void initializeFrom(DirectoryDialog dialog, Text textEntry) {
   145 	public static void initializeFrom(DirectoryDialog dialog, Text textEntry) {
    91 		if (textEntry == null)
   146 		if (textEntry == null)
    92 			return;
   147 			return;
    93 		String existing = textEntry.getText();
   148 		String existing = textEntry.getText().trim();
    94 		initializeFrom(dialog, existing);
   149 		initializeFrom(dialog, existing);
    95 	}
   150 	}
    96 
   151 
    97 	/**
   152 	/**
    98 	 * When issuing a "Browse..." command with an expected path, initialize
   153 	 * When issuing a "Browse..." command with an expected directory, initialize
    99 	 * the dialog with the existing path.
   154 	 * the dialog with the nearest existing path.
   100 	 * @param dialog
   155 	 * @param dialog
   101 	 * @param path
   156 	 * @param path
   102 	 */
   157 	 */
   103 	public static void initializeFrom(DirectoryDialog dialog, IPath path) {
   158 	public static void initializeFrom(DirectoryDialog dialog, IPath path) {
   104 		if (path != null) {
   159 		if (path != null) {
   105 			initializeFrom(dialog, path.toOSString());
   160 			initializeFrom(dialog, path.toOSString());
   106 		}
   161 		}
   107 	}
   162 	}
   108 	
   163 	
   109 	/**
   164 	/**
   110 	 * When issuing a "Browse..." command with an expected path, initialize
   165 	 * When issuing a "Browse..." command with an expected directory, initialize
   111 	 * the dialog with the existing path.
   166 	 * the dialog with the nearest existing path.
   112 	 * @param dialog
   167 	 * @param dialog
   113 	 * @param path
   168 	 * @param path
   114 	 */
   169 	 */
   115 	public static void initializeFrom(DirectoryDialog dialog, String path) {
   170 	public static void initializeFrom(DirectoryDialog dialog, String path) {
   116 		if (path != null && path.length() > 0) {
   171 		if (path != null && path.length() > 0) {