From 74784513c78728995892e2282702ae6f2893c1fd Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Fri, 2 Jun 2017 00:56:45 -0700 Subject: [PATCH] Settings now allow custom title and save file names. --- .gitignore | 1 + src/main/java/libsys/Main.java | 7 +- src/main/java/libsys/MainGUI.form | 48 +++-- src/main/java/libsys/MainGUI.java | 64 +++++-- src/main/java/libsys/Settings.java | 101 +++++++++++ src/main/java/libsys/SettingsDialog.form | 135 ++++++++++++++ src/main/java/libsys/SettingsDialog.java | 220 +++++++++++++++++++++++ 7 files changed, 543 insertions(+), 33 deletions(-) create mode 100644 src/main/java/libsys/Settings.java create mode 100644 src/main/java/libsys/SettingsDialog.form create mode 100644 src/main/java/libsys/SettingsDialog.java diff --git a/.gitignore b/.gitignore index 10fd9a7..874482c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +settings.txt *.json .classpath diff --git a/src/main/java/libsys/Main.java b/src/main/java/libsys/Main.java index b17598f..b49ff33 100644 --- a/src/main/java/libsys/Main.java +++ b/src/main/java/libsys/Main.java @@ -6,8 +6,9 @@ package libsys; class Main { - static String bookFilename = "books.json"; - static String userFilename = "users.json"; + static Settings settings = new Settings("settings.txt"); + static String bookFilename = settings.get("Books Filename"); + static String userFilename = settings.get("Users Filename"); public static void main(String[] args) { @@ -35,6 +36,6 @@ class Main handler.users.setUserFileName(userFilename); } - new MainGUI(handler).setVisible(true); + new MainGUI(handler, settings).setVisible(true); } } diff --git a/src/main/java/libsys/MainGUI.form b/src/main/java/libsys/MainGUI.form index a972b75..a02675b 100644 --- a/src/main/java/libsys/MainGUI.form +++ b/src/main/java/libsys/MainGUI.form @@ -52,20 +52,22 @@ - - - - - - - - - + + + + + + + + + + + + - @@ -76,10 +78,18 @@ - - - - + + + + + + + + + + + + @@ -324,7 +334,7 @@ - + @@ -343,6 +353,14 @@ + + + + + + + + diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 3ea0d89..75b136d 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -2,17 +2,21 @@ package libsys; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.concurrent.CountDownLatch; @SuppressWarnings({"unchecked", "serial"}) public class MainGUI extends javax.swing.JFrame { Handler handler; + Settings settings; - public MainGUI(Handler handler) + public MainGUI(Handler handler, Settings settings) { this.handler = handler; + this.settings = settings; initComponents(); + nameLabel.setText(settings.get(settings.getKey(0))); } // //GEN-BEGIN:initComponents @@ -41,6 +45,7 @@ public class MainGUI extends javax.swing.JFrame nameLabel = new javax.swing.JLabel(); createBookBtn = new javax.swing.JButton(); createUserBtn = new javax.swing.JButton(); + settingsBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -193,7 +198,7 @@ public class MainGUI extends javax.swing.JFrame jTabbedPane.addTab("User", userPanel); nameLabel.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N - nameLabel.setText("Welcome to abc Library"); + nameLabel.setText("Welcome to the library"); createBookBtn.setText("Add new book"); createBookBtn.addActionListener(new java.awt.event.ActionListener() { @@ -209,6 +214,13 @@ public class MainGUI extends javax.swing.JFrame } }); + settingsBtn.setText("Settings"); + settingsBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + settingsBtnActionPerformed(evt); + } + }); + javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout.setHorizontalGroup( @@ -216,16 +228,18 @@ public class MainGUI extends javax.swing.JFrame .addGroup(mainPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jTabbedPane) .addGroup(mainPanelLayout.createSequentialGroup() - .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(mainPanelLayout.createSequentialGroup() - .addComponent(createBookBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(createUserBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addComponent(nameLabel)) - .addGap(0, 0, Short.MAX_VALUE))) - .addContainerGap()) + .addComponent(jTabbedPane) + .addContainerGap()) + .addGroup(mainPanelLayout.createSequentialGroup() + .addComponent(nameLabel) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(mainPanelLayout.createSequentialGroup() + .addComponent(createBookBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(createUserBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(settingsBtn)))) ); mainPanelLayout.setVerticalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -234,10 +248,15 @@ public class MainGUI extends javax.swing.JFrame .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(10, 10, 10) - .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(createBookBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(createUserBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(mainPanelLayout.createSequentialGroup() + .addGap(10, 10, 10) + .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(createBookBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(createUserBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup() + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(settingsBtn))) .addContainerGap()) ); @@ -298,6 +317,20 @@ public class MainGUI extends javax.swing.JFrame } }//GEN-LAST:event_createBookBtnActionPerformed + private void settingsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingsBtnActionPerformed + SettingsDialog settingsUI = new SettingsDialog(this, true, settings); + + settingsUI.setVisible(true); + + if (settingsUI.getReturnStatus() != -1) + { + settings.update(settings.getKey(0), settingsUI.getTitle()); + settings.update(settings.getKey(1), settingsUI.getUsersFilename()); + settings.update(settings.getKey(2), settingsUI.getBooksFilename()); + nameLabel.setText(settingsUI.getTitle()); + } + }//GEN-LAST:event_settingsBtnActionPerformed + private void rentBtnActionPerformed(java.awt.event.ActionEvent evt) { User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); @@ -378,6 +411,7 @@ public class MainGUI extends javax.swing.JFrame private javax.swing.JLabel nameLabel; private javax.swing.JButton rentBtn; private javax.swing.JButton returnBtn; + private javax.swing.JButton settingsBtn; private javax.swing.JLabel userBooks; private javax.swing.JLabel userID; private javax.swing.JLabel userLimit; diff --git a/src/main/java/libsys/Settings.java b/src/main/java/libsys/Settings.java new file mode 100644 index 0000000..dd782f0 --- /dev/null +++ b/src/main/java/libsys/Settings.java @@ -0,0 +1,101 @@ +package libsys; + +import java.util.Scanner; +import java.io.FileInputStream; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.ArrayList; + +public class Settings +{ + private HashMap items = new HashMap(); + private ArrayList keys = new ArrayList(); + private String settingsFileName; + + public Settings(String settingsFileName) + { + this.settingsFileName = settingsFileName; + + try + { + Scanner in = new Scanner(new FileInputStream(settingsFileName)); + while (in.hasNextLine()) + { + String newInput = in.nextLine(); + String[] inputSplitted = newInput.split(":"); + keys.add(inputSplitted[0]); + items.put(keys.get(keys.size() - 1), inputSplitted[1]); + } + } + catch (Exception e) + { + System.out.println("Settings file not found!"); + setDefault(); + } + } + + public void toFile() + { + try + { + PrintWriter out = new PrintWriter(settingsFileName); + for (int i = 0; i < keys.size(); i++) + { + out.println(keys.get(i) + ":" + items.get(keys.get(i))); + } + out.close(); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + setDefault(); + } + } + + public void update(String key, String newValue) + { + items.remove(key); + items.put(key, newValue); + toFile(); + } + + public int size() + { + return keys.size(); + } + + public String getKey(int location) + { + return keys.get(location); + } + + public String get(String key) + { + return items.get(key); + } + + public void setTitle(String newTitle) + { + update("Title", newTitle); + } + + public void setUsersFilename(String newUsersFilename) + { + update("Users Filename", newUsersFilename); + } + + public void setBooksFilename(String newBooksFilename) + { + update("Books Filename", newBooksFilename); + } + + public void setDefault() + { + keys.add("Title"); + items.put(keys.get(0), "Welcome to the library"); + keys.add("Users Filename"); + items.put(keys.get(1), "users.json"); + keys.add("Books Filename"); + items.put(keys.get(2), "books.json"); + } +} diff --git a/src/main/java/libsys/SettingsDialog.form b/src/main/java/libsys/SettingsDialog.form new file mode 100644 index 0000000..934bb8e --- /dev/null +++ b/src/main/java/libsys/SettingsDialog.form @@ -0,0 +1,135 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/libsys/SettingsDialog.java b/src/main/java/libsys/SettingsDialog.java new file mode 100644 index 0000000..84cdb5b --- /dev/null +++ b/src/main/java/libsys/SettingsDialog.java @@ -0,0 +1,220 @@ +package libsys; + +/** + * + * @author BinHong Lee + */ +public class SettingsDialog extends javax.swing.JDialog { + + /** + * Creates new form SettingsDialog + */ + public SettingsDialog(java.awt.Frame parent, boolean modal, Settings preset) { + super(parent, modal); + initComponents(); + setup(preset); + this.preset = preset; + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + settingsLbl = new javax.swing.JLabel(); + titleLbl = new javax.swing.JLabel(); + titleTxtField = new javax.swing.JTextField(); + usersFilenameLbl = new javax.swing.JLabel(); + usersFilenameTxt = new javax.swing.JTextField(); + booksFilenameLbl = new javax.swing.JLabel(); + booksFilenameTxt = new javax.swing.JTextField(); + saveBtn = new javax.swing.JButton(); + discardBtn = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + + settingsLbl.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N + settingsLbl.setText("Settings"); + + titleLbl.setText("Title :"); + + usersFilenameLbl.setText("Users Filename :"); + + booksFilenameLbl.setText("Books Filename :"); + + saveBtn.setText("Save"); + saveBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + saveBtnActionPerformed(evt); + } + }); + + discardBtn.setText("Discard"); + discardBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + discardBtnActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(saveBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE) + .addComponent(discardBtn)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(settingsLbl) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(usersFilenameLbl) + .addComponent(titleLbl) + .addComponent(booksFilenameLbl)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(booksFilenameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE) + .addComponent(titleTxtField) + .addComponent(usersFilenameTxt)))) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(settingsLbl) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(titleLbl) + .addComponent(titleTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(usersFilenameLbl) + .addComponent(usersFilenameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(booksFilenameLbl) + .addComponent(booksFilenameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(saveBtn) + .addComponent(discardBtn)) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed + doClose(1); + }//GEN-LAST:event_saveBtnActionPerformed + + private void discardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_discardBtnActionPerformed + doClose(-1); + }//GEN-LAST:event_discardBtnActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(SettingsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(SettingsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(SettingsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(SettingsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the dialog */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + SettingsDialog dialog = new SettingsDialog(new javax.swing.JFrame(), true, preset); + dialog.addWindowListener(new java.awt.event.WindowAdapter() { + @Override + public void windowClosing(java.awt.event.WindowEvent e) { + System.exit(0); + } + }); + dialog.setVisible(true); + } + }); + } + + private void setup(Settings preset) + { + titleTxtField.setText(preset.get(preset.getKey(0))); + usersFilenameTxt.setText((preset.get(preset.getKey(1)))); + booksFilenameTxt.setText(preset.get(preset.getKey(2))); + } + + private void doClose(int retStatus) + { + returnStatus = retStatus; + title = titleTxtField.getText(); + usersFilename = usersFilenameTxt.getText(); + booksFilename = booksFilenameTxt.getText(); + setVisible(false); + } + + public String getTitle() + { + return title; + } + + public String getUsersFilename() + { + return usersFilename; + } + + public String getBooksFilename() + { + return booksFilename; + } + + public int getReturnStatus() + { + return returnStatus; + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel booksFilenameLbl; + private javax.swing.JTextField booksFilenameTxt; + private javax.swing.JButton discardBtn; + private javax.swing.JButton saveBtn; + private javax.swing.JLabel settingsLbl; + private javax.swing.JLabel titleLbl; + private javax.swing.JTextField titleTxtField; + private javax.swing.JLabel usersFilenameLbl; + private javax.swing.JTextField usersFilenameTxt; + // End of variables declaration//GEN-END:variables + + private int returnStatus; + private String title; + private String usersFilename; + private String booksFilename; + private static Settings preset; +}