1|package com.infocom.print;
  2|
  3|/**
  4| * Class: PFPageSetupDialog <p>
  5| *
  6| * @author Jean-Pierre Dube <jpdube@videotron.ca>
  7| * @version 1.0
  8| * @since 1.0
  9| * @see JDialog
 10| * @see ActionListener
 11| */
 12|
 13|import javax.swing.JDialog;
 14|import javax.swing.JPanel;
 15|import java.awt.BorderLayout;
 16|import java.awt.FlowLayout;
 17|import javax.swing.JLabel;
 18|import javax.swing.JComboBox;
 19|import javax.swing.JTextField;
 20|import javax.swing.JFrame;
 21|import java.awt.Dimension;
 22|import java.awt.event.ActionListener;
 23|import javax.swing.JButton;
 24|import java.awt.event.ActionEvent;
 25|
 26|
 27|public class PFPageSetupDialog extends JDialog implements ActionListener {
 28|
 29|   //--- Private instances declarations
 30|   private PFPageFormat pageFormat = new PFPageFormat ();
 31|
 32|   //--- Panels
 33|   private FormPanel fieldPanel = new FormPanel (5, 5, 5, 5);
 34|   private JPanel buttonPanel = new JPanel ();
 35|
 36|   //--- Layouts
 37|   private BorderLayout dialogLayout = new BorderLayout ();
 38|   private FlowLayout buttonLayout = new FlowLayout ();
 39|
 40|   //--- Labels
 41|   private JLabel labelPaperSize = new JLabel ("Paper size:");
 42|   private JLabel labelPaperSource = new JLabel ("Paper source:");
 43|   private JLabel labelLeftMargin = new JLabel ("Left margin:");
 44|   private JLabel labelRightMargin = new JLabel ("Right margin:");
 45|   private JLabel labelTopMargin = new JLabel ("Top margin:");
 46|   private JLabel labelBottomMargin = new JLabel ("Bottom margin:");
 47|   private JLabel labelPageOrientation = new JLabel ("Page orientation");
 48|
 49|   //--- Field
 50|   private JComboBox comboPaperSize;
 51|   private JComboBox comboPaperSource = new JComboBox ();
 52|   private JTextField textLeftMargin = new JTextField (10);
 53|   private JTextField textRightMargin = new JTextField (10);
 54|   private JTextField textTopMargin = new JTextField (10);
 55|   private JTextField textBottomMargin = new JTextField (10);
 56|   private JComboBox comboPageOrientation;
 57|
 58|   //--- Buttons
 59|   private JButton buttonAccept = new JButton ("Accept");
 60|   private JButton buttonCancel = new JButton ("Cancel");
 61|
 62|
 63|
 64|   /**
 65|    * Constructor: PFPageSetup <p>
 66|    *
 67|    */
 68|   public PFPageSetupDialog (PFPageFormat parPageFormat) {
 69|
 70|      super (new JFrame (), "Page dialog", true);
 71|
 72|      if (parPageFormat != null)
 73|         pageFormat = parPageFormat;
 74|
 75|      init ();
 76|   }
 77|
 78|
 79|   /**
 80|    * Method: init <p>
 81|    *
 82|    * Initialise this dialog box, panels and fields
 83|    *
 84|    */
 85|   private void init () {
 86|
 87|      //--- Init this dialog
 88|      this.getContentPane ().setLayout (dialogLayout);
 89|      this.getContentPane ().add (fieldPanel, BorderLayout.CENTER);
 90|      this.getContentPane ().add (buttonPanel, BorderLayout.SOUTH);
 91|      this.setSize (new Dimension (500, 300));
 92|
 93|      //--- Init the field
 94|      setFields ();
 95|      fieldPanel.add (labelPaperSize, comboPaperSize, 0, 0);
 96|      fieldPanel.add (labelPaperSource, comboPaperSource, 0, 1);
 97|      fieldPanel.add (labelLeftMargin, textLeftMargin, 2, 0);
 98|      fieldPanel.add (labelRightMargin, textRightMargin, 2, 1);
 99|      fieldPanel.add (labelTopMargin, textTopMargin, 3, 0);
100|      fieldPanel.add (labelBottomMargin, textBottomMargin, 3, 1);
101|
102|      //--- Disable the paper source field
103|      comboPaperSource.setEnabled (false);
104|
105|      //--- Init buttons
106|      buttonPanel.add (buttonAccept);
107|      buttonPanel.add (buttonCancel);
108|
109|      buttonAccept.setActionCommand ("Accept");
110|      buttonCancel.setActionCommand ("Cancel");
111|
112|      buttonAccept.addActionListener (this);
113|      buttonCancel.addActionListener (this);
114|   }
115|
116|
117|   /**
118|    * Method: setFields <p>
119|    *
120|    * This method will set the field from the pageFormat object
121|    * to the display fields.<p>
122|    *
123|    */
124|   private void setFields () {
125|
126|      comboPaperSize = new JComboBox (pageFormat.getPageSizeDefinition ());
127|      comboPaperSize.setSelectedIndex (pageFormat.getPageSizeIndex ());
128|      textLeftMargin.setText (pageFormat.getLeftMargin ().toString ());
129|      textRightMargin.setText (pageFormat.getRightMargin ().toString ());
130|      textTopMargin.setText (pageFormat.getTopMargin ().toString ());
131|      textBottomMargin.setText (pageFormat.getBottomMargin ().toString ());
132|      comboPageOrientation = new JComboBox (pageFormat.getPageOrientationDefinition ());
133|      comboPageOrientation.setSelectedIndex (pageFormat.getPageOrientation ());
134|   }
135|
136|
137|   /**
138|    * Method: getFields <p>
139|    *
140|    * Set the pageFormat properties from the user settings<p>
141|    *
142|    */
143|   public void getFields () {
144|
145|      pageFormat.setPageSize (comboPaperSize.getSelectedIndex ());
146|      pageFormat.setLeftMargin (new PFInchUnit (new Double (textLeftMargin.getText ()).doubleValue ()));
147|      pageFormat.setRightMargin (new PFInchUnit (new Double (textRightMargin.getText ()).doubleValue ()));
148|      pageFormat.setTopMargin (new PFInchUnit (new Double (textTopMargin.getText ()).doubleValue ()));
149|      pageFormat.setBottomMargin (new PFInchUnit (new Double (textBottomMargin.getText ()).doubleValue ()));
150|      pageFormat.setPageOrientation (comboPageOrientation.getSelectedIndex ());
151|   }
152|
153|
154|
155|   /**
156|    * Method: showDialog <p>
157|    *
158|    * Show this dialog and return the pageFormat settings
159|    * made by the user. Note that <code>pageFormat</code> propeties
160|    * are set in the <code>ActionListener</code> <code>actionPerformaed</code> method<p>
161|    *
162|    * @return a value of type PFPageFormat
163|    */
164|   public PFPageFormat showDialog () {
165|
166|      this.pack ();
167|      this.show ();
168|      return (pageFormat);
169|   }
170|
171|
172|   /**
173|    * Method: actionPerformed <p>
174|    *
175|    * Listen to button click events. If the "ok" button is clicked,
176|    * the <code>getFields ()</code> method is called to save the
177|    * user selection in the <code>pageFormat</code> object. Else the
178|    * dialog is disposed.
179|    *
180|    * @param parEvent a value of type ActionEvent
181|    */
182|   public void actionPerformed (ActionEvent parEvent) {
183|
184|      String actionCommand = parEvent.getActionCommand ();
185|
186|      if (actionCommand.equals ("Accept")) {
187|         getFields ();
188|         this.dispose ();
189|      }
190|      else {
191|         this.dispose ();
192|      }
193|   }
194|}// PFPageSetupDialog