1 package net.sf.openrocket.gui.customexpression;
3 import java.awt.Window;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.KeyEvent;
7 import java.awt.event.KeyListener;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
11 import javax.swing.AbstractAction;
12 import javax.swing.ActionMap;
13 import javax.swing.InputMap;
14 import javax.swing.JButton;
15 import javax.swing.JComponent;
16 import javax.swing.JDialog;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTable;
20 import javax.swing.KeyStroke;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
23 import javax.swing.table.JTableHeader;
25 import net.miginfocom.swing.MigLayout;
26 import net.sf.openrocket.document.OpenRocketDocument;
27 import net.sf.openrocket.document.Simulation;
28 import net.sf.openrocket.l10n.Translator;
29 import net.sf.openrocket.logging.LogHelper;
30 import net.sf.openrocket.rocketcomponent.Rocket;
31 import net.sf.openrocket.startup.Application;
34 * Dialog to select from available custom variables
35 * @author Richard Graham
39 public class VariableSelector extends JDialog {
41 private static final Translator trans = Application.getTranslator();
42 private static final LogHelper log = Application.getLogger();
44 private final JTable table;
45 private final VariableTableModel tableModel;
46 private final ExpressionBuilderDialog parentBuilder;
48 public VariableSelector(Window parent, final ExpressionBuilderDialog parentBuilder, final OpenRocketDocument doc){
50 super(parent, trans.get("CustomVariableSelector.title"), JDialog.ModalityType.DOCUMENT_MODAL);
52 this.parentBuilder = parentBuilder;
53 final JButton insertButton = new JButton(trans.get("ExpressionBuilderDialog.InsertVariable"));
55 JPanel mainPanel = new JPanel(new MigLayout());
57 //// Table of variables and model
58 tableModel = new VariableTableModel(doc);
59 table = new JTable(tableModel);
61 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
62 table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
63 int width = table.getColumnModel().getTotalColumnWidth();
64 table.getColumnModel().getColumn(0).setPreferredWidth( (int) (.7 * width));
65 table.getColumnModel().getColumn(1).setPreferredWidth( (int) (.15 * width));
66 table.getColumnModel().getColumn(2).setPreferredWidth( (int) (.15 * width));
67 table.setAutoCreateRowSorter(true);
69 table.addMouseListener(new MouseListener(){
71 public void mouseClicked(MouseEvent e){
72 if (e.getClickCount() == 2){
73 log.debug("Selected variable by double clicking.");
78 public void mouseEntered(MouseEvent e) {}
80 public void mouseExited(MouseEvent e) {}
82 public void mousePressed(MouseEvent e) {}
84 public void mouseReleased(MouseEvent e) {}
87 InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
88 ActionMap actionMap = table.getActionMap();
89 KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
90 inputMap.put(enter, "select");
91 actionMap.put("select", new AbstractAction(){
93 public void actionPerformed(ActionEvent arg0) {
94 log.debug("Selected variable by enter key");
100 JScrollPane scrollPane = new JScrollPane(table);
101 table.setFillsViewportHeight(true);
102 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
104 public void valueChanged(ListSelectionEvent e){
105 if (table.getSelectedRowCount() == 1){
106 insertButton.setEnabled(true);
109 insertButton.setEnabled(false);
114 mainPanel.add(scrollPane, "wrap, push, grow");
117 final JButton cancelButton = new JButton(trans.get("dlg.but.cancel"));
118 cancelButton.addActionListener(new ActionListener() {
120 public void actionPerformed(ActionEvent e) {
121 VariableSelector.this.dispose();
124 mainPanel.add(cancelButton, "right, width :100:200, split 2");
127 insertButton.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
133 insertButton.setEnabled(false); // disabled by default, only enable when a variable selected
134 mainPanel.add(insertButton, "right, width :100:200, wrap");
139 this.setLocationByPlatform(true);
142 private void selectVariable(){
143 int row = table.getSelectedRow();
144 String str = tableModel.getSymbolAt(row);
145 parentBuilder.pasteIntoExpression(str);
146 VariableSelector.this.dispose();