KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Browser > SearchDialog


1 /*
2  * SearchDialog.java
3  *
4  * Created on 13. květen 2004, 15:47
5  */

6
7 package SOFA.SOFAnet.Browser;
8
9 import SOFA.SOFAnet.Admin.RMI.*;
10 import SOFA.SOFAnet.Search.*;
11 import SOFA.SOFAnet.Repository.BundleInfo;
12 import javax.swing.*;
13 import java.rmi.RemoteException JavaDoc;
14 import java.util.*;
15 import java.awt.Component JavaDoc;
16 import java.awt.Frame JavaDoc;
17
18 /**
19  *
20  * @author Ladislav Sobr
21  */

22 public class SearchDialog extends javax.swing.JDialog JavaDoc
23 {
24   private DefaultListModel content;
25   private RMIAdminInterface admin;
26   
27   public static void showDialog(Component JavaDoc frameComp, RMIAdminInterface admin)
28   {
29     Frame JavaDoc frame = JOptionPane.getFrameForComponent(frameComp);
30     JDialog dialog = new SearchDialog(frame, true, admin);
31     dialog.setLocationRelativeTo(frameComp);
32     dialog.setVisible(true);
33   }
34   
35   /** Creates new form SearchDialog */
36   public SearchDialog(java.awt.Frame JavaDoc parent, boolean modal, RMIAdminInterface admin)
37   {
38     super(parent, modal);
39     this.admin = admin;
40     content = new DefaultListModel();
41     initComponents();
42   }
43   
44   /** This method is called from within the constructor to
45    * initialize the form.
46    * WARNING: Do NOT modify this code. The content of this method is
47    * always regenerated by the Form Editor.
48    */

49   private void initComponents()//GEN-BEGIN:initComponents
50
{
51     componentModeButtonGroup = new javax.swing.ButtonGroup JavaDoc();
52     jPanel1 = new javax.swing.JPanel JavaDoc();
53     jPanel7 = new javax.swing.JPanel JavaDoc();
54     jPanel6 = new javax.swing.JPanel JavaDoc();
55     strLabel = new javax.swing.JLabel JavaDoc();
56     strTextField = new javax.swing.JTextField JavaDoc();
57     patternCheckBox = new javax.swing.JCheckBox JavaDoc();
58     componentCheckBox = new javax.swing.JCheckBox JavaDoc();
59     componentModePanel = new javax.swing.JPanel JavaDoc();
60     ccTryRadioButton = new javax.swing.JRadioButton JavaDoc();
61     ccNoRadioButton = new javax.swing.JRadioButton JavaDoc();
62     ccOnlyRadioButton = new javax.swing.JRadioButton JavaDoc();
63     jPanel9 = new javax.swing.JPanel JavaDoc();
64     settingsPanel = new javax.swing.JPanel JavaDoc();
65     timoutLabel = new javax.swing.JLabel JavaDoc();
66     timeoutTextField = new javax.swing.JTextField JavaDoc();
67     shareGroupLabel = new javax.swing.JLabel JavaDoc();
68     shareGroupTextField = new javax.swing.JTextField JavaDoc();
69     firstOnlyCheckBox = new javax.swing.JCheckBox JavaDoc();
70     jPanel8 = new javax.swing.JPanel JavaDoc();
71     searchButton = new javax.swing.JButton JavaDoc();
72     jPanel2 = new javax.swing.JPanel JavaDoc();
73     jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
74     repliesList = new javax.swing.JList JavaDoc();
75     jPanel4 = new javax.swing.JPanel JavaDoc();
76     acquireButton = new javax.swing.JButton JavaDoc();
77     jPanel3 = new javax.swing.JPanel JavaDoc();
78     closeButton = new javax.swing.JButton JavaDoc();
79
80     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
81     setTitle("Search");
82     jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
83
84     jPanel7.setLayout(new java.awt.BorderLayout JavaDoc());
85
86     jPanel7.setBorder(new javax.swing.border.TitledBorder JavaDoc("Search Request"));
87     jPanel6.setLayout(new java.awt.GridLayout JavaDoc(4, 0));
88
89     strLabel.setText("Search String:");
90     jPanel6.add(strLabel);
91
92     jPanel6.add(strTextField);
93
94     patternCheckBox.setText("Search String is Pattern");
95     jPanel6.add(patternCheckBox);
96
97     componentCheckBox.setText("Search String is Component");
98     componentCheckBox.addActionListener(new java.awt.event.ActionListener JavaDoc()
99     {
100       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
101       {
102         componentCheckBoxActionPerformed(evt);
103       }
104     });
105
106     jPanel6.add(componentCheckBox);
107
108     jPanel7.add(jPanel6, java.awt.BorderLayout.NORTH);
109
110     componentModePanel.setLayout(new java.awt.GridLayout JavaDoc(3, 0));
111
112     componentModePanel.setBorder(new javax.swing.border.TitledBorder JavaDoc("Component Mode"));
113     componentModePanel.setEnabled(false);
114     ccTryRadioButton.setSelected(true);
115     ccTryRadioButton.setText("Try Complete Component (including subcomponents)");
116     componentModeButtonGroup.add(ccTryRadioButton);
117     ccTryRadioButton.setEnabled(false);
118     componentModePanel.add(ccTryRadioButton);
119
120     ccNoRadioButton.setText("Single Component (without subcomponents)");
121     componentModeButtonGroup.add(ccNoRadioButton);
122     ccNoRadioButton.setEnabled(false);
123     componentModePanel.add(ccNoRadioButton);
124
125     ccOnlyRadioButton.setText("Complete Component (with subcomponents)");
126     componentModeButtonGroup.add(ccOnlyRadioButton);
127     ccOnlyRadioButton.setEnabled(false);
128     componentModePanel.add(ccOnlyRadioButton);
129
130     jPanel7.add(componentModePanel, java.awt.BorderLayout.CENTER);
131
132     jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);
133
134     jPanel9.setLayout(new java.awt.BorderLayout JavaDoc());
135
136     settingsPanel.setLayout(new java.awt.GridLayout JavaDoc(5, 0));
137
138     settingsPanel.setBorder(new javax.swing.border.TitledBorder JavaDoc("Search Settings"));
139     timoutLabel.setText("Timeout (in milliseconds):");
140     settingsPanel.add(timoutLabel);
141
142     timeoutTextField.setText("3000");
143     settingsPanel.add(timeoutTextField);
144
145     shareGroupLabel.setText("Share Group:");
146     settingsPanel.add(shareGroupLabel);
147
148     settingsPanel.add(shareGroupTextField);
149
150     firstOnlyCheckBox.setSelected(true);
151     firstOnlyCheckBox.setText("First Reply Only");
152     settingsPanel.add(firstOnlyCheckBox);
153
154     jPanel9.add(settingsPanel, java.awt.BorderLayout.NORTH);
155
156     searchButton.setText("Search");
157     searchButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
158     {
159       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
160       {
161         searchButtonActionPerformed(evt);
162       }
163     });
164
165     jPanel8.add(searchButton);
166
167     jPanel9.add(jPanel8, java.awt.BorderLayout.SOUTH);
168
169     jPanel1.add(jPanel9, java.awt.BorderLayout.EAST);
170
171     getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
172
173     jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
174
175     jPanel2.setBorder(new javax.swing.border.TitledBorder JavaDoc("Replies"));
176     repliesList.setModel(content);
177     repliesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
178     repliesList.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
179     {
180       public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt)
181       {
182         repliesListMouseClicked(evt);
183       }
184     });
185
186     jScrollPane1.setViewportView(repliesList);
187
188     jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);
189
190     acquireButton.setText("Acquire Selected Shared Bundle");
191     acquireButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
192     {
193       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
194       {
195         acquireButtonActionPerformed(evt);
196       }
197     });
198
199     jPanel4.add(acquireButton);
200
201     jPanel2.add(jPanel4, java.awt.BorderLayout.SOUTH);
202
203     getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
204
205     closeButton.setText("Close");
206     closeButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
207     {
208       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
209       {
210         closeButtonActionPerformed(evt);
211       }
212     });
213
214     jPanel3.add(closeButton);
215
216     getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
217
218     pack();
219   }//GEN-END:initComponents
220

