001: package org.columba.core.gui.search;
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.gui.search.api.IResultPanel;
022: import org.columba.core.search.ResultListenerAdapter;
023: import org.columba.core.search.api.IResultEvent;
024: import org.columba.core.search.api.ISearchCriteria;
025: import org.columba.core.search.api.ISearchManager;
026: import org.columba.core.search.api.ISearchProvider;
027: import org.jdesktop.swingx.JXCollapsiblePane;
028: import org.jdesktop.swingx.JXHyperlink;
029:
030: public class SearchResultBox extends JPanel {
031:
032: private final static Color borderColor2 = UIManager
033: .getColor("controlShadow");
034:
035: private final static Color borderColor1 = UIManager
036: .getColor("controlHighlight");
037:
038: private JXHyperlink link;
039:
040: private JXHyperlink moreLink;
041:
042: private JXCollapsiblePane collapsible;
043:
044: private IResultPanel resultPanel;
045:
046: private ISearchCriteria criteria;
047:
048: private ISearchProvider searchProvider;
049:
050: /**
051: *
052: * @param mediator
053: * @param p
054: * @param criteria
055: * can be <code>null</code>
056: * @param resultPanel
057: */
058: public SearchResultBox(final IFrameMediator mediator,
059: final ISearchProvider p, final ISearchCriteria criteria,
060: IResultPanel resultPanel) {
061: this .resultPanel = resultPanel;
062: this .criteria = criteria;
063: this .searchProvider = p;
064:
065: collapsible = new JXCollapsiblePane();
066: //collapsible.getContentPane().setBackground(Color.WHITE);
067: collapsible.add(resultPanel.getView());
068:
069: Action toggleAction = collapsible.getActionMap().get(
070: JXCollapsiblePane.TOGGLE_ACTION);
071: // use the collapse/expand icons from the JTree UI
072: toggleAction.putValue(JXCollapsiblePane.COLLAPSE_ICON,
073: UIManager.getIcon("Tree.expandedIcon"));
074: toggleAction.putValue(JXCollapsiblePane.EXPAND_ICON, UIManager
075: .getIcon("Tree.collapsedIcon"));
076: link = new JXHyperlink(toggleAction);
077: if (criteria != null)
078: link.setText(criteria.getTitle());
079: else
080: link.setText(p.getName());
081:
082: if (criteria != null)
083: link.setToolTipText(criteria.getDescription());
084: else
085: link.setToolTipText(p.getDescription());
086:
087: // link.setFont(link.getFont().deriveFont(Font.BOLD));
088: link.setOpaque(true);
089: //link.setBackground(titleBackground);
090: link.setFocusPainted(false);
091:
092: link.setUnclickedColor(UIManager.getColor("Label.foreground"));
093: link.setClickedColor(UIManager.getColor("Label.foreground"));
094:
095: moreLink = new JXHyperlink();
096: moreLink.setEnabled(false);
097: moreLink.setText("Show More ..");
098: Font font = UIManager.getFont("Label.font");
099: Font smallFont = new Font(font.getName(), font.getStyle(), font
100: .getSize() - 2);
101: moreLink.setFont(smallFont);
102: moreLink.addActionListener(new ActionListener() {
103: public void actionPerformed(ActionEvent e) {
104: if (criteria != null)
105: p.showAllResults(mediator, "test", criteria
106: .getTechnicalName());
107: else
108: p.showAllResults(mediator, "test", null);
109: }
110: });
111:
112: setLayout(new BorderLayout());
113: JPanel top = new JPanel();
114: top.setOpaque(true);
115:
116: Border border1 = new CompoundBorder(new SeparatorBorder(),
117: BorderFactory.createEmptyBorder(2, 4, 2, 4));
118:
119: Border border = new CompoundBorder(BorderFactory
120: .createEmptyBorder(2, 4, 2, 4), border1);
121:
122: top.setBorder(border1);
123: //top.setBackground(titleBackground);
124: top.setLayout(new BorderLayout());
125: JLabel iconLabel = new JLabel();
126: //iconLabel.setBackground(titleBackground);
127: iconLabel.setIcon(p.getIcon());
128: iconLabel.setOpaque(true);
129: iconLabel
130: .setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 6));
131: top.add(iconLabel, BorderLayout.WEST);
132: top.add(link, BorderLayout.CENTER);
133: top.add(moreLink, BorderLayout.EAST);
134: add(top, BorderLayout.NORTH);
135: add(collapsible, BorderLayout.CENTER);
136: }
137:
138: public void installListener(ISearchManager searchManager) {
139: searchManager.addResultListener(new MyResultListener());
140: }
141:
142: class MyResultListener extends ResultListenerAdapter {
143: @Override
144: public void resultArrived(IResultEvent event) {
145: if (event.getProviderName() == null)
146: return;
147:
148: if (!event.getProviderName().equals(
149: resultPanel.getProviderTechnicalName()))
150: return;
151: if (event.getSearchCriteria() != null) {
152: if (!event
153: .getSearchCriteria()
154: .getTechnicalName()
155: .equals(
156: resultPanel
157: .getSearchCriteriaTechnicalName()))
158: return;
159: }
160:
161: if (criteria != null)
162: link.setText(criteria.getTitle());
163: else
164: link.setText(searchProvider.getName());
165:
166: if (criteria != null)
167: link.setToolTipText(criteria.getDescription());
168: else
169: link.setToolTipText(searchProvider.getDescription());
170:
171: if (event.getTotalResultCount() == 0) {
172: moreLink.setText("Show More ..");
173: moreLink.setEnabled(false);
174: } else {
175: moreLink.setText("Show More" + " ("
176: + event.getTotalResultCount() + ") ..");
177: moreLink.setEnabled(true);
178: }
179:
180: revalidate();
181: }
182:
183: MyResultListener() {
184: }
185: }
186:
187: /**
188: * The border between the stack components. It separates each component with
189: * a fine line border.
190: */
191: class SeparatorBorder implements Border {
192:
193: boolean isFirst(Component c) {
194: return c.getParent() == null
195: || c.getParent().getComponent(0) == c;
196: }
197:
198: public Insets getBorderInsets(Component c) {
199: // if the collapsible is collapsed, we do not want its border to be
200: // painted.
201: if (c instanceof JXCollapsiblePane) {
202: if (((JXCollapsiblePane) c).isCollapsed()) {
203: return new Insets(0, 0, 0, 0);
204: }
205: }
206: return new Insets(isFirst(c) ? 4 : 1, 0, 1, 0);
207: }
208:
209: public boolean isBorderOpaque() {
210: return true;
211: }
212:
213: public void paintBorder(Component c, Graphics g, int x, int y,
214: int width, int height) {
215: g.setColor(borderColor1);
216: if (isFirst(c)) {
217: g.drawLine(x, y + 2, x + width, y + 2);
218: }
219: // g.setColor(borderColor2);
220: // g.drawLine(x, y + height - 1, x + width, y + height - 1);
221: }
222: }
223:
224: }
|