KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > plugin > ui > CoverageMarkerView


1 /*
2  * Created on Apr 18, 2005
3  *
4  * written by Matthias Kempka
5  */

6 package de.uka.ipd.coverage.plugin.ui;
7
8 import java.util.*;
9 import java.util.List JavaDoc;
10
11 import org.eclipse.core.resources.IMarker;
12 import org.eclipse.core.resources.ResourcesPlugin;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.action.*;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.viewers.*;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.ui.*;
22 import org.eclipse.ui.actions.SelectionProviderAction;
23 import org.eclipse.ui.model.IWorkbenchAdapter;
24 import org.eclipse.ui.part.ViewPart;
25 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
26
27 import de.uka.ipd.coverage.plugin.CoveragePlugin;
28 import de.uka.ipd.coverage.utils.Logger;
29
30 /**
31  * Created on Apr 18, 2005
32  * @author Matthias Kempka
33  */

34 public class CoverageMarkerView extends ViewPart implements MarkerListener {
35
36     private Logger logger = new Logger(this);
37     private CoverageMarkerManager markerManager = CoveragePlugin.getDefault().getMarkerManager();
38
39     private class MListContentProvider implements IStructuredContentProvider {
40
41         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
42             return CoveragePlugin.getDefault().getMarkerManager().getPrimaryMarkers();
43         }
44
45         public void dispose() {
46             markerManager.removeMarkerListener(CoverageMarkerView.this);
47         }
48
49         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
50             logger.info("implement me: inputChanged()"); //$NON-NLS-1$
51
}
52
53         public void setSorter(TableSorter newSorter) {
54             logger.info("should set sorter!!"); //$NON-NLS-1$
55
}
56         
57     }
58
59     private class MListLabelProvider implements ITableLabelProvider {
60         
61         private List JavaDoc labelProviderListenerList = new ArrayList();
62         
63         private Image getImage(Object JavaDoc element) {
64             IMarker marker = (IMarker) element;
65             IWorkbenchAdapter wa = (IWorkbenchAdapter) marker.getAdapter(IWorkbenchAdapter.class);
66             ImageDescriptor id = wa.getImageDescriptor(element);
67             if (id != null) {
68                 return id.createImage();
69             }
70             return null;
71         }
72         
73         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
74             if (columnIndex == 0) {
75                 return getImage(element);
76             } else {
77                 return null;
78             }
79         }
80
81         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
82             return getFields()[columnIndex].getValue(element);
83         }
84
85         public void addListener(ILabelProviderListener listener) {
86             if (!labelProviderListenerList.contains(listener)) {
87                 labelProviderListenerList.add(listener);
88             }
89         }
90
91         public void dispose() {
92             //do nothing
93
}
94
95         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
96             return false;
97         }
98
99         public void removeListener(ILabelProviderListener listener) {
100             labelProviderListenerList.remove(listener);
101         }
102     }
103     
104     public void markersChanged(CoverageMarkerManager source) {
105         runInDisplayThread(new Runnable JavaDoc() {
106             public void run() {
107                 viewer.refresh();
108             }
109         });
110     }
111
112     public void markersAdded(final IMarker[] markers) {
113         runInDisplayThread(new Runnable JavaDoc() {
114             public void run() {
115                 viewer.add(markers);
116             }
117         });
118     }
119     
120     private void runInDisplayThread(Runnable JavaDoc runnable) {
121         Display display = this.viewer.getControl().getDisplay();
122         if(!display.isDisposed()) {
123             display.syncExec(runnable);
124         }
125     }
126     
127     
128
129     private MListContentProvider content;
130
131     private static final String JavaDoc TAG_COLUMN_WIDTH = "columnWidth"; //$NON-NLS-1$
132
private static final String JavaDoc TAG_VERTICAL_POSITION = "verticalPosition"; //$NON-NLS-1$
133
private static final String JavaDoc TAG_HORIZONTAL_POSITION = "horizontalPosition"; //$NON-NLS-1$
134

