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.PluginDetailPanel;
022import icy.plugin.PluginDescriptor;
023import icy.plugin.PluginLauncher;
024import icy.plugin.PluginLoader;
025import icy.search.SearchResult;
026import icy.search.SearchResultConsumer;
027import icy.search.SearchResultProducer;
028
029import java.util.ArrayList;
030import java.util.List;
031
032import plugins.kernel.searchprovider.PluginSearchResultProducerHelper.SearchWord;
033
034/**
035 * This class is used to provide installed plugin elements to the search engine.
036 * 
037 * @author Stephane
038 */
039public class LocalPluginSearchResultProducer extends SearchResultProducer
040{
041    /**
042     * @author Stephane
043     */
044    public static class LocalPluginResult extends PluginSearchResult
045    {
046        public LocalPluginResult(SearchResultProducer provider, PluginDescriptor plugin, String text,
047                List<SearchWord> words, int priority)
048        {
049            super(provider, plugin, text, words, priority);
050        }
051
052        @Override
053        public String getTooltip()
054        {
055            if (plugin.isActionable())
056                return "Left click: Run   -   Right click: Online documentation";
057
058            return "Left click: Show detail   -   Right click: Online documentation";
059        }
060
061        @Override
062        public void execute()
063        {
064            if (plugin.isActionable())
065                PluginLauncher.start(plugin);
066            else
067                new PluginDetailPanel(plugin);
068        }
069    }
070
071    @Override
072    public int getOrder()
073    {
074        // should be close after kernel
075        return 5;
076    }
077
078    @Override
079    public String getName()
080    {
081        return "Installed plugins";
082    }
083
084    @Override
085    public String getTooltipText()
086    {
087        return "Result(s) from installed plugins";
088    }
089
090    @Override
091    public void doSearch(String text, SearchResultConsumer consumer)
092    {
093        final List<SearchWord> words = PluginSearchResultProducerHelper.getSearchWords(text);
094        final List<SearchResult> tmpResults = new ArrayList<SearchResult>();
095
096        for (PluginDescriptor plugin : PluginLoader.getPlugins())
097        {
098            if (hasWaitingSearch())
099                return;
100
101            final int prio = PluginSearchResultProducerHelper.searchInPlugin(plugin, words);
102
103            if (prio > 0)
104                tmpResults.add(new LocalPluginResult(this, plugin, plugin.getDescription(), words, prio));
105        }
106
107        // use a copy to avoid future concurrent accesses
108        results = new ArrayList<SearchResult>(tmpResults);
109        consumer.resultsChanged(this);
110
111        // load descriptions
112        for (SearchResult result : tmpResults)
113        {
114            // abort
115            if (hasWaitingSearch())
116                return;
117
118            ((LocalPluginResult) result).getPlugin().loadDescriptor();
119            consumer.resultChanged(this, result);
120        }
121
122        // load images
123        for (SearchResult result : tmpResults)
124        {
125            // abort
126            if (hasWaitingSearch())
127                return;
128
129            ((LocalPluginResult) result).getPlugin().loadImages();
130            consumer.resultChanged(this, result);
131        }
132    }
133}