From 6296f27f8cc934d4c1727956529c41d5924c7a97 Mon Sep 17 00:00:00 2001 From: "s.feenstra" Date: Thu, 30 Sep 2010 20:59:54 +0200 Subject: [PATCH] Long functions splitted. Removed all println debug's. --- java/Graph Editor/src/app/Main.java | 16 ++++- .../src/controllers/SelectionController.java | 84 +++++++++++++--------- .../src/controllers/actions/DeleteNode.java | 4 +- .../src/controllers/actions/Rename.java | 1 - java/Graph Editor/src/interfaces/GraphFrame.java | 24 ++++--- java/Graph Editor/src/interfaces/GraphPanel.java | 5 +- java/Graph Editor/src/models/GraphModel.java | 42 +++++------ java/Graph Editor/src/models/GraphVertex.java | 10 +++ 8 files changed, 113 insertions(+), 73 deletions(-) diff --git a/java/Graph Editor/src/app/Main.java b/java/Graph Editor/src/app/Main.java index 9ff2e7c..dd82445 100644 --- a/java/Graph Editor/src/app/Main.java +++ b/java/Graph Editor/src/app/Main.java @@ -9,8 +9,18 @@ public class Main { private GraphFrame frame; private boolean twoWindows = false; - public Main(){ - model = new GraphModel(); + public Main(String[] args){ + if(args.length > 0){ + try{ + model = GraphModel.fromFile(args[0]); + }catch(Exception e){ + javax.swing.JOptionPane.showMessageDialog(null, "Error reading file!"); + model = new GraphModel(); + } + }else{ + model = new GraphModel(); + } + frame = new GraphFrame(model); new SelectionController(frame, model); @@ -25,7 +35,7 @@ public class Main { * @param args */ public static void main(String[] args) { - new Main(); + new Main(args); } } diff --git a/java/Graph Editor/src/controllers/SelectionController.java b/java/Graph Editor/src/controllers/SelectionController.java index d3b9dae..5ff9d55 100644 --- a/java/Graph Editor/src/controllers/SelectionController.java +++ b/java/Graph Editor/src/controllers/SelectionController.java @@ -17,9 +17,9 @@ import controllers.actions.Rename; public class SelectionController implements MouseListener,MouseMotionListener,KeyListener{ private GraphPanel object = null; - private GraphFrame frame; + private GraphFrame frame = null; private GraphVertex selectedObject = null; - private GraphModel model; + private GraphModel model = null; private String typedStr; private String originalName; @@ -81,16 +81,14 @@ public class SelectionController implements MouseListener,MouseMotionListener,Ke } public void setSelected(GraphVertex o) { - - if(isChangingName){ + if(isChangingName) this.doChangeName(); - } - if(selectedObject != null){ + if(selectedObject != null) selectedObject.setSelected(false); - } - if(o != null){ - selectedObject = o; + selectedObject = o; + + if(selectedObject != null){ o.setSelected(true); if(isAddingEdge){ finishAddingEdge(); @@ -147,14 +145,19 @@ public class SelectionController implements MouseListener,MouseMotionListener,Ke isChangingName = false; } + + private void handleDoubleClick() + { + this.typedStr = ""; + isChangingName = true; + originalName = selectedObject.getName(); + OriginalNameLength = selectedObject.getWidth(); + } + public void mouseClicked(MouseEvent arg0) { if(arg0.getButton() == MouseEvent.BUTTON1){ if(arg0.getClickCount() == 2){ - this.typedStr = ""; - isChangingName = true; - originalName = selectedObject.getName(); - OriginalNameLength = selectedObject.getWidth(); - System.out.println("DCLICK"); + handleDoubleClick(); }else{ this.setSelected(model.getGraphByCoordinate(arg0.getX(), arg0.getY())); } @@ -207,28 +210,43 @@ public class SelectionController implements MouseListener,MouseMotionListener,Ke this.mouseX = arg0.getX(); this.mouseY = arg0.getY(); if(this.isAddingEdge){ - System.out.println("moved+adding"); - this.object.repaint(); - //panel.repaint(); + this.object.repaint();// Repaint the line to the mouse cursor. + } + } + + private void setGraphText(GraphVertex o, String text){ + int width = 20 + this.getPanel().getDrawTextWidth(text); + o.setWidth(width); + o.setName(text); + } + + private void removeLastCharacterFromInput(){ + this.typedStr = this.typedStr.substring(0,this.typedStr.length()-1); + setGraphText(selectedObject,typedStr); + } + + private void handleKey(int key) + { + switch(key){ + case KeyEvent.VK_ENTER: + doChangeName(); + break; + case KeyEvent.VK_BACK_SPACE: + removeLastCharacterFromInput(); + break; + case KeyEvent.VK_ESCAPE: + doCancelNameChange(); + break; + default: + break; } } @Override public void keyPressed(KeyEvent arg0) { + int key = arg0.getKeyCode(); if(this.isChangingName && selectedObject != null){ - if(arg0.getKeyCode() == KeyEvent.VK_ENTER){ - doChangeName(); - }else if(arg0.getKeyCode() == KeyEvent.VK_ESCAPE){ - doCancelNameChange(); - }else if(arg0.getKeyCode()== KeyEvent.VK_BACK_SPACE){ - this.typedStr = this.typedStr.substring(0,this.typedStr.length()-1); - selectedObject.setName(typedStr); - if(this.OriginalNameLength > selectedObject.getWidth()){ - int width = 20 + this.getPanel().getDrawTextWidth(typedStr); - width = width < this.OriginalNameLength ? this.OriginalNameLength : width; - selectedObject.setWidth(width); - } - } + handleKey(key); } } @@ -250,11 +268,7 @@ public class SelectionController implements MouseListener,MouseMotionListener,Ke if(this.isChangingName && selectedObject != null && !arg0.isActionKey()){ if(arg0.getKeyChar() != KeyEvent.CHAR_UNDEFINED && acceptKeyForName(arg0.getKeyChar())){ this.typedStr += arg0.getKeyChar(); - System.out.println("Adding key"); - selectedObject.setName(typedStr); //SetName for PREVIEW reasons! - int width = 20 + this.getPanel().getDrawTextWidth(typedStr); - if(selectedObject.getWidth() < width) - selectedObject.setWidth(width); + setGraphText(selectedObject,typedStr); } } } diff --git a/java/Graph Editor/src/controllers/actions/DeleteNode.java b/java/Graph Editor/src/controllers/actions/DeleteNode.java index 07efad6..dd8a0b6 100644 --- a/java/Graph Editor/src/controllers/actions/DeleteNode.java +++ b/java/Graph Editor/src/controllers/actions/DeleteNode.java @@ -36,11 +36,11 @@ public class DeleteNode extends AbstractUndoableEdit { for(int i=0; i < arr.length; i++) if(arr[i].EdgeOf(o)) count++; - int idx=0; + count = 0; affectedEdges = new GraphEdge[count]; for(int i=0; i < arr.length; i++) if(arr[i].EdgeOf(o)) - affectedEdges[idx++] = arr[i]; + affectedEdges[count++] = arr[i]; } diff --git a/java/Graph Editor/src/controllers/actions/Rename.java b/java/Graph Editor/src/controllers/actions/Rename.java index 8efedc6..c45a33d 100644 --- a/java/Graph Editor/src/controllers/actions/Rename.java +++ b/java/Graph Editor/src/controllers/actions/Rename.java @@ -21,7 +21,6 @@ public class Rename extends AbstractUndoableEdit { public void redo() { - System.out.println("REDO - " + o.getName() + " -> " + savedName); int width = 20 + this.selectionController.getPanel().getDrawTextWidth(savedName); if(width > o.getWidth()) o.setWidth(width); diff --git a/java/Graph Editor/src/interfaces/GraphFrame.java b/java/Graph Editor/src/interfaces/GraphFrame.java index 1609bee..5442e66 100644 --- a/java/Graph Editor/src/interfaces/GraphFrame.java +++ b/java/Graph Editor/src/interfaces/GraphFrame.java @@ -158,14 +158,22 @@ public class GraphFrame extends JFrame { 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 ); - } + doOpenFile(fc.getSelectedFile()); + } + } + + + private void doOpenFile(File f) { + try { + GraphModel model = GraphModel.fromFile(f); + panel.setModel(model); + } catch (Exception e) { + JOptionPane + .showMessageDialog( + this, + "An error occured trying to read the input file. \r\n" + e + .getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); } } diff --git a/java/Graph Editor/src/interfaces/GraphPanel.java b/java/Graph Editor/src/interfaces/GraphPanel.java index 8174d84..f6451b7 100644 --- a/java/Graph Editor/src/interfaces/GraphPanel.java +++ b/java/Graph Editor/src/interfaces/GraphPanel.java @@ -47,6 +47,7 @@ public class GraphPanel extends JPanel implements Observer{ this.model = model; model.addObserver(this); selectionController.setModel(model); + repaint(); } public void addNode() @@ -85,7 +86,6 @@ public class GraphPanel extends JPanel implements Observer{ public void paintComponent(Graphics g) { - System.out.println("paintComponent"); g.setColor(this.getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); @@ -131,10 +131,7 @@ public class GraphPanel extends JPanel implements Observer{ @Override public void update(Observable arg0, Object arg1) { if(arg0 == model){ - System.out.println("Refresh @ graphpanel" + arg0.toString()); repaint(); - }else{ - System.out.println("FOUT: onbekend object riep GraphPanel.update aan: " + arg0.toString()); } } } diff --git a/java/Graph Editor/src/models/GraphModel.java b/java/Graph Editor/src/models/GraphModel.java index 50f63a8..9b2097a 100644 --- a/java/Graph Editor/src/models/GraphModel.java +++ b/java/Graph Editor/src/models/GraphModel.java @@ -80,7 +80,6 @@ public class GraphModel extends Observable implements Observer{ public void writeToFile(File file) throws Exception { - FileOutputStream a = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8"); @@ -100,6 +99,15 @@ public class GraphModel extends Observable implements Observer{ writeToFile(new File(file)); } + private static GraphVertex[] readVertexes(int count, Scanner s) + { + GraphVertex[] vertexes = new GraphVertex[count]; + for(int i=0; i < count; i++) + vertexes[i] = new GraphVertex(s); + return vertexes; + } + + public static GraphModel fromFile(File file) throws Exception { GraphModel model = new GraphModel(); @@ -109,32 +117,26 @@ public class GraphModel extends Observable implements Observer{ int numKanten = scanner.nextInt(); scanner.nextLine(); - GraphVertex[] vertexes = new GraphVertex[numKanten]; - GraphEdge[] edges = new GraphEdge[numKanten]; + GraphVertex[] vertexes = readVertexes(numKnopen,scanner); + GraphEdge[] edges = readKanten(scanner, numKanten, vertexes); + + model.addEdge(edges); + model.addGraph(vertexes); - 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++){ + return model; + } + private static GraphEdge[] readKanten(Scanner scanner, int numKanten, + GraphVertex[] vertexes) { + GraphEdge[] edges = new GraphEdge[numKanten]; + for (int i = 0; i < numKanten; i++) { int obj1 = scanner.nextInt(); int obj2 = scanner.nextInt(); - edges[i] = new GraphEdge(vertexes[obj1],vertexes[obj2]); + edges[i] = new GraphEdge(vertexes[obj1], vertexes[obj2]); if(i != (numKanten-1)){ scanner.nextLine(); } } - model.addEdge(edges); - model.addGraph(vertexes); - - return model; + return edges; } diff --git a/java/Graph Editor/src/models/GraphVertex.java b/java/Graph Editor/src/models/GraphVertex.java index ebebaac..0ce9958 100644 --- a/java/Graph Editor/src/models/GraphVertex.java +++ b/java/Graph Editor/src/models/GraphVertex.java @@ -2,6 +2,7 @@ package models; import java.awt.Rectangle; import java.util.Observable; +import java.util.Scanner; public class GraphVertex extends Observable{ private String name; @@ -20,6 +21,15 @@ public class GraphVertex extends Observable{ shape.width = 100; } + public GraphVertex(Scanner s){ + shape = new Rectangle(); + this.shape.x = s.nextInt(); + this.shape.y = s.nextInt(); + this.shape.width = s.nextInt(); + this.shape.height = s.nextInt(); + this.name = s.nextLine(); + } + public GraphVertex(){ this("default."); } -- 2.11.4.GIT