001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.swing;
020:
021: import java.awt.*;
022:
023: import javax.swing.*;
024: import javax.swing.plaf.*;
025: import javax.swing.plaf.basic.*;
026:
027: /**
028: *
029: * @author Matthew Large
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class JMultiLineToolTip extends JToolTip {
034: private static final String uiClassID = "ToolTipUI";
035:
036: String tipText;
037: JComponent component;
038:
039: public JMultiLineToolTip() {
040: updateUI();
041: }
042:
043: public void updateUI() {
044: setUI(MultiLineToolTipUI.createUI(this ));
045: }
046:
047: public void setColumns(int columns) {
048: this .columns = columns;
049: this .fixedwidth = 0;
050: }
051:
052: public int getColumns() {
053: return columns;
054: }
055:
056: public void setFixedWidth(int width) {
057: this .fixedwidth = width;
058: this .columns = 0;
059: }
060:
061: public int getFixedWidth() {
062: return fixedwidth;
063: }
064:
065: protected int columns = 0;
066: protected int fixedwidth = 0;
067: }
068:
069: class MultiLineToolTipUI extends BasicToolTipUI {
070: static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
071: Font smallFont;
072: static JToolTip tip;
073: protected CellRendererPane rendererPane;
074:
075: private static JTextArea textArea;
076:
077: public static ComponentUI createUI(JComponent c) {
078: return sharedInstance;
079: }
080:
081: public MultiLineToolTipUI() {
082: super ();
083: }
084:
085: public void installUI(JComponent c) {
086: super .installUI(c);
087: tip = (JToolTip) c;
088: rendererPane = new CellRendererPane();
089: c.add(rendererPane);
090: }
091:
092: public void uninstallUI(JComponent c) {
093: super .uninstallUI(c);
094:
095: c.remove(rendererPane);
096: rendererPane = null;
097: }
098:
099: public void paint(Graphics g, JComponent c) {
100: Dimension size = c.getSize();
101: textArea.setBackground(c.getBackground());
102: rendererPane.paintComponent(g, textArea, c, 1, 1,
103: size.width - 1, size.height - 1, true);
104: }
105:
106: public Dimension getPreferredSize(JComponent c) {
107: String tipText = ((JToolTip) c).getTipText();
108: if (tipText == null)
109: return new Dimension(0, 0);
110: textArea = new JTextArea(tipText);
111: rendererPane.removeAll();
112: rendererPane.add(textArea);
113: textArea.setWrapStyleWord(true);
114: int width = ((JMultiLineToolTip) c).getFixedWidth();
115: int columns = ((JMultiLineToolTip) c).getColumns();
116:
117: if (columns > 0) {
118: textArea.setColumns(columns);
119: textArea.setSize(0, 0);
120: textArea.setLineWrap(true);
121: textArea.setSize(textArea.getPreferredSize());
122: } else if (width > 0) {
123: textArea.setLineWrap(true);
124: Dimension d = textArea.getPreferredSize();
125: d.width = width;
126: d.height++;
127: textArea.setSize(d);
128: } else
129: textArea.setLineWrap(false);
130:
131: Dimension dim = textArea.getPreferredSize();
132:
133: dim.height += 1;
134: dim.width += 1;
135: return dim;
136: }
137:
138: public Dimension getMinimumSize(JComponent c) {
139: return getPreferredSize(c);
140: }
141:
142: public Dimension getMaximumSize(JComponent c) {
143: return getPreferredSize(c);
144: }
145: }
|