KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > terminalemulator > Attr


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 /*
24  * "Attr.java"
25  * Attr.java 1.7 01/07/23
26  */

27
28 package org.netbeans.lib.terminalemulator;
29
30 class Attr {
31     // 'f' suffix for oFfset
32
// 'w' suffix for Width
33
// 'm' suffix for mask
34
private final static int BGCOLORf = 0;
35     private final static int BGCOLORw = 5;
36     private final static int BGCOLORm = 0xf;
37     public final static int BGCOLOR = BGCOLORm << BGCOLORf;
38
39     private final static int FGCOLORf = BGCOLORf + BGCOLORw;
40     private final static int FGCOLORw = 5;
41     private final static int FGCOLORm = 0xf;
42     public final static int FGCOLOR = FGCOLORm << FGCOLORf;
43
44     private final static int HIDDENf = FGCOLORf + FGCOLORw;
45     private final static int HIDDENw = 1;
46     public final static int HIDDEN = 0x1 << HIDDENf;
47
48     private final static int REVERSEf = HIDDENf + HIDDENw;
49     private final static int REVERSEw = 1;
50     public final static int REVERSE = 0x1 << REVERSEf;
51
52     private final static int BLINKf = REVERSEf + REVERSEw;
53     private final static int BLINKw = 1;
54     public final static int BLINK = 0x1 << BLINKf;
55
56     private final static int UNDERSCOREf = BLINKf + BLINKw;
57     private final static int UNDERSCOREw = 1;
58     public final static int UNDERSCORE = 0x1 << UNDERSCOREf;
59
60     private final static int BRIGHTf = UNDERSCOREf + UNDERSCOREw;
61     private final static int BRIGHTw = 1;
62     public final static int BRIGHT = 0x1 << BRIGHTf;
63
64     private final static int DIMf = BRIGHTf + BRIGHTw;
65     private final static int DIMw = 1;
66     public final static int DIM = 0x1 << DIMf;
67
68     private final static int ACTIVEf = DIMf + DIMw;
69     public final static int ACTIVE = 0x1 << ACTIVEf;
70
71     // Since an attr value of 0 means render using default attributes
72
// We need a value that signifies that no attribute has been set.
73
// Can't use the highest (sign) bit since Java has no unsigned and
74
// we get complaints from the compiler.
75
public final static int UNSET = 0x40000000;
76
77
78
79     /**
80      * attr = Attr.setBackgroundColor(attr, 7);
81      */

82
83     public static int setBackgroundColor(int attr, int code) {
84     code &= BGCOLORm; // throw all but lowest relevant bits away
85
attr &= ~ BGCOLOR; // 0 out existing bits
86
attr |= code << BGCOLORf;
87     return attr;
88     }
89
90
91     /**
92      * attr = Attr.setForegroundColor(attr, 7);
93      */

94
95     public static int setForegroundColor(int attr, int code) {
96     code &= FGCOLORm; // throw all but lowest relevant bits away
97
attr &= ~ FGCOLOR; // 0 out existing bits
98
attr |= code << FGCOLORf;
99     return attr;
100     }
101
102     /**
103      * Use this to get at the FG color value embedded in an attr.
104      */

105     public static int foregroundColor(int attr) {
106     return (attr >> FGCOLORf) & FGCOLORm;
107     }
108
109     /**
110      * Use this to get at the BG color value embedded in an attr.
111      */

112     public static int backgroundColor(int attr) {
113     return (attr >> BGCOLORf) & BGCOLORm;
114     }
115
116     /*
117      * Read-modify-write utility for setting bitfields in 'attr'.
118      */

119     public static int setAttribute(int attr, int value) {
120     switch (value) {
121         case 0:
122         // Reset all attributes
123
attr = 0;
124         break;
125         case 5:
126         // Attr.BLINK
127
// FALLTHRU
128
case 1:
129         attr &= ~ Attr.DIM;
130         attr |= Attr.BRIGHT;
131         break;
132         case 2:
133         attr &= ~ Attr.BRIGHT;
134         attr |= Attr.DIM;
135         break;
136         case 4:
137         attr |= Attr.UNDERSCORE;
138         break;
139         case 7:
140         attr |= Attr.REVERSE;
141         break;
142         case 8:
143         attr |= Attr.HIDDEN;
144         break;
145
146         case 9:
147         // Term specific
148
attr |= Attr.ACTIVE;
149         break;
150
151         // turn individual attributes off (dtterm specific?)
152
case 25:
153         // blinking off
154
// FALLTHRU
155
case 22:
156         attr &= ~ Attr.DIM;
157         attr &= ~ Attr.BRIGHT;
158         break;
159         case 24:
160         attr &= ~ Attr.UNDERSCORE;
161         break;
162         case 27:
163         attr &= ~ Attr.REVERSE;
164         break;
165         case 28:
166         attr &= ~ Attr.HIDDEN;
167         break;
168
169         case 30:
170         case 31:
171         case 32:
172         case 33:
173         case 34:
174         case 35:
175         case 36:
176         case 37:
177         attr = Attr.setForegroundColor(attr, value-30+1);
178         break;
179
180         case 39:
181         // revert to default (dtterm specific)
182
attr = Attr.setForegroundColor(attr, 0);
183         break;
184
185         case 40:
186         case 41:
187         case 42:
188         case 43:
189         case 44:
190         case 45:
191         case 46:
192         case 47:
193         attr = Attr.setBackgroundColor(attr, value-40+1);
194         break;
195
196         case 49:
197         // revert to default (dtterm specific)
198
attr = Attr.setBackgroundColor(attr, 0);
199         break;
200
201             case 50:
202         case 51:
203         case 52:
204         case 53:
205         case 54:
206         case 55:
207         case 56:
208         case 57:
209                 // custom colors
210
attr = Attr.setForegroundColor(attr, value-50+9);
211         break;
212
213         case 60:
214         case 61:
215         case 62:
216         case 63:
217         case 64:
218         case 65:
219         case 66:
220         case 67:
221         // custom colors
222
attr = Attr.setBackgroundColor(attr, value-60+9);
223         break;
224
225         default:
226         // silently ignore unrecognized attribute
227
break;
228     }
229     return attr;
230     }
231
232     /*
233      * Read-modify-write utility for unsetting bitfields in 'attr'.
234      * Note: this doesn't cover the unsetting operations which
235      * setAttributes does.
236      */

237     public static int unsetAttribute(int attr, int value) {
238     switch (value) {
239         case 0:
240         // Reset all attributes
241
attr = 0;
242         break;
243         case 5:
244         // Attr.BLINK
245
// FALLTHRU
246
case 1:
247         attr &= ~ Attr.BRIGHT;
248         break;
249         case 2:
250         attr &= ~ Attr.DIM;
251         break;
252         case 4:
253         attr &= ~ Attr.UNDERSCORE;
254         break;
255         case 7:
256         attr &= ~ Attr.REVERSE;
257         break;
258         case 8:
259         attr &= ~ Attr.HIDDEN;
260         break;
261         case 9:
262         attr &= ~ Attr.ACTIVE;
263         break;
264
265         case 30:
266         case 31:
267         case 32:
268         case 33:
269         case 34:
270         case 35:
271         case 36:
272         case 37:
273         attr = Attr.setForegroundColor(attr, 0);
274         break;
275
276         case 40:
277         case 41:
278         case 42:
279         case 43:
280         case 44:
281         case 45:
282         case 46:
283         case 47:
284         attr = Attr.setBackgroundColor(attr, 0);
285         break;
286
287             case 50:
288         case 51:
289         case 52:
290         case 53:
291         case 54:
292         case 55:
293         case 56:
294         case 57:
295         // custom colors
296
attr = Attr.setForegroundColor(attr, 0);
297         break;
298
299         case 60:
300         case 61:
301         case 62:
302         case 63:
303         case 64:
304         case 65:
305         case 66:
306         case 67:
307         // custom colors
308
attr = Attr.setBackgroundColor(attr, 0);
309         break;
310                 
311         default:
312         // silently ignore unrecognized attribute
313
break;
314     }
315     return attr;
316     }
317 }
318
Popular Tags