Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings UI refactoring #269

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import globalquake.core.report.EarthquakeReporter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class DebugSettingsPanel extends SettingsPanel {

Expand All @@ -14,14 +14,16 @@ public class DebugSettingsPanel extends SettingsPanel {
private final JCheckBox chkBoxRevisions;

public DebugSettingsPanel() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(new EmptyBorder(5,5,5,5));

add(chkBoxReports = new JCheckBox("Enable Earthquake Reports", Settings.reportsEnabled));
add(new JLabel(" Reports will be stored in %s".formatted(EarthquakeReporter.ANALYSIS_FOLDER.getPath())));
add(chkBoxCoreWaves = new JCheckBox("Display PKP and PKIKP Waves", Settings.displayCoreWaves));
add(chkBoxConfidencePolygons = new JCheckBox("Display epicenter confidence polygons", Settings.confidencePolygons));
add(chkBoxRevisions = new JCheckBox("Reduce number of revisions", Settings.reduceRevisions));
setLayout(new BorderLayout());

JPanel debugPanel = createGridBagPanel();
debugPanel.add(chkBoxReports = new JCheckBox("Enable Earthquake Reports.", Settings.reportsEnabled), createGbc(0));
debugPanel.add(createJTextArea("Reports will be stored in %s".formatted(EarthquakeReporter.ANALYSIS_FOLDER.getPath()), this), createGbc(1));
debugPanel.add(chkBoxCoreWaves = new JCheckBox("Display PKP and PKIKP Waves.", Settings.displayCoreWaves), createGbc(2));
debugPanel.add(chkBoxConfidencePolygons = new JCheckBox("Display epicenter confidence polygons.", Settings.confidencePolygons), createGbc(3));
debugPanel.add(chkBoxRevisions = new JCheckBox("Reduce number of revisions", Settings.reduceRevisions), createGbc(4));

add(debugPanel, BorderLayout.NORTH);
}

@Override
Expand All @@ -36,4 +38,4 @@ public void save() {
public String getTitle() {
return "Debug";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import globalquake.core.intensity.IntensityScales;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.Instant;
Expand All @@ -17,206 +15,144 @@
import java.util.Objects;

public class GeneralSettingsPanel extends SettingsPanel {
private JComboBox<IntensityScale> comboBoxScale;
private JCheckBox chkBoxHomeLoc;
private JComboBox<IntensityScale> comboBoxScale;
private JCheckBox chkBoxHomeLoc;

private JTextField textFieldLat;
private JTextField textFieldLon;
private JComboBox<DistanceUnit> distanceUnitJComboBox;
private JComboBox<ZoneId> timezoneCombobox;
private JTextField textFieldLat;
private JTextField textFieldLon;
private JComboBox<DistanceUnit> distanceUnitJComboBox;
private JComboBox<ZoneId> timezoneCombobox;

private JSlider sliderStoreTime;
private JSlider sliderStoreTime;

public GeneralSettingsPanel(SettingsFrame settingsFrame) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
public GeneralSettingsPanel(SettingsFrame settingsFrame) {
setLayout(new BorderLayout());

createHomeLocationSettings();
//createAlertsDialogSettings();
add(createIntensitySettingsPanel());
createOtherSettings(settingsFrame);
add(createSettingStoreTime());
}
JPanel generalPanel = createVerticalPanel(false);
generalPanel.add(createHomeLocationSettings());
generalPanel.add(createIntensitySettingsPanel());
generalPanel.add(createOtherSettings(settingsFrame));
generalPanel.add(createSettingStoreTime());

private void createOtherSettings(SettingsFrame settingsFrame) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createTitledBorder("Other"));
add(generalPanel, BorderLayout.NORTH);
}

JPanel row1 = new JPanel();
private Component createHomeLocationSettings() {
JPanel outsidePanel = createGridBagPanel("Home location settings");

row1.add(new JLabel("Distance units: "));
outsidePanel.add(new JLabel("Home Latitude:"), createGbc(0, 0));
outsidePanel.add(textFieldLat = new JTextField(String.format("%s", Settings.homeLat)), createGbc(1, 0));

distanceUnitJComboBox = new JComboBox<>(DistanceUnit.values());
distanceUnitJComboBox.setSelectedIndex(Math.max(0, Math.min(distanceUnitJComboBox.getItemCount() - 1, Settings.distanceUnitsIndex)));
outsidePanel.add(new JLabel("Home Longitude:"), createGbc(0, 1));
outsidePanel.add(textFieldLon = new JTextField(String.format("%s", Settings.homeLon)), createGbc(1, 1));

distanceUnitJComboBox.addItemListener(itemEvent -> {
Settings.distanceUnitsIndex = distanceUnitJComboBox.getSelectedIndex();
settingsFrame.refreshUI();
});

row1.add(distanceUnitJComboBox);

JPanel row2 = new JPanel();

row2.add(new JLabel("Timezone: "));
Comparator<ZoneId> zoneIdComparator = Comparator.comparingInt(zone -> zone.getRules().getOffset(Instant.now()).getTotalSeconds());

// Use a DefaultComboBoxModel for better control and management
DefaultComboBoxModel<ZoneId> timezoneModel = new DefaultComboBoxModel<>();

// Populate the model with available timezones and sort them using the custom Comparator
ZoneId.getAvailableZoneIds().stream()
.map(ZoneId::of)
.sorted(zoneIdComparator)
.forEach(timezoneModel::addElement);

// Create the JComboBox with the populated and sorted model
timezoneCombobox = new JComboBox<>(timezoneModel);

// this assures that default timezone will always be selected
timezoneCombobox.setSelectedItem(ZoneId.systemDefault());

// if theres valid timezone in the settings then it will be selected
timezoneCombobox.setSelectedItem(ZoneId.of(Settings.timezoneStr));

// Add the JComboBox to your UI
row2.add(timezoneCombobox);

timezoneCombobox.setRenderer(new DefaultListCellRenderer(){
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value instanceof ZoneId zoneId) {
String offset = zoneId.getRules().getOffset(Instant.now()).toString();
label.setText(String.format("%s (%s)", zoneId, offset));
}

return label;
}
});
outsidePanel.add(createJTextArea("Home location will be used for playing additional alarm sounds if an earthquake occurs nearby.", outsidePanel), createGbc(2));

timezoneCombobox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Settings.timezoneStr = ((ZoneId) Objects.requireNonNull(timezoneCombobox.getSelectedItem())).getId();
Settings.initTimezoneSettings();
}
});
outsidePanel.add(chkBoxHomeLoc = new JCheckBox("Display home location.", Settings.displayHomeLocation), createGbc(3));

