001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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:
027: package org.cougaar.tools.csmart.ui.util;
028:
029: import org.cougaar.tools.csmart.ui.viewer.CSMART;
030: import org.cougaar.util.ConfigFinder;
031: import org.cougaar.util.log.Logger;
032:
033: import javax.swing.*;
034: import java.awt.*;
035: import java.io.File;
036: import java.util.ArrayList;
037:
038: /**
039: * Utilities for CSMART GUI.
040: */
041:
042: public class Util {
043:
044: /**
045: * Return a path for the specified filename; the path is determined
046: * using ConfigFinder. If there is any error, null is returned.
047: * @param filename the filename for which to get the path
048: * @return the pathname or null if an error
049: */
050: public static String getPath(String filename) {
051: Logger log = CSMART
052: .createLogger("org.cougaar.tools.csmart.ui.util.Util");
053: ConfigFinder configFinder = ConfigFinder.getInstance("csmart");
054: File file = configFinder.locateFile(filename);
055: String path = null;
056: if (file != null) {
057: try {
058: path = file.getCanonicalPath();
059: } catch (Exception e) {
060: if (log.isWarnEnabled()) {
061: log.warn("Could not find: " + filename);
062: }
063: }
064: } else {
065: if (log.isWarnEnabled()) {
066: log.warn("Could not find: " + filename);
067: }
068: }
069: return path;
070: }
071:
072: /**
073: * Get the single NamedFrame object for the CSMARTUL application.
074: * The NamedFrame object ensures that all frames in the application
075: * have unique names, and it notifies the CSMARTUL application when frames
076: * are added/removed so that the menu of existing frames can be updated.
077: * This method is pointless.
078: * @deprecated The singleton NamedFrame should be obtained directly
079: * using NamedFrame.getNamedFrame().
080: * @return the NamedFrame object for the application
081: **/
082: public static NamedFrame getNamedFrame() {
083: return NamedFrame.getNamedFrame();
084: }
085:
086: /**
087: * Display a list of objects in a dialog.
088: * @param parent the parent component of the dialog
089: * @param values a list of values
090: * @param title the title for the dialog
091: * @param prompt a prompt displayed above the list
092: */
093: public static void showObjectsInList(Component parent,
094: ArrayList values, String title, String prompt) {
095: displayList(parent, values, title, prompt, false);
096: }
097:
098: /**
099: * Display a list of objects in a dialog and return an
100: * array of objects selected by the user.
101: * @param parent the parent component of the dialog
102: * @param values a list of values
103: * @param title the title for the dialog
104: * @param prompt a prompt displayed above the list
105: * @return the objects selected by the user
106: */
107: public static Object[] getObjectsFromList(Component parent,
108: ArrayList values, String title, String prompt) {
109: return displayList(parent, values, title, prompt, true);
110: }
111:
112: private static Object[] displayList(Component parent,
113: ArrayList values, String title, String prompt,
114: boolean allowSelection) {
115: JList list = new JList(values.toArray());
116: JScrollPane jsp = new JScrollPane(list,
117: ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
118: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
119: jsp.setPreferredSize(new Dimension(400, 100));
120: JPanel infoPanel = new JPanel();
121: infoPanel.setLayout(new GridBagLayout());
122: list.setBackground(infoPanel.getBackground());
123: int x = 0;
124: int y = 0;
125: infoPanel
126: .add(new JLabel(prompt), new GridBagConstraints(x, y++,
127: 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
128: GridBagConstraints.NONE,
129: new Insets(10, 0, 5, 5), 0, 0));
130: infoPanel.add(jsp, new GridBagConstraints(x, y++, 1, 1, 0.0,
131: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
132: new Insets(0, 0, 5, 0), 0, 0));
133: if (allowSelection) {
134: int result = JOptionPane.showOptionDialog(parent,
135: infoPanel, title, JOptionPane.OK_CANCEL_OPTION,
136: JOptionPane.PLAIN_MESSAGE, null, null, null);
137: if (result == JOptionPane.OK_OPTION)
138: return list.getSelectedValues();
139: else
140: return null;
141: } else {
142: // FIXME: Bug 1929: Selection still possible.
143: // maybe do list.setEnabled(false);?
144: // But that greys out the text, making it hard to read
145: //list.setEnabled(false);
146: Object[] options = { "OK" };
147: JOptionPane.showOptionDialog(parent, infoPanel, title,
148: JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE,
149: null, options, options[0]);
150: return null;
151: }
152: }
153:
154: }
|