MethodEditor.java

package algorithmen.umleditor;

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

/**
 * Editor für eine Methode.
 */
public class MethodEditor extends Editor {
  
  protected JTextField nameField;
  protected JTextField typeField;
  protected JCheckBox staticField;
  protected JCheckBox abstractField;
  protected JComboBox accessField;
  protected Vector fieldsClone;
  
  public MethodEditor(Method m, Frame p) {
    super(m, 460, 300, p);
  }
  
  public MethodEditor(Method m, Dialog p) {
    super(m, 460, 300, p);
  }
  
  protected void update() {
    Method m = (Method) model;
    m.setName(nameField.getText());
    m.setStatic(staticField.isSelected());
    m.setAbstract(abstractField.isSelected());
    m.setType(typeField.getText());
    m.setVisibility((Visibility) accessField.getSelectedItem());
    m.setParameter(fieldsClone);
  }
  
  protected void buildMainPanel(JPanel mp) {
    mp.setLayout(new BoxLayout(mp, BoxLayout.Y_AXIS));
    Method m = (Method) model;
    
    // topPanel mit 4 kleinen Feldern
    JPanel topPanel = new JPanel(new GridLayout(2, 2));
    
    // add name field
    JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    namePanel.add(new JLabel("Name:"));
    nameField = new JTextField(m.getName(), 15);
    namePanel.add(nameField);
    
    // add static/abstract field
    JPanel staticPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    staticPanel.add(new JLabel("statisch:"));
    staticField = new JCheckBox();
    staticField.setSelected(m.isStatic());
    staticPanel.add(staticField);
    staticPanel.add(new JLabel("abstrakt:"));
    abstractField = new JCheckBox();
    abstractField.setSelected(m.isAbstract());
    staticPanel.add(abstractField);
    
    // add type field
    JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    typePanel.add(new JLabel("Typ:"));
    typeField = new JTextField(m.getType(), 15);
    typePanel.add(typeField);
    
    // add access field
    JPanel accessPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    accessPanel.add(new JLabel("Zugriffsklasse:"));
    accessField = new JComboBox(Visibility.getVisibilities());
    accessField.setSelectedItem(m.getVisibility());
    accessPanel.add(accessField);
    
    // Listeditorpanel bekommt eine (tiefe!) Kopie der Liste, damit beim
    // Abbrechen alle Änderungen verschwinden.
    fieldsClone = (Vector) m.getParameter().clone();  // leider flacher Klon!
    for (int i = 0; i < fieldsClone.size(); i++) {
      fieldsClone.set(i, ((Field)fieldsClone.get(i)).clone());
    }
    
    // alle Teile zusammenbauen
    topPanel.add(namePanel);
    topPanel.add(staticPanel);
    topPanel.add(typePanel);
    topPanel.add(accessPanel);
    mp.add(topPanel);
    mp.add(new ListEditorPanel(fieldsClone, "Felder", new FieldView(), this));
  }
}