synthoptionpaneui.java

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

JAVA
1,377
字号
	GridBagConstraints cons = new GridBagConstraints();	cons.gridx = cons.gridy = 0;	cons.gridwidth = GridBagConstraints.REMAINDER;	cons.gridheight = 1;        SynthContext context = getContext(optionPane, ENABLED);	cons.anchor = context.getStyle().getInt(context,                      "OptionPane.messageAnchor", GridBagConstraints.CENTER);        context.dispose();	cons.insets = new Insets(0,0,3,0);	addMessageComponents(body, cons, getMessage(),			  getMaxCharactersPerLineCount(), false);	top.add(realBody, BorderLayout.CENTER);	addIcon(top);	return top;    }    /**     * Creates the appropriate object to represent <code>msg</code> and     * places it into <code>container</code>. If <code>msg</code> is an     * instance of Component, it is added directly, if it is an Icon,     * a JLabel is created to represent it, otherwise a JLabel is     * created for the string, if <code>d</code> is an Object[], this     * method will be recursively invoked for the children.     * <code>internallyCreated</code> is true if Objc is an instance     * of Component and was created internally by this method (this is     * used to correctly set hasCustomComponents only if !internallyCreated).     */    protected void addMessageComponents(Container container,				     GridBagConstraints cons,				     Object msg, int maxll,				     boolean internallyCreated) {	if (msg == null) {	    return;        }	if (msg instanceof Component) {            // To workaround problem where Gridbad will set child            // to its minimum size if its preferred size will not fit            // within allocated cells            if (msg instanceof JScrollPane || msg instanceof JPanel) {                cons.fill = GridBagConstraints.BOTH;                cons.weighty = 1;            } else {	        cons.fill = GridBagConstraints.HORIZONTAL;            }	    cons.weightx = 1;	    container.add((Component) msg, cons);	    cons.weightx = 0;            cons.weighty = 0;	    cons.fill = GridBagConstraints.NONE;	    cons.gridy++;	    if (!internallyCreated) {		hasCustomComponents = true;            }	} else if (msg instanceof Object[]) {	    Object [] msgs = (Object[]) msg;	    for (int i = 0; i < msgs.length; i++) {		addMessageComponents(container, cons, msgs[i], maxll, false);            }	} else if (msg instanceof Icon) {	    JLabel label = new JLabel( (Icon)msg, SwingConstants.CENTER );            configureMessageLabel(label);	    addMessageComponents(container, cons, label, maxll, true);	} else {	    String s = msg.toString();	    int len = s.length();	    if (len <= 0) {		return;            }	    int nl = -1;	    int nll = 0;	    if ((nl = s.indexOf(newline)) >= 0) {		nll = newline.length();	    } else if ((nl = s.indexOf("\r\n")) >= 0) {	        nll = 2;	    } else if ((nl = s.indexOf('\n')) >= 0) {	        nll = 1;	    }	    if (nl >= 0) {		// break up newlines		if (nl == 0) {                    JPanel breakPanel = new JPanel() {		        public Dimension getPreferredSize() {			    Font       f = getFont();			    			    if (f != null) {				return new Dimension(1, f.getSize() + 2);                            }			    return new Dimension(0, 0);		        }                    };                    breakPanel.setName("OptionPane.break");		    addMessageComponents(container, cons, breakPanel, maxll,                                         true);		} else {		    addMessageComponents(container, cons, s.substring(0, nl),				      maxll, false);                }		addMessageComponents(container, cons, s.substring(nl + nll),                                     maxll, false);	    } else if (len > maxll) {		Container c = Box.createVerticalBox();                c.setName("OptionPane.verticalBox");		burstStringInto(c, s, maxll);		addMessageComponents(container, cons, c, maxll, true );	    } else {	        JLabel label;		label = new JLabel( s, JLabel.LEADING );                label.setName("OptionPane.label");                configureMessageLabel(label);		addMessageComponents(container, cons, label, maxll, true);	    }	}    }    /**     * Returns the message to display from the JOptionPane the receiver is     * providing the look and feel for.     */    protected Object getMessage() {	inputComponent = null;	if (optionPane != null) {	    if (optionPane.getWantsInput()) {		/* Create a user component to capture the input. If the		   selectionValues are non null the component and there		   are < 20 values it'll be a combobox, if non null and		   >= 20, it'll be a list, otherwise it'll be a textfield. */		Object             message = optionPane.getMessage();		Object[]           sValues = optionPane.getSelectionValues();		Object             inputValue = optionPane		                           .getInitialSelectionValue();		JComponent         toAdd;		if (sValues != null) {		    if (sValues.length < 20) {			JComboBox            cBox = new JComboBox();                        cBox.setName("OptionPane.comboBox");			for(int counter = 0, maxCounter = sValues.length;			    counter < maxCounter; counter++) {			    cBox.addItem(sValues[counter]);                        }			if (inputValue != null) {			    cBox.setSelectedItem(inputValue);                        }			inputComponent = cBox;			toAdd = cBox;		    } else {			JList                list = new JList(sValues);			JScrollPane          sp = new JScrollPane(list);                        list.setName("OptionPane.scrollPane");                        list.setName("OptionPane.list");			list.setVisibleRowCount(10);			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);			if(inputValue != null)			    list.setSelectedValue(inputValue, true);			list.addMouseListener(new ListSelectionListener());			toAdd = sp;			inputComponent = list;		    }		} else {		    MultiplexingTextField   tf = new MultiplexingTextField(20);                    tf.setName("OptionPane.textField");                    tf.setKeyStrokes(new KeyStroke[] {                                     KeyStroke.getKeyStroke("ENTER") } );		    if (inputValue != null) {                        String inputString = inputValue.toString();			tf.setText(inputString);                        tf.setSelectionStart(0);                        tf.setSelectionEnd(inputString.length());                    }		    tf.addActionListener(new TextFieldActionListener());		    toAdd = inputComponent = tf;		}		Object[]           newMessage;		if (message == null) {		    newMessage = new Object[1];		    newMessage[0] = toAdd;				} else {		    newMessage = new Object[2];		    newMessage[0] = message;		    newMessage[1] = toAdd;		}		return newMessage;	    }	    return optionPane.getMessage();	}	return null;    }    /**     * Creates and adds a JLabel representing the icon returned from     * <code>getIcon</code> to <code>top</code>. This is messaged from     * <code>createMessageArea</code>     */    protected void addIcon(Container top) {	/* Create the icon. */	Icon                  sideIcon = getIcon();	if (sideIcon != null) {	    JLabel            iconLabel = new JLabel(sideIcon);            iconLabel.setName("OptionPane.iconLabel");	    iconLabel.setVerticalAlignment(SwingConstants.TOP);	    top.add(iconLabel, BorderLayout.BEFORE_LINE_BEGINS);	}    }    /**     * Returns the icon from the JOptionPane the receiver is providing     * the look and feel for, or the default icon as returned from     * <code>getDefaultIcon</code>.     */    protected Icon getIcon() {	Icon      mIcon = (optionPane == null ? null : optionPane.getIcon());	if(mIcon == null && optionPane != null)	    mIcon = getIconForType(optionPane.getMessageType());	return mIcon;    }    /**     * Returns the icon to use for the passed in type.     */    protected Icon getIconForType(int messageType) {	if(messageType < 0 || messageType > 3)	    return null;        String propertyName = null;	switch(messageType) {	case 0:	    propertyName = "OptionPane.errorIcon";            break;	case 1:	    propertyName = "OptionPane.informationIcon";            break;	case 2:	    propertyName = "OptionPane.warningIcon";            break;	case 3:	    propertyName = "OptionPane.questionIcon";            break;	}        if (propertyName != null) {            SynthContext context = getContext(optionPane, ENABLED);            Icon icon = context.getStyle().getIcon(context, propertyName);            context.dispose();            return icon;        }	return null;    }    /**     * Returns the maximum number of characters to place on a line.     */    protected int getMaxCharactersPerLineCount() {	return optionPane.getMaxCharactersPerLineCount();    }   /**     * Recursively creates new JLabel instances to represent <code>d</code>.     * Each JLabel instance is added to <code>c</code>.     */    protected void burstStringInto(Container c, String d, int maxll) {	// Primitive line wrapping	int len = d.length();	if (len <= 0)	    return;	if (len > maxll) {	    int p = d.lastIndexOf(' ', maxll);	    if (p <= 0)		p = d.indexOf(' ', maxll);	    if (p > 0 && p < len) {		burstStringInto(c, d.substring(0, p), maxll);		burstStringInto(c, d.substring(p + 1), maxll);		return;	    }	}	JLabel label = new JLabel(d, JLabel.LEFT);        label.setName("OptionPane.label");        configureMessageLabel(label);	c.add(label);    }    protected Container createSeparator() {        JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);        separator.setName("OptionPane.separator");        return separator;    }    /**     * Creates and returns a Container containing the buttons. The buttons     * are created by calling <code>getButtons</code>.     */    protected Container createButtonArea() {        JPanel bottom = new JPanel();        bottom.setName("OptionPane.buttonArea");        SynthContext context = getContext(optionPane, ENABLED);        SynthStyle style = context.getStyle();	bottom.setLayout(new ButtonAreaLayout(           style.getBoolean(context, "OptionPane.sameSizeButtons", true),           style.getInt(context, "OptionPane.buttonPadding", 10),           style.getInt(context, "OptionPane.buttonOrientation",                        SwingConstants.RIGHT),           style.getBoolean(context, "OptionPane.isYesLast", true)));	addButtonComponents(bottom, getButtons(context),                            getInitialValueIndex());        context.dispose();	return bottom;    }    /**     * Creates the appropriate object to represent each of the objects in     * <code>buttons</code> and adds it to <code>container</code>. This     * differs from addMessageComponents in that it will recurse on     * <code>buttons</code> and that if button is not a Component     * it will create an instance of JButton.     */    protected void addButtonComponents(Container container, Object[] buttons,				 int initialIndex) {	if (buttons != null && buttons.length > 0) {	    boolean            sizeButtonsToSame = getSizeButtonsToSameWidth();	    boolean            createdAll = true;	    int                numButtons = buttons.length;	    JButton[]          createdButtons = null;

⌨️ 快捷键说明

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