| /* |
| * Copyright (C) 2016 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.google.devrel.cluestick.studioclient; |
| |
| import com.appspot.cluestick_server.search.model.Result; |
| import com.intellij.openapi.actionSystem.AnAction; |
| import com.intellij.openapi.components.ServiceManager; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.openapi.wm.ToolWindow; |
| import com.intellij.openapi.wm.ToolWindowAnchor; |
| import com.intellij.openapi.wm.ToolWindowManager; |
| import com.intellij.ui.content.Content; |
| import com.intellij.ui.content.ContentFactory; |
| import com.intellij.ui.content.ContentManager; |
| |
| import java.util.List; |
| |
| /** |
| * Wrapper of {@link ToolWindow}, which contains a instance of {@link ToolWindow} and it can be |
| * registered dynamically. |
| * To show a ToolWindow from {@link AnAction#actionPerformed}, call like following: |
| * <pre> |
| * @Override |
| * public void actionPerformed(AnActionEvent event) { |
| * Project project = getEventProject(event); |
| * DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(project); |
| * ToolWindow toolWindow = toolWindowWrapper.getToolWindow(); |
| * toolWindow.show((Runnable) null); |
| * ..... |
| * } |
| * </pre> |
| */ |
| public class DynamicToolWindowWrapper { |
| private static final String TOOL_WINDOW_TAG = "Find Sample Code"; |
| private Project project; |
| private ToolWindow toolWindow; |
| |
| public DynamicToolWindowWrapper(Project project) { |
| this.project = project; |
| } |
| |
| public static DynamicToolWindowWrapper getInstance(Project project) { |
| return ServiceManager.getService(project, DynamicToolWindowWrapper.class); |
| } |
| |
| public ToolWindow getToolWindow(Symbol symbol, List<Result> results) { |
| if (toolWindow == null) { |
| toolWindow = ToolWindowManager.getInstance(project).registerToolWindow(TOOL_WINDOW_TAG, true, |
| ToolWindowAnchor.BOTTOM); |
| toolWindow.setIcon(IconFetcher.GoogleDevelopers); |
| } |
| toolWindow.show(null); |
| |
| SearchResultsView view = new SearchResultsView(project, symbol, results); |
| view.setClose(new Runnable() { |
| @Override |
| public void run() { |
| toolWindow.hide(null); |
| } |
| }); |
| |
| // nb. Title is intentionally blank, this allows only a single Find Samples window at once |
| // (title dedups). |
| String title = ""; |
| Content content = ContentFactory.SERVICE.getInstance().createContent(view, title, false); |
| content.setDisposer(view); |
| content.setShouldDisposeContent(false); // prevent multiple dispose |
| |
| ContentManager contentManager = toolWindow.getContentManager(); |
| contentManager.removeAllContents(true); |
| contentManager.addContent(content); |
| contentManager.setSelectedContent(content); |
| return toolWindow; |
| } |
| } |