KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > actions > TextViewerAction


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.ui.console.actions;
12
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.text.ITextOperationTarget;
16 import org.eclipse.jface.text.ITextViewer;
17 import org.eclipse.ui.texteditor.IUpdate;
18
19 /**
20  * Common function for actions that operate on a text viewer.
21  * <p>
22  * Clients may subclass this class.
23  * </p>
24  * @since 3.0
25  */

26 public class TextViewerAction extends Action implements IUpdate {
27
28     private int fOperationCode= -1;
29     private ITextOperationTarget fOperationTarget;
30
31     /**
32      * Constructs a new action in the given text viewer with
33      * the specified operation code.
34      *
35      * @param viewer
36      * @param operationCode
37      */

38     public TextViewerAction(ITextViewer viewer, int operationCode) {
39         fOperationCode= operationCode;
40         fOperationTarget= viewer.getTextOperationTarget();
41         update();
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.texteditor.IUpdate#update()
46      *
47      * Updates the enabled state of the action.
48      * Fires a property change if the enabled state changes.
49      *
50      * @see org.eclipse.jface.action.Action#firePropertyChange(String, Object, Object)
51      */

52     public void update() {
53
54         boolean wasEnabled= isEnabled();
55         boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
56         setEnabled(isEnabled);
57
58         if (wasEnabled != isEnabled) {
59             firePropertyChange(ENABLED, wasEnabled ? Boolean.TRUE : Boolean.FALSE, isEnabled ? Boolean.TRUE : Boolean.FALSE);
60         }
61     }
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.jface.action.IAction#run()
65      */

66     public void run() {
67         if (fOperationCode != -1 && fOperationTarget != null) {
68             fOperationTarget.doOperation(fOperationCode);
69         }
70     }
71     
72     /**
73      * Configures this action with a label, tool tip, and description.
74      *
75      * @param text action label
76      * @param toolTipText action tool tip
77      * @param description action description
78      */

79     public void configureAction(String JavaDoc text, String JavaDoc toolTipText, String JavaDoc description) {
80         setText(text);
81         setToolTipText(toolTipText);
82         setDescription(description);
83     }
84 }
85
Popular Tags