KickJava   Java API By Example, From Geeks To Geeks.

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


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.RefactoringHistoryContentProvider;
43 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
44 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryLabelProvider;
45
46 /**
47  * Control which is capable of selecting elements of a refactoring history. The
48  * refactoring history can be sorted by project or by timestamps.
49  * <p>
50  * This control expects a control configuration with a checkable tree viewer.
51  * </p>
52  *
53  * @since 3.2
54  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

331     protected void handleSelectAll() {
332         final RefactoringHistory history= getInput();
333         if (history != null)
334             setCheckedDescriptors(history.getDescriptors());
335     }
336
337     /**
338      * Is sorting by date enabled?
339      *
340      * @return <code>true</code> if it is enabled, <code>false</code>
341      * otherwise
342      */

343     public boolean isSortByDate() {
344         return !isSortByProjects();
345     }
346
347     /**
348      * Is sorting by projects enabled?
349      *
350      * @return <code>true</code> if it is enabled, <code>false</code>
351      * otherwise
352      */

353     public boolean isSortByProjects() {
354         final IContentProvider provider= fHistoryViewer.getContentProvider();
355         if (provider instanceof BrowseRefactoringHistoryContentProvider) {
356             final BrowseRefactoringHistoryContentProvider extended= (BrowseRefactoringHistoryContentProvider) provider;
357             return extended.isSortProjects();
358         }
359         return false;
360     }
361
362     /**
363      * {@inheritDoc}
364      */

365     public void setInput(final RefactoringHistory history) {
366         super.setInput(history);
367         if (fDeselectAllButton != null)
368             fDeselectAllButton.setEnabled(false);
369         if (fSelectAllButton != null)
370             fSelectAllButton.setEnabled(history != null && !history.isEmpty());
371     }
372
373     /**
374      * Sorts the refactorings by date.
375      */

376     public void sortByDate() {
377         fSortTimestamps.run();
378     }
379
380     /**
381      * Sorts the refactorings by projects.
382      */

383     public void sortByProjects() {
384         fSortProjects.run();
385     }
386 }
Popular Tags