KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaLogicalStructuresPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.debug.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
65  * The preference page for creating/modifying logical structures
66  */

67 public class JavaLogicalStructuresPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, ISelectionChangedListener, Listener {
68
69     public class LogicalStructuresListViewerLabelProvider extends LabelProvider implements IColorProvider, ITableLabelProvider {
70
71         /* (non-Javadoc)
72          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
73          */

74         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
75             JavaLogicalStructure logicalStructure= (JavaLogicalStructure) element;
76             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
77             if (columnIndex == 0) {
78                 String JavaDoc qualifiedName= logicalStructure.getQualifiedTypeName();
79                 int index= qualifiedName.lastIndexOf('.') + 1;
80                 String JavaDoc simpleName= qualifiedName.substring(index);
81                 buffer.append(simpleName);
82                 if (index > 0) {
83                     buffer.append(" (").append(logicalStructure.getQualifiedTypeName()).append(')'); //$NON-NLS-1$
84
}
85             }
86             else if (columnIndex == 1) {
87                 buffer.append(logicalStructure.getDescription());
88                 String JavaDoc pluginId= logicalStructure.getContributingPluginId();
89                 if (pluginId != null) {
90                     buffer.append(MessageFormat.format(DebugUIMessages.JavaLogicalStructuresPreferencePage_8, new String JavaDoc[] {pluginId}));
91                 }
92             }
93             return buffer.toString();
94         }
95
96         /* (non-Javadoc)
97          * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
98          */

99         public Color getForeground(Object JavaDoc element) {
100             return null;
101         }
102
103         /* (non-Javadoc)
104          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
105          */

106         public Color getBackground(Object JavaDoc 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         /* (non-Javadoc)
116          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
117          */

118         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
119             return null;
120         }
121     }
122     
123     public class LogicalStructuresListViewerContentProvider implements IStructuredContentProvider {
124         
125         private List JavaDoc fLogicalStructures;
126         
127         LogicalStructuresListViewerContentProvider() {
128             fLogicalStructures= new ArrayList JavaDoc();
129             JavaLogicalStructure[] logicalStructures= JavaLogicalStructures.getJavaLogicalStructures();
130             for (int i= 0; i < logicalStructures.length; i++) {
131                 add(logicalStructures[i]);
132             }
133         }
134
135         /* (non-Javadoc)
136          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
137          */

138         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
139             return fLogicalStructures.toArray();
140         }
141
142         /* (non-Javadoc)
143          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
144          */

145         public void dispose() {
146         }
147
148         /* (non-Javadoc)
149          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
150          */

151         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
152         }
153
154         /**
155          * Add the given logical structure to the content provider.
156          */

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         /**
168          * Compare two logical structures, return <code>true</code> if the first one is 'greater' than
169          * the second one.
170          */

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         /**
184          * Remove the given logical structures from the content provider.
185          */

186         public void remove(List JavaDoc list) {
187             fLogicalStructures.removeAll(list);
188         }
189
190         /**
191          * Refresh (reorder) the given logical structure.
192          */

