Fixed table sorting bug in custom expression variable and operator selector windows.
[debian/openrocket] / core / src / net / sf / openrocket / gui / customexpression / VariableSelector.java
1 package net.sf.openrocket.gui.customexpression;
2
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;
10
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;
24
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;
32
33 /**
34  * Dialog to select from available custom variables
35  * @author Richard Graham
36  *
37  */
38
39 public class VariableSelector extends JDialog {
40
41         private static final Translator trans = Application.getTranslator();
42         private static final LogHelper log = Application.getLogger();
43         
44         private final JTable table;
45         private final VariableTableModel tableModel;
46         private final ExpressionBuilderDialog parentBuilder;
47
48         public VariableSelector(Window parent, final ExpressionBuilderDialog parentBuilder, final OpenRocketDocument doc){
49
50                 super(parent, trans.get("CustomVariableSelector.title"), JDialog.ModalityType.DOCUMENT_MODAL);
51
52                 this.parentBuilder = parentBuilder;
53                 final JButton insertButton = new JButton(trans.get("ExpressionBuilderDialog.InsertVariable"));
54
55                 JPanel mainPanel = new JPanel(new MigLayout());
56
57                 //// Table of variables and model
58                 tableModel = new VariableTableModel(doc);
59                 table = new JTable(tableModel);
60                 
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);
68
69                 table.addMouseListener(new MouseListener(){
70                         @Override
71                         public void mouseClicked(MouseEvent e){
72                                 if (e.getClickCount() == 2){
73                                         log.debug("Selected variable by double clicking.");
74                                         selectVariable();
75                                 }
76                         }
77                         @Override
78                         public void mouseEntered(MouseEvent e) {}
79                         @Override
80                         public void mouseExited(MouseEvent e) {}
81                         @Override
82                         public void mousePressed(MouseEvent e) {}
83                         @Override
84                         public void mouseReleased(MouseEvent e) {}
85                 } );
86                 
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(){
92                         @Override
93                         public void actionPerformed(ActionEvent arg0) {
94                                 log.debug("Selected variable by enter key");
95                                 selectVariable();
96                         }
97                 });
98                 
99
100                 JScrollPane scrollPane = new JScrollPane(table);
101                 table.setFillsViewportHeight(true);
102                 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
103                         @Override
104                         public void valueChanged(ListSelectionEvent e){
105                                 if (table.getSelectedRowCount() == 1){
106                                         insertButton.setEnabled(true);
107                                 }
108                                 else {
109                                         insertButton.setEnabled(false);
110                                 }
111                         }
112                 });
113
114                 mainPanel.add(scrollPane, "wrap, push, grow");
115
116                 //// Cancel button
117                 final JButton cancelButton = new JButton(trans.get("dlg.but.cancel"));
118                 cancelButton.addActionListener(new ActionListener() {
119                         @Override
120                         public void actionPerformed(ActionEvent e) {
121                                 VariableSelector.this.dispose();
122                         }
123                 });
124                 mainPanel.add(cancelButton, "right, width :100:200, split 2");
125
126                 //// Insert button
127                 insertButton.addActionListener(new ActionListener() {
128                         @Override
129                         public void actionPerformed(ActionEvent e) {
130                                 selectVariable();
131                         }
132                 });
133                 insertButton.setEnabled(false); // disabled by default, only enable when a variable selected
134                 mainPanel.add(insertButton, "right, width :100:200, wrap");
135
136                 this.add(mainPanel);
137                 this.validate();
138                 this.pack();
139                 this.setLocationByPlatform(true);       
140         }
141         
142         private void selectVariable(){
143                 int row = table.getSelectedRow();
144                 String str = table.getValueAt(row, 1).toString();
145                 parentBuilder.pasteIntoExpression(str);
146                 VariableSelector.this.dispose();
147         }
148         
149 }