01: /*******************************************************************************
02: * Copyright (c) 2006, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.actions;
11:
12: import org.eclipse.jface.action.Action;
13: import org.eclipse.jface.action.IAction;
14: import org.eclipse.jface.util.IPropertyChangeListener;
15: import org.eclipse.jface.viewers.StructuredViewer;
16: import org.eclipse.jface.viewers.ViewerComparator;
17: import org.eclipse.pde.internal.ui.PDEPluginImages;
18:
19: public class SortAction extends Action {
20:
21: private boolean fSorted;
22:
23: private StructuredViewer fViewer;
24:
25: private ViewerComparator fComparator;
26:
27: private ViewerComparator fDefaultComparator;
28:
29: /**
30: * @param viewer
31: * @param tooltipText
32: * @param sorter
33: * @param defaultSorter
34: * @param listener
35: * @param useMiniImage
36: */
37: public SortAction(StructuredViewer viewer, String tooltipText,
38: ViewerComparator sorter, ViewerComparator defaultSorter,
39: IPropertyChangeListener listener) {
40:
41: super (tooltipText, IAction.AS_CHECK_BOX);
42: // Set the tooltip
43: setToolTipText(tooltipText);
44: // Set the image
45: setImageDescriptor(PDEPluginImages.DESC_ALPHAB_SORT_CO);
46: // Set the default comparator
47: fDefaultComparator = defaultSorter;
48: // Set the viewer
49: fViewer = viewer;
50: // Set the comparator
51: // If one was not specified, use the default
52: if (sorter == null) {
53: fComparator = new ViewerComparator();
54: } else {
55: fComparator = sorter;
56: }
57: // Determine if the viewer is already sorted
58: // Note: Most likely the default comparator is null
59: if (viewer.getComparator() == fDefaultComparator) {
60: fSorted = false;
61: } else {
62: fSorted = true;
63: }
64: // Set the status of this action depending on whether it is sorted or
65: // not
66: setChecked(fSorted);
67: // If a listener was specified, use it
68: if (listener != null) {
69: addListenerObject(listener);
70: }
71: }
72:
73: /* (non-Javadoc)
74: * @see org.eclipse.jface.action.Action#run()
75: */
76: public void run() {
77: // Toggle sorting on/off
78: if (fSorted) {
79: // Sorting is on
80: // Turn it off
81: fViewer.setComparator(fDefaultComparator);
82: fSorted = false;
83: } else {
84: // Sorting is off
85: // Turn it on
86: fViewer.setComparator(fComparator);
87: fSorted = true;
88: }
89: notifyResult(true);
90: }
91:
92: }
|