1 19 20 package org.netbeans.core.output; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.util.ResourceBundle ; 25 import java.io.File ; 26 import javax.swing.JTextPane ; 27 import javax.swing.UIManager ; 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 40 public class OutputSettings extends SystemOption { 41 42 static final long serialVersionUID = 5773055866277884154L; 43 44 45 private static ResourceBundle bundle; 46 47 48 public static final String PROP_FONT_SIZE = "fontSize"; 50 public static final String PROP_TAB_SIZE = "tabSize"; 52 public static final String PROP_FOREGROUND = "foreground"; 54 public static final String PROP_CURSOR_FOREGROUND = "cursorForeground"; 56 public static final String PROP_JUMP_CURSOR_FOREGROUND = "jumpCursorForeground"; 58 public static final String PROP_JUMP_LINK_FOREGROUND = "jumpLinkForeground"; 60 public static final String PROP_BACKGROUND = "background"; 62 public static final String PROP_CURSOR_BACKGROUND = "cursorBackground"; 64 public static final String PROP_JUMP_CURSOR_BACKGROUND = "jumpCursorBackground"; 66 public static final String PROP_HISTORY_SIZE = "historySize"; 68 public static final String PROP_REDIRECTION = "redirection"; 70 public static final String PROP_DIRECTORY = "directory"; 72 public static final String PROP_SELECTION_BACKGROUND = "selectionBackground"; 74 private static final String REDIR_FOLDER = "output"; 76 private static int fontSize; 77 static { 78 java.awt.Font f = 80 javax.swing.UIManager.getFont ("controlFont"); 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 directory; 93 94 95 private static Color selectionBackground; 96 97 static { 98 String userDir = System.getProperty ("netbeans.user"); 99 100 if (userDir == null) 101 userDir = System.getProperty ("netbeans.home"); 102 if (userDir != null) { 103 userDir = new File (userDir).getAbsolutePath(); 104 105 directory = new File (userDir, REDIR_FOLDER); 106 directory.mkdirs(); 107 } else { 108 directory = new File (System.getProperty("user.dir")); } 112 } 113 114 public OutputSettings () { 115 } 116 117 public String 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 std = UIManager.getFont ("controlFont"); if (std != null) { 130 sz = std.getSize(); 131 } else { 132 sz = new JTextPane ().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 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 getDirectory() { 151 return directory; 152 } 153 154 public void setDirectory(File s) { 155 if (directory != s) { 156 File old = directory; 158 directory = s; 159 firePropertyChange (PROP_DIRECTORY, old, directory); 160 } 161 } 162 163 boolean checkRedirectionDirExists(File s) { 164 if (!s.exists()) { 165 DialogDescriptor dd = new DialogDescriptor ( 166 NbBundle.getMessage (OutputSettings.class, 167 "FMT_NO_DIR", s), 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 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"), NbBundle.getMessage (OutputSettings.class, 183 "TTL_NO_DIR"), 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 e) { 193 ErrorManager.getDefault().annotate (e, 194 NbBundle.getMessage (OutputSettings.class, 195 "MSG_CREATE_FAIL")); ErrorManager.getDefault().notify(ErrorManager.USER, 197 e); 198 return false; 199 } 200 } else { 201 return false; 202 } 203 } 204 return true; 205 } 206 207 208 public int getTabSize() { 209 return tabSize; 210 } 211 212 213 public void setTabSize(int tabSize) { 214 if (this.tabSize != tabSize) { 215 this.tabSize = tabSize; 216 change(); 217 } 218 } 219 220 221 public int getHistorySize() { 222 return historySize; 223 } 224 225 226 public void setHistorySize(int historySize) { 227 if (this.historySize != historySize) { 228 this.historySize = historySize; 229 change(); 230 } 231 } 232 233 public Color getBaseForeground() { 234 Color result = UIManager.getColor("nb.output.foreground"); 235 if (result == null) { 236 result = UIManager.getColor("textText"); } 238 return result == null ? Color.black : result; 239 } 240 241 public Color getCursorForeground() { 242 Color result = UIManager.getColor("text"); return result == null ? Color.white : result; 244 } 245 246 public Color getJumpCursorForeground() { 247 Color result = UIManager.getColor("text"); return result == null ? Color.white : result; 249 } 250 251 private static Color jumpLinkForeground = null; 252 public Color getJumpLinkForeground() { 253 if (jumpLinkForeground == null) { 254 Color result = UIManager.getColor("nb.hyperlink.foreground"); 256 if (result == null) { 257 Color 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 (Color.HSBtoRGB(f[0], f[1], f[2])); 265 } 266 } 267 jumpLinkForeground = result; 268 } 269 return jumpLinkForeground; 270 } 271 272 public Color getBaseBackground() { 273 Color result = UIManager.getColor("nb.output.background"); 274 if (result == null) { 275 result = UIManager.getColor("control"); } 277 return result == null ? Color.gray : result; 278 } 279 280 public Color getCursorBackground() { 281 return getJumpLinkForeground(); 282 } 283 284 public Color getJumpCursorBackground() { 285 Color result = UIManager.getColor("controlHighlight"); return result == null ? getBaseBackground().brighter() : result; 287 } 288 289 292 public Color getSelectionBackground() { 293 Color result = UIManager.getColor("nb.output.selectionBackground"); if (result == null) { 295 result = UIManager.getColor("List.selectionBackground"); } 297 if (result == null) { 298 result = new Color (204, 204, 255); 299 } 300 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 findContrastingColor (Color toReplace, Color contrast) { 315 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 (resultRGB[0],resultRGB[1],resultRGB[2]); 349 } 350 351 private int brightnessDiff (Color a, Color 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 a, Color 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 370 public void setBaseForeground(Color c) {} 371 372 public void setCursorForeground(Color c) {} 373 374 public void setJumpCursorForeground(Color c) {} 375 376 public void setJumpLinkForeground(Color c) {} 377 378 public void setBaseBackground(Color c) { } 379 380 public void setCursorBackground(Color c) {} 381 382 public void setJumpCursorBackground(Color c) {} 383 384 public void setSelectionBackground(Color selectionBackground) {} 385 386 public void setFontSize(int fontSize) { } 387 388 389 private void change() { 390 firePropertyChange (null, null, null); 391 } 392 393 394 static String getString(String s) { 395 if (bundle == null) { 396 bundle = NbBundle.getBundle(OutputSettings.class); 397 } 398 return bundle.getString(s); 399 } 400 401 } 402 | Popular Tags |