Action borrowed from cfeclipse: "remove trailing spaces at end of file"
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / FoldingActionGroup.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import org.eclipse.jface.action.IMenuManager;
14 import org.eclipse.jface.text.ITextViewer;
15 import org.eclipse.jface.text.source.projection.IProjectionListener;
16 import org.eclipse.jface.text.source.projection.ProjectionViewer;
17 import org.eclipse.ui.actions.ActionGroup;
18 import org.eclipse.ui.editors.text.IFoldingCommandIds;
19 import org.eclipse.ui.texteditor.ITextEditor;
20 import org.eclipse.ui.texteditor.TextOperationAction;
21
22
23 /**
24  * Groups the JDT folding actions.
25  *  
26  * @since 3.0
27  */
28 public class FoldingActionGroup extends ActionGroup {
29         private ProjectionViewer fViewer;
30         
31         private TextOperationAction fToggle;
32         private TextOperationAction fExpand;
33         private TextOperationAction fCollapse;
34         private TextOperationAction fExpandAll;
35
36         private IProjectionListener fProjectionListener;
37         
38         /**
39          * Creates a new projection action group for <code>editor</code>. If the
40          * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
41          * action group is disabled.
42          * 
43          * @param editor the text editor to operate on
44          * @param viewer the viewer of the editor
45          */
46         public FoldingActionGroup(ITextEditor editor, ITextViewer viewer) {
47                 if (viewer instanceof ProjectionViewer) {
48                         fViewer= (ProjectionViewer) viewer;
49                         
50                         fProjectionListener= new IProjectionListener() {
51
52                                 public void projectionEnabled() {
53                                         update();
54                                 }
55
56                                 public void projectionDisabled() {
57                                         update();
58                                 }
59                         };
60                         
61                         fViewer.addProjectionListener(fProjectionListener);
62                         
63                         fToggle= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Toggle.", editor, ProjectionViewer.TOGGLE, true); //$NON-NLS-1$
64                         fToggle.setChecked(true);
65                         fToggle.setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
66                         editor.setAction("FoldingToggle", fToggle); //$NON-NLS-1$
67                         
68                         fExpandAll= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
69                         fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
70                         editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
71                         
72                         fExpand= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
73                         fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
74                         editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
75                         
76                         fCollapse= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
77                         fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
78                         editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
79                 }
80         }
81         
82         /**
83          * Returns <code>true</code> if the group is enabled. 
84          * <pre>
85          * Invariant: isEnabled() <=> fViewer and all actions are != null.
86          * </pre>
87          * 
88          * @return <code>true</code> if the group is enabled
89          */
90         protected boolean isEnabled() {
91                 return fViewer != null;
92         }
93         
94         /*
95          * @see org.eclipse.ui.actions.ActionGroup#dispose()
96          */
97         public void dispose() {
98                 if (isEnabled()) {
99                         fViewer.removeProjectionListener(fProjectionListener);
100                         fViewer= null;
101                 }
102                 super.dispose();
103         }
104         
105         /**
106          * Updates the actions.
107          */
108         protected void update() {
109                 if (isEnabled()) {
110                         fToggle.update();
111                         fToggle.setChecked(fViewer.getProjectionAnnotationModel() != null);
112                         fExpand.update();
113                         fExpandAll.update();
114                         fCollapse.update();
115                 }
116         }
117         
118         /**
119          * Fills the menu with all folding actions.
120          * 
121          * @param manager the menu manager for the folding submenu
122          */
123         public void fillMenu(IMenuManager manager) {
124                 if (isEnabled()) {
125                         update();
126                         manager.add(fToggle);
127                         manager.add(fExpandAll);
128                         manager.add(fExpand);
129                         manager.add(fCollapse);
130                 }
131         }
132         
133         /*
134          * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
135          */
136         public void updateActionBars() {
137                 update();
138         }
139 }