KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > context > ContextResultBox


1 package org.columba.core.gui.context;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Component JavaDoc;
6 import java.awt.Font JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.Insets JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11
12 import javax.swing.Action JavaDoc;
13 import javax.swing.BorderFactory JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.JPanel JavaDoc;
16 import javax.swing.UIManager JavaDoc;
17 import javax.swing.border.Border JavaDoc;
18 import javax.swing.border.CompoundBorder JavaDoc;
19
20 import org.columba.api.gui.frame.IFrameMediator;
21 import org.columba.core.context.ContextResultListenerAdapter;
22 import org.columba.core.context.api.IContextProvider;
23 import org.columba.core.context.api.IContextResultEvent;
24 import org.columba.core.context.api.IContextSearchManager;
25 import org.jdesktop.swingx.JXCollapsiblePane;
26 import org.jdesktop.swingx.JXHyperlink;
27
28 public class ContextResultBox extends JPanel JavaDoc {
29
30     private final static Color JavaDoc borderColor2 = UIManager
31             .getColor("controlShadow");
32
33     private final static Color JavaDoc borderColor1 = UIManager
34             .getColor("controlHighlight");
35
36     private JXHyperlink link;
37
38     private JXHyperlink moreLink;
39
40     private JXCollapsiblePane collapsible;
41
42     private IContextProvider provider;
43
44     private IContextSearchManager contextSearchManager;
45
46     public ContextResultBox(final IFrameMediator frameMediator,
47             final IContextProvider provider,
48             final IContextSearchManager contextSearchManager) {
49         super();
50
51         this.provider = provider;
52         this.contextSearchManager = contextSearchManager;
53
54         // get notified if search result arrived, to update view
55
this.contextSearchManager
56                 .addResultListener(new MyContextResultListener());
57
58         collapsible = new JXCollapsiblePane();
59         // collapsible.getContentPane().setBackground(Color.WHITE);
60
collapsible.add(provider.getView());
61         collapsible.setCollapsed(true);
62
63         Action JavaDoc toggleAction = collapsible.getActionMap().get(
64                 JXCollapsiblePane.TOGGLE_ACTION);
65         // use the collapse/expand icons from the JTree UI
66
toggleAction.putValue(JXCollapsiblePane.COLLAPSE_ICON, UIManager
67                 .getIcon("Tree.expandedIcon"));
68         toggleAction.putValue(JXCollapsiblePane.EXPAND_ICON, UIManager
69                 .getIcon("Tree.collapsedIcon"));
70         link = new JXHyperlink(toggleAction);
71         link.setText(provider.getName());
72         link.setToolTipText(provider.getDescription());
73
74         // link.setFont(link.getFont().deriveFont(Font.BOLD));
75
link.setOpaque(true);
76         // link.setBackground(titleBackground);
77
link.setFocusPainted(false);
78
79         link.setUnclickedColor(UIManager.getColor("Label.foreground"));
80         link.setClickedColor(UIManager.getColor("Label.foreground"));
81
82         moreLink = new JXHyperlink();
83         moreLink.setEnabled(false);
84         moreLink.setText("Show More ..");
85         Font JavaDoc font = UIManager.getFont("Label.font");
86         Font JavaDoc smallFont = new Font JavaDoc(font.getName(), font.getStyle(), font
87                 .getSize() - 2);
88         moreLink.setFont(smallFont);
89         moreLink.addActionListener(new ActionListener JavaDoc() {
90             public void actionPerformed(ActionEvent JavaDoc e) {
91                 provider.showMoreResults(frameMediator);
92             }
93         });
94
95         setLayout(new BorderLayout JavaDoc());
96         JPanel JavaDoc top = new JPanel JavaDoc();
97
98         top.setBorder(new CompoundBorder JavaDoc(new SeparatorBorder(), BorderFactory
99                 .createEmptyBorder(2, 4, 2, 4)));
100
101         top.setLayout(new BorderLayout JavaDoc());
102         JLabel JavaDoc iconLabel = new JLabel JavaDoc();
103
104         iconLabel.setIcon(provider.getIcon());
105
106         iconLabel.setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 6));
107         top.add(iconLabel, BorderLayout.WEST);
108         top.add(link, BorderLayout.CENTER);
109         top.add(moreLink, BorderLayout.EAST);
110         add(top, BorderLayout.NORTH);
111         add(collapsible, BorderLayout.CENTER);
112     }
113
114     public void showResults() {
115
116         if (provider.isEnabledShowMoreLink()) {
117             int count = provider.getTotalResultCount();
118             moreLink.setEnabled(count > 0);
119
120             if (count > 0) {
121                 moreLink.setText("Show More (" + count + ") ..");
122             }
123         }
124     }
125
126     class MyContextResultListener extends ContextResultListenerAdapter {
127
128         @Override JavaDoc
129         public void resultArrived(final IContextResultEvent event) {
130             if (!event.getProviderName().equals(provider.getTechnicalName()))
131                 return;
132
133             provider.showResult();
134             showResults();
135
136         }
137
138     }
139
140     /**
141      * The border between the stack components. It separates each component with
142      * a fine line border.
143      */

144     class SeparatorBorder implements Border JavaDoc {
145
146         boolean isFirst(Component JavaDoc c) {
147             return c.getParent() == null || c.getParent().getComponent(0) == c;
148         }
149
150         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
151             // if the collapsible is collapsed, we do not want its border to be
152
// painted.
153
if (c instanceof JXCollapsiblePane) {
154                 if (((JXCollapsiblePane) c).isCollapsed()) {
155                     return new Insets JavaDoc(0, 0, 0, 0);
156                 }
157             }
158             return new Insets JavaDoc(isFirst(c) ? 4 : 1, 0, 1, 0);
159         }
160
161         public boolean isBorderOpaque() {
162             return true;
163         }
164
165         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
166                 int width, int height) {
167             g.setColor(borderColor1);
168             if (isFirst(c)) {
169                 g.drawLine(x, y + 2, x + width, y + 2);
170             }
171 // g.setColor(borderColor2);
172
// g.drawLine(x, y + height - 1, x + width, y + height - 1);
173
}
174     }
175
176 }
177
Popular Tags