/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * This program 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 * TreeMapTopicPanel.java
 *
 * Created on Oct 19, 2011, 8:12:21 PM
 */

package org.wandora.application.gui.topicpanels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Collection;

import javax.swing.Icon;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.wandora.application.CancelledException;
import org.wandora.application.LocatorHistory;
import org.wandora.application.RefreshListener;
import org.wandora.application.Wandora;
import org.wandora.application.gui.UIBox;
import org.wandora.application.gui.topicpanels.treemap.TreeMapComponent;
import org.wandora.application.gui.topicstringify.TopicToString;
import org.wandora.topicmap.Association;
import org.wandora.topicmap.Locator;
import org.wandora.topicmap.Topic;
import org.wandora.topicmap.TopicMapException;
import org.wandora.topicmap.TopicMapListener;

/**
 *
 * @author akivela
 */


public class TreeMapTopicPanel extends javax.swing.JPanel implements TopicMapListener, RefreshListener, TopicPanel, ActionListener, ComponentListener {


    private static final long serialVersionUID = 1L;
    
    private Topic topic;
    private String topicSI;
    
    private TreeMapComponent treeMapComponent = null;


    
    
    /** Creates new form TreeMapTopicPanel */
    public TreeMapTopicPanel() {
    }
    
    
    @Override
    public void init() {
        initComponents();
        treeMapComponent = new TreeMapComponent(this);
        contentPanel.add(treeMapComponent, BorderLayout.CENTER);
        //contentPanel.addComponentListener(this);
    }
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        contentPanel = new javax.swing.JPanel();
        removedTopicMessage = new javax.swing.JPanel();
        removedTopicMessageLabel = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(400, 300));
        setLayout(new java.awt.GridBagLayout());

        contentPanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(contentPanel, gridBagConstraints);

        removedTopicMessage.setBackground(new java.awt.Color(255, 255, 255));
        removedTopicMessage.setLayout(new java.awt.GridBagLayout());

        removedTopicMessageLabel.setText("<html>Topic is either merged or removed and can not be viewed!</html>");
        removedTopicMessage.add(removedTopicMessageLabel, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(removedTopicMessage, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel removedTopicMessage;
    private javax.swing.JLabel removedTopicMessageLabel;
    // End of variables declaration//GEN-END:variables

    
    
    @Override
    public boolean supportsOpenTopic() {
        return true;
    }
    
    
    @Override
    public void open(Topic topic) throws TopicMapException {
        try {
            this.topic = topic;
            this.topicSI = topic.getOneSubjectIdentifier().toExternalForm();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        doRefresh();
    }

    
    public void clearCaches() {
        if(treeMapComponent != null) {
            treeMapComponent.clearCaches();
        }
    }
    
    
    
    // -------------------------------------------------- TOPIC MAP LISTENER ---
    
    
    
    
    @Override
    public void topicSubjectIdentifierChanged(Topic t, Locator added, Locator removed) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicBaseNameChanged(Topic t, String newName, String oldName) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicTypeChanged(Topic t, Topic added, Topic removed) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicVariantChanged(Topic t, Collection<Topic> scope, String newName, String oldName) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicDataChanged(Topic t, Topic type, Topic version, String newValue, String oldValue) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicSubjectLocatorChanged(Topic t, Locator newLocator, Locator oldLocator) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicRemoved(Topic t) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void topicChanged(Topic t) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void associationTypeChanged(Association a, Topic newType, Topic oldType) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void associationPlayerChanged(Association a, Topic role, Topic newPlayer, Topic oldPlayer) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void associationRemoved(Association a) throws TopicMapException {
	clearCaches();
        doRefresh();
    }

    @Override
    public void associationChanged(Association a) throws TopicMapException {
	clearCaches();
        doRefresh();
    }
    
    
    // -------------------------------------------------------------------------

    
    @Override
    public void doRefresh() throws TopicMapException {
        try {
            topic=Wandora.getWandora().getTopicMap().getTopic(topicSI);
            if(topic==null || topic.isRemoved()) {
                System.out.println("Topic is null or removed!");
                contentPanel.setVisible(false);
                removedTopicMessage.setVisible(true);
                return;
            }
            else {
                contentPanel.setVisible(true);
                removedTopicMessage.setVisible(false);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Topic is null or removed!");
            contentPanel.setVisible(false);
            removedTopicMessage.setVisible(true);
            return;
        }
        
        treeMapComponent.initialize(topic);
        treeMapComponent.repaint();
        revalidate();
        repaint();
    }
    
    

    @Override
    public void stop() {
	// NOTHING TO STOP!
    }

    
    
    
    @Override
    public void refresh() throws TopicMapException {
	doRefresh();
    }
    

    @Override
    public boolean applyChanges() throws CancelledException, TopicMapException {
        return true;
    }

    @Override
    public JPanel getGui() {
	return this;
    }

    @Override
    public Topic getTopic() throws TopicMapException {
	return topic;
    }
    
    
    @Override
    public LocatorHistory getTopicHistory() {
        return null;
    }
    
    
    @Override
    public Icon getIcon() {
        return UIBox.getIcon("gui/icons/topic_panel_treemap.png");
    }

    @Override
    public boolean noScroll(){
        return false;
    }

    
    
    @Override
    public JPopupMenu getViewPopupMenu() {
        return UIBox.makePopupMenu(getViewMenuStruct(), this);
    }
    
    @Override
    public JMenu getViewMenu() {
        return UIBox.makeMenu(getViewMenuStruct(), this);
    }

    
    @Override
    public Object[] getViewMenuStruct() {
        int d = treeMapComponent.getIterationDepth();
        Object[] menuStructure = new Object[] {
            "Set depth to 1", (d == 1 ? UIBox.getIcon("gui/icons/checkbox_selected.png") : UIBox.getIcon("gui/icons/checkbox.png")), this,
            "Set depth to 2", (d == 2 ? UIBox.getIcon("gui/icons/checkbox_selected.png") : UIBox.getIcon("gui/icons/checkbox.png")), this,
            "Set depth to 3", (d == 3 ? UIBox.getIcon("gui/icons/checkbox_selected.png") : UIBox.getIcon("gui/icons/checkbox.png")), this,
            "Set depth to 4", (d == 4 ? UIBox.getIcon("gui/icons/checkbox_selected.png") : UIBox.getIcon("gui/icons/checkbox.png")), this,
            //"---",
            //"Reset zoom", this,
        };
        return menuStructure;
    }
    
    
    
    public void toggleVisibility(String componentName) {
	throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    @Override
    public String getName(){
        return "Treemap";
    }

    @Override
    public String getTitle() {
        if(topic != null) return TopicToString.toString(topic);
        else return "";
    }
    
    
    @Override
    public int getOrder() {
        return 200;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e == null) return;
        String ac = e.getActionCommand();
        if("Set depth to 1".equals(ac)) {
            treeMapComponent.setIterationDepth(1);
            Wandora.getWandora().topicPanelsChanged();
        }
        if("Set depth to 2".equals(ac)) {
            treeMapComponent.setIterationDepth(2);
            Wandora.getWandora().topicPanelsChanged();
        }
        else if("Set depth to 3".equals(ac)) {
            treeMapComponent.setIterationDepth(3);
            Wandora.getWandora().topicPanelsChanged();
        }
        else if("Set depth to 4".equals(ac)) {
            treeMapComponent.setIterationDepth(4);
            Wandora.getWandora().topicPanelsChanged();
        }
        else if("Reset zoom".equals(ac)) {
            treeMapComponent.resetZoom();
        }
    }

    
    
    // -------------------------------------------------------------------------
    
    
    @Override
    public void componentResized(ComponentEvent e) {
        handleComponentEvent(e);
    }

    @Override
    public void componentMoved(ComponentEvent e) {
        handleComponentEvent(e);
    }

    @Override
    public void componentShown(ComponentEvent e) {
        handleComponentEvent(e);
    }

    @Override
    public void componentHidden(ComponentEvent e) {
        handleComponentEvent(e);
    }
    
    
    private void handleComponentEvent(ComponentEvent e) {
        if(treeMapComponent != null) treeMapComponent.setSize(getWidth(), getHeight());
    }
    
    
    
    
    
    // -------------------------------------------------------------------------
    // -------------------------------------------------------------------------
    // -------------------------------------------------------------------------
    
    


}
    

