Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37312cc133d2cf692d9b1878772cb6164d88c20c (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
 *  Copyright (c) 2000, 2012 IBM Corporation 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:
 *     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.cdt.dsf.debug.internal.ui.IDsfDebugHelpContextIds;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.TrayDialog;
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.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;
import org.eclipse.ui.PlatformUI;

/**
 * Provides a dialog for changing the maximum length allowed in the detail pane
 *
 * @since 3.0
 */
public class DetailPaneMaxLengthDialog extends TrayDialog {

	private static final String SETTINGS_ID = DsfUIPlugin.PLUGIN_ID + ".MAX_DETAILS_LENGTH_DIALOG"; //$NON-NLS-1$

	private Text fTextWidget;
	private Text fErrorTextWidget;
	private String fErrorMessage;
	private String fValue;
	private IInputValidator fValidator;

	/**
	 * Constructs a new dialog on the given shell.
	 *
	 * @param parent shell
	 */
	public DetailPaneMaxLengthDialog(Shell parent) {
		super(parent);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		fValue = Integer.toString(
				DsfUIPlugin.getDefault().getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH));
		fValidator = new IInputValidator() {
			@Override
			public String isValid(String newText) {
				try {
					int num = Integer.parseInt(newText);
					if (num < 0) {
						return MessagesForDetailPane.PaneMaxLengthDialog_IntegerCannotBeNegative;
					}
				} catch (NumberFormatException e) {
					return MessagesForDetailPane.PaneMaxLengthDialog_EnterAnInteger;
				}
				return null;
			}

		};
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.SelectionDialog#getDialogBoundsSettings()
	 */
	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		IDialogSettings settings = DsfUIPlugin.getDefault().getDialogSettings();
		IDialogSettings section = settings.getSection(SETTINGS_ID);
		if (section == null) {
			section = settings.addNewSection(SETTINGS_ID);
		}
		return section;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		getShell().setText(MessagesForDetailPane.PaneMaxLengthDialog_ConfigureDetails);
		Control contents = super.createContents(parent);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(getDialogArea(),
				IDsfDebugHelpContextIds.DETAIL_PANE_MAX_LENGTH_ACTION);
		return contents;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		Label label = new Label(composite, SWT.WRAP);
		label.setText(MessagesForDetailPane.PaneMaxLengthDialog_MaxCharactersToDisplay);
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_CENTER);
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
		label.setLayoutData(data);
		label.setFont(parent.getFont());
		fTextWidget = new Text(composite, SWT.SINGLE | SWT.BORDER);
		fTextWidget.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		fTextWidget.setText(fValue);
		fTextWidget.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validateInput();
				fValue = fTextWidget.getText();
			}
		});
		fErrorTextWidget = new Text(composite, SWT.READ_ONLY);
		fErrorTextWidget.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		fErrorTextWidget.setBackground(fErrorTextWidget.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		setErrorMessage(fErrorMessage);
		applyDialogFont(composite);
		return composite;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		String text = getValue();
		try {
			DsfUIPlugin.getDefault().getPreferenceStore().setValue(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH,
					Integer.parseInt(text));
		} catch (NumberFormatException e) {
			DsfUIPlugin.log(e);
		}
		super.okPressed();
	}

	/**
	 * Returns the string typed into this input dialog.
	 *
	 * @return the input string
	 * @since 3.3
	 */
	public String getValue() {
		return fValue;
	}

	/**
	 * Validates the current input
	 * @since 3.3
	 */
	private void validateInput() {
		String errorMessage = null;
		if (fValidator != null) {
			errorMessage = fValidator.isValid(fTextWidget.getText());
		}
		setErrorMessage(errorMessage);
	}

	/**
	 * Sets the current error message or none if null
	 * @param errorMessage
	 * @since 3.3
	 */
	public void setErrorMessage(String errorMessage) {
		fErrorMessage = errorMessage;
		if (fErrorTextWidget != null && !fErrorTextWidget.isDisposed()) {
			fErrorTextWidget.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
			fErrorTextWidget.getParent().update();
			// Access the ok button by id, in case clients have overridden button creation.
			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
			Control button = getButton(IDialogConstants.OK_ID);
			if (button != null) {
				button.setEnabled(errorMessage == null);
			}
		}
	}
}

Back to the top