001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.nls;
011:
012: import org.eclipse.core.runtime.IPath;
013:
014: import org.eclipse.core.resources.ResourcesPlugin;
015:
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Shell;
018:
019: import org.eclipse.jface.viewers.ILabelProvider;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.jface.viewers.ViewerFilter;
022: import org.eclipse.jface.window.Window;
023:
024: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
025:
026: import org.eclipse.jdt.core.ICompilationUnit;
027: import org.eclipse.jdt.core.IJavaElement;
028: import org.eclipse.jdt.core.IJavaModel;
029: import org.eclipse.jdt.core.IJavaProject;
030: import org.eclipse.jdt.core.IPackageFragment;
031: import org.eclipse.jdt.core.IPackageFragmentRoot;
032: import org.eclipse.jdt.core.JavaCore;
033: import org.eclipse.jdt.core.JavaModelException;
034:
035: import org.eclipse.jdt.ui.JavaElementComparator;
036: import org.eclipse.jdt.ui.JavaElementLabelProvider;
037: import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
038:
039: import org.eclipse.jdt.internal.ui.JavaPlugin;
040: import org.eclipse.jdt.internal.ui.util.PixelConverter;
041: import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
042: import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
043: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
044: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
045: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
046: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
047:
048: class SourceFirstPackageSelectionDialogField {
049:
050: private SourceFolderSelectionDialogButtonField fSourceFolderSelection;
051: private PackageFragmentSelection fPackageSelection;
052: private Shell fShell;
053:
054: public SourceFirstPackageSelectionDialogField(String sourceLabel,
055: String packageLabel, String browseLabel1,
056: String browseLabel2, String statusHint, String dialogTitle,
057: String dialogMessage, String dialogEmptyMessage,
058: ICompilationUnit cu, IDialogFieldListener updateListener,
059: IPackageFragment fragment) {
060: fSourceFolderSelection = new SourceFolderSelectionDialogButtonField(
061: sourceLabel, browseLabel1, new SFStringButtonAdapter());
062:
063: fPackageSelection = new PackageFragmentSelection(this ,
064: packageLabel, browseLabel2, statusHint,
065: new PackageSelectionStringButtonAdapter(this ,
066: dialogTitle, dialogMessage, dialogEmptyMessage));
067: fPackageSelection
068: .setDialogFieldListener(new PackageSelectionDialogFieldListener());
069:
070: fSourceFolderSelection
071: .setSourceChangeListener(fPackageSelection);
072:
073: setDefaults(fragment, cu);
074:
075: fPackageSelection.setUpdateListener(updateListener);
076: fSourceFolderSelection.setUpdateListener(updateListener);
077: }
078:
079: private void setDefaults(IPackageFragment fragment,
080: ICompilationUnit cu) {
081: IJavaElement element = fragment;
082: if (element == null) {
083: element = cu;
084: }
085:
086: fSourceFolderSelection
087: .setRoot(searchSourcePackageFragmentRoot(element));
088: fPackageSelection
089: .setPackageFragment(searchPackageFragment(element));
090: }
091:
092: private IPackageFragment searchPackageFragment(IJavaElement jElement) {
093: return (IPackageFragment) jElement
094: .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
095: }
096:
097: private IPackageFragmentRoot searchSourcePackageFragmentRoot(
098: IJavaElement jElement) {
099: IJavaElement parent = jElement
100: .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
101: if (parent == null) {
102: return null;
103: }
104:
105: IPackageFragmentRoot res = (IPackageFragmentRoot) parent;
106: try {
107: if (res.getKind() == IPackageFragmentRoot.K_SOURCE) {
108: return res;
109: }
110: } catch (JavaModelException e) {
111: // nothing to do
112: }
113:
114: return null;
115: }
116:
117: class PackageSelectionDialogFieldListener implements
118: IDialogFieldListener {
119:
120: public void dialogFieldChanged(DialogField field) {
121: String packName = fPackageSelection.getText();
122: if (packName.length() == 0)
123: fPackageSelection
124: .setStatus(NLSUIMessages.NLSAccessorConfigurationDialog_default);
125: else
126: fPackageSelection.setStatus(""); //$NON-NLS-1$
127: }
128: }
129:
130: class SFStringButtonAdapter implements IStringButtonAdapter {
131: public void changeControlPressed(DialogField field) {
132:
133: IPackageFragmentRoot newSourceContainer = chooseSourceContainer(fSourceFolderSelection
134: .getRoot());
135: if (newSourceContainer != null) {
136: fSourceFolderSelection.setRoot(newSourceContainer);
137: }
138: }
139: }
140:
141: private IPackageFragmentRoot chooseSourceContainer(
142: IJavaElement initElement) {
143: Class[] acceptedClasses = new Class[] {
144: IPackageFragmentRoot.class, IJavaProject.class };
145: TypedElementSelectionValidator validator = new TypedElementSelectionValidator(
146: acceptedClasses, false) {
147: public boolean isSelectedValid(Object element) {
148: try {
149: if (element instanceof IJavaProject) {
150: IJavaProject jproject = (IJavaProject) element;
151: IPath path = jproject.getProject()
152: .getFullPath();
153: return (jproject.findPackageFragmentRoot(path) != null);
154: } else if (element instanceof IPackageFragmentRoot) {
155: return (((IPackageFragmentRoot) element)
156: .getKind() == IPackageFragmentRoot.K_SOURCE);
157: }
158: return true;
159: } catch (JavaModelException e) {
160: JavaPlugin.log(e.getStatus()); // just log, no ui in validation
161: }
162: return false;
163: }
164: };
165:
166: acceptedClasses = new Class[] { IJavaModel.class,
167: IPackageFragmentRoot.class, IJavaProject.class };
168: ViewerFilter filter = new TypedViewerFilter(acceptedClasses) {
169: public boolean select(Viewer viewer, Object parent,
170: Object element) {
171: if (element instanceof IPackageFragmentRoot) {
172: try {
173: return (((IPackageFragmentRoot) element)
174: .getKind() == IPackageFragmentRoot.K_SOURCE);
175: } catch (JavaModelException e) {
176: JavaPlugin.log(e.getStatus()); // just log, no ui in validation
177: return false;
178: }
179: }
180: return super .select(viewer, parent, element);
181: }
182: };
183:
184: StandardJavaElementContentProvider provider = new StandardJavaElementContentProvider();
185: ILabelProvider labelProvider = new JavaElementLabelProvider(
186: JavaElementLabelProvider.SHOW_DEFAULT);
187: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
188: fShell, labelProvider, provider);
189: dialog.setValidator(validator);
190: dialog.setComparator(new JavaElementComparator());
191: dialog
192: .setTitle(NLSUIMessages.SourceFirstPackageSelectionDialogField_ChooseSourceContainerDialog_title);
193: dialog
194: .setMessage(NLSUIMessages.SourceFirstPackageSelectionDialogField_ChooseSourceContainerDialog_description);
195: dialog.addFilter(filter);
196: dialog.setInput(JavaCore.create(ResourcesPlugin.getWorkspace()
197: .getRoot()));
198: dialog.setInitialSelection(initElement);
199:
200: if (dialog.open() == Window.OK) {
201: Object element = dialog.getFirstResult();
202: if (element instanceof IJavaProject) {
203: IJavaProject jproject = (IJavaProject) element;
204: return jproject.getPackageFragmentRoot(jproject
205: .getProject());
206: } else if (element instanceof IPackageFragmentRoot) {
207: return (IPackageFragmentRoot) element;
208: }
209: return null;
210: }
211: return null;
212: }
213:
214: public IPackageFragment getSelected() {
215: IPackageFragment res = fPackageSelection.getPackageFragment();
216: return res;
217: }
218:
219: public IPackageFragmentRoot getSelectedFragmentRoot() {
220: return fSourceFolderSelection.getRoot();
221: }
222:
223: public void setSelected(IPackageFragment newSelection) {
224: fPackageSelection.setPackageFragment(newSelection);
225: fSourceFolderSelection
226: .setRoot(searchSourcePackageFragmentRoot(newSelection));
227: }
228:
229: public void createControl(Composite parent, int nOfColumns,
230: int textWidth) {
231: fShell = parent.getShell();
232: PixelConverter converter = new PixelConverter(parent);
233: fSourceFolderSelection.doFillIntoGrid(parent, nOfColumns,
234: textWidth);
235: LayoutUtil.setWidthHint(fSourceFolderSelection
236: .getTextControl(null), converter
237: .convertWidthInCharsToPixels(60));
238:
239: fPackageSelection.doFillIntoGrid(parent, nOfColumns, textWidth);
240: LayoutUtil.setWidthHint(fPackageSelection.getTextControl(null),
241: converter.convertWidthInCharsToPixels(60));
242: }
243:
244: }
|