001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.kernel.searchprovider;
020
021import icy.gui.plugin.PluginRichToolTip;
022import icy.network.NetworkUtil;
023import icy.plugin.PluginDescriptor;
024import icy.search.SearchResult;
025import icy.search.SearchResultProducer;
026import icy.util.StringUtil;
027
028import java.awt.Image;
029import java.util.List;
030
031import org.pushingpixels.flamingo.api.common.RichTooltip;
032
033import plugins.kernel.searchprovider.PluginSearchResultProducerHelper.SearchWord;
034
035public abstract class PluginSearchResult extends SearchResult
036{
037    protected final PluginDescriptor plugin;
038    protected final int priority;
039    protected String description;
040
041    public PluginSearchResult(SearchResultProducer provider, PluginDescriptor plugin, String text,
042            List<SearchWord> words, int priority)
043    {
044        super(provider);
045
046        this.plugin = plugin;
047        this.priority = priority;
048
049        description = "";
050        int wi = 0;
051        while (StringUtil.isEmpty(description) && (wi < words.size()))
052        {
053            final SearchWord sw = words.get(wi);
054
055            if (!sw.reject)
056                // no more than 80 characters...
057                description = StringUtil.trunc(text, sw.word, 80);
058
059            wi++;
060        }
061
062        if (!StringUtil.isEmpty(description))
063        {
064            // remove carriage return
065            description = description.replace("\n", "");
066
067            for (SearchWord sw : words)
068                // highlight search keywords (only for more than 2 characters search)
069                if (!sw.reject && (sw.length() > 2))
070                    description = StringUtil.htmlBoldSubstring(description, sw.word, true);
071        }
072    }
073
074    public PluginDescriptor getPlugin()
075    {
076        return plugin;
077    }
078
079    @Override
080    public String getTitle()
081    {
082        return plugin.getName();
083    }
084
085    @Override
086    public Image getImage()
087    {
088        if (plugin.isIconLoaded())
089            return plugin.getIconAsImage();
090
091        return PluginDescriptor.DEFAULT_ICON.getImage();
092    }
093
094    @Override
095    public String getDescription()
096    {
097        return description;
098    }
099
100    @Override
101    public void executeAlternate()
102    {
103        final String url = plugin.getWeb();
104
105        if (!StringUtil.isEmpty(url))
106            NetworkUtil.openBrowser(url);
107    }
108
109    @Override
110    public RichTooltip getRichToolTip()
111    {
112        return new PluginRichToolTip(plugin);
113    }
114
115    @Override
116    public int compareTo(SearchResult o)
117    {
118        if (o instanceof PluginSearchResult)
119            return ((PluginSearchResult) o).priority - priority;
120
121        return super.compareTo(o);
122    }
123}