Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

migrate to ToolWindowFactory #34

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<applicationConfigurable groupId="tools" instance="idea.plugin.psiviewer.controller.application.Configuration"/>
<applicationService serviceImplementation="idea.plugin.psiviewer.controller.application.PsiViewerApplicationSettings"/>
<errorHandler implementation="com.sylvanaar.idea.errorreporting.YouTrackBugReporter"/>
<toolWindow id="PsiViewer" icon="/images/psiToolWindow.png" anchor="right" factoryClass="idea.plugin.psiviewer.controller.project.PsiViewerToolWindowFactory"/>
</extensions>

<!-- Component's actions -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static void enableToolWindows(boolean enableToolWindows) {
Project[] projects = ProjectManager.getInstance().getOpenProjects();
for (Project project : projects) {
PsiViewerProjectComponent pc = PsiViewerProjectComponent.getInstance(project);
if (enableToolWindows) pc.initToolWindow();
if (enableToolWindows) pc.registerToolWindow();
else
pc.unregisterToolWindow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.components.panels.HorizontalLayout;
import idea.plugin.psiviewer.PsiViewerConstants;
import idea.plugin.psiviewer.controller.actions.PropertyToggleAction;
import idea.plugin.psiviewer.controller.application.PsiViewerApplicationSettings;
import idea.plugin.psiviewer.util.Helpers;
import idea.plugin.psiviewer.view.PsiViewerPanel;
import org.jdesktop.swingx.combobox.ListComboBoxModel;
Expand Down Expand Up @@ -90,34 +88,19 @@ public PsiViewerProjectComponent(Project project)
_project = project;
}

public void projectOpened()
{
if (PsiViewerApplicationSettings.getInstance().PLUGIN_ENABLED)
{
initToolWindow();
}
}

public void projectClosed()
{
unregisterToolWindow();
}

public void initComponent()
{
}

public void disposeComponent()
{
}

@NotNull
public String getComponentName()
{
return PLUGIN_NAME + '.' + PROJECT_COMPONENT_NAME;
}

public void initToolWindow()
public void registerToolWindow() {
ToolWindow toolWindow = getToolWindow();
initToolWindow(toolWindow);
toolWindow.setAvailable(true, null);
}

PsiViewerPanel initToolWindow(@NotNull ToolWindow toolWindow)
{
_viewerPanel = new PsiViewerPanel(this);

Expand Down Expand Up @@ -166,12 +149,11 @@ public void propertyChange(PropertyChangeEvent evt)
updateLanguagesList(Collections.<Language>emptyList());

_viewerPanel.add(panel, BorderLayout.NORTH);

ToolWindow toolWindow = getToolWindow();
toolWindow.setIcon(Helpers.getIcon(ICON_TOOL_WINDOW));
_viewerPanel.setToolWindow(toolWindow);

_editorListener = new EditorListener(_viewerPanel, _project);

return _viewerPanel;
}

private void handleCurrentState()
Expand Down Expand Up @@ -204,24 +186,12 @@ public void unregisterToolWindow()
_editorListener.stop();
_editorListener = null;
}
if (isToolWindowRegistered())
ToolWindowManager.getInstance(_project).unregisterToolWindow(ID_TOOL_WINDOW);
getToolWindow().setAvailable(false, null);
}

private ToolWindow getToolWindow()
{
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(_project);
if (isToolWindowRegistered())
return toolWindowManager.getToolWindow(ID_TOOL_WINDOW);
else
return toolWindowManager.registerToolWindow(ID_TOOL_WINDOW,
_viewerPanel,
ToolWindowAnchor.RIGHT);
}

private boolean isToolWindowRegistered()
{
return ToolWindowManager.getInstance(_project).getToolWindow(ID_TOOL_WINDOW) != null;
return ToolWindowManager.getInstance(_project).getToolWindow(ID_TOOL_WINDOW);
}

public void readExternal(Element element) throws InvalidDataException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package idea.plugin.psiviewer.controller.project;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import idea.plugin.psiviewer.controller.application.PsiViewerApplicationSettings;
import idea.plugin.psiviewer.view.PsiViewerPanel;
import org.jetbrains.annotations.NotNull;

public class PsiViewerToolWindowFactory implements ToolWindowFactory {

@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
PsiViewerProjectComponent component = PsiViewerProjectComponent.getInstance(project);
ContentManager contentManager = toolWindow.getContentManager();
PsiViewerPanel panel = component.initToolWindow(toolWindow);
Content content = contentManager.getFactory().createContent(panel, null, false);
contentManager.addContent(content);
}

@Override
public boolean shouldBeAvailable(@NotNull Project project) {
return PsiViewerApplicationSettings.getInstance().PLUGIN_ENABLED;
}
}