KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > amazon > AmazonExplorer


1 package thinlet.amazon;
2
3 import java.applet.*;
4 import java.awt.*;
5 import java.io.*;
6 import java.net.*;
7
8 import thinlet.*;
9
10 /**
11  *
12  */

13 public class AmazonExplorer extends Thinlet {
14
15     private static final String JavaDoc[] MODES = {
16         "baby", "books", "classical", "dvd", "electronics", "garden", "kitchen", "magazines",
17         "music", "pc-hardware", "photo", "software", "toys", "universal", "vhs", "videogames" };
18     private static final String JavaDoc[] BOOKS = {
19         "+pmrank", "+salesrank", "+reviewrank", "+pricerank", "+inverse-pricerank",
20         "+daterank", "+titlerank", "-titlerank" };
21     private static final String JavaDoc[] CLASSICAL_MUSIC = {
22         "+psrank", "+salesrank", "+artistrank", "+orig-rel-date", "+titlerank" };
23     private static final String JavaDoc[] DVD = {
24         "+salesrank", "+titlerank" };
25     private static final String JavaDoc[] ELECTRONICS = {
26         "+pmrank", "+salesrank", "+titlerank", "+reviewrank" };
27     private static final String JavaDoc[] GARDEN_TOOL = {
28         "+psrank", "+salesrank", "+titlerank", "-titlerank",
29         "+manufactrank", "-manufactrank", "+price", "-price" };
30     private static final String JavaDoc[] KITCHEN = {
31         "+pmrank", "+salesrank", "+titlerank", "-titlerank",
32         "+manufactrank", "-manufactrank", "+price", "-price" };
33     private static final String JavaDoc[] HARDWARE = {
34         "+psrank", "+salesrank", "+titlerank", "-titlerank" };
35     private static final String JavaDoc[] PHOTO = {
36         "+pmrank", "+salesrank", "+titlerank", "-titlerank" };
37     private static final String JavaDoc[] SOFTWARE = {
38         "+pmrank", "+salesrank", "+titlerank", "+price", "-price" };
39     private static final String JavaDoc[] VHS = {
40         "+psrank", "+salesrank", "+titlerank" };
41     private static final String JavaDoc[] VIDEOGAMES = {
42         "+pmrank", "+salesrank", "+titlerank", "+price", "-price" };
43     private static final String JavaDoc[] SORTS = { "Featured Items", "+pmrank",
44         "Bestselling", "+salesrank", "Average Customer Review", "+reviewrank",
45         "Price (Low to High)", "+pricerank", "Price (High to Low)", "+inverse-pricerank",
46         "Publication Date", "+daterank", "Alphabetical (A-Z)", "+titlerank",
47         "Alphabetical (Z-A)", "-titlerank", "Featured Items", "+psrank",
48         "Artist Name", "+artistrank", "Original Release Date", "+orig-rel-date",
49         "Alphabetical", "+titlerank", "Review", "+reviewrank",
50         "Manufacturer (A-Z)", "+manufactrank", "Manufacturer (Z-A)", "-manufactrank",
51         "Price (Low to High)", "+price", "Price (High to Low)", "-price" };
52         
53     /**
54      *
55      */

56     public AmazonExplorer() {
57         setFont(new Font("SansSerif", Font.PLAIN, 11));
58         try {
59             add(parse("explorer.xml"));
60             selectMode(1, find("searchby"), find("keyword"), find("sort"));
61         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
62     }
63     
64     /**
65      *
66      */

67     public void selectMode(int selected, Object JavaDoc searchby, Object JavaDoc keyword, Object JavaDoc sort) {
68         String JavaDoc mode = MODES[selected];
69         removeAll(searchby);
70         addChoice(searchby, "Keyword");
71         if ((mode == "music") || (mode == "classical")) {
72             addChoice(searchby, "Artist");
73         }
74         else if (mode == "books") {
75             addChoice(searchby, "Author");
76         }
77         else if ((mode == "dvd") || (mode == "vhs") || (mode == "video")) {
78             addChoice(searchby, "Actor");
79             addChoice(searchby, "Director");
80         }
81         else if ((mode == "electronics") || (mode == "kitchen") || (mode == "videogames") ||
82                 (mode == "software") || (mode == "photo") || (mode == "pc-hardware")) {
83             addChoice(searchby, "Manufacturer");
84         }
85         setInteger(searchby, "selected", 0);
86         
87         removeAll(sort);
88         String JavaDoc[] sorts = getSort(mode);
89         if (sorts != null) {
90             for (int i = 0; i < sorts.length; i++) {
91                 for (int j = 0; j < SORTS.length; j += 2) {
92                     if (SORTS[j + 1] == sorts[i]) {
93                         Object JavaDoc choice = create("choice");
94                         setString(choice, "text", SORTS[j]);
95                         add(sort, choice);
96                         break;
97                     }
98                 }
99             }
100         }
101         setInteger(sort, "selected", -1);
102         setString(sort, "text", null);
103         setBoolean(sort, "enabled", sorts != null);
104     }
105     
106     /**
107      *
108      */

109     private void addChoice(Object JavaDoc combobox, String JavaDoc text) {
110         Object JavaDoc choice = create("choice");
111         setString(choice, "text", text);
112         add(combobox, choice);
113     }
114     
115     /**
116      *
117      */

118     private static final String JavaDoc[] getSort(String JavaDoc mode) {
119         if (mode == "books") { return BOOKS; }
120         if ((mode == "classical") || (mode == "music")) { return CLASSICAL_MUSIC; }
121         if (mode == "dvd") { return DVD; }
122         if (mode == "electronics") { return ELECTRONICS; }
123         if ((mode == "garden") || (mode == "universal")) { return GARDEN_TOOL; }
124         if (mode == "kitchen") { return KITCHEN; }
125         if (mode == "pc-hardware") { return HARDWARE; }
126         if (mode == "photo") { return PHOTO; }
127         if (mode == "software") { return SOFTWARE; }
128         if (mode == "vhs") { return VHS; }
129         if (mode == "videogames") { return VIDEOGAMES; }
130         // if ((mode == "baby") || (mode == "magazines") || (mode == "toys")) {
131
return null;
132     }
133     
134     /**
135      *
136      */

137     public void find(int mode, String JavaDoc searchby, Object JavaDoc keyfield, String JavaDoc keyword, int sort) {
138         String JavaDoc searchstring = searchby + "Search=" + convert(keyword) +
139             "&mode=" + MODES[mode] + "&type=lite" +
140             ((sort == -1) ? "" : ("&sort=" + getSort(MODES[mode])[sort]));
141             
142         // add the new keyword to the list
143
boolean cacheditem = false; int n = getCount(keyfield);
144         if (keyword.length() > 0) {
145             for (int i = n - 1; i >= 0; i--) {
146                 String JavaDoc choicetext = getString(getItem(keyfield, i), "text");
147                 if (keyword.equals(choicetext)) { cacheditem = true; break; }
148             }
149             if (!cacheditem) {
150                 Object JavaDoc choice = create("choice");
151                 setString(choice, "text", keyword);
152                 add(keyfield, choice);
153                 if (n > 8) { remove(getItem(keyfield, 0)); }
154             }
155         }
156             
157         Object JavaDoc nProductInfo = getResponse(searchstring + "&page=1");
158         if (!isErrorMessage(nProductInfo)) {
159             try {
160                 Object JavaDoc result = parse("result.xml");
161                 Object JavaDoc nextresult = find(result, "nextresult");
162                 putProperty(nextresult, "SearchString", searchstring);
163                 loadList(nProductInfo, 1, find(result, "resultlist"), find(result, "total"), nextresult);
164                 addPage(result);
165             } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
166         }
167     }
168     
169     /**
170      *
171      */

172     public void resultSelected(Object JavaDoc resultlist, Object JavaDoc detailsbutton) {
173         setBoolean(detailsbutton, "enabled", getSelectedIndex(resultlist) != -1);
174     }
175     
176     /**
177      *
178      */

179     public void findNext(Object JavaDoc nextresult, Object JavaDoc total, Object JavaDoc resultlist) {
180         String JavaDoc searchstring = (String JavaDoc) getProperty(nextresult, "SearchString");
181         int page = ((Integer JavaDoc) getProperty(nextresult, "SearchPage")).intValue() + 1;
182         
183         Object JavaDoc nProductInfo = getResponse(searchstring + "&page=" + page);
184         if (!isErrorMessage(nProductInfo)) {
185             loadList(nProductInfo, page, resultlist, total, nextresult);
186         }
187     }
188     
189     /**
190      *
191      */

192     private void loadList(Object JavaDoc nProductInfo, int page, Object JavaDoc resultlist, Object JavaDoc total, Object JavaDoc nextresult) {
193         loadDetailsList(nProductInfo, resultlist);
194         
195         String JavaDoc sTotalResults = getDOMText(nProductInfo, "TotalResults");
196         setString(total, "text", sTotalResults);
197         
198         putProperty(nextresult, "SearchPage", new Integer JavaDoc(page));
199         setBoolean(nextresult, "enabled", Integer.parseInt(sTotalResults) > getCount(resultlist));
200     }
201     
202     /**
203      *
204      */

205     private void loadDetailsList(Object JavaDoc nProductInfo, Object JavaDoc resultlist) {
206         int n = getDOMCount(nProductInfo, "Details");
207         for (int i = 0; i < n; i++) {
208             Object JavaDoc nDetails = getDOMNode(nProductInfo, "Details", i);
209             Object JavaDoc item = create("item");
210             setString(item, "text", getDOMText(nDetails, "ProductName"));
211             putProperty(item, "Details", nDetails);
212             add(resultlist, item);
213         }
214     }
215     
216     /**
217      *
218      */

219     public void showDetails(Object JavaDoc resultlist) {
220         Object JavaDoc item = getSelectedItem(resultlist);
221         Object JavaDoc nDetails = getProperty(item, "Details");
222         
223         try {
224             Object JavaDoc details = parse("details.xml");
225             putProperty(details, "Asin", getDOMText(nDetails, "Asin"));
226             
227             //Info
228
Object JavaDoc productname = find(details, "productname");
229             setString(productname, "text", getDOMText(nDetails, "ProductName"));
230             //setIcon(productname, "icon", loadIcon(getDOMText(nDetails, "ImageUrlSmall")));
231

232             Object JavaDoc infoproperties = find(details, "infoproperties");
233             addLabel(infoproperties, "Name:", nDetails, "ProductName");
234             addLabel(infoproperties, "Catalog:", nDetails, "Catalog");
235             addList(infoproperties, "Artist:", nDetails, "Artists", "Artist");
236             addList(infoproperties, "Author:", nDetails, "Authors", "Author");
237             addLabel(infoproperties, "Released:", nDetails, "ReleaseDate");
238             addLabel(infoproperties, "Manufacturer:", nDetails, "Manufacturer");
239             addLabel(infoproperties, "List price:", nDetails, "ListPrice");
240             addLabel(infoproperties, "Our price:", nDetails, "OurPrice");
241             addLabel(infoproperties, "Used price:", nDetails, "UsedPrice");
242                 
243             addPage(details);
244         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
245     }
246     
247     /**
248      *
249      */

250     public void checkDetails(Object JavaDoc details, int selectedtab) {
251         if (selectedtab == 0) { return; } // first tab contains lite information
252
String JavaDoc sAsin = (String JavaDoc) getProperty(details, "Asin");
253         if (sAsin != null) {
254             String JavaDoc searchstring = "AsinSearch=" + sAsin + "&type=heavy";
255             Object JavaDoc nProductInfo = getResponse(searchstring);
256             if (!isErrorMessage(nProductInfo)) {
257                 Object JavaDoc nDetails = getDOMNode(nProductInfo, "Details", 0);
258                 if (nDetails == null) { return; } //never
259
putProperty(details, "Asin", null);
260                 
261                 //More
262
Object JavaDoc moreproperties = find(details, "moreproperties");
263                 addList(moreproperties, "Starring:", nDetails, "Starring", "Actor");
264                 addList(moreproperties, "Director:", nDetails, "Directors", "Director");
265                 addLabel(moreproperties, "Theatrical date:", nDetails, "TheatricalReleaseDate");
266                 addLabel(moreproperties, "Refurbished:", nDetails, "RefurbishedPrice");
267                 addLabel(moreproperties, "Collectible:", nDetails, "CollectiblePrice");
268                 addLabel(moreproperties, "Third-party:", nDetails, "ThirdPartyNewPrice");
269                 addLabel(moreproperties, "Sales rank:", nDetails, "SalesRank");
270                 addLabel(moreproperties, "Media:", nDetails, "Media");
271                 addLabel(moreproperties, "Number:", nDetails, "NumMedia");
272                 addLabel(moreproperties, "Mpaa rating:", nDetails, "MpaaRating");
273                 addLabel(moreproperties, "Availability:", nDetails, "Availability");
274                 
275                 //Review
276
Object JavaDoc nReviews = getDOMNode(nDetails, "Reviews", 0);
277                 if (nReviews != null) {
278                     Object JavaDoc reviewpanel = find(details, "reviewpanel");
279                     String JavaDoc sAvgCustomerRating = getDOMText(nReviews, "AvgCustomerRating");
280                     Object JavaDoc avgrating = find(reviewpanel, "avgrating");
281                     setInteger(avgrating, "value", getNumber(sAvgCustomerRating));
282                     setString(avgrating, "tooltip", sAvgCustomerRating);
283                     
284                     loadReview(reviewpanel, nReviews, 0);
285                     putProperty(reviewpanel, "Reviews", nReviews);
286                 }
287                 
288                 //Detail
289
Object JavaDoc detailpanel = find(details, "detailpanel");
290                 loadList(getDOMNode(nDetails, "Platforms", 0), "Platform",
291                     detailpanel, "platformtitle", "platformlist");
292                 loadList(getDOMNode(nDetails, "Features", 0), "Feature",
293                     detailpanel, "featuretitle", "featurelist");
294                     
295                 //Link
296
Object JavaDoc linkpanel = find(details, "linkpanel");
297                 loadLink(find(linkpanel, "accessories"), nDetails, "Accessories");
298                 loadLink(find(linkpanel, "similars"), nDetails, "SimilarProducts");
299                 loadLink(find(linkpanel, "wishlists"), nDetails, "Lists");
300                 putProperty(find(linkpanel, "marketsearch"), "Asin", sAsin);
301                     
302                 Object JavaDoc nBrowseList = getDOMNode(nDetails, "BrowseList", 0);
303                 if (nBrowseList != null) {
304                     int n = getDOMCount(nBrowseList, "BrowseNode");
305                     Object JavaDoc browselist = find(linkpanel, "browselist");
306                     for (int i = 0; i < n; i++) {
307                         Object JavaDoc nBrowseNode = getDOMNode(nBrowseList, "BrowseNode", i);
308                         Object JavaDoc browseitem = create("item");
309                         setString(browseitem, "text", getDOMText(getDOMNode(nBrowseNode, "BrowseName", 0)));
310                         add(browselist, browseitem);
311                     }
312                 }
313             }
314         }
315     }
316     
317     /**
318      *
319      */

320     private void loadLink(Object JavaDoc button, Object JavaDoc nDetails, String JavaDoc listname) {
321         Object JavaDoc list = getDOMNode(nDetails, listname, 0);
322         setBoolean(button, "enabled", (list != null));
323         if (list != null) {
324             putProperty(button, listname, list);
325         }
326     }
327     
328     /**
329      *
330      */

331     public void previousReview(Object JavaDoc reviewpanel) {
332         int i = ((Integer JavaDoc) getProperty(reviewpanel, "ReviewIndex")).intValue();
333         loadReview(reviewpanel, getProperty(reviewpanel, "Reviews"), i - 1);
334     }
335     
336     /**
337      *
338      */

339     public void nextReview(Object JavaDoc reviewpanel) {
340         int i = ((Integer JavaDoc) getProperty(reviewpanel, "ReviewIndex")).intValue();
341         loadReview(reviewpanel, getProperty(reviewpanel, "Reviews"), i + 1);
342     }
343     
344     /**
345      *
346      */

347     private void loadReview(Object JavaDoc reviewpanel, Object JavaDoc nReviews, int i) {
348         int n = getDOMCount(nReviews, "CustomerReview");
349         if ((i >= 0) && (i < n)) {
350             setBoolean(find(reviewpanel, "prevreview"), "enabled", i > 0);
351             setBoolean(find(reviewpanel, "nextreview"), "enabled", i < n - 1);
352             
353             putProperty(reviewpanel, "ReviewIndex", new Integer JavaDoc(i));
354         
355             Object JavaDoc nCustomerReview = getDOMNode(nReviews, "CustomerReview", i);
356             String JavaDoc sRating = getDOMText(nCustomerReview, "Rating");
357             Object JavaDoc rating = find(reviewpanel, "rating");
358             setInteger(rating, "value", getNumber(sRating));
359             setString(rating, "tooltip", sRating);
360             
361             setString(find(reviewpanel, "summary"), "text", getDOMText(nCustomerReview, "Summary"));
362             setString(find(reviewpanel, "comment"), "text",
363                 convertHTML(getDOMText(nCustomerReview, "Comment")));
364         }
365     }
366     
367     /**
368      *
369      */

370     private void loadList(Object JavaDoc root, String JavaDoc nodename, Object JavaDoc panel, String JavaDoc titlename, String JavaDoc listname) {
371         Object JavaDoc list = find(panel, listname);
372         if (root != null) {
373             int n = getDOMCount(root, nodename);
374             for (int i = 0; i < n; i++) {
375                 Object JavaDoc item = create("item");
376                 setString(item, "text", getDOMText(getDOMNode(root, nodename, i)));
377                 add(list, item);
378             }
379         }
380         else {
381             Object JavaDoc title = find(panel, titlename);
382             setBoolean(title, "visible", false);
383             setBoolean(list, "visible", false);
384         }
385     }
386     
387     /**
388      *
389      */

390     public void showAccessories(Object JavaDoc button) {
391         showLinkList(getProperty(button, "Accessories"), "Accessory", "Accessories:");
392     }
393     
394     /**
395      *
396      */

397     public void showSimilars(Object JavaDoc button) {
398         showLinkList(getProperty(button, "SimilarProducts"), "Product", "Similar products:");
399     }
400     
401     /**
402      *
403      */

404     private void showLinkList(Object JavaDoc nRoot, String JavaDoc leafname, String JavaDoc title) {
405         StringBuffer JavaDoc asins = new StringBuffer JavaDoc();
406         int n = getDOMCount(nRoot, leafname);
407         for (int i = 0; i < n; i++) {
408             if (i != 0) asins.append(',');
409             asins.append(getDOMText(getDOMNode(nRoot, leafname, i)));
410         }
411         
412         Object JavaDoc nProductInfo = getResponse("ListManiaSearch=" + asins.toString() + "&type=lite");
413         if (!isErrorMessage(nProductInfo)) {
414             try {
415                 Object JavaDoc linklist = parse("linklist.xml");
416                 setString(find(linklist, "title"), "text", title);
417                 loadDetailsList(nProductInfo, find(linklist, "resultlist"));
418                 addPage(linklist);
419             } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
420         }
421     }
422     
423     /**
424      *
425      */

426     public void showWishlists(Object JavaDoc button) {
427         try {
428             Object JavaDoc listmania = parse("listmania.xml");
429             Object JavaDoc nLists = getProperty(button, "Lists");
430             putProperty(listmania, "Lists", nLists);
431             loadWishlist(nLists, 0, listmania,
432                 find(listmania, "resultlist"), find(listmania, "listname"),
433                 find(listmania, "prevlist"), find(listmania, "nextlist"));
434             addPage(listmania);
435         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
436     }
437     
438     /**
439      *
440      */

441     public void previousWishlist(Object JavaDoc listmania,
442             Object JavaDoc resultlist, Object JavaDoc listname, Object JavaDoc prevlist, Object JavaDoc nextlist) {
443         int index = ((Integer JavaDoc) getProperty(listmania, "ListsIndex")).intValue();
444         loadWishlist(getProperty(listmania, "Lists"), index - 1,
445             listmania, resultlist, listname, prevlist, nextlist);
446     }
447     
448     /**
449      *
450      */

451     public void nextWishlist(Object JavaDoc listmania,
452             Object JavaDoc resultlist, Object JavaDoc listname, Object JavaDoc prevlist, Object JavaDoc nextlist) {
453         int index = ((Integer JavaDoc) getProperty(listmania, "ListsIndex")).intValue();
454         loadWishlist(getProperty(listmania, "Lists"), index + 1,
455             listmania, resultlist, listname, prevlist, nextlist);
456     }
457     
458     /**
459      *
460      */

461     private void loadWishlist(Object JavaDoc nLists, int index, Object JavaDoc listmania,
462             Object JavaDoc resultlist, Object JavaDoc listname, Object JavaDoc prevlist, Object JavaDoc nextlist) {
463         int n = getDOMCount(nLists, "ListId");
464         if ((index >= 0) && (index < n)) {
465             String JavaDoc sListId = getDOMText(getDOMNode(nLists, "ListId", index));
466             Object JavaDoc nProductInfo = getResponse("ListManiaSearch=" + sListId + "&type=lite");
467             if (!isErrorMessage(nProductInfo)) {
468                 setString(listname, "text", getDOMText(nProductInfo, "ListName"));
469                 removeAll(resultlist);
470                 loadDetailsList(nProductInfo, resultlist);
471                 putProperty(listmania, "ListsIndex", new Integer JavaDoc(index));
472                 setBoolean(prevlist, "enabled", index > 0);
473                 setBoolean(nextlist, "enabled", index < n - 1);
474             }
475         }
476     }
477     
478     /**
479      *
480      */

481     public void searchMarket(Object JavaDoc button, Object JavaDoc offertype) {
482         String JavaDoc asin = (String JavaDoc) getProperty(button, "Asin");
483         String JavaDoc type = getString(getItem(offertype, getInteger(offertype, "selected")), "text");
484         
485         String JavaDoc searchstring = "AsinSearch=" + asin + "&type=heavy&offer=" + type;
486         Object JavaDoc nProductInfo = getResponse(searchstring + "&offerpage=1");
487         if (!isErrorMessage(nProductInfo)) {
488             try {
489                 Object JavaDoc market = parse("market.xml");
490                 
491                 Object JavaDoc nDetails = getDOMNode(nProductInfo, "Details", 0);
492                 if (nDetails == null) { return; }
493                 
494                 String JavaDoc offerings = getDOMText(nDetails, "NumberOfOfferings");
495                 setString(find(market, "offerings"), "text", offerings);
496                 
497                 Object JavaDoc nInfo = getDOMNode(nDetails, "ThirdPartyProductInfo", 0);
498                 Object JavaDoc detailslist = find(market, "detailslist");
499                 int n = getDOMCount(nInfo, "ThirdPartyProductDetails");
500                 for (int i = 0; i < n; i++) {
501                     Object JavaDoc nProduct = getDOMNode(nInfo, "ThirdPartyProductDetails", i);
502                     Object JavaDoc row = create("row");
503                     putProperty(row, "ThirdPartyProductDetails", nProduct);
504                     Object JavaDoc cellNick = create("cell");
505                     setString(cellNick, "text", getDOMText(nProduct, "SellerNickname"));
506                     add(row, cellNick);
507                     Object JavaDoc cellPrice = create("cell");
508                     setString(cellPrice, "text", getDOMText(nProduct, "OfferingPrice"));
509                     add(row, cellPrice);
510                     add(detailslist, row);
511                 }
512                 
513                 addPage(market);
514             } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
515         }
516     }
517     
518     /**
519      *
520      */

521     public void showMarketDetails(Object JavaDoc detailslist) {
522         Object JavaDoc nProduct = getProperty(getSelectedItem(detailslist), "ThirdPartyProductDetails");
523         try {
524             Object JavaDoc exchange = parse("exchange.xml");
525             Object JavaDoc properties = find(exchange, "properties");
526             
527             addLabel(properties, "Nickname:", nProduct, "SellerNickname");
528             addLabel(properties, "Price:", nProduct, "OfferingPrice");
529             addLabel(properties, "Condition:", nProduct, "Condition");
530             addField(properties, "Condition:", nProduct, "ConditionType");
531             addLabel(properties, "Availability:", nProduct, "ExchangeAvailability");
532             addLabel(properties, "Country:", nProduct, "SellerCountry");
533             addLabel(properties, "State:", nProduct, "SellerState");
534             addField(properties, "Comments:", nProduct, "ShipComments");
535             addLabel(properties, "Rating:", nProduct, "SellerRating");
536             
537             add(exchange);
538         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
539     }
540     
541     /**
542      *
543      */

544     private void addLabel(Object JavaDoc panel, String JavaDoc title, Object JavaDoc root, String JavaDoc key) {
545         String JavaDoc value = getDOMText(root, key);
546         if (value != null) {
547             Object JavaDoc label = create("label");
548             setString(label, "text", title);
549             add(panel, label);
550             Object JavaDoc field = create("label");
551             setString(field, "text", value);
552             setInteger(field, "weightx", 1);
553             add(panel, field);
554         }
555     }
556     
557     /**
558      *
559      */

560     private void addField(Object JavaDoc panel, String JavaDoc title, Object JavaDoc root, String JavaDoc key) {
561         String JavaDoc value = getDOMText(root, key);
562         if (value != null) {
563             Object JavaDoc label = create("label");
564             setString(label, "text", title);
565             add(panel, label);
566             Object JavaDoc field = create("textfield");
567             setString(field, "text", getDOMText(root, key));
568             setBoolean(field, "editable", false);
569             setInteger(field, "weightx", 1);
570             add(panel, field);
571         }
572     }
573     
574     /**
575      *
576      */

577     private void addList(Object JavaDoc panel, String JavaDoc title, Object JavaDoc root, String JavaDoc key, String JavaDoc itemkey) {
578         Object JavaDoc mainnode = getDOMNode(root, key, 0);
579         if (mainnode != null);
580         int n = getDOMCount(mainnode, itemkey);
581         for (int i = 0; i < n; i++) {
582             Object JavaDoc label = create("label");
583             if (i == 0) { setString(label, "text", title); }
584             add(panel, label);
585             
586             Object JavaDoc field = create("label");
587             setString(field, "text", getDOMText(getDOMNode(mainnode, itemkey, i)));
588             setInteger(field, "weightx", 1);
589             add(panel, field);
590         }
591     }
592     
593     // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
594

595     /**
596      *
597      */

598     public void exit() {
599         System.exit(0);
600     }
601     
602     /**
603      *
604      */

605     public void defaultTheme() {
606         setColors(0xe6e6e6, 0x000000, 0xffffff,
607             0x909090, 0xb0b0b0, 0xededed, 0xb9b9b9, 0x89899a, 0xc5c5dd);
608     }
609     
610     /**
611      *
612      */

613     public void yellowTheme() {
614         setColors(0xeeeecc, 0x000000, 0xffffff,
615             0x999966, 0xb0b096, 0xededcb, 0xcccc99, 0xcc6600, 0xffcc66);
616     }
617     
618     /**
619      *
620      */

621     public void blueTheme() {
622         setColors(0x6375d6, 0xffffff, 0x7f8fdd,
623             0xd6dff5, 0x9caae5, 0x666666, 0x003399, 0xff3333, 0x666666);
624     }
625     
626     /**
627      *
628      */

629     public void about() {
630         try {
631             add(parse("about.xml"));
632         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
633     }
634     
635     // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
636

637     /**
638      *
639      */

640     private Object JavaDoc getResponse(String JavaDoc path) {
641         String JavaDoc associatestag = "thinlet-20";
642         String JavaDoc developertoken = "D3MAIAYX2Q6JLY";
643         String JavaDoc url = "http://xml.amazon.com/onca/xml2?t=" + associatestag +
644             "&dev-t=" + developertoken + "&" + path + "&f=xml";
645         if (getParent() instanceof Applet) {
646             String JavaDoc proxy = ((Applet) getParent()).getParameter("proxy");
647             if (proxy != null) {
648                 url = proxy + convert(url);
649             }
650         }
651         try {
652             return parseDOM(new URL(url).openStream());
653         } catch (Exception JavaDoc exc) {
654             showException(exc);
655             return null;
656         }
657     }
658     
659     /**
660      *
661      */

662     private void addPage(Object JavaDoc page) {
663         Object JavaDoc mainpanel = find("mainpanel");
664         int n = getCount(mainpanel);
665         setBoolean(getItem(mainpanel, n - 1), "visible", false);
666         add(mainpanel, page);
667     }
668     
669     /**
670      *
671      */

672     public void removePage() {
673         Object JavaDoc mainpanel = find("mainpanel");
674         int n = getCount(mainpanel);
675         setBoolean(getItem(mainpanel, n - 2), "visible", true);
676         remove(getItem(mainpanel, n - 1));
677     }
678     
679     /**
680      *
681      */

682     protected void handleException(Throwable JavaDoc throwable) {
683         showException(throwable);
684     }
685     
686     /**
687      *
688      */

689     private void showException(Throwable JavaDoc thr) {
690         StringWriter writer = new StringWriter();
691         thr.printStackTrace(new PrintWriter(writer));
692         String JavaDoc trace = writer.toString().replace('\r', ' ').replace('\t', ' ');
693         String JavaDoc thrclass = thr.getClass().getName();
694         thrclass = thrclass.substring(thrclass.lastIndexOf('.') + 1);
695         
696         try {
697             Object JavaDoc exceptiondialog = parse("exception.xml");
698             setString(exceptiondialog, "text", thrclass);
699             setString(find(exceptiondialog, "message"), "text", thr.getMessage());
700             setString(find(exceptiondialog, "stacktrace"), "text", trace);
701             add(exceptiondialog);
702         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
703     }
704     
705     /**
706      *
707      */

708     private boolean isErrorMessage(Object JavaDoc nProductInfo) {
709         if (nProductInfo == null) { return true; }
710         String JavaDoc sErrorMsg = getDOMText(nProductInfo, "ErrorMsg");
711         if (sErrorMsg == null) { return false; }
712         try {
713             Object JavaDoc exceptiondialog = parse("error.xml");
714             setString(find(exceptiondialog, "message"), "text", sErrorMsg);
715             add(exceptiondialog);
716         } catch (Exception JavaDoc exc) { exc.printStackTrace(); }
717         return true;
718     }
719     
720     /**
721      *
722      */

723     public void closeDialog(Object JavaDoc dialog) {
724         remove(dialog);
725     }
726     
727     // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
728

729     /**
730      *
731      */

732     private String JavaDoc getDOMText(Object JavaDoc node, String JavaDoc key) {
733         Object JavaDoc leaf = getDOMNode(node, key, 0);
734         return (leaf != null) ? getDOMText(leaf) : null;
735     }
736     
737     /**
738      *
739      */

740     private static int getNumber(String JavaDoc text) {
741         try {
742             if (text.startsWith("$")) { text = text.substring(1); }
743             int dot = text.indexOf('.');
744             text = text + "00";
745             if (dot != -1) {
746                 text = text.substring(0, dot) +
747                     text.substring(dot + 1, Math.min(dot + 3, text.length()));
748             }
749             return Integer.parseInt(text);
750         } catch (Exception JavaDoc exc) {
751             return 0;
752         }
753     }
754     
755     /**
756      *
757      */

758     private static String JavaDoc convert(String JavaDoc text) {
759         StringBuffer JavaDoc converted = new StringBuffer JavaDoc(text.length());
760         for (int i = 0; i < text.length(); i++) {
761             char c = text.charAt(i);
762             if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
763                     ((c >= '0') && (c <= '9'))) {
764                 converted.append(c);
765             }
766             else {
767                 converted.append('%');
768                 converted.append(Integer.toHexString((int) c));
769             }
770         }
771         return converted.toString();
772     }
773     
774     /**
775      *
776      */

777     private static String JavaDoc convertHTML(String JavaDoc comment) {
778         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(comment.length());
779         for (int i = 0; i < comment.length(); i++) {
780             if (comment.startsWith("&lt;P>", i)) {
781                 sb.append("\n\n"); i += 5;
782             }
783             else if (comment.startsWith("&lt;BR>", i)) {
784                 sb.append("\n"); i += 6;
785             }
786             else sb.append(comment.charAt(i));
787         }
788         return sb.toString();
789     }
790     
791     /**
792      *
793      */

794     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
795         new FrameLauncher("Thinlet Amazon Explorer", new AmazonExplorer(), 208, 256); // 208x235
796
}
797 }
798
Popular Tags