|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.cpp.internal.api.utils.ui; |
|
19 |
|
20 import java.io.File; |
|
21 |
|
22 import org.eclipse.core.runtime.IPath; |
|
23 import org.eclipse.swt.widgets.DirectoryDialog; |
|
24 import org.eclipse.swt.widgets.FileDialog; |
|
25 import org.eclipse.swt.widgets.Text; |
|
26 |
|
27 /** |
|
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. |
|
37 */ |
|
38 public class BrowseDialogUtils { |
|
39 /** |
|
40 * When issuing a "Browse..." command next to a text entry field, initialize |
|
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. |
|
62 * @param dialog |
|
63 * @param textEntry |
|
64 */ |
|
65 public static void initializeFrom(FileDialog dialog, Text textEntry) { |
|
66 if (textEntry == null) |
|
67 return; |
|
68 String existing = textEntry.getText().trim(); |
|
69 initializeFrom(dialog, existing); |
|
70 } |
|
71 |
|
72 /** |
|
73 * When issuing a "Browse..." command with an expected file, initialize |
|
74 * the dialog with the nearest existing file or directory. |
|
75 * @param dialog |
|
76 * @param path |
|
77 */ |
|
78 public static void initializeFrom(FileDialog dialog, IPath path) { |
|
79 if (path != null) { |
|
80 initializeFrom(dialog, path.toOSString()); |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * When issuing a "Browse..." command with an expected file, initialize |
|
86 * the dialog with the nearest existing file or directory. |
|
87 * @param dialog |
|
88 * @param path |
|
89 */ |
|
90 public static void initializeFrom(FileDialog dialog, String path) { |
|
91 if (path != null && path.length() > 0) { |
|
92 boolean isDirectory = path.endsWith("/") || path.endsWith("\\"); |
|
93 File file = new File(path); |
|
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) { |
|
102 if (file.isAbsolute()) { |
|
103 dialog.setFilterPath(isDirectory ? file.getAbsolutePath() : file.getParent()); |
|
104 } |
|
105 } else { |
|
106 if (!file.isAbsolute()) |
|
107 return; |
|
108 File dir = file.getParentFile(); |
|
109 while (dir != null && !dir.exists()) { |
|
110 dir = dir.getParentFile(); |
|
111 } |
|
112 if (dir != null) { |
|
113 dialog.setFilterPath(dir.getAbsolutePath()); |
|
114 } |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 /** |
|
120 * When issuing a "Browse..." command next to a text entry field, initialize |
|
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. |
|
142 * @param dialog |
|
143 * @param textEntry |
|
144 */ |
|
145 public static void initializeFrom(DirectoryDialog dialog, Text textEntry) { |
|
146 if (textEntry == null) |
|
147 return; |
|
148 String existing = textEntry.getText().trim(); |
|
149 initializeFrom(dialog, existing); |
|
150 } |
|
151 |
|
152 /** |
|
153 * When issuing a "Browse..." command with an expected directory, initialize |
|
154 * the dialog with the nearest existing path. |
|
155 * @param dialog |
|
156 * @param path |
|
157 */ |
|
158 public static void initializeFrom(DirectoryDialog dialog, IPath path) { |
|
159 if (path != null) { |
|
160 initializeFrom(dialog, path.toOSString()); |
|
161 } |
|
162 } |
|
163 |
|
164 /** |
|
165 * When issuing a "Browse..." command with an expected directory, initialize |
|
166 * the dialog with the nearest existing path. |
|
167 * @param dialog |
|
168 * @param path |
|
169 */ |
|
170 public static void initializeFrom(DirectoryDialog dialog, String path) { |
|
171 if (path != null && path.length() > 0) { |
|
172 File file = new File(path); |
|
173 if (!file.isAbsolute()) |
|
174 return; |
|
175 while (file != null && !file.exists()) { |
|
176 file = file.getParentFile(); |
|
177 } |
|
178 if (file != null) { |
|
179 dialog.setFilterPath(file.getAbsolutePath()); |
|
180 } |
|
181 } |
|
182 } |
|
183 } |