KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > SourceFolderSelectionDialogButtonField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.refactoring.nls;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Text;
23
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.JavaCore;
27
28 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaSourcePackageFragmentRootCompletionProcessor;
29 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
30 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
31 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
32 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
33 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
34
35 class SourceFolderSelectionDialogButtonField extends StringButtonDialogField implements IDialogFieldListener {
36
37     private IPackageFragmentRoot fRoot;
38     private SourceChangeListener fListener;
39     private IDialogFieldListener fUpdateListener;
40
41     public SourceFolderSelectionDialogButtonField(String JavaDoc descriptionLabel, String JavaDoc browseLabel, IStringButtonAdapter adapter) {
42         super(adapter);
43         setContentAssistProcessor(new JavaSourcePackageFragmentRootCompletionProcessor());
44         setLabelText(descriptionLabel);
45         setButtonLabel(browseLabel);
46         setDialogFieldListener(this);
47     }
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
51      */

52     public void dialogFieldChanged(DialogField field) {
53         // propagate a textchange to the fragment root of this
54
setRoot(getRootFromString(getText()));
55     }
56
57     public void setUpdateListener(IDialogFieldListener updateListener) {
58         fUpdateListener= updateListener;
59     }
60
61     public Control[] doFillIntoGrid(Composite parent, int nColumns, int textWidth) {
62         Control[] res= super.doFillIntoGrid(parent, nColumns);
63
64         final Text text= getTextControl(null);
65         LayoutUtil.setWidthHint(text, textWidth);
66         LayoutUtil.setHorizontalGrabbing(text);
67
68         return res;
69     }
70
71     public void setSourceChangeListener(SourceChangeListener listener) {
72         fListener= listener;
73     }
74
75     /**
76      * tries to build a packagefragmentroot out of a string and sets the string into this
77      * packagefragmentroot.
78      *
79      * @param rootString
80      */

81     private IPackageFragmentRoot getRootFromString(String JavaDoc rootString) {
82         if (rootString.length() == 0) {
83             return null;
84         }
85         IPath path= new Path(rootString);
86         IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
87         IResource res= workspaceRoot.findMember(path);
88         if (res == null) {
89             return null;
90         }
91         int resType= res.getType();
92         if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
93             IProject proj= res.getProject();
94             if (!proj.isOpen()) {
95                 return null;
96             }
97             IJavaProject jproject= JavaCore.create(proj);
98             IPackageFragmentRoot root= jproject.getPackageFragmentRoot(res);
99             if (root.exists()) {
100                 return root;
101             }
102         }
103         return null;
104     }
105
106     public void setRoot(IPackageFragmentRoot root) {
107         fRoot= root;
108
109         if (fRoot != null) {
110             String JavaDoc str= getRootString();
111             if (!getText().equals(str)) {
112                 setText(str);
113             }
114         } else {
115             // dont ripple if the root is not a real root
116
}
117
118         fListener.sourceRootChanged(fRoot);
119         if (fUpdateListener != null) {
120             fUpdateListener.dialogFieldChanged(this);
121         }
122     }
123
124     public IPackageFragmentRoot getRoot() {
125         return fRoot;
126     }
127
128     private String JavaDoc getRootString() {
129         return (fRoot == null) ? "" : fRoot.getPath().makeRelative().toString(); //$NON-NLS-1$
130
}
131
132
133 }
134
Popular Tags