diff options
Diffstat (limited to 'src/uk/org/ury/library/viewer')
-rw-r--r-- | src/uk/org/ury/library/viewer/LibraryTableModel.java | 184 | ||||
-rw-r--r-- | src/uk/org/ury/library/viewer/LibraryViewer.java | 95 | ||||
-rw-r--r-- | src/uk/org/ury/library/viewer/LibraryViewerPanel.java | 10 | ||||
-rw-r--r-- | src/uk/org/ury/library/viewer/library_viewer_gui.xml | 12 |
4 files changed, 217 insertions, 84 deletions
diff --git a/src/uk/org/ury/library/viewer/LibraryTableModel.java b/src/uk/org/ury/library/viewer/LibraryTableModel.java new file mode 100644 index 0000000..1ce5709 --- /dev/null +++ b/src/uk/org/ury/library/viewer/LibraryTableModel.java @@ -0,0 +1,184 @@ +/** + * + */ +package uk.org.ury.library.viewer; + +import java.util.List; + +import javax.swing.table.AbstractTableModel; + +import uk.org.ury.database.exceptions.MissingPropertyException; +import uk.org.ury.library.item.LibraryItem; +import uk.org.ury.library.item.LibraryItemProperty; + + +/** + * A table model for the library viewer. + * + * @author Matt Windsor + */ + +public class LibraryTableModel extends AbstractTableModel +{ + + /** + * + */ + + private static final long serialVersionUID = -1744980619128903509L; + + private List<LibraryItem> data; + + + /** + * Construct a new table model. + * + * @param data The list of data on which the model will be based. + */ + + public + LibraryTableModel (List<LibraryItem> data) + { + this.data = data; + } + + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getColumnCount() + */ + + @Override + public int + getColumnCount () + { + return 6; + } + + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getRowCount() + */ + + @Override + public int + getRowCount () + { + return data.size (); + } + + + /** + * @param c The column whose class should be investigated. + * + * @return the column class of column c. + */ + + @Override + public Class<?> + getColumnClass (int c) + { + return getValueAt (0, c).getClass (); + } + + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + + @Override + public Object + getValueAt (int rowIndex, int columnIndex) + { + LibraryItem li = data.get (rowIndex); + + try + { + String[] columnData = {li.get (LibraryItemProperty.TITLE), + li.get (LibraryItemProperty.ARTIST), + li.get (LibraryItemProperty.ALBUM)}; + + switch (columnIndex) + { + default: // Title, artist, album, unknown + + if (columnIndex >= columnData.length) + return "Unknown"; + else + return columnData[columnIndex]; + + case 3: // Medium + + // TODO: Make this less kludge-y + + String mediumString = li.get (LibraryItemProperty.MEDIUM); + + if (mediumString.equals ("c")) + return "Compact Disc"; + else if (mediumString.equals ("7")) + return "7\" Vinyl"; + else if (mediumString.equals ("2")) + return "12\" Vinyl"; + else + return "Unrecognised"; + + case 4: // Clean? + + // Return true if marked true, false if marked false or unknown etc. + + String cleanString = li.get (LibraryItemProperty.IS_CLEAN); + + // TODO: Nicer way of showing this + + if (cleanString.equals ("y")) + return "Yes"; + else if (cleanString.equals ("n")) + return "No"; + else + return "???"; + + case 5: // isDigitised + + // Return true if marked true, false if marked false or unknown etc. + + String digitisedString = li.get (LibraryItemProperty.IS_DIGITISED); + + if (digitisedString.equals ("t")) + return true; + else + return false; + } + } + catch (MissingPropertyException e) + { + return "Unknown"; + } + } + + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getColumnName(int, int) + */ + + @Override + public String + getColumnName (int index) + { + switch (index) + { + case 0: + return "Title"; + case 1: + return "Artist"; + case 2: + return "Album"; + case 3: + return "Medium"; + case 4: + return "Clean?"; + case 5: + return "On system?"; + default: + return "ERROR"; + } + } +} diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java index a98fa54..9587d5e 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewer.java +++ b/src/uk/org/ury/library/viewer/LibraryViewer.java @@ -1,27 +1,21 @@ package uk.org.ury.library.viewer; -import java.lang.reflect.InvocationTargetException; - import java.util.ArrayList; import java.util.List; -import javax.swing.SwingUtilities; - import uk.org.ury.config.ConfigReader; import uk.org.ury.database.DatabaseDriver; import uk.org.ury.database.UserClass; import uk.org.ury.database.exceptions.MissingCredentialsException; import uk.org.ury.database.exceptions.QueryFailureException; import uk.org.ury.frontend.AbstractFrontendModule; -import uk.org.ury.frontend.FrontendError; -import uk.org.ury.frontend.FrontendFrame; import uk.org.ury.frontend.FrontendMaster; import uk.org.ury.frontend.FrontendModulePanel; - -import uk.org.ury.library.LibraryItem; +import uk.org.ury.frontend.exceptions.UICreationFailureException; import uk.org.ury.library.LibraryUtils; import uk.org.ury.library.exceptions.EmptySearchException; +import uk.org.ury.library.item.LibraryItem; public class LibraryViewer extends AbstractFrontendModule { @@ -33,7 +27,6 @@ public class LibraryViewer extends AbstractFrontendModule private DatabaseDriver dd; private List<LibraryItem> libraryList; private LibraryViewerPanel panel; - private FrontendFrame frame; private ConfigReader config; @@ -53,64 +46,9 @@ public class LibraryViewer extends AbstractFrontendModule System.out.println(e); } - frame = null; libraryList = new ArrayList<LibraryItem> (); panel = null; } - - - /** - * Initialise the library viewer frontend as an applet. - */ - - public void - init () - { - frame = null; - libraryList = new ArrayList<LibraryItem> (); - panel = null; - - - try - { - SwingUtilities.invokeAndWait (new Runnable () - { - public void - run () - { - panel.setOpaque (true); - setContentPane (panel); - - runFrontend (null); - } - - }); - } - catch (InterruptedException e) - { - // TODO Auto-generated catch block - e.printStackTrace (); - } - catch (InvocationTargetException e) - { - // TODO Auto-generated catch block - e.printStackTrace (); - } - } - - - /** - * Run the library viewer frontend as an applet. - */ - - public void - start () - { - frame = null; - panel = new LibraryViewerPanel (this, null); - - add (panel); - } /** @@ -129,14 +67,22 @@ public class LibraryViewer extends AbstractFrontendModule catch (MissingCredentialsException e) { // TODO: Privilege de-escalation - FrontendError.reportFatal (e.getMessage (), frame); + master.fatalError (e.getMessage ()); } catch (Exception f) { - FrontendError.reportFatal (f.getMessage (), frame); + master.fatalError (f.getMessage ()); } - panel = new LibraryViewerPanel (this, master); + try + { + panel = new LibraryViewerPanel (this, master); + } + catch (UICreationFailureException e) + { + master.fatalError (e.getMessage ()); + } + return panel; } @@ -152,19 +98,16 @@ public class LibraryViewer extends AbstractFrontendModule * * @throws EmptySearchException if the search string is * empty or null (from LibraryUtils.search). + * + * @throws QueryFailureException if the search query + * fails (from LibraryUtils.search). */ public void - doSearch (String search) throws EmptySearchException + doSearch (String search) + throws EmptySearchException, QueryFailureException { - try - { - libraryList = LibraryUtils.search (dd, search); - } - catch (QueryFailureException e) - { - FrontendError.reportFatal (e.getMessage (), frame); - } + libraryList = LibraryUtils.search (dd, search); } diff --git a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java index 7638822..e453095 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java +++ b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java @@ -14,9 +14,10 @@ import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingWorker; +import uk.org.ury.database.exceptions.QueryFailureException; import uk.org.ury.frontend.FrontendMaster; import uk.org.ury.frontend.FrontendModulePanel; -import uk.org.ury.library.LibraryTableModel; +import uk.org.ury.frontend.exceptions.UICreationFailureException; import uk.org.ury.library.exceptions.EmptySearchException; @@ -59,10 +60,13 @@ public class LibraryViewerPanel extends FrontendModulePanel * @param viewer The LibraryViewer controlling this LibraryViewerPanel. * * @param master The FrontendMaster driving the frontend. + * + * @throws UICreationFailureException if the UI creation fails. */ public LibraryViewerPanel (LibraryViewer viewer, FrontendMaster master) + throws UICreationFailureException { super (viewer, "library_viewer_gui.xml", master); @@ -138,6 +142,10 @@ public class LibraryViewerPanel extends FrontendModulePanel { master.doSearch (searchField.getText ()); } + catch (QueryFailureException e) + { + searchFailureMessage = "Search failed: " + e.getMessage (); + } catch (EmptySearchException e) { searchFailureMessage = "Please type in a search term."; diff --git a/src/uk/org/ury/library/viewer/library_viewer_gui.xml b/src/uk/org/ury/library/viewer/library_viewer_gui.xml index 6b7e745..5177e3f 100644 --- a/src/uk/org/ury/library/viewer/library_viewer_gui.xml +++ b/src/uk/org/ury/library/viewer/library_viewer_gui.xml @@ -1,13 +1,11 @@ <?xml version="1.0" encoding="UTF-8"?> <panel layout="BorderLayout"> - <hbox constraints="BorderLayout.NORTH" border="EmptyBorder(5,5,5,5)" size="640,32"> - <label text="Search for:" labelfor="searchField" displayedmnemonic="VK_F" - font="Verdana-BOLD-14" /> + <hbox constraints="BorderLayout.NORTH" border="EmptyBorder(5,5,5,5)"> + <label text="Search for:" labelfor="searchField" displayedmnemonic="VK_F" /> <hbox border="EmptyBorder(0,5,0,5)"> <textfield id="searchField" mnemonic="VK_F" action="search" /> </hbox> - <button id="searchButton" text="Search" mnemonic="VK_S" action="search" - font="Verdana-BOLD-14" /> + <button id="searchButton" text="Search" mnemonic="VK_S" action="search" /> </hbox> <hbox constraints="BorderLayout.CENTER" border="EmptyBorder(0,5,0,5)"> @@ -16,14 +14,14 @@ </scrollpane> <panel id="searchingPanel" constraints="BorderLayout.CENTER" visible="false" layout="BorderLayout"> - <label id="searchingLabel" text="Searching..." font="Verdana-BOLD-16" + <label id="searchingLabel" text="Searching..." constraints="BorderLayout.CENTER" horizontalalignment="CENTER" /> <progressbar id="searchingProgressBar" indeterminate="true" constraints="BorderLayout.SOUTH" /> </panel> <panel id="messagePanel" constraints="BorderLayout.CENTER" visible="false" layout="BorderLayout"> - <label id="messageLabel" text="Searching..." font="Verdana-BOLD-16" + <label id="messageLabel" text="You shouldn't see this." constraints="BorderLayout.CENTER" horizontalalignment="CENTER" /> </panel> </hbox> |