1 11 package org.eclipse.jdt.internal.debug.ui.snippeteditor; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.IJavaProject; 19 import org.eclipse.jdt.core.IPackageFragmentRoot; 20 import org.eclipse.jdt.core.IType; 21 import org.eclipse.jdt.core.JavaModelException; 22 import org.eclipse.jdt.core.search.SearchEngine; 23 import org.eclipse.jdt.internal.debug.ui.ExceptionHandler; 24 import org.eclipse.jdt.internal.debug.ui.Filter; 25 import org.eclipse.jdt.internal.debug.ui.FilterLabelProvider; 26 import org.eclipse.jdt.internal.debug.ui.FilterViewerComparator; 27 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 29 import org.eclipse.jdt.internal.ui.util.SWTUtil; 30 import org.eclipse.jdt.ui.IJavaElementSearchConstants; 31 import org.eclipse.jdt.ui.JavaUI; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.dialogs.TitleAreaDialog; 34 import org.eclipse.jface.viewers.ColumnLayoutData; 35 import org.eclipse.jface.viewers.ColumnWeightData; 36 import org.eclipse.jface.viewers.ISelection; 37 import org.eclipse.jface.viewers.ISelectionChangedListener; 38 import org.eclipse.jface.viewers.IStructuredContentProvider; 39 import org.eclipse.jface.viewers.IStructuredSelection; 40 import org.eclipse.jface.viewers.SelectionChangedEvent; 41 import org.eclipse.jface.viewers.TableLayout; 42 import org.eclipse.jface.viewers.TableViewer; 43 import org.eclipse.jface.viewers.Viewer; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.events.SelectionEvent; 46 import org.eclipse.swt.events.SelectionListener; 47 import org.eclipse.swt.graphics.Font; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.layout.GridLayout; 50 import org.eclipse.swt.widgets.Button; 51 import org.eclipse.swt.widgets.Composite; 52 import org.eclipse.swt.widgets.Control; 53 import org.eclipse.swt.widgets.Shell; 54 import org.eclipse.swt.widgets.Table; 55 import org.eclipse.swt.widgets.TableColumn; 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 58 import org.eclipse.ui.dialogs.SelectionDialog; 59 60 import com.ibm.icu.text.MessageFormat; 61 62 public class SelectImportsDialog extends TitleAreaDialog { 63 64 private String [] fImports; 65 private Button fAddPackageButton; 66 private Button fAddTypeButton; 67 private Button fRemoveImportsButton; 68 private TableViewer fImportsViewer; 69 private Table fImportsTable; 70 private JavaSnippetEditor fEditor; 71 72 private ImportsContentProvider fImportContentProvider; 73 74 77 protected class ImportsContentProvider implements IStructuredContentProvider { 78 79 private TableViewer fViewer; 80 private List fImportNames; 81 82 public ImportsContentProvider(TableViewer viewer) { 83 fViewer = viewer; 84 populateImports(); 85 } 86 87 protected void populateImports() { 88 fImportNames= new ArrayList (1); 89 if (fImports != null) { 90 for (int i = 0; i < fImports.length; i++) { 91 String name = fImports[i]; 92 addImport(name); 93 } 94 } 95 } 96 97 protected void addImport(String name) { 98 Filter imprt = new Filter(name, false); 99 if (!fImportNames.contains(imprt)) { 100 fImportNames.add(imprt); 101 fViewer.add(imprt); 102 } 103 } 104 105 106 protected void removeImports(Object [] imports) { 107 for (int i = 0; i < imports.length; i++) { 108 Filter imprt = (Filter)imports[i]; 109 fImportNames.remove(imprt); 110 } 111 fViewer.remove(imports); 112 } 113 114 117 public Object [] getElements(Object inputElement) { 118 return fImportNames.toArray(); 119 } 120 121 124 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 125 } 126 127 130 public void dispose() { 131 } 132 } 133 134 public SelectImportsDialog(JavaSnippetEditor editor, String [] imports) { 135 super(editor.getShell()); 136 fEditor= editor; 137 fImports= imports; 138 } 139 140 private void createImportButtons(Composite container) { 141 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IJavaDebugHelpContextIds.SNIPPET_IMPORTS_DIALOG); 142 Font font = container.getFont(); 143 144 Composite buttonContainer = new Composite(container, SWT.NONE); 146 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL); 147 buttonContainer.setLayoutData(gd); 148 GridLayout buttonLayout = new GridLayout(); 149 buttonLayout.numColumns = 1; 150 buttonLayout.marginHeight = 0; 151 buttonLayout.marginWidth = 0; 152 buttonContainer.setLayout(buttonLayout); 153 154 fAddTypeButton = new Button(buttonContainer, SWT.PUSH); 156 fAddTypeButton.setText(SnippetMessages.getString("SelectImportsDialog.Add_&Type_1")); fAddTypeButton.setToolTipText(SnippetMessages.getString("SelectImportsDialog.Choose_a_Type_to_Add_as_an_Import_2")); gd = getButtonGridData(fAddTypeButton); 159 fAddTypeButton.setLayoutData(gd); 160 fAddTypeButton.setFont(font); 161 fAddTypeButton.addSelectionListener(new SelectionListener() { 162 public void widgetSelected(SelectionEvent se) { 163 addType(); 164 } 165 public void widgetDefaultSelected(SelectionEvent se) { 166 } 167 }); 168 169 fAddPackageButton = new Button(buttonContainer, SWT.PUSH); 171 fAddPackageButton.setText(SnippetMessages.getString("SelectImportsDialog.Add_&Package_3")); fAddPackageButton.setToolTipText(SnippetMessages.getString("SelectImportsDialog.Choose_a_Package_to_Add_as_an_Import_4")); gd = getButtonGridData(fAddPackageButton); 174 fAddPackageButton.setLayoutData(gd); 175 fAddPackageButton.setFont(font); 176 fAddPackageButton.addSelectionListener(new SelectionListener() { 177 public void widgetSelected(SelectionEvent se) { 178 addPackage(); 179 } 180 public void widgetDefaultSelected(SelectionEvent se) { 181 } 182 }); 183 184 fRemoveImportsButton = new Button(buttonContainer, SWT.PUSH); 186 fRemoveImportsButton.setText(SnippetMessages.getString("SelectImportsDialog.&Remove_5")); fRemoveImportsButton.setToolTipText(SnippetMessages.getString("SelectImportsDialog.Remove_All_Selected_Imports_6")); gd = getButtonGridData(fRemoveImportsButton); 189 fRemoveImportsButton.setLayoutData(gd); 190 fRemoveImportsButton.setFont(font); 191 fRemoveImportsButton.addSelectionListener(new SelectionListener() { 192 public void widgetSelected(SelectionEvent se) { 193 removeImports(); 194 } 195 public void widgetDefaultSelected(SelectionEvent se) { 196 } 197 }); 198 fRemoveImportsButton.setEnabled(false); 199 200 } 201 202 private GridData getButtonGridData(Button button) { 203 GridData gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 204 button.setLayoutData(gd); 205 SWTUtil.setButtonDimensionHint(button); 206 return gd; 207 } 208 209 private void removeImports() { 210 IStructuredSelection selection = (IStructuredSelection)fImportsViewer.getSelection(); 211 fImportContentProvider.removeImports(selection.toArray()); 212 } 213 214 private void addPackage() { 215 Shell shell= fAddPackageButton.getDisplay().getActiveShell(); 216 ElementListSelectionDialog dialog = null; 217 try { 218 IJavaProject project= fEditor.getJavaProject(); 219 List projects= new ArrayList (); 220 projects.add(project); 221 IPackageFragmentRoot[] roots= project.getAllPackageFragmentRoots(); 222 for (int i = 0; i < roots.length; i++) { 223 IPackageFragmentRoot root = roots[i]; 224 projects.add(root.getParent()); 225 } 226 dialog = JDIDebugUIPlugin.createAllPackagesDialog(shell, (IJavaProject[])projects.toArray(new IJavaProject[projects.size()]), false); 227 } catch (JavaModelException jme) { 228 String title= SnippetMessages.getString("SelectImportsDialog.Add_package_as_import_7"); String message= SnippetMessages.getString("SelectImportsDialog.Could_not_open_package_selection_dialog_8"); ExceptionHandler.handle(jme, title, message); 231 return; 232 } 233 if (dialog == null) { 234 return; 235 } 236 dialog.setTitle(SnippetMessages.getString("SelectImportsDialog.Add_package_as_import_7")); dialog.setMessage(SnippetMessages.getString("SelectImportsDialog.&Select_a_package_to_add_as_an_Import_10")); dialog.setMultipleSelection(true); 239 if (dialog.open() == IDialogConstants.CANCEL_ID) { 240 return; 241 } 242 Object [] packages= dialog.getResult(); 243 if (packages != null) { 244 for (int i = 0; i < packages.length; i++) { 245 IJavaElement pkg = (IJavaElement)packages[i]; 246 String filter = pkg.getElementName(); 247 filter += ".*"; fImportContentProvider.addImport(filter); 249 } 250 } 251 } 252 253 private void addType() { 254 Shell shell= fAddTypeButton.getDisplay().getActiveShell(); 255 SelectionDialog dialog= null; 256 try { 257 dialog= JavaUI.createTypeDialog(shell, PlatformUI.getWorkbench().getProgressService(), 258 SearchEngine.createJavaSearchScope(new IJavaElement[]{fEditor.getJavaProject()}, true), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, false); 259 } catch (JavaModelException jme) { 260 String title= SnippetMessages.getString("SelectImportsDialog.Add_Type_as_Import_12"); String message= SnippetMessages.getString("SelectImportsDialog.Could_not_open_class_selection_dialog_13"); ExceptionHandler.handle(jme, title, message); 263 return; 264 } 265 266 dialog.setTitle(SnippetMessages.getString("SelectImportsDialog.Add_Type_as_Import_12")); dialog.setMessage(SnippetMessages.getString("SelectImportsDialog.&Select_a_type_to_add_to_add_as_an_import_15")); if (dialog.open() == IDialogConstants.CANCEL_ID) { 269 return; 270 } 271 272 Object [] types= dialog.getResult(); 273 if (types != null && types.length > 0) { 274 IType type = (IType)types[0]; 275 fImportContentProvider.addImport(type.getFullyQualifiedName()); 276 } 277 } 278 279 282 protected Control createDialogArea(Composite parent) { 283 Font font = parent.getFont(); 284 285 Composite dialogComp = (Composite)super.createDialogArea(parent); 286 Composite outer = new Composite(dialogComp, SWT.NONE); 288 GridLayout topLayout = new GridLayout(); 289 topLayout.numColumns = 2; 290 outer.setLayout(topLayout); 291 outer.setFont(font); 292 293 setTitle(MessageFormat.format(SnippetMessages.getString("SelectImportsDialog.Manage_the_Java_Snippet_Editor_Imports_for___{0}__1"), new String []{fEditor.getEditorInput().getName()})); 295 GridData gd = new GridData(); 296 gd.verticalAlignment = GridData.FILL; 297 gd.horizontalAlignment = GridData.FILL; 298 outer.setLayoutData(gd); 299 300 fImportsTable= new Table(outer, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); 302 303 TableLayout tableLayout= new TableLayout(); 304 ColumnLayoutData[] columnLayoutData= new ColumnLayoutData[1]; 305 columnLayoutData[0]= new ColumnWeightData(100); 306 tableLayout.addColumnData(columnLayoutData[0]); 307 fImportsTable.setLayout(tableLayout); 308 fImportsTable.setFont(font); 309 new TableColumn(fImportsTable, SWT.NONE); 310 311 fImportsViewer = new TableViewer(fImportsTable); 312 fImportsViewer.setLabelProvider(new FilterLabelProvider()); 313 fImportsViewer.setComparator(new FilterViewerComparator()); 314 fImportContentProvider = new ImportsContentProvider(fImportsViewer); 315 fImportsViewer.setContentProvider(fImportContentProvider); 316 fImportsViewer.setInput(this); 318 gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 319 gd.widthHint = 100; 320 gd.heightHint= 300; 321 fImportsViewer.getTable().setLayoutData(gd); 322 fImportsViewer.addSelectionChangedListener(new ISelectionChangedListener() { 323 public void selectionChanged(SelectionChangedEvent event) { 324 ISelection selection = event.getSelection(); 325 if (selection.isEmpty()) { 326 fRemoveImportsButton.setEnabled(false); 327 } else { 328 fRemoveImportsButton.setEnabled(true); 329 } 330 } 331 }); 332 333 createImportButtons(outer); 334 applyDialogFont(outer); 335 return outer; 336 } 337 338 341 protected void okPressed() { 342 String [] imports= null; 343 Object [] results= fImportContentProvider.getElements(null); 344 if (results != null && results.length > 0) { 345 imports= new String [results.length]; 346 for (int i = 0; i < results.length; i++) { 347 Filter imprt = (Filter)results[i]; 348 imports[i]= imprt.getName(); 349 } 350 } 351 fEditor.setImports(imports); 352 super.okPressed(); 353 } 354 355 360 protected void configureShell(Shell shell) { 361 super.configureShell(shell); 362 shell.setText(SnippetMessages.getString("SelectImportsDialog.Java_Snippet_Imports_18")); } 364 } 365 | Popular Tags |