KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > RenderingHintsManager


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer;
28
29 import java.awt.RenderingHints JavaDoc;
30
31 public class RenderingHintsManager
32 {
33     public static final int RENDER_MODE_DEFAULT = 0;
34     public static final int RENDER_MODE_QUALITY = 1;
35     public static final int RENDER_MODE_SPEED = 2;
36 // public static final int RENDER_MODE_CUSTOM = 4;
37

38     protected static RenderingHintsManager _sharedInstance = null;
39     public static RenderingHintsManager sharedInstance()
40     {
41         if (_sharedInstance == null)
42             _sharedInstance = new RenderingHintsManager();
43         
44         return _sharedInstance;
45     }
46     
47     protected RenderingHintsManager() {
48         super();
49     }
50
51 // public RenderingHintsManager()
52
// {
53
// super();
54
// setDefaultRenderMode(getRenderingHints());
55
// }
56

57     protected RenderingHints JavaDoc renderingHints =
58         new RenderingHints JavaDoc(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);
59     
60     public RenderingHints JavaDoc getRenderingHints() {
61         return renderingHints;
62     }
63             
64     protected boolean checkAlphaInterpolation(Object JavaDoc alpha)
65     {
66         if ( !(alpha.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT)) ||
67                  !(alpha.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)) ||
68                  !(alpha.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED)) )
69         {
70             return false;
71         }
72         return true;
73     }
74     
75     public Object JavaDoc getAlphaInterpolation() {
76         return renderingHints.get(RenderingHints.KEY_ALPHA_INTERPOLATION);
77     }
78     
79     public void setAlphaInterpolation(Object JavaDoc alphaInterpolation)
80     {
81         if (checkAlphaInterpolation(alphaInterpolation))
82             renderingHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, alphaInterpolation);
83     }
84     
85     protected boolean checkAntiAliasing(Object JavaDoc antiAlias)
86     {
87         if ( !(antiAlias.equals(RenderingHints.VALUE_ANTIALIAS_DEFAULT)) ||
88                  !(antiAlias.equals(RenderingHints.VALUE_ANTIALIAS_OFF)) ||
89                  !(antiAlias.equals(RenderingHints.VALUE_ANTIALIAS_ON)) )
90         {
91             return false;
92         }
93         return true;
94     }
95
96     public Object JavaDoc getAntiAliasing() {
97         return renderingHints.get(RenderingHints.KEY_ANTIALIASING);
98     }
99     public void setAntiAliasing(Object JavaDoc antiAliasing) {
100         if (checkAntiAliasing(antiAliasing))
101             renderingHints.put(RenderingHints.KEY_ANTIALIASING, antiAliasing);
102     }
103     
104     protected boolean checkColorRendering(Object JavaDoc colorRendering)
105     {
106         if ( !(colorRendering.equals(RenderingHints.VALUE_COLOR_RENDER_DEFAULT)) ||
107                  !(colorRendering.equals(RenderingHints.VALUE_COLOR_RENDER_QUALITY)) ||
108                  !(colorRendering.equals(RenderingHints.VALUE_COLOR_RENDER_SPEED)) )
109         {
110             return false;
111         }
112         return true;
113     }
114     
115     public Object JavaDoc getColorRendering() {
116         return renderingHints.get(RenderingHints.KEY_COLOR_RENDERING);
117     }
118     public void setColorRendering(Object JavaDoc colorRendering) {
119         if (checkColorRendering(colorRendering))
120             renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, colorRendering);
121     }
122     
123     protected boolean checkDithering(Object JavaDoc dithering)
124     {
125         if ( !(dithering.equals(RenderingHints.VALUE_DITHER_DEFAULT)) ||
126                  !(dithering.equals(RenderingHints.VALUE_DITHER_DISABLE)) ||
127                  !(dithering.equals(RenderingHints.VALUE_DITHER_ENABLE)) )
128         {
129             return false;
130         }
131         return true;
132     }
133     
134     public Object JavaDoc getDithering() {
135         return renderingHints.get(RenderingHints.KEY_DITHERING);
136     }
137     public void setDithering(Object JavaDoc dithering) {
138         if (checkDithering(dithering))
139             renderingHints.put(RenderingHints.KEY_DITHERING, dithering);
140     }
141     
142     protected boolean checkInterpolation(Object JavaDoc interpolation)
143     {
144         if ( !(interpolation.equals(RenderingHints.VALUE_INTERPOLATION_BICUBIC)) ||
145                  !(interpolation.equals(RenderingHints.VALUE_INTERPOLATION_BILINEAR)) ||
146                  !(interpolation.equals(RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)) )
147         {
148             return false;
149         }
150         return true;
151     }
152     
153     public Object JavaDoc getInterpolation() {
154         return renderingHints.get(RenderingHints.KEY_INTERPOLATION);
155     }
156     public void setInterpolation(Object JavaDoc interpolation)
157     {
158         if (checkInterpolation(interpolation))
159             renderingHints.put(RenderingHints.KEY_INTERPOLATION, interpolation);
160     }
161     
162     protected boolean checkRendering(Object JavaDoc rendering)
163     {
164         if ( !(rendering.equals(RenderingHints.VALUE_RENDER_DEFAULT)) ||
165                  !(rendering.equals(RenderingHints.VALUE_RENDER_QUALITY)) ||
166                  !(rendering.equals(RenderingHints.VALUE_RENDER_SPEED)) )
167         {
168             return false;
169         }
170         return true;
171     }
172
173     public Object JavaDoc getRendering() {
174         return renderingHints.get(RenderingHints.KEY_RENDERING);
175     }
176     public void setRendering(Object JavaDoc rendering) {
177         if (checkRendering(rendering))
178             renderingHints.put(RenderingHints.KEY_RENDERING, rendering);
179     }
180     
181     protected boolean checkStrokeControl(Object JavaDoc stroke)
182     {
183         if ( !(stroke.equals(RenderingHints.VALUE_STROKE_DEFAULT)) ||
184                  !(stroke.equals(RenderingHints.VALUE_STROKE_NORMALIZE)) ||
185                  !(stroke.equals(RenderingHints.VALUE_STROKE_PURE)) )
186         {
187             return false;
188         }
189         return true;
190     }
191  
192     public Object JavaDoc getStrokeControl() {
193         return renderingHints.get(RenderingHints.KEY_STROKE_CONTROL);
194     }
195     public void setStrokeControl(Object JavaDoc strokeControl) {
196         if (checkStrokeControl(strokeControl))
197             renderingHints.put(RenderingHints.KEY_STROKE_CONTROL, strokeControl);
198     }
199     
200     protected boolean checkTextAntiAliasing(Object JavaDoc textAntiAliasing)
201     {
202         if ( !(textAntiAliasing.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) ||
203                  !(textAntiAliasing.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) ||
204                  !(textAntiAliasing.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) )
205         {
206             return false;
207         }
208         return true;
209     }
210     
211     public Object JavaDoc getTextAntiAliasing() {
212         return renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
213     }
214     public void setTextAntiAliasing(Object JavaDoc textAntiAliasing) {
215         if (checkTextAntiAliasing(textAntiAliasing))
216             renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, textAntiAliasing);
217     }
218     
219     public void setRenderMode(int renderMode)
220     {
221         switch (renderMode)
222         {
223             case(RENDER_MODE_DEFAULT):
224                 setDefaultRenderMode(getRenderingHints());
225                 break;
226             case(RENDER_MODE_QUALITY):
227                 setQualityRenderMode(getRenderingHints());
228                 break;
229             case(RENDER_MODE_SPEED):
230                 setSpeedRenderMode(getRenderingHints());
231                 break;
232         }
233     }
234         
235     public static void setDefaultRenderMode(RenderingHints JavaDoc rh)
236     {
237         rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);
238         rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
239         rh.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_DEFAULT);
240         rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DEFAULT);
241         rh.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT);
242         rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
243         rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);
244         rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
245         rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
246     }
247     
248     public static void setQualityRenderMode(RenderingHints JavaDoc rh)
249     {
250         rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
251         rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
252         rh.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
253         rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
254         rh.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
255         rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
256         rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
257         rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
258         rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
259     }
260     
261     public static void setSpeedRenderMode(RenderingHints JavaDoc rh)
262     {
263         rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
264         rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
265         rh.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
266         rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
267         rh.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
268         rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
269         rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
270         rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
271         rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
272     }
273     
274 }
275
Popular Tags