Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / SyntaxStyle.java @ 40561

History | View | Annotate | Download (4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.console.jedit;
25
/*
26
 * SyntaxStyle.java - A simple text style class
27
 * Copyright (C) 1999 Slava Pestov
28
 *
29
 * You may use and modify this package for any purpose. Redistribution is
30
 * permitted, in both source and binary form, provided that this notice
31
 * remains intact in all source distributions of this package.
32
 */
33

    
34
import java.awt.Color;
35
import java.awt.Font;
36
import java.awt.FontMetrics;
37
import java.awt.Graphics;
38
import java.awt.Toolkit;
39

    
40
/**
41
 * A simple text style class. It can specify the color, italic flag,
42
 * and bold flag of a run of text.
43
 * @author Slava Pestov
44
 * @version $Id$
45
 */
46
public class SyntaxStyle
47
{
48
        /**
49
         * Creates a new SyntaxStyle.
50
         * @param color The text color
51
         * @param italic True if the text should be italics
52
         * @param bold True if the text should be bold
53
         */
54
        public SyntaxStyle(Color color, boolean italic, boolean bold)
55
        {
56
                this.color = color;
57
                this.italic = italic;
58
                this.bold = bold;
59
        }
60

    
61
        /**
62
         * Returns the color specified in this style.
63
         */
64
        public Color getColor()
65
        {
66
                return color;
67
        }
68

    
69
        /**
70
         * Returns true if no font styles are enabled.
71
         */
72
        public boolean isPlain()
73
        {
74
                return !(bold || italic);
75
        }
76

    
77
        /**
78
         * Returns true if italics is enabled for this style.
79
         */
80
        public boolean isItalic()
81
        {
82
                return italic;
83
        }
84

    
85
        /**
86
         * Returns true if boldface is enabled for this style.
87
         */
88
        public boolean isBold()
89
        {
90
                return bold;
91
        }
92

    
93
        /**
94
         * Returns the specified font, but with the style's bold and
95
         * italic flags applied.
96
         */
97
        public Font getStyledFont(Font font)
98
        {
99
                if(font == null)
100
                        throw new NullPointerException("font param must not"
101
                                + " be null");
102
                if(font.equals(lastFont))
103
                        return lastStyledFont;
104
                lastFont = font;
105
                lastStyledFont = new Font(font.getFamily(),
106
                        (bold ? Font.BOLD : 0)
107
                        | (italic ? Font.ITALIC : 0),
108
                        font.getSize());
109
                return lastStyledFont;
110
        }
111

    
112
        /**
113
         * Returns the font metrics for the styled font.
114
         */
115
        public FontMetrics getFontMetrics(Font font)
116
        {
117
                if(font == null)
118
                        throw new NullPointerException("font param must not"
119
                                + " be null");
120
                if(font.equals(lastFont) && fontMetrics != null)
121
                        return fontMetrics;
122
                lastFont = font;
123
                lastStyledFont = new Font(font.getFamily(),
124
                        (bold ? Font.BOLD : 0)
125
                        | (italic ? Font.ITALIC : 0),
126
                        font.getSize());
127
                fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
128
                        lastStyledFont);
129
                return fontMetrics;
130
        }
131

    
132
        /**
133
         * Sets the foreground color and font of the specified graphics
134
         * context to that specified in this style.
135
         * @param gfx The graphics context
136
         * @param font The font to add the styles to
137
         */
138
        public void setGraphicsFlags(Graphics gfx, Font font)
139
        {
140
                Font _font = getStyledFont(font);
141
                gfx.setFont(_font);
142
                gfx.setColor(color);
143
        }
144

    
145
        /**
146
         * Returns a string representation of this object.
147
         */
148
        public String toString()
149
        {
150
                return getClass().getName() + "[color=" + color +
151
                        (italic ? ",italic" : "") +
152
                        (bold ? ",bold" : "") + "]";
153
        }
154

    
155
        // private members
156
        private Color color;
157
        private boolean italic;
158
        private boolean bold;
159
        private Font lastFont;
160
        private Font lastStyledFont;
161
        private FontMetrics fontMetrics;
162
}