Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62a474209a7ed379adc1368f8c10a200c7603a3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 *  Copyright (c) 2006, 2009 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *     Wind River Systems, Inc. - extended implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.internal.ui.viewmodel.detailsupport;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.ui.texteditor.IUpdate;

/**
 * Common function for actions that operate on a text viewer.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @since 3.0
 */
public class TextViewerAction extends Action implements IUpdate {

    private int fOperationCode= -1;
    private ITextOperationTarget fOperationTarget;

    /**
     * Constructs a new action in the given text viewer with
     * the specified operation code.
     * 
     * @param viewer
     * @param operationCode
     */
    public TextViewerAction(ITextViewer viewer, int operationCode) {
        fOperationCode= operationCode;
        fOperationTarget= viewer.getTextOperationTarget();
        update();
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.texteditor.IUpdate#update()
     * 
     * Updates the enabled state of the action.
     * Fires a property change if the enabled state changes.
     * 
     * @see org.eclipse.jface.action.Action#firePropertyChange(String, Object, Object)
     */
    @Override
	public void update() {

        boolean wasEnabled= isEnabled();
        boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
        setEnabled(isEnabled);

        if (wasEnabled != isEnabled) {
            firePropertyChange(ENABLED, wasEnabled ? Boolean.TRUE : Boolean.FALSE, isEnabled ? Boolean.TRUE : Boolean.FALSE);
        }
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IAction#run() 
     */
    @Override
    public void run() {
        if (fOperationCode != -1 && fOperationTarget != null) {
            fOperationTarget.doOperation(fOperationCode);
        }
    }
    
    /**
     * Configures this action with a label, tool tip, and description.
     * 
     * @param text action label
     * @param toolTipText action tool tip
     * @param description action description
     */
    public void configureAction(String text, String toolTipText, String description) {
        setText(text);
        setToolTipText(toolTipText);
        setDescription(description);
    }
}

Back to the top