Skip to content

Commit

Permalink
Added about dialog, Desktop class, busy label
Browse files Browse the repository at this point in the history
  • Loading branch information
PranavAmarnath committed Mar 9, 2021
1 parent fa809ca commit 2099f0b
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 31 deletions.
50 changes: 39 additions & 11 deletions src/main/java/com/secres/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.secres;

import java.awt.Desktop;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

Expand All @@ -14,35 +17,60 @@ public class Main {
public Main() {
createView();
}

public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.awt.application.name", "SecresCSV");
// For picky mac users
System.setProperty("apple.laf.useScreenMenuBar", "true");
// Mac header on mac menubar
System.setProperty("apple.awt.application.name", "Secres");
System.setProperty("apple.awt.application.appearance", "system");
// Acceleration of graphics, should ONLY be used by developers
//System.setProperty("apple.awt.graphics.EnableQ2DX","true");
System.setProperty("apple.awt.antialiasing", "true");
System.setProperty("apple.awt.textantialiasing", "true");
/*
if(System.getProperty("os.name").toLowerCase().contains("win")) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
}
*/
if(System.getProperty("os.name").toString().contains("Mac")) {
try {
SwingUtilities.invokeLater(() -> {
Desktop desktop = Desktop.getDesktop();

JPanel aboutPanel = View.createAboutPanel();
desktop.setAboutHandler(e -> {
JOptionPane.showMessageDialog(View.getFrame(), aboutPanel, "About SecresCSV", JOptionPane.PLAIN_MESSAGE);
});
desktop.setPreferencesHandler(e -> {
JOptionPane.showMessageDialog(View.getFrame(), "Preferences", "Preferences", JOptionPane.INFORMATION_MESSAGE);
});
desktop.setQuitHandler((e,r) -> {
System.exit(0);
});
});
} catch (Exception e) { e.printStackTrace(); }
}
SwingUtilities.invokeLater(() -> {
FlatLightLaf.install();
new Main();
});
}
public static void createModelLoad(File PATH, JTable table) {

static void createModelLoad(File PATH, JTable table) {
new Model(PATH, table, false);
}
public static void createModelRefresh(File PATH, JTable table) {

static void createModelRefresh(File PATH, JTable table) {
new Model(PATH, table, true);
}
public static void saveModel(String PATH, JTable table) {

static void saveModel(String PATH, JTable table) {
Model.save(PATH, table);
}
public void createView() {

private void createView() {
new View();
}

Expand Down
58 changes: 53 additions & 5 deletions src/main/java/com/secres/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
import com.opencsv.exceptions.CsvValidationException;
import com.secres.View.TablePanel;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXBusyLabel;

/**
* The <code>Model</code> class defines all I/O from the CSV files.
* <P>
Expand All @@ -39,13 +46,17 @@ public class Model {
private CSVReader reader;
/** Current line */
private String[] line;
/** Busy label */
private static JXBusyLabel busyLabel;

/**
* Model constructor
* Model constructor to load CSV data
* @param path Path to file
* @param table The table
* @param refresh If the user is refreshing (true) or if it's the first load (false)
*/
public Model(File path, JTable table, boolean refresh) {
createBusyLabel();
class Worker extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() {
Expand Down Expand Up @@ -76,6 +87,7 @@ protected Void doInBackground() {
@Override
protected void done() {
try {
removeBusyLabel();
if(refresh == true) {
((TablePanel) View.getTabbedPane().getSelectedComponent()).getTable().setModel(model);
JOptionPane.showMessageDialog(View.getFrame(), "Refreshed data.");
Expand All @@ -93,7 +105,14 @@ protected void done() {
worker.execute();
}

public static void save(String path, JTable table) {
/**
* Executes {@link #save(String, JTable)} on a SwingWorker after creating the busy label
* @param path The path to export to
* @param table The table to export
* @see #save(String, JTable)
*/
static void save(String path, JTable table) {
createBusyLabel();
new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() {
Expand All @@ -102,12 +121,18 @@ protected Void doInBackground() {
}
@Override
protected void done() {
removeBusyLabel();
JOptionPane.showMessageDialog(View.getFrame(), "Finished saving file.");
}
}.execute();
}

public static void exportToCSV(String pathToExportTo, JTable tableToExport) {
/**
* Export table data to same path of CSV file.
* @param pathToExportTo The path to export to
* @param tableToExport The table to export
*/
private static void exportToCSV(String pathToExportTo, JTable tableToExport) {
try {
TableModel model = tableToExport.getModel();
FileWriter csv = new FileWriter(new File(pathToExportTo));
Expand Down Expand Up @@ -141,25 +166,48 @@ public static void exportToCSV(String pathToExportTo, JTable tableToExport) {
}
}

/**
* A method to show an error in a <code>JOptionPane</code>.
* @param title Title of the dialog
* @param e The Exception
*/
private static void showError(String title, Exception e) {
JTextPane textPane = new JTextPane();
textPane.setText(e.getMessage());
JOptionPane.showMessageDialog(View.getFrame(), textPane, title, JOptionPane.ERROR_MESSAGE);
}

/**
* Creates the busy label.
*/
private static void createBusyLabel() {
busyLabel = new JXBusyLabel(new Dimension(18, 18)); // dimensions of icons to keep scaled
busyLabel.setBusy(true);
View.getToolBar().add(busyLabel);
}

/**
* Removes the busy label.
*/
private static void removeBusyLabel() {
View.getToolBar().remove(busyLabel);
View.getToolBar().revalidate();
View.getToolBar().repaint();
}

/**
* Returns table model
* @return <code>DefaultTableModel</code> - table model
*/
public DefaultTableModel getModel() {
DefaultTableModel getModel() {
return model;
}

/**
* Returns table header
* @return <code>Object[]</code> - header
*/
public Object[] getHeaders() {
Object[] getHeaders() {
return header;
}

Expand Down
Loading

0 comments on commit 2099f0b

Please sign in to comment.