source: src/main/java/weka/gui/ComponentHelper.java @ 27

Last change on this file since 27 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 5.4 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ComponentHelper.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.awt.Component;
26import java.awt.Image;
27import java.net.URL;
28
29import javax.swing.ImageIcon;
30import javax.swing.JOptionPane;
31
32
33/**
34 * A helper class for some common tasks with Dialogs, Icons, etc.
35 *
36 *
37 * @author FracPete (fracpete at waikato dot ac dot nz)
38 * @version $Revision: 1.3 $
39 */
40
41public class ComponentHelper {
42  /** the default directories for images */
43  public final static String[] IMAGES = {"weka/gui/", "weka/gui/images/"};
44 
45  /**
46   * returns the ImageIcon for a given filename and directory, NULL if not successful
47   *
48   * @param dir           the directory to look in for the file
49   * @param filename      the file to retrieve
50   * @return              the imageicon if found, otherwise null
51   */
52  public static ImageIcon getImageIcon(String dir, String filename) {
53    URL            url;
54    ImageIcon      result;
55    int            i;
56   
57    result = null;
58    url    = Loader.getURL(dir, filename);
59   
60    // try to find the image at default locations
61    if (url == null) {
62      for (i = 0; i < IMAGES.length; i++) {
63        url = Loader.getURL(IMAGES[i], filename);
64        if (url != null)
65          break;
66      }
67    }
68   
69    if (url != null)
70      result = new ImageIcon(url);
71   
72    return result;
73  }
74 
75  /**
76   * returns the ImageIcon for a given filename, NULL if not successful
77   *
78   * @param filename      the file to retrieve
79   * @return              the imageicon if found, otherwise null
80   */
81  public static ImageIcon getImageIcon(String filename) {
82    return getImageIcon("", filename);
83  }
84 
85  /**
86   * returns the Image for a given directory and filename, NULL if not successful
87   *
88   * @param dir           the directory to look in for the file
89   * @param filename      the file to retrieve
90   * @return              the image if found, otherwise null
91   */
92  public static Image getImage(String dir, String filename) {
93    ImageIcon      img;
94    Image          result;
95   
96    result = null;
97    img    = getImageIcon(dir, filename);
98   
99    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, String msg, int buttons, int messageType) { 
137    String        icon;
138   
139    switch (messageType) {
140      case JOptionPane.ERROR_MESSAGE:
141      icon = "weka/gui/images/error.gif";
142      break;
143    case JOptionPane.INFORMATION_MESSAGE:
144      icon = "weka/gui/images/information.gif";
145      break;
146    case JOptionPane.WARNING_MESSAGE:
147      icon = "weka/gui/images/information.gif";
148      break;
149    case JOptionPane.QUESTION_MESSAGE:
150      icon = "weka/gui/images/question.gif";
151      break;
152    default:
153      icon = "weka/gui/images/information.gif";
154      break;
155    }
156   
157    return JOptionPane.showConfirmDialog(parent, msg, title, buttons, messageType, getImageIcon(icon));
158  }
159 
160  /**
161   * pops up an input dialog
162   *
163   * @param parent        the parent of this dialog, can be <code>null</code>
164   * @param title         the title to display, can be <code>null</code>
165   * @param msg           the message to display
166   * @param initialValue  the initial value to display as input
167   * @return              the entered value, or if cancelled <code>null</code> 
168   */
169  public static String showInputBox(Component parent, String title, String msg, Object initialValue) {
170    Object        result;
171   
172    if (title == null)
173      title = "Input...";
174   
175    result = JOptionPane.showInputDialog(
176             parent, msg, title, JOptionPane.QUESTION_MESSAGE, getImageIcon("question.gif"), null, initialValue);
177   
178    if (result != null)
179      return result.toString();
180    else
181      return null;
182  }
183}
184
Note: See TracBrowser for help on using the repository browser.