KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > actions > SortAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.viewers.StructuredViewer;
17 import org.eclipse.jface.viewers.ViewerComparator;
18 import org.eclipse.pde.internal.ui.PDEPluginImages;
19
20 public class SortAction extends Action {
21
22     private boolean fSorted;
23
24     private StructuredViewer fViewer;
25
26     private ViewerComparator fComparator;
27
28     private ViewerComparator fDefaultComparator;
29
30     /**
31      * @param viewer
32      * @param tooltipText
33      * @param sorter
34      * @param defaultSorter
35      * @param listener
36      * @param useMiniImage
37      */

38     public SortAction(StructuredViewer viewer, String JavaDoc tooltipText,
39             ViewerComparator sorter, ViewerComparator defaultSorter,
40             IPropertyChangeListener listener) {
41         
42         super(tooltipText, IAction.AS_CHECK_BOX);
43         // Set the tooltip
44
setToolTipText(tooltipText);
45         // Set the image
46
setImageDescriptor(PDEPluginImages.DESC_ALPHAB_SORT_CO);
47         // Set the default comparator
48
fDefaultComparator = defaultSorter;
49         // Set the viewer
50
fViewer = viewer;
51         // Set the comparator
52
// If one was not specified, use the default
53
if (sorter == null) {
54             fComparator = new ViewerComparator();
55         } else {
56             fComparator = sorter;
57         }
58         // Determine if the viewer is already sorted
59
// Note: Most likely the default comparator is null
60
if (viewer.getComparator() == fDefaultComparator) {
61             fSorted = false;
62         } else {
63             fSorted = true;
64         }
65         // Set the status of this action depending on whether it is sorted or
66
// not
67
setChecked(fSorted);
68         // If a listener was specified, use it
69
if (listener != null) {
70             addListenerObject(listener);
71         }
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.jface.action.Action#run()
76      */

77     public void run() {
78         // Toggle sorting on/off
79
if (fSorted) {
80             // Sorting is on
81
// Turn it off
82
fViewer.setComparator(fDefaultComparator);
83             fSorted = false;
84         } else {
85             // Sorting is off
86
// Turn it on
87
fViewer.setComparator(fComparator);
88             fSorted = true;
89         }
90         notifyResult(true);
91     }
92
93 }
94
Popular Tags