From afc6fa72b6ed718b3196645a9d72edf4726acd15 Mon Sep 17 00:00:00 2001 From: Sougandh S Date: Sun, 8 Nov 2026 10:20:29 +0530 Subject: [PATCH] Add Preview button to String Substitution preference page This commit adds a "Preview" button on the String Substitution pref page. It is enabled for a single selection and opens a small dialog showing the variable's name, value, description and contributing plug-in, each with a bold title. This will be useful for readability when there is long values --- .../preferences/DebugPreferencesMessages.java | 11 ++ .../DebugPreferencesMessages.properties | 11 ++ .../StringVariablePreferencePage.java | 33 ++++-- .../StringVariablePreviewDialog.java | 102 ++++++++++++++++++ 4 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreviewDialog.java diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java index 17ff5e5f2d9..0657315e6a1 100644 --- a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java @@ -157,6 +157,17 @@ public class DebugPreferencesMessages extends NLS { public static String SimpleVariablePreferencePage_7; public static String SimpleVariablePreferencePage_8; public static String SimpleVariablePreferencePage_9; + public static String SimpleVariablePreferencePage_Preview; + public static String StringVariablePreviewDialog_Title; + public static String StringVariablePreviewDialog_Name; + public static String StringVariablePreviewDialog_Value; + public static String StringVariablePreviewDialog_Description; + public static String StringVariablePreviewDialog_ReadOnly; + public static String StringVariablePreviewDialog_Contributed; + public static String StringVariablePreviewDialog_ContributedBy; + public static String StringVariablePreviewDialog_Yes; + public static String StringVariablePreviewDialog_No; + public static String StringVariablePreviewDialog_Close; public static String SimpleVariablePreferencePage_10; public static String SimpleVariablePreferencePage_11; public static String SimpleVariablePreferencePage_12; diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties index 164f2766ece..f1ef446d8fa 100644 --- a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties @@ -132,6 +132,17 @@ SimpleVariablePreferencePage_6=&Create and configure string substitution variabl SimpleVariablePreferencePage_7=&New... SimpleVariablePreferencePage_8=&Edit... SimpleVariablePreferencePage_9=&Remove +SimpleVariablePreferencePage_Preview=&Preview... +StringVariablePreviewDialog_Title=Variable Preview +StringVariablePreviewDialog_Name=Name +StringVariablePreviewDialog_Value=Value +StringVariablePreviewDialog_Description=Description +StringVariablePreviewDialog_ReadOnly=Read-only +StringVariablePreviewDialog_Contributed=Contributed +StringVariablePreviewDialog_ContributedBy=Contributed by +StringVariablePreviewDialog_Yes=Yes +StringVariablePreviewDialog_No=No +StringVariablePreviewDialog_Close=&Close SimpleVariablePreferencePage_10=&Name: SimpleVariablePreferencePage_11=&Value: SimpleVariablePreferencePage_12=&Description: diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreferencePage.java b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreferencePage.java index 877962313bb..75b83ad430d 100644 --- a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreferencePage.java +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,7 +15,6 @@ import java.text.MessageFormat; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.eclipse.core.runtime.CoreException; @@ -75,6 +74,7 @@ public class StringVariablePreferencePage extends PreferencePage implements IWor protected Button envAddButton; protected Button envEditButton; protected Button envRemoveButton; + private Button envPreviewButton; protected SimpleVariableContentProvider variableContentProvider= new SimpleVariableContentProvider(); @@ -247,6 +247,22 @@ public void widgetSelected(SelectionEvent event) { } }); envRemoveButton.setEnabled(false); + envPreviewButton = SWTFactory.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_Preview, null); + envPreviewButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent event) { + handlePreviewButtonPressed(); + } + }); + envPreviewButton.setEnabled(false); + } + + private void handlePreviewButtonPressed() { + IStructuredSelection selection = variableTable.getStructuredSelection(); + if (selection.size() != 1) { + return; + } + new StringVariablePreviewDialog(getShell(), (VariableWrapper) selection.getFirstElement()).open(); } private void handleAddButtonPressed() { @@ -290,9 +306,7 @@ private boolean addVariable(String name, String description, String value) { return false; } List editedVariables = variableContentProvider.getWorkingSetVariables(); - Iterator iter = editedVariables.iterator(); - while (iter.hasNext()) { - VariableWrapper currentVariable = iter.next(); + for (VariableWrapper currentVariable : editedVariables) { if (!currentVariable.isRemoved()) { String currentName = currentVariable.getName(); if (currentName.equals(name)) { @@ -360,9 +374,7 @@ private void handleRemoveButtonPressed() { IStructuredSelection selection = variableTable.getStructuredSelection(); List variablesToRemove = selection.toList(); StringBuilder contributedVariablesToRemove= new StringBuilder(); - Iterator iter = variablesToRemove.iterator(); - while (iter.hasNext()) { - VariableWrapper variable = iter.next(); + for (VariableWrapper variable : variablesToRemove) { if (variable.isContributed()) { contributedVariablesToRemove.append('\t').append(variable.getName()).append('\n'); } @@ -387,6 +399,7 @@ private void handleRemoveButtonPressed() { protected void handleTableSelectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (event.getStructuredSelection()); VariableWrapper variable= (VariableWrapper) selection.getFirstElement(); + envPreviewButton.setEnabled(selection.size() == 1); if (variable == null || variable.isReadOnly()) { envEditButton.setEnabled(false); envRemoveButton.setEnabled(false); @@ -503,11 +516,9 @@ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { */ public void saveChanges() { IStringVariableManager manager = getVariableManager(); - Iterator iterator = fWorkingSet.iterator(); List remove = new ArrayList<>(); List add = new ArrayList<>(); - while (iterator.hasNext()) { - VariableWrapper variable = iterator.next(); + for (VariableWrapper variable : fWorkingSet) { if (!variable.isReadOnly()) { IValueVariable underlyingVariable = variable.getUnderlyingVariable(); if (variable.isRemoved()) { diff --git a/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreviewDialog.java b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreviewDialog.java new file mode 100644 index 00000000000..1c0bd873ffc --- /dev/null +++ b/debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/StringVariablePreviewDialog.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2026 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 + *******************************************************************************/ +package org.eclipse.debug.internal.ui.preferences; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.variables.VariablesPlugin; +import org.eclipse.debug.internal.ui.preferences.StringVariablePreferencePage.VariableWrapper; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; + +class StringVariablePreviewDialog extends Dialog { + + private final VariableWrapper fVariable; + + StringVariablePreviewDialog(Shell parentShell, VariableWrapper variable) { + super(parentShell); + fVariable = variable; + setShellStyle(SWT.RESIZE | getShellStyle()); + } + + @Override + protected void configureShell(Shell newShell) { + super.configureShell(newShell); + newShell.setText(DebugPreferencesMessages.StringVariablePreviewDialog_Title); + } + + @Override + protected void createButtonsForButtonBar(Composite parent) { + createButton(parent, IDialogConstants.CANCEL_ID, DebugPreferencesMessages.StringVariablePreviewDialog_Close, + false); + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite comp = (Composite) super.createDialogArea(parent); + + StringBuilder text = new StringBuilder(); + List styles = new ArrayList<>(); + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_Name, fVariable.getName()); + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_Value, fVariable.getValue()); + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_Description, + fVariable.getDescription()); + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_ReadOnly, + yesNo(fVariable.isReadOnly())); + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_Contributed, + yesNo(fVariable.isContributed())); + if (fVariable.isContributed()) { + appendDetail(text, styles, DebugPreferencesMessages.StringVariablePreviewDialog_ContributedBy, + VariablesPlugin.getDefault().getStringVariableManager() + .getContributingPluginId(fVariable.getUnderlyingVariable())); + } + + StyledText styledText = new StyledText(comp, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL | SWT.READ_ONLY); + GridData gd = new GridData(GridData.FILL_BOTH); + gd.heightHint = convertHeightInCharsToPixels(22); + gd.widthHint = convertWidthInCharsToPixels(120); + styledText.setLayoutData(gd); + styledText.setText(text.toString()); + styledText.setStyleRanges(styles.toArray(new StyleRange[0])); + return comp; + } + + private static String yesNo(boolean flag) { + return flag ? DebugPreferencesMessages.StringVariablePreviewDialog_Yes + : DebugPreferencesMessages.StringVariablePreviewDialog_No; + } + + /** + * Appends a bold title followed by its value on the next line; skipped if the + * value is null. + */ + private static void appendDetail(StringBuilder text, List styles, String title, String value) { + if (value == null) { + return; + } + if (text.length() > 0) { + text.append("\n\n"); //$NON-NLS-1$ + } + styles.add(new StyleRange(text.length(), title.length(), null, null, SWT.BOLD)); + text.append(title).append('\n').append(value); + } +}