View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.panels;
17  
18  import java.awt.BorderLayout;
19  
20  import javax.swing.JPanel;
21  import javax.swing.JScrollPane;
22  import javax.swing.JTree;
23  import javax.swing.tree.DefaultTreeModel;
24  import javax.swing.tree.TreePath;
25  
26  import p3j.pppm.ProjectionModel;
27  
28  /**
29   * Super class for navigation panels.
30   * 
31   * @author Christina Bohk
32   * @author Roland Ewald
33   * 
34   */
35  public abstract class AbstractNavigationPanel extends JPanel {
36  
37  	/** Serialization ID. */
38  	private static final long serialVersionUID = -8802994657383849297L;
39  
40  	/** Tree to display current structure of the projection. */
41  	private JTree tree;
42  
43  	/** The model of the projection tree. */
44  	private DefaultTreeModel treeModel;
45  
46  	/** Scroll pane for the tree. */
47  	private JScrollPane scrollPane;
48  
49  	/** The projection to be edited. */
50  	private ProjectionModel projection;
51  
52  	/** Panel from {@link import p3j.gui.P3J} that holds the content. */
53  	private final JPanel contentPanel;
54  
55  	/**
56  	 * Instantiates a new abstract navigation panel.
57  	 * 
58  	 * @param projectionModel
59  	 *            the projectionModel
60  	 * @param content
61  	 *            the content panel
62  	 */
63  	public AbstractNavigationPanel(ProjectionModel projectionModel,
64  			JPanel content) {
65  		projection = projectionModel;
66  		contentPanel = content;
67  		if (projection == null) {
68  			return;
69  		}
70  		setProjection(projection);
71  	}
72  
73  	/**
74  	 * Sets the new projection model to be displayed.
75  	 * 
76  	 * @param projMod
77  	 *            the new projection model
78  	 */
79  	public final void setProjection(ProjectionModel projMod) {
80  		projection = projMod;
81  		removeAll();
82  		initTree();
83  		setLayout(new BorderLayout());
84  		if (getScrollPane() != null) {
85  			add(getScrollPane(), BorderLayout.CENTER);
86  		}
87  	}
88  
89  	/**
90  	 * Initializes tree (after projection changed, for example).
91  	 */
92  	protected abstract void initTree();
93  
94  	/**
95  	 * Select root of the tree.
96  	 */
97  	public void selectRoot() {
98  		tree.setSelectionPath(new TreePath(treeModel.getRoot()));
99  	}
100 
101 	public final JTree getTree() {
102 		return tree;
103 	}
104 
105 	public void setTree(JTree tree) {
106 		this.tree = tree;
107 	}
108 
109 	public final DefaultTreeModel getTreeModel() {
110 		return treeModel;
111 	}
112 
113 	public void setTreeModel(DefaultTreeModel treeModel) {
114 		this.treeModel = treeModel;
115 	}
116 
117 	public final JScrollPane getScrollPane() {
118 		return scrollPane;
119 	}
120 
121 	public void setScrollPane(JScrollPane scrollPane) {
122 		this.scrollPane = scrollPane;
123 	}
124 
125 	public ProjectionModel getProjectionModel() {
126 		return projection;
127 	}
128 
129 	public JPanel getContentPanel() {
130 		return contentPanel;
131 	}
132 }