KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.core.IPackageFragment;
14 import org.eclipse.jdt.core.IPackageFragmentRoot;
15
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Text;
21
22 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
23 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
24 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaPackageCompletionProcessor;
25 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
26 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
27 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
28 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonStatusDialogField;
29
30 class PackageFragmentSelection extends StringButtonStatusDialogField implements SourceChangeListener {
31
32     private final SourceFirstPackageSelectionDialogField fDialogField;
33     private JavaPackageCompletionProcessor fCurrPackageCompletionProcessor;
34     private IDialogFieldListener fUpdateListener;
35
36     public PackageFragmentSelection(SourceFirstPackageSelectionDialogField field, String JavaDoc packageLabel, String JavaDoc browseLabel,
37         String JavaDoc statusHint, IStringButtonAdapter adapter) {
38         super(adapter);
39         fDialogField= field;
40         setLabelText(packageLabel);
41         setButtonLabel(browseLabel);
42         setStatusWidthHint(statusHint);
43         fCurrPackageCompletionProcessor= new JavaPackageCompletionProcessor();
44     }
45
46     public void setUpdateListener(IDialogFieldListener updateListener) {
47         fUpdateListener= updateListener;
48     }
49
50     public Control[] doFillIntoGrid(Composite parent, int nColumns, int textWidth) {
51         Control[] res= super.doFillIntoGrid(parent, nColumns);
52
53         final Text text= getTextControl(null);
54         text.addModifyListener(new ModifyListener() {
55             public void modifyText(ModifyEvent e) {
56                 updateListener();
57             }
58         });
59         LayoutUtil.setWidthHint(text, textWidth);
60         LayoutUtil.setHorizontalGrabbing(text);
61         ControlContentAssistHelper.createTextContentAssistant(text, fCurrPackageCompletionProcessor);
62         TextFieldNavigationHandler.install(text);
63
64         return res;
65     }
66
67     public void setPackageFragment(IPackageFragment fragment) {
68         if (fragment != null) {
69             setText(fragment.getElementName());
70         }
71
72         updateListener();
73     }
74
75     private void updateListener() {
76         if (fUpdateListener != null) {
77             fUpdateListener.dialogFieldChanged(this);
78         }
79     }
80
81     public IPackageFragment getPackageFragment() {
82         return calculateFragment(fDialogField.getSelectedFragmentRoot());
83     }
84
85     private IPackageFragment calculateFragment(IPackageFragmentRoot root) {
86         if (root == null) {
87             return null;
88         } else {
89             return root.getPackageFragment(getText());
90         }
91     }
92
93     public void sourceRootChanged(IPackageFragmentRoot newRoot) {
94         fCurrPackageCompletionProcessor.setPackageFragmentRoot(newRoot);
95
96         setPackageFragment(calculateFragment(newRoot));
97     }
98 }
99
Popular Tags