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