ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / popup / NotificationPopup.java
blob21a2d196d76a2b8b9af77ebdea4a1603099e2b17
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.ui.popup;
19 import com.intellij.openapi.ui.popup.Balloon;
20 import com.intellij.openapi.ui.popup.JBPopup;
21 import com.intellij.openapi.ui.popup.JBPopupFactory;
22 import com.intellij.openapi.ui.popup.JBPopupListener;
23 import com.intellij.openapi.wm.impl.IdeFrameImpl;
24 import com.intellij.ui.components.panels.Wrapper;
25 import com.intellij.ui.components.panels.NonOpaquePanel;
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.ActionListener;
31 /**
32 * @author max
34 public class NotificationPopup {
36 private Impl myImpl;
38 public NotificationPopup(final JComponent owner, final JComponent content, Color backgroud) {
39 this(owner, content, backgroud, true);
42 public NotificationPopup(final JComponent owner, final JComponent content, Color backgroud, boolean useDefaultPreferredSize) {
43 this(owner, content, backgroud, useDefaultPreferredSize, null, false);
46 public NotificationPopup(final JComponent owner, final JComponent content, Color backgroud, final boolean useDefaultPreferredSize, ActionListener clickHandler, boolean closeOnClick) {
47 final IdeFrameImpl frame = findFrame(owner);
48 if (frame == null || !frame.isShowing()) {
49 final FramelessNotificationPopup popup = new FramelessNotificationPopup(owner, content, backgroud, useDefaultPreferredSize, clickHandler);
51 myImpl = new Impl() {
52 public void addListener(JBPopupListener listener) {
53 popup.getPopup().addListener(listener);
56 public void hide() {
57 popup.getPopup().cancel();
60 } else {
61 final Wrapper wrapper = new NonOpaquePanel(content) {
62 @Override
63 public Dimension getPreferredSize() {
64 final Dimension size = super.getPreferredSize();
65 if (useDefaultPreferredSize) {
66 if (size.width > 400 || size.height > 200) {
67 size.width = 400;
68 size.height = 200;
71 return size;
75 final Balloon balloon = JBPopupFactory.getInstance().createBalloonBuilder(wrapper)
76 .setFadeoutTime(5000)
77 .setHideOnClickOutside(false)
78 .setHideOnFrameResize(false)
79 .setHideOnKeyOutside(false)
80 .setCloseButtonEnabled(true)
81 .setFillColor(backgroud)
82 .setShowCallout(false)
83 .setClickHandler(clickHandler, closeOnClick)
84 .createBalloon();
86 frame.getBalloonLayout().add(balloon);
88 myImpl = new Impl() {
89 public void addListener(JBPopupListener listener) {
90 balloon.addListener(listener);
93 public void hide() {
94 balloon.hide();
100 private IdeFrameImpl findFrame(JComponent owner) {
101 final Window frame = SwingUtilities.getWindowAncestor(owner);
102 if (frame instanceof IdeFrameImpl) {
103 return (IdeFrameImpl)frame;
106 return null;
109 public JBPopup getPopup() {
110 return null;
114 interface Impl {
115 void addListener(JBPopupListener listener);
116 void hide();
119 public void addListener(JBPopupListener listener) {
122 public void hide() {