CatalogNavigator.java 53.4 KB
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
/*
 * Copyright 2003 Association for Universities for Research in Astronomy, Inc.,
 * Observatory Control System, Gemini Telescopes Project.
 *
 * $Id: CatalogNavigator.java,v 1.12 2009/04/14 10:56:02 abrighto Exp $
 */

package jsky.catalog.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

import jsky.catalog.Catalog;
import jsky.catalog.CatalogDirectory;
import jsky.catalog.HTMLQueryResultHandler;
import jsky.catalog.QueryResult;
import jsky.catalog.TableQueryResult;
import jsky.catalog.URLQueryResult;
import jsky.catalog.astrocat.AstroCatConfig;
import jsky.util.*;
import jsky.util.SwingWorker;
import jsky.util.gui.DialogUtil;
import jsky.util.gui.GenericToolBarTarget;
import jsky.util.gui.ProgressException;
import jsky.util.gui.ProgressPanel;
import jsky.util.gui.SwingUtil;
import uk.ac.starlink.table.StarTable;

/**
 * Used to navigate the catalog hierarchy. This class displays a tree of catalogs in one panel and the interface for
 * searching the catalog, or the query results in the other panel.
 * <p/>
 * The tree display is based on a top level catalog directory. The details must be defined in a derived class.
 * 
 * ** This class has been imported into Osp Project from Jsky library. 
 */
