Editor.java

package algorithmen.umleditor;

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

/**
 * Base class of all editors.
 * It provides a main panel and a standard button panel for closing
 * with or without updating its model object.
 */
abstract public class Editor extends JDialog {
  protected UmlElement model;
    
  /**
   * Erzeugt einen Editor der Größe xSize x ySize für ein UmlElement o,
   * der modal ist bzgl. dem Jdialog parent.
   */
  public Editor(UmlElement o, int xSize, int ySize, Dialog parent) {
    super(parent, true);
    initEditor(o, xSize, ySize);
  }
  
  /**
   * Erzeugt einen Editor der Größe xSize x ySize für ein UmlElement o,
   * der modal ist bzgl. dem JFrame parent.
   */
  public Editor(UmlElement o, int xSize, int ySize, Frame parent) {
    super(parent, true);
    initEditor(o, xSize, ySize);  
  }

  /**
   * gemeinsamer Teil der Konstruktoren
   */
  protected void initEditor(UmlElement o, int xSize, int ySize) {
    model = o;
    
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    
    // Haupt-Panel, wird von den Kindern gefüllt
    JPanel mainPanel = new JPanel();
    buildMainPanel(mainPanel);
    cp.add(mainPanel, BorderLayout.CENTER);
    
    // erzeuge Panel mit Buttons zum Abbrechen und Übernehmen
    JPanel buttonPanel = new JPanel();
    JButton noButton = new JButton("Abbrechen");
    noButton.addActionListener(new NoButtonListener());
    buttonPanel.add(noButton);
    JButton okButton = new JButton("Übernehmen");
    okButton.addActionListener(new OkButtonListener());
    buttonPanel.add(okButton);
    cp.add(buttonPanel, BorderLayout.SOUTH);
    
    setSize(xSize, ySize);
    setVisible(true);
  }
  
  /**
   * Aufbau des Haupt-Editor-Panels.
   */
  abstract protected void buildMainPanel(JPanel mp);
  
  /**
   * Übernahme der Editorwerte an die Model-Klasse
   */
  abstract protected void update();  
  
  class NoButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      dispose();
    }
  }
  
  class OkButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      update();
      dispose();
    }
  }
}