KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > RefactoringStatusViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ltk.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
16 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
17 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
18
19 import org.eclipse.ltk.internal.ui.refactoring.util.PixelConverter;
20 import org.eclipse.ltk.internal.ui.refactoring.util.ViewerPane;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.accessibility.AccessibleAdapter;
24 import org.eclipse.swt.accessibility.AccessibleEvent;
25 import org.eclipse.swt.custom.SashForm;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.TableColumn;
34
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.action.ToolBarManager;
37 import org.eclipse.jface.resource.JFaceResources;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.SelectionChangedEvent;
42 import org.eclipse.jface.viewers.StructuredSelection;
43 import org.eclipse.jface.viewers.TableViewer;
44 import org.eclipse.jface.viewers.Viewer;
45 import org.eclipse.jface.viewers.ViewerComparator;
46
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.part.PageBook;
49
50 import org.eclipse.compare.CompareUI;
51
52 import org.eclipse.ltk.ui.refactoring.IStatusContextViewer;
53
54 public class RefactoringStatusViewer extends SashForm {
55
56     private static class NullContextViewer implements IStatusContextViewer {
57         private Label fLabel;
58         public NullContextViewer() {
59         }
60         public void createControl(Composite parent) {
61             fLabel= new Label(parent, SWT.CENTER | SWT.FLAT);
62             fLabel.setText(RefactoringUIMessages.ErrorWizardPage_no_context_information_available);
63         }
64         public void setInput(RefactoringStatusContext input) {
65             // do nothing
66
}
67         public Control getControl() {
68             return fLabel;
69         }
70     }
71     
72     private class NextProblem extends Action {
73         public NextProblem() {
74             setImageDescriptor(CompareUI.DESC_ETOOL_NEXT);
75             setDisabledImageDescriptor(CompareUI.DESC_DTOOL_NEXT);
76             setHoverImageDescriptor(CompareUI.DESC_CTOOL_NEXT);
77             setToolTipText(RefactoringUIMessages.ErrorWizardPage_next_Change);
78             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IRefactoringHelpContextIds.NEXT_PROBLEM_ACTION);
79         }
80         public void run() {
81             revealElement(true);
82         }
83         public void update() {
84             boolean enabled= false;
85             if (fStatus != null && fStatus.hasEntries()) {
86                 RefactoringStatusEntry[] entries= fStatus.getEntries();
87                 int index= fTableViewer.getTable().getSelectionIndex();
88                 enabled= index == -1 || index < entries.length - 1;
89             }
90             setEnabled(enabled);
91         }
92     }
93     
94     private class PreviousProblem extends Action {
95         public PreviousProblem() {
96             setImageDescriptor(CompareUI.DESC_ETOOL_PREV);
97             setDisabledImageDescriptor(CompareUI.DESC_DTOOL_PREV);
98             setHoverImageDescriptor(CompareUI.DESC_CTOOL_PREV);
99             setToolTipText(RefactoringUIMessages.ErrorWizardPage_previous_Change);
100             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IRefactoringHelpContextIds.PREVIOUS_PROBLEM_ACTION);
101         }
102         public void run() {
103             revealElement(false);
104         }
105         public void update() {
106             boolean enabled= false;
107             if (fStatus != null && fStatus.hasEntries()) {
108                 int index= fTableViewer.getTable().getSelectionIndex();
109                 enabled= index == -1 || index > 0;
110             }
111             setEnabled(enabled);
112         }
113     }
114     
115     private static class RefactoringStatusSorter extends ViewerComparator {
116         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
117             int r1= ((RefactoringStatusEntry)e1).getSeverity();
118             int r2= ((RefactoringStatusEntry)e2).getSeverity();
119             if (r1 < r2)
120                 return 1;
121             if (r2 < r1)
122                 return -1;
123             return 0;
124         }
125
126     }
127     
128     private RefactoringStatus fStatus;
129     private TableViewer fTableViewer;
130     private PageBook fContextViewerContainer;
131     private StatusContextViewerDescriptor fCurrentDescriptor;
132     private IStatusContextViewer fCurrentContextViewer;
133     private NullContextViewer fNullContextViewer;
134     private RefactoringStatusEntryFilter fFilter= null;
135     
136     private NextProblem fNextProblem;
137     private PreviousProblem fPreviousProblem;
138     
139     public RefactoringStatusViewer(Composite parent, int style) {
140         super(parent, style | SWT.VERTICAL);
141         createContents();
142     }
143
144     /**
145      * Sets the status entry filter.
146      *
147      * @param filter
148      * the status entry filter to set, or <code>null</code>
149      */