135     public static final String JavaDoc SORT_ACTION_ID = "sort"; //$NON-NLS-1$
136
public static final String JavaDoc FILTERS_ACTION_ID = "filters"; //$NON-NLS-1$
137

138     private final static IField[] VISIBLE_FIELDS = {
139             new FieldCoverageState(),
140             new FieldMessage(),
141             new FieldLine(),
142             new FieldCoveredUses(),
143             new FieldUncoveredUses(),
144             new FieldResource(),
145         };
146     
147     private final static ColumnLayoutData[] DEFAULT_COLUMN_LAYOUTS = {
148         new ColumnPixelData(19, false),
149         new ColumnWeightData(200),
150         new ColumnWeightData(20),
151         new ColumnWeightData(100),
152         new ColumnWeightData(100),
153         new ColumnWeightData(60)
154     };
155     
156     private TableViewer viewer;
157     private IMemento memento;
158     protected ColumnLayoutData[] columnLayouts;
159
160     protected SelectionProviderAction openAction;
161
162     private Map actions = new HashMap();
163     private ISelectionProvider selectionProvider = new SelectionProviderAdapter();
164
165     private TableSorter sorter;
166
167     /* (non-Javadoc)
168      * Method declared on IViewPart.
169      */

170     public void init(IViewSite site, IMemento memento) throws PartInitException {
171         super.init(site, memento);
172         this.memento = memento;
173     }
174     
175 // /**
176
// *
177
// */
178
// void haltTableUpdates() {
179
// content.cancelPendingChanges();
180
// }
181
//
182
// void change(Collection toRefresh) {
183
// content.change(toRefresh);
184
// }
185
//
186
// void setContents(Collection contents, IProgressMonitor mon) {
187
// content.set(contents, mon);
188
// }
189
//
190

191     protected void viewerSelectionChanged(IStructuredSelection selection) {
192             Object JavaDoc[] rawSelection = selection.toArray();
193             IMarker[] markers = new IMarker[rawSelection.length];
194             for (int idx = 0; idx < rawSelection.length; idx++) {
195                 markers[idx] = ((IMarker)rawSelection[idx]);
196             }
197             setSelection(new StructuredSelection(markers));
198             updateStatusMessage(selection);
199     }
200     
201     /**
202      * Updates that message displayed in the status line. If the
203      * selection parameter is <code>null</code> or its size is 0, the status
204      * area is blanked out. If only 1 marker is selected, the
205      * status area is updated with the contents of the message
206      * attribute of this marker. In other cases (more than one marker
207      * is selected) the status area indicates how many items have
208      * been selected.
209      * <p>
210      * This method may be overwritten.
211      * </p><p>
212      * This method is called whenever a selection changes in this view.
213      * </p>
214      * @param selection a valid selection or <code>null</code>
215      */

216     protected void updateStatusMessage(IStructuredSelection selection) {
217         String JavaDoc message = ""; //$NON-NLS-1$
218

219         if (selection == null || selection.size() == 0){
220             // Show stats on all items in the view
221
message = updateSummaryVisible();
222         } else if (selection.size() == 1) {
223             // Use the Message attribute of the marker
224
IMarker marker = (IMarker)selection.getFirstElement();
225             try {
226                 message = (String JavaDoc) marker.getAttribute("message"); //$NON-NLS-1$
227
} catch (CoreException e) {
228                 logger.log(e);
229                 e.printStackTrace();
230             }
231         } else if (selection.size() > 1) {
232             // Show stats on only those items in the selection
233
message = updateSummaryVisible();
234         }
235         getViewSite().getActionBars().getStatusLineManager().setMessage(message);
236     }
237     
238
239     /**
240      * @return the update summary
241      */

242     protected String JavaDoc updateSummaryVisible() {
243         return ""; //$NON-NLS-1$
244
}
245     
246     /* (non-Javadoc)
247      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
248      */

