1 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 descriptionLabel, String browseLabel, IStringButtonAdapter adapter) { 42 super(adapter); 43 setContentAssistProcessor(new JavaSourcePackageFragmentRootCompletionProcessor()); 44 setLabelText(descriptionLabel); 45 setButtonLabel(browseLabel); 46 setDialogFieldListener(this); 47 } 48 49 52 public void dialogFieldChanged(DialogField field) { 53 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 81 private IPackageFragmentRoot getRootFromString(String 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 str= getRootString(); 111 if (!getText().equals(str)) { 112 setText(str); 113 } 114 } else { 115 } 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 getRootString() { 129 return (fRoot == null) ? "" : fRoot.getPath().makeRelative().toString(); } 131 132 133 } 134 | Popular Tags |