Fixed some issues with pre-loading data.

This commit is contained in:
Andrew Lalis 2023-04-21 21:43:48 +02:00
parent a200aee640
commit 6b839349fb
8 changed files with 97 additions and 68 deletions

View File

@ -32,4 +32,22 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.github.andrewlalis.running_every_day.RecorderApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -24,6 +24,8 @@ public class RecorderApp {
System.out.println("Added App Window close listener"); System.out.println("Added App Window close listener");
window.setVisible(true); window.setVisible(true);
System.out.println("Set App Window as visible"); System.out.println("Set App Window as visible");
window.initComponentData();
System.out.println("Initialized component data");
} catch (SQLException e) { } catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Failed to open database: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "Failed to open database: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace(); e.printStackTrace();

View File

@ -33,8 +33,10 @@ public class AggregateStatisticsPanel extends JPanel {
refreshButton.addActionListener(e -> refreshStats()); refreshButton.addActionListener(e -> refreshStats());
controlsPanel.add(refreshButton); controlsPanel.add(refreshButton);
this.add(controlsPanel, BorderLayout.NORTH); this.add(controlsPanel, BorderLayout.NORTH);
}
// SwingUtilities.invokeLater(this::refreshStats); public void init() {
SwingUtilities.invokeLater(this::refreshStats);
} }
private void refreshStats() { private void refreshStats() {

View File

@ -6,8 +6,14 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class RecorderAppWindow extends JFrame { public class RecorderAppWindow extends JFrame {
private final RunRecordsPanel runRecordsPanel;
private final AggregateStatisticsPanel aggregateStatisticsPanel;
public RecorderAppWindow(DataSource dataSource) { public RecorderAppWindow(DataSource dataSource) {
super("Run-Recorder"); super("Run-Recorder");
this.runRecordsPanel = new RunRecordsPanel(dataSource);
this.aggregateStatisticsPanel = new AggregateStatisticsPanel(dataSource);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setContentPane(buildGui(dataSource)); this.setContentPane(buildGui(dataSource));
this.setPreferredSize(new Dimension(1000, 600)); this.setPreferredSize(new Dimension(1000, 600));
@ -15,10 +21,15 @@ public class RecorderAppWindow extends JFrame {
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
} }
public void initComponentData() {
runRecordsPanel.init();
aggregateStatisticsPanel.init();
}
private Container buildGui(DataSource dataSource) { private Container buildGui(DataSource dataSource) {
JTabbedPane tabbedPane = new JTabbedPane(); JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Run Records", new RunRecordsPanel(dataSource)); tabbedPane.addTab("Run Records", runRecordsPanel);
tabbedPane.addTab("Aggregate Statistics", new AggregateStatisticsPanel(dataSource)); tabbedPane.addTab("Aggregate Statistics", aggregateStatisticsPanel);
tabbedPane.addTab("Charts", new ChartsPanel(dataSource)); tabbedPane.addTab("Charts", new ChartsPanel(dataSource));
return tabbedPane; return tabbedPane;
} }

View File

@ -5,6 +5,8 @@ import com.github.andrewlalis.running_every_day.data.db.DataSource;
import com.github.andrewlalis.running_every_day.data.db.Queries; import com.github.andrewlalis.running_every_day.data.db.Queries;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*; import java.awt.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
@ -22,17 +24,28 @@ public class RunRecordsPanel extends JPanel {
private final DataSource dataSource; private final DataSource dataSource;
private final RunRecordTableModel tableModel; private final RunRecordTableModel tableModel;
private final JTextField currentPageField; private final JTextField currentPageField = new JTextField("1", 3);
private final JButton firstPageButton; private final JButton firstPageButton = new JButton("First Page");
private final JButton previousPageButton; private final JButton previousPageButton = new JButton("Previous Page");
private final JButton nextPageButton; private final JButton nextPageButton = new JButton("Next Page");
private final JButton lastPageButton; private final JButton lastPageButton = new JButton("Last Page");
public RunRecordsPanel(DataSource dataSource) { public RunRecordsPanel(DataSource dataSource) {
super(new BorderLayout()); super(new BorderLayout());
this.dataSource = dataSource; this.dataSource = dataSource;
this.tableModel = new RunRecordTableModel(dataSource); this.tableModel = new RunRecordTableModel(dataSource);
tableModel.addTableModelListener(e -> updateButtonStates());
this.add(buildTablePanel(), BorderLayout.CENTER);
this.add(buildPaginationPanel(), BorderLayout.SOUTH);
this.add(buildActionsPanel(), BorderLayout.NORTH);
}
public void init() {
SwingUtilities.invokeLater(tableModel::firstPage);
}
private Container buildTablePanel() {
var table = new JTable(tableModel); var table = new JTable(tableModel);
table.getTableHeader().setReorderingAllowed(false); table.getTableHeader().setReorderingAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
@ -48,56 +61,10 @@ public class RunRecordsPanel extends JPanel {
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
table.getColumnModel().getColumn(i).setResizable(false); table.getColumnModel().getColumn(i).setResizable(false);
} }
var scrollPane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); return new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.add(scrollPane, BorderLayout.CENTER); }
JPanel paginationPanel = new JPanel(new GridLayout(1, 6));
firstPageButton = new JButton("First Page");
firstPageButton.addActionListener(e -> {
tableModel.firstPage();
updateButtonStates();
});
previousPageButton = new JButton("Previous Page");
previousPageButton.addActionListener(e -> {
tableModel.previousPage();
updateButtonStates();
});
nextPageButton = new JButton("Next Page");
nextPageButton.addActionListener(e -> {
tableModel.nextPage();
updateButtonStates();
});
lastPageButton = new JButton("Last Page");
lastPageButton.addActionListener(e -> {
tableModel.lastPage();
updateButtonStates();
});
JPanel currentPagePanel = new JPanel();
currentPagePanel.add(new JLabel("Current Page: "));
this.currentPageField = new JTextField("1", 3);
currentPagePanel.add(this.currentPageField);
JPanel pageSizePanel = new JPanel();
pageSizePanel.add(new JLabel("Page Size: "));
JComboBox<Integer> pageSizeSelector = new JComboBox<>(new Integer[]{5, 10, 20, 50, 100, 500});
pageSizeSelector.setSelectedItem(tableModel.getPagination().size());
pageSizeSelector.addItemListener(e -> {
int size = (int) e.getItem();
tableModel.setPageSize(size);
updateButtonStates();
});
pageSizePanel.add(pageSizeSelector);
paginationPanel.add(firstPageButton);
paginationPanel.add(previousPageButton);
paginationPanel.add(currentPagePanel);
paginationPanel.add(pageSizePanel);
paginationPanel.add(nextPageButton);
paginationPanel.add(lastPageButton);
this.add(paginationPanel, BorderLayout.SOUTH);
private JPanel buildActionsPanel() {
JPanel actionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel actionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton addActionButton = new JButton("Add a Record"); JButton addActionButton = new JButton("Add a Record");
addActionButton.addActionListener(e -> { addActionButton.addActionListener(e -> {
@ -111,7 +78,37 @@ public class RunRecordsPanel extends JPanel {
JButton generateRandomDataButton = new JButton("Generate Random Data"); JButton generateRandomDataButton = new JButton("Generate Random Data");
generateRandomDataButton.addActionListener(e -> generateRandomData()); generateRandomDataButton.addActionListener(e -> generateRandomData());
actionsPanel.add(generateRandomDataButton); actionsPanel.add(generateRandomDataButton);
this.add(actionsPanel, BorderLayout.NORTH); return actionsPanel;
}
private JPanel buildPaginationPanel() {
JPanel paginationPanel = new JPanel(new GridLayout(1, 6));
firstPageButton.addActionListener(e -> tableModel.firstPage());
previousPageButton.addActionListener(e -> tableModel.previousPage());
nextPageButton.addActionListener(e -> tableModel.nextPage());
lastPageButton.addActionListener(e -> tableModel.lastPage());
JPanel currentPagePanel = new JPanel();
currentPagePanel.add(new JLabel("Current Page: "));
currentPagePanel.add(this.currentPageField);
JPanel pageSizePanel = new JPanel();
pageSizePanel.add(new JLabel("Page Size: "));
JComboBox<Integer> pageSizeSelector = new JComboBox<>(new Integer[]{5, 10, 20, 50, 100, 500});
pageSizeSelector.setSelectedItem(tableModel.getPagination().size());
pageSizeSelector.addItemListener(e -> {
int size = (int) e.getItem();
tableModel.setPageSize(size);
});
pageSizePanel.add(pageSizeSelector);
paginationPanel.add(firstPageButton);
paginationPanel.add(previousPageButton);
paginationPanel.add(currentPagePanel);
paginationPanel.add(pageSizePanel);
paginationPanel.add(nextPageButton);
paginationPanel.add(lastPageButton);
return paginationPanel;
} }
private void updateButtonStates() { private void updateButtonStates() {
@ -151,7 +148,6 @@ public class RunRecordsPanel extends JPanel {
c.commit(); c.commit();
c.setAutoCommit(true); c.setAutoCommit(true);
tableModel.firstPage(); tableModel.firstPage();
updateButtonStates();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -20,6 +20,7 @@ public class WindowDataSourceCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
try { try {
dataSource.close(); dataSource.close();
System.out.println("Data source closed");
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -53,6 +53,9 @@ public class DateSeriesChartRenderer extends JFreeChartRenderer {
false false
); );
} }
if (series.getItemCount() < 2) {
throw new IllegalStateException("Not enough data.");
}
XYDataset dataset = new TimeSeriesCollection(series); XYDataset dataset = new TimeSeriesCollection(series);
DateAxis domainAxis = new DateAxis(); DateAxis domainAxis = new DateAxis();

View File

@ -17,14 +17,10 @@ public abstract class JFreeChartRenderer implements ChartRenderer {
protected abstract JFreeChart getChart() throws Exception; protected abstract JFreeChart getChart() throws Exception;
protected void applyCustomStyles(JFreeChart chart) {} protected void applyCustomStyles(JFreeChart chart) {}
public void refresh() { public void refresh() throws Exception {
try { lastChart = getChart();
lastChart = getChart(); standardChartTheme.apply(lastChart);
standardChartTheme.apply(lastChart); applyCustomStyles(lastChart);
applyCustomStyles(lastChart);
} catch (Exception e) {
e.printStackTrace();
}
} }
@Override @Override
@ -35,7 +31,7 @@ public abstract class JFreeChartRenderer implements ChartRenderer {
} }
lastChart.draw(graphics, area); lastChart.draw(graphics, area);
} catch (Exception e) { } catch (Exception e) {
graphics.setColor(Color.BLACK); graphics.setColor(Color.RED);
graphics.setBackground(Color.WHITE); graphics.setBackground(Color.WHITE);
graphics.fill(area); graphics.fill(area);
graphics.drawString("Error: " + e.getMessage(), 20, 40); graphics.drawString("Error: " + e.getMessage(), 20, 40);