// $Id: PmiiTreeView.java,v 1.10 2004/06/14 20:47:27 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.gui.jtree;

import java.awt.*;
import java.awt.event.*;

import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.*;


/**
 * View for certain JTree.
 *
 * A View, which is a collection of classes representing the elements in the user interface
 * (all of the things the user can see and respond to on the screen, such as buttons, display
 * boxes, and so forth).
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public final class PmiiTreeView extends JPanel {

    public static final String REFRESH_ACTION = "Refresh";
    public static final String DELETE_ACTION = "Delete";
    public static final String ADD_ACTION = "Add";
    public static final String HTML_ACTION = "Html";

    private final JTree theTree;

    private final JPopupMenu contextMenu = new JPopupMenu();

    private final JMenuItem refreshItem;
    private final JMenuItem deleteItem;
    private final JMenuItem addItem;
    private final JMenuItem htmlItem;


    /**
     * Constructor.
     *
     * @param   treeModel   modell for this tree
     */
    public PmiiTreeView(final TreeModel treeModel) {
        super(new GridLayout(1,1));
        theTree = new JTree(treeModel);
        theTree.setEditable(false);
        theTree.setRootVisible(false);
        theTree.setExpandsSelectedPaths(true);      // TODO new
        theTree.setScrollsOnExpand(true);           // TODO new
        theTree.setShowsRootHandles(true);
        theTree.getSelectionModel().setSelectionMode
                    (TreeSelectionModel.SINGLE_TREE_SELECTION);
        // Enable tool tips for the tree, without this tool tips will not
        //  be picked up.
        ToolTipManager.sharedInstance().registerComponent(theTree);

        theTree.setCellRenderer(new PmiiTreeCellRenderer());
        /* Make tree ask for the height of each row. */
        theTree.setRowHeight(-1);


        this.add(new JScrollPane(theTree));
        this.setMinimumSize(new Dimension(150, 100));

        refreshItem = new JMenuItem("Refresh");
        refreshItem.setActionCommand(REFRESH_ACTION);
        refreshItem.setEnabled(false);
        contextMenu.add(refreshItem);

        deleteItem = new JMenuItem("Delete");
        deleteItem.setActionCommand(DELETE_ACTION);
        deleteItem.setEnabled(false);
        contextMenu.add(deleteItem);

        addItem = new JMenuItem("Add");
        addItem.setActionCommand(ADD_ACTION);
        contextMenu.add(addItem);

        htmlItem = new JMenuItem("Html");
        htmlItem.setActionCommand(HTML_ACTION);
        contextMenu.add(htmlItem);

    }

    /* @see JTree#expandPath
     */
    public final void expandPath(final TreePath path) {
        theTree.expandPath(path);
    }


    /**
     * Return context menu.
     *
     * @return  context menu
     */
    public final JPopupMenu getContextMenu() {
        return contextMenu;
    }


    /**
     * Registers ActionListener for all context menu entries.
     *
     * @param   listener    register this one
     */
    public final void addActionCommandToContextMenus(final ActionListener listener) {
        refreshItem.addActionListener(listener);
        deleteItem.addActionListener(listener);
        addItem.addActionListener(listener);
        htmlItem.addActionListener(listener);
    }


    /* @see JTree#addTreeSelectionListener
     */
    public final void addTreeSelectionListener(final TreeSelectionListener listener) {
         theTree.addTreeSelectionListener(listener);
    }


    /* @see JTree#setSelectionRow
     */
    public void setSelectionRow(int row) {
        theTree.setSelectionRow(row);
    }


    /* @see JTree#setSelectionPath
     */
    public void setSelectionPath(TreePath path) {
        theTree.setSelectionPath(path);
    }


    /* @see JTree#getSelectionPath
     */
    public TreePath getSelectionPath() {
        return theTree.getSelectionPath();
    }


    /* @see JTree#getSelectionPaths
     */
    public TreePath[] getSelectionPaths() {
        return theTree.getSelectionPaths();
    }


    /* @see JTree#getPathForLocation
     */
    public TreePath getPathForLocation(int x, int y) {
        return theTree.getPathForLocation(x, y);
    }


    /*
     * @see JTree#startEditingAtPath
     */
    public void startEditingAtPath(TreePath path) {
        theTree.startEditingAtPath(path);
    }


    /*
     * @see JTree#treeAddMouseListener
     */
    public void treeAddMouseListener(MouseListener mouseListener) {
        theTree.addMouseListener(mouseListener);
    }

}
