KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > history > RefactoringHistoryControl


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.history;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24
25 import org.eclipse.core.resources.IProject;
26
27 import org.eclipse.ltk.core.refactoring.RefactoringCore;
28 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
29 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
30 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
31
32 import org.eclipse.ltk.internal.ui.refactoring.Messages;
33 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
34
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.custom.BusyIndicator;
37 import org.eclipse.swt.custom.CLabel;
38 import org.eclipse.swt.custom.ViewForm;
39 import org.eclipse.swt.events.MouseAdapter;
40 import org.eclipse.swt.events.MouseEvent;
41 import org.eclipse.swt.graphics.Color;
42 import org.eclipse.swt.graphics.Point;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.swt.widgets.TreeItem;
50 import org.eclipse.swt.widgets.Widget;
51
52 import org.eclipse.jface.dialogs.Dialog;
53 import org.eclipse.jface.viewers.AbstractTreeViewer;
54 import org.eclipse.jface.viewers.CheckStateChangedEvent;
55 import org.eclipse.jface.viewers.CheckboxTreeViewer;
56 import org.eclipse.jface.viewers.ICheckStateListener;
57 import org.eclipse.jface.viewers.ISelection;
58 import org.eclipse.jface.viewers.ISelectionChangedListener;
59 import org.eclipse.jface.viewers.IStructuredSelection;
60 import org.eclipse.jface.viewers.ITreeViewerListener;
61 import org.eclipse.jface.viewers.SelectionChangedEvent;
62 import org.eclipse.jface.viewers.StructuredSelection;
63 import org.eclipse.jface.viewers.TreeExpansionEvent;
64 import org.eclipse.jface.viewers.TreeViewer;
65
66 import org.eclipse.ui.progress.UIJob;
67
68 import org.eclipse.compare.CompareViewerPane;
69 import org.eclipse.compare.Splitter;
70
71 import org.eclipse.ltk.ui.refactoring.history.IRefactoringHistoryControl;
72 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryContentProvider;
73 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
74 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryLabelProvider;
75
76 /**
77  * Control which is capable of displaying refactoring histories.
78  *
79  * @since 3.2
80  */

