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