/*
* PageFormatPanel.java
*
* Created on April 20, 2007, 12:29 PM
*/
package ika.gui;
import ika.utils.MathUtils;
import java.awt.event.ItemEvent;
import java.awt.geom.Rectangle2D;
/**
* A panel that lets the user enter a width and height in millimeters.
* Used by PageFormatDialog.
* @author Bernhard Jenny, Institute of Cartography, ETH Zurich.
*/
public class PageFormatPanel extends javax.swing.JPanel {
public static final double A4WIDTH = 210;
public static final double A4HEIGHT = 297;
public static final double A3WIDTH = 297;
public static final double A3HEIGHT = 420;
public static final double A2WIDTH = 420;
public static final double A2HEIGHT = 594;
public static final double LETTERWIDTH = 215.9;
public static final double LETTERHEIGHT = 279.4;
public static final double LEGALWIDTH = 215.9;
public static final double LEGALHEIGHT = 355.6;
private PageFormat pageFormat = new PageFormat();
private boolean updating = false;
private static final double TOL = 0.001;
/** Creates new form PageFormatPanel */
public PageFormatPanel() {
initComponents();
// update GUI
this.updateScaleField();
this.updateSizeComboBox();
this.updateSizeFields();
this.updateUnitLabels();
this.updateUnitButtons();
this.updatePositionFields();
this.updateAutomatic();
}
public void setShowScale(boolean showScale) {
mainScalePanel.setVisible(showScale);
this.validate();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
unitsButtonGroup = new javax.swing.ButtonGroup();
orientationButtonGroup = new javax.swing.ButtonGroup();
widthLabel = new javax.swing.JLabel();
heightLabel = new javax.swing.JLabel();
sizeComboBox = new javax.swing.JComboBox();
sizeLabel = new javax.swing.JLabel();
widthUnitLabel = new javax.swing.JLabel();
heightUnitLabel = new javax.swing.JLabel();
unitsLabel = new javax.swing.JLabel();
pxRadioButton = new javax.swing.JRadioButton();
mmRadioButton = new javax.swing.JRadioButton();
mapDocumentInfoTextArea = new javax.swing.JTextArea();
orientationLabel = new javax.swing.JLabel();
portraitRadioButton = new javax.swing.JRadioButton();
landscapeRadioButton = new javax.swing.JRadioButton();
documentSizeLabel = new javax.swing.JLabel();
documentPositionLabel = new javax.swing.JLabel();
leftLabel = new javax.swing.JLabel();
bottomLabel = new javax.swing.JLabel();
automaticCheckBox = new javax.swing.JCheckBox();
positionInfoTextArea = new javax.swing.JTextArea();
mainScalePanel = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
scalePanel = new javax.swing.JPanel();
_1toLabel = new javax.swing.JLabel();
scaleField = new javax.swing.JFormattedTextField();
mapScaleLabel = new javax.swing.JLabel();
mapScaleInfoTextArea2 = new javax.swing.JTextArea();
widthField = new javax.swing.JFormattedTextField();
heightField = new javax.swing.JFormattedTextField();
leftField = new javax.swing.JFormattedTextField();
bottomField = new javax.swing.JFormattedTextField();
setLayout(new java.awt.GridBagLayout());
widthLabel.setText("Width:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 7;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(widthLabel, gridBagConstraints);
heightLabel.setText("Height:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(heightLabel, gridBagConstraints);
sizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Custom", "A4", "A3", "A2", "Letter", "Legal" }));
sizeComboBox.setMinimumSize(new java.awt.Dimension(80, 27));
sizeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
sizeComboBoxItemStateChanged(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 6;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0);
add(sizeComboBox, gridBagConstraints);
sizeLabel.setText("Size:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 6;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(sizeLabel, gridBagConstraints);
widthUnitLabel.setText("px");
widthUnitLabel.setMaximumSize(new java.awt.Dimension(30, 16));
widthUnitLabel.setMinimumSize(new java.awt.Dimension(30, 16));
widthUnitLabel.setPreferredSize(new java.awt.Dimension(30, 16));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 7;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
add(widthUnitLabel, gridBagConstraints);
heightUnitLabel.setText("px");
heightUnitLabel.setMaximumSize(new java.awt.Dimension(30, 16));
heightUnitLabel.setMinimumSize(new java.awt.Dimension(30, 16));
heightUnitLabel.setPreferredSize(new java.awt.Dimension(30, 16));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
add(heightUnitLabel, gridBagConstraints);
unitsLabel.setText("Units:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 9;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(unitsLabel, gridBagConstraints);
unitsButtonGroup.add(pxRadioButton);
pxRadioButton.setSelected(true);
pxRadioButton.setText("Pixels");
pxRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
pxRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
unitRadioButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 9;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(5, 10, 3, 0);
add(pxRadioButton, gridBagConstraints);
unitsButtonGroup.add(mmRadioButton);
mmRadioButton.setText("Millimeter");
mmRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
mmRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
unitRadioButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 10;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(3, 10, 10, 0);
add(mmRadioButton, gridBagConstraints);
mapDocumentInfoTextArea.setColumns(20);
mapDocumentInfoTextArea.setEditable(false);
mapDocumentInfoTextArea.setFont(new java.awt.Font("Lucida Grande", 0, 10));
mapDocumentInfoTextArea.setLineWrap(true);
mapDocumentInfoTextArea.setRows(3);
mapDocumentInfoTextArea.setText("The map document size and position are used when saving the map to a graphics file format that is not georeferenced. When set to Automatic, the size and position of the map document is adjusted to include the whole map.");
mapDocumentInfoTextArea.setWrapStyleWord(true);
mapDocumentInfoTextArea.setFocusable(false);
mapDocumentInfoTextArea.setOpaque(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
add(mapDocumentInfoTextArea, gridBagConstraints);
orientationLabel.setText("Orientation:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 11;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(orientationLabel, gridBagConstraints);
orientationButtonGroup.add(portraitRadioButton);
portraitRadioButton.setSelected(true);
portraitRadioButton.setText("Portrait");
portraitRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
portraitRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
orientationRadioButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 11;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(5, 10, 3, 0);
add(portraitRadioButton, gridBagConstraints);
orientationButtonGroup.add(landscapeRadioButton);
landscapeRadioButton.setText("Landscape");
landscapeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
landscapeRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
orientationRadioButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 12;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
add(landscapeRadioButton, gridBagConstraints);
documentSizeLabel.setText("Map Document Size");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.ipadx = 20;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(25, 0, 0, 0);
add(documentSizeLabel, gridBagConstraints);
documentPositionLabel.setText("Map Document Position");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 13;
gridBagConstraints.ipadx = 20;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(25, 0, 0, 0);
add(documentPositionLabel, gridBagConstraints);
leftLabel.setText("Left Border:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 14;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(leftLabel, gridBagConstraints);
bottomLabel.setText("Bottom Border:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(bottomLabel, gridBagConstraints);
automaticCheckBox.setText("Automatic Map Document Size and Position");
automaticCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
automaticCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
automaticCheckBoxActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
add(automaticCheckBox, gridBagConstraints);
positionInfoTextArea.setColumns(20);
positionInfoTextArea.setEditable(false);
positionInfoTextArea.setFont(new java.awt.Font("Lucida Grande", 0, 10));
positionInfoTextArea.setLineWrap(true);
positionInfoTextArea.setRows(2);
positionInfoTextArea.setText("Enter the map document position in georeferencing map units, not millimeter or pixels.");
positionInfoTextArea.setWrapStyleWord(true);
positionInfoTextArea.setFocusable(false);
positionInfoTextArea.setOpaque(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 16;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
add(positionInfoTextArea, gridBagConstraints);
mainScalePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
jPanel2.setPreferredSize(new java.awt.Dimension(219, 116));
jPanel2.setLayout(new java.awt.GridBagLayout());
scalePanel.setLayout(new java.awt.GridBagLayout());
_1toLabel.setText("1:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
scalePanel.add(_1toLabel, gridBagConstraints);
scaleField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.###"))));
scaleField.setMinimumSize(new java.awt.Dimension(120, 28));
scaleField.setPreferredSize(new java.awt.Dimension(120, 28));
scaleField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
scaleFieldPropertyChange(evt);
}
});
scalePanel.add(scaleField, new java.awt.GridBagConstraints());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
jPanel2.add(scalePanel, gridBagConstraints);
mapScaleLabel.setText("Map Scale");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.ipadx = 20;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
jPanel2.add(mapScaleLabel, gridBagConstraints);
mapScaleInfoTextArea2.setColumns(20);
mapScaleInfoTextArea2.setEditable(false);
mapScaleInfoTextArea2.setFont(new java.awt.Font("Lucida Grande", 0, 10));
mapScaleInfoTextArea2.setLineWrap(true);
mapScaleInfoTextArea2.setRows(4);
mapScaleInfoTextArea2.setText("The map scale is essential for correct line widths, and also determines the extension of the area covered by the map document.");
mapScaleInfoTextArea2.setWrapStyleWord(true);
mapScaleInfoTextArea2.setFocusable(false);
mapScaleInfoTextArea2.setOpaque(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
jPanel2.add(mapScaleInfoTextArea2, gridBagConstraints);
mainScalePanel.add(jPanel2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
add(mainScalePanel, gridBagConstraints);
widthField.setText("0.0");
widthField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
sizeFieldPropertyChange(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 7;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
add(widthField, gridBagConstraints);
heightField.setText("0.0");
heightField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
sizeFieldPropertyChange(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 8;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
add(heightField, gridBagConstraints);
leftField.setText("0.0");
leftField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
dimensionFieldPropertyChange(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 14;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
add(leftField, gridBagConstraints);
bottomField.setText("0.0");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 15;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
add(bottomField, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void automaticCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_automaticCheckBoxActionPerformed
if (this.updating)
return;
this.readAutomatic();
this.updateAutomatic();
this.updatePositionFields();
this.updateSizeFields();
this.updateSizeComboBox();
}//GEN-LAST:event_automaticCheckBoxActionPerformed
private void orientationRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orientationRadioButtonActionPerformed
this.readOrientation();
this.updateSizeFields();
}//GEN-LAST:event_orientationRadioButtonActionPerformed
private void unitRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unitRadioButtonActionPerformed
this.readUnits();
this.updateUnitLabels();
this.updateSizeFields();
}//GEN-LAST:event_unitRadioButtonActionPerformed
private void sizeComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_sizeComboBoxItemStateChanged
if (evt.getStateChange() == ItemEvent.SELECTED) {
this.readSizePopup();
this.updateSizeFields();
}
}//GEN-LAST:event_sizeComboBoxItemStateChanged
private void scaleFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_scaleFieldPropertyChange
Rectangle2D bounds = this.pageFormat.getPageSizeWorldCoordinates();
this.readScale();
if (this.pageFormat.isAutomatic()) {
this.pageFormat.setPageWorldCoordinates(bounds);
this.updateSizeFields();
this.updateSizeComboBox();
}
}//GEN-LAST:event_scaleFieldPropertyChange
private void sizeFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_sizeFieldPropertyChange
this.readSizeField();
this.updateSizeComboBox();
}//GEN-LAST:event_sizeFieldPropertyChange
private void dimensionFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dimensionFieldPropertyChange
this.readPosition();
}//GEN-LAST:event_dimensionFieldPropertyChange
private void readScale() {
if (this.updating)
return;
this.updating = true;
try {
double scale = ((Number)(this.scaleField.getValue())).doubleValue();
this.pageFormat.setPageScale(scale);
} finally {
this.updating = false;
}
}
/**
* read the size popup-menu
*/
private void readSizePopup() {
if (this.updating)
return;
this.updating = true;
try {
String formatStr = this.sizeComboBox.getSelectedItem().toString();
double w = Double.NaN;
double h = Double.NaN;
if ("A4".equals(formatStr)) {
w = A4WIDTH;
h = A4HEIGHT;
} else if ("A3".equals(formatStr)) {
w = A3WIDTH;
h = A3HEIGHT;
} else if ("A2".equals(formatStr)) {
w = A2WIDTH;
h = A2HEIGHT;
} else if ("Letter".equals(formatStr)) {
w = LETTERWIDTH;
h = LETTERHEIGHT;
} else if ("Legal".equals(formatStr)) {
w = LEGALWIDTH;
h = LEGALHEIGHT;
} else {
return;
}
if (this.pageFormat.isUnitPixels()) {
w *= PageFormat.MM2PX;
h *= PageFormat.MM2PX;
}
if (this.landscapeRadioButton.isSelected()) {
final double tmp = w;
w = h;
h = tmp;
}
this.pageFormat.setPageWidth(w);
this.pageFormat.setPageHeight(h);
} finally {
this.updating = false;
}
}
/**
* read the number fields.
*/
private void readSizeField() {
if (this.updating)
return;
this.updating = true;
try {
final double w = ((Number)widthField.getValue()).doubleValue();
final double h = ((Number)heightField.getValue()).doubleValue();
this.pageFormat.setPageWidth(w);
this.pageFormat.setPageHeight(h);
} finally {
this.updating = false;
}
}
private void readUnits() {
if (this.updating)
return;
this.updating = true;
try {
boolean unitPixels = this.pxRadioButton.isSelected();
this.pageFormat.setUnitPixels(unitPixels);
} finally {
this.updating = false;
}
}
private void readOrientation() {
if (this.updating)
return;
this.updating = true;
try {
double w = this.pageFormat.getPageWidth();
double h = this.pageFormat.getPageHeight();
this.pageFormat.setPageWidth(h);
this.pageFormat.setPageHeight(w);
} finally {
this.updating = false;
}
}
private void readPosition() {
if (this.updating)
return;
this.updating = true;
try {
final double left = ((Number)leftField.getValue()).doubleValue();
final double bottom = ((Number)bottomField.getValue()).doubleValue();
this.pageFormat.setPageLeft(left);
this.pageFormat.setPageBottom(bottom);
} finally {
this.updating = false;
}
}
private void readAutomatic() {
if (this.updating)
return;
this.updating = true;
try {
final boolean automatic = this.automaticCheckBox.isSelected();
this.pageFormat.setAutomatic(automatic);
} finally {
this.updating = false;
}
}
private void updateSizeComboBox() {
if (this.updating)
return;
this.updating = true;
try {
// get the current page size in millimeter
double w = this.pageFormat.getPageWidth();
double h = this.pageFormat.getPageHeight();
if (this.pageFormat.isUnitPixels()) {
w /= PageFormat.MM2PX;
h /= PageFormat.MM2PX;
}
// test if current size corresponds to standard page size and
// adjust popup menu if this is the case.
if (MathUtils.numbersAreClose(w, A4WIDTH, TOL) &&
MathUtils.numbersAreClose(h, A4HEIGHT, TOL))
this.sizeComboBox.setSelectedItem("A4");
else if (MathUtils.numbersAreClose(w, A3WIDTH, TOL) &&
MathUtils.numbersAreClose(h, A3HEIGHT, TOL))
this.sizeComboBox.setSelectedItem("A3");
else if (MathUtils.numbersAreClose(w, LETTERWIDTH, TOL) &&
MathUtils.numbersAreClose(h, LETTERHEIGHT, TOL))
this.sizeComboBox.setSelectedItem("Letter");
else if (MathUtils.numbersAreClose(w, LEGALWIDTH, TOL) &&
MathUtils.numbersAreClose(h, LEGALHEIGHT, TOL))
this.sizeComboBox.setSelectedItem("Legal");
else
this.sizeComboBox.setSelectedItem("Custom");
} finally {
this.updating = false;
}
}
private void updateSizeFields() {
if (this.updating)
return;
this.updating = true;
try {
final double w = this.pageFormat.getPageWidth();
final double h = this.pageFormat.getPageHeight();
widthField.setValue(w);
heightField.setValue(h);
} finally {
this.updating = false;
}
}
private void updateScaleField() {
if (this.updating)
return;
this.updating = true;
try {
final double scale = this.pageFormat.getPageScale();
this.scaleField.setValue(scale);
} finally {
this.updating = false;
}
}
private void updateUnitLabels() {
if (this.pageFormat.isUnitPixels()) {
this.widthUnitLabel.setText("px");
this.heightUnitLabel.setText("px");
} else {
this.widthUnitLabel.setText("mm");
this.heightUnitLabel.setText("mm");
}
}
private void updateUnitButtons() {
if (this.updating)
return;
this.updating = true;
try {
this.pxRadioButton.setSelected(this.pageFormat.isUnitPixels());
this.mmRadioButton.setSelected(this.pageFormat.isUnitMillimeter());
} finally {
this.updating = false;
}
}
private void updatePositionFields() {
if (this.updating)
return;
this.updating = true;
try {
final double left = this.pageFormat.getPageLeft();
final double bottom = this.pageFormat.getPageBottom();
leftField.setValue(left);
bottomField.setValue(bottom);
} finally {
this.updating = false;
}
}
private void updateAutomatic() {
if (this.updating)
return;
this.updating = true;
try {
final boolean automatic = this.pageFormat.isAutomatic();
this.automaticCheckBox.setSelected(automatic);
final boolean custom = !automatic;
this.sizeLabel.setEnabled(custom);
this.sizeComboBox.setEnabled(custom);
this.widthLabel.setEnabled(custom);
this.widthField.setEnabled(custom);
this.heightLabel.setEnabled(custom);
this.heightField.setEnabled(custom);
this.orientationLabel.setEnabled(custom);
this.portraitRadioButton.setEnabled(custom);
this.landscapeRadioButton.setEnabled(custom);
this.leftLabel.setEnabled(custom);
this.leftField.setEnabled(custom);
this.bottomLabel.setEnabled(custom);
this.bottomField.setEnabled(custom);
} finally {
this.updating = false;
}
}
public PageFormat getPageFormat() {
return (PageFormat)this.pageFormat.clone();
}
public void setPageFormat(PageFormat pageFormat) {
this.pageFormat = pageFormat;
this.updateScaleField();
this.updateSizeComboBox();
this.updateSizeFields();
this.updateUnitLabels();
this.updateUnitButtons();
this.updatePositionFields();
this.updateAutomatic();
}
@Override
public void setEnabled(boolean enabled) {
automaticCheckBox.setEnabled(enabled);
bottomLabel.setEnabled(enabled);
bottomField.setEnabled(enabled);
documentPositionLabel.setEnabled(enabled);
documentSizeLabel.setEnabled(enabled);
heightLabel.setEnabled(enabled);
heightField.setEnabled(enabled);
heightUnitLabel.setEnabled(enabled);
_1toLabel.setEnabled(enabled);
landscapeRadioButton.setEnabled(enabled);
leftLabel.setEnabled(enabled);
leftField.setEnabled(enabled);
mapDocumentInfoTextArea.setEnabled(enabled);
mapScaleInfoTextArea2.setEnabled(enabled);
mapScaleLabel.setEnabled(enabled);
mmRadioButton.setEnabled(enabled);
orientationLabel.setEnabled(enabled);
portraitRadioButton.setEnabled(enabled);
positionInfoTextArea.setEnabled(enabled);
pxRadioButton.setEnabled(enabled);
scaleField.setEnabled(enabled);
scalePanel.setEnabled(enabled);
sizeComboBox.setEnabled(enabled);
sizeLabel.setEnabled(enabled);
unitsLabel.setEnabled(enabled);
widthLabel.setEnabled(enabled);
widthField.setEnabled(enabled);
widthUnitLabel.setEnabled(enabled);
super.setEnabled(enabled);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel _1toLabel;
private javax.swing.JCheckBox automaticCheckBox;
private javax.swing.JFormattedTextField bottomField;
private javax.swing.JLabel bottomLabel;
private javax.swing.JLabel documentPositionLabel;
private javax.swing.JLabel documentSizeLabel;
private javax.swing.JFormattedTextField heightField;
private javax.swing.JLabel heightLabel;
private javax.swing.JLabel heightUnitLabel;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton landscapeRadioButton;
private javax.swing.JFormattedTextField leftField;
private javax.swing.JLabel leftLabel;
private javax.swing.JPanel mainScalePanel;
private javax.swing.JTextArea mapDocumentInfoTextArea;
private javax.swing.JTextArea mapScaleInfoTextArea2;
private javax.swing.JLabel mapScaleLabel;
private javax.swing.JRadioButton mmRadioButton;
private javax.swing.ButtonGroup orientationButtonGroup;
private javax.swing.JLabel orientationLabel;
private javax.swing.JRadioButton portraitRadioButton;
private javax.swing.JTextArea positionInfoTextArea;
private javax.swing.JRadioButton pxRadioButton;
private javax.swing.JFormattedTextField scaleField;
private javax.swing.JPanel scalePanel;
private javax.swing.JComboBox sizeComboBox;
private javax.swing.JLabel sizeLabel;
private javax.swing.ButtonGroup unitsButtonGroup;
private javax.swing.JLabel unitsLabel;
private javax.swing.JFormattedTextField widthField;
private javax.swing.JLabel widthLabel;
private javax.swing.JLabel widthUnitLabel;
// End of variables declaration//GEN-END:variables
}