AmazonClientGUIpublic class AmazonClientGUI extends Object implements ActionListener, ListSelectionListener
Fields Summary |
---|
public static final int | AUTHOR_SEARCH | public static final int | ASIN_SEARCH | public static final int | KEYWORD_SEARCH | public static final String | SEARCH_PROPERTY | private ora.jwsnut.chapter1.amazon.Details[] | details | private PropertyChangeSupport | pcs | private JLabel | resultLabel | private JLabel | rankLabel | private JComboBox | searchCombo | private JTextField | searchKey | private JEditorPane | reviews | private DefaultListModel | searchResults | private JList | resultsList | private JButton | goButton | private JFrame | frame |
Constructors Summary |
---|
public AmazonClientGUI()
JPanel panel = buildGUI();
frame = new JFrame("Amazon.com Web Service Demonstration");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
|
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent evt)
if (!searchKey.getText().trim().equals("")) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
firePropertyChange(SEARCH_PROPERTY, null, null);
}
| public void | addPropertyChangeListener(java.beans.PropertyChangeListener l)
pcs.addPropertyChangeListener(l);
| private javax.swing.JPanel | buildGUI()
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new GridBagLayout());
JPanel middlePanel = new JPanel(new BorderLayout());
resultLabel = new JLabel(" ");
// The top panel has the input components
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(3, 3, 3, 3);
topPanel.add(new JLabel("Search Type"), c);
c.gridy++;
topPanel.add(new JLabel("Search Key:"), c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.weightx = 1.0;
c.anchor = GridBagConstraints.WEST;
searchCombo = new JComboBox(new String[] { "Author", "ISBN", "Keyword" });
topPanel.add(searchCombo, c);
c.gridy++;
c.gridwidth = 1;
c.fill = GridBagConstraints.HORIZONTAL;
searchKey = new JTextField(32);
topPanel.add(searchKey, c);
searchKey.addActionListener(this);
c.gridx++;
c.weightx = 0.0;
goButton = new JButton("Go");
topPanel.add(goButton, c);
goButton.addActionListener(this);
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(middlePanel, BorderLayout.CENTER);
mainPanel.add(resultLabel, BorderLayout.SOUTH);
// The left panel has the list with all the results
JPanel leftPanel = new JPanel(new BorderLayout());
resultsList = new JList(searchResults);
resultsList.setVisibleRowCount(20);
resultsList.setPrototypeCellValue("MMMMMMMMMMMMMMM");
resultsList.addListSelectionListener(this);
resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
leftPanel.add(new JScrollPane(resultsList), BorderLayout.CENTER);
leftPanel.setBorder(BorderFactory.createTitledBorder("Results"));
middlePanel.add(leftPanel, BorderLayout.WEST);
// The right panel shows the sales rank and the
// reviews for the selected item.
JPanel rightPanel = new JPanel(new BorderLayout());
JPanel topRightPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 4));
topRightPanel.add(new JLabel("Sales Rank:"));
rankLabel = new JLabel();
topRightPanel.add(rankLabel);
JPanel lowerRightPanel = new JPanel(new BorderLayout());
lowerRightPanel.setBorder(BorderFactory.createTitledBorder("Reviews"));
reviews = new JEditorPane();
reviews.setContentType("text/html");
reviews.setEditable(false);
lowerRightPanel.add(new JScrollPane(reviews), BorderLayout.CENTER);
rightPanel.add(topRightPanel, BorderLayout.NORTH);
rightPanel.add(lowerRightPanel, BorderLayout.CENTER);
middlePanel.add(rightPanel, BorderLayout.CENTER);
return mainPanel;
| private void | firePropertyChange(java.lang.String property, java.lang.Object oldValue, java.lang.Object newValue)
pcs.firePropertyChange(property, oldValue, newValue);
| public java.lang.String | getSearchKey()
return searchKey.getText().trim();
| public int | getSearchType()
return searchCombo.getSelectedIndex();
| public void | removePropertyChangeListener(java.beans.PropertyChangeListener l)
pcs.removePropertyChangeListener(l);
| public void | setResults(ora.jwsnut.chapter1.amazon.ProductInfo info)
searchResults.clear();
if (info == null || info.getDetails().length == 0) {
// Error - clear the result list selection
resultsList.clearSelection();
details = null;
} else {
details = info.getDetails();
// Populate the list
for (int i = 0; i < details.length; i++) {
searchResults.addElement(details[i].getProductName());
}
resultsList.setSelectedIndex(0);
}
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
| public void | valueChanged(javax.swing.event.ListSelectionEvent evt)
int index = resultsList.getSelectedIndex();
if (index == -1) {
// No selection
rankLabel.setText("");
resultLabel.setText("");
reviews.setText("");
} else {
Details detail = details[index];
rankLabel.setText(detail.getSalesRank());
String[] authors = detail.getAuthors();
String result = detail.getProductName();
if (authors != null && authors.length > 0) {
result = authors[0] + ": " + result;
}
resultLabel.setText(result);
Reviews reviewList = detail.getReviews();
StringBuffer sb = new StringBuffer();
if (reviewList != null) {
sb.append("<B><I>Average customer review: ");
String rating = reviewList.getAvgCustomerRating();
sb.append(rating == null ? "None" : rating);
sb.append("</I></B><P>");
CustomerReview[] custReviews = reviewList.getCustomerReviews();
if (custReviews != null) {
for (int i = 0; i < custReviews.length; i++) {
CustomerReview c = custReviews[i];
sb.append("<FONT SIZE=\"+1\" COLOR=\"#FF0000\">Rating: ");
sb.append(c.getRating());
sb.append(" ");
sb.append(c.getSummary());
sb.append("<BR>");
sb.append("</B><FONT SIZE=\"-1\" COLOR=\"#000000\"><P>");
sb.append(c.getComment());
sb.append("<P>");
}
}
}
reviews.setText(sb.toString());
reviews.setCaretPosition(0);
}
|
|