1 package net.sf.openrocket.gui.main;
3 import java.util.ArrayList;
4 import java.util.Arrays;
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;
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;
20 public class DocumentSelectionModel {
22 private static final Simulation[] NO_SIMULATION = new Simulation[0];
24 private final ComponentTreeSelectionListener componentTreeSelectionListener =
25 new ComponentTreeSelectionListener();
26 private final SimulationListSelectionListener simulationListSelectionListener =
27 new SimulationListSelectionListener();
30 private final OpenRocketDocument document;
32 private RocketComponent componentSelection = null;
33 private Simulation[] simulationSelection = NO_SIMULATION;
35 private TreeSelectionModel componentTreeSelectionModel = null;
36 private ListSelectionModel simulationListSelectionModel = null;
38 private final List<DocumentSelectionListener> listeners =
39 new ArrayList<DocumentSelectionListener>();
43 public DocumentSelectionModel(OpenRocketDocument document) {
44 this.document = document;
51 * Return the currently selected simulations. Returns an empty array if none
54 * @return an array of the currently selected simulations, may be of zero length.
56 public Simulation[] getSelectedSimulations() {
57 return Arrays.copyOf(simulationSelection, simulationSelection.length);
60 public void setSelectedSimulations(Simulation[] sims) {
61 simulationSelection = sims;
62 clearComponentSelection();
64 simulationListSelectionModel.clearSelection();
65 for (Simulation s: sims) {
66 int index = document.getSimulationIndex(s);
68 simulationListSelectionModel.addSelectionInterval(index, index);
74 * Return the currently selected rocket component. Returns <code>null</code>
75 * if no rocket component is selected.
77 * @return the currently selected rocket component, or <code>null</code>.
79 public RocketComponent getSelectedComponent() {
80 return componentSelection;
83 public void setSelectedComponent(RocketComponent component) {
84 componentSelection = component;
85 clearSimulationSelection();
87 TreePath path = ComponentTreeModel.makeTreePath(component);
88 componentTreeSelectionModel.setSelectionPath(path);
95 public void attachComponentTreeSelectionModel(TreeSelectionModel model) {
96 if (componentTreeSelectionModel != null)
97 componentTreeSelectionModel.removeTreeSelectionListener(
98 componentTreeSelectionListener);
100 componentTreeSelectionModel = model;
102 model.addTreeSelectionListener(componentTreeSelectionListener);
103 clearComponentSelection();
108 public void attachSimulationListSelectionModel(ListSelectionModel model) {
109 if (simulationListSelectionModel != null)
110 simulationListSelectionModel.removeListSelectionListener(
111 simulationListSelectionListener);
113 simulationListSelectionModel = model;
115 model.addListSelectionListener(simulationListSelectionListener);
116 clearSimulationSelection();
121 public void clearSimulationSelection() {
122 if (simulationSelection.length == 0)
125 simulationSelection = NO_SIMULATION;
126 if (simulationListSelectionModel != null)
127 simulationListSelectionModel.clearSelection();
129 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
133 public void clearComponentSelection() {
134 if (componentSelection == null)
137 componentSelection = null;
138 if (componentTreeSelectionModel != null)
139 componentTreeSelectionModel.clearSelection();
141 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
146 public void addDocumentSelectionListener(DocumentSelectionListener l) {
150 public void removeDocumentSelectionListener(DocumentSelectionListener l) {
154 protected void fireDocumentSelection(int type) {
155 DocumentSelectionListener[] array =
156 listeners.toArray(new DocumentSelectionListener[0]);
158 for (DocumentSelectionListener l: array) {
159 l.valueChanged(type);
165 private class ComponentTreeSelectionListener implements TreeSelectionListener {
168 public void valueChanged(TreeSelectionEvent e) {
169 TreePath path = componentTreeSelectionModel.getSelectionPath();
171 componentSelection = null;
172 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
176 componentSelection = (RocketComponent)path.getLastPathComponent();
178 clearSimulationSelection();
179 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
184 private class SimulationListSelectionListener implements ListSelectionListener {
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);
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));
203 simulationSelection = list.toArray(NO_SIMULATION);
205 clearComponentSelection();
206 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);