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.misc;
17  
18  import java.awt.BorderLayout;
19  import java.awt.Color;
20  import java.awt.event.MouseAdapter;
21  import java.awt.event.MouseEvent;
22  import java.util.List;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.BoxLayout;
26  import javax.swing.JLabel;
27  import javax.swing.JPanel;
28  import javax.swing.JScrollPane;
29  
30  import p3j.gui.panels.projections.IProjectionTree;
31  import p3j.gui.panels.projections.ProjectionTreeNode;
32  import p3j.misc.gui.GUI;
33  
34  /**
35   * Preview of all entities below a certain node.
36   * 
37   * Created: August 26, 2008
38   * 
39   * @param <E>
40   *          type of the target entity
41   * @author Christina Bohk
42   * @author Roland Ewald
43   * 
44   */
45  public class SubNodeSummary<E> extends JPanel {
46  
47  	/**
48  	 * Serialization ID.
49  	 */
50  	private static final long serialVersionUID = 6979550281628665829L;
51  
52  	/**
53  	 * Default constructor.
54  	 * 
55  	 * @param node
56  	 *          the current node
57  	 * @param projTree
58  	 *          the overall projection tree
59  	 * @param desiredClass
60  	 *          the class of the target entities
61  	 */
62  	public SubNodeSummary(ProjectionTreeNode<?> node, IProjectionTree projTree,
63  	    Class<E> desiredClass) {
64  		setLayout(GUI.getStdBorderLayout());
65  
66  		JPanel panel = new JPanel();
67  		BoxLayout bl = new BoxLayout(panel, BoxLayout.Y_AXIS);
68  		panel.setLayout(bl);
69  
70  		List<ProjectionTreeNode<E>> children = node.getChildsByType(desiredClass);
71  
72  		for (ProjectionTreeNode<E> child : children) {
73  			String str = child.getEntityLabel();
74  			JLabel label = new JLabel("<html><u>" + str + "</u></html>");
75  			label.setForeground(Color.BLUE);
76  			label.addMouseListener(new ClickAssignmentListener<E>(child.getEntity(),
77  			    node, projTree));
78  
79  			panel.add(label);
80  		}
81  
82  		JScrollPane scrollPane = new JScrollPane(panel);
83  		scrollPane.setBorder(BorderFactory.createEmptyBorder());
84  		add(scrollPane, BorderLayout.CENTER);
85  	}
86  
87  }
88  
89  /**
90   * Action listener that navigates to the {@link ProjectionTreeNode} holding the
91   * given target entity after clicking.
92   * 
93   * Created: August 26, 2008
94   * 
95   * @param <E>
96   *          type of the target entity
97   * 
98   * @author Christina Bohk
99   * @author Roland Ewald
100  * 
101  */
102 class ClickAssignmentListener<E> extends MouseAdapter {
103 
104 	/** The target entity. */
105 	private final E entity;
106 
107 	/** The current node. */
108 	private final ProjectionTreeNode<?> node;
109 
110 	/** Reference to the projection tree. */
111 	private final IProjectionTree projectionTree;
112 
113 	ClickAssignmentListener(E targetEntity, ProjectionTreeNode<?> piNode,
114 	    IProjectionTree projTree) {
115 		entity = targetEntity;
116 		node = piNode;
117 		projectionTree = projTree;
118 	}
119 
120 	@Override
121 	public void mouseClicked(MouseEvent e) {
122 		projectionTree.selectNode(node.getChildWithEntity(entity));
123 	}
124 }