1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 33 class SortDropDownAction extends Action implements IMenuCreator { 34 35 private static final String TAG_SORTERS= "sorters"; private static final String TAG_DEFAULT_SORTERS= "defaultSorters"; private static final String TAG_ELEMENT= "element"; private static final String TAG_PAGE_ID= "pageId"; private static final String TAG_SORTER_ID= "sorterId"; 42 private static Map fgLastCheckedForType= new HashMap (5); 43 44 private SearchResultViewer fViewer; 45 private String fPageId; 46 private Menu fMenu; 47 private Map 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 (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 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 fViewer.setSorter(new WorkbenchViewerSorter()); 81 } 82 } 83 84 public Menu getMenu(final Menu parent) { 85 dispose(); 87 fMenu= new Menu(parent); 88 89 Iterator iter= SearchPlugin.getDefault().getSorterDescriptors().iterator(); 90 while (iter.hasNext()) { 91 Object value= fLastCheckedForType.get(fPageId); 92 final String checkedId; 93 if (value instanceof SorterDescriptor) 94 checkedId= ((SorterDescriptor)value).getId(); 95 else 96 checkedId= ""; 98 final SorterDescriptor sorterDesc= (SorterDescriptor) iter.next(); 99 if (!sorterDesc.getPageId().equals(fPageId) && !sorterDesc.getPageId().equals("*")) 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 () { 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 } 133 134 private SorterDescriptor findSorter(String pageId) { 135 Iterator iter= SearchPlugin.getDefault().getSorterDescriptors().iterator(); 136 while (iter.hasNext()) { 137 SorterDescriptor sorterDesc= (SorterDescriptor)iter.next(); 138 if (sorterDesc.getPageId().equals(pageId) || sorterDesc.getPageId().equals("*")) return sorterDesc; 140 } 141 return null; 142 } 143 144 private SorterDescriptor getSorter(String sorterId) { 145 Iterator 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 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 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 map, String 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 pageId= mementoElements[i].getString(TAG_PAGE_ID); 186 String 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 map, String mapName) { 199 Iterator iter= map.entrySet().iterator(); 200 memento= memento.createChild(mapName); 201 while (iter.hasNext()) { 202 IMemento mementoElement= memento.createChild(TAG_ELEMENT); 203 Map.Entry entry= (Map.Entry )iter.next(); 204 mementoElement.putString(TAG_PAGE_ID, (String )entry.getKey()); 205 mementoElement.putString(TAG_SORTER_ID, ((SorterDescriptor)entry.getValue()).getId()); 206 } 207 } 208 209 int getSorterCount() { 210 int count= 0; 211 Iterator iter= SearchPlugin.getDefault().getSorterDescriptors().iterator(); 212 while (iter.hasNext()) { 213 SorterDescriptor sorterDesc= (SorterDescriptor)iter.next(); 214 if (sorterDesc.getPageId().equals(fPageId) || sorterDesc.getPageId().equals("*")) count++; 216 } 217 return count; 218 } 219 } 220 | Popular Tags |