KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > AbstractRulerActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12
13 package org.eclipse.ui.texteditor;
14
15
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.events.MouseListener;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Event;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.viewers.ISelection;
25
26 import org.eclipse.jface.text.source.IVerticalRulerInfo;
27
28 import org.eclipse.ui.IActionDelegate2;
29 import org.eclipse.ui.IEditorActionDelegate;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.actions.ActionDelegate;
32
33 /**
34  * This class serves as an adapter for actions contributed to the vertical ruler's
35  * context menu. This adapter provides the contributed actions access to their editor
36  * and the editor's vertical ruler. These actions gain only limited access to the vertical
37  * ruler as defined by <code>IVerticalRulerInfo</code>. The adapter updates the
38  * adapter (inner) action on menu and mouse action on the vertical ruler.<p>
39  * Extending classes must implement the factory method
40  * <code>createAction(ITextEditor editor, IVerticalRulerInfo)</code>.
41  *
42  * @since 2.0
43  */

44 public abstract class AbstractRulerActionDelegate extends ActionDelegate implements IEditorActionDelegate, IActionDelegate2, MouseListener, IMenuListener {
45
46     /** The editor. */
47     private ITextEditor fEditor;
48     /** The action calling the action delegate. */
49     private IAction fCallerAction;
50     /** The underlying action. */
51     private IAction fAction;
52
53     /**
54      * The factory method creating the underlying action.
55      *
56      * @param editor the editor the action to be created will work on
57      * @param rulerInfo the vertical ruler the action to be created will work on
58      * @return the created action
59      */

60     protected abstract IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo);
61
62
63     /*
64      * @see IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
65      */

66     public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
67         if (fEditor != null) {
68             IVerticalRulerInfo rulerInfo= (IVerticalRulerInfo) fEditor.getAdapter(IVerticalRulerInfo.class);
69             if (rulerInfo != null) {
70                 Control control= rulerInfo.getControl();
71                 if (control != null && !control.isDisposed())
72                     control.removeMouseListener(this);
73             }
74
75             if (fEditor instanceof ITextEditorExtension)
76                 ((ITextEditorExtension) fEditor).removeRulerContextMenuListener(this);
77         }
78
79         fEditor= (ITextEditor)(targetEditor == null ? null : targetEditor.getAdapter(ITextEditor.class));
80         fCallerAction= callerAction;
81         fAction= null;
82
83         if (fEditor != null) {
84             if (fEditor instanceof ITextEditorExtension)
85                 ((ITextEditorExtension) fEditor).addRulerContextMenuListener(this);
86
87             IVerticalRulerInfo rulerInfo= (IVerticalRulerInfo) fEditor.getAdapter(IVerticalRulerInfo.class);
88             if (rulerInfo != null) {
89                 fAction= createAction(fEditor, rulerInfo);
90                 update();
91
92                 Control control= rulerInfo.getControl();
93                 if (control != null && !control.isDisposed())
94                     control.addMouseListener(this);
95             }
96         }
97     }
98
99     /*
100      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
101      */

102     public void run(IAction callerAction) {
103         if (fAction != null)
104             fAction.run();
105     }
106     
107     /*
108      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
109      * @since 3.2
110      */

111     public void runWithEvent(IAction action, Event event) {
112         if (fAction != null)
113             fAction.runWithEvent(event);
114     }
115
116     /*
117      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
118      */

119     public void selectionChanged(IAction action, ISelection selection) {
120         /*
121          * This is a ruler action - don't update on selection.
122          * The call was introduced to support annotation roll-overs
123          * but this seems not to be needed.
124          */

125 // update();
126
}
127
128     /**
129      * Updates to the current state.
130      */

131     private void update() {
132         if (fAction instanceof IUpdate) {
133             ((IUpdate) fAction).update();
134             if (fCallerAction != null) {
135                 fCallerAction.setText(fAction.getText());
136                 fCallerAction.setEnabled(fAction.isEnabled());
137             }
138         }
139     }
140
141     /*
142      * @see IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
143      */

144     public void menuAboutToShow(IMenuManager manager) {
145         update();
146     }
147
148     /*
149      * @see MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
150      */

151     public void mouseDoubleClick(MouseEvent e) {
152     }
153
154     /*
155      * @see MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
156      */

157     public void mouseDown(MouseEvent e) {
158         update();
159     }
160
161     /*
162      * @see MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
163      */

164     public void mouseUp(MouseEvent e) {
165     }
166 }
167
Popular Tags