aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
blob: af3514908ca7b4c46b6250bb122a22753785fe80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * 
 */
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;

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;


/**
 * Frontend panel providing access to an underlying library viewer.
 * 
 * @author Matt Windsor, Nathan Lasseter
 */

public class LibraryViewerPanel extends FrontendPanel
{
  /**
   * 
   */
  private static final long serialVersionUID = -2441616418398056712L;
 

  /* 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.
   * 
   * @param inMaster  The LibraryViewer controlling this LibraryViewerPanel.
   */
  
  public
  LibraryViewerPanel (LibraryViewer inMaster)
  {
    super ();
    
    master = inMaster;
   
    
    URL path = getClass ().getResource ("library_viewer_gui.xml");
    
    if (path == null)
      throw new IllegalArgumentException ("XML file does not exist.");
  
    SwingEngine se = new SwingEngine (this);
    se.getTaglib ().registerTag ("hint", HintField.class);
    
    
    /* The UI implementation is contained in library_viewer_gui.xml.
     *
     * It is hooked into the object resultsTable and the action 
     * method search.
     * 
     * See the XML file for more details.
     */
    
    try
      {
        se.insert (path, this);
      }
    catch (Exception e)
      {
        FrontendError.reportFatal ("UI creation failure: " + e.getMessage (), null);
      }
  }
  
  
  /**
   * @return  the name of the panel.
   * 
   * @see     uk.org.ury.frontend.FrontendPanel#getName()
   */
  
  @Override
  public String
  getName ()
  {
    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 ();
  }
}