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.rocketcomponent.RocketComponent;
19 public class DocumentSelectionModel {
21 private static final Simulation[] NO_SIMULATION = new Simulation[0];
23 private final ComponentTreeSelectionListener componentTreeSelectionListener =
24 new ComponentTreeSelectionListener();
25 private final SimulationListSelectionListener simulationListSelectionListener =
26 new SimulationListSelectionListener();
29 private final OpenRocketDocument document;
31 private RocketComponent componentSelection = null;
32 private Simulation[] simulationSelection = NO_SIMULATION;
34 private TreeSelectionModel componentTreeSelectionModel = null;
35 private ListSelectionModel simulationListSelectionModel = null;
37 private final List<DocumentSelectionListener> listeners =
38 new ArrayList<DocumentSelectionListener>();
42 public DocumentSelectionModel(OpenRocketDocument document) {
43 this.document = document;
50 * Return the currently selected simulations. Returns an empty array if none
53 * @return an array of the currently selected simulations, may be of zero length.
55 public Simulation[] getSelectedSimulations() {
56 return Arrays.copyOf(simulationSelection, simulationSelection.length);
59 public void setSelectedSimulations(Simulation[] sims) {
60 simulationSelection = sims;
61 clearComponentSelection();
63 simulationListSelectionModel.clearSelection();
64 for (Simulation s: sims) {
65 int index = document.getSimulationIndex(s);
67 simulationListSelectionModel.addSelectionInterval(index, index);
73 * Return the currently selected rocket component. Returns <code>null</code>
74 * if no rocket component is selected.
76 * @return the currently selected rocket component, or <code>null</code>.
78 public RocketComponent getSelectedComponent() {
79 return componentSelection;
82 public void setSelectedComponent(RocketComponent component) {
83 componentSelection = component;
84 clearSimulationSelection();
86 TreePath path = ComponentTreeModel.makeTreePath(component);
87 componentTreeSelectionModel.setSelectionPath(path);
94 public void attachComponentTreeSelectionModel(TreeSelectionModel model) {
95 if (componentTreeSelectionModel != null)
96 componentTreeSelectionModel.removeTreeSelectionListener(
97 componentTreeSelectionListener);
99 componentTreeSelectionModel = model;
101 model.addTreeSelectionListener(componentTreeSelectionListener);
102 clearComponentSelection();
107 public void attachSimulationListSelectionModel(ListSelectionModel model) {
108 if (simulationListSelectionModel != null)
109 simulationListSelectionModel.removeListSelectionListener(
110 simulationListSelectionListener);
112 simulationListSelectionModel = model;
114 model.addListSelectionListener(simulationListSelectionListener);
115 clearSimulationSelection();
120 public void clearSimulationSelection() {
121 if (simulationSelection.length == 0)
124 simulationSelection = NO_SIMULATION;
125 if (simulationListSelectionModel != null)
126 simulationListSelectionModel.clearSelection();
128 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
132 public void clearComponentSelection() {
133 if (componentSelection == null)
136 componentSelection = null;
137 if (componentTreeSelectionModel != null)
138 componentTreeSelectionModel.clearSelection();
140 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
145 public void addDocumentSelectionListener(DocumentSelectionListener l) {
149 public void removeDocumentSelectionListener(DocumentSelectionListener l) {
153 protected void fireDocumentSelection(int type) {
154 DocumentSelectionListener[] array =
155 listeners.toArray(new DocumentSelectionListener[0]);
157 for (DocumentSelectionListener l: array) {
158 l.valueChanged(type);
164 private class ComponentTreeSelectionListener implements TreeSelectionListener {
167 public void valueChanged(TreeSelectionEvent e) {
168 TreePath path = componentTreeSelectionModel.getSelectionPath();
170 componentSelection = null;
171 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
175 componentSelection = (RocketComponent)path.getLastPathComponent();
177 clearSimulationSelection();
178 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
183 private class SimulationListSelectionListener implements ListSelectionListener {
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);
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));
202 simulationSelection = list.toArray(NO_SIMULATION);
204 clearComponentSelection();
205 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);