Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76897c4f10c6388311e34cbab7fbea82b2cafdb8 (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
88
89
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.actions.expressions;

import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
import org.eclipse.debug.internal.ui.views.expression.ExpressionView;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;

/**
 * This action activates the cell editor in the expressions view to edit the
 * expression string.  If no expression column found or for multi-line
 * expressions, revert to the
 */
public class EditWatchExpressinInPlaceAction extends Action implements ISelectionChangedListener {

    private ExpressionView fView;
    private TreeModelViewer fViewer;
    private EditWatchExpressionAction fEditActionDelegate = new EditWatchExpressionAction();

    public EditWatchExpressinInPlaceAction(ExpressionView view) {
        fView = view;
        fViewer = (TreeModelViewer)view.getViewer();
        fEditActionDelegate.init(view);
        ISelectionProvider selectionProvider = fView.getSite().getSelectionProvider();
        selectionProvider.addSelectionChangedListener(this);
        fEditActionDelegate.selectionChanged(this, selectionProvider.getSelection());
    }

    @Override
	public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = fEditActionDelegate.getCurrentSelection();
        setEnabled(selection != null && selection.size() == 1);
    }

    public void dispose() {
        fView.getSite().getSelectionProvider().removeSelectionChangedListener(this);
    }

    @Override
	public void run() {
        IStructuredSelection selelection = fEditActionDelegate.getCurrentSelection();

        if (selelection.size() != 1) {
            return;
        }

        // Always edit multi-line expressions in dialog.  Otherwise try to find the expression
        // column and activate cell editor there.
        int expressionColumn = getExpressionColumnIndex();
        IWatchExpression[] expressions = fEditActionDelegate.getSelectedExpressions();
        if (expressionColumn != -1 && !isWatchExpressionWithNewLine(expressions)) {
            fViewer.editElement(selelection.getFirstElement(), expressionColumn);
        } else if (expressions.length == 1) {
            fEditActionDelegate.run(this);
        }
    }

    private boolean isWatchExpressionWithNewLine(IWatchExpression[] expressions) {
        return expressions.length == 1 &&
            expressions[0].getExpressionText().indexOf('\n') != -1;
    }

    private int getExpressionColumnIndex() {
        Object[] columnProperties = fViewer.getColumnProperties();
        for (int i = 0; columnProperties != null && i < columnProperties.length; i++) {
            if (IDebugUIConstants.COLUMN_ID_VARIABLE_NAME.equals(columnProperties[i])) {
                return i;
            }
        }
        return -1;
    }
}

Back to the top