|
| 1 | +package io.github.albertus82.eqbulletin.gui.listener; |
| 2 | + |
| 3 | +import org.eclipse.jface.dialogs.Dialog; |
| 4 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 5 | +import org.eclipse.jface.layout.GridDataFactory; |
| 6 | +import org.eclipse.swt.SWT; |
| 7 | +import org.eclipse.swt.events.FocusAdapter; |
| 8 | +import org.eclipse.swt.events.FocusEvent; |
| 9 | +import org.eclipse.swt.events.SelectionAdapter; |
| 10 | +import org.eclipse.swt.events.SelectionEvent; |
| 11 | +import org.eclipse.swt.layout.GridData; |
| 12 | +import org.eclipse.swt.layout.GridLayout; |
| 13 | +import org.eclipse.swt.widgets.Button; |
| 14 | +import org.eclipse.swt.widgets.Composite; |
| 15 | +import org.eclipse.swt.widgets.Control; |
| 16 | +import org.eclipse.swt.widgets.Label; |
| 17 | +import org.eclipse.swt.widgets.Scale; |
| 18 | +import org.eclipse.swt.widgets.Shell; |
| 19 | +import org.eclipse.swt.widgets.Text; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import io.github.albertus82.jface.Formatter; |
| 24 | +import io.github.albertus82.jface.JFaceMessages; |
| 25 | +import io.github.albertus82.jface.listener.IntegerVerifyListener; |
| 26 | + |
| 27 | +/** |
| 28 | + * A simple input dialog for soliciting an input string from the user. |
| 29 | + * <p> |
| 30 | + * This concrete dialog class can be instantiated as is, or further subclassed |
| 31 | + * as required. |
| 32 | + * </p> |
| 33 | + */ |
| 34 | +public class ScaleInputDialog extends Dialog { |
| 35 | + |
| 36 | + private static final Logger log = LoggerFactory.getLogger(ScaleInputDialog.class); |
| 37 | + |
| 38 | + /** |
| 39 | + * The title of the dialog. |
| 40 | + */ |
| 41 | + private String title; |
| 42 | + |
| 43 | + /** |
| 44 | + * The message to display, or <code>null</code> if none. |
| 45 | + */ |
| 46 | + private String message; |
| 47 | + |
| 48 | + /** |
| 49 | + * The input value; the empty string by default. |
| 50 | + */ |
| 51 | + private int value; |
| 52 | + |
| 53 | + private final int minimum; |
| 54 | + private final int maximum; |
| 55 | + private final int increment; |
| 56 | + private final int pageIncrement; |
| 57 | + |
| 58 | + /** |
| 59 | + * Ok button widget. |
| 60 | + */ |
| 61 | + private Button okButton; |
| 62 | + |
| 63 | + private Scale scale; |
| 64 | + |
| 65 | + /** |
| 66 | + * Input text widget. |
| 67 | + */ |
| 68 | + private Text text; |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates an input dialog with OK and Cancel buttons. Note that the dialog will |
| 72 | + * have no visual representation (no widgets) until it is told to open. |
| 73 | + * <p> |
| 74 | + * Note that the <code>open</code> method blocks for input dialogs. |
| 75 | + * </p> |
| 76 | + * |
| 77 | + * @param parentShell the parent shell, or <code>null</code> to create a |
| 78 | + * top-level shell |
| 79 | + * @param dialogTitle the dialog title, or <code>null</code> if none |
| 80 | + * @param dialogMessage the dialog message, or <code>null</code> if none |
| 81 | + * @param initialValue the initial input value, or <code>null</code> if none |
| 82 | + * (equivalent to the empty string) |
| 83 | + * @param validator an input validator, or <code>null</code> if none |
| 84 | + */ |
| 85 | + public ScaleInputDialog(final Shell parentShell, final String dialogTitle, final String dialogMessage, final int initialValue, final int minimum, final int maximum, final int increment, final int pageIncrement) { |
| 86 | + super(parentShell); |
| 87 | + this.title = dialogTitle; |
| 88 | + message = dialogMessage; |
| 89 | + value = initialValue; |
| 90 | + this.minimum = minimum; |
| 91 | + this.maximum = maximum; |
| 92 | + this.increment = increment; |
| 93 | + this.pageIncrement = pageIncrement; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void buttonPressed(int buttonId) { |
| 98 | + if (buttonId == IDialogConstants.OK_ID) { |
| 99 | + value = scale.getSelection(); |
| 100 | + } |
| 101 | + super.buttonPressed(buttonId); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void configureShell(final Shell shell) { |
| 106 | + super.configureShell(shell); |
| 107 | + if (title != null) { |
| 108 | + shell.setText(title); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected void createButtonsForButtonBar(final Composite parent) { |
| 114 | + // create OK and Cancel buttons by default |
| 115 | + okButton = createButton(parent, IDialogConstants.OK_ID, JFaceMessages.get("lbl.button.ok"), true); |
| 116 | + createButton(parent, IDialogConstants.CANCEL_ID, JFaceMessages.get("lbl.button.cancel"), false); |
| 117 | + //do this here because setting the text will set enablement on the ok |
| 118 | + // button |
| 119 | + scale.setFocus(); |
| 120 | + scale.setSelection(value); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected Control createDialogArea(final Composite parent) { |
| 125 | + // create composite |
| 126 | + final Composite composite = (Composite) super.createDialogArea(parent); |
| 127 | + if (composite.getLayout() instanceof GridLayout) { |
| 128 | + ((GridLayout) composite.getLayout()).numColumns += 3; |
| 129 | + } |
| 130 | + // create message |
| 131 | + if (message != null) { |
| 132 | + Label label = new Label(composite, SWT.WRAP); |
| 133 | + label.setText(message); |
| 134 | + final GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER); |
| 135 | + data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); |
| 136 | + data.horizontalSpan += 3; |
| 137 | + label.setLayoutData(data); |
| 138 | + label.setFont(parent.getFont()); |
| 139 | + } |
| 140 | + scale = new Scale(composite, getScaleStyle()); |
| 141 | + scale.setMinimum(minimum); |
| 142 | + scale.setMaximum(maximum); |
| 143 | + scale.setIncrement(increment); |
| 144 | + scale.setPageIncrement(pageIncrement); |
| 145 | + final GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 146 | + data.verticalAlignment = GridData.FILL; |
| 147 | + data.grabExcessHorizontalSpace = true; |
| 148 | + scale.setLayoutData(data); |
| 149 | + scale.addSelectionListener(new SelectionAdapter() { |
| 150 | + @Override |
| 151 | + public void widgetSelected(final SelectionEvent e) { |
| 152 | + text.setText(Integer.toString(scale.getSelection())); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + Label label = new Label(composite, SWT.NONE); |
| 157 | + label.setText("\u00B1"); |
| 158 | + GridDataFactory.swtDefaults().applyTo(label); |
| 159 | + label.setFont(parent.getFont()); |
| 160 | + |
| 161 | + text = new Text(composite, SWT.BORDER | SWT.TRAIL); |
| 162 | + final int widthHint = new Formatter(getClass()).computeWidth(text, Integer.toString(maximum).length(), SWT.NORMAL); |
| 163 | + GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).hint(widthHint, SWT.DEFAULT).applyTo(text); |
| 164 | + text.setTextLimit(Integer.toString(maximum).length()); |
| 165 | + text.setText(Integer.toString(value)); |
| 166 | + text.addFocusListener(new TextFocusListener()); |
| 167 | + text.addVerifyListener(new IntegerVerifyListener(false)); |
| 168 | + |
| 169 | + Label label2 = new Label(composite, SWT.NONE); |
| 170 | + label2.setText("\u00B0"); |
| 171 | + GridDataFactory.swtDefaults().applyTo(label2); |
| 172 | + label2.setFont(parent.getFont()); |
| 173 | + |
| 174 | + applyDialogFont(composite); |
| 175 | + return composite; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Returns the ok button. |
| 180 | + * |
| 181 | + * @return the ok button |
| 182 | + */ |
| 183 | + protected Button getOkButton() { |
| 184 | + return okButton; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Returns the text area. |
| 189 | + * |
| 190 | + * @return the text area |
| 191 | + */ |
| 192 | + protected Scale getScale() { |
| 193 | + return scale; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Returns the string typed into this input dialog. |
| 198 | + * |
| 199 | + * @return the input string |
| 200 | + */ |
| 201 | + public Integer getValue() { |
| 202 | + return value; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Returns the style bits that should be used for the input text field. Defaults |
| 207 | + * to a single line entry. Subclasses may override. |
| 208 | + * |
| 209 | + * @return the integer style bits that should be used when creating the input |
| 210 | + * text |
| 211 | + * |
| 212 | + * @since 3.4 |
| 213 | + */ |
| 214 | + protected int getScaleStyle() { |
| 215 | + return SWT.HORIZONTAL; |
| 216 | + } |
| 217 | + |
| 218 | + protected class TextFocusListener extends FocusAdapter { |
| 219 | + @Override |
| 220 | + public void focusLost(final FocusEvent fe) { |
| 221 | + try { |
| 222 | + int textValue = Integer.parseInt(text.getText()); |
| 223 | + if (textValue > maximum) { |
| 224 | + textValue = maximum; |
| 225 | + } |
| 226 | + if (textValue < minimum) { |
| 227 | + textValue = minimum; |
| 228 | + } |
| 229 | + text.setText(Integer.toString(textValue)); |
| 230 | + scale.setSelection(textValue); |
| 231 | + } |
| 232 | + catch (final RuntimeException e) { |
| 233 | + log.debug("Cannot update the selection (which is the value) of the scale:", e); |
| 234 | + text.setText(Integer.toString(scale.getSelection())); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | +} |
0 commit comments