001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.events.MouseEvent;
013: import org.eclipse.swt.events.MouseListener;
014: import org.eclipse.swt.widgets.Control;
015: import org.eclipse.swt.widgets.Event;
016:
017: import org.eclipse.jface.action.IAction;
018: import org.eclipse.jface.action.IMenuListener;
019: import org.eclipse.jface.action.IMenuManager;
020: import org.eclipse.jface.viewers.ISelection;
021:
022: import org.eclipse.jface.text.source.IVerticalRulerInfo;
023:
024: import org.eclipse.ui.IActionDelegate2;
025: import org.eclipse.ui.IEditorActionDelegate;
026: import org.eclipse.ui.IEditorPart;
027: import org.eclipse.ui.actions.ActionDelegate;
028:
029: /**
030: * This class serves as an adapter for actions contributed to the vertical ruler's
031: * context menu. This adapter provides the contributed actions access to their editor
032: * and the editor's vertical ruler. These actions gain only limited access to the vertical
033: * ruler as defined by <code>IVerticalRulerInfo</code>. The adapter updates the
034: * adapter (inner) action on menu and mouse action on the vertical ruler.<p>
035: * Extending classes must implement the factory method
036: * <code>createAction(ITextEditor editor, IVerticalRulerInfo)</code>.
037: *
038: * @since 2.0
039: */
040: public abstract class AbstractRulerActionDelegate extends
041: ActionDelegate implements IEditorActionDelegate,
042: IActionDelegate2, MouseListener, IMenuListener {
043:
044: /** The editor. */
045: private ITextEditor fEditor;
046: /** The action calling the action delegate. */
047: private IAction fCallerAction;
048: /** The underlying action. */
049: private IAction fAction;
050:
051: /**
052: * The factory method creating the underlying action.
053: *
054: * @param editor the editor the action to be created will work on
055: * @param rulerInfo the vertical ruler the action to be created will work on
056: * @return the created action
057: */
058: protected abstract IAction createAction(ITextEditor editor,
059: IVerticalRulerInfo rulerInfo);
060:
061: /*
062: * @see IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
063: */
064: public void setActiveEditor(IAction callerAction,
065: IEditorPart targetEditor) {
066: if (fEditor != null) {
067: IVerticalRulerInfo rulerInfo = (IVerticalRulerInfo) fEditor
068: .getAdapter(IVerticalRulerInfo.class);
069: if (rulerInfo != null) {
070: Control control = rulerInfo.getControl();
071: if (control != null && !control.isDisposed())
072: control.removeMouseListener(this );
073: }
074:
075: if (fEditor instanceof ITextEditorExtension)
076: ((ITextEditorExtension) fEditor)
077: .removeRulerContextMenuListener(this );
078: }
079:
080: fEditor = (ITextEditor) (targetEditor == null ? null
081: : targetEditor.getAdapter(ITextEditor.class));
082: fCallerAction = callerAction;
083: fAction = null;
084:
085: if (fEditor != null) {
086: if (fEditor instanceof ITextEditorExtension)
087: ((ITextEditorExtension) fEditor)
088: .addRulerContextMenuListener(this );
089:
090: IVerticalRulerInfo rulerInfo = (IVerticalRulerInfo) fEditor
091: .getAdapter(IVerticalRulerInfo.class);
092: if (rulerInfo != null) {
093: fAction = createAction(fEditor, rulerInfo);
094: update();
095:
096: Control control = rulerInfo.getControl();
097: if (control != null && !control.isDisposed())
098: control.addMouseListener(this );
099: }
100: }
101: }
102:
103: /*
104: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
105: */
106: public void run(IAction callerAction) {
107: if (fAction != null)
108: fAction.run();
109: }
110:
111: /*
112: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
113: * @since 3.2
114: */
115: public void runWithEvent(IAction action, Event event) {
116: if (fAction != null)
117: fAction.runWithEvent(event);
118: }
119:
120: /*
121: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
122: */
123: public void selectionChanged(IAction action, ISelection selection) {
124: /*
125: * This is a ruler action - don't update on selection.
126: * The call was introduced to support annotation roll-overs
127: * but this seems not to be needed.
128: */
129: // update();
130: }
131:
132: /**
133: * Updates to the current state.
134: */
135: private void update() {
136: if (fAction instanceof IUpdate) {
137: ((IUpdate) fAction).update();
138: if (fCallerAction != null) {
139: fCallerAction.setText(fAction.getText());
140: fCallerAction.setEnabled(fAction.isEnabled());
141: }
142: }
143: }
144:
145: /*
146: * @see IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
147: */
148: public void menuAboutToShow(IMenuManager manager) {
149: update();
150: }
151:
152: /*
153: * @see MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
154: */
155: public void mouseDoubleClick(MouseEvent e) {
156: }
157:
158: /*
159: * @see MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
160: */
161: public void mouseDown(MouseEvent e) {
162: update();
163: }
164:
165: /*
166: * @see MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
167: */
168: public void mouseUp(MouseEvent e) {
169: }
170: }
|