221   private void repliesListMouseClicked(java.awt.event.MouseEvent JavaDoc evt)//GEN-FIRST:event_repliesListMouseClicked
222
{//GEN-HEADEREND:event_repliesListMouseClicked
223
if (evt.getClickCount() == 2)
224     {
225       acquireButton.doClick(); //emulate button click
226
}
227   }//GEN-LAST:event_repliesListMouseClicked
228

229   private void closeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_closeButtonActionPerformed
230
{//GEN-HEADEREND:event_closeButtonActionPerformed
231
dispose();
232   }//GEN-LAST:event_closeButtonActionPerformed
233

234   private void acquireButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_acquireButtonActionPerformed
235
{//GEN-HEADEREND:event_acquireButtonActionPerformed
236
SearchReplyItem reply = (SearchReplyItem)repliesList.getSelectedValue();
237     if (reply != null)
238     {
239       AcquireSharedBundleDialog.showDialog(this, admin, reply.getBundleName(), reply.getNodeName());
240     }
241   }//GEN-LAST:event_acquireButtonActionPerformed
242

243   private void searchButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_searchButtonActionPerformed
244
{//GEN-HEADEREND:event_searchButtonActionPerformed
245
String JavaDoc strText = strTextField.getText().trim();
246     boolean pattern = patternCheckBox.isSelected();
247     boolean component = componentCheckBox.isSelected();
248     int completeComponent = SearchPattern.CC_TRY;
249     if (ccNoRadioButton.isSelected()) completeComponent = SearchPattern.CC_NO;
250     if (ccOnlyRadioButton.isSelected()) completeComponent = SearchPattern.CC_ONLY;
251
252     if (strText.length() == 0)
253     {
254       JOptionPane.showMessageDialog(this, "Empty Search String", "Error", JOptionPane.ERROR_MESSAGE);
255       return;
256     }
257     
258     if (!pattern)
259     {
260       BundleInfo bundleInfo = new BundleInfo();
261       try
262       {
263         bundleInfo.fromBundleName(strText);
264       }
265       catch (BundleInfo.InvalidBundleNameException e)
266       {
267         if (component) JOptionPane.showMessageDialog(this, "Invalid name of component:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
268         else JOptionPane.showMessageDialog(this, "Invalid name of bundle:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
269         return;
270       }
271
272       if (component && !bundleInfo.isComponent())
273       {
274         JOptionPane.showMessageDialog(this, "Invalid name of component", "Error", JOptionPane.ERROR_MESSAGE);
275         return;
276       }
277     }
278     
279     int timeout = 3000;
280     try
281     {
282       timeout = Integer.parseInt(timeoutTextField.getText());
283       if (timeout <= 0) throw new NumberFormatException JavaDoc();
284     }
285     catch (NumberFormatException JavaDoc e)
286     {
287       JOptionPane.showMessageDialog(this, "Invalid Timeout", "Error", JOptionPane.ERROR_MESSAGE);
288       return;
289     }
290
291     String JavaDoc shareGroup = shareGroupTextField.getText();
292     boolean firstOnly = firstOnlyCheckBox.isSelected();
293     
294     SearchPattern searchPattern = new SearchPattern();
295     searchPattern.setStr(strText);
296     searchPattern.setPattern(pattern);
297     searchPattern.setComponent(component);
298     searchPattern.setCompleteComponent(completeComponent);
299     
300     SearchReplyItem[] replies = null;
301     try
302     {
303       replies = admin.search(searchPattern, firstOnly, shareGroup, timeout);
304     }
305     catch (RemoteException JavaDoc e)
306     {
307       JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
308       dispose();
309       return;
310     }
311     catch (RMIAdminException e)
312     {
313       JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
314       dispose();
315       return;
316     }
317
318     content.clear();
319     if (replies != null)
320     {
321       content.ensureCapacity(replies.length);
322       for (int i = 0; i < replies.length; i++) content.addElement(replies[i]);
323     }
324   }//GEN-LAST:event_searchButtonActionPerformed
325

326   private void componentCheckBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_componentCheckBoxActionPerformed
327
{//GEN-HEADEREND:event_componentCheckBoxActionPerformed
328
boolean component = componentCheckBox.isSelected();
329     componentModePanel.setEnabled(component);
330     ccTryRadioButton.setEnabled(component);
331     ccNoRadioButton.setEnabled(component);
332     ccOnlyRadioButton.setEnabled(component);
333   }//GEN-LAST:event_componentCheckBoxActionPerformed
334

335   // Variables declaration - do not modify//GEN-BEGIN:variables
336
private javax.swing.JButton JavaDoc acquireButton;
337   private javax.swing.JRadioButton JavaDoc ccNoRadioButton;
338   private javax.swing.JRadioButton JavaDoc ccOnlyRadioButton;
339   private javax.swing.JRadioButton JavaDoc ccTryRadioButton;
340   private javax.swing.JButton JavaDoc closeButton;
341   private javax.swing.JCheckBox JavaDoc componentCheckBox;
342   private javax.swing.ButtonGroup JavaDoc componentModeButtonGroup;
343   private javax.swing.JPanel JavaDoc componentModePanel;
344   private javax.swing.JCheckBox JavaDoc firstOnlyCheckBox;
345   private javax.swing.JPanel JavaDoc jPanel1;
346   private javax.swing.JPanel JavaDoc jPanel2;
347   private javax.swing.JPanel JavaDoc jPanel3;
348   private javax.swing.JPanel JavaDoc jPanel4;
349   private javax.swing.JPanel JavaDoc jPanel6;
350   private javax.swing.JPanel JavaDoc jPanel7;
351   private javax.swing.JPanel JavaDoc jPanel8;
352   private javax.swing.JPanel JavaDoc jPanel9;
353   private javax.swing.JScrollPane JavaDoc jScrollPane1;
354   private javax.swing.JCheckBox JavaDoc patternCheckBox;
355   private javax.swing.JList JavaDoc repliesList;
356   private javax.swing.JButton JavaDoc searchButton;
357   private javax.swing.JPanel JavaDoc settingsPanel;
358   private javax.swing.JLabel JavaDoc shareGroupLabel;
359   private javax.swing.JTextField JavaDoc shareGroupTextField;
360   private javax.swing.JLabel JavaDoc strLabel;
361   private javax.swing.JTextField JavaDoc strTextField;
362   private javax.swing.JTextField JavaDoc timeoutTextField;
363   private javax.swing.JLabel JavaDoc timoutLabel;
364   // End of variables declaration//GEN-END:variables
365

366 }
367
Popular Tags