81 public class RefactoringHistoryControl extends Composite implements IRefactoringHistoryControl {
82
83     /** Label for the refactoring history tree viewer */
84     protected final class RefactoringHistoryLabel extends CLabel {
85
86         /**
87          * Creates a new refactoring history label.
88          *
89          * @param parent
90          * the parent control
91          * @param style
92          * the style
93          */

94         public RefactoringHistoryLabel(final Composite parent, final int style) {
95             super(parent, style);
96         }
97
98         /**
99          * {@inheritDoc}
100          */

101         public Point computeSize(final int wHint, final int hHint, final boolean changed) {
102             return super.computeSize(wHint, Math.max(24, hHint), changed);
103         }
104
105         /**
106          * {@inheritDoc}
107          */

108         public Color getForeground() {
109             if (isEnabled())
110                 return super.getForeground();
111             return getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
112         }
113     }
114
115     /** Checkbox tree viewer for the refactoring history */
116     protected final class RefactoringHistoryTreeViewer extends CheckboxTreeViewer {
117
118         /**
119          * Creates a new refactoring history tree viewer.
120          *
121          * @param parent
122          * the parent control
123          * @param style
124          * the style
125          */

126         public RefactoringHistoryTreeViewer(final Composite parent, final int style) {
127             super(parent, style);
128             addCheckStateListener(new ICheckStateListener() {
129
130                 /**
131                  * {@inheritDoc}
132                  */

133                 public final void checkStateChanged(final CheckStateChangedEvent event) {
134                     reconcileCheckState(event.getElement(), event.getChecked());
135                     handleCheckStateChanged();
136                 }
137             });
138             addTreeListener(new ITreeViewerListener() {
139
140                 /**
141                  * {@inheritDoc}
142                  */

143                 public final void treeCollapsed(final TreeExpansionEvent event) {
144                     // Do nothing
145
}
146
147                 /**
148                  * {@inheritDoc}
149                  */

150                 public final void treeExpanded(final TreeExpansionEvent event) {
151                     BusyIndicator.showWhile(getDisplay(), new Runnable JavaDoc() {
152
153                         public final void run() {
154                             final Object JavaDoc element= event.getElement();
155                             if (getGrayed(element)) {
156                                 final RefactoringHistory history= RefactoringHistoryControl.this.getInput();
157                                 if (history != null)
158                                     reconcileCheckState(history);
159                             } else if (getChecked(element)) {
160                                 setSubTreeGrayed(element, false);
161                                 setSubtreeChecked(element, true);
162                             }
163                         }
164                     });
165                 }
166             });
167         }
168
169         /**
170          * Returns the children of the specified element.
171          *
172          * @param element
173          * the element
174          * @return the children of the element
175          */

176         private Object JavaDoc[] getChildren(final Object JavaDoc element) {
177             return ((RefactoringHistoryContentProvider) getContentProvider()).getChildren(element);
178         }
179
180         /**
181          * Reconciles the check state of the specified element and dependencies.
182          *
183          * @param element
184          * the changed element
185          */

186         private void reconcileCheckState(final Object JavaDoc element) {
187             final Object JavaDoc[] children= getChildren(element);
188             for (int index= 0; index < children.length; index++) {
189                 reconcileCheckState(children[index]);
190             }
191             int checkCount= 0;
192             final Collection JavaDoc collection= getCoveredDescriptors(element);
193             for (final Iterator JavaDoc iterator= collection.iterator(); iterator.hasNext();) {
194                 final RefactoringDescriptorProxy proxy= (RefactoringDescriptorProxy) iterator.next();
195                 if (fCheckedDescriptors.contains(proxy))
196                     checkCount++;
197             }
198             setElementChecked(element, checkCount > 0);
199             setElementGrayed(element, checkCount != 0 && checkCount != collection.size());
200         }
201
202         /**
203          * Reconciles the check state of the specified element and dependencies.
204          *
205          * @param element
206          * the changed element
207          * @param checked
208          * <code>true</code> if the element is checked,
209          * <code>false</code> otherwise
210          */

211         private void reconcileCheckState(final Object JavaDoc element, final boolean checked) {
212             if (element instanceof RefactoringHistoryEntry) {
213                 final RefactoringHistoryEntry entry= (RefactoringHistoryEntry) element;
214                 final RefactoringDescriptorProxy proxy= entry.getDescriptor();
215                 if (checked)
216                     fCheckedDescriptors.add(proxy);
217                 else
218                     fCheckedDescriptors.remove(proxy);
219             } else if (element instanceof RefactoringHistoryNode) {
220                 final Collection JavaDoc collection= getCoveredDescriptors(element);
221                 if (checked)
222                     fCheckedDescriptors.addAll(collection);
223                 else
224                     fCheckedDescriptors.removeAll(collection);
225             }
226             RefactoringHistoryControl.this.reconcileCheckState();
227         }
228
229         /**
230          * Determines whether the specified element is checked.
231          *
232          * @param element
233          * the element
234          * @param checked
235          * <code>true</code> to render it checked,
236          * <code>false</code> otherwise
237          */

238         private void setElementChecked(final Object JavaDoc element, final boolean checked) {
239             final Widget widget= RefactoringHistoryTreeViewer.this.findItem(element);
240             if (widget instanceof TreeItem) {
241                 final TreeItem item= (TreeItem) widget;
242                 item.setChecked(checked);
243             }
244         }
245
246         /**
247          * Determines whether the specified element is grayed.
248          *
249          * @param element
250          * the element
251          * @param grayed
252          * <code>true</code> to render it grayed,
253          * <code>false</code> otherwise
254          */

255         private void setElementGrayed(final Object JavaDoc element, final boolean grayed) {
256             final Widget widget= RefactoringHistoryTreeViewer.this.findItem(element);
257             if (widget instanceof TreeItem) {
258                 final TreeItem item= (TreeItem) widget;
259                 item.setGrayed(grayed);
260             }
261         }
262
263         /**
264          * Determines whether the subtree of the specified element is rendered
265          * grayed.
266          *
267          * @param element
268          * the element specifying the subtree
269          * @param grayed
270          * <code>true</code> to render the subtree grayed,
271          * <code>false</code> otherwise
272          */

273         private void setSubTreeGrayed(final Object JavaDoc element, final boolean grayed) {
274             setElementGrayed(element, grayed);
275             final Object JavaDoc[] children= getChildren(element);
276             for (int index= 0; index < children.length; index++) {
277                 setSubTreeGrayed(children[index], grayed);
278             }
279         }
280     }
281
282     /**
283      * The checked refactoring descriptors (element type:
284      * <code>RefactoringDescriptorProxy</code>)
285      */

286     private final Set JavaDoc fCheckedDescriptors= new HashSet JavaDoc();
287
288     /** The refactoring history control configuration to use */
289     protected final RefactoringHistoryControlConfiguration fControlConfiguration;
290
291     /** The detail field */
292     private Text fDetailField= null;
293
294     /** The detail label */
295     private Label fDetailLabel= null;
296
297     /** The history pane */
298     protected CompareViewerPane fHistoryPane= null;
299
300     /** The history viewer */
301     protected TreeViewer fHistoryViewer= null;
302
303     /**
304      * The selected refactoring descriptors (element type:
305      * <code>RefactoringDescriptorProxy</code>)
306      */

307     private final Set JavaDoc fSelectedDescriptors= new HashSet JavaDoc();
308
309     /** The selection label, or <code>null</code> */
310     private Label fSelectionLabel= null;
311
312     /** The splitter control */
313     private Splitter fSplitterControl= null;
314
315     /**
316      * Creates a new refactoring history control.
317      *
318      * @param parent
319      * the parent control
320      * @param configuration
321      * the refactoring history control configuration to use
322      */

323     public RefactoringHistoryControl(final Composite parent, final RefactoringHistoryControlConfiguration configuration) {
324         super(parent, SWT.NONE);
325         Assert.isNotNull(configuration);
326         fControlConfiguration= configuration;
327     }
328
329     /**
330      * {@inheritDoc}
331      */

332     public final void addCheckStateListener(final ICheckStateListener listener) {
333         Assert.isNotNull(listener);
334         if (fHistoryViewer instanceof RefactoringHistoryTreeViewer) {
335             final RefactoringHistoryTreeViewer viewer= (RefactoringHistoryTreeViewer) fHistoryViewer;
336             viewer.addCheckStateListener(listener);
337         }
338     }
339
340     /**
341      * {@inheritDoc}
342      */

343     public final void addSelectionChangedListener(final ISelectionChangedListener listener) {
344         Assert.isNotNull(listener);
345         if (fHistoryViewer != null)
346             fHistoryViewer.addSelectionChangedListener(listener);
347     }
348
349     /**
350      * Creates the button bar at the bottom of the component.
351      *
352      * @param parent
353      * the parent composite
354      */

355     protected void createBottomButtonBar(final Composite parent) {
356         // Do nothing
357
}
358
359     /**
360      * {@inheritDoc}
361      */

362     public void createControl() {
363         RefactoringCore.getHistoryService().connect();
364         GridLayout layout= new GridLayout(getContainerColumns(), false);
365         layout.marginHeight= 0;
366         layout.marginWidth= 0;
367         layout.horizontalSpacing= 0;
368         layout.verticalSpacing= 0;
369         setLayout(layout);
370         setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
371         fSplitterControl= new Splitter(this, SWT.VERTICAL);
372         fSplitterControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
373         createRightButtonBar(this);
374         final Composite leftPane= new Composite(fSplitterControl, SWT.NONE);
375         layout= new GridLayout();
376         layout.marginWidth= 0;
377         layout.marginHeight= 2;
378         layout.verticalSpacing= 2;
379         leftPane.setLayout(layout);
380         fHistoryPane= new CompareViewerPane(leftPane, SWT.BORDER | SWT.FLAT);
381         fHistoryPane.setTopLeft(new RefactoringHistoryLabel(fHistoryPane, SWT.NONE));
382         fHistoryPane.setText(getHistoryPaneText());
383         fHistoryPane.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
384         fHistoryViewer= createHistoryViewer(fHistoryPane);
385         if (!fControlConfiguration.isTimeDisplayed())
386             fHistoryViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
387         fHistoryViewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
388         fHistoryViewer.setUseHashlookup(true);
389         fHistoryViewer.setContentProvider(getContentProvider());
390         fHistoryViewer.setLabelProvider(getLabelProvider());
391         fHistoryViewer.addSelectionChangedListener(new ISelectionChangedListener() {
392
393             public final void selectionChanged(final SelectionChangedEvent event) {
394                 final ISelection selection= event.getSelection();
395                 if (selection instanceof IStructuredSelection)
396                     handleSelectionChanged((IStructuredSelection) selection);
397             }
398         });
399         fHistoryPane.setContent(fHistoryViewer.getControl());
400         createToolBar(fHistoryPane);
401         createDetailPane(fSplitterControl);
402         final MouseAdapter listener= new MouseAdapter() {
403
404             public void mouseDoubleClick(final MouseEvent event) {
405                 final Control content= fHistoryPane.getContent();
406                 if (content != null && content.getBounds().contains(event.x, event.y))
407                     return;
408                 final Control control= fHistoryPane.getParent().getParent();
409                 if (control instanceof Splitter)
410                     ((Splitter) control).setMaximizedControl(fHistoryPane.getParent());
411             }
412         };
413         addMouseListener(listener);
414         fHistoryPane.getTopLeft().addMouseListener(listener);
415         fSplitterControl.setWeights(new int[] { 65, 35});
416         createBottomButtonBar(this);
417         Dialog.applyDialogFont(this);
418     }
419
420     /**
421      * Creates the detail label of the control
422      *
423      * @param parent
424      * the parent composite
425      */

426     protected void createDetailLabel(final Composite parent) {
427         Assert.isNotNull(parent);
428         fDetailLabel= new Label(parent, SWT.HORIZONTAL | SWT.LEFT | SWT.WRAP);
429         fDetailLabel.setText(RefactoringUIMessages.RefactoringHistoryControl_comment_viewer_label);
430         final GridData data= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
431         data.horizontalSpan= 1;
432         fDetailLabel.setLayoutData(data);
433     }
434
435     /**
436      * Creates the detail pane of the control.
437      *
438      * @param parent
439      * the parent composite
440      * @return the detail pane
441      */

442     protected Composite createDetailPane(final Composite parent) {
443         Assert.isNotNull(parent);
444         final Composite composite= new Composite(parent, SWT.NONE);
445         final GridLayout layout= new GridLayout(getDetailColumns(), true);
446         layout.horizontalSpacing= 0;
447         layout.marginHeight= 0;
448         layout.marginWidth= 0;
449         composite.setLayout(layout);
450         createDetailLabel(composite);
451         createSelectionLabel(composite);
452         fDetailField= new Text(composite, SWT.BORDER | SWT.FLAT | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
453         fDetailField.setText(fControlConfiguration.getCommentCaption());
454         final GridData data= new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
455         data.horizontalSpan= getDetailColumns();
456         fDetailField.setLayoutData(data);
457         return composite;
458     }
459
460     /**
461      * Creates the history viewer of the control.
462      *
463      * @param parent
464      * the parent composite
465      * @return the history viewer
466      */

467     protected TreeViewer createHistoryViewer(final Composite parent) {
468         Assert.isNotNull(parent);
469         if (fControlConfiguration.isCheckableViewer())
470             return new RefactoringHistoryTreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
471         else
472             return new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
473     }
474
475     /**
476      * Creates the button bar at the right of the component.
477      *
478      * @param parent
479      * the parent composite
480      */

481     protected void createRightButtonBar(final Composite parent) {
482         // Do nothing
483
}
484
485     /**
486      * Creates the selection label of the control
487      *
488      * @param parent
489      * the parent composite
490      */

491     protected void createSelectionLabel(final Composite parent) {
492         Assert.isNotNull(parent);
493         fSelectionLabel= new Label(parent, SWT.HORIZONTAL | SWT.RIGHT | SWT.WRAP);
494         final GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END);
495         data.horizontalSpan= 1;
496         fSelectionLabel.setLayoutData(data);
497     }
498
499     /**
500      * Creates the toolbar of the control.
501      *
502      * @param parent
503      * the parent control
504      */

505     protected void createToolBar(final ViewForm parent) {
506         // Do nothing
507
}
508
509     /**
510      * {@inheritDoc}
511      */

512     public final void dispose() {
513         RefactoringCore.getHistoryService().disconnect();
514         super.dispose();
515     }
516
517     /**
518      * {@inheritDoc}
519      */

520     public final RefactoringDescriptorProxy[] getCheckedDescriptors() {
521         if (fHistoryViewer instanceof RefactoringHistoryTreeViewer)
522             return (RefactoringDescriptorProxy[]) fCheckedDescriptors.toArray(new RefactoringDescriptorProxy[fCheckedDescriptors.size()]);
523         return getSelectedDescriptors();
524     }
525
526     /**
527      * Returns the number of columns to use for the container layout.
528      *
529      * @return the number of columns
530      */

531     protected int getContainerColumns() {
532         return 2;
533     }
534
535     /**
536      * Returns the content provider to use.
537      *
538      * @return the content provider
539      */

540     protected RefactoringHistoryContentProvider getContentProvider() {
541         return fControlConfiguration.getContentProvider();
542     }
543
544     /**
545      * {@inheritDoc}
546      */

547     public final Control getControl() {
548         return this;
549     }
550
551     /**
552      * Returns the refactoring descriptors covered by the specified node.
553      *
554      * @param element
555      * the refactoring history element
556      * @return the collection of covered descriptors
557      */

558     private Collection JavaDoc getCoveredDescriptors(final Object JavaDoc element) {
559         Assert.isNotNull(element);
560         final Set JavaDoc set= new HashSet JavaDoc();
561         getCoveredDescriptors(element, set);
562         return set;
563     }
564
565     /**
566      * Computes the refactoring descriptors covered by the specified node.
567      *
568      * @param element
569      * the refactoring history element
570      * @param set
571      * the set of refactoring descriptor proxies
572      */

573     private void getCoveredDescriptors(final Object JavaDoc element, final Set JavaDoc set) {
574         Assert.isNotNull(element);
575         Assert.isNotNull(set);
576         final RefactoringHistoryContentProvider provider= (RefactoringHistoryContentProvider) fHistoryViewer.getContentProvider();
577         if (provider != null) {
578             if (element instanceof RefactoringHistoryEntry) {
579                 final RefactoringHistoryEntry entry= (RefactoringHistoryEntry) element;
580                 final RefactoringDescriptorProxy proxy= entry.getDescriptor();
581                 set.add(proxy);
582             } else {
583                 final Object JavaDoc[] children= provider.getChildren(element);
584                 for (int index= 0; index < children.length; index++) {
585                     final Object JavaDoc child= children[index];
586                     if (child instanceof RefactoringHistoryNode)
587                         getCoveredDescriptors(child, set);
588                 }
589             }
590         }
591     }
592
593     /**
594      * Returns the number of columns to use for the detail pane layout.
595      *
596      * @return the number of columns
597      */

598     protected int getDetailColumns() {
599         return 2;
600     }
601
602     /**
603      * Returns the text to be displayed in the history pane.
604      *
605      * @return the text in the history pane
606      */

607     private String JavaDoc getHistoryPaneText() {
608         String JavaDoc text= null;
609         final IProject project= fControlConfiguration.getProject();
610         if (project != null)
611             text= Messages.format(fControlConfiguration.getProjectPattern(), project.getName());
612         else
613             text= fControlConfiguration.getWorkspaceCaption();
614         return text;
615     }
616
617     /**
618      * Returns the input of the refactoring history control.
619      *
620      * @return the input, or <code>null</code>
621      */

622     public final RefactoringHistory getInput() {
623         return (RefactoringHistory) fHistoryViewer.getInput();
624     }
625
626     /**
627      * Returns the label provider to use.
628      *
629      * @return the label provider
630      */

631     protected RefactoringHistoryLabelProvider getLabelProvider() {
632         return fControlConfiguration.getLabelProvider();
633     }
634
635     /**
636      * {@inheritDoc}
637      */

638     public final RefactoringDescriptorProxy[] getSelectedDescriptors() {
639         return (RefactoringDescriptorProxy[]) fSelectedDescriptors.toArray(new RefactoringDescriptorProxy[fSelectedDescriptors.size()]);
640     }
641
642     /**
643      * Handles the check state changed event.
644      */

645     protected void handleCheckStateChanged() {
646         if (fSelectionLabel != null) {
647             final RefactoringHistory history= getInput();
648             if (history != null) {
649                 final int total= history.getDescriptors().length;
650                 final int checked= fCheckedDescriptors.size();
651                 if (fSelectionLabel.isEnabled()) {
652                     if (total > 0 && fControlConfiguration.isCheckableViewer())
653                         fSelectionLabel.setText(Messages.format(RefactoringUIMessages.RefactoringHistoryControl_selection_pattern, new String JavaDoc[] { String.valueOf(checked), String.valueOf(total)}));
654                     else
655                         fSelectionLabel.setText(RefactoringUIMessages.RefactoringHistoryControl_no_selection);
656                 } else
657                     fSelectionLabel.setText(""); //$NON-NLS-1$
658
}
659         }
660     }
661
662     /**
663      * Handles the selection changed event.
664      *
665      * @param selection
666      * the new selection
667      */

668     protected void handleSelectionChanged(final IStructuredSelection selection) {
669         Assert.isNotNull(selection);
670         fSelectedDescriptors.clear();
671         final Object JavaDoc[] elements= selection.toArray();
672         for (int index= 0; index < elements.length; index++) {
673             final Object JavaDoc element= elements[index];
674             if (element instanceof RefactoringHistoryEntry) {
675                 final RefactoringHistoryEntry entry= (RefactoringHistoryEntry) element;
676                 final RefactoringDescriptorProxy proxy= entry.getDescriptor();
677                 fSelectedDescriptors.add(proxy);
678             } else if (element instanceof RefactoringHistoryNode) {
679                 final RefactoringHistoryNode node= (RefactoringHistoryNode) element;
680                 fSelectedDescriptors.addAll(getCoveredDescriptors(node));
681             }
682         }
683         if (elements.length == 1 && elements[0] instanceof RefactoringHistoryEntry) {
684             final RefactoringHistoryEntry entry= (RefactoringHistoryEntry) elements[0];
685             final RefactoringDescriptorProxy proxy= entry.getDescriptor();
686             final Job job= new UIJob(RefactoringUIMessages.RefactoringHistoryControl_resolving_information) {
687
688                 public final IStatus runInUIThread(final IProgressMonitor monitor) {
689                     final RefactoringDescriptor descriptor= proxy.requestDescriptor(monitor);
690                     if (descriptor != null) {
691                         String JavaDoc comment= descriptor.getComment();
692                         if ("".equals(comment)) //$NON-NLS-1$
693
comment= RefactoringUIMessages.RefactoringHistoryControl_no_comment;
694                         fDetailField.setText(comment);
695                     }
696                     return Status.OK_STATUS;
697                 }
698             };
699             job.setSystem(true);
700             job.schedule();
701         } else
702             fDetailField.setText(fControlConfiguration.getCommentCaption());
703     }
704
705     /**
706      * Reconciles the check state of the control.
707      */

708     public void reconcileCheckState() {
709         final RefactoringHistory history= getInput();
710         if (history != null && fHistoryViewer instanceof RefactoringHistoryTreeViewer)
711             ((RefactoringHistoryTreeViewer) fHistoryViewer).reconcileCheckState(history);
712     }
713
714     /**
715      * Reconciles the selection state of the control.
716      */

717     public void reconcileSelectionState() {
718         final RefactoringHistoryNode[] nodes= new RefactoringHistoryNode[fSelectedDescriptors.size()];
719         int index= 0;
720         for (final Iterator JavaDoc iterator= fSelectedDescriptors.iterator(); iterator.hasNext(); index++) {
721             final RefactoringDescriptorProxy descriptor= (RefactoringDescriptorProxy) iterator.next();
722             nodes[index]= new RefactoringHistoryEntry(null, descriptor);
723             fHistoryViewer.expandToLevel(nodes[index], AbstractTreeViewer.ALL_LEVELS);
724         }
725         fHistoryViewer.setSelection(new StructuredSelection(nodes), true);
726     }
727
728     /**
729      * {@inheritDoc}
730      */

731     public final void removeCheckStateListener(final ICheckStateListener listener) {
732         Assert.isNotNull(listener);
733         Assert.isNotNull(listener);
734         if (fHistoryViewer instanceof RefactoringHistoryTreeViewer) {
735             final RefactoringHistoryTreeViewer viewer= (RefactoringHistoryTreeViewer) fHistoryViewer;
736             viewer.removeCheckStateListener(listener);
737         }
738     }
739
740     /**
741      * {@inheritDoc}
742      */

743     public final void removeSelectionChangedListener(final ISelectionChangedListener listener) {
744         Assert.isNotNull(listener);
745         if (fHistoryViewer != null)
746             fHistoryViewer.removeSelectionChangedListener(listener);
747     }
748
749     /**
750      * {@inheritDoc}
751      */

752     public final void setCheckedDescriptors(final RefactoringDescriptorProxy[] descriptors) {
753         Assert.isNotNull(descriptors);
754         if (fHistoryViewer instanceof RefactoringHistoryTreeViewer) {
755             fCheckedDescriptors.clear();
756             fCheckedDescriptors.addAll(Arrays.asList(descriptors));
757             final RefactoringHistoryTreeViewer viewer= (RefactoringHistoryTreeViewer) fHistoryViewer;
758             final RefactoringHistory history= RefactoringHistoryControl.this.getInput();
759             if (history != null)
760                 viewer.reconcileCheckState(history);
761             handleCheckStateChanged();
762         } else
763             setSelectedDescriptors(descriptors);
764     }
765
766     /**
767      * Sets the expanded state of the viewer.
768      */

769     protected void setExpandedState() {
770         final RefactoringHistoryContentProvider provider= (RefactoringHistoryContentProvider) fHistoryViewer.getContentProvider();
771         if (provider != null) {
772             handleCheckStateChanged();
773             final Object JavaDoc[] roots= provider.getRootElements();
774             if (roots != null) {
775                 for (int index= 0; index < roots.length; index++) {
776                     if (!(roots[index] instanceof RefactoringHistoryEntry)) {
777                         fHistoryViewer.setExpandedState(roots[index], true);
778                         return;
779                     }
780                 }
781             }
782         }
783     }
784
785     /**
786      * Sets the enablement of the detail pane.
787      */

788     protected void setHistoryControlEnablement() {
789         boolean enable= false;
790         final RefactoringHistory history= (RefactoringHistory) fHistoryViewer.getInput();
791         if (history != null) {
792             final RefactoringDescriptorProxy[] proxies= history.getDescriptors();
793             if (proxies.length > 0)
794                 enable= true;
795         }
796         if (fDetailField != null)
797             fDetailField.setEnabled(enable);
798         if (fDetailLabel != null)
799             fDetailLabel.setEnabled(enable);
800         if (fHistoryPane != null)
801             fHistoryPane.setEnabled(enable);
802         if (fSelectionLabel != null)
803             fSelectionLabel.setEnabled(enable);
804         if (enable) {
805             fDetailField.setText(fControlConfiguration.getCommentCaption());
806             if (fSelectionLabel != null)
807                 fSelectionLabel.setText(RefactoringUIMessages.RefactoringHistoryControl_no_selection);
808         } else {
809             fDetailField.setText(""); //$NON-NLS-1$
810
if (fSelectionLabel != null)
811                 fSelectionLabel.setText(""); //$NON-NLS-1$
812
}
813     }
814
815     /**
816      * {@inheritDoc}
817      */

818     public void setInput(final RefactoringHistory history) {
819         fHistoryViewer.setInput(history);
820         fSelectedDescriptors.clear();
821         fCheckedDescriptors.clear();
822         if (history != null) {
823             final RefactoringHistoryContentProvider provider= (RefactoringHistoryContentProvider) fHistoryViewer.getContentProvider();
824             if (provider != null) {
825                 provider.inputChanged(fHistoryViewer, null, history);
826                 setExpandedState();
827                 setHistoryControlEnablement();
828             }
829         }
830     }
831
832     /**
833      * {@inheritDoc}
834      */

835     public final void setSelectedDescriptors(final RefactoringDescriptorProxy[] descriptors) {
836         Assert.isNotNull(descriptors);
837         if (fHistoryViewer != null) {
838             fSelectedDescriptors.clear();
839             fSelectedDescriptors.addAll(Arrays.asList(descriptors));
840             final RefactoringHistoryNode[] nodes= new RefactoringHistoryNode[descriptors.length];
841             for (int index= 0; index < descriptors.length; index++)
842                 nodes[index]= new RefactoringHistoryEntry(null, descriptors[index]);
843             fHistoryViewer.setSelection(new StructuredSelection(nodes));
844         }
845     }
846 }
Popular Tags