1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.util.List ; 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 60 public class ImportOrganizeInputDialog extends StatusDialog { 61 62 private class ImportOrganizeInputAdapter implements IDialogFieldListener, IStringButtonAdapter { 63 66 public void dialogFieldChanged(DialogField field) { 67 doDialogFieldChanged(field); 68 } 69 70 73 public void changeControlPressed(DialogField field) { 74 doBrowsePackages(); 75 } 76 } 77 78 private StringButtonDialogField fNameDialogField; 79 private SelectionButtonDialogField fBrowseTypeButton; 80 private List fExistingEntries; 81 private final boolean fIsStatic; 82 83 public ImportOrganizeInputDialog(Shell parent, List existingEntries, boolean isStatic) { 84 super(parent); 85 fIsStatic= isStatic; 86 setShellStyle(getShellStyle() | SWT.RESIZE); 87 88 fExistingEntries= existingEntries; 89 90 String 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(""); 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(""); } else { 118 fNameDialogField.setText(editedEntry.name); 119 } 120 } 121 122 public ImportOrderEntry getResult() { 123 String val= fNameDialogField.getText(); 124 if ("*".equals(val)) { return new ImportOrderEntry("", fIsStatic); } 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 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 newText= fNameDialogField.getText(); 205 if (newText.length() == 0) { 206 status.setError(""); } else { 208 if (newText.equals("*")) { if (doesExist("", fIsStatic)) { 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 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 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 |