001/** 002 * ChooseUnitDialog -- A dialog that allows the user to select the set of units to be 003 * used. 004 * 005 * Copyright (C) 2009-2016, by Joseph A. Huwaldt. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or modify it under the terms 008 * of the GNU Lesser General Public License as published by the Free Software Foundation; 009 * either version 2.1 of the License, or (at your option) any later version. 010 * 011 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License along with 016 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - 017 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html 018 */ 019package geomss.app; 020 021import jahuwaldt.swing.EscapeJDialog; 022import java.awt.Component; 023import java.awt.Container; 024import java.awt.event.*; 025import java.util.Objects; 026import static java.util.Objects.requireNonNull; 027import java.util.ResourceBundle; 028import javax.measure.quantity.Quantity; 029import javax.measure.unit.Unit; 030import javax.swing.*; 031 032/** 033 * A modal dialog the allows the user to select a set of units to be used. 034 * 035 * <p> Modified by: Joseph A. Huwaldt </p> 036 * 037 * @author Joseph A. Huwaldt, Date: April 5, 2009 038 * @version October 29, 2016 039 * 040 * @param <Q> The Quantity (unit type) of the unit chosen by this dialog. 041 */ 042@SuppressWarnings("serial") 043public class ChooseUnitDialog<Q extends Quantity> extends EscapeJDialog implements ActionListener { 044 045 // The resource bundle for this class. 046 private static final ResourceBundle RB 047 = ResourceBundle.getBundle("geomss.app.ChooseUnitDialogResources", java.util.Locale.getDefault()); 048 049 // The panel the user interacts with to select the units. 050 private final ChooseUnitPanel<Q> unitPanel; 051 052 // The unit selected. 053 private Unit<Q> outputUnits = null; 054 055 /** 056 * Construct a model dialog that allows the user to select a set of units to be used. 057 * 058 * @param parent A reference to the parent component that this dialog belongs to 059 * (null is fine). 060 * @param title The title for the dialog window. May not be null. 061 * @param message A message to display telling the user what to do. May not be null. 062 * @param typeStr A string indicating the unit type (such as "Length Units: ", or 063 * "Time Units: "). May not be null. 064 * @param unitList The options to select from. May not be null. 065 * @param initial The initially selected unit (or null for none). 066 */ 067 public ChooseUnitDialog(Component parent, String title, String message, String typeStr, 068 Unit<Q>[] unitList, Unit<Q> initial) { 069 super(parent, requireNonNull(title), true); 070 requireNonNull(message); 071 requireNonNull(typeStr); 072 requireNonNull(unitList); 073 074 075 // Layout the dialog window. 076 Container cp = this.getContentPane(); 077 cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS)); 078 079 // Add the edit panel to this dialog. 080 unitPanel = new ChooseUnitPanel(message, typeStr, unitList, initial); 081 cp.add(unitPanel); 082 083 // Define OK and Cancel buttons. 084 Box panel = Box.createHorizontalBox(); 085 cp.add(panel); 086 panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 087 panel.add(Box.createGlue()); 088 089 JButton cancelBtn = new JButton(RB.getString("cancel")); 090 cancelBtn.addActionListener(new ActionListener() { 091 @Override 092 public void actionPerformed(ActionEvent e) { 093 performEscapeAction(null); 094 } 095 }); 096 panel.add(cancelBtn); 097 098 panel.add(Box.createHorizontalStrut(40)); 099 100 // If multiple selections allowed, change "OK" button text to "Done". 101 JButton okayBtn = new JButton(RB.getString("select")); 102 okayBtn.addActionListener(new ActionListener() { 103 @Override 104 public void actionPerformed(ActionEvent e) { 105 ChooseUnitDialog.this.actionPerformed(null); 106 } 107 }); 108 this.getRootPane().setDefaultButton(okayBtn); 109 panel.add(okayBtn); 110 111 panel.add(Box.createHorizontalStrut(20)); 112 113 // Pack the window. 114 this.pack(); 115 116 // Position the window automatically. 117 if (Objects.isNull(parent)) 118 setLocationByPlatform(true); 119 else 120 setLocationRelativeTo(parent); 121 } 122 123 /** 124 * @return the unit that has been selected by the user or <code>null</code> if the 125 * user cancels the edit. 126 */ 127 public Unit<Q> getSelected() { 128 return outputUnits; 129 } 130 131 /** 132 * This method is called when the user clicks on the "OK Button". 133 * 134 * @param evt The action event (ignored). 135 */ 136 @Override 137 public void actionPerformed(ActionEvent evt) { 138 139 // Set the output units to be the units the user has chosen. 140 outputUnits = unitPanel.getSelected(); 141 142 // Hide the window. 143 this.setVisible(false); 144 } 145 146 /** 147 * Response to ENTER key pressed goes here. This has the same result as clicking on 148 * the OK button. 149 * 150 * @param e The key event (ignored). 151 */ 152 @Override 153 protected void performEnterAction(KeyEvent e) { 154 actionPerformed(null); 155 } 156 157}