001: package org.enhydra.jawe;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.GraphicsEnvironment;
007: import java.awt.Window;
008:
009: import javax.swing.BorderFactory;
010: import javax.swing.Icon;
011: import javax.swing.JLabel;
012: import javax.swing.JPanel;
013: import javax.swing.JWindow;
014: import javax.swing.SwingConstants;
015:
016: /**
017: * Used to view some kind of splash screen.
018: *
019: * @author Sasa Bojanic
020: * @version 1.0
021: */
022: public class WaitScreen extends JWindow {
023:
024: JLabel splashI = new JLabel();
025: JLabel splashM = new JLabel();
026: JLabel splashT = new JLabel();
027: JPanel p = new JPanel();
028:
029: public WaitScreen(Window parent) {
030: super (parent);
031:
032: getContentPane().add(p, BorderLayout.CENTER);
033: splashI.setLayout(new BorderLayout());
034: splashI.add(splashT, BorderLayout.NORTH);
035: splashI.add(splashM, BorderLayout.CENTER);
036: splashI.setBorder(BorderFactory.createRaisedBevelBorder());
037: p.add(splashI);
038: }
039:
040: public void show(Icon splashIcon, String splashTitle,
041: String splashMessage) {
042: String pre = "<html><font size=4 face=\"sans-serif\">";// color=\"blue\">";
043: String post = "</font></html>";
044: if (splashTitle == null)
045: splashTitle = "";
046: if (splashMessage == null)
047: splashMessage = "";
048: int pw = getPreferredWidth(splashTitle, splashMessage);
049: splashTitle = pre + splashTitle + post;
050: pre = "<html><font size=4 face=\"sans-serif\">";// color=\"red\">";
051: splashMessage = pre + splashMessage + post;
052:
053: try {
054: splashI.setIcon(splashIcon);
055: } catch (Exception ex) {
056: }
057: splashT.setText(splashTitle);
058: splashT.setHorizontalAlignment(SwingConstants.CENTER);
059: splashM.setText(splashMessage);
060: splashM.setHorizontalAlignment(SwingConstants.CENTER);
061: try {
062: if (pw > 600)
063: pw = 600;
064: splashI
065: .setPreferredSize(new Dimension(pw,
066: (int) ((3 + pw / 600)
067: * getFontMetrics(getFont())
068: .getHeight() * 1.5)));
069: pack();
070: Utils.center(this , 10, 10);
071: setVisible(true);
072: p.paintImmediately(p.getBounds());
073: } catch (Exception ex) {
074: setVisible(false);
075: }
076:
077: }
078:
079: protected int getPreferredWidth(String s1, String s2) {
080: int w1 = 10 * s1.length();
081: int w2 = 10 * s2.length();
082: Font f = getFont();
083: if (f == null) {
084: f = GraphicsEnvironment.getLocalGraphicsEnvironment()
085: .getAllFonts()[0];
086: }
087: try {
088: w1 = getFontMetrics(f).stringWidth(s1);
089: w2 = getFontMetrics(f).stringWidth(s2);
090: } catch (Exception ex) {
091: }
092:
093: if (w1 > w2) {
094: return w1 + 50;
095: }
096:
097: return w2 + 50;
098: }
099:
100: }
|