Package jetbrains.communicator.idea

Source Code of jetbrains.communicator.idea.BaseToolWindow

/*
* Copyright 2000-2006 JetBrains s.r.o.
*
* 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 jetbrains.communicator.idea;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.components.ProjectComponent;
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 icons.IdetalkCoreIcons;
import jetbrains.communicator.core.Pico;
import jetbrains.communicator.core.dispatcher.LocalMessageDispatcher;
import jetbrains.communicator.util.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
* @author Kir
*/
public abstract class BaseToolWindow implements ProjectComponent {
  protected final ToolWindowManager myToolWindowManager;
  protected final ActionManager myActionManager;
  protected final Project myProject;

  protected JComponent myPanel;
  protected ToolWindow myToolWindow;
  protected Window myWindow;

  protected BaseToolWindow(ToolWindowManager toolWindowManager, ActionManager actionManager, Project project) {
    myProject = project;
    myToolWindowManager = toolWindowManager;
    myActionManager = actionManager;

    myPanel = new JPanel(new BorderLayout()) {
      public void addNotify() {
        super.addNotify();
        myWindow = SwingUtilities.getWindowAncestor(this);
      }
    };
  }

  protected abstract String getToolWindowId();
  protected abstract void createToolWindowComponent();
  protected abstract ToolWindowAnchor getAnchor();


  public void projectOpened() {
    myPanel.setOpaque(true);
    myPanel.setBackground(com.intellij.util.ui.UIUtil.getListBackground());

    createToolWindowComponent();
    myToolWindow = myToolWindowManager.registerToolWindow(getToolWindowId(), myPanel, getAnchor(), myProject, true);
    myToolWindow.setIcon(IdetalkCoreIcons.IdeTalk.User_toolwindow);
  }

  public void projectClosed() {
  }

  public void disposeComponent() {
  }

  public void expandToolWindow() {
    final Semaphore semaphore = new Semaphore(1);
    semaphore.tryAcquire();
    UIUtil.invokeLater(new Runnable() {
      public void run() {
        ToolWindow window = myToolWindowManager.getToolWindow(getToolWindowId());
        if (window != null) {
          window.show(new Runnable() {
            public void run() {
              semaphore.release();
            }
          });
        }
        else {
          semaphore.release();
        }
      }
    });
    waitForOpen(semaphore);
  }

  private static void waitForOpen(Semaphore semaphore) {
    if (!EventQueue.isDispatchThread()) {
      try {
        semaphore.tryAcquire(1, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        //
      }
    }
  }

  public static LocalMessageDispatcher getLocalDispatcher() {
    return (LocalMessageDispatcher) Pico.getInstance().getComponentInstanceOfType(LocalMessageDispatcher.class);
  }
}
TOP

Related Classes of jetbrains.communicator.idea.BaseToolWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.