/*
 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.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.jstatcom.component.ResultField;
import com.jstatcom.component.StdMessages;
import com.jstatcom.component.TopFrameReference;
import com.jstatcom.io.FileSupport;

/**
 * A dialog for editing project settings.
 * 
 * @author Markus Kraetzig 
 */
public final class ProjectDialog extends JDialog {
    private static final Logger log = Logger.getLogger(ProjectDialog.class);

    private static final ProjectDialog INSTANCE = new ProjectDialog();

    private ProjectData[] dataArray = null;

    private boolean cancelled = false;

    private boolean isSaveDialog = false;

    private Project editProject = null;

    Map descriptions = new HashMap();

    private ProjectTreeNode treeNode = null;

    private JPanel jPanel = null;

    private JPanel jPanel1 = null;

    private JPanel jPanel2 = null;

    private JButton jButton = null;

    private JButton jButton1 = null;

    private JLabel jLabel = null;

    private JLabel jLabel1 = null;

    private JLabel jLabel2 = null;

    private JComboBox dataComboBox = null;

    private JTextField projectNameField = null;

    private JTextField fileNameField = null;

    private JLabel jLabel4 = null;

    private JPanel jPanel3 = null;

    private JLabel jLabel3 = null;

    private ResultField dataDesField = null;

    private ResultField projectDesArea = null;

    private JButton browseButton = null;

    /**
     * This method initializes
     * 
     */
    public ProjectDialog() {
        super(TopFrameReference.getTopFrameRef(), true);
        initialize();
    }

    /**
     * Gets singleton instance of this dialog.
     * 
     * @return initialized instance
     */
    public static ProjectDialog getInstance() {
        return INSTANCE;
    }

    /**
     * Set the project node that is currently being edited.
     * 
     * @param node
     *            the project node
     * @throws IllegalArgumentException
     *             if (node == null)
     */
    public void setProjectNode(ProjectTreeNode node) {
        if (node == null)
            throw new IllegalArgumentException("Argument was null.");
        treeNode = node;
        getProjectNameField().setText(node.getName());
        File pFile = node.getProjectFile();
        getFileNameField().setText(pFile.getAbsolutePath());
        getProjectDesArea().setText(node.getDescription());

        getDataComboBox().removeAllItems();
        getDataDesField().setText("");

        // get project
        editProject = ProjectManager.getInstance().getProjectFromFile(pFile);
        for (Iterator iter = editProject.getProjectDataIterator(); iter
                .hasNext();) {
            ProjectData element = (ProjectData) iter.next();
            getDataComboBox().addItem(element.getProjectDataType());
            descriptions.put(element.getProjectDataType(), element
                    .getDescription());
        }
        // get project data
        int datNumber = getDataComboBox().getItemCount();
        dataArray = new ProjectData[datNumber];
        int i = 0;
        for (Iterator iter = editProject.getProjectDataIterator(); iter
                .hasNext();) {
            ProjectData element = (ProjectData) iter.next();
            dataArray[i++] = element;
        }

        if (datNumber > 0) {
            getDataComboBox().setSelectedIndex(0);
            ProjectDataTypes type = (ProjectDataTypes) getDataComboBox()
                    .getSelectedItem();
            getDataDesField().setText(descriptions.get(type) + "");
        }

    }

    /**
     * Gets the new project tree node.
     * 
     * @return project node created from the settings
     */
    public ProjectTreeNode getProjectNode() {
        return treeNode;
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this
                .setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        this.setContentPane(getJPanel());
        this.setTitle("Edit Project Settings");
        this.setSize(716, 563);
        setLocationRelativeTo(TopFrameReference.getTopFrameRef());

    }

    /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel() {
        if (jPanel == null) {
            jPanel = new JPanel();
            jPanel.setLayout(new BorderLayout());
            jPanel.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
            jPanel.add(getJPanel2(), java.awt.BorderLayout.CENTER);
        }
        return jPanel;
    }