public abstract class CatalogNavigator extends JPanel
        implements QueryResultDisplay, GenericToolBarTarget, HTMLQueryResultHandler {

    // Used to access internationalized strings (see i18n/gui*.proprties)
    private static final I18N _I18N = I18N.getInstance(CatalogNavigator.class);

//    // True if this is the main application window (enables exit menu item)
//    private static boolean _mainWindowFlag = false;

    // Set to true to query catalogs automatically when selected
    private boolean _autoQuery = false;

    // Displays the catalog tree and the catalog query widgets
    private JPanel _queryPanel;

    // Displays query results, such as tabular data.
    private JPanel _resultPanel;

    // Tree displaying catalog hierarchy
    private CatalogTree _catalogTree;

    // Query panel currently being displayed
    private JComponent _queryComponent;

    // Result panel currently being displayed
    private JComponent _resultComponent;

    // The original URL for the display component's data (for history list)
    private URL _origURL;

    // reuse file chooser widget for open
    private static JFileChooser _fileChooser;

    // reuse file chooser widget for saveAs
    private static JFileChooser _saveFileChooser;

    // Panel used to display download progress information
    private ProgressPanel _progressPanel;

    // list of listeners for change events
    private EventListenerList _listenerList = new EventListenerList();

    // Stack of CatalogHistoryItems, used to go back to a previous panel
    private Stack<CatalogHistoryItem> _backStack = new Stack<CatalogHistoryItem>();

    // Stack of CatalogHistoryItems, used to go forward to the next panel
    private Stack<CatalogHistoryItem> _forwStack = new Stack<CatalogHistoryItem>();

    // Set when the back or forward actions are active to avoid the normal history stack handling
    private boolean _noStack = false;

    // Saved query result (set in background thread)
    private QueryResult _queryResult;

    // Optional object to use to plot table data
    private TablePlotter _plotter;

    // Utility object used to control background thread
    private SwingWorker _worker;

//    // Top level frame for viewing an HTML page
//    private HTMLViewerFrame _htmlViewerFrame;

    // Hash table associating each panel with a tree node
    private Hashtable<JComponent, Catalog> _panelTreeNodeTable = new Hashtable<JComponent, Catalog>(10);

    // Manages a list of previously viewed catalogs or query results.
    private CatalogHistoryList _historyList;

    // Manages a list of settings for stored querries, so that you can repeat the query later on
    private CatalogQueryList _queryList;

    // Maps query components to their corresponding result components
    private Hashtable<JComponent, JComponent> _queryResultComponentMap = new Hashtable<JComponent, JComponent>();

    // Maps table ids and url strings to their corresponding query components
    private Hashtable<String, JComponent> _queryComponentMap = new Hashtable<String, JComponent>();

    // The pane dividing the catalog tree and the query panel
    private JSplitPane _querySplitPane;

    // The pane dividing the query and the results panel
    private JSplitPane _resultSplitPane;

    // Action to use for the "Open..." menu and toolbar items
    private AbstractAction _openAction = new AbstractAction(
            _I18N.getString("open"),
            Resources.getIcon("Open24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("catalogOpenTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            open();
        }
    };

    // Action to use for the "Open URL..." menu and toolbar items
    private AbstractAction _openUrlAction = new AbstractAction(
            _I18N.getString("openURL")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("openURLTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            openURL();
        }
    };

    // Action to use for the "Clear" menu and toolbar items
    private AbstractAction _clearAction = new AbstractAction(
            _I18N.getString("clear")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("clearTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            clear();
        }
    };

    // May be set to override the default _openAction
    private AbstractAction _openActionOverride;

    // Action to use for the "Save as..." menu and toolbar items
    private AbstractAction _saveAsAction = new AbstractAction(
            _I18N.getString("saveAs"),
            Resources.getIcon("SaveAs24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("saveAsTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            saveAs();
        }
    };

    // Action to use for the "Save With Image..." menu and toolbar items
    private AbstractAction _saveWithImageAction = new AbstractAction(
            _I18N.getString("saveCatalogWithImage")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("saveCatalogWithImageTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            saveWithImage();
        }
    };

    // Action to use for the "Save as HTML..." menu and toolbar items
    private AbstractAction _saveAsHTMLAction = new AbstractAction(
            _I18N.getString("saveAsHTML")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("saveAsHTMLTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            saveAsHTML();
        }
    };

    // Action to use for the "Print..." menu and toolbar items
    private AbstractAction _printAction = new AbstractAction(
            _I18N.getString("print") + "...",
            Resources.getIcon("Print24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("printTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            print();
        }
    };

    // Action to use for the "Back" menu and toolbar items
    private AbstractAction _backAction = new AbstractAction(
            _I18N.getString("back"),
            Resources.getIcon("Back24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("catalogBackTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            back();
        }
    };

    // Action to use for the "Forward" menu and toolbar items
    private AbstractAction _forwAction = new AbstractAction(
            _I18N.getString("forward"),
            Resources.getIcon("Forward24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("catalogForwardTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            forward();
        }
    };

    // Action to use for the "Add Row" menu item
    private AbstractAction _addRowAction = new AbstractAction(
            _I18N.getString("addRow")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("addRowTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            addRow();
        }
    };

    // Action to use for the "Delete Rows..." menu item
    private AbstractAction _deleteSelectedRowsAction = new AbstractAction(
            _I18N.getString("deleteSelectedRows")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("deleteSelectedRowsTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            deleteSelectedRows();
        }
    };

    // Action to use for the "Query => Store => New Query..." menu item
    private AbstractAction _storeNewQueryAction = new AbstractAction(
            _I18N.getString("new")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("newQueryTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            storeNewQuery();
        }
    };

    // Action to use for the "Query => Delete => All" menu item
    private AbstractAction _deleteAllQueryAction = new AbstractAction(
            _I18N.getString("all")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("deleteAllQueryTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            clearQueryList();
        }
    };

    // Action to use for the "File => Close" menu item
    private AbstractAction _closeAction = new AbstractAction(
            _I18N.getString("close")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("closeTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            close();
        }
    };

    // Action to use for the "File => Exit" menu item
    private AbstractAction _exitAction = new AbstractAction(
            _I18N.getString("exit")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("exitTip"));
        }

        @Override
		public void actionPerformed(ActionEvent evt) {
            exit();
        }
    };

    /**
     * Returns the top level catalog directory to use The contents of this are determined by the AstroCat.xml file found
     * either under ~/.jsky3 or as a resource (jsky-catalog/src/main/resources).
     *
     * @param load if true, load the catalog directory (which can be slow) if not already loaded, otherwise an empty
     *             catalog directory is returned if not already initialized. This is just a placeholder that a listener
     *             can be added to, to be notified later when the directory has been initialized.
     * @return the top level catalog directory
     */
    public static CatalogDirectory getCatalogDirectory(boolean load) {
        return AstroCatConfig.getConfigFile(load);
    }

    /**
     * Returns the top level catalog directory to use The contents of this are determined by the AstroCat.xml file found
     * either under ~/.jsky3 or as a resource (jsky-catalog/src/main/resources).
     *
     * @return the top level catalog directory
     */
    public static CatalogDirectory getCatalogDirectory() {
        return AstroCatConfig.getConfigFile();
    }


    /**
     * Construct a CatalogNavigator using the given CatalogTree widget (Call setQueryResult to set the root catalog to
     * display).
     *
     * @param catalogTree a CatalogTree (normally a subclass of CatalogTree that knows about certain types of catalogs)
     */
    public CatalogNavigator(CatalogTree catalogTree) {
        setLayout(new BorderLayout());

        _catalogTree = catalogTree;
        catalogTree.setQueryResultDisplay(this);
        catalogTree.setHTMLQueryResultHandler(this);
        catalogTree.setPreferredSize(new Dimension(300, 0));

        // Turn autoQuery off when the user selects the catalog in the tree
        // (Its turned on when selected from the main menu)
        catalogTree.getTree().getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            @Override
			public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
                _autoQuery = false;
            }
        });

        _queryPanel = new JPanel();
        _queryPanel.setLayout(new BorderLayout());

        _resultPanel = new JPanel();
        _resultPanel.setLayout(new BorderLayout());

        _querySplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, catalogTree, _queryPanel);
        _querySplitPane.setOneTouchExpandable(false);
        _querySplitPane.setDividerLocation(385);
        _querySplitPane.setBorder(null);

        _resultSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, _querySplitPane, _resultPanel);
        _resultSplitPane.setOneTouchExpandable(false);
        _resultSplitPane.setDividerLocation(320);
        _resultSplitPane.setBorder(null);
        add(_resultSplitPane, BorderLayout.CENTER);

        _queryList = new CatalogQueryList();
        _historyList = new CatalogHistoryList();
        setQueryComponent(new EmptyPanel());
        setResultComponent(new EmptyPanel());
    }

    /**
     * Construct a CatalogNavigator using the given CatalogTree widget and TablePlotter (Call setQueryResult to set the
     * root catalog to display).
     *
     * @param catalogTree a CatalogTree (normally a subclass of CatalogTree that knows about certain types of catalogs)
     * @param plotter     the object to use to plot catalog table data (when the plot button is pressed)
     */
    public CatalogNavigator(CatalogTree catalogTree, TablePlotter plotter) {
        this(catalogTree);
        _plotter = plotter;
    }

    /**
     * @return the object displaying the catalog tree
     */
    public CatalogTree getCatalogTree() {
        return _catalogTree;
    }

    /**
     * @return the pane dividing the catalog tree and the query panel
     */
    protected JSplitPane getQuerySplitPane() {
        return _querySplitPane;
    }

    /**
     * @return the pane dividing the query and the results panel
     */
    public JSplitPane getResultSplitPane() {
        return _resultSplitPane;
    }

    /**
     * @param b Set to true to query catalogs automatically when selected
     */
    public void setAutoQuery(boolean b) {
        _autoQuery = b;
    }

    /**
     * @return the object used to plot table data, or null if none was defined.
     */
    public TablePlotter getPlotter() {
        return _plotter;
    }

    /**
     * @param tp the object used to plot table data.
     */
    public void setPlotter(TablePlotter tp) {
        _plotter = tp;
    }

    /**
     * Set the query or result component to display. The choice is made based on which interfaces the component
     * implements. If the component implements QueryResultDisplay, it is considered a result component.
     *
     * @param component the query or result component to display
     */
    public void setComponent(JComponent component) {
        if (component instanceof QueryResultDisplay) {
            setResultComponent(component);
        } else {
            setQueryComponent(component);

            //System.out.println("XXX _autoQuery = " + _autoQuery  + ", component is a " + component.getClass());

            if ((component instanceof CatalogQueryTool)
                    && (_autoQuery || ((CatalogQueryTool) component).getCatalog().isLocal())) {
                ((CatalogQueryTool) component).search();
            }
        }
    }

    /**
     * @param component the query component to display
     */
    public void setQueryComponent(JComponent component) {
        if (component == null || component == _queryComponent) {
            return;
        }
        if (_queryComponent != null) {
            addToHistory();
            _queryPanel.remove(_queryComponent);
            _queryComponent = null;
        }
        _queryComponent = component;
        Catalog cat = _catalogTree.getSelectedCatalog();
        if (cat != null) {
            _panelTreeNodeTable.put(_queryComponent, cat);
        }
        _queryPanel.add(_queryComponent, BorderLayout.CENTER);

        // restore the query result corresponding to this catalog, if known
        Object resultComp = _queryResultComponentMap.get(_queryComponent);
        if (resultComp == null) {
            setResultComponent(new EmptyPanel());
        } else {
            setResultComponent((JComponent) resultComp);
        }
        update();
    }

    /**
     * @return the panel currently being displayed
     */
    public JComponent getQueryComponent() {
        return _queryComponent;
    }

    /**
     * @param component the result component to display
     */
    public void setResultComponent(JComponent component) {
        if (component == null || component == _resultComponent) {
            return;
        }
        if (_resultComponent != null) {
//            if (_resultComponent instanceof TableDisplayTool) {
            // if we're not reusing the current table window, tell it to hide any related popup
            // windows before replacing it (It might be needed again later though, if the user
            // goes back to it).
            //((TableDisplayTool)_resultComponent).hidePopups();
//            }
            _resultPanel.remove(_resultComponent);
            _resultComponent = null;
        }
        _resultComponent = component;
        if (_queryComponent != null) {
            _queryResultComponentMap.put(_queryComponent, _resultComponent);
        }
        _resultPanel.add(_resultComponent, BorderLayout.CENTER);
        update();
        _resultComponentChanged();

        // try to display the right amount of the query window
//        SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//                _resultSplitPane.resetToPreferredSizes();
//            }
//        });
    }

    /**
     * @return the panel currently being displayed
     */
    public JComponent getResultComponent() {
        return _resultComponent;
    }

    /**
     * Called whenever the display component is changed
     */
    protected void _resultComponentChanged() {
        // set the state of the "Save As..." menu item
        _saveAsAction.setEnabled(_resultComponent instanceof Saveable);
        _printAction.setEnabled(_resultComponent instanceof PrintableWithDialog);
        boolean isTable = (_resultComponent instanceof TableDisplayTool);
        _saveWithImageAction.setEnabled(isTable);
        _deleteSelectedRowsAction.setEnabled(isTable);
        _addRowAction.setEnabled(isTable);
        fireChange(new ChangeEvent(this));
    }

    /**
     * Register to receive change events from this object whenever a new query result is displayed.
     *
     * @param l the listener
     */
    public void addChangeListener(ChangeListener l) {
        _listenerList.add(ChangeListener.class, l);
    }

    /**
     * Stop receiving change events from this object.
     *
     * @param l the listener
     */
    public void removeChangeListener(ChangeListener l) {
        _listenerList.remove(ChangeListener.class, l);
    }

    /**
     * Notify any listeners that a new query result is being displayed.
     *
     * @param e the event
     */
    protected void fireChange(ChangeEvent e) {
        Object[] listeners = _listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == ChangeListener.class) {
                ((ChangeListener) listeners[i + 1]).stateChanged(e);
            }
        }
    }

    /**
     * Add the current catalog to the history stack, removing duplicates.
     */
    protected void addToHistory() {
        if (_queryComponent == null) {
            return;
        }
        CatalogHistoryItem historyItem = makeCatalogHistoryItem();
        if (historyItem == null) {
            return;
        }
        if (!_noStack) {
            _backStack.push(historyItem);
            _backAction.setEnabled(true);
            if (_forwStack.size() != 0) {
                _cleanupHistoryStack(_forwStack);
                _forwStack.clear();
                _forwAction.setEnabled(false);
            }
        }
        _historyList.add(historyItem);
    }

    /**
     * @return a new CatalogHistoryItem for the currently displayed catalog.
     */
    protected CatalogHistoryItem makeCatalogHistoryItem() {
        String s = _queryComponent.getName();
        if (s != null) {
            return new CatalogHistoryItem(s, _origURL, _queryComponent);
        }
        return null;
    }

    /**
     * Add history items (for previously displayed components) to the given menu
     *
     * @param menu the menu to add the items to
     */
    public void addHistoryMenuItems(JMenu menu) {
        Iterator<CatalogHistoryItem> it = _historyList.iterator();
        while (it.hasNext()) {
            menu.add(it.next());
        }
    }

    // This method may be redefined in subclasses to do cleanup work before components are
    // removed from the given history stack (_backStack or _forwStack).
    private void _cleanupHistoryStack(Stack stack) {
        unplot(stack);
    }

    /**
     * Clear out the history and back/forward stacks
     */
    protected void clearHistory() {
        _backAction.setEnabled(false);
        _backStack.clear();
        _forwAction.setEnabled(false);
        _forwStack.clear();
        _historyList.clear();
    }

    /**
     * Set the original URL for the current catalog or table.
     *
     * @param url the URL of the catalog, table or FITS file
     */
    public void setOrigURL(URL url) {
        _origURL = url;
    }

    /**
     * Returns the original URL for the current catalog or table.
     *
     * @return the URL of the catalog, table or FITS file
     */
    public URL getOrigURL() {
        return _origURL;
    }

    /**
     * Remove any plot symbols or graphics managed by any of the display components in the given stack
     *
     * @param stack history stack
     */
    protected void unplot(Stack stack) {
        // Unplot any catalog symbols before loosing the information
        int n = stack.size();
        for (int i = 0; i < n; i++) {
            CatalogHistoryItem item = (CatalogHistoryItem) (stack.get(i));
            Object resultComp = _queryResultComponentMap.get(item.getQueryComponent());
            if (resultComp instanceof TableDisplayTool) {
                ((TableDisplayTool) resultComp).unplot();
            }
        }
    }

    /**
     * Remove any plot symbols or graphics managed by any of the display components
     */
    public void unplot() {
        Enumeration e = _queryResultComponentMap.elements();
        while (e.hasMoreElements()) {
            JComponent comp = (JComponent) e.nextElement();
            if (comp instanceof TableDisplayTool) {
                ((TableDisplayTool) comp).unplot();
            }
        }
    }

    /**
     * Update the layout after a new component has been inserted
     */
    protected void update() {
        _queryPanel.revalidate();
        _resultPanel.revalidate();
        JFrame parent = SwingUtil.getFrame(this);
        if (parent != null) {
            parent.repaint();
        }
    }

    /**
     * Select the node in the catalog directory tree corresponding to the current display component
     */
    protected void updateTreeSelection() {
        if (_queryComponent instanceof CatalogQueryTool) {
            _catalogTree.setSelectedCatalog(((CatalogQueryTool) _queryComponent).getCatalog(), true);
            _updateTitle(((CatalogQueryTool) _queryComponent).getCatalog());
        } else if (_queryComponent instanceof TableDisplayTool) {
            _catalogTree.setSelectedCatalog(((TableDisplayTool) _queryComponent).getTable(), true);
        }
    }

    public QueryResult getQueryResult() {
        return _queryResult;
    }

    /**
     * Display the given query result.
     */
    @Override
	public void setQueryResult(QueryResult queryResult) {
        if (queryResult == null) {
            return;
        }
        if (_worker != null) {
            // shouldn't happen if user interface disables it
            DialogUtil.error(_I18N.getString("queryInProgress"));
            return;
        }

        _queryResult = queryResult;

        // Use a background thread for remote catalog access, local catalogs are handled in this thread
        boolean isLocal = true;
        if (queryResult instanceof URLQueryResult) {
            URLQueryResult uqr = (URLQueryResult) queryResult;
            URL url = uqr.getURL();
            isLocal = (url.getProtocol().equals("file"));
        } else if (queryResult instanceof Catalog) {
            isLocal = ((Catalog) queryResult).isLocal();
        }
        if (isLocal) {
            setComponent(makeQueryResultComponent(queryResult));
        } else {
            // remote catalog: run in a separate thread, so the user can monitor progress
            makeProgressPanel();
            _worker = new SwingWorker() {
                @Override
				public Object construct() {
                    try {
                        return makeQueryResultComponent(_queryResult);
                    } catch (Exception e) {
                        return e;
                    }
                }

                @Override
				public void finished() {
                    _worker = null;
                    _progressPanel.stop();
                    Object o = getValue();
                    if (o instanceof Exception) {
                        DialogUtil.error((Exception) o);
                    } else if (o instanceof JComponent) {
                        setComponent((JComponent) o);
                    }
                }
            };
            _worker.start();
        }
    }

    /**
     * Update the frame's title to display the name of the given catalog
     *
     * @param catalog the given catalog
     */
    private void _updateTitle(Catalog catalog) {
        String title = _I18N.getString("catalogNavigator");
        String s = catalog.getTitle();
        if (s != null && s.length() > 0) {
            title += " - " + s;
        }
        JFrame parent = SwingUtil.getFrame(this);
        if (parent != null) {
            parent.setTitle(title);
        }
    }

    /**
     * If it does not already exist, make the panel used to display the progress of network access.
     */
    protected void makeProgressPanel() {
        if (_progressPanel == null) {
            JFrame parent = SwingUtil.getFrame(this);
            _progressPanel = ProgressPanel.makeProgressPanel(_I18N.getString("accessingCatalogServer"), parent);
            _progressPanel.addActionListener(new ActionListener() {
                @Override
				public void actionPerformed(ActionEvent e) {
                    if (_worker != null) {
                        _worker.interrupt();
                        _worker = null;
                    }
                }
            });
        }
    }

    /**
     * Create and return a JComponent displaying the given query result.
     *
     * @param queryResult result of a catalog query
     * @return a JComponent displaying the given query result
     */
    protected JComponent makeQueryResultComponent(QueryResult queryResult) {
        _origURL = null;
        try {
            // See if there is a user interface handler for the query result
            if (queryResult instanceof CatalogUIHandler) {
                JComponent c = ((CatalogUIHandler) queryResult).makeComponent(this);
                if (c != null) {
                    return c;
                }
            }

            // No UI handler, return the default component for the query result
            if (queryResult instanceof CatalogDirectory) {
                return makeCatalogDirectoryComponent((CatalogDirectory) queryResult);
            }
            if (queryResult instanceof TableQueryResult) {
                return makeTableQueryResultComponent((TableQueryResult) queryResult);
            }
            if (queryResult instanceof Catalog) {
                return makeCatalogComponent((Catalog) queryResult);
            }
            if (queryResult instanceof URLQueryResult) {
                return makeURLComponent((URLQueryResult) queryResult);
            }
        } catch (Exception e) {
            if (_progressPanel != null) {
                _progressPanel.stop();
            }
            DialogUtil.error(e);
        }
        return new EmptyPanel();
    }

    /**
     * @param catalogDirectory a catalog directory
     * @return a new JComponent displaying the contents of the given catalog directory
     */
    protected JComponent makeCatalogDirectoryComponent(CatalogDirectory catalogDirectory) {
        // get the number of catalogs in the directory
        int numCatalogs = catalogDirectory.getNumCatalogs();
        if (numCatalogs == 0) {
            return makeCatalogComponent(catalogDirectory);
        }
        if (numCatalogs == 1) {
            return makeCatalogComponent(catalogDirectory.getCatalog(0));
        }
        return new EmptyPanel();
    }

    /**
     * Return a new JComponent displaying the contents of the given table query result.
     *
     * @param tableQueryResult a table as the result of a query
     * @return a component displaying the table
     */
    protected JComponent makeTableQueryResultComponent(TableQueryResult tableQueryResult) {
        if (_resultComponent instanceof TableDisplayTool) {
            TableDisplayTool tdt = (TableDisplayTool) _resultComponent;
            if (tdt.getTable().getName().equals(tableQueryResult.getName())) {
                tdt.setQueryResult(tableQueryResult);
                return tdt;
            }
        }
        TableDisplayTool t = new TableDisplayTool(tableQueryResult, this, _plotter);

        // add a popup menu to the table
        makeTablePopupMenu(t);
        return t;
    }

    /**
     * Add a popup menu to the given TableDisplayTool
     *
     * @param t component for displaying tables
     */
    protected void makeTablePopupMenu(TableDisplayTool t) {
        final JPopupMenu m = new JPopupMenu();
        m.add(_addRowAction);
        m.add(_deleteSelectedRowsAction);
        t.getTableDisplay().getTable().addMouseListener(new MouseAdapter() {
            @Override
			public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    m.show(e.getComponent(), e.getX(), e.getY());
                }
            }

            @Override
			public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    m.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
    }

    /**
     * @param catalog a catalog
     * @return a new JComponent displaying the contents of (or the interface for searching) the given catalog
     */
    protected JComponent makeCatalogComponent(Catalog catalog) {
        // catalog may contain multiple tables and implement the CatalogDirectory interface
        if (catalog instanceof CatalogDirectory) {
            CatalogDirectory catalogDirectory = (CatalogDirectory) catalog;
            int numCatalogs = catalogDirectory.getNumCatalogs();
            if (numCatalogs == 1) {
                Catalog c = catalogDirectory.getCatalog(0);
                if (c instanceof TableQueryResult) {
                    return makeTableQueryResultComponent((TableQueryResult) c);
                } else {
                    DialogUtil.error(_I18N.getString("subCatalogError") + ": " + c);
                    return new EmptyPanel();
                }
            } else if (numCatalogs > 1) {
                return makeTableQueryResultComponent(catalogDirectory.getCatalogList());
            }
        }
        if (catalog instanceof TableQueryResult) {
            return makeTableQueryResultComponent((TableQueryResult) catalog);
        }

        // Default to normal catalog query component
        return makeCatalogQueryTool(catalog);
    }

    /**
     * Make a panel for querying a catalog
     *
     * @param catalog the catalog
     * @return the query panel to display
     */
    protected CatalogQueryTool makeCatalogQueryTool(Catalog catalog) {
        return new CatalogQueryTool(catalog, this);
    }

    /**
     * Makes a component to display the contents of a URL
     *
     * @param queryResult contains the URL are related inforation
     * @return a new JComponent displaying the contents of the URL.
     * @throws java.io.IOException if the URL can't be read
     */
    protected JComponent makeURLComponent(URLQueryResult queryResult) throws IOException {
        try {
            URL url = queryResult.getURL();
            URLConnection connection;
            if (url.getProtocol().equals("file")) {
                connection = url.openConnection();
            } else {
                connection = _progressPanel.openConnection(url);
            }
            if (connection == null) {
                return _queryComponent;
            }
            String format = queryResult.getFormat();
            String contentType = connection.getContentType();
            if (contentType == null) {
                contentType = "unknown";
            }
            return makeURLComponent(url, contentType, format);
        } catch (ProgressException e) {
            // ignore: user pressed the stop button in the progress panel
        } catch (FileNotFoundException e) {
            DialogUtil.error(_I18N.getString("fileNotFound", e.getMessage()));
        } catch (Exception e) {
            DialogUtil.error(e);
        }
        if (_resultComponent != null) {
            return _resultComponent;
        }
        return new EmptyPanel();
    }

    /**
     * @param url         the URL to read
     * @param contentType the content type from the http server
     * @param format      a format string from the table row, or null if not defined
     * @return a new JComponent displaying the contents of the given URL.
     * @throws java.io.IOException on read error
     */
    protected JComponent makeURLComponent(URL url, String contentType, String format) throws IOException {
        String filename = url.getFile();

        if (contentType.startsWith("text/plain")) {
            displayPlainText(url);
            return _resultComponent;
        }
        
        if (contentType.startsWith("text/")) {
            // assume HTML or something that a browser can display...
            displayHTMLPage(url);
            return _resultComponent;
        }

        // If it is not one of the known content types, call a method that may be
        // redefined in a derived class to handle that type
        return makeUnknownURLComponent(url, contentType);
    }