249     public void createPartControl(Composite parent) {
250         parent.setLayout(new FillLayout());
251         
252         viewer = new TableViewer(createTable(parent));
253         restoreColumnWidths(memento);
254         createColumns(viewer.getTable());
255
256         content = new MListContentProvider();
257         viewer.setContentProvider(content);
258         
259         viewer.setLabelProvider(new MListLabelProvider());
260     
261         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
262             public void selectionChanged(SelectionChangedEvent event) {
263                 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
264                 viewerSelectionChanged(selection);
265             }});
266     
267         setSorter(getSorter());
268         
269         //create the actions before the input is set on the viewer but after the
270
//sorter and filter are set so the actions will be enabled correctly.
271
createActions();
272
273         viewer.setInput(getViewerInput());
274         
275 // viewer.setSelection(restoreSelection(memento));
276

277         Scrollable scrollable = (Scrollable) viewer.getControl();
278         ScrollBar bar = scrollable.getVerticalBar();
279         if (bar != null) {
280             bar.setSelection(restoreVerticalScrollBarPosition(memento));
281         }
282         bar = scrollable.getHorizontalBar();
283         if (bar != null) {
284             bar.setSelection(restoreHorizontalScrollBarPosition(memento));
285         }
286         
287         MenuManager mgr = initContextMenu();
288         Menu menu = mgr.createContextMenu(viewer.getControl());
289         viewer.getControl().setMenu(menu);
290         getSite().registerContextMenu(mgr, getSelectionProvider());
291         
292         getSite().setSelectionProvider(getSelectionProvider());
293         
294         initActionBars(getViewSite().getActionBars());
295 // registerGlobalActions(getViewSite().getActionBars());
296

297         viewer.addOpenListener(new IOpenListener() {
298             public void open(OpenEvent event) {
299                 handleOpenEvent(event);
300             }
301         });
302 // viewer.getControl().addKeyListener(new KeyAdapter() {
303
// public void keyPressed(KeyEvent e) {
304
// handleKeyPressed(e);
305
// }
306
// });
307

308         markerManager.addMarkerListener(this);
309         
310         CoveragePlugin.getDefault().setMarkerView(this);
311     }
312     
313     private ISelectionProvider getSelectionProvider() {
314         return selectionProvider;
315     }
316
317     /**
318      * @param selection
319      */

320     protected void setSelection(IStructuredSelection selection) {
321         getSelectionProvider().setSelection(selection);
322     }
323
324     /**
325      * @param sorter2
326      */

327     void setSorter(TableSorter sorter2) {
328         TableSorter newSorter = new TableSorter(sorter2);
329         
330         sorter = newSorter;
331         content.setSorter(newSorter);
332 // newSorter.saveState(getDialogSettings());
333
sorterChanged();
334     }
335
336     /* (non-Javadoc)
337      * @see org.eclipse.ui.IWorkbenchPart#dispose()
338      */

339     public void dispose() {
340         openAction.dispose();
341         CoveragePlugin.getDefault().setMarkerView(null);
342     }
343     
344     /**
345      * Creates the table control.
346      */

