- /*
- * Created on 1. 23.
- * Handles functionalities for File Menu Items such as
- * New, Open, Close, Save, SaveAs, Print, Exit
- * It extends JFrame in GraphicalAVR
- */
- package com.prioria.graphicalAVR;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.imageio.*;
- /**
- * @author Chan Park
- *
- */
- public class FileMenu extends JFrame
- {
- protected JFileChooser chooser; //file chooser for selecting file
- protected File Dir; //source directory
- protected File currentFile; //test file
- protected WorkSpace myWork; //workspace to draw
- protected FunctionBlockList FBL; //list of functionblocks
- protected ConnectionList CL; //list of connections
- //constructor taking no parameters
- public FileMenu()
- {
- myWork = null;
- currentFile = null;
- Dir = null;
- FBL = null;
- CL = null;
- }
- public FileMenu(WorkSpace myWork) //pass the WorkSpace
- {
- this.myWork = myWork; //content will be retrieved from here..
- currentFile = null;
- Dir = null;
- FBL = null;
- CL = null;
- }
- public WorkSpace getWorkSpace() //directly access workSpace
- {
- return myWork;
- }
- public void setWorkSpace(FunctionBlockList fc, ConnectionList cc)
- {
- myWork = new WorkSpace(fc, cc);
- }
- protected void newFile()
- {
- //new file started
- System.out.println("1. " + myWork.getFunctionBlockList().toString());
- System.out.println("2. " + myWork.getConnectionList().toString());
- }
- protected void openFile() throws IOException
- {
- chooser = new JFileChooser();
- try
- {
- Dir = (new File(".")).getCanonicalFile();
- chooser.setCurrentDirectory(Dir);
- }
- catch (IOException ie)
- {
- System.err.println(ie);
- }
- if(chooser.showDialog(this, "Open") != JFileChooser.APPROVE_OPTION)
- return;
- File f = chooser.getSelectedFile();
- System.out.println("selected File(reading): " + f);
- if(f == null || !f.isFile())
- {
- return;
- }
- currentFile = f; //got the file..
- try // read the content here
- {
- FileInputStream in = new FileInputStream("c://eclipse//eclipse//workspace//Prioria//source//" + getFileName());
- ObjectInputStream s = new ObjectInputStream(in);
- //myWork = (WorkSpace)s.readObject();
- FBL = (FunctionBlockList)s.readObject();
- CL = (ConnectionList)s.readObject();
- System.out.println("previewing: " + in);
- System.out.println("FBL: " + FBL);
- System.out.println("CL: " + CL);
- in.close();
- } catch (NotSerializableException se) {
- System.err.println(se);
- } catch (FileNotFoundException fe) {
- System.err.println(fe);
- } catch (IOException se) {
- System.err.println(se);
- } catch (ClassNotFoundException ce) {
- System.err.println(ce);
- }
- System.out.println("inside FileMenu.. reading..");
- System.out.println("FBL: " + FBL.toString());
- System.out.println("CL: " + CL.toString());
- setWorkSpace(FBL, CL); //data back to the WorkSpace
- //System.out.println("myWork: " + myWork.toString());
- }
- protected boolean saveFile(boolean saveAs)
- {
- //save functionblocklist and connectionlist object into a file
- if(saveAs == false)
- {
- chooser = new JFileChooser();
- if(chooser.showDialog(this, "Save") != JFileChooser.APPROVE_OPTION)
- return false;
- File f = chooser.getSelectedFile();
- System.out.println("selected File(writing): " + f);
- if(f == null)
- return false;
- currentFile = f;
- setTitle("Prioria - [" + currentFile.getName() + "]");
- }
- else
- {
- chooser = new JFileChooser();
- if(chooser.showDialog(this, "Save As..") != JFileChooser.APPROVE_OPTION)
- return false;
- File f = chooser.getSelectedFile();
- currentFile = f;
- }
- try
- {
- FileOutputStream out = new FileOutputStream("c://eclipse//eclipse//workspace//Prioria//source//" + getFileName());
- ObjectOutputStream s = new ObjectOutputStream(out);
- //s.writeObject(myWork);
- FunctionBlockList temp1 = myWork.getFunctionBlockList();
- ConnectionList temp2 = myWork.getConnectionList();
- s.writeObject(temp1);
- s.writeObject(temp2);
- s.flush();
- out.close();
- System.out.println("inside FileMenu.. saving..");
- System.out.println("Work: " + s.toString());
- //System.out.println("FBL: " + temp1.toString());
- //System.out.println("CL: " + temp2.toString());
- }catch (NotSerializableException se) {
- System.err.println(se);
- }catch (FileNotFoundException fe) {
- System.err.println(fe);
- } catch (IOException se) {
- System.err.println(se);
- }
- return true;
- }
- protected void printFile()
- {}
- protected void exitFile()
- {
- System.exit(0);
- }
- protected String getFileName()
- {
- if(currentFile == null)
- {
- return ("Untitled");
- }
- else
- {
- return currentFile.getName();
- }
- }
- //*********************************************************
- }
Raw Paste