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);
60 * Return the currently selected rocket component. Returns <code>null</code>
61 * if no rocket component is selected.
63 * @return the currently selected rocket component, or <code>null</code>.
65 public RocketComponent getSelectedComponent() {
66 return componentSelection;
73 public void attachComponentTreeSelectionModel(TreeSelectionModel model) {
74 if (componentTreeSelectionModel != null)
75 componentTreeSelectionModel.removeTreeSelectionListener(
76 componentTreeSelectionListener);
78 componentTreeSelectionModel = model;
80 model.addTreeSelectionListener(componentTreeSelectionListener);
81 clearComponentSelection();
86 public void attachSimulationListSelectionModel(ListSelectionModel model) {
87 if (simulationListSelectionModel != null)
88 simulationListSelectionModel.removeListSelectionListener(
89 simulationListSelectionListener);
91 simulationListSelectionModel = model;
93 model.addListSelectionListener(simulationListSelectionListener);
94 clearSimulationSelection();
99 public void clearSimulationSelection() {
100 if (simulationSelection.length == 0)
103 simulationSelection = NO_SIMULATION;
104 if (simulationListSelectionModel != null)
105 simulationListSelectionModel.clearSelection();
107 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);
111 public void clearComponentSelection() {
112 if (componentSelection == null)
115 componentSelection = null;
116 if (componentTreeSelectionModel != null)
117 componentTreeSelectionModel.clearSelection();
119 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
124 public void addDocumentSelectionListener(DocumentSelectionListener l) {
128 public void removeDocumentSelectionListener(DocumentSelectionListener l) {
132 protected void fireDocumentSelection(int type) {
133 DocumentSelectionListener[] array =
134 listeners.toArray(new DocumentSelectionListener[0]);
136 for (DocumentSelectionListener l: array) {
137 l.valueChanged(type);
143 private class ComponentTreeSelectionListener implements TreeSelectionListener {
146 public void valueChanged(TreeSelectionEvent e) {
147 TreePath path = componentTreeSelectionModel.getSelectionPath();
149 componentSelection = null;
150 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
154 componentSelection = (RocketComponent)path.getLastPathComponent();
156 clearSimulationSelection();
157 fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
162 private class SimulationListSelectionListener implements ListSelectionListener {
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);
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));
181 simulationSelection = list.toArray(NO_SIMULATION);
183 clearComponentSelection();
184 fireDocumentSelection(DocumentSelectionListener.SIMULATION_SELECTION_CHANGE);