001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.SWT;
013: import org.eclipse.swt.custom.StyledText;
014: import org.eclipse.swt.events.HelpListener;
015: import org.eclipse.swt.graphics.Point;
016: import org.eclipse.swt.widgets.Event;
017:
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.jface.action.IMenuCreator;
020: import org.eclipse.jface.resource.ImageDescriptor;
021: import org.eclipse.jface.util.IPropertyChangeListener;
022:
023: /**
024: * An <code>Action</code> wrapper for text widget navigation and selection actions.
025: * @since 2.0
026: */
027: public class TextNavigationAction extends Action {
028:
029: /** The text widget */
030: private StyledText fTextWidget;
031: /** The styled text action id */
032: private int fAction;
033: /** The action's action id */
034: private String fActionId;
035: /** This action's action definition id */
036: private String fActionDefinitionId;
037:
038: /**
039: * Creates a new <code>TextNavigationAction</code>.
040: * @param textWidget the text widget
041: * @param action the styled text widget action
042: */
043: public TextNavigationAction(StyledText textWidget, int action) {
044: fTextWidget = textWidget;
045: fAction = action;
046: }
047:
048: /**
049: * Returns the text widget this actions is bound to.
050: *
051: * @return returns the text widget this actions is bound to
052: */
053: protected StyledText getTextWidget() {
054: return fTextWidget;
055: }
056:
057: /*
058: * @see IAction#run()
059: */
060: public void run() {
061: Point selection = fTextWidget.getSelection();
062: fTextWidget.invokeAction(fAction);
063: fireSelectionChanged(selection);
064: }
065:
066: private void doFireSelectionChanged(Point selection) {
067: Event event = new Event();
068: event.x = selection.x;
069: event.y = selection.y;
070: fTextWidget.notifyListeners(SWT.Selection, event);
071: }
072:
073: /**
074: * Sends a selection event with the current selection to all
075: * selection listeners of the action's text widget
076: *
077: * @since 3.0
078: */
079: protected void fireSelectionChanged() {
080: fireSelectionChanged(null);
081: }
082:
083: /**
084: * Fires a selection event to all selection listener of the action's
085: * text widget if the current selection differs from the given selection.
086: *
087: * @param oldSelection the old selection
088: * @since 3.0
089: */
090: protected void fireSelectionChanged(Point oldSelection) {
091: Point selection = fTextWidget.getSelection();
092: if (oldSelection == null || !selection.equals(oldSelection))
093: doFireSelectionChanged(selection);
094: }
095:
096: /*
097: * @see IAction#runWithEvent(Event)
098: */
099: public void runWithEvent(Event event) {
100: run();
101: }
102:
103: /*
104: * @see IAction#setActionDefinitionId(String)
105: */
106: public void setActionDefinitionId(String id) {
107: fActionDefinitionId = id;
108: }
109:
110: /*
111: * @see IAction#getActionDefinitionId()
112: */
113: public String getActionDefinitionId() {
114: return fActionDefinitionId;
115: }
116:
117: /*
118: * @see IAction#setId(String)
119: */
120: public void setId(String id) {
121: fActionId = id;
122: }
123:
124: /*
125: * @see IAction#getId()
126: */
127: public String getId() {
128: return fActionId;
129: }
130:
131: // ----------------------------------------------------------------------------------------------------------------------------------
132: // All the subsequent methods are just empty method bodies.
133:
134: /*
135: * @see IAction#addPropertyChangeListener(IPropertyChangeListener)
136: */
137: public void addPropertyChangeListener(
138: IPropertyChangeListener listener) {
139: }
140:
141: /*
142: * @see IAction#getAccelerator()
143: */
144: public int getAccelerator() {
145: return 0;
146: }
147:
148: /*
149: * @see IAction#getDescription()
150: */
151: public String getDescription() {
152: return null;
153: }
154:
155: /*
156: * @see IAction#getDisabledImageDescriptor()
157: */
158: public ImageDescriptor getDisabledImageDescriptor() {
159: return null;
160: }
161:
162: /*
163: * @see IAction#getHelpListener()
164: */
165: public HelpListener getHelpListener() {
166: return null;
167: }
168:
169: /*
170: * @see IAction#getHoverImageDescriptor()
171: */
172: public ImageDescriptor getHoverImageDescriptor() {
173: return null;
174: }
175:
176: /*
177: * @see IAction#getImageDescriptor()
178: */
179: public ImageDescriptor getImageDescriptor() {
180: return null;
181: }
182:
183: /*
184: * @see IAction#getMenuCreator()
185: */
186: public IMenuCreator getMenuCreator() {
187: return null;
188: }
189:
190: /*
191: * @see IAction#getStyle()
192: */
193: public int getStyle() {
194: return 0;
195: }
196:
197: /*
198: * @see IAction#getText()
199: */
200: public String getText() {
201: return null;
202: }
203:
204: /*
205: * @see IAction#getToolTipText()
206: */
207: public String getToolTipText() {
208: return null;
209: }
210:
211: /*
212: * @see IAction#isChecked()
213: */
214: public boolean isChecked() {
215: return false;
216: }
217:
218: /*
219: * @see IAction#isEnabled()
220: */
221: public boolean isEnabled() {
222: return true;
223: }
224:
225: /*
226: * @see IAction#removePropertyChangeListener(IPropertyChangeListener)
227: */
228: public void removePropertyChangeListener(
229: IPropertyChangeListener listener) {
230: }
231:
232: /*
233: * @see org.eclipse.jface.action.IAction#setAccelerator(int)
234: */
235: public void setAccelerator(int keycode) {
236: }
237:
238: /*
239: * @see IAction#setChecked(boolean)
240: */
241: public void setChecked(boolean checked) {
242: }
243:
244: /*
245: * @see IAction#setDescription(String)
246: */
247: public void setDescription(String text) {
248: }
249:
250: /*
251: * @see IAction#setDisabledImageDescriptor(ImageDescriptor)
252: */
253: public void setDisabledImageDescriptor(ImageDescriptor newImage) {
254: }
255:
256: /*
257: * @see IAction#setEnabled(boolean)
258: */
259: public void setEnabled(boolean enabled) {
260: }
261:
262: /*
263: * @see IAction#setHelpListener(HelpListener)
264: */
265: public void setHelpListener(HelpListener listener) {
266: }
267:
268: /*
269: * @see IAction#setHoverImageDescriptor(ImageDescriptor)
270: */
271: public void setHoverImageDescriptor(ImageDescriptor newImage) {
272: }
273:
274: /*
275: * @see IAction#setImageDescriptor(ImageDescriptor)
276: */
277: public void setImageDescriptor(ImageDescriptor newImage) {
278: }
279:
280: /*
281: * @see IAction#setMenuCreator(IMenuCreator)
282: */
283: public void setMenuCreator(IMenuCreator creator) {
284: }
285:
286: /*
287: * @see IAction#setText(String)
288: */
289: public void setText(String text) {
290: }
291:
292: /*
293: * @see IAction#setToolTipText(String)
294: */
295: public void setToolTipText(String text) {
296: }
297: }
|