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