cf35cbb409dda9d942ff07a815845e39004284b7
[debian/openrocket] / core / src / net / sf / openrocket / gui / customexpression / CustomExpressionPanel.java
1 package net.sf.openrocket.gui.customexpression;
2
3 import java.awt.Color;
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;
9
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;
16
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;
27
28 public class CustomExpressionPanel extends JPanel {
29         
30         private static final LogHelper log = Application.getLogger();
31         private static final Translator trans = Application.getTranslator();
32         
33         private JPanel expressionSelectorPanel;
34         private Simulation simulation;
35         
36         public CustomExpressionPanel(final Simulation simulation) {
37                 super(new MigLayout("fill"));
38                 this.simulation = simulation;
39
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");
43                 
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");
47                 
48                 //// New expression
49                 JButton button = new JButton(trans.get("customExpressionPanel.but.NewExpression"));
50                 button.addActionListener(new ActionListener() {
51                         @Override
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);
57                                 updateExpressions();
58                         }
59                 });
60                                 
61                 this.add(button, "left");
62                 
63                 updateExpressions();
64         }
65         
66         /*
67          * Update the expressionSelectorPanel
68          */
69         private void updateExpressions(){
70                 
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");
76                 }
77
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
81         }
82         
83         private void deleteExpression(CustomExpression expression){
84                 simulation.getCustomExpressions().remove(expression);
85         }
86         
87         /**
88          * Moves an expression up or down in the expression list
89          * @param expression
90          * @param move integer - +1 to move down, -1 to move up
91          */
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)
96                         return;
97                 else
98                         Collections.swap(expressions, i, i+move);
99         }
100
101         
102         /*
103          * A JPanel which configures a single expression
104          */
105         private class SingleExpression extends JPanel {
106                 
107                 // Convenience method to make the labels consistent
108                 private JLabel setLabelStyle(JLabel l){
109                         l.setBackground(Color.WHITE);
110                         l.setOpaque(true);
111                         l.setBorder(BorderFactory.createRaisedBevelBorder() );
112                         l.setText(" " + l.getText() + " ");
113                         return l;
114                 }
115                 
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]",""));
120                         
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);
128                         
129                         JLabel unitLabel = new JLabel( trans.get("customExpression.Units")+ " :");
130                         UnitSelector unitSelector = new UnitSelector(expression.getType().getUnitGroup());
131                         
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() {
136                                 @Override
137                                 public void actionPerformed(ActionEvent e){
138                                         Window parent = SwingUtilities.getWindowAncestor(CustomExpressionPanel.this);
139                                         new ExpressionBuilderDialog(parent, expression.getSimulation(), expression).setVisible(true);
140                                         updateExpressions();
141                                 }
142                         });
143                         
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() {
149                                 @Override
150                                 public void actionPerformed(ActionEvent e) {
151                                         moveExpression(expression, -1);
152                                         updateExpressions();
153                                 }
154                         });
155                         
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() {
161                                 @Override
162                                 public void actionPerformed(ActionEvent e) {
163                                         moveExpression(expression, 1);
164                                         updateExpressions();
165                                 }
166                         });
167                         
168                         
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() {
174                                 @Override
175                                 public void actionPerformed(ActionEvent e) {
176                                         deleteExpression(expression);
177                                         updateExpressions();
178                                 }
179                         });
180                         
181                         this.add(nameLabel);
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());
187                         this.add(unitLabel);
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");
194                 }
195         }
196 }