e40f4783a45fa73c490bc4b01d041a1037573ba2
[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
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.source.projection.IProjectionListener;
17 import org.eclipse.jface.text.source.projection.ProjectionViewer;
18
19 import org.eclipse.ui.actions.ActionGroup;
20 import org.eclipse.ui.editors.text.IFoldingCommandIds;
21 import org.eclipse.ui.texteditor.ITextEditor;
22 import org.eclipse.ui.texteditor.TextOperationAction;
23
24
25 /**
26  * Groups the JDT folding actions.
27  *  
28  * @since 3.0
29  */
30 public class FoldingActionGroup extends ActionGroup {
31         private ProjectionViewer fViewer;
32         
33         private TextOperationAction fToggle;
34         private TextOperationAction fExpand;
35         private TextOperationAction fCollapse;
36         private TextOperationAction fExpandAll;
37
38         private IProjectionListener fProjectionListener;
39         
40         /**
41          * Creates a new projection action group for <code>editor</code>. If the
42          * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
43          * action group is disabled.
44          * 
45          * @param editor the text editor to operate on
46          * @param viewer the viewer of the editor
47          */
48         public FoldingActionGroup(ITextEditor editor, ITextViewer viewer) {
49                 if (viewer instanceof ProjectionViewer) {
50                         fViewer= (ProjectionViewer) viewer;
51                         
52                         fProjectionListener= new IProjectionListener() {
53
54                                 public void projectionEnabled() {
55                                         update();
56                                 }
57
58                                 public void projectionDisabled() {
59                                         update();
60                                 }
61                         };
62                         
63                         fViewer.addProjectionListener(fProjectionListener);
64                         
65                         fToggle= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Toggle.", editor, ProjectionViewer.TOGGLE, true); //$NON-NLS-1$
66                         fToggle.setChecked(true);
67                         fToggle.setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
68                         editor.setAction("FoldingToggle", fToggle); //$NON-NLS-1$
69                         
70                         fExpandAll= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
71                         fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
72                         editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
73                         
74                         fExpand= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
75                         fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
76                         editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
77                         
78                         fCollapse= new TextOperationAction(ActionMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
79                         fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
80                         editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
81                 }
82         }
83         
84         /**
85          * Returns <code>true</code> if the group is enabled. 
86          * <pre>
87          * Invariant: isEnabled() <=> fViewer and all actions are != null.
88          * </pre>
89          * 
90          * @return <code>true</code> if the group is enabled
91          */
92         protected boolean isEnabled() {
93                 return fViewer != null;
94         }
95         
96         /*
97          * @see org.eclipse.ui.actions.ActionGroup#dispose()
98          */
99         public void dispose() {
100                 if (isEnabled()) {
101                         fViewer.removeProjectionListener(fProjectionListener);
102                         fViewer= null;
103                 }
104                 super.dispose();
105         }
106         
107         /**
108          * Updates the actions.
109          */
110         protected void update() {
111                 if (isEnabled()) {
112                         fToggle.update();
113                         fToggle.setChecked(fViewer.getProjectionAnnotationModel() != null);
114                         fExpand.update();
115                         fExpandAll.update();
116                         fCollapse.update();
117                 }
118         }
119         
120         /**
121          * Fills the menu with all folding actions.
122          * 
123          * @param manager the menu manager for the folding submenu
124          */
125         public void fillMenu(IMenuManager manager) {
126                 if (isEnabled()) {
127                         update();
128                         manager.add(fToggle);
129                         manager.add(fExpandAll);
130                         manager.add(fExpand);
131                         manager.add(fCollapse);
132                 }
133         }
134         
135         /*
136          * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
137          */
138         public void updateActionBars() {
139                 update();
140         }
141 }