KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > PathEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.DirectoryDialog;
19
20 /**
21  * A field editor to edit directory paths.
22  */

23 public class PathEditor extends ListEditor {
24
25     /**
26      * The last path, or <code>null</code> if none.
27      */

28     private String JavaDoc lastPath;
29
30     /**
31      * The special label text for directory chooser,
32      * or <code>null</code> if none.
33      */

34     private String JavaDoc dirChooserLabelText;
35
36     /**
37      * Creates a new path field editor
38      */

39     protected PathEditor() {
40     }
41
42     /**
43      * Creates a path field editor.
44      *
45      * @param name the name of the preference this field editor works on
46      * @param labelText the label text of the field editor
47      * @param dirChooserLabelText the label text displayed for the directory chooser
48      * @param parent the parent of the field editor's control
49      */

50     public PathEditor(String JavaDoc name, String JavaDoc labelText,
51             String JavaDoc dirChooserLabelText, Composite parent) {
52         init(name, labelText);
53         this.dirChooserLabelText = dirChooserLabelText;
54         createControl(parent);
55     }
56
57     /* (non-Javadoc)
58      * Method declared on ListEditor.
59      * Creates a single string from the given array by separating each
60      * string with the appropriate OS-specific path separator.
61      */

62     protected String JavaDoc createList(String JavaDoc[] items) {
63         StringBuffer JavaDoc path = new StringBuffer JavaDoc("");//$NON-NLS-1$
64

65         for (int i = 0; i < items.length; i++) {
66             path.append(items[i]);
67             path.append(File.pathSeparator);
68         }
69         return path.toString();
70     }
71
72     /* (non-Javadoc)
73      * Method declared on ListEditor.
74      * Creates a new path element by means of a directory dialog.
75      */

76     protected String JavaDoc getNewInputObject() {
77
78         DirectoryDialog dialog = new DirectoryDialog(getShell());
79         if (dirChooserLabelText != null) {
80             dialog.setMessage(dirChooserLabelText);
81         }
82         if (lastPath != null) {
83             if (new File JavaDoc(lastPath).exists()) {
84                 dialog.setFilterPath(lastPath);
85             }
86         }
87         String JavaDoc dir = dialog.open();
88         if (dir != null) {
89             dir = dir.trim();
90             if (dir.length() == 0) {
91                 return null;
92             }
93             lastPath = dir;
94         }
95         return dir;
96     }
97
98     /* (non-Javadoc)
99      * Method declared on ListEditor.
100      */

101     protected String JavaDoc[] parseString(String JavaDoc stringList) {
102         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(stringList, File.pathSeparator
103                 + "\n\r");//$NON-NLS-1$
104
ArrayList JavaDoc v = new ArrayList JavaDoc();
105         while (st.hasMoreElements()) {
106             v.add(st.nextElement());
107         }
108         return (String JavaDoc[]) v.toArray(new String JavaDoc[v.size()]);
109     }
110 }
111
Popular Tags