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