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 13 public class AmazonExplorer extends Thinlet { 14 15 private static final String [] 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 [] BOOKS = { 19 "+pmrank", "+salesrank", "+reviewrank", "+pricerank", "+inverse-pricerank", 20 "+daterank", "+titlerank", "-titlerank" }; 21 private static final String [] CLASSICAL_MUSIC = { 22 "+psrank", "+salesrank", "+artistrank", "+orig-rel-date", "+titlerank" }; 23 private static final String [] DVD = { 24 "+salesrank", "+titlerank" }; 25 private static final String [] ELECTRONICS = { 26 "+pmrank", "+salesrank", "+titlerank", "+reviewrank" }; 27 private static final String [] GARDEN_TOOL = { 28 "+psrank", "+salesrank", "+titlerank", "-titlerank", 29 "+manufactrank", "-manufactrank", "+price", "-price" }; 30 private static final String [] KITCHEN = { 31 "+pmrank", "+salesrank", "+titlerank", "-titlerank", 32 "+manufactrank", "-manufactrank", "+price", "-price" }; 33 private static final String [] HARDWARE = { 34 "+psrank", "+salesrank", "+titlerank", "-titlerank" }; 35 private static final String [] PHOTO = { 36 "+pmrank", "+salesrank", "+titlerank", "-titlerank" }; 37 private static final String [] SOFTWARE = { 38 "+pmrank", "+salesrank", "+titlerank", "+price", "-price" }; 39 private static final String [] VHS = { 40 "+psrank", "+salesrank", "+titlerank" }; 41 private static final String [] VIDEOGAMES = { 42 "+pmrank", "+salesrank", "+titlerank", "+price", "-price" }; 43 private static final String [] 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 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 exc) { exc.printStackTrace(); } 62 } 63 64 67 public void selectMode(int selected, Object searchby, Object keyword, Object sort) { 68 String 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 [] 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 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 109 private void addChoice(Object combobox, String text) { 110 Object choice = create("choice"); 111 setString(choice, "text", text); 112 add(combobox, choice); 113 } 114 115 118 private static final String [] getSort(String 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 return null; 132 } 133 134 137 public void find(int mode, String searchby, Object keyfield, String keyword, int sort) { 138 String searchstring = searchby + "Search=" + convert(keyword) + 139 "&mode=" + MODES[mode] + "&type=lite" + 140 ((sort == -1) ? "" : ("&sort=" + getSort(MODES[mode])[sort])); 141 142 boolean cacheditem = false; int n = getCount(keyfield); 144 if (keyword.length() > 0) { 145 for (int i = n - 1; i >= 0; i--) { 146 String choicetext = getString(getItem(keyfield, i), "text"); 147 if (keyword.equals(choicetext)) { cacheditem = true; break; } 148 } 149 if (!cacheditem) { 150 Object 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 nProductInfo = getResponse(searchstring + "&page=1"); 158 if (!isErrorMessage(nProductInfo)) { 159 try { 160 Object result = parse("result.xml"); 161 Object 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 exc) { exc.printStackTrace(); } 166 } 167 } 168 169 172 public void resultSelected(Object resultlist, Object detailsbutton) { 173 setBoolean(detailsbutton, "enabled", getSelectedIndex(resultlist) != -1); 174 } 175 176 179 public void findNext(Object nextresult, Object total, Object resultlist) { 180 String searchstring = (String ) getProperty(nextresult, "SearchString"); 181 int page = ((Integer ) getProperty(nextresult, "SearchPage")).intValue() + 1; 182 183 Object nProductInfo = getResponse(searchstring + "&page=" + page); 184 if (!isErrorMessage(nProductInfo)) { 185 loadList(nProductInfo, page, resultlist, total, nextresult); 186 } 187 } 188 189 192 private void loadList(Object nProductInfo, int page, Object resultlist, Object total, Object nextresult) { 193 loadDetailsList(nProductInfo, resultlist); 194 195 String sTotalResults = getDOMText(nProductInfo, "TotalResults"); 196 setString(total, "text", sTotalResults); 197 198 putProperty(nextresult, "SearchPage", new Integer (page)); 199 setBoolean(nextresult, "enabled", Integer.parseInt(sTotalResults) > getCount(resultlist)); 200 } 201 202 205 private void loadDetailsList(Object nProductInfo, Object resultlist) { 206 int n = getDOMCount(nProductInfo, "Details"); 207 for (int i = 0; i < n; i++) { 208 Object nDetails = getDOMNode(nProductInfo, "Details", i); 209 Object item = create("item"); 210 setString(item, "text", getDOMText(nDetails, "ProductName")); 211 putProperty(item, "Details", nDetails); 212 add(resultlist, item); 213 } 214 } 215 216 219 public void showDetails(Object resultlist) { 220 Object item = getSelectedItem(resultlist); 221 Object nDetails = getProperty(item, "Details"); 222 223 try { 224 Object details = parse("details.xml"); 225 putProperty(details, "Asin", getDOMText(nDetails, "Asin")); 226 227 Object productname = find(details, "productname"); 229 setString(productname, "text", getDOMText(nDetails, "ProductName")); 230 232 Object 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 exc) { exc.printStackTrace(); } 245 } 246 247 250 public void checkDetails(Object details, int selectedtab) { 251 if (selectedtab == 0) { return; } String sAsin = (String ) getProperty(details, "Asin"); 253 if (sAsin != null) { 254 String searchstring = "AsinSearch=" + sAsin + "&type=heavy"; 255 Object nProductInfo = getResponse(searchstring); 256 if (!isErrorMessage(nProductInfo)) { 257 Object nDetails = getDOMNode(nProductInfo, "Details", 0); 258 if (nDetails == null) { return; } putProperty(details, "Asin", null); 260 261 Object 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 Object nReviews = getDOMNode(nDetails, "Reviews", 0); 277 if (nReviews != null) { 278 Object reviewpanel = find(details, "reviewpanel"); 279 String sAvgCustomerRating = getDOMText(nReviews, "AvgCustomerRating"); 280 Object 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 Object 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 Object 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 nBrowseList = getDOMNode(nDetails, "BrowseList", 0); 303 if (nBrowseList != null) { 304 int n = getDOMCount(nBrowseList, "BrowseNode"); 305 Object browselist = find(linkpanel, "browselist"); 306 for (int i = 0; i < n; i++) { 307 Object nBrowseNode = getDOMNode(nBrowseList, "BrowseNode", i); 308 Object browseitem = create("item"); 309 setString(browseitem, "text", getDOMText(getDOMNode(nBrowseNode, "BrowseName", 0))); 310 add(browselist, browseitem); 311 } 312 } 313 } 314 } 315 } 316 317 320 private void loadLink(Object button, Object nDetails, String listname) { 321 Object list = getDOMNode(nDetails, listname, 0); 322 setBoolean(button, "enabled", (list != null)); 323 if (list != null) { 324 putProperty(button, listname, list); 325 } 326 } 327 328 331 public void previousReview(Object reviewpanel) { 332 int i = ((Integer ) getProperty(reviewpanel, "ReviewIndex")).intValue(); 333 loadReview(reviewpanel, getProperty(reviewpanel, "Reviews"), i - 1); 334 } 335 336 339 public void nextReview(Object reviewpanel) { 340 int i = ((Integer ) getProperty(reviewpanel, "ReviewIndex")).intValue(); 341 loadReview(reviewpanel, getProperty(reviewpanel, "Reviews"), i + 1); 342 } 343 344 347 private void loadReview(Object reviewpanel, Object 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 (i)); 354 355 Object nCustomerReview = getDOMNode(nReviews, "CustomerReview", i); 356 String sRating = getDOMText(nCustomerReview, "Rating"); 357 Object 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 370 private void loadList(Object root, String nodename, Object panel, String titlename, String listname) { 371 Object list = find(panel, listname); 372 if (root != null) { 373 int n = getDOMCount(root, nodename); 374 for (int i = 0; i < n; i++) { 375 Object item = create("item"); 376 setString(item, "text", getDOMText(getDOMNode(root, nodename, i))); 377 add(list, item); 378 } 379 } 380 else { 381 Object title = find(panel, titlename); 382 setBoolean(title, "visible", false); 383 setBoolean(list, "visible", false); 384 } 385 } 386 387 390 public void showAccessories(Object button) { 391 showLinkList(getProperty(button, "Accessories"), "Accessory", "Accessories:"); 392 } 393 394 397 public void showSimilars(Object button) { 398 showLinkList(getProperty(button, "SimilarProducts"), "Product", "Similar products:"); 399 } 400 401 404 private void showLinkList(Object nRoot, String leafname, String title) { 405 StringBuffer asins = new StringBuffer (); 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 nProductInfo = getResponse("ListManiaSearch=" + asins.toString() + "&type=lite"); 413 if (!isErrorMessage(nProductInfo)) { 414 try { 415 Object linklist = parse("linklist.xml"); 416 setString(find(linklist, "title"), "text", title); 417 loadDetailsList(nProductInfo, find(linklist, "resultlist")); 418 addPage(linklist); 419 } catch (Exception exc) { exc.printStackTrace(); } 420 } 421 } 422 423 426 public void showWishlists(Object button) { 427 try { 428 Object listmania = parse("listmania.xml"); 429 Object 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 exc) { exc.printStackTrace(); } 436 } 437 438 441 public void previousWishlist(Object listmania, 442 Object resultlist, Object listname, Object prevlist, Object nextlist) { 443 int index = ((Integer ) getProperty(listmania, "ListsIndex")).intValue(); 444 loadWishlist(getProperty(listmania, "Lists"), index - 1, 445 listmania, resultlist, listname, prevlist, nextlist); 446 } 447 448 451 public void nextWishlist(Object listmania, 452 Object resultlist, Object listname, Object prevlist, Object nextlist) { 453 int index = ((Integer ) getProperty(listmania, "ListsIndex")).intValue(); 454 loadWishlist(getProperty(listmania, "Lists"), index + 1, 455 listmania, resultlist, listname, prevlist, nextlist); 456 } 457 458 461 private void loadWishlist(Object nLists, int index, Object listmania, 462 Object resultlist, Object listname, Object prevlist, Object nextlist) { 463 int n = getDOMCount(nLists, "ListId"); 464 if ((index >= 0) && (index < n)) { 465 String sListId = getDOMText(getDOMNode(nLists, "ListId", index)); 466 Object 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 (index)); 472 setBoolean(prevlist, "enabled", index > 0); 473 setBoolean(nextlist, "enabled", index < n - 1); 474 } 475 } 476 } 477 478 481 public void searchMarket(Object button, Object offertype) { 482 String asin = (String ) getProperty(button, "Asin"); 483 String type = getString(getItem(offertype, getInteger(offertype, "selected")), "text"); 484 485 String searchstring = "AsinSearch=" + asin + "&type=heavy&offer=" + type; 486 Object nProductInfo = getResponse(searchstring + "&offerpage=1"); 487 if (!isErrorMessage(nProductInfo)) { 488 try { 489 Object market = parse("market.xml"); 490 491 Object nDetails = getDOMNode(nProductInfo, "Details", 0); 492 if (nDetails == null) { return; } 493 494 String offerings = getDOMText(nDetails, "NumberOfOfferings"); 495 setString(find(market, "offerings"), "text", offerings); 496 497 Object nInfo = getDOMNode(nDetails, "ThirdPartyProductInfo", 0); 498 Object detailslist = find(market, "detailslist"); 499 int n = getDOMCount(nInfo, "ThirdPartyProductDetails"); 500 for (int i = 0; i < n; i++) { 501 Object nProduct = getDOMNode(nInfo, "ThirdPartyProductDetails", i); 502 Object row = create("row"); 503 putProperty(row, "ThirdPartyProductDetails", nProduct); 504 Object cellNick = create("cell"); 505 setString(cellNick, "text", getDOMText(nProduct, "SellerNickname")); 506 add(row, cellNick); 507 Object 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 exc) { exc.printStackTrace(); } 515 } 516 } 517 518 521 public void showMarketDetails(Object detailslist) { 522 Object nProduct = getProperty(getSelectedItem(detailslist), "ThirdPartyProductDetails"); 523 try { 524 Object exchange = parse("exchange.xml"); 525 Object 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 exc) { exc.printStackTrace(); } 539 } 540 541 544 private void addLabel(Object panel, String title, Object root, String key) { 545 String value = getDOMText(root, key); 546 if (value != null) { 547 Object label = create("label"); 548 setString(label, "text", title); 549 add(panel, label); 550 Object field = create("label"); 551 setString(field, "text", value); 552 setInteger(field, "weightx", 1); 553 add(panel, field); 554 } 555 } 556 557 560 private void addField(Object panel, String title, Object root, String key) { 561 String value = getDOMText(root, key); 562 if (value != null) { 563 Object label = create("label"); 564 setString(label, "text", title); 565 add(panel, label); 566 Object 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 577 private void addList(Object panel, String title, Object root, String key, String itemkey) { 578 Object 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 label = create("label"); 583 if (i == 0) { setString(label, "text", title); } 584 add(panel, label); 585 586 Object 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 595 598 public void exit() { 599 System.exit(0); 600 } 601 602 605 public void defaultTheme() { 606 setColors(0xe6e6e6, 0x000000, 0xffffff, 607 0x909090, 0xb0b0b0, 0xededed, 0xb9b9b9, 0x89899a, 0xc5c5dd); 608 } 609 610 613 public void yellowTheme() { 614 setColors(0xeeeecc, 0x000000, 0xffffff, 615 0x999966, 0xb0b096, 0xededcb, 0xcccc99, 0xcc6600, 0xffcc66); 616 } 617 618 621 public void blueTheme() { 622 setColors(0x6375d6, 0xffffff, 0x7f8fdd, 623 0xd6dff5, 0x9caae5, 0x666666, 0x003399, 0xff3333, 0x666666); 624 } 625 626 629 public void about() { 630 try { 631 add(parse("about.xml")); 632 } catch (Exception exc) { exc.printStackTrace(); } 633 } 634 635 637 640 private Object getResponse(String path) { 641 String associatestag = "thinlet-20"; 642 String developertoken = "D3MAIAYX2Q6JLY"; 643 String url = "http://xml.amazon.com/onca/xml2?t=" + associatestag + 644 "&dev-t=" + developertoken + "&" + path + "&f=xml"; 645 if (getParent() instanceof Applet) { 646 String 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 exc) { 654 showException(exc); 655 return null; 656 } 657 } 658 659 662 private void addPage(Object page) { 663 Object mainpanel = find("mainpanel"); 664 int n = getCount(mainpanel); 665 setBoolean(getItem(mainpanel, n - 1), "visible", false); 666 add(mainpanel, page); 667 } 668 669 672 public void removePage() { 673 Object 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 682 protected void handleException(Throwable throwable) { 683 showException(throwable); 684 } 685 686 689 private void showException(Throwable thr) { 690 StringWriter writer = new StringWriter(); 691 thr.printStackTrace(new PrintWriter(writer)); 692 String trace = writer.toString().replace('\r', ' ').replace('\t', ' '); 693 String thrclass = thr.getClass().getName(); 694 thrclass = thrclass.substring(thrclass.lastIndexOf('.') + 1); 695 696 try { 697 Object 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 exc) { exc.printStackTrace(); } 703 } 704 705 708 private boolean isErrorMessage(Object nProductInfo) { 709 if (nProductInfo == null) { return true; } 710 String sErrorMsg = getDOMText(nProductInfo, "ErrorMsg"); 711 if (sErrorMsg == null) { return false; } 712 try { 713 Object exceptiondialog = parse("error.xml"); 714 setString(find(exceptiondialog, "message"), "text", sErrorMsg); 715 add(exceptiondialog); 716 } catch (Exception exc) { exc.printStackTrace(); } 717 return true; 718 } 719 720 723 public void closeDialog(Object dialog) { 724 remove(dialog); 725 } 726 727 729 732 private String getDOMText(Object node, String key) { 733 Object leaf = getDOMNode(node, key, 0); 734 return (leaf != null) ? getDOMText(leaf) : null; 735 } 736 737 740 private static int getNumber(String 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 exc) { 751 return 0; 752 } 753 } 754 755 758 private static String convert(String text) { 759 StringBuffer converted = new StringBuffer (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 777 private static String convertHTML(String comment) { 778 StringBuffer sb = new StringBuffer (comment.length()); 779 for (int i = 0; i < comment.length(); i++) { 780 if (comment.startsWith("<P>", i)) { 781 sb.append("\n\n"); i += 5; 782 } 783 else if (comment.startsWith("<BR>", i)) { 784 sb.append("\n"); i += 6; 785 } 786 else sb.append(comment.charAt(i)); 787 } 788 return sb.toString(); 789 } 790 791 794 public static void main(String [] args) throws Exception { 795 new FrameLauncher("Thinlet Amazon Explorer", new AmazonExplorer(), 208, 256); } 797 } 798 | Popular Tags |