13a98890dee5feee66c1b6816b32d708242db6ff
[debian/openrocket] / 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.List;
6
7 import javax.swing.AbstractAction;
8 import javax.swing.JPanel;
9 import javax.swing.JToggleButton;
10 import javax.swing.event.ChangeEvent;
11 import javax.swing.event.ChangeListener;
12
13 import net.miginfocom.swing.MigLayout;
14 import net.sf.openrocket.l10n.Translator;
15 import net.sf.openrocket.rocketcomponent.Configuration;
16 import net.sf.openrocket.startup.Application;
17
18
19 public class StageSelector extends JPanel implements ChangeListener {
20
21         private final Configuration configuration;
22         
23         private List<JToggleButton> buttons = new ArrayList<JToggleButton>();
24         
25         public StageSelector(Configuration configuration) {
26                 super(new MigLayout("gap 0!"));
27                 this.configuration = configuration;
28                 
29                 JToggleButton button = new JToggleButton(new StageAction(0));
30                 this.add(button);
31                 buttons.add(button);
32                 
33                 updateButtons();
34                 configuration.addChangeListener(this);
35         }
36         
37         private void updateButtons() {
38                 int stages = configuration.getStageCount();
39                 if (buttons.size() == stages)
40                         return;
41                 
42                 while (buttons.size() > stages) {
43                         JToggleButton button = buttons.remove(buttons.size()-1);
44                         this.remove(button);
45                 }
46                 
47                 while (buttons.size() < stages) {
48                         JToggleButton button = new JToggleButton(new StageAction(buttons.size()));
49                         this.add(button);
50                         buttons.add(button);
51                 }
52                 
53                 this.revalidate();
54         }
55         
56
57
58
59         @Override
60         public void stateChanged(ChangeEvent e) {
61                 updateButtons();
62         }
63         
64         
65         private class StageAction extends AbstractAction implements ChangeListener {
66                 private final int stage;
67                 private final Translator trans = Application.getTranslator();
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
107                 @Override
108                 public void stateChanged(ChangeEvent e) {
109                         this.putValue(SELECTED_KEY, configuration.isStageActive(stage));
110                 }
111         }
112 }