FieldView.java

package algorithmen.umleditor;

import java.awt.*;
import javax.swing.*;

/**
 * View eines Datenfelds.
 * Zuständig für die graphische Darstellung der Felddaten.
 */
public class FieldView extends View {
  
  /**
   * Erzeugt einen View für das Field model an der Position p.
   */
  public FieldView(Field model, Point p) {
    super(model, p);
  }
  
  /**
   * Erzeugt einen Standard-View.
   */
  public FieldView() {
    this(new Field(), new Point(10, 10));
  }
  
  /**
   * Zeichnet das Datenfeld als einfachen String in UML-Notation.
   * Mögliche Weiterentwicklung: Zeilenumbrüche im String.
   */
  public void draw(Graphics g) {
    String s = toUmlString();
    size.width = getStringWidth(s, g);
    size.height = getStringHeight(g);
    g.drawString(s, pos.x, pos.y + size.height);
    
    // underline, if static
    if (((Field) model).isStatic()) {
      int yLine = pos.y + size.height + 1;
      g.drawLine(pos.x, yLine, pos.x + size.width, yLine);
    }
  }
  
  /**
   * Returns String representation of a Field in UML form.
   * Typical examples:
   *    "#alter: int"
   *    "+PI: double"
   */
  public String toUmlString() {
    Field f = (Field) model;
    String s = f.getVisibility().getUmlSymbol();
    s += f.getName() + ": " + f.getType();
    return s;
  }
  
  public Editor createEditor(UmlElement m, Frame p) {
    return new FieldEditor((Field) m, p);
  }
  
  public Editor createEditor(UmlElement m, Dialog p) {
    return new FieldEditor((Field) m, p);
  }
  
  public UmlElement createStandardObject() {
    return new Field();
  }
  
}