KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > detail > FixedListDetailPane


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser.detail;
9
10 import org.gjt.jclasslib.browser.AbstractDetailPane;
11 import org.gjt.jclasslib.browser.BrowserServices;
12 import org.gjt.jclasslib.util.ExtendedJLabel;
13 import org.gjt.jclasslib.util.GUIHelper;
14
15 import javax.swing.*;
16 import javax.swing.tree.TreePath JavaDoc;
17 import java.awt.*;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 /**
22     Base class for all detail panes with a structure of
23     a fixed number of key-value pairs arranged in a list.
24     
25     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
26     @version $Revision: 1.5 $ $Date: 2003/08/18 08:12:02 $
27 */

28 public abstract class FixedListDetailPane extends AbstractDetailPane {
29     
30     // Visual components
31

32     private java.util.List JavaDoc detailPaneEntries;
33     private JScrollPane scrollPane;
34
35     /**
36         Constructor.
37         @param services the associated browser services.
38      */

39     protected FixedListDetailPane(BrowserServices services) {
40         super(services);
41     }
42
43     /**
44         Add a detail entry consisting of a key value pair.
45         @param key the key label
46         @param value the value label
47      */

48     protected void addDetailPaneEntry(ExtendedJLabel key, ExtendedJLabel value) {
49         addDetailPaneEntry(key, value, null);
50     }
51
52     /**
53         Add a detail entry consisting of a key value pair with a associated comment
54         which is made scrollable.
55         @param key the key label
56         @param value the value label
57         @param comment the comment
58      */

59     protected void addDetailPaneEntry(ExtendedJLabel key,
60                                       ExtendedJLabel value,
61                                       ExtendedJLabel comment) {
62                                           
63         if (detailPaneEntries == null) {
64             detailPaneEntries = new ArrayList JavaDoc();
65         }
66         
67         detailPaneEntries.add(
68                 new DetailPaneEntry(key, value, comment)
69             );
70     }
71     
72     protected void setupComponent() {
73         
74         setupLabels();
75         
76         setLayout(new GridBagLayout());
77         
78         GridBagConstraints gKey = new GridBagConstraints();
79         gKey.anchor = GridBagConstraints.NORTHWEST;
80         gKey.insets = new Insets(1,10,0,10);
81         
82         GridBagConstraints gValue = new GridBagConstraints();
83         gValue.gridx = 1;
84         gValue.anchor = GridBagConstraints.NORTHEAST;
85         gValue.insets = new Insets(1,0,0,5);
86
87         GridBagConstraints gComment = new GridBagConstraints();
88         gComment.gridx = 2;
89         gComment.anchor = GridBagConstraints.NORTHWEST;
90         gComment.insets = new Insets(1,0,0,5);
91         gComment.fill = GridBagConstraints.HORIZONTAL;
92         
93         GridBagConstraints gCommentOnly = (GridBagConstraints)gComment.clone();
94         gCommentOnly.gridx = 1;
95         gCommentOnly.gridwidth = 2;
96         
97         GridBagConstraints gRemainder = new GridBagConstraints();
98         gRemainder.gridx = 2;
99         gRemainder.weightx = gRemainder.weighty = 1;
100         gRemainder.fill = GridBagConstraints.BOTH;
101
102         Iterator JavaDoc it = detailPaneEntries.iterator();
103         while (it.hasNext()) {
104             DetailPaneEntry entry = (DetailPaneEntry)it.next();
105             if (entry == null) {
106                 continue;
107             }
108
109             gComment.gridy = gValue.gridy = ++gKey.gridy;
110             if (entry.key != null) {
111                 add(entry.key, gKey);
112             }
113             if (entry.value != null) {
114                 add(entry.value, gValue);
115             }
116             if (entry.comment != null) {
117                 add(entry.comment, (entry.value == null) ? gCommentOnly : gComment);
118
119                 entry.comment.setAutoTooltip(true);
120             }
121             
122         }
123
124         gRemainder.gridy = gKey.gridy + 1;
125         gRemainder.gridy += addSpecial(gRemainder.gridy);
126
127         add(new JPanel(), gRemainder);
128         scrollPane = new JScrollPane(this);
129         GUIHelper.setDefaultScrollbarUnits(scrollPane);
130         scrollPane.setBorder(null);
131
132     }
133
134     /**
135         Get the scroll pane.
136         @return the scroll pane.
137      */

138     public JScrollPane getScrollPane() {
139         return scrollPane;
140     }
141
142     public void show(TreePath JavaDoc treePath) {
143
144         scrollPane.getViewport().setViewPosition(new Point(0, 0));
145
146     }
147
148     /**
149         Setup all label and fill the <tt>detailPaneEntries</tt> list so that
150         <tt>setupComponent</tt> can layout the pane.
151      */

152     protected abstract void setupLabels();
153
154     /**
155         Hook for derived classes to add additional visual elements.
156         @param gridy the current <tt>gridy</tt> of the <tt>GridbagLayout</tt>.
157         @return the number of added rows.
158      */

159     protected int addSpecial(int gridy) {
160         return 0;
161     }
162
163     private static class DetailPaneEntry {
164         public final ExtendedJLabel key;
165         public final ExtendedJLabel value;
166         public final ExtendedJLabel comment;
167         
168         private DetailPaneEntry(ExtendedJLabel key,
169                                 ExtendedJLabel value,
170                                 ExtendedJLabel comment) {
171             this.key = key;
172             this.value = value;
173             this.comment = comment;
174         }
175     }
176
177     
178 }
179
180
Popular Tags