1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.preference.IPreferenceStore; 18 19 import org.eclipse.jface.text.ITextOperationTarget; 20 import org.eclipse.jface.text.ITextViewer; 21 import org.eclipse.jface.text.source.projection.IProjectionListener; 22 import org.eclipse.jface.text.source.projection.ProjectionViewer; 23 24 import org.eclipse.ui.actions.ActionGroup; 25 import org.eclipse.ui.texteditor.ITextEditor; 26 import org.eclipse.ui.texteditor.IUpdate; 27 import org.eclipse.ui.texteditor.ResourceAction; 28 import org.eclipse.ui.texteditor.TextOperationAction; 29 30 import org.eclipse.ui.editors.text.IFoldingCommandIds; 31 32 import org.eclipse.jdt.ui.PreferenceConstants; 33 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 37 38 39 44 public class FoldingActionGroup extends ActionGroup { 45 private static abstract class PreferenceAction extends ResourceAction implements IUpdate { 46 PreferenceAction(ResourceBundle bundle, String prefix, int style) { 47 super(bundle, prefix, style); 48 } 49 } 50 51 54 private class FoldingAction extends PreferenceAction { 55 56 FoldingAction(ResourceBundle bundle, String prefix) { 57 super(bundle, prefix, IAction.AS_PUSH_BUTTON); 58 } 59 60 public void update() { 61 setEnabled(FoldingActionGroup.this.isEnabled() && fViewer.isProjectionMode()); 62 } 63 64 } 65 66 private ProjectionViewer fViewer; 67 68 private final PreferenceAction fToggle; 69 private final TextOperationAction fExpand; 70 private final TextOperationAction fCollapse; 71 private final TextOperationAction fExpandAll; 72 private final IProjectionListener fProjectionListener; 73 74 75 private final PreferenceAction fRestoreDefaults; 76 private final FoldingAction fCollapseMembers; 77 private final FoldingAction fCollapseComments; 78 private final TextOperationAction fCollapseAll; 79 80 81 89 public FoldingActionGroup(final ITextEditor editor, ITextViewer viewer) { 90 if (!(viewer instanceof ProjectionViewer)) { 91 fToggle= null; 92 fExpand= null; 93 fCollapse= null; 94 fExpandAll= null; 95 fCollapseAll= null; 96 fRestoreDefaults= null; 97 fCollapseMembers= null; 98 fCollapseComments= null; 99 fProjectionListener= null; 100 return; 101 } 102 103 fViewer= (ProjectionViewer) viewer; 104 105 fProjectionListener= new IProjectionListener() { 106 107 public void projectionEnabled() { 108 update(); 109 } 110 111 public void projectionDisabled() { 112 update(); 113 } 114 }; 115 116 fViewer.addProjectionListener(fProjectionListener); 117 118 fToggle= new PreferenceAction(FoldingMessages.getResourceBundle(), "Projection.Toggle.", IAction.AS_CHECK_BOX) { public void run() { 120 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 121 boolean current= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED); 122 store.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, !current); 123 } 124 125 public void update() { 126 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); 127 128 boolean isEnabled= (target != null && target.canDoOperation(ProjectionViewer.TOGGLE)); 129 setEnabled(isEnabled); 130 } 131 }; 132 fToggle.setChecked(true); 133 fToggle.setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE); 134 editor.setAction("FoldingToggle", fToggle); 136 fExpandAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL); 138 editor.setAction("FoldingExpandAll", fExpandAll); 140 fCollapseAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.CollapseAll.", editor, ProjectionViewer.COLLAPSE_ALL, true); fCollapseAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE_ALL); 142 editor.setAction("FoldingCollapseAll", fCollapseAll); 144 fExpand= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND); 146 editor.setAction("FoldingExpand", fExpand); 148 fCollapse= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE); 150 editor.setAction("FoldingCollapse", fCollapse); 152 fRestoreDefaults= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.Restore.") { public void run() { 154 if (editor instanceof JavaEditor) { 155 JavaEditor javaEditor= (JavaEditor) editor; 156 javaEditor.resetProjection(); 157 } 158 } 159 }; 160 fRestoreDefaults.setActionDefinitionId(IFoldingCommandIds.FOLDING_RESTORE); 161 editor.setAction("FoldingRestore", fRestoreDefaults); 163 fCollapseMembers= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseMembers.") { public void run() { 165 if (editor instanceof JavaEditor) { 166 JavaEditor javaEditor= (JavaEditor) editor; 167 javaEditor.collapseMembers(); 168 } 169 } 170 }; 171 fCollapseMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_MEMBERS); 172 editor.setAction("FoldingCollapseMembers", fCollapseMembers); 174 fCollapseComments= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseComments.") { public void run() { 176 if (editor instanceof JavaEditor) { 177 JavaEditor javaEditor= (JavaEditor) editor; 178 javaEditor.collapseComments(); 179 } 180 } 181 }; 182 fCollapseComments.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_COMMENTS); 183 editor.setAction("FoldingCollapseComments", fCollapseComments); } 185 186 194 protected boolean isEnabled() { 195 return fViewer != null; 196 } 197 198 201 public void dispose() { 202 if (isEnabled()) { 203 fViewer.removeProjectionListener(fProjectionListener); 204 fViewer= null; 205 } 206 super.dispose(); 207 } 208 209 212 protected void update() { 213 if (isEnabled()) { 214 fToggle.update(); 215 fToggle.setChecked(fViewer.isProjectionMode()); 216 fExpand.update(); 217 fExpandAll.update(); 218 fCollapse.update(); 219 fCollapseAll.update(); 220 fRestoreDefaults.update(); 221 fCollapseMembers.update(); 222 fCollapseComments.update(); 223 } 224 } 225 226 231 public void fillMenu(IMenuManager manager) { 232 if (isEnabled()) { 233 update(); 234 manager.add(fToggle); 235 manager.add(fExpandAll); 236 manager.add(fExpand); 237 manager.add(fCollapse); 238 manager.add(fCollapseAll); 239 manager.add(fRestoreDefaults); 240 manager.add(fCollapseMembers); 241 manager.add(fCollapseComments); 242 } 243 } 244 245 248 public void updateActionBars() { 249 update(); 250 } 251 } 252 | Popular Tags |