1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright(c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  * pmHelpSearchPanel.java
29  * Search help keywords
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import java.util.*;
37 import javax.swing.JPanel;
38 import javax.swing.border.*;
39 import javax.swing.event.*;
40 import javax.swing.*;
41 
42 import com.sun.admin.pm.server.*;
43 
44 
45 public class pmHelpSearchPanel extends JPanel {
46 
47     pmHelpController controller;
48     pmHelpSearchQueryPanel queryPanel;
49     pmHelpSearchResultPanel resultPanel;
50     JLabel textPanels[];
51 
pmHelpSearchPanel(pmHelpController ctrl)52     public pmHelpSearchPanel(pmHelpController ctrl) {
53         controller = ctrl;
54 
55         // build subpanels
56         queryPanel = new pmHelpSearchQueryPanel(this);
57         resultPanel = new pmHelpSearchResultPanel(this);
58 
59         textPanels = new JLabel[4];
60         textPanels[0] = new JLabel(
61             pmUtility.getResource("To.find.help.articles..."));
62         textPanels[1] = new JLabel(
63             pmUtility.getResource("enter.keywords.below..."));
64 
65         // lay out top panel
66         this.setLayout(new GridBagLayout());
67         GridBagConstraints c = new GridBagConstraints();
68         c.insets = new Insets(5, 5, 5, 5);
69         c.gridwidth = GridBagConstraints.REMAINDER;
70         c.gridx = 0;
71         c.gridy = 0;
72         c.gridheight = 1; // GridBagConstraints.REMAINDER;
73         c.fill = GridBagConstraints.BOTH;
74         c.weightx = 1.0;
75         c.weighty = 0.0;
76 
77         JPanel p = new JPanel();
78         p.setLayout(new GridBagLayout());
79         GridBagConstraints pc = new GridBagConstraints();
80         pc.insets = new Insets(5, 5, 0, 5);
81         // pc.fill = GridBagConstraints.HORIZONTAL;
82         pc.weightx = 1.0;
83         pc.anchor = GridBagConstraints.WEST;
84         pc.gridx = 0;
85         pc.gridy = GridBagConstraints.RELATIVE;
86 
87         p.add(textPanels[0], pc);
88         pc.insets = new Insets(0, 5, 5, 5);
89         p.add(textPanels[1], pc);
90         // p.add(textPanels[2]);
91 
92         this.add(p, c);
93 
94 
95         p = new JPanel();
96         p.setLayout(new BorderLayout());
97         queryPanel.setBorder(BorderFactory.createEtchedBorder());
98         p.add(queryPanel, "North");
99         resultPanel.setBorder(BorderFactory.createEtchedBorder());
100         p.add(resultPanel, "Center");
101         // p.setBorder(BorderFactory.createEtchedBorder());
102 
103         c.gridy = 1;
104         // new stuff
105         c.gridy = GridBagConstraints.RELATIVE;
106         c.gridheight = 0;
107         c.weighty = 1.0;
108         c.weightx = 0.0;
109         c.fill = GridBagConstraints.BOTH;
110         c.anchor = GridBagConstraints.EAST;
111         // end new stuff
112 
113         this.add(p, c);
114         this.setBorder(BorderFactory.createEtchedBorder());
115 
116 
117         // figure out when we are tabbed or un-tabbed
118         controller.outerPanel.addChangeListener(new ChangeListener() {
119             public void stateChanged(ChangeEvent e) {
120                 JTabbedPane tp = (JTabbedPane) e.getSource();
121                 Debug.info("HELP:  Tab event!");
122                 if (!(tp.getSelectedComponent() instanceof
123                        com.sun.admin.pm.client.pmHelpSearchPanel)) {
124                     Debug.info("HELP:  Tab event: resetting default");
125 		    /*
126 		     * controller.frame.getRootPane().
127 		     *	setDefaultButton(
128 		     * 		controller.frame.dismiss);
129 		     */
130 		    /*
131 		     * System.out.println(controller);
132 		     * System.out.println(controller.frame);
133 		     * System.out.println(controller.frame.dismiss);
134 		     */
135 
136                     if (controller.frame.dismiss != null)
137                         controller.frame.dismiss.
138                             setAsDefaultButton();
139                 } else {
140                     // better to have the tab itself keep focus.
141                     // queryPanel.query.requestFocus();
142                 }
143             }
144         });
145 
146     }
147 
148 
149     // place item titles in search result panel
setSearchResults(Vector items)150     public void setSearchResults(Vector items) {
151         Vector v = new Vector();
152 
153         if (items.size() == 0) {
154             resultPanel.setListEmpty(true);
155             v.addElement(pmUtility.getResource("Nothing.matched."));
156         } else {
157             Enumeration e = items.elements();
158             while (e.hasMoreElements()) {
159                 pmHelpItem i = (pmHelpItem) e.nextElement();
160                 v.addElement(i);
161             }
162             resultPanel.setListEmpty(false);
163         }
164         resultPanel.setResultList(v);
165     }
166 }
167 
168 
169 
170 class pmHelpSearchResultPanel extends JPanel {
171 
172     JList resultList = null;
173     pmButton selectButton = null;
174     pmHelpSearchPanel parentPanel = null;
175     protected boolean listEmpty = true;
176 
177 
pmHelpSearchResultPanel(pmHelpSearchPanel par)178     public pmHelpSearchResultPanel(pmHelpSearchPanel par) {
179 
180         parentPanel = par;
181 
182         this.setLayout(new GridBagLayout());
183 
184         GridBagConstraints c = new GridBagConstraints();
185         c.insets = new Insets(10, 10, 10, 10);
186         c.fill = GridBagConstraints.NONE;
187         c.weightx = c.weighty = 0.0;
188         c.gridx = 0;
189         c.gridy = 0;
190         c.anchor = GridBagConstraints.NORTHWEST;
191 
192         JLabel promptLabel = new JLabel(
193             pmUtility.getResource("Search.Results:"));
194 /*
195  * MNEMONIC
196  *        promptLabel.setDisplayedMnemonic(
197  *            pmUtility.getIntResource("Search.Results:.mnemonic"));
198  */
199 
200         this.add(promptLabel, c);
201 
202         selectButton = new pmButton(
203             pmUtility.getResource("Show"));
204         selectButton.setMnemonic(
205             pmUtility.getIntResource("Show.mnemonic"));
206 
207         selectButton.setEnabled(false);
208 
209         selectButton.addActionListener(new ActionListener() {
210             // load the selected item into view panel
211             public void actionPerformed(ActionEvent e) {
212                 pmHelpItem selectedItem = (pmHelpItem)
213                     resultList.getSelectedValue();
214                 Debug.info("HELP:  Selected " + selectedItem);
215                 parentPanel.controller.showHelpItem(selectedItem);
216 
217             }
218         });
219 
220         c.gridy = 1;
221         c.anchor = GridBagConstraints.SOUTHWEST;
222         this.add(selectButton, c);
223 
224 
225         Vector resultItems = new Vector();
226 
227         resultList = new JList(resultItems);
228         JScrollPane scrollPane = new JScrollPane();
229         scrollPane.getViewport().setView(resultList);
230         resultList.setVisibleRowCount(8);
231 
232         promptLabel.setLabelFor(resultList);
233 
234         resultList.addListSelectionListener(new ListSelectionListener() {
235             public void valueChanged(ListSelectionEvent e) {
236                 if (!listEmpty) {
237                     selectButton.setEnabled(true);
238 		    /*
239 		     * parentPanel.controller.frame.
240 		     * getRootPane().setDefaultButton(selectButton);
241 		     */
242                     selectButton.setAsDefaultButton();
243 
244                 }
245             }});
246 
247 
248         resultList.addMouseListener(new MouseAdapter() {
249             public void mouseClicked(MouseEvent e) {
250                 if (e.getClickCount() == 2) {
251                     JList l = (JList) e.getSource();
252                     int i = l.locationToIndex(e.getPoint());
253                     Debug.info("HELP:  doubleclick index: " + i);
254                     if (!listEmpty && i >= 0) {
255                         pmHelpItem item = (pmHelpItem) l.getModel().
256                             getElementAt(i);
257                         Debug.info("HELP:  doubleclick: " + item.tag);
258                         parentPanel.controller.showHelpItem(item);
259                     }
260                 }
261             }
262         });
263 
264 
265         c.gridwidth = 2;
266         c.gridx = 1;
267         c.gridy = 0;
268         c.weightx = c.weighty = 1.0;
269         c.fill = GridBagConstraints.BOTH;
270         c.anchor = GridBagConstraints.WEST;
271 
272         this.add(scrollPane, c);
273 
274     }
275 
setResultList(Vector v)276     public void setResultList(Vector v) {
277         resultList.setListData(v);
278         resultList.setSelectedValue(v.elementAt(0), true);
279 
280     }
281 
setListEmpty(boolean e)282     void setListEmpty(boolean e) {
283         listEmpty = e;
284         selectButton.setEnabled(false);
285 	/*
286 	 * parentPanel.controller.frame.getRootPane().
287 	 * setDefaultButton(parentPanel.controller.frame.dismiss);
288 	 */
289         parentPanel.controller.frame.dismiss.setAsDefaultButton();
290 
291     }
292 
293 
294 }
295 
296 
297 class pmHelpSearchQueryPanel extends JPanel {
298 
299     JTextField query;
300     pmButton search;
301     pmHelpSearchPanel parentPanel = null;
302 
pmHelpSearchQueryPanel(pmHelpSearchPanel par)303     public pmHelpSearchQueryPanel(pmHelpSearchPanel par) {
304 
305         parentPanel = par;
306 
307         this.setLayout(new GridBagLayout());
308 
309         GridBagConstraints c = new GridBagConstraints();
310         c.insets = new Insets(10, 10, 10, 10);
311         c.fill = GridBagConstraints.NONE;
312         c.weightx = c.weighty = 0.0;
313         c.anchor = GridBagConstraints.WEST;
314 
315         c.gridx = 0;
316         c.gridy = 0;
317         c.gridwidth = 1;
318         c.gridheight = 1;
319 
320         JLabel promptLabel =
321             new JLabel(pmUtility.getResource("Keywords:"));
322 /*
323  * MNEMONIC
324  *     promptLabel.setDisplayedMnemonic(
325  *         pmUtility.getIntResource("Keywords:.mnemonic"));
326  */
327 
328         this.add(promptLabel, c);
329 
330         search = new pmButton(
331             pmUtility.getResource("Find"));
332         search.setMnemonic(
333             pmUtility.getIntResource("Find.mnemonic"));
334 
335         search.addActionListener(new ActionListener() {
336             public void actionPerformed(ActionEvent e) {
337                 // parse the keyword strings
338                 Vector v = null;
339                 StringTokenizer st = null;
340                 String s = query.getText();
341                 if (s != null) {
342                     v = new Vector();
343                     st = new StringTokenizer(s);
344                     while (st.hasMoreTokens())
345                         v.addElement(st.nextToken());
346                     v = getItemsForKeywords(v);
347                     parentPanel.setSearchResults(v);
348 
349                     if (v != null && v.size() != 0) {
350                         Debug.info("HELP:  search vector full");
351                         parentPanel.resultPanel.resultList.requestFocus();
352                     } else {
353                         Debug.info("HELP:  search vector empty");
354                     }
355                 }
356 
357             }
358         });
359 
360         c.fill = GridBagConstraints.NONE;
361         c.gridx = 2;
362 	c.gridy = 0;	// GridBagConstraints.RELATIVE;
363         this.add(search, c);
364 
365         query = new JTextField();
366         query.setEditable(true);
367         query.setText(" ");
368 
369         promptLabel.setLabelFor(query);
370 
371         query.addActionListener(new ActionListener() {
372             public void actionPerformed(ActionEvent e) {
373                 Debug.info("HELP:  Action!");
374                 pmHelpSearchQueryPanel.this.search.doClick();
375             }
376         });
377 
378         query.getDocument().addDocumentListener(new DocumentListener() {
379             public void changedUpdate(DocumentEvent e) {
380                 // ignore
381             }
382 
383             public void insertUpdate(DocumentEvent e) {
384                 // make search the default button
385                 Debug.info("HELP:  search doc inserted update");
386                 pmHelpSearchQueryPanel.this.search.setEnabled(true);
387 		/*
388 		 * parentPanel.controller.frame.
389 		 * getRootPane().setDefaultButton(
390 		 * pmHelpSearchQueryPanel.this.search);
391 		 */
392                 if (pmHelpSearchQueryPanel.this.search != null)
393                     pmHelpSearchQueryPanel.this.search.
394                         setAsDefaultButton();
395             }
396 
397             public void removeUpdate(DocumentEvent e) {
398                 Debug.info("HELP:  search doc removed update");
399                 // restore the default button
400                 if (query.getText().length() == 0) {
401                     /*
402                      * parentPanel.controller.frame.
403                      * getRootPane().setDefaultButton(
404                      * parentPanel.controller.frame.dismiss);
405                      */
406                     if (parentPanel.controller.frame.dismiss != null)
407                         parentPanel.controller.frame.dismiss.
408                             setAsDefaultButton();
409                 }
410             }
411         });
412 
413 
414 
415 	c.gridwidth = 1;	// GridBagConstraints.REMAINDER;
416         c.gridx = 1;
417         c.weightx = 1.0;
418         c.fill = GridBagConstraints.HORIZONTAL;
419         c.anchor = GridBagConstraints.EAST;
420 
421         this.add(query, c);
422 
423     }
424 
425 
getItemsForKeywords(Vector keywords)426     Vector getItemsForKeywords(Vector keywords) {
427         Vector result = new Vector();
428 
429         Debug.info("HELP:  getItemsForKeywords: " + keywords);
430 
431         Enumeration words = keywords.elements();
432         while (words.hasMoreElements()) {
433             String s = (String) words.nextElement();
434             Vector newItems = pmHelpRepository.helpItemsForKeyword(s);
435             Debug.info("HELP:  getItemsForKeywords new items: " + newItems);
436 
437             if (newItems != null) {
438                 Enumeration items = newItems.elements();
439                 while (items.hasMoreElements()) {
440                     pmHelpItem i = (pmHelpItem) items.nextElement();
441                     Debug.info("HELP:  getItemsForKeywords result: " + result);
442                     Debug.info("HELP:  getItemsForKeywords item: " + i);
443 
444                     if (!result.contains(i))
445                         result.addElement(i);
446                 }
447             }
448         }
449         return result;
450     }
451 }
452