001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.util;
027:
028: import java.awt.Component;
029: import java.awt.Dimension;
030:
031: import javax.swing.Icon;
032: import javax.swing.JDialog;
033: import javax.swing.JOptionPane;
034:
035: /**
036: * This fixes bugs in JOptionPane.showOptionDialog and
037: * JOptionPane.showInputDialog that sizes the dialog too small when
038: * using larger demo fonts. It fixes the workaround for bug#4135218 by
039: * dynamically returning a preferred size based on the current
040: * preferred size instead of statically computing a preferred size.
041: * The static technique doesn't track UI changes.
042: **/
043:
044: public class OptionPane extends JOptionPane {
045: public OptionPane(Object message, int messageType, int optionType,
046: Icon icon, Object[] options, Object initialValue) {
047: super (message, messageType, optionType, icon, options,
048: initialValue);
049: }
050:
051: private Dimension prevSize;
052: private Dimension prevResult;
053:
054: public Dimension getPreferredSize() {
055: Dimension sz = super .getPreferredSize();
056: if (!sz.equals(prevSize)) {
057: prevSize = sz;
058: prevResult = new Dimension(sz.width + 5, sz.height + 2);
059: }
060: return prevResult;
061: }
062:
063: public static int showOptionDialog(Component parentComponent,
064: Object message, String title, int optionType,
065: int messageType, Icon icon, Object[] options,
066: Object initialValue) {
067: JOptionPane pane = new OptionPane(message, messageType,
068: optionType, icon, options, initialValue);
069: pane.setInitialValue(initialValue);
070:
071: JDialog dialog = pane.createDialog(parentComponent, title);
072:
073: pane.selectInitialValue();
074:
075: dialog.show();
076:
077: Object selectedValue = pane.getValue();
078:
079: if (selectedValue == null)
080: return JOptionPane.CLOSED_OPTION;
081: if (options == null) {
082: if (selectedValue instanceof Integer)
083: return ((Integer) selectedValue).intValue();
084: return JOptionPane.CLOSED_OPTION;
085: }
086: for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
087: if (options[counter].equals(selectedValue))
088: return counter;
089: }
090: return JOptionPane.CLOSED_OPTION;
091: }
092:
093: public static Object showInputDialog(Component parentComponent,
094: Object message, String title, int messageType, Icon icon,
095: Object[] selectionValues, Object initialSelectionValue) {
096: JOptionPane pane = new OptionPane(message, messageType,
097: JOptionPane.OK_CANCEL_OPTION, icon, null, null);
098:
099: pane.setWantsInput(true);
100: pane.setSelectionValues(selectionValues);
101: pane.setInitialSelectionValue(initialSelectionValue);
102:
103: JDialog dialog = pane.createDialog(parentComponent, title);
104:
105: pane.selectInitialValue();
106: dialog.pack(); // This makes the difference!
107: dialog.show();
108:
109: Object value = pane.getInputValue();
110:
111: if (value == JOptionPane.UNINITIALIZED_VALUE)
112: return null;
113: return value;
114: }
115: }
|