193         public void refresh(JavaLogicalStructure logicalStructure) {
194             fLogicalStructures.remove(logicalStructure);
195             add(logicalStructure);
196         }
197
198         public void saveUserDefinedJavaLogicalStructures() {
199             List JavaDoc logicalStructures= new ArrayList JavaDoc();
200             for (Iterator JavaDoc 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 JavaDoc[] fTableColumnProperties= {
218         "type", //$NON-NLS-1$
219
"showAs", //$NON-NLS-1$
220
};
221     protected String JavaDoc[] 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     /**
232      * Constructor
233      */

234     public JavaLogicalStructuresPreferencePage() {
235         super(DebugUIMessages.JavaLogicalStructuresPreferencePage_0);
236         setPreferenceStore(JDIDebugUIPlugin.getDefault().getPreferenceStore());
237         setDescription(DebugUIMessages.JavaLogicalStructuresPreferencePage_11);
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
242      */

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     /**
254      * Creates the source viewer
255      * @param parent the parent to add the viewer to
256      */

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     /**
277      * Creates the button group for the table of logical structures
278      * @param container the parent container to add the buttons to
279      */

280     private void createTableButtons(Composite container) {
281         // button container
282
Composite buttonContainer = SWTFactory.createComposite(container, container.getFont(), 1, 1, GridData.VERTICAL_ALIGN_BEGINNING, 1, 0);
283        // add button
284
fAddLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_2, DebugUIMessages.JavaLogicalStructuresPreferencePage_3, null);
285         fAddLogicalStructureButton.addListener(SWT.Selection, this);
286        // edit button
287
fEditLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_4, DebugUIMessages.JavaLogicalStructuresPreferencePage_5, null);
288         fEditLogicalStructureButton.addListener(SWT.Selection, this);
289        // remove button
290
fRemoveLogicalStructureButton = SWTFactory.createPushButton(buttonContainer, DebugUIMessages.JavaLogicalStructuresPreferencePage_6, DebugUIMessages.JavaLogicalStructuresPreferencePage_7, null);
291         fRemoveLogicalStructureButton.addListener(SWT.Selection, this);
292         // initialize the buttons state
293
selectionChanged((IStructuredSelection)fLogicalStructuresViewer.getSelection());
294     }
295
296     /**
297      * @param container
298      */

299     private void createTable(Composite parent) {
300         SWTFactory.createWrapLabel(parent, DebugUIMessages.JavaLogicalStructuresPreferencePage_1, 2, 300);
301         
302         // logical structures list
303
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         // create table columns
316
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 JavaDoc e1, Object JavaDoc e2) {
346                 if (e1 == null) {
347                     return -1;
348                 } else if (e2 == null) {
349                     return 1;
350                 } else {
351                     String JavaDoc type1= ((JavaLogicalStructure)e1).getQualifiedTypeName();
352                     int index= type1.lastIndexOf('.') + 1;
353                     if (index > 0) {
354                         type1= type1.substring(index);
355                     }
356                     String JavaDoc 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     /* (non-Javadoc)
368      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
369      */

370     public void init(IWorkbench workbench) {}
371
372     /* (non-Javadoc)
373      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
374      */

375     public void selectionChanged(SelectionChangedEvent event) {
376         ISelection selection= event.getSelection();
377         if (selection instanceof IStructuredSelection) {
378             selectionChanged((IStructuredSelection)selection);
379         }
380     }
381     
382     /**
383      * Modify the state of the button from the selection.
384      */

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 JavaDoc 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     /**
406      * Refreshes the code viewer after an edit has taken place
407      * @param structure the logical structure that was modified
408      */

409     private void refreshCodeViewer(JavaLogicalStructure structure){
410         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
411         if (structure != null){
412             String JavaDoc snippet= structure.getValue();
413             if (snippet != null) {
414                 buffer.append(snippet);
415             } else {
416                 String JavaDoc[][] variables = structure.getVariables();
417                 for (int i = 0; i < variables.length; i++) {
418                     buffer.append(variables[i][0]);
419                     buffer.append(" = "); //$NON-NLS-1$
420
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     /* (non-Javadoc)
433      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
434      */

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     /**
447      * Performs the add button operation
448      */

449     protected void addLogicalStructure() {
450         JavaLogicalStructure logicalStructure= new JavaLogicalStructure("", true, "", "", new String JavaDoc[0][]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
451
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     /**
459      * Performs the edit button operation
460      */

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     /**
473      * Performs the remove button operation
474      */

475     protected void removeLogicalStrutures() {
476         IStructuredSelection selection= (IStructuredSelection)fLogicalStructuresViewer.getSelection();
477         if (selection.size() > 0) {
478             List JavaDoc selectedElements= selection.toList();
479             Object JavaDoc[] elements= fLogicalStructuresContentProvider.getElements(null);
480             Object JavaDoc 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 JavaDoc[] 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     /* (non-Javadoc)
503      * @see org.eclipse.jface.preference.PreferencePage#performOk()
504      */

505     public boolean performOk() {
506         if (fCodeViewer != null) {
507             fLogicalStructuresContentProvider.saveUserDefinedJavaLogicalStructures();
508             fCodeViewer.dispose();
509         }
510         return super.performOk();
511     }
512
513     /* (non-Javadoc)
514      * @see org.eclipse.jface.preference.PreferencePage#performCancel()
515      */

516     public boolean performCancel() {
517         if (fCodeViewer != null) {
518             fCodeViewer.dispose();
519         }
520         return super.performCancel();
521     }
522 }
523
Popular Tags