// $Id: PmiiTreeCellRenderer.java,v 1.9 2004/06/14 20:46:42 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 javax.swing.*;
import javax.swing.tree.*;

import com.meyling.principia.module.ModuleProperties;
import com.meyling.principia.module.ModuleState;

import java.awt.*;


/**
 * Renderer for a JTree.
 *
 * @version $Revision: 1.9 $
 * @author  Michael Meyling
 */
public final class PmiiTreeCellRenderer extends DefaultTreeCellRenderer {


    public PmiiTreeCellRenderer() {
        super();
    }


    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean isSelected, boolean expanded, boolean leaf, int row,
        boolean hasFocus) {

        JLabel aLabel = (JLabel) super.getTreeCellRendererComponent(
            tree, value, isSelected, expanded, leaf, row, hasFocus);

        ModuleElement unit;
        ModuleProperties prop;
        if (value instanceof DefaultMutableTreeNode) {
            if (((DefaultMutableTreeNode) value).getUserObject()
                    instanceof ModuleElement) {
                unit = (ModuleElement) ((DefaultMutableTreeNode)
                    value).getUserObject();
                aLabel.setText(unit.getName());
                aLabel.setToolTipText("here should stand a tool tip");  // TODO
            } else {
                prop = (ModuleProperties) ((DefaultMutableTreeNode)
                    value).getUserObject();
//                aLabel.setText(prop.getName());
                String text = prop.getName();
                if (!prop.getModuleVersion().equals("1.00.00")) {
                    text += " "+ prop.getModuleVersion();
                }
                if (prop.getRuleVersion().equals("1.02.00")) {
                    text += "'";
                }
                aLabel.setText(text);
                final ModuleState state = prop.getState();

                if (state == ModuleState.STATE_LOADED_AND_VERIFIED) {
                    aLabel.setToolTipText(prop.getAddress());
                } else {
                    aLabel.setToolTipText(prop.getStateDescription());
                }

                if (state == ModuleState.STATE_LOADING_FROM_WEB) {
                    final ImageIcon webLoadingIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_web_loading.gif"));
                    aLabel.setIcon(webLoadingIcon);
                } else if (state == ModuleState.STATE_LOADING_FROM_BUFFER) {
                    final ImageIcon webLoadingIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_buffer_loading.gif"));
                    aLabel.setIcon(webLoadingIcon);
                } else if (state == ModuleState.STATE_LOADING_INTO_MEMORY) {
                    final ImageIcon webLoadingIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_memory_loading.gif"));
                    aLabel.setIcon(webLoadingIcon);
                } else if (state == ModuleState.STATE_CHECKING) {
                    final ImageIcon webLoadingIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_checking.gif"));
                    aLabel.setIcon(webLoadingIcon);
                } else if (prop.hasFailures()) {
                    final ImageIcon badIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_error.gif"));
//                    aLabel.setForeground(Color.gray);
                    aLabel.setIcon(badIcon);
                } else if (state == ModuleState.STATE_LOADED_AND_VERIFIED) {
                    final ImageIcon goodIcon = new ImageIcon(
                            getClass().getResource("/com/meyling/principia/pic/module_ok.gif"));
//                    aLabel.setForeground(Color.gray);
                    aLabel.setIcon(goodIcon);
                } else {
                    System.out.println("unknown module state: " + state.getText());
                    throw new IllegalStateException("unknown module state: " + state.getText());
                }
            }
        }
        return aLabel;
    }

}
