synthoptionpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,377 行 · 第 1/4 页

JAVA
1,377
字号
	    if (sizeButtonsToSame) {		createdButtons = new JButton[numButtons];            }	    for(int counter = 0; counter < numButtons; counter++) {		Object       button = buttons[counter];		Component    newComponent;		if (button instanceof Component) {		    createdAll = false;		    newComponent = (Component)button;		    container.add(newComponent);		    hasCustomComponents = true;				} else {		    JButton      aButton;                    if (button instanceof ButtonFactory) {                        aButton = ((ButtonFactory)button).createButton();                    }                    else if (button instanceof Icon) {                        aButton = new JButton((Icon)button);                    }                    else {                        aButton = new JButton(button.toString());                    }                    aButton.setName("OptionPane.button");		    aButton.setMultiClickThreshhold(multiClickThreshold);                    configureButton(aButton);		    container.add(aButton);                    ActionListener buttonListener = createButtonActionListener(counter);                    if (buttonListener != null) {                        aButton.addActionListener(buttonListener);                    }		    newComponent = aButton;		}		if (sizeButtonsToSame && createdAll && 		   (newComponent instanceof JButton)) {		    createdButtons[counter] = (JButton)newComponent;		}		if (counter == initialIndex) {		    initialFocusComponent = newComponent;                    if (initialFocusComponent instanceof JButton) {                        JButton defaultB = (JButton)initialFocusComponent;                        defaultB.addAncestorListener(new AncestorListener() {                           public void ancestorAdded(AncestorEvent e) {                                JButton defaultButton = (JButton)e.getComponent();                               JRootPane root = SwingUtilities.getRootPane(defaultButton);                               if (root != null) {                                   root.setDefaultButton(defaultButton);                               }                           }                           public void ancestorRemoved(AncestorEvent event) {}                           public void ancestorMoved(AncestorEvent event) {}                        });                    }		}	    }	    ((ButtonAreaLayout)container.getLayout()).		              setSyncAllSizes((sizeButtonsToSame && createdAll));	    /* Set the padding, windows seems to use 8 if <= 2 components,	       otherwise 4 is used. It may actually just be the size of the	       buttons is always the same, not sure. */	    if (sizeButtonsToSame && createdAll) {		JButton               aButton;		int                   padSize;		padSize = (numButtons <= 2? 8 : 4);		for(int counter = 0; counter < numButtons; counter++) {		    aButton = createdButtons[counter];		    aButton.setMargin(new Insets(2, padSize, 2, padSize));		}	    }	}    }    protected ActionListener createButtonActionListener(int buttonIndex) {        return new ButtonActionListener(buttonIndex);    }    /**     * Returns the buttons to display from the JOptionPane the receiver is     * providing the look and feel for. If the JOptionPane has options     * set, they will be provided, otherwise if the optionType is     * YES_NO_OPTION, yesNoOptions is returned, if the type is     * YES_NO_CANCEL_OPTION yesNoCancelOptions is returned, otherwise     * defaultButtons are returned.     */    protected Object[] getButtons(SynthContext context) {	if (optionPane != null) {	    Object[] suppliedOptions = optionPane.getOptions();	    if (suppliedOptions == null) {                Object[] defaultOptions;		int type = optionPane.getOptionType();                Locale l = optionPane.getLocale();                SynthStyle style = context.getStyle();		if (type == JOptionPane.YES_NO_OPTION) {                    defaultOptions = new ButtonFactory[2];                    defaultOptions[0] = new ButtonFactory(                        UIManager.getString("OptionPane.yesButtonText", l),                        getMnemonic("OptionPane.yesButtonMnemonic", l),                        style.getIcon(context, "OptionPane.yesIcon"));                    defaultOptions[1] = new ButtonFactory(                        UIManager.getString("OptionPane.noButtonText", l),                        getMnemonic("OptionPane.noButtonMnemonic", l),                        style.getIcon(context, "OptionPane.noIcon"));		} else if (type == JOptionPane.YES_NO_CANCEL_OPTION) {                    defaultOptions = new ButtonFactory[3];                    defaultOptions[0] = new ButtonFactory(                        UIManager.getString("OptionPane.yesButtonText", l),                        getMnemonic("OptionPane.yesButtonMnemonic", l),                        style.getIcon(context, "OptionPane.yesIcon"));                    defaultOptions[1] = new ButtonFactory(                        UIManager.getString("OptionPane.noButtonText",l),                        getMnemonic("OptionPane.noButtonMnemonic", l),                        style.getIcon(context, "OptionPane.noIcon"));                    defaultOptions[2] = new ButtonFactory(                        UIManager.getString("OptionPane.cancelButtonText",l),                        getMnemonic("OptionPane.cancelButtonMnemonic", l),                        style.getIcon(context, "OptionPane.cancelIcon"));		} else if (type == JOptionPane.OK_CANCEL_OPTION) {                    defaultOptions = new ButtonFactory[2];                    defaultOptions[0] = new ButtonFactory(                        UIManager.getString("OptionPane.okButtonText",l),                        getMnemonic("OptionPane.okButtonMnemonic", l),                        style.getIcon(context, "OptionPane.okIcon"));                    defaultOptions[1] = new ButtonFactory(                        UIManager.getString("OptionPane.cancelButtonText",l),                        getMnemonic("OptionPane.cancelButtonMnemonic", l),                        style.getIcon(context, "OptionPane.cancelIcon"));		} else {                    defaultOptions = new ButtonFactory[1];                    defaultOptions[0] = new ButtonFactory(                        UIManager.getString("OptionPane.okButtonText",l),                        getMnemonic("OptionPane.okButtonMnemonic", l),                        style.getIcon(context, "OptionPane.okIcon"));                }                return defaultOptions;                	    }	    return suppliedOptions;	}	return null;    }    /**     * Returns the mnemonic for the passed in key.     */    private int getMnemonic(String key, Locale l) {        String value = (String)UIManager.get(key, l);        if (value == null) {            return 0;        }        try {            return Integer.parseInt(value);        }        catch (NumberFormatException nfe) { }        return 0;    }    /**     * Returns true, basic L&F wants all the buttons to have the same     * width.     */    protected boolean getSizeButtonsToSameWidth() {	return true;    }    /**     * Returns the initial index into the buttons to select. The index     * is calculated from the initial value from the JOptionPane and     * options of the JOptionPane or 0.     */    protected int getInitialValueIndex() {	if (optionPane != null) {	    Object             iv = optionPane.getInitialValue();	    Object[]           options = optionPane.getOptions();	    if(options == null) {		return 0;	    }	    else if(iv != null) {		for(int counter = options.length - 1; counter >= 0; counter--){		    if(options[counter].equals(iv))			return counter;		}	    }	}	return -1;    }    /**     * Sets the input value in the option pane the receiver is providing     * the look and feel for based on the value in the inputComponent.     */    protected void resetInputValue() {	if(inputComponent != null && (inputComponent instanceof JTextField)) {	    optionPane.setInputValue(((JTextField)inputComponent).getText());	} else if(inputComponent != null &&                  (inputComponent instanceof JComboBox)) {	    optionPane.setInputValue(((JComboBox)inputComponent)				     .getSelectedItem());	} else if(inputComponent != null) {	    optionPane.setInputValue(((JList)inputComponent)				     .getSelectedValue());        }    }    /**     * If inputComponent is non-null, the focus is requested on that,     * otherwise request focus on the default value     */    public void selectInitialValue(JOptionPane op) {	if (inputComponent != null)	    inputComponent.requestFocus();	else {	    if (initialFocusComponent != null)	        initialFocusComponent.requestFocus();            if (initialFocusComponent instanceof JButton) {                JRootPane root = SwingUtilities.getRootPane(initialFocusComponent);                if (root != null) {                    root.setDefaultButton((JButton)initialFocusComponent);                }            }        }    }    /**     * Returns true if in the last call to validateComponent the message     * or buttons contained a subclass of Component.     */    public boolean containsCustomComponents(JOptionPane op) {	return hasCustomComponents;    }    /**     * <code>ButtonAreaLayout</code> behaves in a similar manner to     * <code>FlowLayout</code>. It lays out all components from left to     * right. If <code>syncAllSizes</code> is true, the sizes of each     * component will be set to the largest preferred size width.     *     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of BasicOptionPaneUI.     */      public static class ButtonAreaLayout implements LayoutManager {	protected boolean           syncAllSizes;	protected int               padding;        private int orientation;        private boolean reverseButtons;	public ButtonAreaLayout(boolean syncAllSizes, int padding,                                int orientation, boolean reverseButtons) {	    this.syncAllSizes = syncAllSizes;	    this.padding = padding;            this.orientation = orientation;            this.reverseButtons = reverseButtons;	}	public void setSyncAllSizes(boolean newValue) {	    syncAllSizes = newValue;	}	public boolean getSyncAllSizes() {	    return syncAllSizes;	}	public void setPadding(int newPadding) {	    this.padding = newPadding;	}	public int getPadding() {	    return padding;	}	public void addLayoutComponent(String string, Component comp) {	}        public void setOrientation(int orientation) {            this.orientation = orientation;        }        public int getOrientation() {            return orientation;        }        private int getOrientation(Container container) {            if (container.getComponentOrientation().isLeftToRight()) {                return getOrientation();            }            switch (getOrientation()) {            case SwingConstants.LEFT:                return SwingConstants.RIGHT;            case SwingConstants.RIGHT:                return SwingConstants.LEFT;            case SwingConstants.CENTER:                return SwingConstants.CENTER;            }            return SwingConstants.LEFT;        }	public void layoutContainer(Container container) {	    Component[]      children = container.getComponents();	    if(children != null && children.length > 0) {		int               numChildren = children.length;		Insets            insets = container.getInsets();                int maxWidth = 0;                int maxHeight = 0;                int totalButtonWidth = 0;                int x = 0;                for(int counter = 0; counter < numChildren; counter++) {                    Dimension pref = children[counter].getPreferredSize();                    maxWidth = Math.max(maxWidth, pref.width);                    maxHeight = Math.max(maxHeight, pref.height);                    totalButtonWidth += pref.width;                }                if (getSyncAllSizes()) {                    totalButtonWidth = maxWidth * numChildren;                }                totalButtonWidth += (numChildren - 1) * padding;                switch (getOrientation(container)) {                case SwingConstants.LEFT:                    x = insets.left;                    break;                case SwingConstants.RIGHT:                    x = container.getWidth() - insets.right - totalButtonWidth;                    break;                case SwingConstants.CENTER:                    x = (container.getWidth() - totalButtonWidth) / 2;                    break;                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?