Bug fixes and startup checks
[debian/openrocket] / src / net / sf / openrocket / gui / main / DocumentSelectionModel.java
1 package net.sf.openrocket.gui.main;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import javax.swing.ListSelectionModel;
8 import javax.swing.event.ListSelectionEvent;
9 import javax.swing.event.ListSelectionListener;
10 import javax.swing.event.TreeSelectionEvent;
11 import javax.swing.event.TreeSelectionListener;
12 import javax.swing.tree.TreePath;
13 import javax.swing.tree.TreeSelectionModel;
14
15 import net.sf.openrocket.document.OpenRocketDocument;
16 import net.sf.openrocket.document.Simulation;
17 import net.sf.openrocket.rocketcomponent.RocketComponent;
18
19 public class DocumentSelectionModel {
20
21         private static final Simulation[] NO_SIMULATION = new Simulation[0];
22
23         private final ComponentTreeSelectionListener componentTreeSelectionListener =
24                 new ComponentTreeSelectionListener();
25         private final SimulationListSelectionListener simulationListSelectionListener =
26                 new SimulationListSelectionListener();
27
28         
29         private final OpenRocketDocument document;
30         
31         private RocketComponent componentSelection = null;
32         private Simulation[] simulationSelection = NO_SIMULATION;
33
34         private TreeSelectionModel componentTreeSelectionModel = null; 
35         private ListSelectionModel simulationListSelectionModel = null;
36
37         private final List<DocumentSelectionListener> listeners =
38                 new ArrayList<DocumentSelectionListener>();
39
40         
41         
42         public DocumentSelectionModel(OpenRocketDocument document) {
43                 this.document = document;
44         }
45         
46
47
48
49         /**
50          * Return the currently selected simulations.  Returns an empty array if none
51          * are selected.
52          * 
53          * @return      an array of the currently selected simulations, may be of zero length.
54          */
55         public Simulation[] getSelectedSimulations() {
56                 return Arrays.copyOf(simulationSelection, simulationSelection.length);
57         }
58
59         /**
60          * Return the currently selected rocket component.  Returns <code>null</code>
61          * if no rocket component is selected.
62          * 
63          * @return      the currently selected rocket component, or <code>null</code>.
64          */
65         public RocketComponent getSelectedComponent() {
66                 return componentSelection;
67         }
68
69
70
71         
72
73         public void attachComponentTreeSelectionModel(TreeSelectionModel model) {
74                 if (componentTreeSelectionModel != null)
75                         componentTreeSelectionModel.removeTreeSelectionListener(
76                                         componentTreeSelectionListener);
77
78                 componentTreeSelectionModel = model;
79                 if (model != null)
80                         model.addTreeSelectionListener(componentTreeSelectionListener);
81                 clearComponentSelection();
82         }
83         
84         
85         
86         public void attachSimulationListSelectionModel(ListSelectionModel model) {
87                 if (simulationListSelectionModel != null)
88                         simulationListSelectionModel.removeListSelectionListener(
89                                         simulationListSelectionListener);
90                 
91                 simulationListSelectionModel = model;
92                 if (model != null)
93                         model.addListSelectionListener(simulationListSelectionListener);
94                 clearSimulationSelection();
95         }
96
97         
98         
99         public void clearSimulationSelection() {
100                 if (simulationSelection.length == 0)
101                         return;
102                 
103                 simulationSelection = NO_SIMULATION;
104                 if (simulationListSelectionModel != null)
105                         simulationListSelectionModel.clearSelection();
106                 
107                 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
108         }
109         
110         
111         public void clearComponentSelection() {
112                 if (componentSelection == null)
113                         return;
114                 
115                 componentSelection = null;
116                 if (componentTreeSelectionModel != null)
117                         componentTreeSelectionModel.clearSelection();
118                 
119                 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
120         }
121
122
123         
124         public void addDocumentSelectionListener(DocumentSelectionListener l) {
125                 listeners.add(l);
126         }
127         
128         public void removeDocumentSelectionListener(DocumentSelectionListener l) {
129                 listeners.remove(l);
130         }
131         
132         protected void fireDocumentSelection(int type) {
133                 DocumentSelectionListener[] array = 
134                         listeners.toArray(new DocumentSelectionListener[0]);
135                 
136                 for (DocumentSelectionListener l: array) {
137                         l.valueChanged(type);
138                 }
139         }
140         
141         
142         
143         private class ComponentTreeSelectionListener implements TreeSelectionListener {
144
145                 @Override
146                 public void valueChanged(TreeSelectionEvent e) {
147                         TreePath path = componentTreeSelectionModel.getSelectionPath();
148                         if (path == null) {
149                                 componentSelection = null;
150                                 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
151                                 return;
152                         }
153                         
154                         componentSelection = (RocketComponent)path.getLastPathComponent();
155                         
156                         clearSimulationSelection();
157                         fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
158                 }
159
160         }
161         
162         private class SimulationListSelectionListener implements ListSelectionListener {
163
164                 @Override
165                 public void valueChanged(ListSelectionEvent e) {
166                         int min = simulationListSelectionModel.getMinSelectionIndex();
167                         int max = simulationListSelectionModel.getMaxSelectionIndex();
168                         if (min < 0 || max < 0) {
169                                 simulationSelection = NO_SIMULATION;
170                                 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
171                                 return;
172                         }
173                         
174                         ArrayList<Simulation> list = new ArrayList<Simulation>();
175                         for (int i = min; i <= max; i++) {
176                                 if (simulationListSelectionModel.isSelectedIndex(i) && 
177                                                 (i < document.getSimulationCount())) {
178                                         list.add(document.getSimulation(i));
179                                 }
180                         }
181                         simulationSelection = list.toArray(NO_SIMULATION);
182                         
183                         clearComponentSelection();
184                         fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
185                 }
186                 
187         }
188
189 }