ObjectEditor.java

package algorithmen.umleditor;

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

/**
 * Editor für ein Object.
 */
public class ObjectEditor extends Editor {
  
  protected JTextField nameField;
  protected JTextField typeField;
  protected Vector fieldsClone;
  
  public ObjectEditor(Object o, Frame p) {
    super(o, 460, 300, p);
  }
  
  public ObjectEditor(Object o, Dialog p) {
    super(o, 460, 300, p);
  }
  
  protected void buildMainPanel(JPanel mp) {
    mp.setLayout(new BoxLayout(mp, BoxLayout.Y_AXIS));
    Object o = (Object) model;
    
    JPanel topPanel = new JPanel(new GridLayout(1, 2));
    
    // add name field
    JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    namePanel.add(new JLabel("Name:"));
    nameField = new JTextField(o.getName(), 15);
    namePanel.add(nameField);
    
    // add type field
    JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    typePanel.add(new JLabel("Typ:"));
    typeField = new JTextField(o.getType(), 15);
    typePanel.add(typeField);
    
    // Listeditorpanel bekommt eine (tiefe!) Kopie der Liste, damit beim
    // Abbrechen alle Änderungen verschwinden.
    fieldsClone = (Vector) o.getFields().clone();  // leider flacher Klon!
    for (int i = 0; i < fieldsClone.size(); i++) {
      fieldsClone.set(i, ((Field) fieldsClone.get(i)).clone());
    }
    
    // alles zusammenbauen
    topPanel.add(namePanel);
    topPanel.add(typePanel);
    mp.add(topPanel);
    mp.add(new ListEditorPanel(fieldsClone, "Felder", new FieldView(), this));
    
  }
  
  protected void update() {
    Object o = (Object) model;
    o.setName(nameField.getText());
    o.setType(typeField.getText());
    o.setFields(fieldsClone);  
  }
  
}