changedUpdate exception
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / HeavyweightHint.java
blob958d136d041d5d554a780cbcff3e568eddcc4d37
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.
16 package com.intellij.ui;
18 import com.intellij.openapi.diagnostic.Logger;
19 import org.jetbrains.annotations.NotNull;
21 import javax.swing.*;
22 import javax.swing.event.EventListenerList;
23 import java.awt.*;
24 import java.util.EventListener;
25 import java.util.EventObject;
27 public class HeavyweightHint implements Hint {
28 private static final Logger LOG = Logger.getInstance("#com.intellij.ui.LightweightHint");
30 private final JComponent myComponent;
31 private final boolean myFocusableWindowState;
32 private final EventListenerList myListenerList;
34 private JWindow myWindow;
36 public HeavyweightHint(final JComponent component) {
37 this(component, true);
40 public HeavyweightHint(@NotNull final JComponent component, final boolean focusableWindowState) {
41 myComponent = component;
42 myFocusableWindowState = focusableWindowState;
43 myListenerList = new EventListenerList();
46 /**
47 * Shows the hint as the window
49 public void show(@NotNull JComponent parentComponent, int x, int y, JComponent focusBackComponent) {
50 Dimension preferredSize = myComponent.getPreferredSize();
52 LOG.assertTrue(parentComponent.isShowing());
54 Window windowAncestor = SwingUtilities.getWindowAncestor(parentComponent);
55 LOG.assertTrue(windowAncestor != null);
57 myWindow = new JWindow(windowAncestor);
58 myWindow.setFocusableWindowState(myFocusableWindowState);
60 Point locationOnScreen = parentComponent.getLocationOnScreen();
62 myWindow.getContentPane().setLayout(new BorderLayout());
63 myWindow.getContentPane().add(myComponent, BorderLayout.CENTER);
64 myWindow.setBounds(locationOnScreen.x + x, locationOnScreen.y + y, preferredSize.width, preferredSize.height);
65 myWindow.pack();
66 myWindow.setVisible(true);
69 protected void fireHintHidden() {
70 final EventListener[] listeners = myListenerList.getListeners(HintListener.class);
71 for (EventListener listener1 : listeners) {
72 HintListener listener = (HintListener) listener1;
73 listener.hintHidden(new EventObject(this));
77 public Dimension getPreferredSize(){
78 return myComponent.getPreferredSize();
81 public boolean isVisible() {
82 return myComponent.isShowing();
85 public JComponent getComponent() {
86 return myComponent;
89 /**
90 * Hides and disposes hint window
92 public void hide() {
93 if(myWindow != null){
94 myWindow.dispose();
95 myWindow = null;
97 fireHintHidden();
100 public void addHintListener(HintListener listener) {
101 myListenerList.add(HintListener.class, listener);
104 public void removeHintListener(HintListener listener) {
105 myListenerList.remove(HintListener.class, listener);