|
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 dialogs. |
|
29 */ |
|
30 public class BrowseDialogUtils { |
|
31 /** |
|
32 * When issuing a "Browse..." command next to a text entry field, initialize |
|
33 * the dialog with the existing path in that text field. |
|
34 * @param dialog |
|
35 * @param textEntry |
|
36 */ |
|
37 public static void initializeFrom(FileDialog dialog, Text textEntry) { |
|
38 if (textEntry == null) |
|
39 return; |
|
40 String existing = textEntry.getText(); |
|
41 initializeFrom(dialog, existing); |
|
42 } |
|
43 |
|
44 /** |
|
45 * When issuing a "Browse..." command with an expected path, initialize |
|
46 * the dialog with the existing path. |
|
47 * @param dialog |
|
48 * @param path |
|
49 */ |
|
50 public static void initializeFrom(FileDialog dialog, IPath path) { |
|
51 if (path != null) { |
|
52 initializeFrom(dialog, path.toOSString()); |
|
53 } |
|
54 } |
|
55 |
|
56 /** |
|
57 * When issuing a "Browse..." command with an expected path, initialize |
|
58 * the dialog with the existing path. |
|
59 * @param dialog |
|
60 * @param path |
|
61 */ |
|
62 public static void initializeFrom(FileDialog dialog, String path) { |
|
63 if (path != null && path.length() > 0) { |
|
64 File file = new File(path); |
|
65 if (file.exists()) { |
|
66 if (file.isAbsolute()) { |
|
67 dialog.setFilterPath(file.getParent()); |
|
68 } |
|
69 dialog.setFileName(file.getName()); |
|
70 } else { |
|
71 if (!file.isAbsolute()) |
|
72 return; |
|
73 File dir = file.getParentFile(); |
|
74 while (dir != null && !dir.exists()) { |
|
75 dir = dir.getParentFile(); |
|
76 } |
|
77 if (dir != null) { |
|
78 dialog.setFilterPath(dir.getAbsolutePath()); |
|
79 } |
|
80 } |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * When issuing a "Browse..." command next to a text entry field, initialize |
|
86 * the dialog with the existing path in that text field. |
|
87 * @param dialog |
|
88 * @param textEntry |
|
89 */ |
|
90 public static void initializeFrom(DirectoryDialog dialog, Text textEntry) { |
|
91 if (textEntry == null) |
|
92 return; |
|
93 String existing = textEntry.getText(); |
|
94 initializeFrom(dialog, existing); |
|
95 } |
|
96 |
|
97 /** |
|
98 * When issuing a "Browse..." command with an expected path, initialize |
|
99 * the dialog with the existing path. |
|
100 * @param dialog |
|
101 * @param path |
|
102 */ |
|
103 public static void initializeFrom(DirectoryDialog dialog, IPath path) { |
|
104 if (path != null) { |
|
105 initializeFrom(dialog, path.toOSString()); |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * When issuing a "Browse..." command with an expected path, initialize |
|
111 * the dialog with the existing path. |
|
112 * @param dialog |
|
113 * @param path |
|
114 */ |
|
115 public static void initializeFrom(DirectoryDialog dialog, String path) { |
|
116 if (path != null && path.length() > 0) { |
|
117 File file = new File(path); |
|
118 if (!file.isAbsolute()) |
|
119 return; |
|
120 while (file != null && !file.exists()) { |
|
121 file = file.getParentFile(); |
|
122 } |
|
123 if (file != null) { |
|
124 dialog.setFilterPath(file.getAbsolutePath()); |
|
125 } |
|
126 } |
|
127 } |
|
128 } |