150     public void setFilter(final RefactoringStatusEntryFilter filter) {
151         fFilter= filter;
152     }
153
154     /**
155      * Sets the refactoring status.
156      *
157      * @param status the refactoring status.
158      */

159     public void setStatus(RefactoringStatus status){
160         fStatus= status;
161         if (fTableViewer.getInput() != fStatus) {
162             fTableViewer.setInput(fStatus);
163             fTableViewer.getTable().getColumn(0).pack();
164             ISelection selection= fTableViewer.getSelection();
165             if (selection.isEmpty()) {
166                 RefactoringStatusEntry entry= getFirstEntry();
167                 if (entry != null) {
168                     fTableViewer.setSelection(new StructuredSelection(entry));
169                     showContextViewer(entry);
170                     fTableViewer.getControl().setFocus();
171                 }
172             }
173             fNextProblem.update();
174             fPreviousProblem.update();
175         }
176     }
177     
178     /**
179      * Returns the currently used <tt>RefactoringStatus</tt>.
180      * @return the <tt>RefactoringStatus</tt>
181      */

182     public RefactoringStatus getStatus() {
183         return fStatus;
184     }
185     
186     //---- UI creation ----------------------------------------------------------------------
187

188     public Point computeSize (int wHint, int hHint, boolean changed) {
189         PixelConverter converter= new PixelConverter(this);
190         return new Point(converter.convertWidthInCharsToPixels(90), converter.convertHeightInCharsToPixels(25));
191     }
192     
193     private void createContents() {
194         GridLayout layout= new GridLayout();
195         layout.numColumns= 1; layout.marginWidth= 0; layout.marginHeight= 0;
196         setLayout(layout);
197         
198         ViewerPane contextPane= new ViewerPane(this, SWT.BORDER | SWT.FLAT);
199         contextPane.setText(RefactoringUIMessages.RefactoringStatusViewer_Found_problems);
200         ToolBarManager tbm= contextPane.getToolBarManager();
201         tbm.add(fNextProblem= new NextProblem());
202         tbm.add(fPreviousProblem= new PreviousProblem());
203         tbm.update(true);
204         createTableViewer(contextPane);
205         contextPane.setContent(fTableViewer.getControl());
206         
207         fContextViewerContainer= new PageBook(this, SWT.NONE);
208         fNullContextViewer= new NullContextViewer();
209         fNullContextViewer.createControl(fContextViewerContainer);
210         fContextViewerContainer.showPage(fNullContextViewer.getControl());
211         fCurrentContextViewer= fNullContextViewer;
212         fCurrentContextViewer= fNullContextViewer;
213         fCurrentDescriptor= null;
214         
215         setWeights(new int[]{35, 65});
216     }
217     
218     private void createTableViewer(Composite parent) {
219         fTableViewer= new TableViewer(new Table(parent, SWT.SINGLE | SWT.H_SCROLL));
220         fTableViewer.setLabelProvider(new RefactoringStatusEntryLabelProvider());
221         fTableViewer.setContentProvider(new RefactoringStatusContentProvider());
222         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
223             public void selectionChanged(SelectionChangedEvent event) {
224                 entrySelected(event.getSelection());
225                 fNextProblem.update();
226                 fPreviousProblem.update();
227             }
228         });
229         fTableViewer.setComparator(new RefactoringStatusSorter());
230         Table tableControl= fTableViewer.getTable();
231         // must set the dialog font here since we pack the table and this
232
// might otherwise happen with the wrong font resulting in clipped
233
// messages.
234
tableControl.setFont(JFaceResources.getDialogFont());
235         GridData gd= new GridData(GridData.FILL_BOTH);
236         tableControl.setLayoutData(gd);
237         // Add a column so that we can pack it in setVisible.
238
TableColumn tc= new TableColumn(tableControl, SWT.NONE);
239         tc.setResizable(false);
240         
241         tableControl.getAccessible().addAccessibleListener(new AccessibleAdapter() {
242             public void getName(AccessibleEvent e) {
243                 e.result= RefactoringUIMessages.RefactoringStatusViewer_Found_problems;
244             }
245         });
246     }
247
248     //---- Feed status entry into context viewer ---------------------------------------------------------
249

