KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output > OutputSettings


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.output;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.io.File JavaDoc;
26 import javax.swing.JTextPane JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28
29 import org.openide.DialogDescriptor;
30 import org.openide.DialogDisplayer;
31 import org.openide.ErrorManager;
32 import org.openide.options.SystemOption;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35
36 /** Settings for output window.
37 *
38 * @author Petr Hamernik
39 */

40 public class OutputSettings extends SystemOption {
41     /** generated Serialized Version UID */
42     static final long serialVersionUID = 5773055866277884154L;
43
44     /** bundle to obtain text information from */
45     private static ResourceBundle JavaDoc bundle;
46
47     /** Property name of the fontSize property */
48     public static final String JavaDoc PROP_FONT_SIZE = "fontSize"; // NOI18N
49
/** Property name of the tabSize property */
50     public static final String JavaDoc PROP_TAB_SIZE = "tabSize"; // NOI18N
51
/** Property name of the foreground property */
52     public static final String JavaDoc PROP_FOREGROUND = "foreground"; // NOI18N
53
/** Property name of the cursorForeground property */
54     public static final String JavaDoc PROP_CURSOR_FOREGROUND = "cursorForeground"; // NOI18N
55
/** Property name of the jumpCursorForeground property */
56     public static final String JavaDoc PROP_JUMP_CURSOR_FOREGROUND = "jumpCursorForeground"; // NOI18N
57
/** Property name of the jumpCursorForeground property */
58     public static final String JavaDoc PROP_JUMP_LINK_FOREGROUND = "jumpLinkForeground"; // NOI18N
59
/** Property name of the background property */
60     public static final String JavaDoc PROP_BACKGROUND = "background"; // NOI18N
61
/** Property name of the cursorBackground property */
62     public static final String JavaDoc PROP_CURSOR_BACKGROUND = "cursorBackground"; // NOI18N
63
/** Property name of the jumpCursorBackground property */
64     public static final String JavaDoc PROP_JUMP_CURSOR_BACKGROUND = "jumpCursorBackground"; // NOI18N
65
/** Property name of the historySize property */
66     public static final String JavaDoc PROP_HISTORY_SIZE = "historySize"; // NOI18N
67
/** Property name of the redirection property */
68     public static final String JavaDoc PROP_REDIRECTION = "redirection"; // NOI18N
69
/** Property name of the redirection location property */
70     public static final String JavaDoc PROP_DIRECTORY = "directory"; // NOI18N
71
/** Property name of the selectionBackground property */
72     public static final String JavaDoc PROP_SELECTION_BACKGROUND = "selectionBackground"; // NOI18N
73

74     private static final String JavaDoc REDIR_FOLDER = "output"; // NOI18N
75

76     private static int fontSize;
77     static {
78         //issue 33875, font size should be based on -fontsize/I10N supplied size
79
java.awt.Font JavaDoc f =
80             javax.swing.UIManager.getFont ("controlFont"); //NOI18N
81
if (f != null) {
82             fontSize = f.getSize();
83         } else {
84             fontSize = 12;
85         }
86     }
87     
88     private static int tabSize = 8;
89     private static int historySize = 10000;
90     private static boolean redirection = false;
91     
92     private static File JavaDoc directory;
93     
94     /** Holds value of property selectionBackground. */
95     private static Color JavaDoc selectionBackground;
96     
97     static {
98         String JavaDoc userDir = System.getProperty ("netbeans.user");
99
100         if (userDir == null)
101             userDir = System.getProperty ("netbeans.home");
102         if (userDir != null) {
103             userDir = new File JavaDoc(userDir).getAbsolutePath();
104
105             directory = new File JavaDoc (userDir, REDIR_FOLDER);
106             directory.mkdirs();
107         } else {
108             // Running in e.g. org.netbeans.core.Plain.
109
// Just pick some random location.
110
directory = new File JavaDoc(System.getProperty("user.dir")); // NOI18N
111
}
112     }
113     
114     public OutputSettings () {
115     }
116
117     public String JavaDoc displayName () {
118         return getString("CTL_Output_settings");
119     }
120
121     public HelpCtx getHelpCtx () {
122         return new HelpCtx (OutputSettings.class);
123     }
124
125     private int sz=-1;
126     public int getFontSize() {
127         if (sz == -1) {
128             Font JavaDoc std = UIManager.getFont ("controlFont"); //NOI18N
129
if (std != null) {
130                 sz = std.getSize();
131             } else {
132                 sz = new JTextPane JavaDoc().getFont().getSize();
133             }
134         }
135         return sz;
136     }
137
138     public boolean isRedirection() {
139         return redirection;
140     }
141
142     public void setRedirection(boolean redirection) {
143         if (this.redirection != redirection) {
144             Boolean JavaDoc old = this.redirection ? Boolean.TRUE : Boolean.FALSE;
145             this.redirection = redirection;
146             firePropertyChange (PROP_REDIRECTION, old, redirection ? Boolean.TRUE : Boolean.FALSE);
147         }
148     }
149     
150     public File JavaDoc getDirectory() {
151         return directory;
152     }
153
154     public void setDirectory(File JavaDoc s) {
155         if (directory != s) {
156             //issue 32911, should not be possible to set redirection to a nonexistent dir
157
File JavaDoc old = directory;
158             directory = s;
159             firePropertyChange (PROP_DIRECTORY, old, directory);
160         }
161     }
162     
163     boolean checkRedirectionDirExists(File JavaDoc s) {
164         if (!s.exists()) {
165             DialogDescriptor dd = new DialogDescriptor (
166                 NbBundle.getMessage (OutputSettings.class,
167                     "FMT_NO_DIR", s), //NOI18N
168
NbBundle.getMessage (OutputSettings.class, "TTL_NO_DIR"),
169                 true, DialogDescriptor.YES_NO_OPTION,
170                 DialogDescriptor.YES_OPTION,
171                 DialogDescriptor.DEFAULT_ALIGN,
172                 HelpCtx.DEFAULT_HELP, null);
173
174             Object JavaDoc retVal = DialogDisplayer.getDefault().notify(dd);
175
176             if (retVal == dd.YES_OPTION) {
177                 try {
178                     if (!s.mkdir()) {
179                     DialogDescriptor dd1 = new DialogDescriptor (
180                         NbBundle.getMessage (OutputSettings.class,
181                             "MSG_CREATE_FAIL"), //NOI18N
182
NbBundle.getMessage (OutputSettings.class,
183                             "TTL_NO_DIR"), //NOI18N
184
true, DialogDescriptor.WARNING_MESSAGE,
185                         DialogDescriptor.YES_OPTION,
186                         DialogDescriptor.DEFAULT_ALIGN,
187                         HelpCtx.DEFAULT_HELP, null);
188
189                         DialogDisplayer.getDefault().notify (dd1);
190                         return false;
191                     }
192                 } catch (Exception JavaDoc e) {
193                     ErrorManager.getDefault().annotate (e,
194                         NbBundle.getMessage (OutputSettings.class,
195                         "MSG_CREATE_FAIL")); //NOI18N
196
ErrorManager.getDefault().notify(ErrorManager.USER,
197                         e);
198                     return false;
199                 }
200             } else {
201                 return false;
202             }
203         }
204         return true;
205     }
206     
207     /** Tab size getter */
208     public int getTabSize() {
209         return tabSize;
210     }
211
212     /** Tab size setter */
213     public void setTabSize(int tabSize) {
214         if (this.tabSize != tabSize) {
215             this.tabSize = tabSize;
216             change();
217         }
218     }
219     
220     /** History size getter */
221     public int getHistorySize() {
222         return historySize;
223     }
224
225     /** History size setter */
226     public void setHistorySize(int historySize) {
227         if (this.historySize != historySize) {
228             this.historySize = historySize;
229             change();
230         }
231     }
232
233     public Color JavaDoc getBaseForeground() {
234         Color JavaDoc result = UIManager.getColor("nb.output.foreground");
235         if (result == null) {
236             result = UIManager.getColor("textText"); //NOI18N
237
}
238         return result == null ? Color.black : result;
239     }
240
241     public Color JavaDoc getCursorForeground() {
242         Color JavaDoc result = UIManager.getColor("text"); //NOI18N
243
return result == null ? Color.white : result;
244     }
245
246     public Color JavaDoc getJumpCursorForeground() {
247         Color JavaDoc result = UIManager.getColor("text"); //NOI18N
248
return result == null ? Color.white : result;
249     }
250     
251     private static Color JavaDoc jumpLinkForeground = null;
252     public Color JavaDoc getJumpLinkForeground() {
253         if (jumpLinkForeground == null) {
254             Color JavaDoc result = UIManager.getColor("nb.hyperlink.foreground"); //NOI18N
255

256             if (result == null) {
257                 Color JavaDoc bg = getBaseBackground();
258                 result = Color.blue;
259                 float[] f = new float[3];
260                 Color.RGBtoHSB(bg.getRed(), bg.getGreen(), bg.getBlue(), f);
261                 if (f[2] < 0.6) {
262                     Color.RGBtoHSB (0, 0, 255, f);
263                     f[1] = Math.min (0f, f[1] - 0.3f);
264                     result = new Color JavaDoc(Color.HSBtoRGB(f[0], f[1], f[2]));
265                 }
266             }
267             jumpLinkForeground = result;
268         }
269         return jumpLinkForeground;
270     }
271     
272     public Color JavaDoc getBaseBackground() {
273         Color JavaDoc result = UIManager.getColor("nb.output.background");
274         if (result == null) {
275             result = UIManager.getColor("control"); // NOI18N
276
}
277         return result == null ? Color.gray : result;
278     }
279     
280     public Color JavaDoc getCursorBackground() {
281         return getJumpLinkForeground();
282     }
283
284     public Color JavaDoc getJumpCursorBackground() {
285         Color JavaDoc result = UIManager.getColor("controlHighlight"); //NOI18N
286
return result == null ? getBaseBackground().brighter() : result;
287     }
288     
289     /** Getter for property selectionBackground.
290      * @return Value of property selectionBackground.
291      */

292     public Color JavaDoc getSelectionBackground() {
293         Color JavaDoc result = UIManager.getColor("nb.output.selectionBackground"); //NOI18N
294
if (result == null) {
295             result = UIManager.getColor("List.selectionBackground"); // NOI18N
296
}
297         if (result == null) {
298             result = new Color JavaDoc(204, 204, 255);
299         }
300         //Hotfix for background color problems on win classic - the
301
//list selection color is dark blue, and black is unreadable against
302
//it. Term does not appear to have a concept of selection *foreground*,
303
//so not sure what better solution there is here.
304
if (brightnessDiff (getBaseForeground(), result) < 96) {
305             if (isBrighterThan (result, getBaseForeground())) {
306                 result = result.darker().darker();
307             } else {
308                 result = findContrastingColor(result, getBaseForeground());
309             }
310         }
311         return result;
312     }
313     
314     private Color JavaDoc findContrastingColor (Color JavaDoc toReplace, Color JavaDoc contrast) {
315         //FIXME - this is far from an intelligent algorithm. Better
316
//would be some heuristic for color perception, since it is possible
317
//to have mathematically dissimilar colors that are nonetheless
318
//unreadable when combined. Still, this does produce a readable
319
//result on all look and feels it was tried on.
320

321         int[] toReplaceRGB = new int[] {
322             toReplace.getRed(),
323             toReplace.getGreen(),
324             toReplace.getBlue()
325         };
326         
327         int[] toContrastRGB = new int[] {
328             contrast.getRed(),
329             contrast.getGreen(),
330             contrast.getBlue()
331         };
332         
333         int[] diffRGB = new int[] {
334             toReplaceRGB[0] - toContrastRGB[0],
335             toReplaceRGB[1] - toContrastRGB[1],
336             toReplaceRGB[2] - toContrastRGB[2]
337         };
338         
339         int[] resultRGB = new int[3];
340         
341         for (int i=0; i < 3; i++) {
342             if (diffRGB[i] == 0) {
343                 diffRGB[i] = 16;
344             }
345             resultRGB[i] = toReplaceRGB[i];
346             resultRGB[i] = Math.max (0, Math.min (255, resultRGB[i] + (4 * diffRGB[i])));
347         }
348         return new Color JavaDoc (resultRGB[0],resultRGB[1],resultRGB[2]);
349     }
350     
351     private int brightnessDiff (Color JavaDoc a, Color JavaDoc b) {
352         int red = Math.abs(a.getRed() - b.getRed());
353         int green = Math.abs(a.getGreen() - b.getGreen());
354         int blue = Math.abs(a.getBlue() - b.getBlue());
355         int avg = (red + green + blue) / 3;
356         return avg;
357     }
358     
359     private boolean isBrighterThan (Color JavaDoc a, Color JavaDoc b) {
360         int red = a.getRed() - b.getRed();
361         int green = a.getGreen() - b.getGreen();
362         int blue = a.getBlue() - b.getBlue();
363         return (red + green + blue) / 3 < 0;
364     }
365
366     //Setters for now hidden properties - retained for serialization
367
//backward compatibility (this class should be rewritten to use
368
//registry anyway once that's public - TDB
369
/** @deprecated - does nothing */
370     public void setBaseForeground(Color JavaDoc c) {}
371     /** @deprecated - does nothing */
372     public void setCursorForeground(Color JavaDoc c) {}
373     /** @deprecated - does nothing */
374     public void setJumpCursorForeground(Color JavaDoc c) {}
375     /** @deprecated - does nothing */
376     public void setJumpLinkForeground(Color JavaDoc c) {}
377     /** @deprecated - does nothing */
378     public void setBaseBackground(Color JavaDoc c) { }
379     /** @deprecated - does nothing */
380     public void setCursorBackground(Color JavaDoc c) {}
381     /** @deprecated - does nothing */
382     public void setJumpCursorBackground(Color JavaDoc c) {}
383     /** @deprecated - does nothing */
384     public void setSelectionBackground(Color JavaDoc selectionBackground) {}
385     /** @deprecated - does nothing */
386     public void setFontSize(int fontSize) { }
387     
388     
389     private void change() {
390         firePropertyChange (null, null, null);
391     }
392
393     /** @return localized string */
394     static String JavaDoc getString(String JavaDoc s) {
395         if (bundle == null) {
396             bundle = NbBundle.getBundle(OutputSettings.class);
397         }
398         return bundle.getString(s);
399     }
400     
401 }
402
Popular Tags