KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > ImportOrganizeInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.preferences;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IStatus;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 import org.eclipse.jface.dialogs.StatusDialog;
26 import org.eclipse.jface.operation.IRunnableContext;
27 import org.eclipse.jface.window.Window;
28
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.dialogs.SelectionDialog;
31
32 import org.eclipse.jdt.core.IPackageFragment;
33 import org.eclipse.jdt.core.IType;
34 import org.eclipse.jdt.core.JavaConventions;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jdt.core.search.IJavaSearchScope;
38 import org.eclipse.jdt.core.search.SearchEngine;
39
40 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
41 import org.eclipse.jdt.ui.JavaUI;
42
43 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
44 import org.eclipse.jdt.internal.ui.dialogs.PackageSelectionDialog;
45 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
46 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
47 import org.eclipse.jdt.internal.ui.preferences.ImportOrganizeConfigurationBlock.ImportOrderEntry;
48 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
50 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
56
57 /**
58  * Dialog to enter a new package entry in the organize import preference page.
59  */

60 public class ImportOrganizeInputDialog extends StatusDialog {
61     
62     private class ImportOrganizeInputAdapter implements IDialogFieldListener, IStringButtonAdapter {
63         /**
64          * @see IDialogFieldListener#dialogFieldChanged(DialogField)
65          */

66         public void dialogFieldChanged(DialogField field) {
67             doDialogFieldChanged(field);
68         }
69
70         /**
71          * @see IStringButtonAdapter#changeControlPressed(DialogField)
72          */

73         public void changeControlPressed(DialogField field) {
74             doBrowsePackages();
75         }
76     }
77     
78     private StringButtonDialogField fNameDialogField;
79     private SelectionButtonDialogField fBrowseTypeButton;
80     private List JavaDoc fExistingEntries;
81     private final boolean fIsStatic;
82         
83     public ImportOrganizeInputDialog(Shell parent, List JavaDoc/*<ImportOrderEntry>*/ existingEntries, boolean isStatic) {
84         super(parent);
85         fIsStatic= isStatic;
86         setShellStyle(getShellStyle() | SWT.RESIZE);
87         
88         fExistingEntries= existingEntries;
89         
90         String JavaDoc label, title;
91         if (isStatic) {
92             title= PreferencesMessages.ImportOrganizeInputDialog_title_static;
93             label= PreferencesMessages.ImportOrganizeInputDialog_name_group_static_label;
94         } else {
95             title= PreferencesMessages.ImportOrganizeInputDialog_title;
96             label= PreferencesMessages.ImportOrganizeInputDialog_name_group_label;
97         }
98         setTitle(title);
99
100         ImportOrganizeInputAdapter adapter= new ImportOrganizeInputAdapter();
101
102         fNameDialogField= new StringButtonDialogField(adapter);
103         fNameDialogField.setLabelText(label);
104         fNameDialogField.setButtonLabel(PreferencesMessages.ImportOrganizeInputDialog_browse_packages_button);
105         fNameDialogField.setDialogFieldListener(adapter);
106         fNameDialogField.setText(""); //$NON-NLS-1$
107

108         fBrowseTypeButton= new SelectionButtonDialogField(SWT.PUSH);
109         fBrowseTypeButton.setLabelText(PreferencesMessages.ImportOrganizeInputDialog_browse_types_label);
110         fBrowseTypeButton.setDialogFieldListener(adapter);
111     }
112         
113     public void setInitialSelection(ImportOrderEntry editedEntry) {
114         Assert.isNotNull(editedEntry);
115         if (editedEntry.name.length() == 0) {
116             fNameDialogField.setText(""); //$NON-NLS-1$
117
} else {
118             fNameDialogField.setText(editedEntry.name);
119         }
120     }
121     
122     public ImportOrderEntry getResult() {
123         String JavaDoc val= fNameDialogField.getText();
124         if ("*".equals(val)) { //$NON-NLS-1$
125
return new ImportOrderEntry("", fIsStatic); //$NON-NLS-1$
126
} else {
127             return new ImportOrderEntry(val, fIsStatic);
128         }
129     }
130         
131     protected Control createDialogArea(Composite parent) {
132         Composite composite= (Composite) super.createDialogArea(parent);
133         initializeDialogUnits(parent);
134         
135         GridLayout layout= (GridLayout) composite.getLayout();
136         layout.numColumns= 2;
137         
138         fNameDialogField.doFillIntoGrid(composite, 3);
139         
140         LayoutUtil.setHorizontalSpan(fNameDialogField.getLabelControl(null), 2);
141         
142         int fieldWidthHint= convertWidthInCharsToPixels(60);
143         Text text= fNameDialogField.getTextControl(null);
144         LayoutUtil.setWidthHint(text, fieldWidthHint);
145         LayoutUtil.setHorizontalGrabbing(text);
146         TextFieldNavigationHandler.install(text);
147         
148         DialogField.createEmptySpace(composite, 1);
149         fBrowseTypeButton.doFillIntoGrid(composite, 1);
150         
151         fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
152         
153         applyDialogFont(composite);
154         return composite;
155     }
156     
157     final void doBrowsePackages() {
158         IRunnableContext context= new BusyIndicatorRunnableContext();
159         IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
160         int style= PackageSelectionDialog.F_REMOVE_DUPLICATES | PackageSelectionDialog.F_SHOW_PARENTS | PackageSelectionDialog.F_HIDE_DEFAULT_PACKAGE;
161         PackageSelectionDialog dialog= new PackageSelectionDialog(getShell(), context, style, scope);
162         dialog.setFilter(fNameDialogField.getText());
163         dialog.setIgnoreCase(false);
164         dialog.setTitle(PreferencesMessages.ImportOrganizeInputDialog_ChoosePackageDialog_title);
165         dialog.setMessage(PreferencesMessages.ImportOrganizeInputDialog_ChoosePackageDialog_description);
166         dialog.setEmptyListMessage(PreferencesMessages.ImportOrganizeInputDialog_ChoosePackageDialog_empty);
167         if (dialog.open() == Window.OK) {
168             IPackageFragment res= (IPackageFragment) dialog.getFirstResult();
169             fNameDialogField.setText(res.getElementName());
170         }
171     }
172     
173     private void doBrowseTypes() {
174         IRunnableContext context= new BusyIndicatorRunnableContext();
175         IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
176         int style= IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
177         try {
178             SelectionDialog dialog= JavaUI.createTypeDialog(getShell(), context, scope, style, false, fNameDialogField.getText());
179             dialog.setTitle(PreferencesMessages.ImportOrganizeInputDialog_ChooseTypeDialog_title);
180             dialog.setMessage(PreferencesMessages.ImportOrganizeInputDialog_ChooseTypeDialog_description);
181             if (dialog.open() == Window.OK) {
182                 IType res= (IType) dialog.getResult()[0];
183                 fNameDialogField.setText(res.getFullyQualifiedName('.'));
184             }
185         } catch (JavaModelException e) {
186             ExceptionHandler.handle(e, getShell(), PreferencesMessages.ImportOrganizeInputDialog_ChooseTypeDialog_title, PreferencesMessages.ImportOrganizeInputDialog_ChooseTypeDialog_error_message);
187         }
188     }
189     
190     /**
191      * @param field
192      */

193     final void doDialogFieldChanged(DialogField field) {
194         if (field == fBrowseTypeButton) {
195             doBrowseTypes();
196         } else {
197             doValidation();
198         }
199     }
200     
201     
202     private void doValidation() {
203         StatusInfo status= new StatusInfo();
204         String JavaDoc newText= fNameDialogField.getText();
205         if (newText.length() == 0) {
206             status.setError(""); //$NON-NLS-1$
207
} else {
208             if (newText.equals("*")) { //$NON-NLS-1$
209
if (doesExist("", fIsStatic)) { //$NON-NLS-1$
210
status.setError(PreferencesMessages.ImportOrganizeInputDialog_error_entryExists);
211                 }
212             } else {
213                 IStatus val= JavaConventions.validateJavaTypeName(newText, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
214                 if (val.matches(IStatus.ERROR)) {
215                     status.setError(PreferencesMessages.ImportOrganizeInputDialog_error_invalidName);
216                 } else {
217                     if (doesExist(newText, fIsStatic)) {
218                         status.setError(PreferencesMessages.ImportOrganizeInputDialog_error_entryExists);
219                     }
220                 }
221             }
222         }
223         updateStatus(status);
224     }
225     
226     private boolean doesExist(String JavaDoc name, boolean isStatic) {
227         for (int i= 0; i < fExistingEntries.size(); i++) {
228             ImportOrderEntry entry= (ImportOrderEntry) fExistingEntries.get(i);
229             if (name.equals(entry.name) && isStatic == entry.isStatic) {
230                 return true;
231             }
232         }
233         return false;
234     }
235     
236
237     /*
238      * @see org.eclipse.jface.window.Window#configureShell(Shell)
239      */

240     protected void configureShell(Shell newShell) {
241         super.configureShell(newShell);
242         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.IMPORT_ORGANIZE_INPUT_DIALOG);
243     }
244
245
246 }
247
Popular Tags