Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b892aec324116e539c42884478e30c2558236ed2 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2004, 2012 QNX Software 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:
 * QNX Software Systems - Initial API and implementation
 * IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions;

import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * The "Add Expression" dialog.
 */
public class ExpressionDialog extends Dialog {

	private Button fBtnOk = null;

	private Text fTextExpression;

	private String fExpression = ""; //$NON-NLS-1$

	/**
	 * Constructor for ExpressionDialog.
	 *
	 * @param parentShell
	 */
	public ExpressionDialog(Shell parentShell, String expression) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		if (expression != null)
			fExpression = expression;
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(ActionMessages.getString("ExpressionDialog.0")); //$NON-NLS-1$
		shell.setImage(DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_EXPRESSION));
	}

	@Override
	protected Control createContents(Composite parent) {
		Control control = super.createContents(parent);
		setOkButtonState();
		return control;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		((GridLayout) composite.getLayout()).marginWidth = 10;
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		createDataWidgets(composite);
		initializeDataWidgets();
		return composite;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		fBtnOk = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	private void createDataWidgets(Composite parent) {
		fTextExpression = createExpressionText(parent);
	}

	private void initializeDataWidgets() {
		fTextExpression.setText(fExpression);
		fTextExpression.setSelection(fExpression.length());
		fTextExpression.selectAll();
		setOkButtonState();
	}

	private Text createExpressionText(Composite parent) {
		Label label = new Label(parent, SWT.RIGHT);
		label.setText(ActionMessages.getString("ExpressionDialog.1")); //$NON-NLS-1$
		final Text text = new Text(parent, SWT.BORDER);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 300;
		text.setLayoutData(gridData);
		addModifyListener(text);
		return text;
	}

	protected void setOkButtonState() {
		if (fBtnOk == null)
			return;
		fBtnOk.setEnabled(fTextExpression.getText().trim().length() > 0);
	}

	private void storeData() {
		fExpression = fTextExpression.getText().trim();
	}

	private void addModifyListener(Text text) {
		text.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				setOkButtonState();
			}
		});
	}

	public String getExpression() {
		return fExpression;
	}

	@Override
	protected void okPressed() {
		storeData();
		super.okPressed();
	}
}

Back to the top