1 /* 2 * Created on Jan 19, 2004 3 * 4 * Copyright (C) 2004 Sean Ruff 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 package org.un4gvn.editorviewer.internal.action; 21 22 import org.eclipse.jface.action.Action; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.MenuManager; 26 import org.eclipse.jface.action.Separator; 27 import org.eclipse.jface.viewers.ViewerSorter; 28 import org.eclipse.ui.IActionBars; 29 import org.eclipse.ui.IWorkbenchActionConstants; 30 import org.un4gvn.editorviewer.internal.EditorViewerPlugin; 31 import org.un4gvn.editorviewer.internal.Messages; 32 import org.un4gvn.editorviewer.internal.element.EditorElement; 33 import org.un4gvn.editorviewer.internal.element.FolderElement; 34 import org.un4gvn.editorviewer.internal.ui.EditorViewer; 35 36 /*** 37 * Adds view menus to switch between None,Name and Type sort modes. 38 * <p> 39 * @author Sean Ruff 40 */ 41 42 public class SortActionGroup extends MultiActionGroup { 43 44 /*** 45 * EditorViewer for callbacks 46 */ 47 private EditorViewer _viewer = null; 48 49 /*** 50 * Constructor 51 * @param viewer 52 * @param sortMode 53 */ 54 public SortActionGroup(EditorViewer viewer, int sortMode) { 55 super(createActions(viewer), sortMode); 56 _viewer = viewer; 57 //initialize our sorter 58 super.fActions[sortMode].run(); 59 } 60 61 /*** 62 * Fill the action bars with the submenu and actions 63 */ 64 public void fillActionBars(IActionBars actionBars) { 65 super.fillActionBars(actionBars); 66 contributeToViewMenu(actionBars.getMenuManager()); 67 } 68 69 /*** 70 * Contribute the submenu and actions to the MenuManager 71 * @param viewMenu MenuManager to modify 72 */ 73 private void contributeToViewMenu(IMenuManager viewMenu) { 74 viewMenu.add(new Separator()); 75 76 // Create layout sub menu 77 IMenuManager layoutSubMenu= new MenuManager(Messages.getString("SortActionGroup.0")); //$NON-NLS-1$ 78 final String layoutGroupName= Messages.getString("SortActionGroup.1"); //$NON-NLS-1$ 79 Separator marker= new Separator(layoutGroupName); 80 81 viewMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 82 viewMenu.add(marker); 83 viewMenu.appendToGroup(layoutGroupName, layoutSubMenu); 84 viewMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS+Messages.getString("SortActionGroup.2"))); //$NON-NLS-1$ 85 addActions(layoutSubMenu); 86 } 87 88 static IAction[] createActions(EditorViewer viewer) { 89 90 EditorViewerPlugin plugin = EditorViewerPlugin.getInstance(); 91 92 IAction nullSortAction = new SortAction(viewer, EditorViewerPlugin.NONE_SORT_MODE); 93 nullSortAction.setText(Messages.getString("SortActionGroup.3")); //$NON-NLS-1$ 94 nullSortAction.setToolTipText(Messages.getString("SortActionGroup.4")); //$NON-NLS-1$ 95 nullSortAction.setDescription(Messages.getString("SortActionGroup.5")); //$NON-NLS-1$ 96 nullSortAction.setImageDescriptor(plugin.getImageDescriptor(Messages.getString("SortActionGroup.6"))); //$NON-NLS-1$ 97 98 IAction nameSortAction = new SortAction(viewer, EditorViewerPlugin.NAME_SORT_MODE); 99 nameSortAction.setText(Messages.getString("SortActionGroup.7")); //$NON-NLS-1$ 100 nameSortAction.setToolTipText(Messages.getString("SortActionGroup.8")); //$NON-NLS-1$ 101 nameSortAction.setDescription(Messages.getString("SortActionGroup.9")); //$NON-NLS-1$ 102 nameSortAction.setImageDescriptor(plugin.getImageDescriptor(Messages.getString("SortActionGroup.10"))); //$NON-NLS-1$ 103 104 IAction typeSortAction = new SortAction(viewer, EditorViewerPlugin.TYPE_SORT_MODE); 105 typeSortAction.setText(Messages.getString("SortActionGroup.11")); //$NON-NLS-1$ 106 typeSortAction.setToolTipText(Messages.getString("SortActionGroup.12")); //$NON-NLS-1$ 107 typeSortAction.setDescription(Messages.getString("SortActionGroup.13")); //$NON-NLS-1$ 108 typeSortAction.setImageDescriptor(plugin.getImageDescriptor(Messages.getString("SortActionGroup.14"))); //$NON-NLS-1$ 109 110 //NOTE: the value of the SortMode parameter (second param) supplied to each SortAction instance 111 //must match the position within the following returned array 112 return new IAction[]{ nullSortAction, nameSortAction, typeSortAction}; 113 } 114 } 115 116 class SortAction extends Action implements IAction { 117 118 private static final ViewerSorter NAME_SORTER = new NameSorter(); 119 private static final ViewerSorter TYPE_SORTER = new TypeSorter(); 120 private static final ViewerSorter NULL_SORTER = null; 121 public static final ViewerSorter[] SORTERS = { NULL_SORTER, NAME_SORTER, TYPE_SORTER }; 122 123 private int _mode; 124 private EditorViewer _viewer; 125 126 public SortAction(EditorViewer viewer, int sortMode) { 127 _mode = sortMode; 128 _viewer = viewer; 129 } 130 131 /* 132 * @see org.eclipse.jface.action.IAction#run() 133 */ 134 public void run() { 135 _viewer.setSorter(SORTERS[_mode]); 136 } 137 138 139 static class NameSorter extends ViewerSorter { 140 141 } 142 143 static class TypeSorter extends ViewerSorter { 144 /* (non-Javadoc) 145 * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object) 146 */ 147 public int category(Object element) { 148 if ( element instanceof FolderElement ) 149 return 1; 150 else if ( element instanceof EditorElement ) 151 return 2; 152 else 153 return 3; 154 } 155 156 } 157 158 159 160 161 162 163 }

This page was automatically generated by Maven