From 6614fb5b498b59296ceeaed3c45571474f5808de Mon Sep 17 00:00:00 2001 From: "s.feenstra" Date: Thu, 30 Sep 2010 20:18:45 +0200 Subject: [PATCH] Load/Save File added. --- java/Graph Editor/Todo.txt | 6 --- java/Graph Editor/src/interfaces/GraphFrame.java | 54 +++++++++++++++++-- java/Graph Editor/src/interfaces/GraphPanel.java | 4 ++ java/Graph Editor/src/models/GraphEdge.java | 7 +++ java/Graph Editor/src/models/GraphModel.java | 68 ++++++++++++++++++++++++ java/Graph Editor/src/models/GraphVertex.java | 5 ++ 6 files changed, 133 insertions(+), 11 deletions(-) diff --git a/java/Graph Editor/Todo.txt b/java/Graph Editor/Todo.txt index 112852b..b8e914c 100644 --- a/java/Graph Editor/Todo.txt +++ b/java/Graph Editor/Todo.txt @@ -1,12 +1,6 @@ Todo: - Files: - Save (Implement) - Load (Implement) - Menu: - SaveFile (Add Option+Link) - LoadFile (Link to correct function) Enable/Disable (Redo/Undo depending on actions available) diff --git a/java/Graph Editor/src/interfaces/GraphFrame.java b/java/Graph Editor/src/interfaces/GraphFrame.java index 0915346..1609bee 100644 --- a/java/Graph Editor/src/interfaces/GraphFrame.java +++ b/java/Graph Editor/src/interfaces/GraphFrame.java @@ -7,6 +7,7 @@ import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.io.File; import javax.swing.*; import javax.swing.undo.*; @@ -32,7 +33,9 @@ public class GraphFrame extends JFrame { private JMenuItem menuOptionEditAddNode; private JMenuItem menuOptionEditAddEdge; private JMenuItem menuOptionEditRemoveNode; + private JMenuItem menuOptionFileSave; + private ActionListener onMenuFileSave; private ActionListener onMenuEditUndo; private ActionListener onMenuEditRedo; private ActionListener onMenuEditAddNode; @@ -72,20 +75,28 @@ public class GraphFrame extends JFrame { menuOptionsEditRedo = new JMenuItem("Herhalen",KeyEvent.VK_R); menuOptionsEditRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,Event.CTRL_MASK)); + menuOptionFileSave = new JMenuItem("Opslaan",KeyEvent.VK_S); + menuOptionFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK)); + + onMenuFileSave = new ActionListener(){ + public void actionPerformed(ActionEvent arg0) { + saveFile(); + } + }; + onMenuFileClose = new ActionListener(){ public void actionPerformed(ActionEvent arg0) { - System.exit(0); + System.exit(0); // TODO Werkt niet goed? } }; onMenuFileOpen = new ActionListener(){ public void actionPerformed(ActionEvent arg0) { - //TODO + openFile(); } }; onMenuEditUndo = new ActionListener(){ public void actionPerformed(ActionEvent arg0) { - //TODO undoManager.undo(); } }; @@ -118,8 +129,10 @@ public class GraphFrame extends JFrame { menuOptionFileOpen.addActionListener(onMenuFileOpen); menuOptionEditRemoveNode.addActionListener(onMenuEditDeleteNode); menuOptionsEditRedo.addActionListener(onMenuEditRedo); - + menuOptionFileSave.addActionListener(onMenuFileSave); + menuOptionFile.add(menuOptionFileOpen); + menuOptionFile.add(menuOptionFileSave); menuOptionFile.addSeparator(); menuOptionFile.add(menuOptionFileClose); @@ -133,12 +146,43 @@ public class GraphFrame extends JFrame { menubar.add(menuOptionFile); menubar.add(menuOptionEdit); setJMenuBar(menubar); - panel = new GraphPanel(model); + panel = new GraphPanel(model); add(panel); setVisible(true); } + + protected void openFile() + { + final JFileChooser fc = new JFileChooser(); + int returnVal = fc.showOpenDialog(this); + if(returnVal == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try{ + GraphModel model = GraphModel.fromFile(file); + panel.setModel(model); + model.setChanged(); + }catch(Exception e){ + JOptionPane.showMessageDialog(this, (String)("An error occured trying to read the input file. \r\n" + e.getMessage()) , "Error",JOptionPane.ERROR_MESSAGE ); + } + } + } + + protected void saveFile() + { + final JFileChooser fc = new JFileChooser(); + int returnVal = fc.showSaveDialog(this); + if(returnVal == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try{ + panel.getModel().writeToFile(file); + }catch(Exception e){ + JOptionPane.showMessageDialog(this, (String)("An error occured trying to save the current state to a file. \r\n" + e.getMessage()) , "Error",JOptionPane.ERROR_MESSAGE ); + } + } + } + public void addAction(UndoableEdit action){ this.undoManager.addEdit(action); } diff --git a/java/Graph Editor/src/interfaces/GraphPanel.java b/java/Graph Editor/src/interfaces/GraphPanel.java index 4fc42bf..8174d84 100644 --- a/java/Graph Editor/src/interfaces/GraphPanel.java +++ b/java/Graph Editor/src/interfaces/GraphPanel.java @@ -71,6 +71,10 @@ public class GraphPanel extends JPanel implements Observer{ selectionController.removeSelected(); } + public GraphModel getModel(){ + return model; + } + public int getDrawTextWidth(String in) { FontMetrics fm = this.getFontMetrics(this.getFont()); diff --git a/java/Graph Editor/src/models/GraphEdge.java b/java/Graph Editor/src/models/GraphEdge.java index 86974ae..88a0a47 100644 --- a/java/Graph Editor/src/models/GraphEdge.java +++ b/java/Graph Editor/src/models/GraphEdge.java @@ -25,6 +25,13 @@ public class GraphEdge extends Observable{ return false; } + public GraphVertex getEdge(boolean src){ + if(src) + return nodes[0]; + else + return nodes[1]; + } + public void setNodes(GraphVertex[] nodes){ this.nodes = nodes; setChanged(); diff --git a/java/Graph Editor/src/models/GraphModel.java b/java/Graph Editor/src/models/GraphModel.java index 4af4b6c..50f63a8 100644 --- a/java/Graph Editor/src/models/GraphModel.java +++ b/java/Graph Editor/src/models/GraphModel.java @@ -1,9 +1,11 @@ package models; +import java.io.*; import java.util.List; import java.util.ArrayList; import java.util.Observable; import java.util.Observer; +import java.util.Scanner; public class GraphModel extends Observable implements Observer{ @@ -75,5 +77,71 @@ public class GraphModel extends Observable implements Observer{ this.setChanged(); } } + + public void writeToFile(File file) throws Exception + { + FileOutputStream a = new FileOutputStream(file); + FileOutputStream fos = new FileOutputStream(file); + OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8"); + + out.write(vertexes.size() + " " + edges.size() + "\r\n"); + for(int i=0; i < vertexes.size(); i++) + out.write(vertexes.get(i).getX() + " " + vertexes.get(i).getY() + " " + vertexes.get(i).getWidth() + " " + vertexes.get(i).getHeight() +" "+ vertexes.get(i).getName() + "\r\n"); + for(int i=0; i < edges.size(); i++) + out.write(vertexes.indexOf(edges.get(i).getEdge(false)) + " " + vertexes.indexOf(edges.get(i).getEdge(true)) + "\r\n"); + out.flush(); + out.close(); + fos.flush(); + fos.close(); + } + + public void writeToFile(String file) throws Exception + { + writeToFile(new File(file)); + } + + public static GraphModel fromFile(File file) throws Exception + { + GraphModel model = new GraphModel(); + Scanner scanner = new Scanner(file, "UTF-8"); + + int numKnopen = scanner.nextInt(); + int numKanten = scanner.nextInt(); + scanner.nextLine(); + + GraphVertex[] vertexes = new GraphVertex[numKanten]; + GraphEdge[] edges = new GraphEdge[numKanten]; + + for(int i=0; i < numKnopen; i++){ + int x = scanner.nextInt(); + int y = scanner.nextInt(); + int w = scanner.nextInt(); + int h = scanner.nextInt(); + String name = scanner.nextLine(); + + vertexes[i] = new GraphVertex(name); + vertexes[i].setX(x);vertexes[i].setY(y); + vertexes[i].setWidth(w);vertexes[i].setHeight(h); + } + for(int i=0; i < numKanten; i++){ + int obj1 = scanner.nextInt(); + int obj2 = scanner.nextInt(); + edges[i] = new GraphEdge(vertexes[obj1],vertexes[obj2]); + if(i != (numKanten-1)){ + scanner.nextLine(); + } + } + model.addEdge(edges); + model.addGraph(vertexes); + + return model; + } + + + public static GraphModel fromFile(String fileName) throws Exception + { + File file = new File(fileName); + return fromFile(file); + } } diff --git a/java/Graph Editor/src/models/GraphVertex.java b/java/Graph Editor/src/models/GraphVertex.java index b94d9c5..ebebaac 100644 --- a/java/Graph Editor/src/models/GraphVertex.java +++ b/java/Graph Editor/src/models/GraphVertex.java @@ -55,6 +55,11 @@ public class GraphVertex extends Observable{ shape.width = w; setChanged(); } + + public void setHeight(int h){ + shape.height = h; + setChanged(); + } public int getHeight(){ return shape.height; -- 2.11.4.GIT