row2.add(timezoneCombobox);
return outsidePanel;
}

panel.add(row1);
panel.add(row2);
private Component createIntensitySettingsPanel() {
JPanel panel = createGridBagPanel("Intensity scale");

add(panel);
}
comboBoxScale = new JComboBox<>(IntensityScales.INTENSITY_SCALES);
comboBoxScale.setSelectedIndex(Settings.intensityScaleIndex);
panel.add(alignLeft(comboBoxScale), createGbc(0));

private void createHomeLocationSettings() {
JPanel outsidePanel = new JPanel(new BorderLayout());
outsidePanel.setBorder(BorderFactory.createTitledBorder("Home location settings"));
panel.add(createJTextArea("Keep in mind that the displayed intensities are estimated, not measured.", panel), createGbc(1));

JPanel homeLocationPanel = new JPanel();
homeLocationPanel.setLayout(new GridLayout(2,1));
return panel;
}

JLabel lblLat = new JLabel("Home Latitude: ");
JLabel lblLon = new JLabel("Home Longitude: ");
private Component createOtherSettings(SettingsFrame settingsFrame) {
JPanel panel = createGridBagPanel("Other");

textFieldLat = new JTextField(20);
textFieldLat.setText(String.format("%s", Settings.homeLat));
textFieldLat.setColumns(10);

textFieldLon = new JTextField(20);
textFieldLon.setText(String.format("%s", Settings.homeLon));
textFieldLon.setColumns(10);
panel.add(new JLabel("Distance units:"), createGbc(0, 0));
distanceUnitJComboBox = new JComboBox<>(DistanceUnit.values());
distanceUnitJComboBox.setSelectedIndex(Math.max(0, Math.min(distanceUnitJComboBox.getItemCount() - 1, Settings.distanceUnitsIndex)));
distanceUnitJComboBox.addItemListener(itemEvent -> {
Settings.distanceUnitsIndex = distanceUnitJComboBox.getSelectedIndex();
settingsFrame.refreshUI();
});
panel.add(alignLeft(distanceUnitJComboBox), createGbc(1, 0));

JPanel latPanel = new JPanel();
//latPanel.setLayout(new BoxLayout(latPanel, BoxLayout.X_AXIS));
panel.add(new JLabel("Timezone:"), createGbc(0, 1));
Comparator<ZoneId> zoneIdComparator = Comparator.comparingInt(zone -> zone.getRules().getOffset(Instant.now()).getTotalSeconds());

latPanel.add(lblLat);
latPanel.add(textFieldLat);
// Use a DefaultComboBoxModel for better control and management
DefaultComboBoxModel<ZoneId> timezoneModel = new DefaultComboBoxModel<>();

JPanel lonPanel = new JPanel();
//lonPanel.setLayout(new BoxLayout(lonPanel, BoxLayout.X_AXIS));
// Populate the model with available timezones and sort them using the custom Comparator
ZoneId.getAvailableZoneIds().stream()
.map(ZoneId::of)
.sorted(zoneIdComparator)
.forEach(timezoneModel::addElement);

lonPanel.add(lblLon);
lonPanel.add(textFieldLon);
// Create the JComboBox with the populated and sorted model
timezoneCombobox = new JComboBox<>(timezoneModel);
// this assures that default timezone will always be selected
timezoneCombobox.setSelectedItem(ZoneId.systemDefault());

homeLocationPanel.add(latPanel);
homeLocationPanel.add(lonPanel);
// if there's a valid timezone in the settings, then it will be selected
timezoneCombobox.setSelectedItem(ZoneId.of(Settings.timezoneStr));

JTextArea infoLocation = new JTextArea("Home location will be used for playing additional alarm \n sounds if an earthquake occurs nearby");
infoLocation.setBorder(new EmptyBorder(5,5,5,5));
infoLocation.setLineWrap(true);
infoLocation.setEditable(false);
infoLocation.setBackground(homeLocationPanel.getBackground());
timezoneCombobox.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

chkBoxHomeLoc = new JCheckBox("Display home location");
chkBoxHomeLoc.setSelected(Settings.displayHomeLocation);
outsidePanel.add(chkBoxHomeLoc);
if (value instanceof ZoneId zoneId) {
String offset = zoneId.getRules().getOffset(Instant.now()).toString();
label.setText(String.format("%s (%s)", zoneId, offset));
}

outsidePanel.add(homeLocationPanel, BorderLayout.NORTH);
outsidePanel.add(infoLocation, BorderLayout.CENTER);
outsidePanel.add(chkBoxHomeLoc, BorderLayout.SOUTH);
return label;
}
});

