Debian Codegraph @

uk/ac/babraham/FastQC/Help/HelpSearchPanel.java

raw symbols SCIP
1 /**
2 * Copyright 2009-15 Simon Andrews
3 *
4 * This file is part of SeqMonk.
5 *
6 * SeqMonk is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SeqMonk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with SeqMonk; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Help;
21
22 import java.awt.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.io.IOException;
26
27 import javax.swing.BorderFactory;
28 import javax.swing.DefaultListModel;
29 import javax.swing.JButton;
30 import javax.swing.JList;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTextField;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.event.ListSelectionEvent;
36 import javax.swing.event.ListSelectionListener;
37
38 /**
39 * The Class HelpSearchPanel.
40 */
41 public class HelpSearchPanel extends JPanel implements ActionListener, ListSelectionListener, Runnable {
42
43 /** The root. */
44 private HelpIndexRoot root;
45
46 /** The query field. */
47 private JTextField queryField;
48
49 /** The result list. */
50 private JList resultList;
51
52 /** The list model. */
53 private DefaultListModel listModel;
54
55 /** The search button. */
56 private JButton searchButton;
57
58 /** The dialog. */
59 private HelpDialog dialog;
60
61 /** The results scroll pane. */
62 private JScrollPane resultsScrollPane;
63
64 /**
65 * Instantiates a new help search panel.
66 *
67 * @param root the root
68 * @param dialog the dialog
69 */
70 public HelpSearchPanel (HelpIndexRoot root,HelpDialog dialog) {
71 this.root = root;
72 this.dialog = dialog;
73
74 setLayout(new BorderLayout());
75
76 JPanel queryPanel = new JPanel();
77 queryPanel.setLayout(new BorderLayout());
78 queryPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
79 queryField = new JTextField();
80 queryField.setActionCommand("search");
81 queryField.addActionListener(this);
82 queryPanel.add(queryField,BorderLayout.CENTER);
83 searchButton = new JButton("Search");
84 searchButton.setActionCommand("search");
85 searchButton.addActionListener(this);
86 queryPanel.add(searchButton,BorderLayout.EAST);
87 add(queryPanel,BorderLayout.NORTH);
88
89 listModel = new DefaultListModel();
90 listModel.addElement("[No search results]");
91 resultList = new JList(listModel);
92 resultList.addListSelectionListener(this);
93 resultList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
94 resultsScrollPane = new JScrollPane(resultList);
95 add(resultsScrollPane,BorderLayout.CENTER);
96
97 }
98
99 /* (non-Javadoc)
100 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
101 */
102 public void actionPerformed(ActionEvent e) {
103 Thread t = new Thread(this);
104 t.start();
105 }
106
107 /* (non-Javadoc)
108 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
109 */
110 public void valueChanged(ListSelectionEvent lse) {
111 Object o = resultList.getSelectedValue();
112 if (o != null && o instanceof HelpPage) {
113 dialog.DisplayPage((HelpPage)o);
114 }
115 }
116
117 /* (non-Javadoc)
118 * @see java.lang.Runnable#run()
119 */
120 public void run() {
121 searchButton.setEnabled(false);
122 listModel.removeAllElements();
123 if (queryField.getText().trim().length() > 0) {
124 HelpPage[] results;
125 try {
126 results = root.findPagesForTerm(queryField.getText().trim());
127 }
128 catch (IOException e) {
129 e.printStackTrace();
130 searchButton.setEnabled(true);
131 return;
132 }
133 if (results.length > 0) {
134 for (int r=0;r<results.length;r++) {
135 listModel.addElement(results[r]);
136 }
137 }
138 else {
139 listModel.addElement("[No search results]");
140 }
141 }
142
143 // This stupid rigmarole is because on OSX the updated list
144 // just won't show up for some reason. Removing the list and
145 // re-adding it forces it to always show up.
146 //
147 // It's not even enough to remake the scroll pane. You have
148 // to replace the entire JList. Aaargh!
149 remove(resultsScrollPane);
150 revalidate();
151 resultList = new JList(listModel);
152 resultList.addListSelectionListener(this);
153 resultsScrollPane = new JScrollPane(resultList);
154 add(resultsScrollPane,BorderLayout.CENTER);
155 revalidate();
156 repaint();
157
158 searchButton.setEnabled(true);
159 }
160 }