001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Container;
021: import java.awt.Font;
022:
023: import javax.swing.BorderFactory;
024: import javax.swing.JComponent;
025: import javax.swing.JEditorPane;
026: import javax.swing.JPanel;
027: import javax.swing.JTextArea;
028: import javax.swing.UIManager;
029: import javax.swing.border.Border;
030: import javax.swing.border.CompoundBorder;
031: import javax.swing.text.JTextComponent;
032: import javax.swing.text.View;
033: import javax.swing.text.html.HTMLDocument;
034:
035: /**
036: * LookAndFeelTweaks. <br>
037: *
038: */
039: public class LookAndFeelTweaks {
040:
041: public final static Border PANEL_BORDER = BorderFactory
042: .createEmptyBorder(3, 3, 3, 3);
043:
044: public final static Border WINDOW_BORDER = BorderFactory
045: .createEmptyBorder(4, 10, 10, 10);
046:
047: public final static Border EMPTY_BORDER = BorderFactory
048: .createEmptyBorder();
049:
050: public static void tweak() {
051: Object listFont = UIManager.get("List.font");
052: UIManager.put("Table.font", listFont);
053: UIManager.put("ToolTip.font", listFont);
054: UIManager.put("TextField.font", listFont);
055: UIManager.put("FormattedTextField.font", listFont);
056: UIManager.put("Viewport.background", "Table.background");
057: }
058:
059: public static PercentLayout createVerticalPercentLayout() {
060: return new PercentLayout(PercentLayout.VERTICAL, 8);
061: }
062:
063: public static PercentLayout createHorizontalPercentLayout() {
064: return new PercentLayout(PercentLayout.HORIZONTAL, 8);
065: }
066:
067: public static ButtonAreaLayout createButtonAreaLayout() {
068: return new ButtonAreaLayout(6);
069: }
070:
071: public static BorderLayout createBorderLayout() {
072: return new BorderLayout(8, 8);
073: }
074:
075: public static void setBorder(JComponent component) {
076: if (component instanceof JPanel) {
077: component.setBorder(PANEL_BORDER);
078: }
079: }
080:
081: public static void setBorderLayout(Container container) {
082: container.setLayout(new BorderLayout(3, 3));
083: }
084:
085: public static void makeBold(JComponent component) {
086: component.setFont(component.getFont().deriveFont(Font.BOLD));
087: }
088:
089: public static void makeMultilineLabel(JTextComponent area) {
090: area.setFont(UIManager.getFont("Label.font"));
091: area.setEditable(false);
092: area.setOpaque(false);
093: if (area instanceof JTextArea) {
094: ((JTextArea) area).setWrapStyleWord(true);
095: ((JTextArea) area).setLineWrap(true);
096: }
097: }
098:
099: public static void htmlize(JComponent component) {
100: htmlize(component, UIManager.getFont("Button.font"));
101: }
102:
103: public static void htmlize(JComponent component, Font font) {
104: String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
105: + font.getName()
106: + "; font-size: "
107: + font.getSize()
108: + "pt; }"
109: + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
110: + font.getName()
111: + "; font-size: "
112: + font.getSize()
113: + "pt; }";
114:
115: try {
116: HTMLDocument doc = null;
117: if (component instanceof JEditorPane) {
118: if (((JEditorPane) component).getDocument() instanceof HTMLDocument) {
119: doc = (HTMLDocument) ((JEditorPane) component)
120: .getDocument();
121: }
122: } else {
123: View v = (View) component
124: .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
125: if (v != null
126: && v.getDocument() instanceof HTMLDocument) {
127: doc = (HTMLDocument) v.getDocument();
128: }
129: }
130: if (doc != null) {
131: doc.getStyleSheet().loadRules(
132: new java.io.StringReader(stylesheet), null);
133: } // end of if (doc != null)
134: } catch (Exception e) {
135: e.printStackTrace();
136: }
137: }
138:
139: public static Border addMargin(Border border) {
140: return new CompoundBorder(border, PANEL_BORDER);
141: }
142:
143: }
|