updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / gui / configdialog / FinSetConfig.java
1 package net.sf.openrocket.gui.configdialog;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5
6 import javax.swing.JButton;
7 import javax.swing.JComboBox;
8 import javax.swing.JLabel;
9 import javax.swing.JPanel;
10 import javax.swing.JSpinner;
11 import javax.swing.SwingUtilities;
12
13 import net.miginfocom.swing.MigLayout;
14 import net.sf.openrocket.gui.SpinnerEditor;
15 import net.sf.openrocket.gui.adaptors.DoubleModel;
16 import net.sf.openrocket.gui.adaptors.EnumModel;
17 import net.sf.openrocket.gui.components.BasicSlider;
18 import net.sf.openrocket.gui.components.UnitSelector;
19 import net.sf.openrocket.rocketcomponent.FinSet;
20 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
21 import net.sf.openrocket.rocketcomponent.RocketComponent;
22 import net.sf.openrocket.unit.UnitGroup;
23
24
25 public abstract class FinSetConfig extends RocketComponentConfig {
26
27         private JButton split = null;
28         
29         public FinSetConfig(RocketComponent component) {
30                 super(component);
31                 
32                 tabbedPane.insertTab("Fin tabs", null, finTabPanel(), "Through-the-wall fin tabs", 0);
33         }
34
35         
36         
37         protected void addFinSetButtons() {
38                 JButton convert=null;
39                 
40                 //// Convert buttons
41                 if (!(component instanceof FreeformFinSet)) {
42                         convert = new JButton("Convert to freeform");
43                         convert.setToolTipText("Convert this fin set into a freeform fin set");
44                         convert.addActionListener(new ActionListener() {
45                                 @Override
46                                 public void actionPerformed(ActionEvent e) {
47                                         // Do change in future for overall safety
48                                         SwingUtilities.invokeLater(new Runnable() {
49                                                 @Override
50                                                 public void run() {
51                                                         ComponentConfigDialog.addUndoPosition("Convert fin set");
52                                                         RocketComponent freeform = 
53                                                                 FreeformFinSet.convertFinSet((FinSet)component);
54                                                         ComponentConfigDialog.showDialog(freeform);
55                                                 }
56                                         });
57
58                                         ComponentConfigDialog.hideDialog();
59                                 }
60                         });
61                 }
62
63                 split = new JButton("Split fins");
64                 split.setToolTipText("Split the fin set into separate fins");
65                 split.addActionListener(new ActionListener() {
66                         @Override
67                         public void actionPerformed(ActionEvent e) {
68                                 // Do change in future for overall safety
69                                 SwingUtilities.invokeLater(new Runnable() {
70                                         @Override
71                                         public void run() {
72                                                 RocketComponent parent = component.getParent();
73                                                 int index = parent.getChildPosition(component);
74                                                 int count = ((FinSet)component).getFinCount();
75                                                 double base = ((FinSet)component).getBaseRotation();
76                                                 if (count <= 1)
77                                                         return;
78                                                 
79                                                 ComponentConfigDialog.addUndoPosition("Split fin set");
80                                                 parent.removeChild(index);
81                                                 for (int i=0; i<count; i++) {
82                                                         FinSet copy = (FinSet)component.copy();
83                                                         copy.setFinCount(1);
84                                                         copy.setBaseRotation(base + i*2*Math.PI/count);
85                                                         copy.setName(copy.getName() + " #" + (i+1));
86                                                         parent.addChild(copy, index+i);
87                                                 }
88                                         }
89                                 });
90
91                                 ComponentConfigDialog.hideDialog();
92                         }
93                 });
94                 split.setEnabled(((FinSet)component).getFinCount() > 1);
95                 
96                 if (convert==null)
97                         addButtons(split);
98                 else
99                         addButtons(split,convert);
100
101         }
102
103         public JPanel finTabPanel() {
104                 JPanel panel = new JPanel(
105                                 new MigLayout("align 50% 20%, fillx, gap rel unrel, ins 20lp 10% 20lp 10%",
106                                 "[150lp::][65lp::][30lp::][200lp::]",""));
107 //              JPanel panel = new JPanel(new MigLayout("fillx, align 20% 20%, gap rel unrel",
108 //                              "[40lp][80lp::][30lp::][100lp::]",""));
109
110                 panel.add(new JLabel("<html><b>Through-the-wall fin tabs:</b>"), "spanx, wrap 30lp");
111                 
112                 JLabel label;
113                 DoubleModel m;
114                 DoubleModel length;
115                 DoubleModel length2;
116                 DoubleModel length_2;
117                 JSpinner spin;
118
119                 length = new DoubleModel(component, "Length", UnitGroup.UNITS_LENGTH, 0);
120                 length2 = new DoubleModel(component, "Length", 0.5, UnitGroup.UNITS_LENGTH, 0);
121                 length_2 = new DoubleModel(component, "Length", -0.5, UnitGroup.UNITS_LENGTH, 0);
122                 
123                 ////  Tab length
124                 label = new JLabel("Tab length:");
125                 label.setToolTipText("The length of the fin tab.");
126                 panel.add(label, "gapleft para, gapright 40lp, growx 1");
127                 
128                 m = new DoubleModel(component, "TabLength", UnitGroup.UNITS_LENGTH, 0);
129                 
130                 spin = new JSpinner(m.getSpinnerModel());
131                 spin.setEditor(new SpinnerEditor(spin));
132                 panel.add(spin,"growx 1");
133                 
134                 panel.add(new UnitSelector(m),"growx 1");
135                 panel.add(new BasicSlider(m.getSliderModel(DoubleModel.ZERO, length)),
136                                 "w 100lp, growx 5, wrap");
137
138                 
139                 ////  Tab length
140                 label = new JLabel("Tab height:");
141                 label.setToolTipText("The spanwise height of the fin tab.");
142                 panel.add(label, "gapleft para");
143                 
144                 m = new DoubleModel(component, "TabHeight", UnitGroup.UNITS_LENGTH, 0);
145                 
146                 spin = new JSpinner(m.getSpinnerModel());
147                 spin.setEditor(new SpinnerEditor(spin));
148                 panel.add(spin,"growx");
149                 
150                 panel.add(new UnitSelector(m),"growx");
151                 panel.add(new BasicSlider(m.getSliderModel(DoubleModel.ZERO, length2)),
152                                 "w 100lp, growx 5, wrap para");
153
154                 
155                 ////  Tab position
156                 label = new JLabel("Tab position:");
157                 label.setToolTipText("The position of the fin tab.");
158                 panel.add(label, "gapleft para");
159                 
160                 m = new DoubleModel(component, "TabShift", UnitGroup.UNITS_LENGTH);
161                 
162                 spin = new JSpinner(m.getSpinnerModel());
163                 spin.setEditor(new SpinnerEditor(spin));
164                 panel.add(spin,"growx");
165                 
166                 panel.add(new UnitSelector(m),"growx");
167                 panel.add(new BasicSlider(m.getSliderModel(length_2, length2)),"w 100lp, growx 5, wrap");
168
169                 
170                 
171                 label = new JLabel("relative to");
172                 panel.add(label, "right, gapright unrel");
173                 
174                 EnumModel<FinSet.TabRelativePosition> em = 
175                         new EnumModel<FinSet.TabRelativePosition>(component, "TabRelativePosition");
176                 
177                 panel.add(new JComboBox(em), "spanx 3, growx");
178                 
179                 return panel;
180         }
181
182         @Override
183         public void updateFields() {
184                 super.updateFields();
185                 if (split != null)
186                         split.setEnabled(((FinSet)component).getFinCount() > 1);
187         }
188 }