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