001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.session.Session;
028: import jacareto.cleverphl.session.SessionList;
029: import jacareto.cleverphl.session.SessionLoader;
030: import jacareto.cleverphl.session.SessionUtilities;
031: import jacareto.struct.Structure;
032: import jacareto.system.Language;
033:
034: import org.apache.regexp.RE;
035: import org.apache.regexp.RESyntaxException;
036:
037: import java.awt.BorderLayout;
038: import java.awt.FlowLayout;
039: import java.awt.Point;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042:
043: import java.util.Hashtable;
044: import java.util.Iterator;
045: import java.util.Vector;
046:
047: import javax.swing.JButton;
048: import javax.swing.JDialog;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JScrollPane;
052: import javax.swing.JTable;
053: import javax.swing.border.EmptyBorder;
054: import javax.swing.table.DefaultTableModel;
055:
056: /**
057: * Dialog which shows search results.
058: *
059: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
060: * @version 1.0
061: */
062: public class SearchResultDialog extends JDialog implements
063: ActionListener, Runnable {
064: /** The CleverPHL instance. */
065: private CleverPHL cleverPHL;
066:
067: /** The JTable showing the search results. */
068: private JTable table;
069:
070: /** The label showing the process status. */
071: private JLabel processStatusLabel;
072:
073: /** The model of the table. */
074: private DefaultTableModel tableModel;
075:
076: /** The search string. */
077: private String searchString;
078: private boolean isRegExp;
079:
080: /** The sessions. */
081: private Hashtable openSessions;
082: private int numberSessions;
083: private String[] sessionNames;
084:
085: /** The buttons. */
086: private JButton openButton;
087:
088: /** The buttons. */
089: private JButton cancelButton;
090:
091: /** The buttons. */
092: private JButton abortButton;
093: private boolean openButtonPressed;
094: private JPanel buttonPanel;
095: private JPanel abortButtonPanel;
096:
097: /** Thread variables. */
098: private boolean isRunning;
099:
100: /** Result values. */
101: private String[] sessionNamesToOpen;
102:
103: /**
104: * Creates a new dialog (visible).
105: *
106: * @param cleverPHL the CleverPHL instance
107: * @param searchString the searchString
108: * @param isRegExp whether or not the string is a regular expression
109: */
110: public SearchResultDialog(CleverPHL cleverPHL, String searchString,
111: boolean isRegExp) {
112: super (cleverPHL.getMainFrame(), true);
113: this .cleverPHL = cleverPHL;
114: this .searchString = searchString;
115: this .isRegExp = isRegExp;
116: setName(cleverPHL.getCustomization().getString(
117: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
118:
119: sessionNamesToOpen = new String[0];
120:
121: Language language = cleverPHL.getLanguage();
122:
123: //setLocationRelativeTo ( cleverPHL.getMainFrame() );
124: setTitle(language
125: .getString("CleverPHL.SearchResultDialog.Title"));
126:
127: processStatusLabel = new JLabel(
128: language
129: .getString("CleverPHL.SearchResultDialog.Message.InitializingSearch")
130: + "...");
131: processStatusLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
132:
133: openButton = new JButton(language
134: .getString("CleverPHL.SearchResultDialog.OpenSessions"));
135: cancelButton = new JButton(language.getString("General.Cancel"));
136: abortButton = new JButton(language.getString("General.Abort"));
137: openButton.addActionListener(this );
138: cancelButton.addActionListener(this );
139: abortButton.addActionListener(this );
140:
141: buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
142: buttonPanel.add(openButton);
143: buttonPanel.add(cancelButton);
144:
145: abortButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
146: abortButtonPanel.add(abortButton);
147:
148: // The table
149: String[] colNames = new String[3];
150: colNames[0] = language
151: .getString("CleverPHL.SearchResultDialog.Session");
152: colNames[1] = language
153: .getString("CleverPHL.SearchResultDialog.Hits");
154: colNames[2] = language
155: .getString("CleverPHL.SearchResultDialog.Status");
156: tableModel = new DefaultTableModel(colNames, 0);
157: table = new JTable(tableModel) {
158: public boolean isCellEditable(int row, int column) {
159: return false;
160: }
161: };
162: table.setRowSelectionAllowed(true);
163: table.setColumnSelectionAllowed(false);
164: sessionNames = SessionUtilities.getAllSessionNames(cleverPHL);
165: numberSessions = sessionNames.length;
166:
167: String[] openSessionNames = SessionUtilities
168: .getOpenSessionNames(cleverPHL);
169: String openString = language
170: .getString("CleverPHL.SearchResultDialog.Open");
171: openSessions = new Hashtable();
172:
173: for (int i = 0; i < openSessionNames.length; i++) {
174: openSessions.put(openSessionNames[i], "");
175: }
176:
177: for (int i = 0; i < sessionNames.length; i++) {
178: String[] tableDataRow = new String[3];
179: tableDataRow[0] = sessionNames[i];
180: tableDataRow[1] = "?";
181: tableDataRow[2] = openSessions.containsKey(sessionNames[i]) ? openString
182: : "";
183: tableModel.addRow(tableDataRow);
184: }
185:
186: JScrollPane scrollPane = new JScrollPane(table,
187: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
188: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
189:
190: getContentPane().setLayout(new BorderLayout());
191: getContentPane().add(processStatusLabel, BorderLayout.NORTH);
192: getContentPane().add(scrollPane, BorderLayout.CENTER);
193: getContentPane().add(abortButtonPanel, BorderLayout.SOUTH);
194:
195: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
196: setLocation(((int) ownerLoc.getX()) + 50, ((int) ownerLoc
197: .getY()) + 50);
198:
199: setSize(400, 300);
200:
201: Thread t = new Thread(this );
202: t.start();
203: setVisible(true);
204: }
205:
206: /**
207: * Starts the search in all sessions.
208: */
209: public void run() {
210: isRunning = true;
211:
212: SessionList sessionList = cleverPHL.getSessionList();
213: SessionLoader sessionLoader = cleverPHL.getSessionLoader();
214:
215: for (int i = 0; i < sessionNames.length; i++) {
216: if (!isRunning) {
217: break;
218: }
219:
220: String sessionName = sessionNames[i];
221: boolean isOpenSession = openSessions
222: .containsKey(sessionName);
223: Structure structure = null;
224:
225: // get the structure
226: Session session = null;
227:
228: if (isOpenSession) {
229: isOpenSession = true;
230: session = sessionList.get(sessionName);
231: } else {
232: try {
233: session = sessionLoader.getSession(sessionName);
234: } catch (Exception ignored) {
235: ;
236: }
237: }
238:
239: if (session != null) {
240: structure = session.getStructure();
241: }
242:
243: // search the structure
244: int numberOfHits = 0;
245:
246: try {
247: if (structure != null) {
248: if (isRegExp) {
249: RE re = new RE(searchString);
250: numberOfHits = structure.search(re, null);
251: } else {
252: numberOfHits = structure.search(null,
253: searchString);
254: }
255: }
256: } catch (RESyntaxException res) {
257: cleverPHL
258: .getLogger()
259: .error(
260: cleverPHL
261: .getLanguage()
262: .getString(
263: "CleverPHL.FindDialog.SyntaxCheck.NotCorrect"),
264: res);
265: }
266:
267: // print the results
268: if (session != null) {
269: tableModel.setValueAt("" + numberOfHits, i, 1);
270:
271: if (isOpenSession) {
272: session.getStructureTree().repaint();
273: }
274: }
275:
276: setProcessLabelIsSearching(i + 1);
277: }
278:
279: searchFinished();
280: }
281:
282: /**
283: * The search has finished.
284: */
285: private void searchFinished() {
286: getContentPane().remove(abortButtonPanel);
287: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
288: validate();
289: repaint();
290: }
291:
292: /**
293: * Updates the process label during the search.
294: *
295: * @param numberSearched number of sessions which have already been searched
296: */
297: private void setProcessLabelIsSearching(int numberSearched) {
298: processStatusLabel.setText(cleverPHL.getLanguage().getString(
299: "CleverPHL.SearchResultDialog.Message.SearchingFor")
300: + ": "
301: + searchString
302: + " ("
303: + numberSearched
304: + "/"
305: + numberSessions + ")");
306: }
307:
308: /**
309: * Called when a button has been pressed.
310: *
311: * @param event DOCUMENT ME!
312: */
313: public void actionPerformed(ActionEvent event) {
314: if (event.getSource() == abortButton) {
315: isRunning = false;
316: searchFinished();
317: } else if (event.getSource() == openButton) {
318: openButtonPressed = true;
319:
320: // get selected sessions which should be openend afterwards
321: int[] rows = table.getSelectedRows();
322: Vector selectedSessions = new Vector();
323:
324: for (int i = 0; i < rows.length; i++) {
325: String sessionName = (String) tableModel.getValueAt(
326: rows[i], 0);
327:
328: if (!openSessions.containsKey(sessionName)) {
329: selectedSessions.add(sessionName);
330: }
331: }
332:
333: sessionNamesToOpen = new String[selectedSessions.size()];
334:
335: Iterator it = selectedSessions.iterator();
336: int i = 0;
337:
338: while (it.hasNext()) {
339: sessionNamesToOpen[i++] = (String) it.next();
340: }
341:
342: dispose();
343: } else {
344: openButtonPressed = false;
345: dispose();
346: }
347: }
348:
349: /**
350: * Returns whether or not the open button has been pressed.
351: *
352: * @return guess!
353: */
354: public boolean isOpenButtonPressed() {
355: return openButtonPressed;
356: }
357:
358: /**
359: * Returns the sessions which should be opened. Returns the names of the sessions which should
360: * be opened.
361: *
362: * @return array with names of sessions which should be opened
363: */
364: public String[] getSessionNamesToOpen() {
365: return sessionNamesToOpen;
366: }
367: }
|