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