public DetachedListener() {
parentFrame = (toolWindowManager.getAnchestor() instanceof Frame) ? (Frame) toolWindowManager.getAnchestor() : null;
}
public void propertyChange(PropertyChangeEvent evt) {
Content content = (Content) evt.getSource();
boolean oldValue = (Boolean) evt.getOldValue();
boolean newValue = (Boolean) evt.getNewValue();
if (!oldValue && newValue) {
final JDialog dialog = new JDialog(parentFrame, false);
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Window parentWindow = SwingUtilities.windowForComponent(tabbedContentManager);
Component component = content.getComponent();
int tabIndex = tabbedContentManager.indexOfComponent(component);
if (tabIndex != -1) {
tabbedContentManager.removeTabAt(tabIndex);
} else {
if (tabbedContentManager.getParent() == null)
toolWindowManager.setMainContent(null);
else
throw new IllegalStateException("Invalid Content : " + content);
}
component.setPreferredSize(component.getSize());
dialog.setTitle(content.getTitle());
dialog.getContentPane().add(component);
Point location = parentWindow.getLocation();
location.x += 5;
location.y += 5;
dialog.setLocation(location);
dialog.pack();
if (TransparencyManager.getInstance().isServiceAvailable()) {
TransparencyListener transparencyListener = new TransparencyListener(dialog);
dialog.addWindowListener(transparencyListener);
dialog.addWindowFocusListener(transparencyListener);
}
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
Component component = dialog.getContentPane().getComponent(0);
ContentUI content = (ContentUI) contentManager.getContent(component);
content.fireSelected(false);
content.setDetached(false);
}
});
dialog.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
ContentUI content = (ContentUI) contentManager.getContent(dialog.getContentPane().getComponent(0));
content.fireSelected(true);
}
public void windowLostFocus(WindowEvent e) {
if (dialog.getContentPane().getComponentCount() > 0) {
ContentUI content = (ContentUI) contentManager.getContent(dialog.getContentPane().getComponent(0));
content.fireSelected(false);
}
}
});
if (parentFrame == null) {
WindowFocusListener windowFocusListener = new WindowFocusListener() {
long start;
long end;
public void windowGainedFocus(WindowEvent e) {
start = System.currentTimeMillis();
}
public void windowLostFocus(WindowEvent e) {
end = System.currentTimeMillis();
long elapsed = end - start;
//System.out.println(elapsed);
if (elapsed < 100)
dialog.toFront();
dialog.removeWindowFocusListener(this);
}
};
dialog.addWindowFocusListener(windowFocusListener);
}
dialog.toFront();
dialog.setVisible(true);
SwingUtil.requestFocus(dialog);
} else if (oldValue && !newValue) {
Window window = SwingUtilities.windowForComponent(content.getComponent());
window.setVisible(false);
window.dispose();
addUIForContent(content);
tabbedContentManager.setSelectedIndex(tabbedContentManager.getTabCount() - 1);