updates
[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         public void setSelectedSimulations(Simulation[] sims) {
60                 simulationSelection = sims;
61                 clearComponentSelection();
62
63                 simulationListSelectionModel.clearSelection();
64                 for (Simulation s: sims) {
65                         int index = document.getSimulationIndex(s);
66                         if (index >= 0) {
67                                 simulationListSelectionModel.addSelectionInterval(index, index);
68                         }
69                 }
70         }
71
72         /**
73          * Return the currently selected rocket component.  Returns <code>null</code>
74          * if no rocket component is selected.
75          * 
76          * @return      the currently selected rocket component, or <code>null</code>.
77          */
78         public RocketComponent getSelectedComponent() {
79                 return componentSelection;
80         }
81         
82         public void setSelectedComponent(RocketComponent component) {
83                 componentSelection = component;
84                 clearSimulationSelection();
85         
86                 TreePath path = ComponentTreeModel.makeTreePath(component);
87                 componentTreeSelectionModel.setSelectionPath(path);
88         }
89
90
91
92         
93
94         public void attachComponentTreeSelectionModel(TreeSelectionModel model) {
95                 if (componentTreeSelectionModel != null)
96                         componentTreeSelectionModel.removeTreeSelectionListener(
97                                         componentTreeSelectionListener);
98
99                 componentTreeSelectionModel = model;
100                 if (model != null)
101                         model.addTreeSelectionListener(componentTreeSelectionListener);
102                 clearComponentSelection();
103         }
104         
105         
106         
107         public void attachSimulationListSelectionModel(ListSelectionModel model) {
108                 if (simulationListSelectionModel != null)
109                         simulationListSelectionModel.removeListSelectionListener(
110                                         simulationListSelectionListener);
111                 
112                 simulationListSelectionModel = model;
113                 if (model != null)
114                         model.addListSelectionListener(simulationListSelectionListener);
115                 clearSimulationSelection();
116         }
117
118         
119         
120         public void clearSimulationSelection() {
121                 if (simulationSelection.length == 0)
122                         return;
123                 
124                 simulationSelection = NO_SIMULATION;
125                 if (simulationListSelectionModel != null)
126                         simulationListSelectionModel.clearSelection();
127                 
128                 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
129         }
130         
131         
132         public void clearComponentSelection() {
133                 if (componentSelection == null)
134                         return;
135                 
136                 componentSelection = null;
137                 if (componentTreeSelectionModel != null)
138                         componentTreeSelectionModel.clearSelection();
139                 
140                 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
141         }
142
143
144         
145         public void addDocumentSelectionListener(DocumentSelectionListener l) {
146                 listeners.add(l);
147         }
148         
149         public void removeDocumentSelectionListener(DocumentSelectionListener l) {
150                 listeners.remove(l);
151         }
152         
153         protected void fireDocumentSelection(int type) {
154                 DocumentSelectionListener[] array = 
155                         listeners.toArray(new DocumentSelectionListener[0]);
156                 
157                 for (DocumentSelectionListener l: array) {
158                         l.valueChanged(type);
159                 }
160         }
161         
162         
163         
164         private class ComponentTreeSelectionListener implements TreeSelectionListener {
165
166                 @Override
167                 public void valueChanged(TreeSelectionEvent e) {
168                         TreePath path = componentTreeSelectionModel.getSelectionPath();
169                         if (path == null) {
170                                 componentSelection = null;
171                                 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
172                                 return;
173                         }
174                         
175                         componentSelection = (RocketComponent)path.getLastPathComponent();
176                         
177                         clearSimulationSelection();
178                         fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
179                 }
180
181         }
182         
183         private class SimulationListSelectionListener implements ListSelectionListener {
184
185                 @Override
186                 public void valueChanged(ListSelectionEvent e) {
187                         int min = simulationListSelectionModel.getMinSelectionIndex();
188                         int max = simulationListSelectionModel.getMaxSelectionIndex();
189                         if (min < 0 || max < 0) {
190                                 simulationSelection = NO_SIMULATION;
191                                 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
192                                 return;
193                         }
194                         
195                         ArrayList<Simulation> list = new ArrayList<Simulation>();
196                         for (int i = min; i <= max; i++) {
197                                 if (simulationListSelectionModel.isSelectedIndex(i) && 
198                                                 (i < document.getSimulationCount())) {
199                                         list.add(document.getSimulation(i));
200                                 }
201                         }
202                         simulationSelection = list.toArray(NO_SIMULATION);
203                         
204                         clearComponentSelection();
205                         fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
206                 }
207                 
208         }
209
210 }