250     private void entrySelected(ISelection s) {
251         if (!(s instanceof IStructuredSelection))
252             return;
253         Object JavaDoc first= ((IStructuredSelection) s).getFirstElement();
254         if (! (first instanceof RefactoringStatusEntry))
255             return;
256         
257         RefactoringStatusEntry entry= (RefactoringStatusEntry)first;
258         showContextViewer(entry);
259     }
260
261     private void showContextViewer(RefactoringStatusEntry entry) {
262         RefactoringStatusContext context= entry.getContext();
263         if (context == null || (fFilter != null && !fFilter.select(entry))) {
264             showNullContextViewer();
265         } else {
266             try {
267                 StatusContextViewerDescriptor descriptor= StatusContextViewerDescriptor.get(context);
268                 if (fCurrentDescriptor != descriptor) {
269                     IStatusContextViewer newViewer;
270                     if (descriptor != null) {
271                         newViewer= descriptor.createViewer();
272                         newViewer.createControl(fContextViewerContainer);
273                     } else {
274                         newViewer= fNullContextViewer;
275                     }
276                     fCurrentDescriptor= descriptor;
277                     newViewer.setInput(context);
278                     if (fCurrentContextViewer != null && fCurrentContextViewer != fNullContextViewer)
279                         fCurrentContextViewer.getControl().dispose();
280                     fCurrentContextViewer= newViewer;
281                     fContextViewerContainer.showPage(fCurrentContextViewer.getControl());
282                 } else {
283                     fCurrentContextViewer.setInput(context);
284                 }
285             } catch (CoreException e) {
286                 showNullContextViewer();
287                 ExceptionHandler.handle(e, getShell(),
288                     RefactoringUIMessages.RefactoringStatusViewer_error_title,
289                     RefactoringUIMessages.RefactoringStatusViewer_error_message);
290             }
291         }
292     }
293     
294     private void showNullContextViewer() {
295         fCurrentContextViewer= fNullContextViewer;
296         fCurrentDescriptor= null;
297         fContextViewerContainer.showPage(fCurrentContextViewer.getControl());
298     }
299     
300     //---- Helpers ----------------------------------------------------------------------------------------
301

302     private RefactoringStatusEntry getFirstEntry() {
303         if (fStatus == null || !fStatus.hasEntries())
304             return null;
305         return fStatus.getEntryAt(0);
306     }
307         
308     private void revealElement(boolean next) {
309         Table table= fTableViewer.getTable();
310         int numberOfItems= table.getItemCount();
311         if (numberOfItems == 0) {
312             return;
313         }
314         int index= table.getSelectionIndex();
315         int last= numberOfItems - 1;
316         boolean doIt= true;
317         if (index == -1) {
318             index= 0;
319         } else if (next && index < last) {
320             index++;
321         } else if (!next && index > 0) {
322             index--;
323         } else {
324             doIt= false;
325         }
326         if (doIt) {
327             // we have to set the selection via the viewer to trigger a
328
// selection change event
329
Object JavaDoc data= table.getItem(index).getData();
330             if (data != null) {
331                 fTableViewer.setSelection(new StructuredSelection(data));
332             }
333         }
334     }
335
336 }
337
Popular Tags