    /**
     * This method initializes jPanel1
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel1() {
        if (jPanel1 == null) {
            FlowLayout flowLayout1 = new FlowLayout();
            jPanel1 = new JPanel();
            jPanel1.setLayout(flowLayout1);
            jPanel1.setPreferredSize(new java.awt.Dimension(10, 50));
            flowLayout1.setHgap(100);
            flowLayout1.setVgap(10);
            jPanel1.add(getJButton(), null);
            jPanel1.add(getJButton1(), null);
        }
        return jPanel1;
    }

    /**
     * This method initializes jPanel2
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel2() {
        if (jPanel2 == null) {
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
            jLabel4 = new JLabel();
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
            jLabel2 = new JLabel();
            jLabel1 = new JLabel();
            jLabel = new JLabel();
            jPanel2 = new JPanel();
            jPanel2.setLayout(new GridBagLayout());
            jPanel2.setBorder(javax.swing.BorderFactory
                    .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
            jLabel.setText("File name (.jsc)");
            jLabel.setPreferredSize(new java.awt.Dimension(54, 20));
            jLabel1.setText("Project name");
            jLabel1.setPreferredSize(new java.awt.Dimension(76, 20));
            jLabel2.setText("Project description");
            jLabel2.setPreferredSize(new java.awt.Dimension(108, 20));
            jLabel4.setText("Select data type");
            jLabel4.setPreferredSize(new java.awt.Dimension(91, 20));
            gridBagConstraints1.gridx = 0;
            gridBagConstraints1.gridy = 1;
            gridBagConstraints1.ipadx = 0;
            gridBagConstraints1.ipady = 0;
            gridBagConstraints1.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints2.gridx = 0;
            gridBagConstraints2.gridy = 0;
            gridBagConstraints2.ipadx = 0;
            gridBagConstraints2.ipady = 0;
            gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints3.gridx = 0;
            gridBagConstraints3.gridy = 2;
            gridBagConstraints3.ipadx = 0;
            gridBagConstraints3.ipady = 0;
            gridBagConstraints3.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTH;
            gridBagConstraints4.gridx = 1;
            gridBagConstraints4.gridy = 0;
            gridBagConstraints4.weightx = 1.0;
            gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints4.ipadx = 0;
            gridBagConstraints4.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints4.gridwidth = 2;
            gridBagConstraints6.gridx = 1;
            gridBagConstraints6.gridy = 1;
            gridBagConstraints6.weightx = 1.0;
            gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints6.ipadx = 0;
            gridBagConstraints6.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints16.gridx = 0;
            gridBagConstraints16.gridy = 3;
            gridBagConstraints16.gridwidth = 3;
            gridBagConstraints16.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints16.weighty = 1.0D;
            gridBagConstraints16.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints17.gridx = 1;
            gridBagConstraints17.gridy = 2;
            gridBagConstraints17.weightx = 1.0;
            gridBagConstraints17.weighty = 1.0;
            gridBagConstraints17.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints17.insets = new java.awt.Insets(5, 5, 0, 5);
            gridBagConstraints17.gridwidth = 2;
            gridBagConstraints11.gridx = 2;
            gridBagConstraints11.gridy = 1;
            gridBagConstraints11.insets = new java.awt.Insets(0, 0, 0, 5);
            jPanel2.add(jLabel, gridBagConstraints1);
            jPanel2.add(jLabel1, gridBagConstraints2);
            jPanel2.add(jLabel2, gridBagConstraints3);
            jPanel2.add(getProjectNameField(), gridBagConstraints4);
            jPanel2.add(getFileNameField(), gridBagConstraints6);
            jPanel2.add(getJPanel3(), gridBagConstraints16);
            jPanel2.add(getProjectDesArea(), gridBagConstraints17);
            jPanel2.add(getBrowseButton(), gridBagConstraints11);
        }
        return jPanel2;
    }

    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setText("OK");
            jButton.setPreferredSize(new java.awt.Dimension(100, 26));
            jButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    // file name
                    String pName = getProjectNameField().getText();
                    if (pName.length() == 0) {
                        StdMessages
                                .error(
                                        "The project name is empty, please choose a name.",
                                        "Project Name Error",
                                        ProjectDialog.this);
                        return;
                    }

                    // project file
                    String fName = getFileNameField().getText();

                    if (fName.length() == 0) {
                        StdMessages
                                .error(
                                        "The project filename is empty or invalid, please change.",
                                        "Project File Error",
                                        ProjectDialog.this);
                        return;
                    }
                    if (!fName.endsWith(".jsc"))
                        fName = fName + ".jsc";
                    File pFile = new File(fName).getAbsoluteFile();
                    if (pFile.getParentFile() != null
                            && !pFile.getParentFile().canWrite()) {
                        StdMessages
                                .error(
                                        "Cannot write to project file\n\""
                                                + pFile.getAbsolutePath()
                                                + "\".\nPlease change the directory or check your write permissions.",
                                        "Project File Error",
                                        ProjectDialog.this);
                        return;

                    }

                    File origFile = null;
                    if (!isSaveDialog)
                        origFile = treeNode.getProjectFile();
                    // check file
                    if ((pFile.exists() && isSaveDialog)
                            || (pFile.exists() && !pFile.equals(origFile))) {
                        int status = JOptionPane
                                .showConfirmDialog(
                                        TopFrameReference.getTopFrameRef(),
                                        "The file  \""
                                                + pFile.getAbsolutePath()
                                                + "\" already exists.\nDo you want to overwrite it?",
                                        "Overwrite?",
                                        JOptionPane.YES_NO_OPTION,
                                        JOptionPane.QUESTION_MESSAGE);
                        if (status == JOptionPane.NO_OPTION)
                            return;
                    }

                    String pDes = getProjectDesArea().getText();

                    boolean wasCurrent = (isSaveDialog) ? true : treeNode
                            .isCurrentProject();
                    if (!isSaveDialog)
                        ProjectTree.getInstance().removeTreeNode(treeNode);

                    treeNode = new ProjectTreeNode(new Project(pName, pDes),
                            pFile, wasCurrent);

                    if (getDataComboBox().getSelectedIndex() > -1)
                        descriptions.put(getDataComboBox().getSelectedItem(),
                                getDataDesField().getText());

                    try {
                        // save current state
                        if (isSaveDialog) {

                            // descriptions
                            for (int i = 0; i < dataArray.length; i++) {
                                dataArray[i].setDescription(descriptions
                                        .get(dataArray[i].getProjectDataType())
                                        + "");
                                ProjectDataManager.getInstance()
                                        .setProjectData(dataArray[i]);
                            }
                            ProjectManager.getInstance().serializeProject(
                                    pFile, pName, pDes);
                        } else { // update project according to settings
                            // descriptions

                            for (int i = 0; i < dataArray.length; i++) {
                                dataArray[i].setDescription(descriptions
                                        .get(dataArray[i].getProjectDataType())
                                        + "");
                            }
                            // copy project and set changed data descriptions
                            Project newProject = new Project(pName, pDes);
                            for (Iterator iter = editProject
                                    .getOutputDataIterator(); iter.hasNext();) {
                                OutputData element = (OutputData) iter.next();
                                newProject.addOutputData(element);
                            }
                            for (Iterator iter = editProject
                                    .getProjectStatesIterator(); iter.hasNext();) {
                                ProjectState element = (ProjectState) iter
                                        .next();
                                newProject.addProjectState(element);
                            }
                            for (int i = 0; i < dataArray.length; i++)
                                newProject.addProjectData(dataArray[i]);

                            ProjectManager.getInstance().setProjectToFile(
                                    pFile, newProject);
                            ProjectTree.getInstance().addProjectNode(treeNode);
                            ProjectTree.getInstance().loadProjectForNode(
                                    treeNode);

                        }
                    } catch (Throwable ex) {
                        String msg = "There was an error saving a project to file:\n\""
                                + pFile.getAbsolutePath() + "\".";
                        log.error(msg, ex);
                        StdMessages.error(msg + "\nThe error message was:\n\""
                                + ex.getMessage()
                                + "\"\nPlease see the log for more details.",
                                "Project Save Error");
                        return;
                    }
                    cancelled = false;
                    setVisible(false);
                }
            });
        }
        return jButton;
    }

    /**
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setText("Cancel");
            jButton1.setPreferredSize(new java.awt.Dimension(100, 26));
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    cancelled = true;
                    treeNode = null;
                    setVisible(false);
                }
            });
        }
        return jButton1;
    }

    /**
     * This method initializes dataComboBox
     * 
     * @return javax.swing.JComboBox
     */
    private JComboBox getDataComboBox() {
        if (dataComboBox == null) {
            dataComboBox = new JComboBox();
            dataComboBox.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.DESELECTED) {
                        // update description on deselection
                        descriptions.put(e.getItem(), getDataDesField()
                                .getText());
                    }
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        // get text from descriptions map
                        Object o = descriptions.get(e.getItem());
                        String des = (o == null) ? "" : o.toString();
                        getDataDesField().setText(des);
                    }
                }
            });

        }
        return dataComboBox;
    }

    /**
     * This method initializes projectNameField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getProjectNameField() {
        if (projectNameField == null) {
            projectNameField = new JTextField();
        }
        return projectNameField;
    }

    /**
     * This method initializes fileNameField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getFileNameField() {
        if (fileNameField == null) {
            fileNameField = new JTextField();
        }
        return fileNameField;
    }

    /**
     * Returns cancelled property.
     * 
     * @return true if this dialog was cancelled
     */
    public boolean isCancelled() {
        return cancelled;
    }

    /**
     * This method initializes jPanel3
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel3() {
        if (jPanel3 == null) {
            jPanel3 = new JPanel();
            jLabel3 = new JLabel();
            GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
            GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
            jPanel3.setLayout(new GridBagLayout());
            jPanel3
                    .setBorder(javax.swing.BorderFactory
                            .createTitledBorder(
                                    javax.swing.BorderFactory
                                            .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED),
                                    "Data Description",
                                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                                    javax.swing.border.TitledBorder.DEFAULT_POSITION,
                                    new java.awt.Font("Dialog",
                                            java.awt.Font.BOLD, 12),
                                    java.awt.Color.black));
            jLabel3.setText("Set Description");
            jLabel3.setPreferredSize(new java.awt.Dimension(87, 20));
            gridBagConstraints12.gridx = 0;
            gridBagConstraints12.gridy = 0;
            gridBagConstraints12.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints12.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints13.gridx = 1;
            gridBagConstraints13.gridy = 0;
            gridBagConstraints13.weightx = 1.0;
            gridBagConstraints13.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints13.ipadx = 0;
            gridBagConstraints13.ipady = 0;
            gridBagConstraints13.insets = new java.awt.Insets(5, 5, 5, 100);
            gridBagConstraints14.gridx = 1;
            gridBagConstraints14.gridy = 1;
            gridBagConstraints14.weightx = 1.0;
            gridBagConstraints14.weighty = 1.0;
            gridBagConstraints14.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints14.ipadx = 0;
            gridBagConstraints14.ipady = 0;
            gridBagConstraints14.insets = new java.awt.Insets(5, 5, 0, 0);
            gridBagConstraints15.gridx = 0;
            gridBagConstraints15.gridy = 1;
            gridBagConstraints15.ipadx = 0;
            gridBagConstraints15.ipady = 0;
            gridBagConstraints15.insets = new java.awt.Insets(5, 5, 5, 5);
            gridBagConstraints15.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints15.anchor = java.awt.GridBagConstraints.NORTH;
            jPanel3.add(jLabel4, gridBagConstraints12);
            jPanel3.add(getDataComboBox(), gridBagConstraints13);
            jPanel3.add(getDataDesField(), gridBagConstraints14);
            jPanel3.add(jLabel3, gridBagConstraints15);
        }
        return jPanel3;
    }

    /**
     * This method initializes dataDesField
     * 
     * @return com.jstatcom.component.ResultField
     */
    private ResultField getDataDesField() {
        if (dataDesField == null) {
            dataDesField = new ResultField();
        }
        return dataDesField;
    }

    /**
     * This method initializes projectDesArea
     * 
     * @return com.jstatcom.component.ResultField
     */
    private ResultField getProjectDesArea() {
        if (projectDesArea == null) {
            projectDesArea = new ResultField();
            projectDesArea.setPreferredSize(new java.awt.Dimension(150, 100));
        }
        return projectDesArea;
    }

    /**
     * This method initializes browseButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getBrowseButton() {
        if (browseButton == null) {
            browseButton = new JButton();
            browseButton.setText("Browse...");
            browseButton.setPreferredSize(new java.awt.Dimension(100, 26));
            browseButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    File file = FileSupport.getInstance().saveProjectFile(
                            ProjectDialog.this);
                    if (file != null)
                        getFileNameField().setText(file.getAbsolutePath());
                }
            });
        }
        return browseButton;
    }

    /**
     * Sets whether this dialog is a save dialog or an edit dialog which affects
     * is behaviour.
     * 
     * @param isSaveDialog
     *            save dialog if true, edit dialog otherwise
     */
    public void setSaveDialog(boolean isSaveDialog) {
        if (isSaveDialog) {
            setTitle("Save Project...");
            getProjectNameField().setText("");
            getFileNameField().setText("");
            getProjectDesArea().setText("");

            // adds all project data types with their descriptions
            // to the combobox
            dataArray = ProjectDataManager.getInstance().getProjectData();
            getDataComboBox().removeAllItems();
            for (int i = 0; i < dataArray.length; i++) {
                getDataComboBox().addItem(dataArray[i].getProjectDataType());
                descriptions.put(dataArray[i].getProjectDataType(),
                        dataArray[i].getDescription());
            }
            if (getDataComboBox().getItemCount() > 0) {
                getDataComboBox().setSelectedIndex(0);
                ProjectDataTypes type = (ProjectDataTypes) getDataComboBox()
                        .getSelectedItem();
                getDataDesField().setText(descriptions.get(type) + "");
            }

        } else
            setTitle("Edit Project Settings");
        this.isSaveDialog = isSaveDialog;
    }
} // @jve:decl-index=0:visual-constraint="10,10"

 

Ads help maintain this website.