/** * Constructs a popup using the specified settings. * * @param content The component that represents the popup content. * @param hideOnClick If <code>true</code> then the popup will hide when any area outside of it is * clicked (rather like a popup menu). If <code>false</code> the popup will not hide when any * area outside of it is clicked. * @param timerDelay The number of milliseconds after which the the popup will automatically hide * itself. If a value of zero is specified, then the popup will remain displayed indefinitely. * @param isModal If <code>true</code>, mouse listener events that occur outside the component * area are forwarded to the appropriate component in the content pane i.e other components * may be interacted with whilst the popup is displayed. If <code>false</code> then the popup * behaves rather like a modal dialog, so that no other components can receive mouse events * until the component has closed. */ public PopupComponent(JComponent content, boolean hideOnClick, int timerDelay, boolean isModal) { glassPane = new JPanel(null); glassPane.setLayout(null); glassPane.add(content); glassPane.setOpaque(false); listeners = new ArrayList<PopupComponentListener>(); setupListeners(); setContent(content); this.HIDE_ON_CLICK = hideOnClick; this.POPUP_IS_MODAL = isModal; // Check to see if a timer delay has been // specified. If so, set up the timer. if (timerDelay > 0) { HIDE_ON_TIMER = true; HIDE_TIMER_DELAY = timerDelay; timer = new Timer( HIDE_TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent e) { // Hide the popup if the mouse is outside // the content if (mouseOverGlassPane == true) { hidePopup(); timer.stop(); } else { timer.stop(); timer.start(); } } }); } }
/** * Called by the constructor methods to create the default <code>contentPane</code>. By default * this method creates a new <code>JComponent</code> add sets a <code>BorderLayout</code> as its * <code>LayoutManager</code>. * * @return the default <code>contentPane</code> */ protected Container createContentPane() { JComponent c = new JPanel(); c.setName(this.getName() + ".contentPane"); c.setLayout( new BorderLayout() { /* This BorderLayout subclass maps a null constraint to CENTER. * Although the reference BorderLayout also does this, some VMs * throw an IllegalArgumentException. */ public void addLayoutComponent(Component comp, Object constraints) { if (constraints == null) { constraints = BorderLayout.CENTER; } super.addLayoutComponent(comp, constraints); } }); return c; }
private static JSheet createSheet(final JOptionPane pane, Component parentComponent, int style) { Window window = getWindowForComponent(parentComponent); final JSheet sheet; if (window instanceof Frame) { sheet = new JSheet((Frame) window); } else { sheet = new JSheet((Dialog) window); } JComponent contentPane = (JComponent) sheet.getContentPane(); contentPane.setLayout(new BorderLayout()); if (isNativeSheetSupported()) { contentPane.setBorder(new EmptyBorder(12, 0, 0, 0)); } contentPane.add(pane, BorderLayout.CENTER); sheet.setResizable(false); sheet.addWindowListener( new WindowAdapter() { private boolean gotFocus = false; @Override public void windowClosing(WindowEvent we) { pane.setValue(null); } @Override public void windowClosed(WindowEvent we) { if (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) { sheet.fireOptionSelected(pane); } } @Override public void windowGainedFocus(WindowEvent we) { // Once window gets focus, set initial focus if (!gotFocus) { // Ugly dirty hack: JOptionPane.selectInitialValue() is protected. // So we call directly into the UI. This may cause mayhem, // because we override the encapsulation. // pane.selectInitialValue(); OptionPaneUI ui = pane.getUI(); if (ui != null) { ui.selectInitialValue(pane); } gotFocus = true; } } }); sheet.addComponentListener( new ComponentAdapter() { @Override public void componentShown(ComponentEvent ce) { // reset value to ensure closing works properly pane.setValue(JOptionPane.UNINITIALIZED_VALUE); } }); pane.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { // Let the defaultCloseOperation handle the closing // if the user closed the window without selecting a button // (newValue = null in that case). Otherwise, close the sheet. if (sheet.isVisible() && event.getSource() == pane && (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { sheet.setVisible(false); sheet.fireOptionSelected(pane); } } }); sheet.pack(); return sheet; }
public BundleOptionsFrame(String displayName, String instanceName, List<OptionGroup> options) { setResizable(false); this.displayName = displayName; content = (JComponent) getContentPane(); content.setLayout(new GridBagLayout()); final BundleOptionsFrame frame = this; // Close button Action closeAction = new AbstractAction("Close") { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false); } }; JButton close_button = new JButton(closeAction); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = currentRow++; c.anchor = GridBagConstraints.LINE_START; c.insets = new Insets(2, 2, 2, 2); content.add(close_button, c); // Escape key binding JComponent root = frame.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close"); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke("ctrl W"), "close"); root.getActionMap().put("close", closeAction); if (instanceName != null) { // Predicate name StringOption opt = new StringOption(); opt.setDisplayName("Predicate name"); opt.setDefault(instanceName); instanceNameField = new StringField(opt); instanceNameField.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { updateTitle(); } }); addField(instanceNameField); JTextField tf = (JTextField) instanceNameField.getComponent(); tf.selectAll(); tf.requestFocusInWindow(); } else { // We're a codec; no instance name instanceNameField = null; } updateTitle(); // Options ExampleField example = null; for (OptionGroup group : options) { addSeparator(group.getDisplayName()); for (Option option : group.getOptions()) { OptionField field; if (option instanceof BooleanOption) { field = new BooleanField((BooleanOption) option); } else if (option instanceof StringOption) { field = new StringField((StringOption) option); } else if (option instanceof NumberOption) { field = new NumberField((NumberOption) option); } else if (option instanceof ChoiceOption) { field = new ChoiceField((ChoiceOption) option); } else if (option instanceof ExampleOption) { if (example != null) { throw new IllegalArgumentException("Cannot display more than one ExampleOption"); } example = new ExampleField((ExampleOption) option); field = example; } else { throw new IllegalArgumentException("Unknown option type"); } addField(field); optionFields.add(field); } } this.exampleField = example; pack(); }