001package jahuwaldt.js.unit; 002 003import jahuwaldt.swing.EscapeJDialog; 004import java.awt.Component; 005import java.awt.Container; 006import java.awt.event.ActionEvent; 007import java.awt.event.ActionListener; 008import java.awt.event.WindowEvent; 009import java.util.ResourceBundle; 010import javax.swing.BorderFactory; 011import javax.swing.Box; 012import javax.swing.BoxLayout; 013import javax.swing.JButton; 014 015/** 016 * A modal dialog the allows the user to select a set of units to be used. 017 * 018 * <p> Modified by: Joseph A. Huwaldt </p> 019 * 020 * @author Joseph A. Huwaldt, Date: July 21, 2008 021 * @version March 17, 2017 022 */ 023@SuppressWarnings("serial") 024public class EditUnitSetDialog extends EscapeJDialog implements ActionListener { 025 026 // The resource bundle for this class. 027 private static final ResourceBundle RESOURCES = EditUnitSetPanel.RESOURCES; 028 029 // The panel the user interacts with to select the units. 030 private final EditUnitSetPanel unitPanel; 031 032 // The UnitSet being edited. 033 private UnitSet outputUnits = null; 034 035 /** 036 * Construct a model dialog that allows the user to select a set of units to be used. 037 * 038 * @param parent A reference to the parent component that this dialog belongs to 039 * (null is fine). 040 * @param title The title for the dialog window. 041 * @param message A message to display telling the user what to do. 042 * @param unitSet The unit set being edited. 043 */ 044 @SuppressWarnings({"LeakingThisInConstructor","OverridableMethodCallInConstructor"}) 045 public EditUnitSetDialog(Component parent, String title, String message, UnitSet unitSet) { 046 super(parent, title, true); 047 048 // Layout the dialog window. 049 Container cp = this.getContentPane(); 050 cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS)); 051 052 // Add the edit panel to this dialog. 053 unitPanel = new EditUnitSetPanel(message, unitSet, true); 054 cp.add(unitPanel); 055 056 // Define OK and Cancel buttons. 057 Box panel = Box.createHorizontalBox(); 058 cp.add(panel); 059 panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 060 panel.add(Box.createGlue()); 061 062 JButton cancelBtn = new JButton(RESOURCES.getString("cancel")); 063 cancelBtn.addActionListener(new ActionListener() { 064 @Override 065 public void actionPerformed(ActionEvent e) { 066 performEscapeAction(null); 067 } 068 }); 069 panel.add(cancelBtn); 070 071 panel.add(Box.createHorizontalStrut(40)); 072 073 JButton okayBtn = new JButton(RESOURCES.getString("okay")); 074 okayBtn.addActionListener(this); 075 this.getRootPane().setDefaultButton(okayBtn); 076 panel.add(okayBtn); 077 078 panel.add(Box.createHorizontalStrut(20)); 079 080 // Pack the window. 081 this.pack(); 082 083 // Position the dialog to be near the parent window. 084 this.setLocationRelativeTo(parent); 085} 086 087 /** 088 * Returns the unit set that has been edited by the user. 089 * 090 * @return The unit set that has been edited by the user or null if the user cancels 091 * the edit. 092 */ 093 public UnitSet getUnits() { 094 return outputUnits; 095 } 096 097 /** 098 * This method is called when the user clicks on the "OK Button". 099 * 100 * @param evt The event that caused this method to be called (ignored). 101 */ 102 @Override 103 public void actionPerformed(ActionEvent evt) { 104 105 // Set the output units to be the units we have been editing. 106 outputUnits = unitPanel.getUnits(); 107 108 // Close the window. 109 this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 110 } 111 112}