KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
16 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
17
18 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages;
19 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
20 import org.eclipse.ltk.internal.ui.refactoring.util.PixelConverter;
21 import org.eclipse.ltk.internal.ui.refactoring.util.SWTUtil;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.ViewForm;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.ToolBar;
34
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.action.IAction;
37 import org.eclipse.jface.action.Separator;
38 import org.eclipse.jface.action.ToolBarManager;
39 import org.eclipse.jface.viewers.IContentProvider;
40 import org.eclipse.jface.viewers.TreeViewer;
41
42 import org.eclipse.ltk.ui.refactoring.history.ISortableRefactoringHistoryControl;
43 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryContentProvider;
44 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
45 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryLabelProvider;
46
47 /**
48  * Control which is capable of browsing elements of a refactoring history. The
49  * refactoring history can be sorted by project or by timestamps.
50  *
51  * @since 3.2
52  */

53 public class SortableRefactoringHistoryControl extends RefactoringHistoryControl implements ISortableRefactoringHistoryControl {
54
55     /** The empty descriptors constant */
56     private static final RefactoringDescriptorProxy[] EMPTY_DESCRIPTORS= {};
57
58     /** The toolbar sort group */
59     private static final String JavaDoc TOOLBAR_SORT_GROUP= "group.sort"; //$NON-NLS-1$
60

61     /** The deselect all button, or <code>null</code> */
62     private Button fDeselectAllButton= null;
63
64     /** The select all button, or <code>null</code> */
65     private Button fSelectAllButton= null;
66
67     /** The sort projects action */
68     private final IAction fSortProjects= new Action(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_project, IAction.AS_RADIO_BUTTON) {
69
70         public final void run() {
71             final BrowseRefactoringHistoryContentProvider provider= (BrowseRefactoringHistoryContentProvider) fHistoryViewer.getContentProvider();
72             provider.setSortProjects(true);
73             fHistoryViewer.setComparator(fViewerComperator);
74             fHistoryViewer.refresh(false);
75             reconcileCheckState();
76             reconcileSelectionState();
77             fSortProjects.setChecked(true);
78             fSortTimestamps.setChecked(false);
79         }
80     };
81
82     /** The sort time stamps action */
83     private final IAction fSortTimestamps= new Action(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_date, IAction.AS_RADIO_BUTTON) {
84
85         public final void run() {
86             final BrowseRefactoringHistoryContentProvider provider= (BrowseRefactoringHistoryContentProvider) fHistoryViewer.getContentProvider();
87             provider.setSortProjects(false);
88             fHistoryViewer.setComparator(null);
89             fHistoryViewer.refresh(false);
90             reconcileCheckState();
91             reconcileSelectionState();
92             fSortTimestamps.setChecked(true);
93             fSortProjects.setChecked(false);
94         }
95     };
96
97     /** The toolbar manager, or <code>null</code> */
98     private ToolBarManager fToolBarManager= null;
99
100     /** The viewer sorter */
101     private final BrowseRefactoringHistoryViewerSorter fViewerComperator= new BrowseRefactoringHistoryViewerSorter();
102
103     /**
104      * Creates a new browse refactoring history control.
105      *
106      * @param parent
107      * the parent control
108      * @param configuration
109      * the refactoring history control configuration to use
110      */

111     public SortableRefactoringHistoryControl(final Composite parent, final RefactoringHistoryControlConfiguration configuration) {
112         super(parent, configuration);
113
114         addDisposeListener(new DisposeListener() {
115
116             public final void widgetDisposed(final DisposeEvent event) {
117                 if (fToolBarManager != null) {
118                     fToolBarManager.removeAll();
119                     fToolBarManager.dispose();
120                     fToolBarManager= null;
121                 }
122             }
123         });
124     }
125
126     /**
127      * {@inheritDoc}
128      */

129     protected void createBottomButtonBar(final Composite parent) {
130         Assert.isNotNull(parent);
131         final Composite composite= new Composite(parent, SWT.NONE);
132         final GridLayout layout= new GridLayout(2, false);
133         layout.marginWidth= 0;
134         layout.marginHeight= 0;
135         layout.marginTop= 5;
136         composite.setLayout(layout);
137
138         final GridData data= new GridData();
139         data.grabExcessHorizontalSpace= true;
140         data.grabExcessVerticalSpace= false;
141         data.horizontalAlignment= SWT.FILL;
142         data.verticalAlignment= SWT.TOP;
143         composite.setLayoutData(data);
144
145         createSelectAllButton(composite);
146         createDeselectAllButton(composite);
147     }
148
149     /**
150      * {@inheritDoc}
151      */

152     public void createControl() {
153         super.createControl();
154         final GridData data= new GridData();
155         data.grabExcessHorizontalSpace= true;
156         data.heightHint= new PixelConverter(this).convertHeightInCharsToPixels(22);
157         data.horizontalAlignment= SWT.FILL;
158         data.verticalAlignment= SWT.FILL;
159         setLayoutData(data);
160     }
161
162     /**
163      * Creates the deselect all button of the control.
164      *
165      * @param parent
166      * the parent composite
167      */

168     protected void createDeselectAllButton(final Composite parent) {
169         Assert.isNotNull(parent);
170         fDeselectAllButton= new Button(parent, SWT.NONE);
171         fDeselectAllButton.setEnabled(false);
172         fDeselectAllButton.setText(RefactoringUIMessages.SelectRefactoringHistoryControl_deselect_all_label);
173         final GridData data= new GridData();
174         data.horizontalAlignment= GridData.END;
175         data.verticalAlignment= GridData.BEGINNING;
176         data.widthHint= SWTUtil.getButtonWidthHint(fDeselectAllButton);
177         fDeselectAllButton.setLayoutData(data);
178
179         fDeselectAllButton.addSelectionListener(new SelectionAdapter() {
180
181             public final void widgetSelected(final SelectionEvent event) {
182                 handleDeselectAll();
183             }
184         });
185     }
186
187     /**
188      * {@inheritDoc}
189      */

190     protected TreeViewer createHistoryViewer(final Composite parent) {
191         Assert.isNotNull(parent);
192         TreeViewer viewer= null;
193         if (fControlConfiguration.isCheckableViewer())
194             viewer= new RefactoringHistoryTreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
195         else
196             viewer= new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
197         return viewer;
198     }
199
200     /**
201      * Creates the select all button of the control.
202      *
203      * @param parent
204      * the parent composite
205      */

206     protected void createSelectAllButton(final Composite parent) {
207         Assert.isNotNull(parent);
208         fSelectAllButton= new Button(parent, SWT.NONE);
209         fSelectAllButton.setEnabled(false);
210         fSelectAllButton.setText(RefactoringUIMessages.SelectRefactoringHistoryControl_select_all_label);
211         final GridData data= new GridData();
212         data.horizontalAlignment= GridData.END;
213         data.grabExcessHorizontalSpace= true;
214         data.verticalAlignment= GridData.BEGINNING;
215         data.widthHint= SWTUtil.getButtonWidthHint(fSelectAllButton);
216         fSelectAllButton.setLayoutData(data);
217
218         fSelectAllButton.addSelectionListener(new SelectionAdapter() {
219
220             public final void widgetSelected(final SelectionEvent event) {
221                 handleSelectAll();
222             }
223         });
224     }
225
226     /**
227      * {@inheritDoc}
228      */

229     protected void createToolBar(final ViewForm parent) {
230         final ToolBarManager manager= getToolBarManager();
231         if (manager != null) {
232             manager.removeAll();
233             manager.add(new Separator(TOOLBAR_SORT_GROUP));
234             fSortProjects.setText(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_project);
235             fSortProjects.setToolTipText(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_project);
236             fSortProjects.setDescription(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_project_description);
237             fSortProjects.setImageDescriptor(RefactoringPluginImages.DESC_ELCL_SORT_PROJECT);
238             fSortProjects.setDisabledImageDescriptor(RefactoringPluginImages.DESC_DLCL_SORT_PROJECT);
239             fSortTimestamps.setText(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_date);
240             fSortTimestamps.setToolTipText(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_date);
241             fSortTimestamps.setDescription(RefactoringUIMessages.BrowseRefactoringHistoryControl_sort_date_description);
242             fSortTimestamps.setImageDescriptor(RefactoringPluginImages.DESC_ELCL_SORT_DATE);
243             fSortTimestamps.setDisabledImageDescriptor(RefactoringPluginImages.DESC_DLCL_SORT_DATE);
244             manager.appendToGroup(TOOLBAR_SORT_GROUP, fSortProjects);
245             manager.appendToGroup(TOOLBAR_SORT_GROUP, fSortTimestamps);
246             manager.update(true);
247         }
248     }
249
250     /**
251      * {@inheritDoc}
252      */

253     protected int getContainerColumns() {
254         return 1;
255     }
256
257     /**
258      * {@inheritDoc}
259      */

260     protected RefactoringHistoryContentProvider getContentProvider() {
261         return new BrowseRefactoringHistoryContentProvider(fControlConfiguration);
262     }
263
264     /**
265      * Returns the deselect all button.
266      *
267      * @return the deselect all button, or <code>null</code>
268      */

269     public Button getDeselectAllButton() {
270         return fDeselectAllButton;
271     }
272
273     /**
274      * {@inheritDoc}
275      */

276     protected RefactoringHistoryLabelProvider getLabelProvider() {
277         return new BrowseRefactoringHistoryLabelProvider(fControlConfiguration);
278     }
279
280     /**
281      * Returns the select all button.
282      *
283      * @return the select all button, or <code>null</code>
284      */

285     public Button getSelectAllButton() {
286         return fSelectAllButton;
287     }
288
289     /**
290      * Returns the toolbar manager of this control
291      *
292      * @return the toolbar manager
293      */

294     protected ToolBarManager getToolBarManager() {
295         if (fToolBarManager == null) {
296             final ToolBar toolbar= new ToolBar(fHistoryPane, SWT.FLAT);
297             fHistoryPane.setTopCenter(toolbar);
298             fToolBarManager= new ToolBarManager(toolbar);
299         }
300         return fToolBarManager;
301     }
302
303     /**
304      * {@inheritDoc}
305      */

306     protected void handleCheckStateChanged() {
307         super.handleCheckStateChanged();
308         final RefactoringHistory history= getInput();
309         if (history != null) {
310             final int checked= getCheckedDescriptors().length;
311             final int total= history.getDescriptors().length;
312             if (fSelectAllButton != null)
313                 fSelectAllButton.setEnabled(checked < total);
314             if (fDeselectAllButton != null)
315                 fDeselectAllButton.setEnabled(checked > 0);
316         }
317     }
318
319     /**
320      * Handles the deselect all event.
321      */

322     protected void handleDeselectAll() {
323         setCheckedDescriptors(EMPTY_DESCRIPTORS);
324     }
325
326     /**
327      * Handles the select all event.
328      */

329     protected void handleSelectAll() {
330         final RefactoringHistory history= getInput();
331         if (history != null)
332             setCheckedDescriptors(history.getDescriptors());
333     }
334
335     /**
336      * {@inheritDoc}
337      */

338     public boolean isSortByDate() {
339         return !isSortByProjects();
340     }
341
342     /**
343      * {@inheritDoc}
344      */

345     public boolean isSortByProjects() {
346         final IContentProvider provider= fHistoryViewer.getContentProvider();
347         if (provider instanceof BrowseRefactoringHistoryContentProvider) {
348             final BrowseRefactoringHistoryContentProvider extended= (BrowseRefactoringHistoryContentProvider) provider;
349             return extended.isSortProjects();
350         }
351         return false;
352     }
353
354     /**
355      * {@inheritDoc}
356      */

357     protected void setHistoryControlEnablement() {
358         super.setHistoryControlEnablement();
359         boolean enable= false;
360         final RefactoringHistory history= (RefactoringHistory) fHistoryViewer.getInput();
361         if (history != null) {
362             final RefactoringDescriptorProxy[] proxies= history.getDescriptors();
363             if (proxies.length > 0)
364                 enable= true;
365         }
366         fSortProjects.setEnabled(enable);
367         fSortTimestamps.setEnabled(enable);
368     }
369
370     /**
371      * {@inheritDoc}
372      */

373     public void setInput(final RefactoringHistory history) {
374         super.setInput(history);
375         if (fDeselectAllButton != null)
376             fDeselectAllButton.setEnabled(false);
377         if (fSelectAllButton != null)
378             fSelectAllButton.setEnabled(history != null && !history.isEmpty());
379     }
380
381     /**
382      * {@inheritDoc}
383      */

384     public void sortByDate() {
385         fSortTimestamps.run();
386     }
387
388     /**
389      * {@inheritDoc}
390      */

391     public void sortByProjects() {
392         fSortProjects.run();
393     }
394 }
Popular Tags