1 package net.sf.openrocket.gui.customexpression;
4 import java.awt.Window;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.ArrayList;
8 import java.util.Collections;
10 import javax.swing.BorderFactory;
11 import javax.swing.JButton;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JScrollPane;
15 import javax.swing.SwingUtilities;
17 import net.miginfocom.swing.MigLayout;
18 import net.sf.openrocket.document.Simulation;
19 import net.sf.openrocket.gui.components.DescriptionArea;
20 import net.sf.openrocket.gui.components.UnitSelector;
21 import net.sf.openrocket.gui.customexpression.ExpressionBuilderDialog;
22 import net.sf.openrocket.gui.util.Icons;
23 import net.sf.openrocket.l10n.Translator;
24 import net.sf.openrocket.logging.LogHelper;
25 import net.sf.openrocket.simulation.CustomExpression;
26 import net.sf.openrocket.startup.Application;
28 public class CustomExpressionPanel extends JPanel {
30 private static final LogHelper log = Application.getLogger();
31 private static final Translator trans = Application.getTranslator();
33 private JPanel expressionSelectorPanel;
34 private Simulation simulation;
36 public CustomExpressionPanel(final Simulation simulation) {
37 super(new MigLayout("fill"));
38 this.simulation = simulation;
40 expressionSelectorPanel = new JPanel(new MigLayout("gapy rel"));
41 JScrollPane scroll = new JScrollPane(expressionSelectorPanel);
42 this.add(scroll, "spany 2, height 10px, wmin 400lp, grow 100, gapright para");
44 DescriptionArea desc = new DescriptionArea(trans.get("customExpressionPanel.lbl.UpdateNote")+"\n\n"+trans.get("customExpressionPanel.lbl.CalcNote"), 8, -2f);
45 desc.setViewportBorder(BorderFactory.createEmptyBorder());
46 this.add(desc, "width 1px, growx 1, wrap unrel");
49 JButton button = new JButton(trans.get("customExpressionPanel.but.NewExpression"));
50 button.addActionListener(new ActionListener() {
52 public void actionPerformed(ActionEvent e) {
53 // Open window to configure expression
54 log.debug("Opening window to configure new expression");
55 Window parent = SwingUtilities.getWindowAncestor(CustomExpressionPanel.this);
56 new ExpressionBuilderDialog(parent, simulation).setVisible(true);
61 this.add(button, "left");
67 * Update the expressionSelectorPanel
69 private void updateExpressions(){
71 expressionSelectorPanel.removeAll();
72 int totalExpressions = simulation.getCustomExpressions().size();
73 for (int i=0; i<totalExpressions; i++){
74 SingleExpression se = new SingleExpression(simulation.getCustomExpressions().get(i), i != 0, i != totalExpressions-1);
75 expressionSelectorPanel.add(se, "wrap");
78 //TODO: High : Find out why repaint method not working properly here.
79 //expressionSelectorPanel.repaint();
80 expressionSelectorPanel.updateUI(); // Not the correct method to use but works
83 private void deleteExpression(CustomExpression expression){
84 simulation.getCustomExpressions().remove(expression);
88 * Moves an expression up or down in the expression list
90 * @param move integer - +1 to move down, -1 to move up
92 private void moveExpression(CustomExpression expression, int move){
93 ArrayList<CustomExpression> expressions = simulation.getCustomExpressions();
94 int i = expressions.indexOf(expression);
95 if (i+move == expressions.size() || i+move < 0)
98 Collections.swap(expressions, i, i+move);
103 * A JPanel which configures a single expression
105 private class SingleExpression extends JPanel {
107 // Convenience method to make the labels consistent
108 private JLabel setLabelStyle(JLabel l){
109 l.setBackground(Color.WHITE);
111 l.setBorder(BorderFactory.createRaisedBevelBorder() );
112 l.setText(" " + l.getText() + " ");
116 private SingleExpression(final CustomExpression expression, boolean showUp, boolean showDown) {
117 super(new MigLayout("ins 0"));
118 // name: aName symbol: a Unit: m/s
119 //super(new MigLayout("","[::100][:200:400][::100][:100:200][::100][:100:200]",""));
121 JLabel nameLabel = new JLabel( trans.get("customExpression.Name")+ " :");
122 JLabel name = new JLabel ( expression.getName() );
123 name = setLabelStyle(name);
124 JLabel symbolLabel = new JLabel( trans.get("customExpression.Symbol")+ " :" );
125 JLabel symbol = new JLabel ( expression.getSymbol());
126 symbol = setLabelStyle(symbol);
127 symbol.setBackground(Color.WHITE);
129 JLabel unitLabel = new JLabel( trans.get("customExpression.Units")+ " :");
130 UnitSelector unitSelector = new UnitSelector(expression.getType().getUnitGroup());
132 JButton editButton = new JButton(Icons.EDIT);
133 editButton.setToolTipText(trans.get("customExpression.Units.but.ttip.Edit"));
134 editButton.setBorderPainted(false);
135 editButton.addActionListener( new ActionListener() {
137 public void actionPerformed(ActionEvent e){
138 Window parent = SwingUtilities.getWindowAncestor(CustomExpressionPanel.this);
139 new ExpressionBuilderDialog(parent, expression.getSimulation(), expression).setVisible(true);
144 JButton upButton = new JButton(Icons.UP);
145 upButton.setToolTipText(trans.get("customExpression.Units.but.ttip.MoveUp"));
146 upButton.setBorderPainted(false);
147 upButton.setVisible(showUp);
148 upButton.addActionListener( new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 moveExpression(expression, -1);
156 JButton downButton = new JButton(Icons.DOWN);
157 downButton.setToolTipText(trans.get("customExpression.Units.but.ttip.MoveDown"));
158 downButton.setBorderPainted(false);
159 downButton.setVisible(showDown);
160 downButton.addActionListener( new ActionListener() {
162 public void actionPerformed(ActionEvent e) {
163 moveExpression(expression, 1);
169 JButton deleteButton = new JButton(Icons.DELETE);
170 //// Remove this expression
171 deleteButton.setToolTipText(trans.get("customExpression.Units.but.ttip.Remove"));
172 deleteButton.setBorderPainted(false);
173 deleteButton.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e) {
176 deleteExpression(expression);
182 this.add(name, "width 200:200:400, growx");
183 this.add(new JPanel());
184 this.add(symbolLabel);
185 this.add(symbol, "width :50:200");
186 this.add(new JPanel());
188 this.add(unitSelector, "width :50:100");
189 this.add(new JPanel(), "growx");
190 this.add(upButton, "right");
191 this.add(downButton, "right");
192 this.add(editButton, "right");
193 this.add(deleteButton, "right");