|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU General Public License |
| 4 | + * as published by the Free Software Foundation; either version 2 |
| 5 | + * of the License, or (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program; if not, write to the Free Software |
| 14 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.mediawiki; |
| 18 | + |
| 19 | +import com.intellij.ide.BrowserUtil; |
| 20 | +import com.intellij.openapi.actionSystem.AnAction; |
| 21 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 22 | +import com.intellij.openapi.actionSystem.CommonDataKeys; |
| 23 | +import com.intellij.openapi.editor.Editor; |
| 24 | +import com.intellij.openapi.util.text.StringUtil; |
| 25 | +import org.jetbrains.annotations.NonNls; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | + |
| 29 | +import java.io.UnsupportedEncodingException; |
| 30 | +import java.net.URLEncoder; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Reedy |
| 34 | + */ |
| 35 | +public class MediaWikiSearchDocsAction extends AnAction { |
| 36 | + public void update(AnActionEvent event) { |
| 37 | + boolean enabled = getSelectedText(event) != null; |
| 38 | + event.getPresentation().setEnabled(enabled); |
| 39 | + event.getPresentation().setVisible(enabled); |
| 40 | + } |
| 41 | + |
| 42 | + @Nullable |
| 43 | + private static String getSelectedText(AnActionEvent e) { |
| 44 | + Editor editor = e.getData(CommonDataKeys.EDITOR); |
| 45 | + if (editor == null) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + String selectedText = editor.getSelectionModel().getSelectedText(); |
| 49 | + return StringUtil.nullize(selectedText, true); |
| 50 | + } |
| 51 | + |
| 52 | + public void actionPerformed(AnActionEvent event) { |
| 53 | + String selectedText = getSelectedText(event); |
| 54 | + if (selectedText == null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + BrowserUtil.browse(getSearchUrl(selectedText)); |
| 58 | + } |
| 59 | + |
| 60 | + @NonNls |
| 61 | + private static String getSearchUrl(@NotNull String textToSearch) { |
| 62 | + try { |
| 63 | + return "https://www.mediawiki.org/w/index.php?title=Special%3ASearch&profile=default&fulltext=Search&search=" + URLEncoder.encode(textToSearch, "UTF-8"); |
| 64 | + } catch (UnsupportedEncodingException e) { |
| 65 | + } |
| 66 | + return ""; |
| 67 | + } |
| 68 | +} |
0 commit comments