JAVA   87
File menu
Guest on 16th July 2022 01:34:32 AM


  1. /*
  2.  * Created on 1. 23.
  3.  * Handles functionalities for File Menu Items such as
  4.  * New, Open, Close, Save, SaveAs, Print, Exit
  5.  * It extends JFrame in GraphicalAVR
  6.  */
  7.     package com.prioria.graphicalAVR;
  8.     import java.io.*;
  9.     import java.awt.*;
  10.         import java.awt.event.*;
  11.     import javax.swing.*;
  12.     import javax.imageio.*;
  13.  
  14. /**
  15.  * @author Chan Park
  16.  *
  17.  */
  18.     public class FileMenu extends JFrame
  19.     {
  20.                 protected JFileChooser chooser; //file chooser for selecting file
  21.                 protected File Dir; //source directory
  22.                 protected File currentFile; //test file
  23.                 protected WorkSpace myWork; //workspace to draw
  24.                 protected FunctionBlockList FBL; //list of functionblocks
  25.                 protected ConnectionList CL; //list of connections
  26.                
  27.                 //constructor taking no parameters
  28.                 public FileMenu()
  29.                 {
  30.                         myWork = null;
  31.                         currentFile = null;
  32.                         Dir = null;
  33.                         FBL = null;
  34.                         CL = null;
  35.                 }
  36.                
  37.                 public FileMenu(WorkSpace myWork) //pass the WorkSpace
  38.                 {
  39.                         this.myWork = myWork; //content will be retrieved from here..
  40.                         currentFile = null;
  41.                         Dir = null;
  42.                         FBL = null;
  43.                         CL = null;
  44.                        
  45.                 }
  46.                
  47.                 public WorkSpace getWorkSpace() //directly access workSpace
  48.                 {
  49.                         return myWork;
  50.                 }
  51.                
  52.                 public void setWorkSpace(FunctionBlockList fc, ConnectionList cc)
  53.                 {
  54.                         myWork = new WorkSpace(fc, cc);
  55.                 }
  56.                
  57.                 protected void newFile()
  58.                 {
  59.                         //new file started
  60.                         System.out.println("1. " + myWork.getFunctionBlockList().toString());
  61.                         System.out.println("2. " + myWork.getConnectionList().toString());
  62.                 }
  63.                
  64.                 protected void openFile() throws IOException
  65.                 {
  66.                        
  67.                         chooser = new JFileChooser();
  68.                         try
  69.                         {
  70.                                         Dir = (new File(".")).getCanonicalFile();
  71.                                         chooser.setCurrentDirectory(Dir);
  72.                         }
  73.                         catch (IOException ie)
  74.                         {
  75.                                 System.err.println(ie);
  76.                         }
  77.                         if(chooser.showDialog(this, "Open") != JFileChooser.APPROVE_OPTION)
  78.                         return;
  79.                         File f = chooser.getSelectedFile();
  80.                         System.out.println("selected File(reading): " + f);
  81.                         if(f == null || !f.isFile())
  82.                         {
  83.                                 return;
  84.                         }
  85.                         currentFile = f; //got the file..
  86.                        
  87.                         try // read the content here
  88.                         {                              
  89.                                 FileInputStream in = new FileInputStream("c://eclipse//eclipse//workspace//Prioria//source//" + getFileName());
  90.                                 ObjectInputStream s = new ObjectInputStream(in);
  91.                                 //myWork = (WorkSpace)s.readObject();
  92.                                 FBL = (FunctionBlockList)s.readObject();
  93.                                 CL = (ConnectionList)s.readObject();
  94.                                 System.out.println("previewing: " + in);
  95.                                 System.out.println("FBL: " + FBL);
  96.                                 System.out.println("CL: " + CL);
  97.                                 in.close();
  98.                                
  99.                         }       catch (NotSerializableException se) {
  100.                                   System.err.println(se);
  101.                         }  catch (FileNotFoundException fe) {
  102.                                   System.err.println(fe);
  103.                     }  catch (IOException se) {
  104.                                   System.err.println(se);
  105.                     }  catch (ClassNotFoundException ce) {
  106.                                   System.err.println(ce);
  107.                     }
  108.                         System.out.println("inside FileMenu.. reading..");
  109.                         System.out.println("FBL: " + FBL.toString());
  110.                         System.out.println("CL: " + CL.toString());
  111.                         setWorkSpace(FBL, CL);  //data back to the WorkSpace
  112.                         //System.out.println("myWork: " + myWork.toString());
  113.  
  114.                 }
  115.                
  116.                 protected boolean saveFile(boolean saveAs)
  117.                 {
  118.                         //save functionblocklist and connectionlist object into a file
  119.                        
  120.                         if(saveAs == false)
  121.                         {
  122.                                 chooser = new JFileChooser();
  123.                                 if(chooser.showDialog(this, "Save") != JFileChooser.APPROVE_OPTION)
  124.                                         return false;
  125.                                 File f = chooser.getSelectedFile();
  126.                                 System.out.println("selected File(writing): " + f);
  127.                                 if(f == null)
  128.                                         return false;
  129.                                 currentFile = f;
  130.                                 setTitle("Prioria - [" + currentFile.getName() + "]");
  131.                         }
  132.                         else
  133.                         {
  134.                                 chooser = new JFileChooser();
  135.                                 if(chooser.showDialog(this, "Save As..") != JFileChooser.APPROVE_OPTION)
  136.                                         return false;
  137.                                 File f = chooser.getSelectedFile();
  138.                                 currentFile = f;
  139.                                
  140.                         }
  141.                        
  142.                         try
  143.                         {
  144.                                 FileOutputStream out = new FileOutputStream("c://eclipse//eclipse//workspace//Prioria//source//" + getFileName());
  145.                                 ObjectOutputStream s = new ObjectOutputStream(out);
  146.                                 //s.writeObject(myWork);
  147.                                 FunctionBlockList temp1 = myWork.getFunctionBlockList();
  148.                                 ConnectionList temp2 = myWork.getConnectionList();
  149.                                 s.writeObject(temp1);
  150.                                 s.writeObject(temp2);
  151.                                 s.flush();
  152.                                 out.close();
  153.                                 System.out.println("inside FileMenu.. saving..");
  154.                                 System.out.println("Work: " + s.toString());
  155.                                 //System.out.println("FBL: " + temp1.toString());
  156.                                 //System.out.println("CL: " + temp2.toString());                               
  157.                         }catch (NotSerializableException se) {
  158.                                 System.err.println(se);
  159.                         }catch (FileNotFoundException fe) {
  160.                                 System.err.println(fe);
  161.                         }  catch (IOException se) {
  162.                                 System.err.println(se);
  163.                         }
  164.                        
  165.                         return true;
  166.                 }
  167.                
  168.                 protected void printFile()
  169.                 {}
  170.                
  171.                 protected void exitFile()
  172.                 {
  173.                         System.exit(0);
  174.                 }
  175.                 protected String getFileName()
  176.                 {
  177.                         if(currentFile == null)
  178.                         {
  179.                                 return ("Untitled");
  180.                         }
  181.                         else
  182.                         {
  183.                                 return currentFile.getName();
  184.                         }
  185.                 }              
  186.                 //*********************************************************
  187.  
  188.     }

Raw Paste

Login or Register to edit or fork this paste. It's free.