//    /*
//     * Attempt to show a URL in the default web browser and return true if successful.
//     */
//    protected boolean displayHTMLPageWithDefaultBrowser(URL url) {
//    }


    /**
     * Display the given HTML URL in a popup window containing a JEditorPane.
     */
    @Override
	public void displayHTMLPage(URL url) {
//        if (_htmlViewerFrame != null) {
//            _htmlViewerFrame.getHTMLViewer().setPage(url);
//            _htmlViewerFrame.setState(Frame.NORMAL);
//            _htmlViewerFrame.setVisible(true);
//            return;
//        }
//        _htmlViewerFrame = new HTMLViewerFrame();
//        _htmlViewerFrame.getHTMLViewer().setPage(url);

        try {
            Desktop.getDesktop().browse(url.toURI());
        } catch (Exception e) {
            DialogUtil.error(e);
        }
    }

    /**
     * Display the text pointed to by the given URL.
     *
     * @param url the URL to read
     */
    public void displayPlainText(URL url) {
        try {
            String msg = FileUtil.getURL(url);
            if (_progressPanel != null) {
                _progressPanel.stop();
            }
            if (msg.length() < 256) {
                DialogUtil.error(msg);
            } else {
                displayHTMLPage(url);
            }
        } catch (IOException e) {
            DialogUtil.error(e);
        }
    }

    /**
     * @param url         the URL to read
     * @param contentType the content type from the http server
     * @return a new JComponent displaying the contents of the given URL. A null return value causes an empty panel to
     *         be displayed. Returning the current component (_resultComponent) will cause no change. This should be
     *         done if the URL is displayed in a separate window.
     */
    @SuppressWarnings({"UnusedDeclaration"})
    protected JComponent makeUnknownURLComponent(URL url, String contentType) {
        if (_resultComponent != null) {
            return _resultComponent;
        }
        return new EmptyPanel();
    }

    /**
     * Display a file chooser to select a local catalog file to open
     */
    public void open() {
        if (_openActionOverride != null) {
            _openActionOverride.actionPerformed(null);
            return;
        }

        if (_fileChooser == null) {
            _fileChooser = makeFileChooser();
        }
        int option = _fileChooser.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION && _fileChooser.getSelectedFile() != null) {
            open(_fileChooser.getSelectedFile().getAbsolutePath());
        }
    }

    /**
     * Create and return a new file chooser to be used to select a local file to open.
     *
     * @return a new file chooser
     */
    protected JFileChooser makeFileChooser() {
        return new JFileChooser(new File("."));
    }

    /**
     * @return the existing file chooser, or a new one if none exists
     */
    public JFileChooser getFileChooser() {
        if (_fileChooser == null) {
            _fileChooser = makeFileChooser();
        }
        return _fileChooser;
    }

    /**
     * Create and return a new file chooser to be used for saving to a file.
     *
     * @return a new file chooser
     */
    protected JFileChooser makeSaveFileChooser() {
        return new JFileChooser(new File("."));
    }

    /**
     * Open the given file or URL
     *
     * @param fileOrUrl a file or URL
     */
    public void open(String fileOrUrl) {
        try {
            setQueryComponent(new EmptyPanel());
            URL url = FileUtil.makeURL(null, fileOrUrl);
            URLQueryResult _queryResult = new URLQueryResult(url);
            setQueryResult(_queryResult);
        } catch (Exception e) {
            DialogUtil.error(e);
        }
    }

    /**
     * Exit the application with the given status.
     */
    public void exit() {
        System.exit(0);
    }

    /**
     * Close the window
     */
    public void close() {
        JFrame parent = SwingUtil.getFrame(this);
        if (parent != null) {
            parent.setVisible(false);
        }
    }

    /**
     * Go back to the previous component in the history list
     */
    public void back() {
        if (_backStack.size() == 0) {
            return;
        }
        if (_queryComponent != null) {
            _queryPanel.remove(_queryComponent);
            URL url = _origURL;  // save and restore this
            CatalogHistoryItem item = makeCatalogHistoryItem();
            _origURL = url;
            if (item != null) {
                _forwStack.push(item);
                _forwAction.setEnabled(true);
            }
        }
        CatalogHistoryItem historyItem = _backStack.pop();
        if (_backStack.size() == 0) {
            _backAction.setEnabled(false);
        }

        // select the related tree node
        if (historyItem.getQueryComponent() != null) {
            Catalog cat = _panelTreeNodeTable.get(historyItem.getQueryComponent());
            if (cat != null) {
                _catalogTree.setSelectedCatalog(cat, true);
            }
        }

        CatalogNavigatorMenuBar.setCurrentCatalogNavigator(this);
        _noStack = true;
        try {
            historyItem.actionPerformed(null);
        } catch (Exception e) {
            DialogUtil.error(e);
        }
        _noStack = false;

        update();
    }

    /**
     * Go forward to the next component in the history list
     */
    public void forward() {
        if (_forwStack.size() == 0) {
            return;
        }
        if (_queryComponent != null) {
            _queryPanel.remove(_queryComponent);
            URL url = _origURL;  // save and restore this
            CatalogHistoryItem item = makeCatalogHistoryItem();
            _origURL = url;
            if (item != null) {
                _backStack.push(item);
                _backAction.setEnabled(true);
            }
        }
        CatalogHistoryItem historyItem = _forwStack.pop();
        if (_forwStack.size() == 0) {
            _forwAction.setEnabled(false);
        }

        // select the related tree node
        if (historyItem.getQueryComponent() != null) {
            Catalog cat = _panelTreeNodeTable.get(historyItem.getQueryComponent());
            if (cat != null) {
                _catalogTree.setSelectedCatalog(cat, true);
            }
        }

        CatalogNavigatorMenuBar.setCurrentCatalogNavigator(this);
        _noStack = true;
        try {
            historyItem.actionPerformed(null);
        } catch (Exception e) {
            DialogUtil.error(e);
        }
        _noStack = false;

        update();
    }

    // These are for the GenericToolBarTarget interface
    @Override
	public AbstractAction getOpenAction() {
        return _openAction;
    }

    public AbstractAction getOpenUrlAction() {
        return _openUrlAction;
    }

    public AbstractAction getClearAction() {
        return _clearAction;
    }

    /**
     * Override the default Open action.
     *
     * @param openAction the new action
     */
    public void setOpenAction(AbstractAction openAction) {
        _openActionOverride = openAction;
    }

    public AbstractAction getSaveAsAction() {
        return _saveAsAction;
    }

    public AbstractAction getSaveAsHTMLAction() {
        return _saveAsHTMLAction;
    }

    public AbstractAction getSaveWithImageAction() {
        return _saveWithImageAction;
    }

    public AbstractAction getPrintAction() {
        return _printAction;
    }

    @Override
	public AbstractAction getBackAction() {
        return _backAction;
    }

    @Override
	public AbstractAction getForwAction() {
        return _forwAction;
    }

    public AbstractAction getAddRowAction() {
        return _addRowAction;
    }

    public AbstractAction getDeleteSelectedRowsAction() {
        return _deleteSelectedRowsAction;
    }

    public AbstractAction getStoreNewQueryAction() {
        return _storeNewQueryAction;
    }

    public AbstractAction getDeleteAllQueryAction() {
        return _deleteAllQueryAction;
    }

    public AbstractAction getCloseAction() {
        return _closeAction;
    }

    public AbstractAction getExitAction() {
        return _exitAction;
    }

    /**
     * Display a dialog to enter a URL to display
     */
    public void openURL() {
        String urlStr = DialogUtil.input(_I18N.getString("enterURLDisplay") + ":");
        if (urlStr != null) {
            URL url;
            try {
                url = new URL(urlStr);
            } catch (Exception e) {
                DialogUtil.error(e);
                return;
            }
            setQueryResult(new URLQueryResult(url));
        }
    }

    /**
     * Clear the display.
     */
    public void clear() {
        setQueryComponent(new EmptyPanel());
        _origURL = null;
    }

    /**
     * Pop up a dialog to ask the user for a file name, and then save the current query result to the selected file.
     */
    public void saveAs() {
        if (_resultComponent instanceof SaveableWithDialog) {
            ((SaveableWithDialog) _resultComponent).saveAs();
        } else {
            DialogUtil.error(_I18N.getString("saveNotSupportedForObjType"));
        }
    }

    /**
     * Save the current query result to the selected file.
     *
     * @param filename the file name
     */
    public void saveAs(String filename) {
        if (_resultComponent instanceof Saveable) {
            try {
                ((Saveable) _resultComponent).saveAs(filename);
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        } else {
            DialogUtil.error(_I18N.getString("saveNotSupportedForObjType"));
        }
    }

    /**
     * Save the current table as a FITS table in the current FITS image (Should be defined in a derived class).
     */
    public void saveWithImage() {
    }

    /**
     * Pop up a dialog to ask the user for a file name, and then save the current query result to the selected file in
     * HTML format.
     */
    public void saveAsHTML() {
        if (_resultComponent instanceof SaveableAsHTML) {
            if (_saveFileChooser == null) {
                _saveFileChooser = makeSaveFileChooser();
            }
            int option = _saveFileChooser.showSaveDialog(this);
            if (option == JFileChooser.APPROVE_OPTION && _saveFileChooser.getSelectedFile() != null) {
                saveAsHTML(_saveFileChooser.getSelectedFile().getAbsolutePath());
            }
        } else {
            DialogUtil.error(_I18N.getString("htmlOutputNotSupportedForObjType"));
        }
    }

    /**
     * Save the current query result to the selected file in HTML format.
     *
     * @param filename the file name
     */
    public void saveAsHTML(String filename) {
        if (_resultComponent instanceof SaveableAsHTML) {
            try {
                ((SaveableAsHTML) _resultComponent).saveAsHTML(filename);
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        } else {
            DialogUtil.error(_I18N.getString("htmlOutputNotSupportedForObjType"));
        }
    }

    /**
     * Pop up a dialog for printing the query results.
     */
    public void print() {
        if (_resultComponent instanceof PrintableWithDialog) {
            try {
                ((PrintableWithDialog) _resultComponent).print();
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        } else {
            DialogUtil.error(_I18N.getString("printingNotSupportedForObjType"));
        }
    }

    /**
     * If a table is being displayed, add an empty row in the table.
     */
    public void addRow() {
        if (_resultComponent instanceof TableDisplayTool) {
            ((TableDisplayTool) _resultComponent).addRow();
        }
    }

    /**
     * If a table is being displayed, delete the selected rows.
     */
    public void deleteSelectedRows() {
        if (_resultComponent instanceof TableDisplayTool) {
            ((TableDisplayTool) _resultComponent).deleteSelectedRows();
        }
    }

    /**
     * Set the editable state of the cells in the displayed table.
     *
     * @param b set to true if editable
     */
    public void setTableCellsEditable(boolean b) {
        if (_resultComponent instanceof TableDisplayTool) {
            ((TableDisplayTool) _resultComponent).setTableCellsEditable(b);
        }
    }

//    /**
//     * @return true if this is the main application window (enables exit menu item)
//     */
//    public static boolean isMainWindow() {
//        return _mainWindowFlag;
//    }

//    /**
//     * @param b Set to true if this is the main application window (enables exit menu item)
//     */
//    public static void setMainWindow(boolean b) {
//        _mainWindowFlag = b;
//    }

    /**
     * Used to identify an empty query or result panel
     */
    public class EmptyPanel extends JPanel implements QueryResultDisplay {
        public EmptyPanel() {
            // There is no border by default on Mac OS X, so add one
            if ("Mac OS X".equals(UIManager.getLookAndFeel().getName())) {
                setBorder(BorderFactory.createEtchedBorder());
            }
        }

        @Override
		public void setQueryResult(QueryResult queryResult) {
            throw new RuntimeException(_I18N.getString("queryResultDisplayError"));
        }
    }

    /**
     * @return the panel used to display download progress information
     */
    protected ProgressPanel getProgressPanel() {
        return _progressPanel;
    }

    /**
     * @return the stack of CatalogHistoryItems, used to go back to a previous panel
     */
    protected Stack<CatalogHistoryItem> getBackStack() {
        return _backStack;
    }

    /**
     * @return the stack of CatalogHistoryItems, used to go forward to the next panel
     */
    protected Stack<CatalogHistoryItem> getForwStack() {
        return _forwStack;
    }

    /**
     * Ask the user for a name, and then store the current query and display settings under that name for later use.
     */
    public void storeNewQuery() {
        String name = DialogUtil.input(this, _I18N.getString("enterNameForQuery"));
        if (name == null || name.length() == 0) {
            return;
        }
        storeQuery(name);
    }

    /**
     * Store the current query and display settings under the given name for later use.
     *
     * @param name the query name
     */
    public void storeQuery(String name) {
        if (_queryComponent instanceof Storeable) {
            try {
                Object queryInfo = ((Storeable) _queryComponent).storeSettings();
                Object resultInfo = null;
                if (_resultComponent instanceof Storeable) {
                    resultInfo = ((Storeable) _resultComponent).storeSettings();
                }
                CatalogQueryItem item = new CatalogQueryItem(name, queryInfo, resultInfo);
                _queryList.add(item);
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        }
    }

    /**
     * Delete the named query and display settings.
     *
     * @param name the query name
     */
    public void deleteQuery(String name) {
        _queryList.remove(name);
    }


    /**
     * Remove all items from the query list.
     */
    public void clearQueryList() {
        _queryList.clear();
    }

    /**
     * Add Query items (for previously stored queries) to the given menu, using the given listener, if supplied,
     * otherwise the default (restore the query).
     *
     * @param menu the menu to add to
     * @param l    the listener
     */
    public void addQueryMenuItems(JMenu menu, ActionListener l) {
        Iterator it = _queryList.iterator();
        if (l == null) {
            while (it.hasNext()) {
                menu.add((CatalogQueryItem) it.next());
            }
        } else {
            while (it.hasNext()) {
                CatalogQueryItem item = (CatalogQueryItem) it.next();
                JMenuItem menuItem = new JMenuItem(item.getName());
                menuItem.addActionListener(l);
                menu.add(menuItem);
            }
        }
    }

    /**
     * @return a StarTable for the currently displayed table, or null if no table is displayed
     */
    public StarTable getStarTable() {
        JComponent c = getResultComponent();
        if (c instanceof TableDisplayTool) {
            TableDisplayTool t = (TableDisplayTool) c;
            TableQueryResult tqr = t.getTable();
            if (tqr != null) {
                return tqr.getStarTable();
            }
        }
        return null;
    }

    /**
     * Register the currently displayed query component with the given URL and table id for later reference in the
     * {@link #selectTableRows} method.
     *
     * @param url     the table URL
     * @param tableId a unique value used to identify the table 
     */
    public void registerTable(URL url, String tableId) {
        if (_queryComponent != null) {
            if (tableId != null) {
                _queryComponentMap.put(tableId, _queryComponent);
            }
            if (url != null) {
                _queryComponentMap.put(url.toString(), _queryComponent);
            }
        }
    }

    /**
     * Selects a single row of an identified table by row index. The table to operate on is identified by one or both of
     * the table-id or url arguments. At least one of these must be supplied; if both are given they should refer to the
     * same thing. Exactly what highlighting means is left to the receiving application.
     *
     * @param tableId identifier associated with a table
     * @param url     URL of a table
     * @param rows    array of row indexes to highlight
     */
    public void selectTableRows(String tableId, URL url, int[] rows) {
        JComponent queryComp = null;
        if (tableId != null) {
            queryComp = _queryComponentMap.get(tableId);
        }
        if (queryComp == null && url != null) {
            queryComp = _queryComponentMap.get(url.toString());
        }
        if (queryComp != null) {
            JComponent resultComp = _queryResultComponentMap.get(queryComp);
            if (resultComp instanceof TableDisplayTool) {
                TableDisplay tableDisplay = ((TableDisplayTool) resultComp).getTableDisplay();
                tableDisplay.getTable().clearSelection();
                for (int row : rows) {
                    tableDisplay.selectRow(row);
                }
                // Display the catalog and table, if not already displayed
                CatalogHistoryItem historyItem = _historyList.getItemForQueryComponent(queryComp);
                if (historyItem != null) {
                    historyItem.actionPerformed(null);
                }
            }
        }
    }
}