fb8e76cce77fd07f504b20a408e46a0081d0e2d7
[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.SwingUtilities;
8
9 import net.sf.openrocket.rocketcomponent.FinSet;
10 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12
13
14 public abstract class FinSetConfig extends RocketComponentConfig {
15
16         private JButton split = null;
17         
18         public FinSetConfig(RocketComponent component) {
19                 super(component);
20         }
21
22         
23         
24         protected void addFinSetButtons() {
25                 JButton convert=null;
26                 
27                 //// Convert buttons
28                 if (!(component instanceof FreeformFinSet)) {
29                         convert = new JButton("Convert to freeform");
30                         convert.setToolTipText("Convert this fin set into a freeform fin set");
31                         convert.addActionListener(new ActionListener() {
32                                 @Override
33                                 public void actionPerformed(ActionEvent e) {
34                                         // Do change in future for overall safety
35                                         SwingUtilities.invokeLater(new Runnable() {
36                                                 @Override
37                                                 public void run() {
38                                                         FreeformFinSet freeform = new FreeformFinSet((FinSet)component);
39                                                         String name = component.getComponentName();
40                                                         
41                                                         if (freeform.getName().startsWith(name)) {
42                                                                 freeform.setName(freeform.getComponentName() + 
43                                                                                 freeform.getName().substring(name.length()));
44                                                         }
45                                                         
46                                                         RocketComponent parent = component.getParent();
47                                                         int index = parent.getChildPosition(component);
48
49                                                         ComponentConfigDialog.addUndoPosition("Convert fin set");
50                                                         parent.removeChild(index);
51                                                         parent.addChild(freeform, index);
52                                                         ComponentConfigDialog.showDialog(freeform);
53                                                 }
54                                         });
55
56                                         ComponentConfigDialog.hideDialog();
57                                 }
58                         });
59                 }
60
61                 split = new JButton("Split fins");
62                 split.setToolTipText("Split the fin set into separate fins");
63                 split.addActionListener(new ActionListener() {
64                         @Override
65                         public void actionPerformed(ActionEvent e) {
66                                 // Do change in future for overall safety
67                                 SwingUtilities.invokeLater(new Runnable() {
68                                         @Override
69                                         public void run() {
70                                                 RocketComponent parent = component.getParent();
71                                                 int index = parent.getChildPosition(component);
72                                                 int count = ((FinSet)component).getFinCount();
73                                                 double base = ((FinSet)component).getBaseRotation();
74                                                 if (count <= 1)
75                                                         return;
76                                                 
77                                                 ComponentConfigDialog.addUndoPosition("Split fin set");
78                                                 parent.removeChild(index);
79                                                 for (int i=0; i<count; i++) {
80                                                         FinSet copy = (FinSet)component.copy();
81                                                         copy.setFinCount(1);
82                                                         copy.setBaseRotation(base + i*2*Math.PI/count);
83                                                         copy.setName(copy.getName() + " #" + (i+1));
84                                                         parent.addChild(copy, index+i);
85                                                 }
86                                         }
87                                 });
88
89                                 ComponentConfigDialog.hideDialog();
90                         }
91                 });
92                 split.setEnabled(((FinSet)component).getFinCount() > 1);
93                 
94                 if (convert==null)
95                         addButtons(split);
96                 else
97                         addButtons(split,convert);
98
99         }
100
101
102         @Override
103         public void updateFields() {
104                 super.updateFields();
105                 if (split != null)
106                         split.setEnabled(((FinSet)component).getFinCount() > 1);
107         }
108 }