001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * ComponentHelper.java
019: * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.gui;
024:
025: import java.awt.Component;
026: import java.awt.Image;
027: import java.net.URL;
028:
029: import javax.swing.ImageIcon;
030: import javax.swing.JOptionPane;
031:
032: /**
033: * A helper class for some common tasks with Dialogs, Icons, etc.
034: *
035: *
036: * @author FracPete (fracpete at waikato dot ac dot nz)
037: * @version $Revision: 1.3 $
038: */
039:
040: public class ComponentHelper {
041: /** the default directories for images */
042: public final static String[] IMAGES = { "weka/gui/",
043: "weka/gui/images/" };
044:
045: /**
046: * returns the ImageIcon for a given filename and directory, NULL if not successful
047: *
048: * @param dir the directory to look in for the file
049: * @param filename the file to retrieve
050: * @return the imageicon if found, otherwise null
051: */
052: public static ImageIcon getImageIcon(String dir, String filename) {
053: URL url;
054: ImageIcon result;
055: int i;
056:
057: result = null;
058: url = Loader.getURL(dir, filename);
059:
060: // try to find the image at default locations
061: if (url == null) {
062: for (i = 0; i < IMAGES.length; i++) {
063: url = Loader.getURL(IMAGES[i], filename);
064: if (url != null)
065: break;
066: }
067: }
068:
069: if (url != null)
070: result = new ImageIcon(url);
071:
072: return result;
073: }
074:
075: /**
076: * returns the ImageIcon for a given filename, NULL if not successful
077: *
078: * @param filename the file to retrieve
079: * @return the imageicon if found, otherwise null
080: */
081: public static ImageIcon getImageIcon(String filename) {
082: return getImageIcon("", filename);
083: }
084:
085: /**
086: * returns the Image for a given directory and filename, NULL if not successful
087: *
088: * @param dir the directory to look in for the file
089: * @param filename the file to retrieve
090: * @return the image if found, otherwise null
091: */
092: public static Image getImage(String dir, String filename) {
093: ImageIcon img;
094: Image result;
095:
096: result = null;
097: img = getImageIcon(dir, filename);
098:
099: if (img != null)
100: result = img.getImage();
101:
102: return result;
103: }
104:
105: /**
106: * returns the Image for a given filename, NULL if not successful
107: *
108: * @param filename the file to retrieve
109: * @return the image if found, otherwise null
110: */
111: public static Image getImage(String filename) {
112: ImageIcon img;
113: Image result;
114:
115: result = null;
116: img = getImageIcon(filename);
117:
118: if (img != null)
119: result = img.getImage();
120:
121: return result;
122: }
123:
124: /**
125: * displays a message box with the given title, message, buttons and icon
126: * ant the dimension.
127: * it returns the pressed button.
128: * @param parent the parent component
129: * @param title the title of the message box
130: * @param msg the text to display
131: * @param buttons the captions of the buttons to display
132: * @param messageType the type of message like defined in <code>JOptionPane</code> (the icon is determined on this basis)
133: * @return the button that was pressed
134: * @see JOptionPane
135: */
136: public static int showMessageBox(Component parent, String title,
137: String msg, int buttons, int messageType) {
138: String icon;
139:
140: switch (messageType) {
141: case JOptionPane.ERROR_MESSAGE:
142: icon = "weka/gui/images/error.gif";
143: break;
144: case JOptionPane.INFORMATION_MESSAGE:
145: icon = "weka/gui/images/information.gif";
146: break;
147: case JOptionPane.WARNING_MESSAGE:
148: icon = "weka/gui/images/information.gif";
149: break;
150: case JOptionPane.QUESTION_MESSAGE:
151: icon = "weka/gui/images/question.gif";
152: break;
153: default:
154: icon = "weka/gui/images/information.gif";
155: break;
156: }
157:
158: return JOptionPane.showConfirmDialog(parent, msg, title,
159: buttons, messageType, getImageIcon(icon));
160: }
161:
162: /**
163: * pops up an input dialog
164: *
165: * @param parent the parent of this dialog, can be <code>null</code>
166: * @param title the title to display, can be <code>null</code>
167: * @param msg the message to display
168: * @param initialValue the initial value to display as input
169: * @return the entered value, or if cancelled <code>null</code>
170: */
171: public static String showInputBox(Component parent, String title,
172: String msg, Object initialValue) {
173: Object result;
174:
175: if (title == null)
176: title = "Input...";
177:
178: result = JOptionPane.showInputDialog(parent, msg, title,
179: JOptionPane.QUESTION_MESSAGE,
180: getImageIcon("question.gif"), null, initialValue);
181:
182: if (result != null)
183: return result.toString();
184: else
185: return null;
186: }
187: }
|