/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.project;

import java.io.File;
import java.util.Date;

import javax.swing.tree.DefaultMutableTreeNode;

import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;

import com.jstatcom.component.CompSettings;

/**
 * A tree node that represents a project. It is used as element for the
 * ProjectTree.
 * 
 * @author Markus Kraetzig 
 */
public final class ProjectTreeNode extends DefaultMutableTreeNode implements
        SettingsElement {

    private String name;

    private String desription;

    private File projectFile;

    private Date date;

    private boolean currentProject = false;

    private boolean lastState = false;

    public static final XmlFormat ProjectTreeNode_XML = new XmlFormat(
            ProjectTreeNode.class) {
        public void format(ProjectTreeNode data, XmlElement xml) {
            xml.setAttribute("name", data.name);
            xml.setAttribute("date", data.date.getTime());
            xml.setAttribute("description", data.desription);
            xml.setAttribute("file", data.projectFile.getAbsolutePath());
            xml.setAttribute("current", data.currentProject);
        }

        public ProjectTreeNode parse(XmlElement xml) {
            String nam = xml.getAttribute("name") + "";
            long dateMillis = Long.parseLong(xml.getAttribute("date") + "");
            String description = xml.getAttribute("description") + "";
            File file = new File(xml.getAttribute("file") + "");
            boolean current = Boolean.valueOf(xml.getAttribute("current") + "")
                    .booleanValue();

            return new ProjectTreeNode(nam, description, new Date(dateMillis),
                    file, current);
        }
    };

    /**
     * Creates a tree node for a project.
     * 
     * @param project
     *            the project that this node stands for
     * @param projectFile
     *            the file where the project is stored
     * @param isCurrent
     *            whether this node is the currently loaded project
     * @throws IllegalArgumentException
     *             if (project == null || projectFile == null)
     */
    public ProjectTreeNode(Project project, File projectFile, boolean isCurrent) {

        if (project == null || projectFile == null)
            throw new IllegalArgumentException("Argument was null.");

        name = project.getName();
        desription = project.getDescription();
        date = project.getDate();
        this.projectFile = projectFile;
        setAllowsChildren(false);
        this.currentProject = isCurrent;
        
    }

    /**
     * Deserialization constructor.
     * 
     * @param name
     * @param description
     * @param date
     * @param projectFile
     * @throw IllegalArgumentException
     *        if (date == null || projectFile == null)
     */
    private ProjectTreeNode(String name, String description, Date date,
            File projectFile, boolean currentProject) {
        if (date == null || projectFile == null)
            throw new IllegalArgumentException("Argument was null.");
        this.name = name;
        this.desription = description;
        this.date = date;
        this.projectFile = projectFile;
        this.currentProject = currentProject;
        setAllowsChildren(false);
    }

    /**
     * Gets the project description.
     * 
     * @return description string
     */
    String getDescription() {
        return desription;
    }

    /**
     * Gets the file where the project is stored.
     * 
     * @return project file
     */
    File getProjectFile() {
        return projectFile;
    }

    /**
     * Gets the name of the underlying project.
     * 
     * @return project name
     */
    String getName() {
        return name;
    }

    /**
     * Gets the date when the underlying project was created.
     * 
     * @return creation date
     */
    Date getDate() {
        return date;
    }

    /**
     * Overwritessuperclass method to make sure that two project nodes are
     * considered equal if they reference the same project file.
     * 
     * @param o
     *            object to compare
     * @return true if o is a
     *         ProjectTreeNode that points to the same project
     *         file, false otherwise
     */
    public final boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof ProjectTreeNode))
            return false;

        return projectFile.equals(((ProjectTreeNode) o).getProjectFile());
    }

    /**
     * Overwritten because equals was overwritten.
     * 
     * @return the hashcode of the project file
     */
    public int hashCode() {
        return projectFile.hashCode();
    }

    /**
     * Sets the contents of this node to the contents of node.
     * Used by the ProjectTree to synchronize all fields between
     * equal nodes, which happens already if only the filenames are similar.
     * Therefore it might be necessary to also copy the remaining fields.
     * 
     * @param node
     *            tree node from which the values are retrieved and set to this
     *            node
     */
    void setNode(ProjectTreeNode node) {
        if (node == null)
            throw new IllegalArgumentException("Argument was null.");
        name = node.getName();
        desription = node.getDescription();
        date = node.getDate();
        this.projectFile = node.getProjectFile();

    }

    /**
     * Gets a string representation of this node.
     * 
     * @return descriptive string
     */
    public String toString() {
        String proj = name;
        return proj + " <" + CompSettings.getDefaultDateFormat().format(date)
                + ">";
    }

    /**
     * Gets whether this node represents the currently loaded project.
     * 
     * @return true if current project, default is
     *         false
     */
    boolean isCurrentProject() {
        return currentProject;
    }

    /**
     * Sets whether this node represents the currently loaded project.
     * 
     * @param currentProject
     *            true if current project, default is
     *            false
     */
    void setCurrentProject(boolean currentProject) {
        this.currentProject = currentProject;
    }

    /**
     * Interface method from SettingsElement to restore a
     * setting. It adds this node to the project tree if the project file still
     * exists.
     */
    public void setElement() {
        currentProject = false;
        if (projectFile != null && projectFile.canRead())
            ProjectTree.getInstance().addProjectNode(this);
    }

    /**
     * Gets whether this node represents the last state that is being restored
     * at program start.
     * 
     * @return true if last state, default is false
     */
    boolean isLastState() {
        return lastState;
    }

    /**
     * Sets whether this node represents the last state that is being restored
     * at program start.
     * 
     * @param lastState
     *            true if last state
     */
    void setLastState(boolean lastState) {
        this.lastState = lastState;
    }
}
 

Ads help maintain this website.