/**
* 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;
import java.awt.BorderLayout;
import java.awt.Frame;
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 javax.swing.JFrame;
import javax.swing.WindowConstants;
import de.chris_soft.nanoarchive.Archive;
import de.chris_soft.nanoarchive.DocumentFoundListener;
import de.chris_soft.nanoarchive.Metadata;
import de.chris_soft.utilities.LabelArchive;
import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.StringLists;
import de.chris_soft.utilities.swing.LabelList;
import de.chris_soft.utilities.swing.LabelListListener;
import de.chris_soft.utilities.swing.pdf.CurrentFileListener;
import de.chris_soft.utilities.swing.pdf.MultiPdfZapper;
/**
* Windows for presentating full text results.
* @author Christian Packenius.
*/
public class FulltextSearchWindows extends JFrame implements Runnable, DocumentFoundListener, LabelListListener,
CurrentFileListener {
private static final long serialVersionUID = -7479536773097235624L;
private final String fulltext;
private final Archive archive;
private final MultiPdfZapper multiPdfZapper;
private LabelList labelList;
private Map<File, String> file2docId = new HashMap<File, String>();
private List<String> labels = new ArrayList<String>();
private String docID;
/**
* Constructor.
* @param fulltext Text to search in archive.
* @param archive Archive to search in.
*/
public FulltextSearchWindows(String fulltext, Archive archive) {
super("Full text search over archive running... [\"" + fulltext + "\"]");
this.fulltext = fulltext;
this.archive = archive;
java.awt.Toolkit.getDefaultToolkit().setDynamicLayout(true);
multiPdfZapper = new MultiPdfZapper();
multiPdfZapper.addCurrentFileListener(this);
List<String> labels = LabelArchive.instance.getAllLabels();
labelList = new LabelList(this, labels);
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);
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.start();
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
archive.documentSearch(fulltext, this);
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#foundDocument(java.io.File, java.util.Properties)
*/
@Override
public void foundDocument(File document, Properties metadata) {
try {
multiPdfZapper.add(document);
String docID = metadata.getProperty(Metadata.DOCUMENT_ID);
file2docId.put(document, docID);
} catch (IOException exception) {
// Ignore.
}
}
/**
* @see de.chris_soft.nanoarchive.DocumentFoundListener#noElseDocumentFound()
*/
@Override
public void noElseDocumentFound() {
setTitle("Full text search [\"" + fulltext + "\"] finished!");
}
/**
* @see de.chris_soft.utilities.swing.LabelListListener#labelAdded(java.lang.String)
*/
@Override
public void labelAdded(String addedLabel) {
labels.add(addedLabel);
storeLabels();
}
/**
* @see de.chris_soft.utilities.swing.LabelListListener#labelRemoved(java.lang.String)
*/
@Override
public void labelRemoved(String removedLabel) {
labels.remove(removedLabel);
storeLabels();
}
private void storeLabels() {
String llist = StringLists.list2string(labels);
try {
archive.putMetadataViaDocID(docID, Metadata.LABELS, llist);
} catch (IOException e) {
LogUtils.log(e);
}
}
/**
* @see de.chris_soft.utilities.swing.pdf.CurrentFileListener#currentFileChanged(java.io.File)
*/
@Override
public void currentFileChanged(File currentFile) {
labelList.removeAllSelections();
docID = file2docId.get(currentFile);
Properties metadata = archive.getMetadataFromDocID(docID);
String labelsAsString = metadata.getProperty(Metadata.LABELS);
List<String> labels = StringLists.string2list(labelsAsString);
for (String label : labels) {
labelList.setLabel(label);
}
this.labels = labels;
}
}