001: package org.columba.core.gui.context;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Font;
007: import java.awt.Graphics;
008: import java.awt.Insets;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011:
012: import javax.swing.Action;
013: import javax.swing.BorderFactory;
014: import javax.swing.JLabel;
015: import javax.swing.JPanel;
016: import javax.swing.UIManager;
017: import javax.swing.border.Border;
018: import javax.swing.border.CompoundBorder;
019:
020: import org.columba.api.gui.frame.IFrameMediator;
021: import org.columba.core.context.ContextResultListenerAdapter;
022: import org.columba.core.context.api.IContextProvider;
023: import org.columba.core.context.api.IContextResultEvent;
024: import org.columba.core.context.api.IContextSearchManager;
025: import org.jdesktop.swingx.JXCollapsiblePane;
026: import org.jdesktop.swingx.JXHyperlink;
027:
028: public class ContextResultBox extends JPanel {
029:
030: private final static Color borderColor2 = UIManager
031: .getColor("controlShadow");
032:
033: private final static Color borderColor1 = UIManager
034: .getColor("controlHighlight");
035:
036: private JXHyperlink link;
037:
038: private JXHyperlink moreLink;
039:
040: private JXCollapsiblePane collapsible;
041:
042: private IContextProvider provider;
043:
044: private IContextSearchManager contextSearchManager;
045:
046: public ContextResultBox(final IFrameMediator frameMediator,
047: final IContextProvider provider,
048: final IContextSearchManager contextSearchManager) {
049: super ();
050:
051: this .provider = provider;
052: this .contextSearchManager = contextSearchManager;
053:
054: // get notified if search result arrived, to update view
055: this .contextSearchManager
056: .addResultListener(new MyContextResultListener());
057:
058: collapsible = new JXCollapsiblePane();
059: // collapsible.getContentPane().setBackground(Color.WHITE);
060: collapsible.add(provider.getView());
061: collapsible.setCollapsed(true);
062:
063: Action toggleAction = collapsible.getActionMap().get(
064: JXCollapsiblePane.TOGGLE_ACTION);
065: // use the collapse/expand icons from the JTree UI
066: toggleAction.putValue(JXCollapsiblePane.COLLAPSE_ICON,
067: UIManager.getIcon("Tree.expandedIcon"));
068: toggleAction.putValue(JXCollapsiblePane.EXPAND_ICON, UIManager
069: .getIcon("Tree.collapsedIcon"));
070: link = new JXHyperlink(toggleAction);
071: link.setText(provider.getName());
072: link.setToolTipText(provider.getDescription());
073:
074: // link.setFont(link.getFont().deriveFont(Font.BOLD));
075: link.setOpaque(true);
076: // link.setBackground(titleBackground);
077: link.setFocusPainted(false);
078:
079: link.setUnclickedColor(UIManager.getColor("Label.foreground"));
080: link.setClickedColor(UIManager.getColor("Label.foreground"));
081:
082: moreLink = new JXHyperlink();
083: moreLink.setEnabled(false);
084: moreLink.setText("Show More ..");
085: Font font = UIManager.getFont("Label.font");
086: Font smallFont = new Font(font.getName(), font.getStyle(), font
087: .getSize() - 2);
088: moreLink.setFont(smallFont);
089: moreLink.addActionListener(new ActionListener() {
090: public void actionPerformed(ActionEvent e) {
091: provider.showMoreResults(frameMediator);
092: }
093: });
094:
095: setLayout(new BorderLayout());
096: JPanel top = new JPanel();
097:
098: top.setBorder(new CompoundBorder(new SeparatorBorder(),
099: BorderFactory.createEmptyBorder(2, 4, 2, 4)));
100:
101: top.setLayout(new BorderLayout());
102: JLabel iconLabel = new JLabel();
103:
104: iconLabel.setIcon(provider.getIcon());
105:
106: iconLabel
107: .setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 6));
108: top.add(iconLabel, BorderLayout.WEST);
109: top.add(link, BorderLayout.CENTER);
110: top.add(moreLink, BorderLayout.EAST);
111: add(top, BorderLayout.NORTH);
112: add(collapsible, BorderLayout.CENTER);
113: }
114:
115: public void showResults() {
116:
117: if (provider.isEnabledShowMoreLink()) {
118: int count = provider.getTotalResultCount();
119: moreLink.setEnabled(count > 0);
120:
121: if (count > 0) {
122: moreLink.setText("Show More (" + count + ") ..");
123: }
124: }
125: }
126:
127: class MyContextResultListener extends ContextResultListenerAdapter {
128:
129: @Override
130: public void resultArrived(final IContextResultEvent event) {
131: if (!event.getProviderName().equals(
132: provider.getTechnicalName()))
133: return;
134:
135: provider.showResult();
136: showResults();
137:
138: }
139:
140: }
141:
142: /**
143: * The border between the stack components. It separates each component with
144: * a fine line border.
145: */
146: class SeparatorBorder implements Border {
147:
148: boolean isFirst(Component c) {
149: return c.getParent() == null
150: || c.getParent().getComponent(0) == c;
151: }
152:
153: public Insets getBorderInsets(Component c) {
154: // if the collapsible is collapsed, we do not want its border to be
155: // painted.
156: if (c instanceof JXCollapsiblePane) {
157: if (((JXCollapsiblePane) c).isCollapsed()) {
158: return new Insets(0, 0, 0, 0);
159: }
160: }
161: return new Insets(isFirst(c) ? 4 : 1, 0, 1, 0);
162: }
163:
164: public boolean isBorderOpaque() {
165: return true;
166: }
167:
168: public void paintBorder(Component c, Graphics g, int x, int y,
169: int width, int height) {
170: g.setColor(borderColor1);
171: if (isFirst(c)) {
172: g.drawLine(x, y + 2, x + width, y + 2);
173: }
174: // g.setColor(borderColor2);
175: // g.drawLine(x, y + height - 1, x + width, y + height - 1);
176: }
177: }
178:
179: }
|