create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / StageSelector.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.event.ActionEvent;
4 import java.util.ArrayList;
5 import java.util.EventObject;
6 import java.util.List;
7
8 import javax.swing.AbstractAction;
9 import javax.swing.JPanel;
10 import javax.swing.JToggleButton;
11
12 import net.miginfocom.swing.MigLayout;
13 import net.sf.openrocket.l10n.Translator;
14 import net.sf.openrocket.rocketcomponent.Configuration;
15 import net.sf.openrocket.startup.Application;
16 import net.sf.openrocket.util.StateChangeListener;
17
18
19 public class StageSelector extends JPanel implements StateChangeListener {
20         private static final Translator trans = Application.getTranslator();
21         
22         private final Configuration configuration;
23         
24         private List<JToggleButton> buttons = new ArrayList<JToggleButton>();
25         
26         public StageSelector(Configuration configuration) {
27                 super(new MigLayout("gap 0!"));
28                 this.configuration = configuration;
29                 
30                 JToggleButton button = new JToggleButton(new StageAction(0));
31                 this.add(button);
32                 buttons.add(button);
33                 
34                 updateButtons();
35                 configuration.addChangeListener(this);
36         }
37         
38         private void updateButtons() {
39                 int stages = configuration.getStageCount();
40                 if (buttons.size() == stages)
41                         return;
42                 
43                 while (buttons.size() > stages) {
44                         JToggleButton button = buttons.remove(buttons.size() - 1);
45                         this.remove(button);
46                 }
47                 
48                 while (buttons.size() < stages) {
49                         JToggleButton button = new JToggleButton(new StageAction(buttons.size()));
50                         this.add(button);
51                         buttons.add(button);
52                 }
53                 
54                 this.revalidate();
55         }
56         
57         
58
59
60         @Override
61         public void stateChanged(EventObject e) {
62                 updateButtons();
63         }
64         
65         
66         private class StageAction extends AbstractAction implements StateChangeListener {
67                 private final int stage;
68                 
69                 public StageAction(final int stage) {
70                         this.stage = stage;
71                         configuration.addChangeListener(this);
72                         stateChanged(null);
73                 }
74                 
75                 @Override
76                 public Object getValue(String key) {
77                         if (key.equals(NAME)) {
78                                 //// Stage
79                                 return trans.get("StageAction.Stage") + " " + (stage + 1);
80                         }
81                         return super.getValue(key);
82                 }
83                 
84                 @Override
85                 public void actionPerformed(ActionEvent e) {
86                         configuration.setToStage(stage);
87                         
88                         //                      boolean state = (Boolean)getValue(SELECTED_KEY);
89                         //                      if (state == true) {
90                         //                              // Was disabled, now enabled
91                         //                              configuration.setToStage(stage);
92                         //                      } else {
93                         //                              // Was enabled, check what to do
94                         //                              if (configuration.isStageActive(stage + 1)) {
95                         //                                      configuration.setToStage(stage);
96                         //                              } else {
97                         //                                      if (stage == 0)
98                         //                                              configuration.setAllStages();
99                         //                                      else 
100                         //                                              configuration.setToStage(stage-1);
101                         //                              }
102                         //                      }
103                         //                      stateChanged(null);
104                 }
105                 
106                 @Override
107                 public void stateChanged(EventObject e) {
108                         this.putValue(SELECTED_KEY, configuration.isStageActive(stage));
109                 }
110         }
111 }