001: package org.columba.core.gui.search;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Font;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Vector;
011:
012: import javax.swing.BorderFactory;
013: import javax.swing.ButtonGroup;
014: import javax.swing.JButton;
015: import javax.swing.JComponent;
016: import javax.swing.JDialog;
017: import javax.swing.JFrame;
018: import javax.swing.JLabel;
019: import javax.swing.JPanel;
020: import javax.swing.JRadioButton;
021: import javax.swing.SwingConstants;
022:
023: import org.columba.core.gui.base.SingleSideEtchedBorder;
024: import org.columba.core.gui.search.api.ICriteriaRenderer;
025: import org.columba.core.search.api.ISearchCriteria;
026: import org.columba.core.search.api.ISearchProvider;
027:
028: import com.jgoodies.forms.builder.DefaultFormBuilder;
029: import com.jgoodies.forms.factories.ButtonBarFactory;
030: import com.jgoodies.forms.layout.FormLayout;
031:
032: class SearchDialog extends JDialog {
033:
034: private JRadioButton rb1;
035:
036: private JRadioButton rb2;
037:
038: private CriteriaPanel criteriaPanel;
039:
040: private JButton searchButton;
041:
042: private JButton helpButton;
043:
044: private JButton clearButton;
045:
046: private JButton closeButton;
047:
048: private boolean success = false;
049:
050: public SearchDialog(JFrame parent, Iterator<ISearchProvider> it) {
051: super (parent, true);
052:
053: setUndecorated(true);
054:
055: // Border border = BorderFactory.createCompoundBorder(new
056: // ShadowBorder(),BorderFactory.createEmptyBorder(10, 10, 10,10));
057: // ((JPanel) getContentPane()).setBorder(border);
058: ((JPanel) getContentPane()).setBorder(BorderFactory
059: .createLineBorder(Color.lightGray));
060:
061: initCriteriaPanel(it);
062: initComponents();
063: layoutComponents();
064:
065: pack();
066:
067: initListeners();
068:
069: getRootPane().setDefaultButton(searchButton);
070: }
071:
072: private void initListeners() {
073: searchButton.addActionListener(new ActionListener() {
074: public void actionPerformed(ActionEvent e) {
075: success = true;
076: setVisible(false);
077: }
078: });
079:
080: clearButton.addActionListener(new ActionListener() {
081: public void actionPerformed(ActionEvent e) {
082: criteriaPanel.clear();
083: }
084: });
085:
086: closeButton.addActionListener(new ActionListener() {
087: public void actionPerformed(ActionEvent e) {
088: setVisible(false);
089: }
090: });
091: }
092:
093: @Override
094: public void setVisible(boolean b) {
095: super .setVisible(b);
096:
097: if (b)
098: success = true;
099: }
100:
101: private void layoutComponents() {
102:
103: setLayout(new BorderLayout());
104:
105: JPanel topPanel = new JPanel();
106: topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0,
107: 10));
108:
109: FormLayout layout = new FormLayout(
110: "left:default:grow, 12dlu, right:default, 3dlu, right:default",
111: "");
112:
113: // create a form builder
114: DefaultFormBuilder builder = new DefaultFormBuilder(layout,
115: topPanel);
116:
117: builder.append(new JLabel());
118: builder.append(rb1);
119: builder.append(rb2);
120:
121: add(topPanel, BorderLayout.NORTH);
122: add(criteriaPanel, BorderLayout.CENTER);
123:
124: JPanel bottomPanel = ButtonBarFactory.buildHelpBar(helpButton,
125: searchButton, clearButton, closeButton);
126:
127: bottomPanel.setBorder(BorderFactory.createCompoundBorder(
128: new SingleSideEtchedBorder(SwingConstants.TOP),
129: BorderFactory.createEmptyBorder(8, 8, 8, 8)));
130:
131: add(bottomPanel, BorderLayout.SOUTH);
132: }
133:
134: private void initCriteriaPanel(Iterator<ISearchProvider> it) {
135: criteriaPanel = new CriteriaPanel();
136: criteriaPanel.setBorder(BorderFactory.createEmptyBorder(0, 10,
137: 10, 10));
138:
139: boolean firstTime = true;
140: while (it.hasNext()) {
141: ISearchProvider p = it.next();
142: System.out.println("provider=" + p.getName());
143:
144: JLabel label = new JLabel(p.getName());
145: label.setIcon(p.getIcon());
146: label.setFont(label.getFont().deriveFont(Font.BOLD));
147:
148: if (!firstTime)
149: label.setBorder(BorderFactory.createEmptyBorder(10, 0,
150: 0, 0));
151: else
152: firstTime = false;
153:
154: criteriaPanel.addSeparator(label);
155: Iterator<ISearchCriteria> it2 = p.getAllCriteria("")
156: .listIterator();
157: while (it2.hasNext()) {
158: ISearchCriteria c = it2.next();
159: System.out.println("criteria=" + c.getName());
160:
161: ICriteriaRenderer r = p.getCriteriaRenderer(c
162: .getTechnicalName());
163: if (r != null)
164: criteriaPanel.addRenderer(r);
165: }
166:
167: }
168: }
169:
170: private void initComponents() {
171: rb1 = new JRadioButton("Match All Words");
172: rb1.setOpaque(false);
173: rb1.setSelected(true);
174: rb2 = new JRadioButton("Match Any");
175: rb2.setOpaque(false);
176: ButtonGroup group = new ButtonGroup();
177: group.add(rb1);
178: group.add(rb2);
179:
180: searchButton = new JButton("Search");
181: searchButton.setMnemonic('s');
182: searchButton.setDefaultCapable(true);
183:
184: helpButton = new JButton("Help");
185: helpButton.setMnemonic('h');
186:
187: clearButton = new JButton("Clear");
188: clearButton.setMnemonic('l');
189:
190: closeButton = new JButton("Close");
191: closeButton.setMnemonic('c');
192:
193: }
194:
195: public boolean isSuccess() {
196: return success;
197: }
198:
199: public boolean isMatchAll() {
200: return rb1.isSelected();
201: }
202:
203: public Iterator<ICriteriaRenderer> getAllCriteriaRenderer() {
204: return criteriaPanel.getAllRenderers();
205: }
206:
207: class CriteriaPanel extends JPanel {
208:
209: private List<ICriteriaRenderer> list = new Vector<ICriteriaRenderer>();
210:
211: private DefaultFormBuilder builder;
212:
213: CriteriaPanel() {
214:
215: FormLayout layout = new FormLayout(
216: "3dlu, right:default, 6dlu, left:default:grow, 12dlu, right:default, 6dlu, left:default:grow",
217: "");
218:
219: // create a form builder
220: builder = new DefaultFormBuilder(layout, this );
221: builder.setLeadingColumnOffset(1);
222: }
223:
224: public void addSeparator(JComponent c) {
225:
226: builder.nextLine();
227: builder.appendRow("default");
228: builder.setLeadingColumnOffset(0);
229: builder.append(c, 6);
230: builder.nextLine();
231: builder.setLeadingColumnOffset(1);
232:
233: }
234:
235: public void addRenderer(ICriteriaRenderer renderer) {
236: if (renderer == null)
237: throw new IllegalArgumentException("renderer == null");
238:
239: list.add(renderer);
240:
241: builder.append(renderer.getLabel());
242: builder.append(renderer.getEditor());
243: }
244:
245: public void removeRenderer(ICriteriaRenderer renderer) {
246: list.remove(renderer);
247: }
248:
249: public void removeRenderer(int index) {
250: list.remove(index);
251: }
252:
253: public Iterator<ICriteriaRenderer> getAllRenderers() {
254: return list.listIterator();
255: }
256:
257: public void clear() {
258: Iterator<ICriteriaRenderer> it = getAllRenderers();
259: while (it.hasNext()) {
260: ICriteriaRenderer r = it.next();
261: r.clear();
262: }
263: }
264: }
265:
266: }
|