/** * Show the given message in a dialog box or independent window, depending on whether the source * component is contained in a Frame or not. * * @param c The Controller that calls this method, or null if it is not called by a Controller. * (The Controller, if any, will be notified when the error message is cleared.) * @param message The message to display. */ public void setErrorMessage(Controller c, String message) { if (popup != null) clearErrorMessage(); if (message == null) return; errorSource = c; errorMessage = message; Component parent = source; while (parent != null && !(parent instanceof Frame)) parent = parent.getParent(); if (parent != null) popup = new Dialog((Frame) parent, "Error Message", true); // modal dialog else popup = new Frame("Error Message"); // independent window popup.setBackground(Color.white); popup.add(new MC(message), BorderLayout.CENTER); Panel buttonBar = new Panel(); buttonBar.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10)); Button OK = new Button(" OK "); OK.addActionListener(this); buttonBar.add(OK); popup.add(buttonBar, BorderLayout.SOUTH); popup.pack(); if (parent == null) popup.setLocation(100, 80); else popup.setLocation(parent.getLocation().x + 50, parent.getLocation().y + 30); popup.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { popup.dispose(); } }); popup.show(); // make the dialog visible. }
// Constructor public SimpleGLCanvas(Window parent, boolean debug) { debugging = debug; animator = new Animator(this); parent.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { // Run this on another thread than the AWT event queue to // make sure the call to Animator.stop() completes before // exiting new Thread( new Runnable() { public void run() { if (animator.isAnimating()) animator.stop(); System.exit(0); } }) .start(); } }); addGLEventListener(this); // Reset timer resetClock(); }
private void installListeners() { if (myWindow != null) { myWindowListener = createWindowListener(); myWindow.addWindowListener(myWindowListener); myPropertyChangeListener = createWindowPropertyChangeListener(); myWindow.addPropertyChangeListener(myPropertyChangeListener); } }
public static void addExitWindowListener(Window window) { Preconditions.checkArgument(window != null); window.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent windowEvent) { System.exit(1); } }); }
/** Recycles the passed in <code>HeavyWeightPopup</code>. */ private static void recycleHeavyWeightPopup(HeavyWeightPopup popup) { synchronized (HeavyWeightPopup.class) { List<HeavyWeightPopup> cache; Window window = SwingUtilities.getWindowAncestor(popup.getComponent()); Map<Window, List<HeavyWeightPopup>> heavyPopupCache = getHeavyWeightPopupCache(); if (window instanceof Popup.DefaultFrame || !window.isVisible()) { // If the Window isn't visible, we don't cache it as we // likely won't ever get a windowClosed event to clean up. // We also don't cache DefaultFrames as this indicates // there wasn't a valid Window parent, and thus we don't // know when to clean up. popup._dispose(); return; } else if (heavyPopupCache.containsKey(window)) { cache = heavyPopupCache.get(window); } else { cache = new ArrayList<HeavyWeightPopup>(); heavyPopupCache.put(window, cache); // Clean up if the Window is closed final Window w = window; w.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent e) { List<HeavyWeightPopup> popups; synchronized (HeavyWeightPopup.class) { Map<Window, List<HeavyWeightPopup>> heavyPopupCache2 = getHeavyWeightPopupCache(); popups = heavyPopupCache2.remove(w); } if (popups != null) { for (int counter = popups.size() - 1; counter >= 0; counter--) { popups.get(counter)._dispose(); } } } }); } if (cache.size() < MAX_CACHE_SIZE) { cache.add(popup); } else { popup._dispose(); } } }
public void setPalette(Window newValue) { AbstractOSXApplication application = getApplication(); if (palette != null) { palette.removeWindowListener(windowHandler); } palette = newValue; if (palette != null) { palette.addWindowListener(windowHandler); if (getValue(Actions.SELECTED_KEY) == Boolean.TRUE) { application.addPalette(palette); palette.setVisible(true); } else { application.removePalette(palette); palette.setVisible(false); } } }
public WindowMenuItem(final Window window, String title) { super(title, window.isVisible()); addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // window.pack(); window.setVisible(!window.isVisible()); } }); window.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { WindowMenuItem.this.setSelected(false); } public void windowOpened(WindowEvent e) { WindowMenuItem.this.setSelected(USE_SYSTEM_UI); } }); }
public static void main(String[] args) throws Exception { window.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if (bot != null) { if (bot.isConnected()) { bot.disconnect(); } } } }); ActionListener task = new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (Palebot.shouldBeConnected() && !bot.isConnected()) { window.connect(); } if (messageTimes.size() > 0 && System.currentTimeMillis() - messageTimes.get(0) > 30000) { messageTimes.remove(0); System.out.println("Message Count: " + getMessageCount()); } } }; Timer timer = new Timer(1000, task); // fire every 1 seconds timer.setRepeats(true); timer.start(); window.revalidate(); }
/** * _more_ * * @return _more_ */ private boolean windowOk() { getContents(); if (contents == null) { return false; } if (shouldMakeDialog()) { if (dialog == null) { dialog = new JDialog((Frame) null, getWindowTitle(), false); LogUtil.registerWindow(dialog); GuiUtils.packDialog(dialog, contents); dialog.setLocation(100, 100); window = dialog; } } else { if (frame == null) { frame = new JFrame(getWindowTitle()); if (menuBar != null) { GuiUtils.decorateFrame(frame, menuBar); } LogUtil.registerWindow(frame); frame.getContentPane().add(contents); frame.pack(); Msg.translateTree(frame); frame.setLocation(100, 100); window = frame; } } if (window != null) { window.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { windowIsClosing(); } }); } return window != null; }
/** Installs the sheet on the owner. This method is invoked just before the JSheet is shown. */ protected void installSheet() { if (!isNativeSheetSupported() && !isInstalled && isExperimentalSheet()) { Window owner = getOwner(); if (owner != null) { owner.addWindowListener(windowEventHandler); } isInstalled = true; } else { Window owner = getOwner(); if (owner != null) { // Determine the location for the sheet and its owner while // the sheet will be visible. // In case we have to shift the owner to fully display the // dialog, we remember the shift back position. Point ownerLoc = owner.getLocation(); Point sheetLoc; if (isShowAsSheet()) { if (owner instanceof JFrame) { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JFrame) owner).getRootPane().getContentPane().getY()); } else if (owner instanceof JDialog) { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JDialog) owner).getRootPane().getContentPane().getY()); } else { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top); } if (sheetLoc.x < 0) { owner.setLocation(ownerLoc.x - sheetLoc.x, ownerLoc.y); sheetLoc.x = 0; shiftBackLocation = ownerLoc; oldLocation = owner.getLocation(); } else { shiftBackLocation = null; oldLocation = ownerLoc; } } else { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + (owner.getHeight() - getHeight()) / 2); } setLocation(sheetLoc); oldFocusOwner = owner.getFocusOwner(); // Note: We mustn't change the windows focusable state because // this also affects the focusable state of the JSheet. // owner.setFocusableWindowState(false); owner.setEnabled(false); // ((JFrame) owner).setResizable(false); if (isShowAsSheet()) { owner.addComponentListener(ownerMovementHandler); } else { if (owner instanceof Frame) { setTitle(((Frame) owner).getTitle()); } } } isInstalled = true; } }
public void show(Component owner, int aScreenX, int aScreenY, final boolean considerForcedXY) { if (ApplicationManagerEx.getApplicationEx() != null && ApplicationManager.getApplication().isHeadlessEnvironment()) return; if (isDisposed()) { throw new IllegalStateException( "Popup was already disposed. Recreate a new instance to show again"); } assert ApplicationManager.getApplication().isDispatchThread(); addActivity(); final boolean shouldShow = beforeShow(); if (!shouldShow) { removeActivity(); return; } prepareToShow(); if (myInStack) { myFocusTrackback = new FocusTrackback(this, owner, true); myFocusTrackback.setMustBeShown(true); } Dimension sizeToSet = null; if (myDimensionServiceKey != null) { sizeToSet = DimensionService.getInstance().getSize(myDimensionServiceKey, myProject); } if (myForcedSize != null) { sizeToSet = myForcedSize; } if (myMinSize == null) { myMinSize = myContent.getMinimumSize(); } if (sizeToSet == null) { sizeToSet = myContent.getPreferredSize(); } if (sizeToSet != null) { sizeToSet.width = Math.max(sizeToSet.width, myMinSize.width); sizeToSet.height = Math.max(sizeToSet.height, myMinSize.height); myContent.setSize(sizeToSet); myContent.setPreferredSize(sizeToSet); } Point xy = new Point(aScreenX, aScreenY); boolean adjustXY = true; if (myDimensionServiceKey != null) { final Point storedLocation = DimensionService.getInstance().getLocation(myDimensionServiceKey, myProject); if (storedLocation != null) { xy = storedLocation; adjustXY = false; } } if (adjustXY) { final Insets insets = myContent.getInsets(); if (insets != null) { xy.x -= insets.left; xy.y -= insets.top; } } if (considerForcedXY && myForcedLocation != null) { xy = myForcedLocation; } if (myLocateByContent) { final Dimension captionSize = myHeaderPanel.getPreferredSize(); xy.y -= captionSize.height; } Rectangle targetBounds = new Rectangle(xy, myContent.getPreferredSize()); Insets insets = myPopupBorder.getBorderInsets(myContent); if (insets != null) { targetBounds.x += insets.left; targetBounds.y += insets.top; } Rectangle original = new Rectangle(targetBounds); if (myLocateWithinScreen) { ScreenUtil.moveRectangleToFitTheScreen(targetBounds); } if (myMouseOutCanceller != null) { myMouseOutCanceller.myEverEntered = targetBounds.equals(original); } myOwner = IdeFrameImpl.findNearestModalComponent(owner); if (myOwner == null) { myOwner = owner; } myRequestorComponent = owner; boolean forcedDialog = (SystemInfo.isMac && !(myOwner instanceof IdeFrame)) || myMayBeParent; PopupComponent.Factory factory = getFactory(myForcedHeavyweight || myResizable, forcedDialog); myNativePopup = factory.isNativePopup(); myPopup = factory.getPopup(myOwner, myContent, targetBounds.x, targetBounds.y); if (myResizable) { final JRootPane root = myContent.getRootPane(); final IdeGlassPaneImpl glass = new IdeGlassPaneImpl(root); root.setGlassPane(glass); final ResizeComponentListener resizeListener = new ResizeComponentListener(this, glass); glass.addMousePreprocessor(resizeListener, this); glass.addMouseMotionPreprocessor(resizeListener, this); } if (myCaption != null && myMovable) { final MoveComponentListener moveListener = new MoveComponentListener(myCaption) { public void mousePressed(final MouseEvent e) { super.mousePressed(e); if (e.isConsumed()) return; if (UIUtil.isCloseClick(e)) { if (myCaption.isWithinPanel(e)) { cancel(); } } } }; ListenerUtil.addMouseListener(myCaption, moveListener); ListenerUtil.addMouseMotionListener(myCaption, moveListener); final MyContentPanel saved = myContent; Disposer.register( this, new Disposable() { public void dispose() { ListenerUtil.removeMouseListener(saved, moveListener); ListenerUtil.removeMouseMotionListener(saved, moveListener); } }); } for (JBPopupListener listener : myListeners) { listener.beforeShown(new LightweightWindowEvent(this)); } myPopup.setRequestFocus(myRequestFocus); myPopup.show(); final Window window = SwingUtilities.getWindowAncestor(myContent); myWindowListener = new MyWindowListener(); window.addWindowListener(myWindowListener); if (myFocusable) { window.setFocusableWindowState(true); window.setFocusable(true); } myWindow = updateMaskAndAlpha(window); if (myWindow instanceof JWindow) { ((JWindow) myWindow).getRootPane().putClientProperty(KEY, this); } if (myWindow != null) { // dialogwrapper-based popups do this internally through peer, // for other popups like jdialog-based we should exclude them manually, but // we still have to be able to use IdeFrame as parent if (!myMayBeParent && !(myWindow instanceof Frame)) { WindowManager.getInstance().doNotSuggestAsParent(myWindow); } } final Runnable afterShow = new Runnable() { public void run() { if (myPreferredFocusedComponent != null && myInStack && myFocusable) { myFocusTrackback.registerFocusComponent(myPreferredFocusedComponent); } removeActivity(); afterShow(); } }; if (myRequestFocus) { getFocusManager() .requestFocus( new FocusCommand() { @Override public ActionCallback run() { if (isDisposed()) { removeActivity(); return new ActionCallback.Done(); } _requestFocus(); final ActionCallback result = new ActionCallback(); final Runnable afterShowRunnable = new Runnable() { @Override public void run() { afterShow.run(); result.setDone(); } }; if (myNativePopup) { final FocusRequestor furtherRequestor = getFocusManager().getFurtherRequestor(); SwingUtilities.invokeLater( new Runnable() { @Override public void run() { if (isDisposed()) { result.setRejected(); return; } furtherRequestor .requestFocus( new FocusCommand() { @Override public ActionCallback run() { if (isDisposed()) { return new ActionCallback.Rejected(); } _requestFocus(); afterShowRunnable.run(); return new ActionCallback.Done(); } }, true) .notify(result) .doWhenProcessed( new Runnable() { @Override public void run() { removeActivity(); } }); } }); } else { afterShowRunnable.run(); } return result; } }, true) .doWhenRejected( new Runnable() { @Override public void run() { afterShow.run(); } }); } else { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { if (isDisposed()) { removeActivity(); return; } afterShow.run(); } }); } }