KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ProblemView


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Sebastian Davids <sdavids@gmx.de>
11  * - Fix for Bug 109361 [Markers] Multiselection in problems view yields invalid status message
12  *******************************************************************************/

13
14 package org.eclipse.ui.views.markers.internal;
15
16 import com.ibm.icu.text.MessageFormat;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.IToolBarManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.commands.ActionHandler;
33 import org.eclipse.jface.dialogs.IDialogSettings;
34 import org.eclipse.jface.viewers.ColumnLayoutData;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.TableLayout;
37 import org.eclipse.jface.viewers.ViewerComparator;
38 import org.eclipse.osgi.util.NLS;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.XMLMemento;
44 import org.eclipse.ui.activities.ActivityManagerEvent;
45 import org.eclipse.ui.activities.IActivityManagerListener;
46 import org.eclipse.ui.handlers.IHandlerActivation;
47 import org.eclipse.ui.handlers.IHandlerService;
48 import org.eclipse.ui.internal.ide.IDEInternalPreferences;
49 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
50 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
51
52 /**
53  * The ProblemView is the view that displays problem markers.
54  *
55  */

56 public class ProblemView extends MarkerView {
57
58     private final static String JavaDoc[] ROOT_TYPES = { IMarker.PROBLEM };
59
60     private final static String JavaDoc TAG_DIALOG_SECTION = "org.eclipse.ui.views.problem"; //$NON-NLS-1$
61

62     private static final String JavaDoc TAG_SYSTEM_FILTER_ENTRY = "systemFilter";//$NON-NLS-1$
63

64     private ActionResolveMarker resolveMarkerAction;
65
66     private IHandlerService handlerService;
67
68     private IHandlerActivation resolveMarkerHandlerActivation;
69
70     private IActivityManagerListener activityManagerListener;
71
72     private IField severityAndMessage = new FieldSeverityAndMessage();
73
74     private IField folder = new FieldFolder();
75
76     private IField resource = new FieldResource();
77
78     private IField lineNumber = new FieldLineNumber();
79
80     private IField creationTime = new FieldCreationTime();
81
82     // Add the marker ID so the table sorter won't reduce
83
// errors on the same line bug 82502
84
private static IField id = new FieldId();
85
86     private class GroupingAction extends Action {
87
88         IField groupingField;
89
90         ProblemView problemView;
91
92         /**
93          * Create a new instance of the receiver.
94          *
95          * @param label
96          * @param field
97          * @param view
98          */

99         public GroupingAction(String JavaDoc label, IField field, ProblemView view) {
100             super(label, IAction.AS_RADIO_BUTTON);
101
102             groupingField = field;
103             problemView = view;
104             IField categoryField = view.getMarkerAdapter().getCategorySorter()
105                     .getCategoryField();
106             if (categoryField == null) {
107                 setChecked(groupingField == null);
108             } else {
109                 setChecked(categoryField.equals(groupingField));
110             }
111         }
112
113         /*
114          * (non-Javadoc)
115          *
116          * @see org.eclipse.jface.action.Action#run()
117          */

118         public void run() {
119
120             if (isChecked()) {
121                 Job categoryJob = new Job(
122                         MarkerMessages.ProblemView_UpdateCategoryJob) {
123                     /*
124                      * (non-Javadoc)
125                      *
126                      * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
127                      */

128                     protected IStatus run(IProgressMonitor monitor) {
129                         try {
130                             markerProcessJob.join();
131                         } catch (InterruptedException JavaDoc e) {
132                             return Status.CANCEL_STATUS;
133                         }
134                         problemView.selectCategoryField(groupingField,
135                                 problemView.getMarkerAdapter()
136                                         .getCategorySorter());
137
138                         getMarkerAdapter().getCategorySorter().saveState(
139                                 getDialogSettings());
140                         return Status.OK_STATUS;
141                     }
142                 };
143                 categoryJob.setSystem(true);
144                 problemView.preserveSelection();
145
146                 IWorkbenchSiteProgressService progressService = getProgressService();
147                 if (progressService == null)
148                     categoryJob.schedule();
149                 else
150                     getProgressService().schedule(categoryJob);
151
152             }
153
154         }
155     }
156
157     /**
158      * Return a new instance of the receiver.
159      */

160     public ProblemView() {
161         super();
162         creationTime.setShowing(false);
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see org.eclipse.ui.views.markers.internal.MarkerView#dispose()
169      */

170     public void dispose() {
171         if (resolveMarkerAction != null) {
172             resolveMarkerAction.dispose();
173         }
174         if (resolveMarkerHandlerActivation != null && handlerService != null) {
175             handlerService.deactivateHandler(resolveMarkerHandlerActivation);
176         }
177
178         PlatformUI.getWorkbench().getActivitySupport().getActivityManager()
179                 .removeActivityManagerListener(activityManagerListener);
180         super.dispose();
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.ui.views.markers.internal.TableView#getSortingFields()
187      */

188     protected IField[] getSortingFields() {
189         return new IField[] { severityAndMessage, folder, resource, lineNumber,
190                 creationTime,
191                 // Add the marker ID so the table sorter won't reduce
192
// errors on the same line bug 82502
193
id };
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.eclipse.ui.views.markers.internal.TableView#getDialogSettings()
200      */

201     protected IDialogSettings getDialogSettings() {
202         IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault()
203                 .getDialogSettings();
204         IDialogSettings settings = workbenchSettings
205                 .getSection(TAG_DIALOG_SECTION);
206
207         if (settings == null) {
208             settings = workbenchSettings.addNewSection(TAG_DIALOG_SECTION);
209         }
210
211         return settings;
212     }
213
214     /*
215      * (non-Javadoc)
216      *
217      * @see org.eclipse.ui.views.markers.internal.TableView#createActions()
218      */

219     protected void createActions() {
220         super.createActions();
221         propertiesAction = new ActionProblemProperties(this, getViewer());
222         resolveMarkerAction = new ActionResolveMarker(this, getViewer());
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see org.eclipse.ui.views.internal.tableview.TableView#registerGlobalActions(org.eclipse.ui.IActionBars)
229      */

230     protected void registerGlobalActions(IActionBars actionBars) {
231         super.registerGlobalActions(actionBars);
232
233         String JavaDoc quickFixId = "org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"; //$NON-NLS-1$
234
resolveMarkerAction.setActionDefinitionId(quickFixId);
235
236         handlerService = (IHandlerService) getViewSite().getService(
237                 IHandlerService.class);
238         if (handlerService != null) {
239             resolveMarkerHandlerActivation = handlerService.activateHandler(
240                     quickFixId, new ActionHandler(resolveMarkerAction));
241         }
242     }
243
244     /*
245      * (non-Javadoc)
246      *
247      * @see org.eclipse.ui.views.markers.internal.MarkerView#fillContextMenuAdditions(org.eclipse.jface.action.IMenuManager)
248      */

249     protected void fillContextMenuAdditions(IMenuManager manager) {
250         manager.add(new Separator());
251         manager.add(resolveMarkerAction);
252     }
253
254     protected String JavaDoc[] getRootTypes() {
255         return ROOT_TYPES;
256     }
257
258     /*
259      * (non-Javadoc)
260      *
261      * @see org.eclipse.ui.views.markers.internal.TableView#getAllFields()
262      */

263     protected IField[] getAllFields() {
264
265         // Add the marker ID so the table sorter won't reduce
266
// errors on the same line bug 82502
267
return new IField[] { severityAndMessage, resource, folder, lineNumber,
268                 creationTime };
269     }
270
271     void updateTitle() {
272         MarkerList visibleMarkers = getVisibleMarkers();
273         String JavaDoc breakdown = formatSummaryBreakDown(visibleMarkers);
274         int filteredCount = visibleMarkers.getItemCount();
275         int totalCount = getTotalMarkers();
276         if (filteredCount != totalCount) {
277             breakdown = NLS.bind(MarkerMessages.problem_filter_matchedMessage,
278                     new Object JavaDoc[] { breakdown, new Integer JavaDoc(filteredCount),
279                             new Integer JavaDoc(totalCount) });
280         }
281         setContentDescription(breakdown);
282     }
283
284     private String JavaDoc formatSummaryBreakDown(MarkerList visibleMarkers) {
285         return MessageFormat.format(
286                 MarkerMessages.problem_statusSummaryBreakdown, new Object JavaDoc[] {
287                         new Integer JavaDoc(visibleMarkers.getErrors()),
288                         new Integer JavaDoc(visibleMarkers.getWarnings()),
289                         new Integer JavaDoc(visibleMarkers.getInfos()) });
290     }
291
292     private String JavaDoc getSummary(MarkerList markers) {
293         String JavaDoc message = MessageFormat.format(
294                 MarkerMessages.marker_statusSummarySelected, new Object JavaDoc[] {
295                         new Integer JavaDoc(markers.getItemCount()),
296                         formatSummaryBreakDown(markers) });
297         return message;
298     }
299
300     /**
301      * Retrieves statistical information (the total number of markers with each
302      * severity type) for the markers contained in the selection passed in. This
303      * information is then massaged into a string which may be displayed by the
304      * caller.
305      *
306      * @param selection
307      * a valid selection or <code>null</code>
308      * @return a message ready for display
309      */

310     protected String JavaDoc updateSummarySelected(IStructuredSelection selection) {
311         Collection JavaDoc selectionList;
312
313         selectionList = new ArrayList JavaDoc();
314         Iterator JavaDoc selectionIterator = selection.iterator();
315         while (selectionIterator.hasNext()) {
316             MarkerNode next = (MarkerNode) selectionIterator.next();
317             if (next.isConcrete()) {
318                 selectionList.add(next);
319             }
320         }
321
322         return getSummary(new MarkerList(selectionList));
323     }
324
325     /*
326      * (non-Javadoc)
327      *
328      * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerTypes()
329      */

330     protected String JavaDoc[] getMarkerTypes() {
331         return new String JavaDoc[] { IMarker.PROBLEM };
332     }
333
334     protected String JavaDoc getStaticContextId() {
335         return PlatformUI.PLUGIN_ID + ".problem_view_context";//$NON-NLS-1$
336
}
337
338     /*
339      * (non-Javadoc)
340      *
341      * @see org.eclipse.ui.views.markers.internal.MarkerView#createFiltersDialog()
342      */

343     protected DialogMarkerFilter createFiltersDialog() {
344
345         MarkerFilter[] filters = getUserFilters();
346         ProblemFilter[] problemFilters = new ProblemFilter[filters.length];
347         System.arraycopy(filters, 0, problemFilters, 0, filters.length);
348         return new DialogProblemFilter(getSite().getShell(), problemFilters);
349     }
350
351     /*
352      * (non-Javadoc)
353      *
354      * @see org.eclipse.ui.views.markers.internal.MarkerView#createFilter(java.lang.String)
355      */

356     protected MarkerFilter createFilter(String JavaDoc name) {
357         return new ProblemFilter(name);
358     }
359
360     /*
361      * (non-Javadoc)
362      *
363      * @see org.eclipse.ui.views.markers.internal.MarkerView#getSectionTag()
364      */

365     protected String JavaDoc getSectionTag() {
366         return TAG_DIALOG_SECTION;
367     }
368
369     /*
370      * (non-Javadoc)
371      *
372      * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerEnablementPreferenceName()
373      */

374     String JavaDoc getMarkerEnablementPreferenceName() {
375         return IDEInternalPreferences.LIMIT_PROBLEMS;
376     }
377
378     /*
379      * (non-Javadoc)
380      *
381      * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerLimitPreferenceName()
382      */

383     String JavaDoc getMarkerLimitPreferenceName() {
384         return IDEInternalPreferences.PROBLEMS_LIMIT;
385     }
386
387     /*
388      * (non-Javadoc)
389      *
390      * @see org.eclipse.ui.views.markers.internal.MarkerView#getFiltersPreferenceName()
391      */

392     String JavaDoc getFiltersPreferenceName() {
393         return IDEInternalPreferences.PROBLEMS_FILTERS;
394     }
395
396     /*
397      * (non-Javadoc)
398      *
399      * @see org.eclipse.ui.views.markers.internal.MarkerView#getAllFilters()
400      */

401     MarkerFilter[] getAllFilters() {
402         MarkerFilter[] userFilters = super.getAllFilters();
403         Collection JavaDoc declaredFilters = MarkerSupportRegistry.getInstance()
404                 .getRegisteredFilters();
405         Iterator JavaDoc iterator = declaredFilters.iterator();
406
407         MarkerFilter[] allFilters = new MarkerFilter[userFilters.length
408                 + declaredFilters.size()];
409         System.arraycopy(userFilters, 0, allFilters, 0, userFilters.length);
410         int index = userFilters.length;
411
412         while (iterator.hasNext()) {
413             allFilters[index] = (MarkerFilter) iterator.next();
414             index++;
415         }
416         return allFilters;
417
418     }
419
420     /*
421      * (non-Javadoc)
422      *
423      * @see org.eclipse.ui.views.markers.internal.MarkerView#addDropDownContributions(org.eclipse.jface.action.IMenuManager)
424      */

425     void addDropDownContributions(IMenuManager menu) {
426
427         MenuManager groupByMenu = new MenuManager(
428                 MarkerMessages.ProblemView_GroupByMenu);
429         groupByMenu.add(new GroupingAction(MarkerMessages.ProblemView_Type,
430                 new FieldCategory(), this));
431
432         Iterator JavaDoc definedGroups = MarkerSupportRegistry.getInstance()
433                 .getMarkerGroups().iterator();
434
435         while (definedGroups.hasNext()) {
436             FieldMarkerGroup group = (FieldMarkerGroup) definedGroups.next();
437             groupByMenu.add(new GroupingAction(group.getDescription(), group,
438                     this));
439         }
440
441         groupByMenu.add(new GroupingAction(MarkerMessages.ProblemView_None,
442                 null, this));
443         menu.add(groupByMenu);
444
445         super.addDropDownContributions(menu);
446     }
447
448     /**
449      * Resize the category column in the table.
450      */

451     protected void regenerateLayout() {
452         TableLayout layout = new TableLayout();
453         getViewer().getTree().setLayout(layout);
454
455         ColumnLayoutData[] columnWidths = getDefaultColumnLayouts();
456         for (int i = 0; i < columnWidths.length; i++) {
457             layout.addColumnData(columnWidths[i]);
458
459         }
460         getViewer().getTree().layout(true);
461
462     }
463
464     /*
465      * (non-Javadoc)
466      *
467      * @see org.eclipse.ui.views.markers.internal.TableView#setSorter(org.eclipse.ui.views.markers.internal.TableSorter)
468      */

469     void setComparator(TableComparator sorter2) {
470         getMarkerAdapter().getCategorySorter().setTableSorter(sorter2);
471         getMarkerAdapter().getCategorySorter().saveState(getDialogSettings());
472         updateForNewComparator(sorter2);
473
474     }
475
476     /*
477      * (non-Javadoc)
478      *
479      * @see org.eclipse.ui.views.markers.internal.MarkerView#getTableSorter()
480      */

481     public TableComparator getTableSorter() {
482         return ((CategoryComparator) getViewer().getComparator()).innerSorter;
483     }
484
485     /*
486      * (non-Javadoc)
487      *
488      * @see org.eclipse.ui.views.markers.internal.MarkerView#createPartControl(org.eclipse.swt.widgets.Composite)
489      */

490     public void createPartControl(Composite parent) {
491         super.createPartControl(parent);
492         createActivityManagerListener();
493         PlatformUI.getWorkbench().getActivitySupport().getActivityManager()
494                 .addActivityManagerListener(activityManagerListener);
495     }
496
497     /**
498      * Create a new listener for activity changes.
499      */

500     private void createActivityManagerListener() {
501         activityManagerListener = new IActivityManagerListener() {
502             /*
503              * (non-Javadoc)
504              *
505              * @see org.eclipse.ui.activities.IActivityManagerListener#activityManagerChanged(org.eclipse.ui.activities.ActivityManagerEvent)
506              */

507             public void activityManagerChanged(
508                     ActivityManagerEvent activityManagerEvent) {
509                 clearEnabledFilters();
510                 refreshViewer();
511             }
512         };
513
514     }
515
516     /**
517      * Return the field whose description matches description.
518      *
519      * @param description
520      * @return IField
521      */

522     public IField findField(String JavaDoc description) {
523         IField[] fields = getSortingFields();
524         for (int i = 0; i < fields.length; i++) {
525             if (fields[i].getDescription().equals(description)) {
526                 return fields[i];
527             }
528         }
529         return null;
530     }
531
532     /*
533      * (non-Javadoc)
534      *
535      * @see org.eclipse.ui.views.markers.internal.TableView#buildComparator()
536      */

537     protected ViewerComparator buildComparator() {
538
539         TableComparator sorter = createTableComparator();
540         CategoryComparator category = new CategoryComparator(sorter);
541         category.restoreState(getDialogSettings(), this);
542         return category;
543     }
544
545     /*
546      * (non-Javadoc)
547      *
548      * @see org.eclipse.ui.views.markers.internal.MarkerView#canBeEditable()
549      */

550     boolean canBeEditable() {
551         return false;
552     }
553
554     /*
555      * (non-Javadoc)
556      *
557      * @see org.eclipse.ui.views.markers.internal.MarkerView#initToolBar(org.eclipse.jface.action.IToolBarManager)
558      */

559     protected void initToolBar(IToolBarManager tbm) {
560         tbm.add(getFilterAction());
561         tbm.update(false);
562     }
563
564     /**
565      * Select the category for the receiver.
566      *
567      * @param description
568      * @param sorter -
569      * the sorter to select for
570      */

571     public void selectCategory(String JavaDoc description, CategoryComparator sorter) {
572
573         if (description == null)
574             selectCategoryField(null, sorter);
575
576         Iterator JavaDoc definedGroups = MarkerSupportRegistry.getInstance()
577                 .getMarkerGroups().iterator();
578         while (definedGroups.hasNext()) {
579             FieldMarkerGroup group = (FieldMarkerGroup) definedGroups.next();
580             if (group.getDescription().equals(description)) {
581                 selectCategoryField(group, sorter);
582                 return;
583             }
584         }
585         selectCategoryField(null, sorter);
586
587     }
588
589     /**
590      * Select the field groupingField.
591      *
592      * @param groupingField
593      * @param sorter
594      */

595     void selectCategoryField(IField groupingField, CategoryComparator sorter) {
596         sorter.setCategoryField(groupingField);
597         
598         //Do not refresh if the input has not been set yet
599
if (getMarkerAdapter() != null) {
600             getMarkerAdapter().getCurrentMarkers().clearGroups();
601             refreshViewer();
602         }
603     }
604
605     /*
606      * (non-Javadoc)
607      *
608      * @see org.eclipse.ui.views.markers.internal.MarkerView#writeFiltersSettings(org.eclipse.ui.XMLMemento)
609      */

610     protected void writeFiltersSettings(XMLMemento memento) {
611         super.writeFiltersSettings(memento);
612
613         // Add the system filters
614
Iterator JavaDoc filters = MarkerSupportRegistry.getInstance()
615                 .getRegisteredFilters().iterator();
616
617         while (filters.hasNext()) {
618             MarkerFilter filter = (MarkerFilter) filters.next();
619             IMemento child = memento.createChild(TAG_SYSTEM_FILTER_ENTRY,
620                     filter.getName());
621             child.putString(MarkerFilter.TAG_ENABLED, String.valueOf(filter
622                     .isEnabled()));
623         }
624
625     }
626
627     /*
628      * (non-Javadoc)
629      *
630      * @see org.eclipse.ui.views.markers.internal.MarkerView#restoreFilters(org.eclipse.ui.IMemento)
631      */

632     void restoreFilters(IMemento memento) {
633
634         super.restoreFilters(memento);
635
636         if (memento == null)
637             return;
638
639         IMemento[] sections = memento.getChildren(TAG_SYSTEM_FILTER_ENTRY);
640
641         Collection JavaDoc registered = MarkerSupportRegistry.getInstance()
642                 .getRegisteredFilters();
643         MarkerFilter[] filters = new MarkerFilter[registered.size()];
644         registered.toArray(filters);
645
646         if (sections != null) {
647
648             for (int i = 0; i < sections.length; i++) {
649                 String JavaDoc filterName = sections[i].getID();
650                 boolean enabled = Boolean.valueOf(
651                         sections[i].getString(MarkerFilter.TAG_ENABLED))
652                         .booleanValue();
653                 setEnablement(filterName, enabled, filters);
654
655             }
656         }
657
658     }
659
660     /**
661      * Set the enablement state of the filter called filterName to enabled.
662      *
663      * @param filterName
664      * @param enabled
665      * @param filters
666      */

667     private void setEnablement(String JavaDoc filterName, boolean enabled,
668             MarkerFilter[] filters) {
669         for (int i = 0; i < filters.length; i++) {
670             if (filters[i].getName().equals(filterName)) {
671                 filters[i].setEnabled(enabled);
672                 return;
673             }
674         }
675
676     }
677
678     /*
679      * (non-Javadoc)
680      *
681      * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerName()
682      */

683     protected String JavaDoc getMarkerName() {
684         return MarkerMessages.problem_title;
685     }
686 }
687
Popular Tags