add(outsidePanel);
}
timezoneCombobox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Settings.timezoneStr = ((ZoneId) Objects.requireNonNull(timezoneCombobox.getSelectedItem())).getId();
Settings.initTimezoneSettings();
}
});

private Component createSettingStoreTime() {
sliderStoreTime = HypocenterAnalysisSettingsPanel.createSettingsSlider(2, 20, 2, 1);
panel.add(alignLeft(timezoneCombobox), createGbc(1, 1));

JLabel label = new JLabel();
ChangeListener changeListener = changeEvent -> label.setText("Waveform data storage time (minutes): %d".formatted(
sliderStoreTime.getValue()));
return panel;
}

sliderStoreTime.addChangeListener(changeListener);
private Component createSettingStoreTime() {
sliderStoreTime = HypocenterAnalysisSettingsPanel.createSettingsSlider(2, 20, 2, 1);

sliderStoreTime.setValue(Settings.logsStoreTimeMinutes);
changeListener.stateChanged(null);
JLabel label = new JLabel("Waveform data storage time (minutes): %d".formatted(sliderStoreTime.getValue()));
sliderStoreTime.addChangeListener(e -> label.setText("Waveform data storage time (minutes): %d".formatted(sliderStoreTime.getValue())));
sliderStoreTime.setValue(Settings.logsStoreTimeMinutes);

return HypocenterAnalysisSettingsPanel.createCoolLayout(sliderStoreTime, label, "5",
"""
return HypocenterAnalysisSettingsPanel.createCoolLayout(sliderStoreTime, label, "5",
"""
In GlobalQuake, waveform data poses the highest demand on your system's RAM.
If you're encountering memory constraints, you have two options:
either reduce the number of selected stations or lower this specific value.
""");
}

private JPanel createIntensitySettingsPanel() {
JPanel panel = new JPanel(new GridLayout(2,1));
panel.setBorder(BorderFactory.createTitledBorder("Intensity Scale"));

comboBoxScale = new JComboBox<>(IntensityScales.INTENSITY_SCALES);
comboBoxScale.setSelectedIndex(Settings.intensityScaleIndex);

JPanel div = new JPanel();
div.add(comboBoxScale);
panel.add(div, BorderLayout.CENTER);

JLabel lbl = new JLabel();
lbl.setFont(new Font("Calibri", Font.PLAIN, 13));
lbl.setText("Keep in mind that the displayed intensities are estimated, not measured");


panel.add(lbl, BorderLayout.SOUTH);

return panel;
}

@Override
public void save() {
Settings.homeLat = parseDouble(textFieldLat.getText(), "Home latitude", -90, 90);
Settings.homeLon = parseDouble(textFieldLon.getText(), "Home longitude", -180, 180);
Settings.intensityScaleIndex = comboBoxScale.getSelectedIndex();
Settings.displayHomeLocation = chkBoxHomeLoc.isSelected();
Settings.distanceUnitsIndex = distanceUnitJComboBox.getSelectedIndex();
Settings.timezoneStr = ((ZoneId) Objects.requireNonNull(timezoneCombobox.getSelectedItem())).getId();
Settings.logsStoreTimeMinutes = sliderStoreTime.getValue();
}

@Override
public String getTitle() {
return "General";
}
}
}

@Override
public void save() {
Settings.homeLat = parseDouble(textFieldLat.getText(), "Home latitude", -90, 90);
Settings.homeLon = parseDouble(textFieldLon.getText(), "Home longitude", -180, 180);
Settings.intensityScaleIndex = comboBoxScale.getSelectedIndex();
Settings.displayHomeLocation = chkBoxHomeLoc.isSelected();
Settings.distanceUnitsIndex = distanceUnitJComboBox.getSelectedIndex();
Settings.timezoneStr = ((ZoneId) Objects.requireNonNull(timezoneCombobox.getSelectedItem())).getId();
Settings.logsStoreTimeMinutes = sliderStoreTime.getValue();
}

@Override
public String getTitle() {
return "General";
}
}
Loading