
import java.awt.*;
import java.awt.event.*;

/** Dialog, der zur Breite-/Hoehen-Eingabe auffordert -> für neues Bild */
public class TNewDrawBox extends Dialog {

 /** Die vom User gewählte, neue Bild-Breite */
 public long NewWidth;
 /** Die vom User gewählte, neue Bild-Höhe */
 public long NewHeight;
 /** Liefert Zurück, ob OK = 1 oder Abbrechen = -1 */
 public int result;

 TextField xmaxField = null; // Nehmen die Größenangaben auf
 TextField ymaxField = null;

 /** Constructor, nimmt Titel und Ausgabestring entgegen 
   * @param String Titel der MessageBox
   * @param String Ausgabestring der Messagebox 
   */
 public TNewDrawBox () {
    super (new Frame(), "Bildgröße wählen", true); // modalen Dialog mit Titel title erzeugen

   // Action-Listener für Button definieren
    class NewDrawBoxListener implements ActionListener {
      // Event-Handler für Komponenten-Events
       public void actionPerformed( ActionEvent e ) {
          if (e.getActionCommand().equals("OK")) { // OK-Button gedrückt
             long TempWidth = 0;  long TempHeight = 0;
             try {
                TempWidth = new Integer(xmaxField.getText()).intValue();
             } catch (NumberFormatException ex) { xmaxField.setText ("0"); return; }
             try {
                TempHeight = new Integer(ymaxField.getText()).intValue();
             } catch (NumberFormatException ex) { ymaxField.setText ("0"); return; }

             NewWidth = TempWidth;  NewHeight = TempHeight; // Rückgabewerte
             result=1;
             setVisible (false); // Dialog verschwinden lassen...
          }
          if (e.getActionCommand().equals("Abbrechen")) { // OK-Button gedrückt
             result=-1;
             setVisible (false); // Dialog verschwinden lassen...
          }
       } 
    }
    NewDrawBoxListener globalListener = new NewDrawBoxListener();
    
   // Window-Listener für Schließen-Button definieren
    WindowListener exitListener = new WindowAdapter()
    {
      // Event-Methode die beim Schließen aufgerufen wird
       public void windowClosing (WindowEvent ThisEvent) {
          result=-1;
          setVisible (false); // Dialog verschwinden lassen...
       }
    };
    addWindowListener(exitListener); // Event-Handling aktiv.

   // InfoText-Label und Button plazieren
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constr = new GridBagConstraints();
    constr.fill = GridBagConstraints.BOTH;
    setLayout (layout);

   // Text-Label und Leerzeilen plazieren
     constr.gridwidth=GridBagConstraints.REMAINDER;  
     Label titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label ("Bitte geben Sie die Größe der neues Bildes ein:" , Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);

   // EIngabefelder für Breite und Höhe plazieren
     constr.gridwidth=1;  constr.weightx=1.0;
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label ("Breite:", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     xmaxField = new TextField (new Long(NewWidth).toString(), 5); 
     layout.setConstraints (xmaxField, constr);
    add (xmaxField);
     constr.gridwidth=GridBagConstraints.REMAINDER;     
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);

     constr.gridwidth=1;  constr.weightx=1.0;
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label ("Höhe:", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);
     ymaxField = new TextField (new Long(NewHeight).toString(), 5); 
     layout.setConstraints (ymaxField, constr);
    add (ymaxField);
     constr.gridwidth=GridBagConstraints.REMAINDER;     
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);

     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);     

   // OK- und Abbrechen-Button plazieren:
     constr.gridwidth=1;  constr.weightx=1.0;
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);     
     Button plotButton = new Button ("OK"); 
     plotButton.addActionListener (globalListener); 
     layout.setConstraints (plotButton, constr);
    add (plotButton);    
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);     
     plotButton = new Button ("Abbrechen"); 
     plotButton.addActionListener (globalListener); 
     layout.setConstraints (plotButton, constr);
    add (plotButton);    
     constr.gridwidth=GridBagConstraints.REMAINDER;
     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);     

     titleLabel = new Label (" ", Label.CENTER);
     layout.setConstraints (titleLabel, constr);
    add (titleLabel);     

   // Komponenten "zusammenpacken" und Frame anzeigen
    pack();
    setSize (getPreferredSize());
    // show();
 }

 /** Setzt Hoehe und Breite Standardwerte  
   * @param long Breite - Standardwert
   * @param long Hoehe - Standardwert
   */ 
 public void setPreset (long width, long height) {
    NewWidth = width;  NewHeight = height;
    xmaxField.setText ( new Long(width).toString() );
    ymaxField.setText ( new Long(height).toString() );
 }

}