/* * PageFormatPanel.java * * Created on April 20, 2007, 12:29 PM */ package ika.gui; import java.text.DecimalFormat; import java.text.ParseException; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.InternationalFormatter; /** * 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 SimplePageFormatPanel extends javax.swing.JPanel { private PageFormat pageFormat = new PageFormat(); private boolean updating = false; /** Creates new form PageFormatPanel */ public SimplePageFormatPanel() { initComponents(); this.scaleField.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { this.update(); } public void removeUpdate(DocumentEvent e) { this.update(); } public void changedUpdate(DocumentEvent e) { this.update(); } private void update() { try { readScale(); updateSizeFields(); } catch (ParseException ex) { widthValueLabel.setText("-"); heightValueLabel.setText("-"); } } }); // update GUI this.updateScaleField(); } /** 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(); javax.swing.JLabel widthLabel = new javax.swing.JLabel(); javax.swing.JLabel heightLabel = new javax.swing.JLabel(); javax.swing.JLabel scaleLabel = new javax.swing.JLabel(); javax.swing.JPanel scalePanel = new javax.swing.JPanel(); javax.swing.JLabel _1toLabel = new javax.swing.JLabel(); scaleField = new javax.swing.JFormattedTextField(); javax.swing.JLabel unitsLabel = new javax.swing.JLabel(); pxRadioButton = new javax.swing.JRadioButton(); mmRadioButton = new javax.swing.JRadioButton(); javax.swing.JLabel documentSizeLabel = new javax.swing.JLabel(); javax.swing.JLabel mapScaleLabel = new javax.swing.JLabel(); widthValueLabel = new javax.swing.JLabel(); heightValueLabel = new javax.swing.JLabel(); 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); scaleLabel.setText("Scale:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; add(scaleLabel, gridBagConstraints); 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.setMinimumSize(new java.awt.Dimension(150, 28)); scaleField.setPreferredSize(new java.awt.Dimension(150, 28)); // setup custom format factory that formats numbers with non-scientific notation // and does not allow numbers smaller than 0 DecimalFormat format = new DecimalFormat("#,##0.#"); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setMinimum(0); scaleField.setFormatterFactory(new DefaultFormatterFactory(formatter)); scalePanel.add(scaleField, new java.awt.GridBagConstraints()); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0); add(scalePanel, 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); 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); mapScaleLabel.setText("Map Scale"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.ipadx = 20; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(25, 0, 0, 0); add(mapScaleLabel, gridBagConstraints); widthValueLabel.setText("0 px"); widthValueLabel.setPreferredSize(new java.awt.Dimension(150, 16)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 7; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0); add(widthValueLabel, gridBagConstraints); heightValueLabel.setText("0 px"); heightValueLabel.setPreferredSize(new java.awt.Dimension(150, 16)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 8; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0); add(heightValueLabel, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents private void unitRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unitRadioButtonActionPerformed this.readUnits(); this.updateSizeFields(); }//GEN-LAST:event_unitRadioButtonActionPerformed private void readScale() throws ParseException { if (this.updating) return; this.updating = true; try { System.out.println("* " + scaleField.getText()); this.scaleField.commitEdit(); java.lang.Number scale = (java.lang.Number)(this.scaleField.getValue()); double newScale = scale.doubleValue(); double oldScale = this.pageFormat.getPageScale(); double f = oldScale / newScale; double newWidth = this.pageFormat.getPageWidth() * f; double newHeight = this.pageFormat.getPageHeight() * f; this.pageFormat.setPageScale(newScale); this.pageFormat.setPageWidth(newWidth); this.pageFormat.setPageHeight(newHeight); } 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 updateSizeFields() { if (this.updating) return; this.updating = true; try { final double w = this.pageFormat.getPageWidth(); final double h = this.pageFormat.getPageHeight(); boolean px = this.pageFormat.isUnitPixels(); DecimalFormat df = new DecimalFormat("#,###.#" + (px ? " px" : " mm")); this.widthValueLabel.setText(df.format(w)); this.heightValueLabel.setText(df.format(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 updateUnitRadioButtons() { this.mmRadioButton.setSelected(this.pageFormat.isUnitMillimeter()); this.pxRadioButton.setSelected(this.pageFormat.isUnitPixels()); } public PageFormat getPageFormat() { return (PageFormat)this.pageFormat.clone(); } public void setPageFormat(PageFormat pageFormat) { this.pageFormat = pageFormat; this.updateScaleField(); this.updateSizeFields(); this.updateUnitRadioButtons(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel heightValueLabel; private javax.swing.JRadioButton mmRadioButton; private javax.swing.JRadioButton pxRadioButton; private javax.swing.JFormattedTextField scaleField; private javax.swing.ButtonGroup unitsButtonGroup; private javax.swing.JLabel widthValueLabel; // End of variables declaration//GEN-END:variables }