347     protected Table createTable(Composite parent) {
348         Table table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
349         table.setLinesVisible(true);
350         table.setFont(parent.getFont());
351         return table;
352     }
353     
354     protected void createColumns(final Table table) {
355 // SelectionListener headerListener = getHeaderListener();
356
TableLayout layout = new TableLayout();
357         table.setLayout(layout);
358         table.setHeaderVisible(true);
359         final IField[] fields = getFields();
360         ColumnLayoutData[] columnWidths = columnLayouts;
361         if (columnWidths == null) {
362             columnWidths = getDefaultColumnLayouts();
363         }
364         for (int i = 0; i < fields.length; i++) {
365             if (columnWidths == null || i >= columnWidths.length || columnWidths[i] == null) {
366                 layout.addColumnData(new ColumnPixelData(504 / fields.length, true));
367             }
368             else {
369                 layout.addColumnData(columnWidths[i]);
370             }
371             TableColumn tc = new TableColumn(table, SWT.NONE,i);
372             tc.setText(fields[i].getColumnHeaderText());
373             tc.setImage(fields[i].getColumnHeaderImage());
374 // tc.addSelectionListener(headerListener);
375
}
376     }
377     
378     protected void createActions() {
379         openAction = new ActionOpenMarker(this, getSelectionProvider());
380     }
381     
382     protected IAction getAction(String JavaDoc id) {
383         return (IAction) actions.get(id);
384     }
385     
386     protected void putAction(String JavaDoc id, IAction action) {
387         actions.put(id, action);
388     }
389     
390     protected MenuManager initContextMenu() {
391         MenuManager mgr = new MenuManager();
392         mgr.setRemoveAllWhenShown(true);
393         mgr.addMenuListener(new IMenuListener() {
394             public void menuAboutToShow(IMenuManager mgr) {
395                 
396                 getViewer().cancelEditing();
397                 fillContextMenu(mgr);
398             }
399         });
400         return mgr;
401     }
402     
403 // protected abstract void initToolBar(IToolBarManager tbm);
404

405     protected void initActionBars(IActionBars actionBars) {
406         initMenu(actionBars.getMenuManager());
407         initToolBar(actionBars.getToolBarManager());
408     }
409     
410     private void initToolBar(IToolBarManager manager) {
411         IAction showDefAction = new Action() {
412             public void run() {
413                 CoveragePlugin.getDefault().getMarkerManager().enablePrimaryMarkers();
414             }
415         };
416         showDefAction.setText(Messages.getString("CoverageMarkerView.2")); //$NON-NLS-1$
417
manager.add(showDefAction);
418     }
419
420     protected void initMenu(IMenuManager menu) {
421         IAction sortAction = getAction(SORT_ACTION_ID);
422         if (sortAction != null)
423             menu.add(sortAction);
424         IAction filtersAction = getAction(FILTERS_ACTION_ID);
425         if (filtersAction != null)
426             menu.add(filtersAction);
427     }
428
429 // protected abstract void registerGlobalActions(IActionBars actionBars);
430
//
431
protected void fillContextMenu(IMenuManager manager) {
432         System.out.println("implement me: CoverageMarkerView.fillContextMenu()"); //$NON-NLS-1$
433
}
434     
435     public void setFocus() {
436         TableViewer viewer = getViewer();
437         if (viewer != null && !viewer.getControl().isDisposed()) {
438             Runnable JavaDoc focusSetter = new Runnable JavaDoc() {
439                 public void run() {
440                     getViewer().getControl().setFocus();
441                 }
442             };
443             runInDisplayThread(focusSetter);
444         }
445     }
446
447     protected TableSorter getSorter() {
448         if (sorter == null) {
449             int[] priorities = new int[getFields().length];
450             int[] directions = new int[getFields().length];
451             for (int i = 0; i < getFields().length; i++) {
452                 priorities[i] = i;
453             }
454             Arrays.fill(directions, TableSorter.ASCENDING);
455             sorter = new TableSorter(getFields(), priorities, directions);
456             sorter.setTopPriority(getFields()[2]);
457 // sorter.restoreState(getDialogSettings());
458
}
459         return sorter;
460     }
461     
462     //protected abstract ITableViewContentProvider getContentProvider();
463

464     protected IField[] getFields() {
465         return VISIBLE_FIELDS;
466     }
467     
468     protected Object JavaDoc getViewerInput() {
469         return ResourcesPlugin.getWorkspace().getRoot();
470     }
471
472
473 // protected abstract IDialogSettings getDialogSettings();
474

475     protected TableViewer getViewer() {
476         return viewer;
477     }
478     
479 // protected SelectionListener getHeaderListener() {
480
// return new SelectionAdapter() {
481
// /**
482
// * Handles the case of user selecting the
483
// * header area.
484
// */
485
// public void widgetSelected(SelectionEvent e) {
486
//
487
// int column = getViewer().getTable().indexOf((TableColumn) e.widget);
488
// if (column == getSorter().getTopPriority())
489
// getSorter().reverseTopPriority();
490
// else {
491
// getSorter().setTopPriority(column);
492
// }
493
// setSorter(getSorter());
494
// }
495
// };
496
// }
497

