1 11 package org.eclipse.jdt.internal.ui.refactoring.nls; 12 13 import org.eclipse.core.runtime.IPath; 14 15 import org.eclipse.core.resources.ResourcesPlugin; 16 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.jface.viewers.ILabelProvider; 21 import org.eclipse.jface.viewers.Viewer; 22 import org.eclipse.jface.viewers.ViewerFilter; 23 import org.eclipse.jface.window.Window; 24 25 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaModel; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.core.IPackageFragment; 32 import org.eclipse.jdt.core.IPackageFragmentRoot; 33 import org.eclipse.jdt.core.JavaCore; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.ui.JavaElementComparator; 37 import org.eclipse.jdt.ui.JavaElementLabelProvider; 38 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.util.PixelConverter; 42 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 43 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; 44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 46 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 47 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 48 49 class SourceFirstPackageSelectionDialogField { 50 51 private SourceFolderSelectionDialogButtonField fSourceFolderSelection; 52 private PackageFragmentSelection fPackageSelection; 53 private Shell fShell; 54 55 public SourceFirstPackageSelectionDialogField(String sourceLabel, String packageLabel, String browseLabel1, 56 String browseLabel2, String statusHint, String dialogTitle, String dialogMessage, String dialogEmptyMessage, 57 ICompilationUnit cu, IDialogFieldListener updateListener, IPackageFragment fragment) { 58 fSourceFolderSelection= new SourceFolderSelectionDialogButtonField(sourceLabel, browseLabel1, new SFStringButtonAdapter()); 59 60 fPackageSelection= new PackageFragmentSelection(this, packageLabel, browseLabel2, statusHint, 61 new PackageSelectionStringButtonAdapter(this, dialogTitle, dialogMessage, dialogEmptyMessage)); 62 fPackageSelection.setDialogFieldListener(new PackageSelectionDialogFieldListener()); 63 64 fSourceFolderSelection.setSourceChangeListener(fPackageSelection); 65 66 setDefaults(fragment, cu); 67 68 fPackageSelection.setUpdateListener(updateListener); 69 fSourceFolderSelection.setUpdateListener(updateListener); 70 } 71 72 private void setDefaults(IPackageFragment fragment, ICompilationUnit cu) { 73 IJavaElement element= fragment; 74 if (element == null) { 75 element= cu; 76 } 77 78 fSourceFolderSelection.setRoot(searchSourcePackageFragmentRoot(element)); 79 fPackageSelection.setPackageFragment(searchPackageFragment(element)); 80 } 81 82 private IPackageFragment searchPackageFragment(IJavaElement jElement) { 83 return (IPackageFragment)jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 84 } 85 86 private IPackageFragmentRoot searchSourcePackageFragmentRoot(IJavaElement jElement) { 87 IJavaElement parent= jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 88 if (parent == null) { 89 return null; 90 } 91 92 IPackageFragmentRoot res= (IPackageFragmentRoot)parent; 93 try { 94 if (res.getKind() == IPackageFragmentRoot.K_SOURCE) { 95 return res; 96 } 97 } catch (JavaModelException e) { 98 } 100 101 return null; 102 } 103 104 class PackageSelectionDialogFieldListener implements IDialogFieldListener { 105 106 public void dialogFieldChanged(DialogField field) { 107 String packName= fPackageSelection.getText(); 108 if (packName.length() == 0) 109 fPackageSelection.setStatus(NLSUIMessages.NLSAccessorConfigurationDialog_default); 110 else 111 fPackageSelection.setStatus(""); } 113 } 114 115 class SFStringButtonAdapter implements IStringButtonAdapter { 116 public void changeControlPressed(DialogField field) { 117 118 IPackageFragmentRoot newSourceContainer= chooseSourceContainer(fSourceFolderSelection.getRoot()); 119 if (newSourceContainer != null) { 120 fSourceFolderSelection.setRoot(newSourceContainer); 121 } 122 } 123 } 124 125 private IPackageFragmentRoot chooseSourceContainer(IJavaElement initElement) { 126 Class [] acceptedClasses= new Class [] { IPackageFragmentRoot.class, IJavaProject.class }; 127 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false) { 128 public boolean isSelectedValid(Object element) { 129 try { 130 if (element instanceof IJavaProject) { 131 IJavaProject jproject= (IJavaProject)element; 132 IPath path= jproject.getProject().getFullPath(); 133 return (jproject.findPackageFragmentRoot(path) != null); 134 } else if (element instanceof IPackageFragmentRoot) { 135 return (((IPackageFragmentRoot)element).getKind() == IPackageFragmentRoot.K_SOURCE); 136 } 137 return true; 138 } catch (JavaModelException e) { 139 JavaPlugin.log(e.getStatus()); } 141 return false; 142 } 143 }; 144 145 acceptedClasses= new Class [] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class }; 146 ViewerFilter filter= new TypedViewerFilter(acceptedClasses) { 147 public boolean select(Viewer viewer, Object parent, Object element) { 148 if (element instanceof IPackageFragmentRoot) { 149 try { 150 return (((IPackageFragmentRoot)element).getKind() == IPackageFragmentRoot.K_SOURCE); 151 } catch (JavaModelException e) { 152 JavaPlugin.log(e.getStatus()); return false; 154 } 155 } 156 return super.select(viewer, parent, element); 157 } 158 }; 159 160 StandardJavaElementContentProvider provider= new StandardJavaElementContentProvider(); 161 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 162 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, labelProvider, provider); 163 dialog.setValidator(validator); 164 dialog.setComparator(new JavaElementComparator()); 165 dialog.setTitle(NLSUIMessages.SourceFirstPackageSelectionDialogField_ChooseSourceContainerDialog_title); 166 dialog.setMessage(NLSUIMessages.SourceFirstPackageSelectionDialogField_ChooseSourceContainerDialog_description); 167 dialog.addFilter(filter); 168 dialog.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())); 169 dialog.setInitialSelection(initElement); 170 171 if (dialog.open() == Window.OK) { 172 Object element= dialog.getFirstResult(); 173 if (element instanceof IJavaProject) { 174 IJavaProject jproject= (IJavaProject)element; 175 return jproject.getPackageFragmentRoot(jproject.getProject()); 176 } else if (element instanceof IPackageFragmentRoot) { 177 return (IPackageFragmentRoot)element; 178 } 179 return null; 180 } 181 return null; 182 } 183 184 185 public IPackageFragment getSelected() { 186 IPackageFragment res= fPackageSelection.getPackageFragment(); 187 return res; 188 } 189 190 public IPackageFragmentRoot getSelectedFragmentRoot() { 191 return fSourceFolderSelection.getRoot(); 192 } 193 194 public void setSelected(IPackageFragment newSelection) { 195 fPackageSelection.setPackageFragment(newSelection); 196 fSourceFolderSelection.setRoot(searchSourcePackageFragmentRoot(newSelection)); 197 } 198 199 public void createControl(Composite parent, int nOfColumns, int textWidth) { 200 fShell= parent.getShell(); 201 PixelConverter converter= new PixelConverter(parent); 202 fSourceFolderSelection.doFillIntoGrid(parent, nOfColumns, textWidth); 203 LayoutUtil.setWidthHint(fSourceFolderSelection.getTextControl(null), converter.convertWidthInCharsToPixels(60)); 204 205 fPackageSelection.doFillIntoGrid(parent, nOfColumns, textWidth); 206 LayoutUtil.setWidthHint(fPackageSelection.getTextControl(null), converter.convertWidthInCharsToPixels(60)); 207 } 208 209 } 210 | Popular Tags |