KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > FoldingActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.actions;
12
13 import org.eclipse.jface.text.ITextViewer;
14 import org.eclipse.jface.text.source.projection.IProjectionListener;
15 import org.eclipse.jface.text.source.projection.ProjectionViewer;
16 import org.eclipse.ui.actions.ActionGroup;
17 import org.eclipse.ui.editors.text.IFoldingCommandIds;
18 import org.eclipse.ui.texteditor.ITextEditor;
19 import org.eclipse.ui.texteditor.TextOperationAction;
20
21
22 /**
23  * Groups the Ant folding actions.
24  *
25  * @since 3.1
26  */

27 public class FoldingActionGroup extends ActionGroup {
28     private ProjectionViewer fViewer;
29     
30     private TextOperationAction fToggle;
31     private TextOperationAction fExpand;
32     private TextOperationAction fCollapse;
33     private TextOperationAction fExpandAll;
34
35     private IProjectionListener fProjectionListener;
36     
37     /**
38      * Creates a new projection action group for <code>editor</code>. If the
39      * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
40      * action group is disabled.
41      *
42      * @param editor the text editor to operate on
43      * @param viewer the viewer of the editor
44      */

45     public FoldingActionGroup(ITextEditor editor, ITextViewer viewer) {
46         if (viewer instanceof ProjectionViewer) {
47             fViewer= (ProjectionViewer) viewer;
48             
49             fProjectionListener= new IProjectionListener() {
50
51                 public void projectionEnabled() {
52                     update();
53                 }
54
55                 public void projectionDisabled() {
56                     update();
57                 }
58             };
59             
60             fViewer.addProjectionListener(fProjectionListener);
61             
62             fToggle= new TextOperationAction(AntEditorActionMessages.getResourceBundle(), "Projection.Toggle.", editor, ProjectionViewer.TOGGLE, true); //$NON-NLS-1$
63
fToggle.setChecked(true);
64             fToggle.setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
65             editor.setAction("FoldingToggle", fToggle); //$NON-NLS-1$
66

67             fExpandAll= new TextOperationAction(AntEditorActionMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
68
fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
69             editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
70

71             fExpand= new TextOperationAction(AntEditorActionMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
72
fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
73             editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
74

75             fCollapse= new TextOperationAction(AntEditorActionMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
76
fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
77             editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
78
}
79     }
80     
81     /**
82      * Returns <code>true</code> if the group is enabled.
83      * <pre>
84      * Invariant: isEnabled() <=> fViewer and all actions are != null.
85      * </pre>
86      *
87      * @return <code>true</code> if the group is enabled
88      */

89     private boolean isEnabled() {
90         return fViewer != null;
91     }
92     
93     /*
94      * @see org.eclipse.ui.actions.ActionGroup#dispose()
95      */

96     public void dispose() {
97         if (isEnabled()) {
98             fViewer.removeProjectionListener(fProjectionListener);
99             fViewer= null;
100         }
101         super.dispose();
102     }
103     
104     /**
105      * Updates the actions.
106      */

107     protected void update() {
108         if (isEnabled()) {
109             fToggle.update();
110             fToggle.setChecked(fViewer.getProjectionAnnotationModel() != null);
111             fExpand.update();
112             fExpandAll.update();
113             fCollapse.update();
114         }
115     }
116     
117     /*
118      * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
119      */

120     public void updateActionBars() {
121         update();
122     }
123 }
124
Popular Tags