|
2 | 2 |
|
3 | 3 | import java.util.function.Supplier;
|
4 | 4 |
|
| 5 | +import org.eclipse.jface.dialogs.Dialog; |
| 6 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 7 | +import org.eclipse.jface.layout.GridDataFactory; |
5 | 8 | import org.eclipse.jface.viewers.TableViewer;
|
6 | 9 | import org.eclipse.jface.window.Window;
|
7 | 10 | import org.eclipse.swt.SWT;
|
| 11 | +import org.eclipse.swt.events.FocusAdapter; |
| 12 | +import org.eclipse.swt.events.FocusEvent; |
8 | 13 | import org.eclipse.swt.events.SelectionAdapter;
|
9 | 14 | import org.eclipse.swt.events.SelectionEvent;
|
| 15 | +import org.eclipse.swt.layout.GridData; |
| 16 | +import org.eclipse.swt.layout.GridLayout; |
| 17 | +import org.eclipse.swt.widgets.Composite; |
| 18 | +import org.eclipse.swt.widgets.Control; |
| 19 | +import org.eclipse.swt.widgets.Label; |
| 20 | +import org.eclipse.swt.widgets.Scale; |
| 21 | +import org.eclipse.swt.widgets.Shell; |
10 | 22 | import org.eclipse.swt.widgets.Table;
|
| 23 | +import org.eclipse.swt.widgets.Text; |
11 | 24 |
|
12 | 25 | import io.github.albertus82.eqbulletin.config.EarthquakeBulletinConfig;
|
13 | 26 | import io.github.albertus82.eqbulletin.gui.ResultsTable;
|
14 | 27 | import io.github.albertus82.eqbulletin.gui.SearchForm;
|
15 | 28 | import io.github.albertus82.eqbulletin.gui.preference.Preference;
|
16 | 29 | import io.github.albertus82.eqbulletin.model.Earthquake;
|
17 | 30 | import io.github.albertus82.eqbulletin.resources.Messages;
|
| 31 | +import io.github.albertus82.jface.Formatter; |
| 32 | +import io.github.albertus82.jface.JFaceMessages; |
| 33 | +import io.github.albertus82.jface.listener.IntegerVerifyListener; |
18 | 34 | import io.github.albertus82.jface.maps.MapBounds;
|
19 | 35 | import io.github.albertus82.jface.preference.IPreferencesConfiguration;
|
20 | 36 | import lombok.AccessLevel;
|
@@ -54,11 +70,11 @@ public void widgetSelected(final SelectionEvent se) {
|
54 | 70 | final SearchForm form = searchFormSupplier.get();
|
55 | 71 |
|
56 | 72 | // Ask user for latitude interval
|
57 |
| - final ScaleInputDialog modal = new ScaleInputDialog(table.getShell(), Messages.get("label.sameareaevents.title"), Messages.get("label.sameareaevents.message"), configuration.getByte(Preference.SAME_AREA_EVENTS_LATITUDE_INTERVAL, Defaults.SAME_AREA_EVENTS_LATITUDE_INTERVAL), LATITUDE_INTERVAL_MIN, LATITUDE_INTERVAL_MAX, 1, 1); |
58 |
| - if (Window.OK != modal.open()) { |
| 73 | + final ScaleInputDialog dialog = new ScaleInputDialog(table.getShell(), Messages.get("label.sameareaevents.title"), Messages.get("label.sameareaevents.message"), configuration.getByte(Preference.SAME_AREA_EVENTS_LATITUDE_INTERVAL, Defaults.SAME_AREA_EVENTS_LATITUDE_INTERVAL), LATITUDE_INTERVAL_MIN, LATITUDE_INTERVAL_MAX, 1, 1); |
| 74 | + if (Window.OK != dialog.open()) { |
59 | 75 | return;
|
60 | 76 | }
|
61 |
| - final float offset = modal.getValue().floatValue(); |
| 77 | + final float offset = dialog.getValue(); |
62 | 78 |
|
63 | 79 | // Latitude (parallels)
|
64 | 80 | final float lat = Math.min(MapBounds.LATITUDE_MAX_VALUE - offset, Math.max(MapBounds.LATITUDE_MIN_VALUE + offset, selection.getLatitude().getValue()));
|
@@ -115,4 +131,148 @@ private static double computeArea(final double lat0deg, final double lat1deg, fi
|
115 | 131 | return a * b * c;
|
116 | 132 | }
|
117 | 133 |
|
| 134 | + private static class ScaleInputDialog extends Dialog { |
| 135 | + |
| 136 | + private final String title; |
| 137 | + private final String message; |
| 138 | + |
| 139 | + private int value; |
| 140 | + |
| 141 | + private final int minimum; |
| 142 | + private final int maximum; |
| 143 | + private final int increment; |
| 144 | + private final int pageIncrement; |
| 145 | + |
| 146 | + private Scale scale; |
| 147 | + private Text text; |
| 148 | + |
| 149 | + private ScaleInputDialog(@NonNull final Shell parentShell, final String dialogTitle, final String dialogMessage, final int initialValue, final int minimum, final int maximum, final int increment, final int pageIncrement) { |
| 150 | + super(parentShell); |
| 151 | + this.title = dialogTitle; |
| 152 | + this.message = dialogMessage; |
| 153 | + this.value = initialValue; |
| 154 | + this.minimum = minimum; |
| 155 | + this.maximum = maximum; |
| 156 | + this.increment = increment; |
| 157 | + this.pageIncrement = pageIncrement; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected void buttonPressed(final int buttonId) { |
| 162 | + if (buttonId == IDialogConstants.OK_ID) { |
| 163 | + value = scale.getSelection(); |
| 164 | + } |
| 165 | + super.buttonPressed(buttonId); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + protected void configureShell(final Shell shell) { |
| 170 | + super.configureShell(shell); |
| 171 | + if (title != null) { |
| 172 | + shell.setText(title); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + protected void createButtonsForButtonBar(final Composite parent) { |
| 178 | + createButton(parent, IDialogConstants.OK_ID, JFaceMessages.get("lbl.button.ok"), true); |
| 179 | + createButton(parent, IDialogConstants.CANCEL_ID, JFaceMessages.get("lbl.button.cancel"), false); |
| 180 | + scale.setFocus(); |
| 181 | + scale.setSelection(value); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + protected Control createDialogArea(final Composite parent) { |
| 186 | + final Composite composite = (Composite) super.createDialogArea(parent); |
| 187 | + if (composite.getLayout() instanceof GridLayout) { |
| 188 | + ((GridLayout) composite.getLayout()).numColumns += 3; |
| 189 | + } |
| 190 | + |
| 191 | + if (message != null) { |
| 192 | + final Label label = new Label(composite, SWT.WRAP); |
| 193 | + label.setText(message); |
| 194 | + final GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER); |
| 195 | + data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); |
| 196 | + data.horizontalSpan += 3; |
| 197 | + label.setLayoutData(data); |
| 198 | + label.setFont(parent.getFont()); |
| 199 | + } |
| 200 | + |
| 201 | + scale = new Scale(composite, getScaleStyle()); |
| 202 | + scale.setMinimum(minimum); |
| 203 | + scale.setMaximum(maximum); |
| 204 | + scale.setIncrement(increment); |
| 205 | + scale.setPageIncrement(pageIncrement); |
| 206 | + final GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 207 | + data.verticalAlignment = GridData.FILL; |
| 208 | + data.grabExcessHorizontalSpace = true; |
| 209 | + scale.setLayoutData(data); |
| 210 | + scale.addSelectionListener(new SelectionAdapter() { |
| 211 | + @Override |
| 212 | + public void widgetSelected(final SelectionEvent e) { |
| 213 | + text.setText(Integer.toString(scale.getSelection())); |
| 214 | + } |
| 215 | + }); |
| 216 | + |
| 217 | + final Label plusMinusSign = new Label(composite, SWT.NONE); |
| 218 | + plusMinusSign.setText("\u00B1"); |
| 219 | + GridDataFactory.swtDefaults().applyTo(plusMinusSign); |
| 220 | + plusMinusSign.setFont(parent.getFont()); |
| 221 | + |
| 222 | + text = new Text(composite, SWT.BORDER | SWT.TRAIL); |
| 223 | + final int widthHint = new Formatter(getClass()).computeWidth(text, Integer.toString(maximum).length(), SWT.NORMAL); |
| 224 | + GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).hint(widthHint, SWT.DEFAULT).applyTo(text); |
| 225 | + text.setTextLimit(Integer.toString(maximum).length()); |
| 226 | + text.setText(Integer.toString(value)); |
| 227 | + text.addFocusListener(new TextFocusListener()); |
| 228 | + text.addVerifyListener(new IntegerVerifyListener(false)); |
| 229 | + |
| 230 | + final Label degreeSign = new Label(composite, SWT.NONE); |
| 231 | + degreeSign.setText("\u00B0"); |
| 232 | + GridDataFactory.swtDefaults().applyTo(degreeSign); |
| 233 | + degreeSign.setFont(parent.getFont()); |
| 234 | + |
| 235 | + applyDialogFont(composite); |
| 236 | + return composite; |
| 237 | + } |
| 238 | + |
| 239 | + public int getValue() { |
| 240 | + return value; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns the style bits that should be used for the input text field. Defaults |
| 245 | + * to a single line entry. Subclasses may override. |
| 246 | + * |
| 247 | + * @return the integer style bits that should be used when creating the input |
| 248 | + * text |
| 249 | + * |
| 250 | + * @since 3.4 |
| 251 | + */ |
| 252 | + protected int getScaleStyle() { |
| 253 | + return SWT.HORIZONTAL; |
| 254 | + } |
| 255 | + |
| 256 | + private class TextFocusListener extends FocusAdapter { |
| 257 | + @Override |
| 258 | + public void focusLost(final FocusEvent fe) { |
| 259 | + try { |
| 260 | + int textValue = Integer.parseInt(text.getText()); |
| 261 | + if (textValue > maximum) { |
| 262 | + textValue = maximum; |
| 263 | + } |
| 264 | + if (textValue < minimum) { |
| 265 | + textValue = minimum; |
| 266 | + } |
| 267 | + text.setText(Integer.toString(textValue)); |
| 268 | + scale.setSelection(textValue); |
| 269 | + } |
| 270 | + catch (final RuntimeException e) { |
| 271 | + log.debug("Cannot update the selection (which is the value) of the scale:", e); |
| 272 | + text.setText(Integer.toString(scale.getSelection())); |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
118 | 278 | }
|
0 commit comments