KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > SortDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.search.internal.ui;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.swt.custom.BusyIndicator;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Menu;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.ActionContributionItem;
23 import org.eclipse.jface.action.IMenuCreator;
24 import org.eclipse.jface.viewers.ViewerSorter;
25
26 import org.eclipse.ui.IMemento;
27 import org.eclipse.ui.model.WorkbenchViewerSorter;
28
29 /**
30  * Drop down action that holds the currently registered sort actions.
31  * @deprecated old search
32  */

33 class SortDropDownAction extends Action implements IMenuCreator {
34
35     // Persistance tags.
36
private static final String JavaDoc TAG_SORTERS= "sorters"; //$NON-NLS-1$
37
private static final String JavaDoc TAG_DEFAULT_SORTERS= "defaultSorters"; //$NON-NLS-1$
38
private static final String JavaDoc TAG_ELEMENT= "element"; //$NON-NLS-1$
39
private static final String JavaDoc TAG_PAGE_ID= "pageId"; //$NON-NLS-1$
40
private static final String JavaDoc TAG_SORTER_ID= "sorterId"; //$NON-NLS-1$
41

42     private static Map JavaDoc fgLastCheckedForType= new HashMap JavaDoc(5);
43
44     private SearchResultViewer fViewer;
45     private String JavaDoc fPageId;
46     private Menu fMenu;
47     private Map JavaDoc fLastCheckedForType;
48
49     public SortDropDownAction(SearchResultViewer viewer) {
50         super(SearchMessages.SortDropDownAction_label);
51         SearchPluginImages.setImageDescriptors(this, SearchPluginImages.T_LCL, SearchPluginImages.IMG_LCL_SEARCH_SORT);
52         fViewer= viewer;
53         setToolTipText(SearchMessages.SortDropDownAction_tooltip);
54         setMenuCreator(this);
55         fLastCheckedForType= new HashMap JavaDoc(5);
56     }
57
58     public void dispose() {
59         if (fMenu != null && !fMenu.isDisposed())
60             fMenu.dispose();
61         fMenu= null;
62     }
63
64     public Menu getMenu(Control parent) {
65         return null;
66     }
67
68     void setPageId(String JavaDoc pageId) {
69         fPageId= pageId;
70         SorterDescriptor sorterDesc= (SorterDescriptor)fLastCheckedForType.get(pageId);
71         if (sorterDesc == null)
72             sorterDesc= (SorterDescriptor)fgLastCheckedForType.get(pageId);
73         if (sorterDesc == null)
74             sorterDesc= findSorter(fPageId);
75         if (sorterDesc != null) {
76             setChecked(sorterDesc);
77             fViewer.setSorter(sorterDesc.createObject());
78         } else {
79             // Use default sort workbench viewer sorter
80
fViewer.setSorter(new WorkbenchViewerSorter());
81         }
82     }
83
84     public Menu getMenu(final Menu parent) {
85         dispose(); // ensure old menu gets disposed
86

87         fMenu= new Menu(parent);
88         
89         Iterator JavaDoc iter= SearchPlugin.getDefault().getSorterDescriptors().iterator();
90         while (iter.hasNext()) {
91             Object JavaDoc value= fLastCheckedForType.get(fPageId);
92             final String JavaDoc checkedId;
93             if (value instanceof SorterDescriptor)
94                 checkedId= ((SorterDescriptor)value).getId();
95             else
96                 checkedId= ""; //$NON-NLS-1$
97

98             final SorterDescriptor sorterDesc= (SorterDescriptor) iter.next();
99             if (!sorterDesc.getPageId().equals(fPageId) && !sorterDesc.getPageId().equals("*")) //$NON-NLS-1$
100
continue;
101             final ViewerSorter sorter= sorterDesc.createObject();
102             if (sorter != null) {
103                 final Action action= new Action() {
104                     public void run() {
105                         if (!checkedId.equals(sorterDesc.getId())) {
106                             SortDropDownAction.this.setChecked(sorterDesc);
107                             BusyIndicator.showWhile(parent.getDisplay(), new Runnable JavaDoc() {
108                                 public void run() {
109                                     fViewer.setSorter(sorter);
110                                 }
111                             });
112                         }
113                     }
114                 };
115                 action.setText(sorterDesc.getLabel());
116                 action.setImageDescriptor(sorterDesc.getImage());
117                 action.setToolTipText(sorterDesc.getToolTipText());
118                 action.setChecked(checkedId.equals(sorterDesc.getId()));
119                 addActionToMenu(fMenu, action);
120             }
121         }
122         return fMenu;
123     }
124
125     protected void addActionToMenu(Menu parent, Action action) {
126         ActionContributionItem item= new ActionContributionItem(action);
127         item.fill(parent, -1);
128     }
129
130     public void run() {
131         // nothing to do
132
}
133
134     private SorterDescriptor findSorter(String JavaDoc pageId) {
135         Iterator JavaDoc iter= SearchPlugin.getDefault().getSorterDescriptors().iterator();
136         while (iter.hasNext()) {
137             SorterDescriptor sorterDesc= (SorterDescriptor)iter.next();
138             if (sorterDesc.getPageId().equals(pageId) || sorterDesc.getPageId().equals("*")) //$NON-NLS-1$
139
return sorterDesc;
140         }
141         return null;
142     }
143
144     private SorterDescriptor getSorter(String JavaDoc sorterId) {
145         Iterator JavaDoc iter= SearchPlugin.getDefault().getSorterDescriptors().iterator();
146         while (iter.hasNext()) {
147             SorterDescriptor sorterDesc= (SorterDescriptor)iter.next();
148             if (sorterDesc.getId().equals(sorterId))
149                 return sorterDesc;
150         }
151         return null;
152     }
153
154     private void setChecked(SorterDescriptor sorterDesc) {
155         fLastCheckedForType.put(fPageId, sorterDesc);
156         fgLastCheckedForType.put(fPageId, sorterDesc);
157     }
158
159     /**
160      * Disposes this action's menu and returns a new unused instance.
161      */

162     SortDropDownAction renew() {
163         SortDropDownAction action= new SortDropDownAction(fViewer);
164         action.fLastCheckedForType= fLastCheckedForType;
165         action.fPageId= fPageId;
166         dispose();
167         return action;
168     }
169
170     //--- Persistency -------------------------------------------------
171

172     void restoreState(IMemento memento) {
173         if (fLastCheckedForType.isEmpty())
174             restoreState(memento, fLastCheckedForType, TAG_SORTERS);
175         if (fgLastCheckedForType.isEmpty())
176             restoreState(memento, fgLastCheckedForType, TAG_DEFAULT_SORTERS);
177     }
178
179     private void restoreState(IMemento memento, Map JavaDoc map, String JavaDoc mapName) {
180         memento= memento.getChild(mapName);
181         if (memento == null)
182             return;
183         IMemento[] mementoElements= memento.getChildren(TAG_ELEMENT);
184         for (int i= 0; i < mementoElements.length; i++) {
185             String JavaDoc pageId= mementoElements[i].getString(TAG_PAGE_ID);
186             String JavaDoc sorterId= mementoElements[i].getString(TAG_SORTER_ID);
187             SorterDescriptor sorterDesc= getSorter(sorterId);
188             if (sorterDesc != null)
189                 map.put(pageId, sorterDesc);
190         }
191     }
192     
193     void saveState(IMemento memento) {
194         saveState(memento, fgLastCheckedForType, TAG_DEFAULT_SORTERS);
195         saveState(memento, fLastCheckedForType, TAG_SORTERS);
196     }
197     
198     private void saveState(IMemento memento, Map JavaDoc map, String JavaDoc mapName) {
199         Iterator JavaDoc iter= map.entrySet().iterator();
200         memento= memento.createChild(mapName);
201         while (iter.hasNext()) {
202             IMemento mementoElement= memento.createChild(TAG_ELEMENT);
203             Map.Entry JavaDoc entry= (Map.Entry JavaDoc)iter.next();
204             mementoElement.putString(TAG_PAGE_ID, (String JavaDoc)entry.getKey());
205             mementoElement.putString(TAG_SORTER_ID, ((SorterDescriptor)entry.getValue()).getId());
206         }
207     }
208
209     int getSorterCount() {
210         int count= 0;
211         Iterator JavaDoc iter= SearchPlugin.getDefault().getSorterDescriptors().iterator();
212         while (iter.hasNext()) {
213             SorterDescriptor sorterDesc= (SorterDescriptor)iter.next();
214             if (sorterDesc.getPageId().equals(fPageId) || sorterDesc.getPageId().equals("*")) //$NON-NLS-1$
215
count++;
216         }
217         return count;
218     }
219 }
220
Popular Tags