001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import java.awt.*;
024: import java.awt.event.*;
025: import javax.swing.*;
026: import javax.swing.border.*;
027: import java.io.*;
028: import com.l2fprod.common.swing.*;
029: import java.util.*;
030: import net.charabia.jsmoothgen.application.gui.Main;
031:
032: /**
033: *
034: */
035:
036: public class HelpButton extends JLabel {
037: protected JWindow m_helpWindow = new JWindow(Main.MAIN);
038:
039: private final Icon ICON_HELP = new javax.swing.ImageIcon(getClass()
040: .getResource("/icons/stock_help-agent-16.png"));
041:
042: public HelpButton(String helptext) {
043: setText("");
044: setIcon(ICON_HELP);
045: m_helpWindow.getContentPane().setBackground(Color.yellow);
046: m_helpWindow.getContentPane().setLayout(new BorderLayout());
047: JEditorPane jep = new JEditorPane("text/html", wrap(helptext));
048: jep.setBackground(Color.yellow);
049: jep.setEditable(false);
050: m_helpWindow.getContentPane().add(jep, BorderLayout.CENTER);
051: jep
052: .setBorder(javax.swing.BorderFactory
053: .createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED));
054: m_helpWindow.pack();
055:
056: m_helpWindow.addMouseListener(new MouseAdapter() {
057: public void mouseEntered(MouseEvent e) {
058: HelpButton.this .m_helpWindow.dispose();
059: }
060:
061: public void mouseExited(MouseEvent e) {
062: HelpButton.this .m_helpWindow.dispose();
063: }
064:
065: });
066:
067: addMouseListener(new MouseAdapter() {
068: public void mouseEntered(MouseEvent e) {
069: HelpButton.this .requestFocus();
070: HelpButton.this .adjustLocation();
071: m_helpWindow.setVisible(true);
072: }
073:
074: public void mouseExited(MouseEvent e) {
075: m_helpWindow.setVisible(false);
076: }
077: });
078:
079: }
080:
081: public void setVisible(boolean b) {
082: super .setVisible(b);
083: if (b == false)
084: m_helpWindow.setVisible(false);
085: }
086:
087: public String wrap(String str) {
088: StringBuffer sb = new StringBuffer();
089: StringBuffer line = new StringBuffer();
090: StringTokenizer stok = new StringTokenizer(str, " ", true);
091: while (stok.hasMoreElements()) {
092: boolean cut = false;
093: String tok = stok.nextToken();
094: line.append(tok);
095:
096: if (tok.indexOf("<br") >= 0 || tok.indexOf("<BR") >= 0
097: || tok.indexOf("<p>") >= 0
098: || tok.indexOf("<p/>") >= 0
099: || tok.indexOf("<P/>") >= 0
100: || tok.indexOf("<P/>") >= 0) {
101: sb.append(line);
102: line.setLength(0);
103: }
104:
105: if (line.length() > 80) {
106: sb.append(line);
107: sb.append("<br>\n");
108: line.setLength(0);
109: }
110:
111: }
112: sb.append(line);
113: return sb.toString();
114: }
115:
116: public void adjustLocation() {
117: // m_helpWindow.setLocationRelativeTo(this);
118: Point p = this .getLocationOnScreen();
119: p.y += getHeight();
120: Rectangle bounds = GraphicsEnvironment
121: .getLocalGraphicsEnvironment().getMaximumWindowBounds();
122: if ((p.x + m_helpWindow.getWidth()) > bounds.width)
123: p.x = bounds.width - m_helpWindow.getWidth();
124: if (p.x < bounds.x)
125: p.x = bounds.x;
126: m_helpWindow.setLocation(p.x, p.y);
127: }
128:
129: }
|