Package paperscope

Source Code of paperscope.SaveGraphDialog

/*
* SaveGraphDialog.java
*
* Created on 05 September 2007, 17:20
*/
/**
*
* @author  Mark
*/

package paperscope;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JFileChooser;

import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Dimension;

import java.io.File;


public class SaveGraphDialog extends javax.swing.JFrame {
   
    /** Creates new form SaveGraphLayoutMessage */
    public SaveGraphDialog(RadialGraph graph, String currentDirectory) {
        initComponents();
        this.graph = graph;
        this.currentDirectory = currentDirectory;
    }

//###########################################################################################   
    public void saveGraphFile()
    {
      //==== Initialize the filechooser, using the user's home dir, or the last working dir
      if(this.currentDirectory.equals(""))
       {  this.fc = new JFileChooser()}
      else
       this.fc = new JFileChooser(this.currentDirectory); }

      int returnVal = fc.showSaveDialog(this);

      if (returnVal == JFileChooser.APPROVE_OPTION)
      {
        //==== Get the file object, and its filename
        File file = fc.getSelectedFile();
        final String fileName = file.getAbsolutePath();
       
        //==== if the file already exists, warn the user before overwriting
        if(file.exists())
        {
            //==== Create a frame with the message
            final JFrame overwriteMessage = new JFrame();
            overwriteMessage.setTitle("Overwrite file?");
            overwriteMessage.setLocationRelativeTo(fc);           
            JLabel title = new JLabel(" " + fileName);
            title.setFont(new Font("Tahoma", 0, 12));
            JLabel title2 = new JLabel(" File already exists, overwrite it?");
            title.setFont(new Font("Tahoma", 0, 12));
           
            //==== Create the button for overwriting the file
            JButton saveButton = new JButton();
            saveButton.setText("Yes");
            saveButton.setToolTipText("Overwrite file?");
            saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                    ResultXML xml = new ResultXML();
                    xml.writeFile(graph.getDataGraph(), fileName);
                    overwriteMessage.setVisible(false);
                }
            });
            //==== Create the button for not overwriting the file
            JButton noButton = new JButton();
            noButton.setText("No");
            noButton.setToolTipText("Overwrite file");
            noButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                    overwriteMessage.setVisible(false);
                }
            });
           
            //==== Add the buttons to a panel, and setup the layout
            JPanel buttons = new JPanel();
            GridLayout buttonLayout = new GridLayout(1,2);
            buttonLayout.addLayoutComponent("savebutton", saveButton);
            buttonLayout.addLayoutComponent("nobutton", noButton);
            buttons.add(saveButton);
            buttons.add(noButton);
            buttons.setLayout(buttonLayout);
           
            //==== Add the label, and the button panel, to another panel and setup the layout
            JPanel complete = new JPanel();
            GridLayout layout = new GridLayout(3,1);
            layout.addLayoutComponent("title", title);
            layout.addLayoutComponent("title2", title2);
            layout.addLayoutComponent("buttons", buttons);
            complete.add(title);
            complete.add(title2);
            complete.add(buttons);
            complete.setLayout(layout);

            Dimension d = new Dimension(450, 100);
            overwriteMessage.setSize(d);
            overwriteMessage.setContentPane(complete);
            overwriteMessage.setVisible(true);
        }
       
        //==== file doesn't already exist, so just write it       
        else
        {           
            //==== If it doesn't already have the .xml extension, add it
            String xmlExtension = ".xml";
            String fileName2 = fileName;
            if (!fileName.contains(xmlExtension))
            fileName2 = file.getAbsolutePath() + xmlExtension;
           
            ResultXML xml = new ResultXML();
            xml.writeFile(this.graph.getDataGraph(), fileName2);
        }
        this.currentDirectory = file.getPath();
      }
    }
   
//###########################################################################################   
    public String getCurrentDirectory()
    {
        return this.currentDirectory;
    }
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        messageText = new javax.swing.JLabel();
        yesButton = new javax.swing.JButton();
        noButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Save Graph Layout?");
        messageText.setText("Save Graph Layout?");

        yesButton.setText("Yes");
        yesButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                yesButtonMouseClicked(evt);
            }
        });

        noButton.setText("No");
        noButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                noButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(messageText, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(yesButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(noButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messageText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yesButton)
                    .addComponent(noButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void noButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_noButtonMouseClicked
        this.setVisible(false);
        saveGraphFile();
    }//GEN-LAST:event_noButtonMouseClicked

    private void yesButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_yesButtonMouseClicked
        this.graph.saveGraphLayout();
        this.setVisible(false);
        saveGraphFile();
    }//GEN-LAST:event_yesButtonMouseClicked
   
    private RadialGraph graph;
    private String currentDirectory;
    private JFileChooser fc;
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel messageText;
    private javax.swing.JButton noButton;
    private javax.swing.JButton yesButton;
    // End of variables declaration//GEN-END:variables
   
}
TOP

Related Classes of paperscope.SaveGraphDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.