1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructure; 18 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructures; 19 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration; 20 import org.eclipse.jdt.ui.text.IJavaPartitions; 21 import org.eclipse.jdt.ui.text.JavaTextTools; 22 import org.eclipse.jface.preference.PreferencePage; 23 import org.eclipse.jface.text.Document; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.jface.viewers.ColumnLayoutData; 26 import org.eclipse.jface.viewers.ColumnWeightData; 27 import org.eclipse.jface.viewers.DoubleClickEvent; 28 import org.eclipse.jface.viewers.IColorProvider; 29 import org.eclipse.jface.viewers.IDoubleClickListener; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.IStructuredContentProvider; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.ITableLabelProvider; 35 import org.eclipse.jface.viewers.LabelProvider; 36 import org.eclipse.jface.viewers.SelectionChangedEvent; 37 import org.eclipse.jface.viewers.StructuredSelection; 38 import org.eclipse.jface.viewers.TableLayout; 39 import org.eclipse.jface.viewers.TableViewer; 40 import org.eclipse.jface.viewers.Viewer; 41 import org.eclipse.jface.viewers.ViewerComparator; 42 import org.eclipse.jface.window.Window; 43 import org.eclipse.swt.SWT; 44 import org.eclipse.swt.events.KeyAdapter; 45 import org.eclipse.swt.events.KeyEvent; 46 import org.eclipse.swt.graphics.Color; 47 import org.eclipse.swt.graphics.Image; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.widgets.Button; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Control; 52 import org.eclipse.swt.widgets.Display; 53 import org.eclipse.swt.widgets.Event; 54 import org.eclipse.swt.widgets.Listener; 55 import org.eclipse.swt.widgets.Table; 56 import org.eclipse.swt.widgets.TableColumn; 57 import org.eclipse.swt.widgets.Widget; 58 import org.eclipse.ui.IWorkbench; 59 import org.eclipse.ui.IWorkbenchPreferencePage; 60 import org.eclipse.ui.PlatformUI; 61 62 import com.ibm.icu.text.MessageFormat; 63 64 67 public class JavaLogicalStructuresPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, ISelectionChangedListener, Listener { 68 69 public class LogicalStructuresListViewerLabelProvider extends LabelProvider implements IColorProvider, ITableLabelProvider { 70 71 74 public String getColumnText(Object element, int columnIndex) { 75 JavaLogicalStructure logicalStructure= (JavaLogicalStructure) element; 76 StringBuffer buffer= new StringBuffer (); 77 if (columnIndex == 0) { 78 String qualifiedName= logicalStructure.getQualifiedTypeName(); 79 int index= qualifiedName.lastIndexOf('.') + 1; 80 String simpleName= qualifiedName.substring(index); 81 buffer.append(simpleName); 82 if (index > 0) { 83 buffer.append(" (").append(logicalStructure.getQualifiedTypeName()).append(')'); } 85 } 86 else if (columnIndex == 1) { 87 buffer.append(logicalStructure.getDescription()); 88 String pluginId= logicalStructure.getContributingPluginId(); 89 if (pluginId != null) { 90 buffer.append(MessageFormat.format(DebugUIMessages.JavaLogicalStructuresPreferencePage_8, new String [] {pluginId})); 91 } 92 } 93 return buffer.toString(); 94 } 95 96 99 public Color getForeground(Object element) { 100 return null; 101 } 102 103 106 public Color getBackground(Object element) { 107 if (element instanceof JavaLogicalStructure) { 108 if (((JavaLogicalStructure) element).isContributed()) { 109 return Display.getCurrent().getSystemColor(SWT.COLOR_INFO_BACKGROUND); 110 } 111 } 112 return null; 113 } 114 115 118 public Image getColumnImage(Object element, int columnIndex) { 119 return null; 120 } 121 } 122 123 public class LogicalStructuresListViewerContentProvider implements IStructuredContentProvider { 124 125 private List fLogicalStructures; 126 127 LogicalStructuresListViewerContentProvider() { 128 fLogicalStructures= new ArrayList (); 129 JavaLogicalStructure[] logicalStructures= JavaLogicalStructures.getJavaLogicalStructures(); 130 for (int i= 0; i < logicalStructures.length; i++) { 131 add(logicalStructures[i]); 132 } 133 } 134 135 138 public Object [] getElements(Object inputElement) { 139 return fLogicalStructures.toArray(); 140 } 141 142 145 public void dispose() { 146 } 147 148 151 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 152 } 153 154 157 public void add(JavaLogicalStructure logicalStructure) { 158 for (int i= 0, length= fLogicalStructures.size(); i < length; i++) { 159 if (!greaterThan(logicalStructure, (JavaLogicalStructure)fLogicalStructures.get(i))) { 160 fLogicalStructures.add(i, logicalStructure); 161 return; 162 } 163 } 164 fLogicalStructures.add(logicalStructure); 165 } 166 167 171 private boolean greaterThan(JavaLogicalStructure logicalStructure1, JavaLogicalStructure logicalStructure2) { 172 int res= logicalStructure1.getQualifiedTypeName().compareToIgnoreCase(logicalStructure2.getQualifiedTypeName()); 173 if (res != 0) { 174 return res > 0; 175 } 176 res= logicalStructure1.getDescription().compareToIgnoreCase(logicalStructure2.getDescription()); 177 if (res != 0) { 178 return res > 0; 179 } 180 return logicalStructure1.hashCode() > logicalStructure2.hashCode(); 181 } 182 183 186 public void remove(List list) { 187 fLogicalStructures.removeAll(list); 188 } 189 190 193 public void refresh(JavaLogicalStructure logicalStructure) { 194 fLogicalStructures.remove(logicalStructure); 195 add(logicalStructure); 196 } 197 198 public void saveUserDefinedJavaLogicalStructures() { 199 List logicalStructures= new ArrayList (); 200 for (Iterator iter = fLogicalStructures.iterator(); iter.hasNext();) { 201 JavaLogicalStructure logicalStructure= (JavaLogicalStructure) iter.next(); 202 if (!logicalStructure.isContributed()) { 203 logicalStructures.add(logicalStructure); 204 } 205 } 206 JavaLogicalStructures.setUserDefinedJavaLogicalStructures((JavaLogicalStructure[]) logicalStructures.toArray(new JavaLogicalStructure[logicalStructures.size()])); 207 } 208 209 } 210 211 private TableViewer fLogicalStructuresViewer; 212 private Button fAddLogicalStructureButton; 213 private Button fEditLogicalStructureButton; 214 private Button fRemoveLogicalStructureButton; 215 private LogicalStructuresListViewerContentProvider fLogicalStructuresContentProvider; 216 217 protected static String [] fTableColumnProperties= { 218 "type", "showAs", }; 221 protected String [] fTableColumnHeaders= { 222 DebugUIMessages.JavaLogicalStructuresPreferencePage_9, 223 DebugUIMessages.JavaLogicalStructuresPreferencePage_10, 224 }; 225 protected ColumnLayoutData[] fTableColumnLayouts= { 226 new ColumnWeightData(70), 227 new ColumnWeightData(30), 228 }; 229 private JDISourceViewer fCodeViewer; 230 231 234 public JavaLogicalStructuresPreferencePage() { 235 super(DebugUIMessages.JavaLogicalStructuresPreferencePage_0); 236 setPreferenceStore(JDIDebugUIPlugin.getDefault().getPreferenceStore()); 237 setDescription(DebugUIMessages.JavaLogicalStructuresPreferencePage_11); 238 } 239 240 243 protected Control createContents(Composite parent) { 244 Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 2, 1, GridData.FILL_BOTH, 0, 2); 245 createTable(comp); 246 createTableButtons(comp); 247 createSourceViewer(comp); 248 noDefaultAndApplyButton(); 249 PlatformUI.getWorkbench().getHelpSystem().setHelp(comp, IJavaDebugHelpContextIds.JAVA_LOGICAL_STRUCTURES_PAGE); 250 return comp; 251 } 252 253 257 public void createSourceViewer(Composite parent) { 258 SWTFactory.createWrapLabel(parent, DebugUIMessages.JavaLogicalStructuresPreferencePage_12, 2, 300); 259 260 fCodeViewer = new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 261 262 JavaTextTools tools = JDIDebugUIPlugin.getDefault().getJavaTextTools(); 263 IDocument document= new Document(); 264 tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING); 265 fCodeViewer.configure(new DisplayViewerConfiguration()); 266 fCodeViewer.setEditable(false); 267 fCodeViewer.setDocument(document); 268 269 Control control = fCodeViewer.getControl(); 270 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 271 gd.horizontalSpan = 2; 272 gd.heightHint = convertHeightInCharsToPixels(10); 273 control.setLayoutData(gd); 274 } 275 276 280 private void createTableButtons(Composite container) { 281 Composite buttonContainer = SWTFactory.createComposite(container, container.getFont(), 1, 1, GridData.VERTICAL_ALIGN_BEGINNING, 1, 0); 283 fAddLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_2, DebugUIMessages.JavaLogicalStructuresPreferencePage_3, null); 285 fAddLogicalStructureButton.addListener(SWT.Selection, this); 286 fEditLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_4, DebugUIMessages.JavaLogicalStructuresPreferencePage_5, null); 288 fEditLogicalStructureButton.addListener(SWT.Selection, this); 289 fRemoveLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_6, DebugUIMessages.JavaLogicalStructuresPreferencePage_7, null); 291 fRemoveLogicalStructureButton.addListener(SWT.Selection, this); 292 selectionChanged((IStructuredSelection)fLogicalStructuresViewer.getSelection()); 294 } 295 296 299 private void createTable(Composite parent) { 300 SWTFactory.createWrapLabel(parent, DebugUIMessages.JavaLogicalStructuresPreferencePage_1, 2, 300); 301 302 fLogicalStructuresViewer = new TableViewer(parent, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 304 Table table = (Table)fLogicalStructuresViewer.getControl(); 305 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 306 gd.heightHint= convertHeightInCharsToPixels(10); 307 gd.widthHint= convertWidthInCharsToPixels(10); 308 table.setLayoutData(gd); 309 table.setFont(parent.getFont()); 310 TableLayout tableLayout = new TableLayout(); 311 table.setLayout(tableLayout); 312 table.setHeaderVisible(true); 313 table.setLinesVisible(true); 314 315 fLogicalStructuresViewer.setColumnProperties(fTableColumnProperties); 317 for (int i = 0; i < fTableColumnHeaders.length; i++) { 318 tableLayout.addColumnData(fTableColumnLayouts[i]); 319 TableColumn column = new TableColumn(table, SWT.NONE, i); 320 column.setResizable(fTableColumnLayouts[i].resizable); 321 column.setText(fTableColumnHeaders[i]); 322 } 323 324 fLogicalStructuresContentProvider= new LogicalStructuresListViewerContentProvider(); 325 fLogicalStructuresViewer.setContentProvider(fLogicalStructuresContentProvider); 326 fLogicalStructuresViewer.setLabelProvider(new LogicalStructuresListViewerLabelProvider()); 327 fLogicalStructuresViewer.addSelectionChangedListener(this); 328 fLogicalStructuresViewer.setInput(this); 329 fLogicalStructuresViewer.addDoubleClickListener(new IDoubleClickListener() { 330 public void doubleClick(DoubleClickEvent event) { 331 IStructuredSelection selection= ((IStructuredSelection) fLogicalStructuresViewer.getSelection()); 332 if (selection.size() == 1 && !((JavaLogicalStructure) selection.getFirstElement()).isContributed()) { 333 editLogicalStructure(); 334 } 335 } 336 }); 337 table.addKeyListener(new KeyAdapter() { 338 public void keyPressed(KeyEvent event) { 339 if (event.character == SWT.DEL && event.stateMask == 0 && fRemoveLogicalStructureButton.isEnabled()) { 340 removeLogicalStrutures(); 341 } 342 } 343 }); 344 fLogicalStructuresViewer.setComparator(new ViewerComparator() { 345 public int compare(Viewer iViewer, Object e1, Object e2) { 346 if (e1 == null) { 347 return -1; 348 } else if (e2 == null) { 349 return 1; 350 } else { 351 String type1= ((JavaLogicalStructure)e1).getQualifiedTypeName(); 352 int index= type1.lastIndexOf('.') + 1; 353 if (index > 0) { 354 type1= type1.substring(index); 355 } 356 String type2= ((JavaLogicalStructure)e2).getQualifiedTypeName(); 357 index= type2.lastIndexOf('.') + 1; 358 if (index > 0) { 359 type2= type2.substring(index); 360 } 361 return type1.compareToIgnoreCase(type2); 362 } 363 } 364 }); 365 } 366 367 370 public void init(IWorkbench workbench) {} 371 372 375 public void selectionChanged(SelectionChangedEvent event) { 376 ISelection selection= event.getSelection(); 377 if (selection instanceof IStructuredSelection) { 378 selectionChanged((IStructuredSelection)selection); 379 } 380 } 381 382 385 private void selectionChanged(IStructuredSelection structuredSelection) { 386 int size= structuredSelection.size(); 387 if (size == 0) { 388 fEditLogicalStructureButton.setEnabled(false); 389 fRemoveLogicalStructureButton.setEnabled(false); 390 refreshCodeViewer(null); 391 } else { 392 JavaLogicalStructure structure = (JavaLogicalStructure)structuredSelection.getFirstElement(); 393 fEditLogicalStructureButton.setEnabled(size == 1 && !structure.isContributed()); 394 boolean removeEnabled= true; 395 for (Iterator iter= structuredSelection.iterator(); iter.hasNext();) { 396 if (((JavaLogicalStructure) iter.next()).isContributed()) { 397 removeEnabled= false; 398 } 399 } 400 fRemoveLogicalStructureButton.setEnabled(removeEnabled); 401 refreshCodeViewer(structure); 402 } 403 } 404 405 409 private void refreshCodeViewer(JavaLogicalStructure structure){ 410 StringBuffer buffer= new StringBuffer (); 411 if (structure != null){ 412 String snippet= structure.getValue(); 413 if (snippet != null) { 414 buffer.append(snippet); 415 } else { 416 String [][] variables = structure.getVariables(); 417 for (int i = 0; i < variables.length; i++) { 418 buffer.append(variables[i][0]); 419 buffer.append(" = "); buffer.append(variables[i][1]); 421 if (buffer.charAt(buffer.length() - 1) != '\n') { 422 buffer.append('\n'); 423 } 424 } 425 } 426 } 427 if (fCodeViewer != null) { 428 fCodeViewer.getDocument().set(buffer.toString()); 429 } 430 } 431 432 435 public void handleEvent(Event event) { 436 Widget source= event.widget; 437 if (source == fAddLogicalStructureButton) { 438 addLogicalStructure(); 439 } else if (source == fEditLogicalStructureButton) { 440 editLogicalStructure(); 441 } else if (source == fRemoveLogicalStructureButton) { 442 removeLogicalStrutures(); 443 } 444 } 445 446 449 protected void addLogicalStructure() { 450 JavaLogicalStructure logicalStructure= new JavaLogicalStructure("", true, "", "", new String [0][]); if (new EditLogicalStructureDialog(getShell(), logicalStructure).open() == Window.OK) { 452 fLogicalStructuresContentProvider.add(logicalStructure); 453 fLogicalStructuresViewer.refresh(); 454 fLogicalStructuresViewer.setSelection(new StructuredSelection(logicalStructure)); 455 } 456 } 457 458 461 protected void editLogicalStructure() { 462 IStructuredSelection structuredSelection= (IStructuredSelection) fLogicalStructuresViewer.getSelection(); 463 if (structuredSelection.size() == 1) { 464 JavaLogicalStructure logicalStructure= (JavaLogicalStructure)structuredSelection.getFirstElement(); 465 new EditLogicalStructureDialog(getShell(), logicalStructure).open(); 466 fLogicalStructuresContentProvider.refresh(logicalStructure); 467 fLogicalStructuresViewer.refresh(); 468 refreshCodeViewer(logicalStructure); 469 } 470 } 471 472 475 protected void removeLogicalStrutures() { 476 IStructuredSelection selection= (IStructuredSelection)fLogicalStructuresViewer.getSelection(); 477 if (selection.size() > 0) { 478 List selectedElements= selection.toList(); 479 Object [] elements= fLogicalStructuresContentProvider.getElements(null); 480 Object newSelectedElement= null; 481 for (int i= 0; i < elements.length; i++) { 482 if (!selectedElements.contains(elements[i])) { 483 newSelectedElement= elements[i]; 484 } else { 485 break; 486 } 487 } 488 fLogicalStructuresContentProvider.remove(((IStructuredSelection) fLogicalStructuresViewer.getSelection()).toList()); 489 fLogicalStructuresViewer.refresh(); 490 if (newSelectedElement == null) { 491 Object [] newElements= fLogicalStructuresContentProvider.getElements(null); 492 if (newElements.length > 0) { 493 fLogicalStructuresViewer.setSelection(new StructuredSelection(newElements[0])); 494 } 495 } else { 496 fLogicalStructuresViewer.setSelection(new StructuredSelection(newSelectedElement)); 497 } 498 } 499 500 } 501 502 505 public boolean performOk() { 506 if (fCodeViewer != null) { 507 fLogicalStructuresContentProvider.saveUserDefinedJavaLogicalStructures(); 508 fCodeViewer.dispose(); 509 } 510 return super.performOk(); 511 } 512 513 516 public boolean performCancel() { 517 if (fCodeViewer != null) { 518 fCodeViewer.dispose(); 519 } 520 return super.performCancel(); 521 } 522 } 523 | Popular Tags |