All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.datalite.zk.javahelp.ZkHelpSearcher Maven / Gradle / Ivy

The newest version!
package cz.datalite.zk.javahelp;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

import javax.help.SearchHit;
import javax.help.SearchTOCItem;
import javax.help.SearchView;
import javax.help.search.MergingSearchEngine;
import javax.help.search.SearchEvent;
import javax.help.search.SearchItem;
import javax.help.search.SearchListener;
import javax.help.search.SearchQuery;

import org.zkoss.zul.Listitem;

/**
 * @author Nelson Cazonda
 *
 */
@SuppressWarnings("unchecked")
public class ZkHelpSearcher implements SearchListener {

	private Enumeration srchResultEnum;
	private Vector nodes;

	private MergingSearchEngine helpsearch;
	private SearchQuery searchquery;
	private boolean searchFinished;

	public ZkHelpSearcher(SearchView view) {
		helpsearch = new MergingSearchEngine(view);
		searchquery = helpsearch.createQuery();
		searchquery.addSearchListener(this);
	}

	public synchronized void itemsFound(SearchEvent e) {
		SearchTOCItem tocitem;
		Enumeration itemEnum = e.getSearchItems();
		// Iterate through each search item in the searchEvent
		while (itemEnum.hasMoreElements()) {
			SearchItem item = (SearchItem) itemEnum.nextElement();
			URL url;
			try {
				url = new URL(item.getBase(), item.getFilename());
			} catch (MalformedURLException me) {
				continue;
			}
			boolean foundNode = false;

			// see if this search item matches that of one we currently have
			// if so just do an update
			Enumeration nodesEnum = nodes.elements();
			while (nodesEnum.hasMoreElements()) {
				tocitem = (SearchTOCItem) nodesEnum.nextElement();
				URL testURL = tocitem.getURL();
				if (testURL != null && url != null && url.sameFile(testURL)) {
					tocitem.addSearchHit(new SearchHit(item.getConfidence(),
							item.getBegin(), item.getEnd()));
					foundNode = true;
					break;
				}
			}

			// No match.
			// OK then add a new one.
			if (!foundNode) {
				tocitem = new SearchTOCItem(item);
				nodes.addElement(tocitem);
			}
		}
	}

	public synchronized void searchStarted(SearchEvent arg0) {
		nodes = new Vector();
		searchFinished = false;
	}

	public synchronized void searchFinished(SearchEvent arg0) {
		searchFinished = true;
		srchResultEnum = nodes.elements();
		notifyAll();
	}

	public synchronized List search(String query) {
		List searchResults = new ArrayList();

		if (searchquery.isActive()) {
			searchquery.stop();
		}
		searchquery.start(query, Locale.getDefault());

		if (!searchFinished) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}

		while (srchResultEnum.hasMoreElements()) {
			SearchTOCItem item = (SearchTOCItem) srchResultEnum.nextElement();
			Listitem auxListItem = new Listitem(item.getName(), item);
			auxListItem.setImage(toImage(item.getConfidence()));
			searchResults.add(auxListItem);
		}
		return searchResults;
	}

	private String toImage(double confdnce) {
		if (confdnce < 1) {
			return Help.IMAGES_FOLDER + "high.gif";
		} else if (confdnce < 10) {
			return Help.IMAGES_FOLDER + "medhigh.gif";
		} else if (confdnce < 25) {
			return Help.IMAGES_FOLDER + "med.gif";
		} else if (confdnce < 50) {
			return Help.IMAGES_FOLDER + "medlow.gif";
		} else {
			return Help.IMAGES_FOLDER + "low.gif";
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy