/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa.gui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import de.chris_soft.nanoarchive.DocumentFoundListener;
import de.chris_soft.nanodoa.God;
import de.chris_soft.utilities.swing.labellist.LabelList;
import de.chris_soft.utilities.swing.labellist.LabelListListener;
import de.chris_soft.utilities.swing.labellist.LabeledContainer;
import de.chris_soft.utilities.swing.pdf.CurrentFileListener;
import de.chris_soft.utilities.swing.pdf.MultiPdfZapper;
/**
* Window for presentating full text results.
* @author Christian Packenius.
*/
public class FulltextSearchWindow extends JFrame implements Runnable, DocumentFoundListener, LabelListListener,
CurrentFileListener, WindowListener, LabeledContainer {
private static WeakHashMap<FulltextSearchWindow, Object> windowMap = new WeakHashMap<FulltextSearchWindow, Object>();
private static final long serialVersionUID = -7479536773097235624L;
private final AppWindow appWindow;
private final String searchItemsText;
private final MultiPdfZapper multiPdfZapper;
private LabelList labelList;
private Map<File, Long> file2docId = new HashMap<File, Long>();
// private List<String> labels = new ArrayList<String>();
private long documentID = 0;
/**
* Constructor.
* @param searchItemsText Text to search in archive.
* @param appWindow Application main window.
* @throws Exception
*/
public FulltextSearchWindow(String searchItemsText, AppWindow appWindow) throws Exception {
super("Full text search over archive running... [\"" + searchItemsText + "\"]");
this.appWindow = appWindow;
this.searchItemsText = searchItemsText;
java.awt.Toolkit.getDefaultToolkit().setDynamicLayout(true);
multiPdfZapper = new MultiPdfZapper();
multiPdfZapper.addCurrentFileListener(this);
labelList = new LabelList(God.centralLabelLister, God.archive, this);
getContentPane().add(BorderLayout.CENTER, multiPdfZapper.getComponent());
getContentPane().add(BorderLayout.EAST, labelList.getComponent());
setSize(800, 500);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setExtendedState(Frame.MAXIMIZED_BOTH);
setVisible(true);
addWindowListener(this);
Thread thread = new Thread(this, "Full text search thread");
thread.setDaemon(true);
thread.start();
addWindowToList(this);
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
God.archive.documentSearch(searchItemsText, this);
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#foundDocument(String, java.io.File, long, java.util.Properties)
*/
@Override
public void foundDocument(String searchTerm, File document, long documentID, Properties metadata) {
try {
multiPdfZapper.add(document);
file2docId.put(document, documentID);
God.specialsTree.foundDocument(searchTerm, document, documentID, metadata);
} catch (IOException exception) {
// Ignore.
}
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#noElseDocumentFound()
*/
@Override
public void noElseDocumentFound() {
setTitle("Full text search [\"" + searchItemsText + "\"] finished!");
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabelListListener#labelSet(long, java.lang.Object)
*/
@Override
public void labelSet(long labelID, Object docID) {
if ((Long) docID == documentID) {
labelList.refresh();
}
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabelListListener#labelUnset(long, java.lang.Object)
*/
@Override
public void labelUnset(long labelID, Object docID) {
if ((Long) docID == documentID) {
labelList.refresh();
}
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabelListListener#labelDeleted(long)
*/
@Override
public void labelDeleted(long labelID) {
labelList.refresh();
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabelListListener#labelAdded(long)
*/
@Override
public void labelAdded(long labelID) {
labelList.refresh();
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabelListListener#labelRenamed(long, java.lang.String)
*/
@Override
public void labelRenamed(long labelID, String newLabelName) {
labelList.refresh();
}
/**
* @see de.chris_soft.utilities.swing.pdf.CurrentFileListener#currentFileChanged(java.io.File)
*/
@Override
public void currentFileChanged(File currentFile) {
appWindow.showSearchedDocument(searchItemsText, file2docId.get(currentFile));
documentID = file2docId.get(currentFile);
labelList.refresh();
}
/**
* @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
*/
@Override
public void windowOpened(WindowEvent e) {
// Ignore.
}
/**
* @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
*/
@Override
public void windowClosing(WindowEvent e) {
// Ignore.
}
/**
* @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
*/
@Override
public void windowClosed(WindowEvent e) {
multiPdfZapper.valueChanged(null);
}
/**
* @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
*/
@Override
public void windowIconified(WindowEvent e) {
// Ignore.
}
/**
* @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
*/
@Override
public void windowDeiconified(WindowEvent e) {
// Ignore.
}
/**
* @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
*/
@Override
public void windowActivated(WindowEvent e) {
// Ignore.
}
/**
* @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
*/
@Override
public void windowDeactivated(WindowEvent e) {
// Ignore.
}
/**
* @param fulltextSearchWindow
*/
private static synchronized void addWindowToList(FulltextSearchWindow fulltextSearchWindow) {
windowMap.put(fulltextSearchWindow, null);
}
/**
* Returns all full text search windows.
* @return List of all fulltext search windows.
*/
public static synchronized List<FulltextSearchWindow> getAllWindows() {
ArrayList<FulltextSearchWindow> list = new ArrayList<FulltextSearchWindow>(windowMap.keySet());
for (FulltextSearchWindow win : list) {
if (!win.isVisible()) {
windowMap.remove(win);
}
}
return new ArrayList<FulltextSearchWindow>(windowMap.keySet());
}
/**
* @see de.chris_soft.utilities.swing.labellist.LabeledContainer#getObjectToBeLabeled()
*/
@Override
public Long getObjectToBeLabeled() {
return documentID == 0 ? null : documentID;
}
}