1:/*
   2: * Programming graphicxal user interfaces
   3: * Example: Diagram
   4: * Jarkko Leponiemi 2002
   5: */
   6:
   7:import java.util.*;
   8:
   9:/**
  10: * A class representing the model of a simple diagram.
  11: */
  12:public class Diagram extends Observable
  13:{
  14:   // the nodes of the diagram
  15:   private List nodes;
  16:   // the connectors of the diagram
  17:   private List connectors;
  18:   
  19:   public Diagram() {
  20:      nodes = new ArrayList();
  21:      connectors = new ArrayList();
  22:   }
  23:   
  24:   // iterate over all nodes
  25:   public Iterator getNodes() { return nodes.iterator(); }
  26:   
  27:   // iterate over all connectors
  28:   public Iterator getConnectors() { return connectors.iterator(); }
  29:   
  30:   // create a new node
  31:   public Node createNode(String title, int x, int y)
  32:   {
  33:      Node n = new Node(title, x, y);
  34:      nodes.add(n);
  35:      setChanged();
  36:      notifyObservers(this);
  37:      return n;
  38:   }
  39:   
  40:   // create a new connector
  41:   public Connector createConnector(Node n1, Node n2) {
  42:      Connector c = new Connector(n1, n2);
  43:      connectors.add(c);
  44:      setChanged();
  45:      notifyObservers(this);
  46:      return c;
  47:   }
  48:   
  49:   // an inner class representing a diagram node
  50:   public class Node
  51:   {
  52:      private String title;
  53:      private int x, y;
  54:      
  55:      public Node(String title, int x, int y) {
  56:         this.title = title;
  57:         this.x = x;
  58:         this.y = y;
  59:      }
  60:      
  61:      public String getTitle() { return title; }
  62:      public void setTitle(String t) {
  63:         title = t;
  64:         setChanged();
  65:         notifyObservers(this);
  66:      }
  67:      
  68:      public int getX() { return x; }
  69:      public int getY() { return y; }
  70:      public void move(int x, int y) {
  71:         this.x = x;
  72:         this.y = y;
  73:         setChanged();
  74:         notifyObservers(this);
  75:      }
  76:   }
  77:   
  78:   // an inner class representing a connector between nodes
  79:   public class Connector
  80:   {
  81:      private Node node1, node2;
  82:      
  83:      public Connector(Node n1, Node n2) {
  84:         node1 = n1;
  85:         node2 = n2;
  86:      }
  87:      
  88:      public Node getNode1() { return node1; }
  89:      public Node getNode2() { return node2; }
  90:   }
  91:}