498     protected ColumnLayoutData[] getDefaultColumnLayouts() {
499         return DEFAULT_COLUMN_LAYOUTS;
500     }
501     
502 // protected TableSortDialog getSortDialog() {
503
// if (getSorter() != null) {
504
// return new TableSortDialog(getSite().getShell(), getSorter());
505
// }
506
// return null;
507
// }
508

509     protected void sorterChanged() {
510         viewer.setSorter(getSorter());
511         
512         final TableViewer viewer = getViewer();
513         if (viewer == null) {
514             return;
515         }
516         
517         getSite().getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
518             public void run() {
519                 viewer.getControl().setRedraw(false);
520                 viewer.refresh(false);
521                 viewer.getControl().setRedraw(true);
522             }
523         });
524     }
525     
526 // protected abstract void handleKeyPressed(KeyEvent event);
527

528     protected void handleOpenEvent(OpenEvent event) {
529         openAction.run();
530     }
531
532     /* (non-Javadoc)
533      * @see org.eclipse.ui.part.ViewPart#saveState(org.eclipse.ui.IMemento)
534      */

535     public void saveState(IMemento memento) {
536         super.saveState(memento);
537
538         //save column widths
539
TableColumn[] columns = viewer.getTable().getColumns();
540         for (int i = 0; i < columns.length; i++) {
541             memento.putInteger(TAG_COLUMN_WIDTH + i, columns[i].getWidth());
542         }
543         
544 // saveSelection(memento);
545

546         //save vertical position
547
Scrollable scrollable = (Scrollable) viewer.getControl();
548         ScrollBar bar = scrollable.getVerticalBar();
549         int position = (bar != null) ? bar.getSelection() : 0;
550         memento.putInteger(TAG_VERTICAL_POSITION, position);
551         //save horizontal position
552
bar = scrollable.getHorizontalBar();
553         position = (bar != null) ? bar.getSelection() : 0;
554         memento.putInteger(TAG_HORIZONTAL_POSITION, position);
555     }
556     
557     private void restoreColumnWidths(IMemento memento) {
558         if (memento == null) {
559             return;
560         }
561         columnLayouts = new ColumnLayoutData[getFields().length];
562         for (int i = 0; i < columnLayouts.length; i++) {
563             Integer JavaDoc width = memento.getInteger(TAG_COLUMN_WIDTH + i);
564             if (width == null) {
565                 columnLayouts = null;
566                 break;
567             }
568             else {
569                 columnLayouts[i] = new ColumnPixelData(width.intValue(), true);
570             }
571         }
572     }
573     
574     private int restoreVerticalScrollBarPosition(IMemento memento) {
575         if (memento == null) {
576             return 0;
577         }
578         Integer JavaDoc position = memento.getInteger(TAG_VERTICAL_POSITION);
579         return (position == null) ? 0 : position.intValue();
580     }
581     
582     private int restoreHorizontalScrollBarPosition(IMemento memento) {
583         if (memento == null) {
584             return 0;
585         }
586         Integer JavaDoc position = memento.getInteger(TAG_HORIZONTAL_POSITION);
587         return (position == null) ? 0 : position.intValue();
588     }
589     
590     /**
591      * Get the IWorkbenchSiteProgressService for the receiver.
592      * @return IWorkbenchSiteProgressService or <code>null</code>.
593      */

594     protected IWorkbenchSiteProgressService getProgressService() {
595         IWorkbenchSiteProgressService service = null;
596         Object JavaDoc siteService =
597             getSite().getAdapter(IWorkbenchSiteProgressService.class);
598         if(siteService != null)
599             service = (IWorkbenchSiteProgressService) siteService;
600         return service;
601     }
602
603 }
604
Popular Tags