Skip to content

Commit 015624a

Browse files
committed
Find events in the same area dialog.
1 parent fbea162 commit 015624a

File tree

6 files changed

+267
-5
lines changed

6 files changed

+267
-5
lines changed

src/main/java/io/github/albertus82/eqbulletin/gui/EarthquakeBulletinGui.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.eclipse.jface.layout.GridDataFactory;
1515
import org.eclipse.jface.layout.GridLayoutFactory;
1616
import org.eclipse.jface.window.ApplicationWindow;
17+
import org.eclipse.jface.window.Window;
1718
import org.eclipse.swt.SWT;
1819
import org.eclipse.swt.custom.SashForm;
1920
import org.eclipse.swt.graphics.Point;
@@ -105,6 +106,7 @@ public static void main(final String... args) {
105106
try {
106107
Display.setAppName(getApplicationName());
107108
Display.setAppVersion(BuildInfo.getProperty("project.version"));
109+
Window.setDefaultImages(Images.getAppIconArray());
108110
start();
109111
}
110112
catch (final RuntimeException | Error e) { // NOSONAR Catch Exception instead of Error. Throwable and Error should not be caught (java:S1181)
@@ -152,7 +154,6 @@ private static void loop(@NonNull final Shell shell) {
152154
@Override
153155
protected void configureShell(final Shell shell) {
154156
super.configureShell(shell);
155-
shell.setImages(Images.getAppIconArray());
156157
shell.setText(getApplicationName());
157158
}
158159

src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindSameAreaEventsSelectionListener.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.function.Supplier;
44

55
import org.eclipse.jface.viewers.TableViewer;
6+
import org.eclipse.jface.window.Window;
67
import org.eclipse.swt.SWT;
78
import org.eclipse.swt.events.SelectionAdapter;
89
import org.eclipse.swt.events.SelectionEvent;
@@ -13,6 +14,7 @@
1314
import io.github.albertus82.eqbulletin.gui.SearchForm;
1415
import io.github.albertus82.eqbulletin.gui.preference.Preference;
1516
import io.github.albertus82.eqbulletin.model.Earthquake;
17+
import io.github.albertus82.eqbulletin.resources.Messages;
1618
import io.github.albertus82.jface.maps.MapBounds;
1719
import io.github.albertus82.jface.preference.IPreferencesConfiguration;
1820
import lombok.AccessLevel;
@@ -25,6 +27,9 @@
2527
@RequiredArgsConstructor
2628
public class FindSameAreaEventsSelectionListener extends SelectionAdapter {
2729

30+
public static final byte LATITUDE_INTERVAL_MIN = 1;
31+
public static final byte LATITUDE_INTERVAL_MAX = 5;
32+
2833
private static final double AUTHALIC_RADIUS = 6371.0072;
2934

3035
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@@ -47,7 +52,13 @@ public void widgetSelected(final SelectionEvent se) {
4752
final Table table = tableViewer.getTable();
4853
if (selection != null && table != null && !table.isDisposed()) {
4954
final SearchForm form = searchFormSupplier.get();
50-
final float offset = configuration.getByte(Preference.SAME_AREA_EVENTS_LATITUDE_INTERVAL, Defaults.SAME_AREA_EVENTS_LATITUDE_INTERVAL);
55+
56+
// 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()) {
59+
return;
60+
}
61+
final float offset = modal.getValue().floatValue();
5162

5263
// Latitude (parallels)
5364
final float lat = Math.min(MapBounds.LATITUDE_MAX_VALUE - offset, Math.max(MapBounds.LATITUDE_MIN_VALUE + offset, selection.getLatitude().getValue()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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+
}

src/main/java/io/github/albertus82/eqbulletin/gui/preference/Preference.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public enum Preference implements IPreference {
9696
CRITERIA_FORMAT(new PreferenceDetailsBuilder(CRITERIA).label(() -> Messages.get("label.form.format")).defaultValue(SearchForm.Defaults.FORMAT.getValue()).build(), new FieldEditorDetailsBuilder(DefaultRadioGroupFieldEditor.class).labelsAndValues(getFormatRadioOptions()).radioNumColumns(2).radioUseGroup(true).build()),
9797
CRITERIA_LIMIT(new PreferenceDetailsBuilder(CRITERIA).label(() -> Messages.get("label.form.limit") + " " + Messages.get("label.form.limit.note")).build(), new FieldEditorDetailsBuilder(EnhancedIntegerFieldEditor.class).emptyStringAllowed(true).numberValidRange(SearchForm.RESULTS_MIN_VALUE, SearchForm.RESULTS_MAX_VALUE).build()),
9898
CRITERIA_RESTRICT(new PreferenceDetailsBuilder(CRITERIA).defaultValue(SearchForm.Defaults.CRITERIA_RESTRICT).label(() -> Messages.get("label.form.criteria.restrict")).build(), new FieldEditorDetailsBuilder(DefaultBooleanFieldEditor.class).build()),
99-
SAME_AREA_EVENTS_LATITUDE_INTERVAL(new PreferenceDetailsBuilder(CRITERIA).defaultValue(FindSameAreaEventsSelectionListener.Defaults.SAME_AREA_EVENTS_LATITUDE_INTERVAL).build(), new FieldEditorDetailsBuilder(ScaleIntegerFieldEditor.class).scaleMinimum(1).scaleMaximum(5).scalePageIncrement(1).build()),
99+
SAME_AREA_EVENTS_LATITUDE_INTERVAL(new PreferenceDetailsBuilder(CRITERIA).defaultValue(FindSameAreaEventsSelectionListener.Defaults.SAME_AREA_EVENTS_LATITUDE_INTERVAL).build(), new FieldEditorDetailsBuilder(ScaleIntegerFieldEditor.class).scaleMinimum(FindSameAreaEventsSelectionListener.LATITUDE_INTERVAL_MIN).scaleMaximum(FindSameAreaEventsSelectionListener.LATITUDE_INTERVAL_MAX).scalePageIncrement(1).build()),
100100
AUTOREFRESH_ENABLED(new PreferenceDetailsBuilder(CRITERIA).defaultValue(SearchForm.Defaults.AUTOREFRESH_ENABLED).build(), new FieldEditorDetailsBuilder(DefaultBooleanFieldEditor.class).build()),
101101
AUTOREFRESH_MINS(new PreferenceDetailsBuilder(CRITERIA).parent(AUTOREFRESH_ENABLED).label(() -> Messages.get("label.form.button.autorefresh")).build(), new FieldEditorDetailsBuilder(EnhancedIntegerFieldEditor.class).numberMinimum(SearchForm.AUTOREFRESH_MIN_VALUE).emptyStringAllowed(true).textLimit(SearchForm.AUTOREFRESH_TEXT_LIMIT).build()),
102102

src/main/resources/io/github/albertus82/eqbulletin/resources/messages.properties

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ label.about.license.gpl.a.href=https://www.gnu.org/licenses/gpl.html
5151
label.about.license.gpl.a.text=GNU General Public License
5252
label.about.title=About ${project.name}
5353
label.about.version=Version {0} ({1})
54+
55+
label.sameareaevents.title=Find events in the same area
56+
label.sameareaevents.message=Latitude interval
57+
58+
5459
label.button.cancel=&Cancel
5560
label.button.close=&Close
5661
label.button.confirm=&Confirm
@@ -113,7 +118,7 @@ label.menu.item.export.csv=Export &CSV...
113118
label.menu.item.feregion=&Flinn-Engdahl regionalization
114119
label.menu.item.google.maps.browser=View map in &browser
115120
label.menu.item.open.browser=&Open event in browser
116-
label.menu.item.find.events.same.area=Find &events in the same area
121+
label.menu.item.find.events.same.area=Find &events in the same area...
117122
label.menu.item.preferences=&Configuration
118123
label.menu.item.save.map=&Save map as...
119124
label.menu.item.show.map=&Show map

src/main/resources/io/github/albertus82/eqbulletin/resources/messages_it.properties

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ label.button.cancel=&Annulla
5555
label.button.close=&Chiudi
5656
label.button.confirm=&Conferma
5757
label.button.ok=&OK
58+
59+
label.sameareaevents.title=Trova eventi nella stessa area
60+
label.sameareaevents.message=Intervallo di latitudine
61+
62+
63+
5864
label.error=Errore
5965
label.feregion.dialog.coordinates=Coordinate
6066
label.feregion.dialog.latitude=Latitudine
@@ -113,7 +119,7 @@ label.menu.item.export.csv=Esporta &CSV...
113119
label.menu.item.feregion=&Regionalizzazione Flinn-Engdahl
114120
label.menu.item.google.maps.browser=Visualizza mappa nel &browser
115121
label.menu.item.open.browser=&Apri evento nel browser
116-
label.menu.item.find.events.same.area=Trova &eventi nella stessa area
122+
label.menu.item.find.events.same.area=Trova &eventi nella stessa area...
117123
label.menu.item.preferences=&Configurazione
118124
label.menu.item.save.map=&Salva mappa con nome...
119125
label.menu.item.show.map=&Visualizza mappa

0 commit comments

Comments
 (0)