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.projections;
17  
18  import javax.swing.JPanel;
19  import javax.swing.event.TreeSelectionEvent;
20  import javax.swing.event.TreeSelectionListener;
21  import javax.swing.tree.TreePath;
22  
23  /**
24   * Default selection listener for projection trees.
25   * 
26   * @author Christina Bohk
27   * @author Roland Ewald
28   * 
29   */
30  public class ProjectionTreeSelectionListener implements TreeSelectionListener {
31  
32  	/** The content panel. */
33  	private final JPanel contentPanel;
34  
35  	/** The projection tree. */
36  	private final IProjectionTree projectionTree;
37  
38  	/**
39  	 * Instantiates a new projection tree selection listener.
40  	 * 
41  	 * @param content
42  	 *          the content
43  	 * @param tree
44  	 *          the tree
45  	 */
46  	public ProjectionTreeSelectionListener(JPanel content, IProjectionTree tree) {
47  		contentPanel = content;
48  		projectionTree = tree;
49  	}
50  
51  	@Override
52  	public void valueChanged(TreeSelectionEvent e) {
53  		TreePath oldPath = e.getOldLeadSelectionPath();
54  		if (oldPath != null) {
55  			((ProjectionTreeNode<?>) oldPath.getLastPathComponent()).deselected();
56  		}
57  		contentPanel.removeAll();
58  		TreePath newPath = e.getNewLeadSelectionPath();
59  		if (newPath != null) {
60  			((ProjectionTreeNode<?>) newPath.getLastPathComponent()).selected(
61  			    newPath, contentPanel, projectionTree);
62  		}
63  		contentPanel.repaint();
64  		contentPanel.updateUI();
65  	}
66  }