aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
diff options
context:
space:
mode:
authorMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-21 23:01:01 +0000
committerMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-21 23:01:01 +0000
commitaf1013959f6ab36ed9f2ff603c08116ee1b55c57 (patch)
tree330aaa14e50e50da5d6b31ffb82ba6ab3913d87a /src/uk/org/ury/library/viewer/LibraryViewerPanel.java
parenta69830990c285b98701cec9d9ef0e12dc8e045b2 (diff)
LibraryViewer etc.: User interface refinements including feedback when no results are matched or an empty string is provided.
Diffstat (limited to 'src/uk/org/ury/library/viewer/LibraryViewerPanel.java')
-rw-r--r--src/uk/org/ury/library/viewer/LibraryViewerPanel.java153
1 files changed, 135 insertions, 18 deletions
diff --git a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
index d6c2f79..af35149 100644
--- a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
+++ b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
@@ -5,9 +5,15 @@ package uk.org.ury.library.viewer;
import java.net.URL;
+import java.util.concurrent.ExecutionException;
+import javax.swing.JButton;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
+import javax.swing.SwingWorker;
import org.swixml.SwingEngine;
@@ -15,6 +21,7 @@ import uk.org.ury.frontend.FrontendError;
import uk.org.ury.frontend.FrontendPanel;
import uk.org.ury.frontend.HintField;
import uk.org.ury.library.LibraryTableModel;
+import uk.org.ury.library.exceptions.EmptySearchException;
/**
@@ -30,32 +37,28 @@ public class LibraryViewerPanel extends FrontendPanel
*/
private static final long serialVersionUID = -2441616418398056712L;
-
- /**
- * Action method for performing a search, bound by the UI XML
- * manifest to the search field and button
- */
-
- public void
- search ()
- {
- // TODO: SwingWorker?
-
- master.doSearch (searchField.getText ());
-
- // This is necessary to force the table to update with the new results.
-
- resultsTable.setModel (new LibraryTableModel (master.getLibraryList ()));
- }
-
/* Controller of this panel. */
+
private LibraryViewer master;
+
/* Panel widgets exposed by the SwiXML user interface. */
private JTable resultsTable;
+ private JScrollPane resultsPane;
+ private JPanel messagePanel;
+ private JLabel messageLabel;
private JTextField searchField;
+ private JButton searchButton;
+
+
+ /* This contains the last search failure message, for use in
+ * letting the user know what happened.
+ */
+
+ private String searchFailureMessage = "ALEX!";
+
/**
* Construct a new LibraryViewerPanel.
@@ -112,4 +115,118 @@ public class LibraryViewerPanel extends FrontendPanel
return "Library Viewer Demo";
}
+
+ /**
+ * Action method for performing a search, bound by the UI XML
+ * manifest to the search field and button.
+ */
+
+ public void
+ search ()
+ {
+ /* We can't let the user search while another search is going on,
+ * but it's not good to let the search "freeze" the UI.
+ *
+ * Hence the search function disables all sensitive parts of the
+ * interface and launches a search as a background process.
+ *
+ * We also swap the results table or no-results panel out for a
+ * panel that says "Searching...", in the interests of
+ * user-friendliness.
+ */
+
+ searchField.setEnabled (false);
+ searchButton.setEnabled (false);
+ resultsPane.setVisible (false);
+ messageLabel.setText ("Searching...");
+ messagePanel.setVisible (true);
+
+
+ SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void> ()
+ {
+ /**
+ * Perform a task in a separate thread from event-dispatch.
+ *
+ * In this case, perform a search.
+ *
+ * @return whether or not the search was successful.
+ */
+
+ @Override
+ public Boolean
+ doInBackground ()
+ {
+ try
+ {
+ master.doSearch (searchField.getText ());
+ }
+ catch (EmptySearchException e)
+ {
+ searchFailureMessage = "Please type in a search term.";
+ return false;
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Perform post-search cleanup and finalisation.
+ */
+
+ @Override
+ public void
+ done ()
+ {
+ // Figure out whether or not the search succeeded.
+
+ boolean hasSucceeded = false;
+
+ try
+ {
+ hasSucceeded = this.get ();
+ }
+ catch (InterruptedException e)
+ {
+ // Ignore
+ }
+ catch (ExecutionException e)
+ {
+ // Ignore
+ }
+
+
+ // Force table update with new results.
+
+ resultsTable.setModel (new LibraryTableModel (master.getLibraryList ()));
+
+
+ /* Re-enable widgets and swap panels according to whether
+ * or not results were found.
+ */
+
+ searchField.setEnabled (true);
+ searchButton.setEnabled (true);
+ messagePanel.setVisible (true);
+
+ if (hasSucceeded == false)
+ {
+ messageLabel.setText (searchFailureMessage);
+ }
+ else if (master.getLibraryList ().size () == 0)
+ {
+ messageLabel.setText ("Sorry, but no results were "
+ + "found for that term.");
+ }
+ else
+ {
+ messagePanel.setVisible (false);
+ resultsPane.setVisible (true);
+ }
+ }
+ };
+
+
+ worker.execute ();
+ }
}