1 19 package org.openide.text; 20 21 import org.openide.options.ContextSystemOption; 22 import org.openide.util.HelpCtx; 23 import org.openide.util.NbBundle; 24 25 import java.awt.Font ; 26 import java.awt.print.PageFormat ; 27 import java.awt.print.Paper ; 28 import java.awt.print.PrinterJob ; 29 30 import java.io.IOException ; 31 import java.io.ObjectInput ; 32 import java.io.ObjectOutput ; 33 34 38 public final class PrintSettings extends ContextSystemOption { 39 41 42 static final long serialVersionUID = -9102470021814206818L; 43 44 45 public static final int CENTER = 0x1; 46 47 48 public static final int RIGHT = 0x2; 49 50 51 public static final int LEFT = 0x0; 52 53 54 public static final String PROP_PAGE_FORMAT = "pageFormat"; 56 57 public static final String PROP_WRAP = "wrap"; 59 60 public static final String PROP_HEADER_FORMAT = "headerFormat"; 62 63 public static final String PROP_FOOTER_FORMAT = "footerFormat"; 65 66 public static final String PROP_HEADER_FONT = "headerFont"; 68 69 public static final String PROP_FOOTER_FONT = "footerFont"; 71 72 public static final String PROP_HEADER_ALIGNMENT = "headerAlignment"; 74 75 public static final String PROP_FOOTER_ALIGNMENT = "footerAlignment"; 77 78 public static final String PROP_LINE_ASCENT_CORRECTION = "lineAscentCorrection"; private static final String HELP_ID = "editing.printing"; 81 82 private static PageFormat pageFormat; 83 84 85 private static boolean wrap = true; 86 87 88 private static String headerFormat; 89 90 91 private static String footerFormat; 92 93 94 private static Font headerFont; 95 96 97 private static Font footerFont; 98 99 100 private static int headerAlignment = CENTER; 101 102 103 private static int footerAlignment = CENTER; 104 105 106 private static float lineAscentCorrection = 1.0f; 107 108 112 public void writeExternal(ObjectOutput obtos) throws IOException { 113 super.writeExternal(obtos); 114 obtos.writeBoolean(wrap); 115 obtos.writeObject(headerFormat); 116 obtos.writeObject(footerFormat); 117 obtos.writeObject(headerFont); 118 obtos.writeObject(footerFont); 119 obtos.writeInt(headerAlignment); 120 obtos.writeInt(footerAlignment); 121 externalizePageFormat(pageFormat, obtos); 122 } 123 124 129 public void readExternal(ObjectInput obtis) throws IOException , ClassNotFoundException { 130 super.readExternal(obtis); 131 wrap = obtis.readBoolean(); 132 headerFormat = (String ) obtis.readObject(); 133 footerFormat = (String ) obtis.readObject(); 134 headerFont = (Font ) obtis.readObject(); 135 footerFont = (Font ) obtis.readObject(); 136 headerAlignment = obtis.readInt(); 137 footerAlignment = obtis.readInt(); 138 pageFormat = internalizePageFormat(obtis); 139 } 140 141 145 private static void externalizePageFormat(PageFormat pf, ObjectOutput obtos) 146 throws IOException { 147 if (pf == null) { 148 obtos.writeInt(PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT); 149 150 return; 151 } 152 153 obtos.writeInt(pf.getOrientation()); 154 155 Paper paper = pf.getPaper(); 156 157 obtos.writeDouble(paper.getWidth()); 159 obtos.writeDouble(paper.getHeight()); 160 161 obtos.writeDouble(paper.getImageableX()); 163 obtos.writeDouble(paper.getImageableY()); 164 obtos.writeDouble(paper.getImageableWidth()); 165 obtos.writeDouble(paper.getImageableHeight()); 166 } 167 168 172 private static PageFormat internalizePageFormat(ObjectInput obtis) 173 throws IOException , ClassNotFoundException { 174 PageFormat pf = new PageFormat (); 175 Paper paper = pf.getPaper(); 176 int etc = obtis.readInt(); 177 178 if (etc == (PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT)) { 179 return null; 180 } 181 182 pf.setOrientation(etc); 183 184 paper.setSize(obtis.readDouble(), obtis.readDouble()); 186 187 paper.setImageableArea(obtis.readDouble(), obtis.readDouble(), obtis.readDouble(), obtis.readDouble()); 189 pf.setPaper(paper); 190 191 return pf; 192 } 193 194 public String displayName() { 195 return NbBundle.getMessage(PrintSettings.class, "CTL_Print_settings"); 196 } 197 198 public HelpCtx getHelpCtx() { 199 return new HelpCtx(HELP_ID); 200 } 201 202 207 public static PageFormat getPageFormat(PrinterJob pj) { 208 if (pageFormat == null) { 209 pageFormat = pj.defaultPage(); 210 } 211 212 return pageFormat; 213 } 214 215 216 @Deprecated 217 public PageFormat getPageFormat() { 218 if (pageFormat == null) { 219 PrinterJob pj = PrinterJob.getPrinterJob(); 220 pageFormat = pj.defaultPage(new PageFormat ()); 221 pj.cancel(); 222 } 223 224 return pageFormat; 225 } 226 227 228 public void setPageFormat(PageFormat pf) { 229 if (pf == null) { 230 return; 231 } 232 233 if (pf.equals(pageFormat)) { 234 return; 235 } 236 237 PageFormat old = pageFormat; 238 pageFormat = pf; 239 firePropertyChange(PROP_PAGE_FORMAT, old, pageFormat); 240 } 241 242 public boolean getWrap() { 243 return wrap; 244 } 245 246 public void setWrap(boolean b) { 247 if (wrap == b) { 248 return; 249 } 250 251 wrap = b; 252 firePropertyChange(PROP_WRAP, (b ? Boolean.FALSE : Boolean.TRUE), (b ? Boolean.TRUE : Boolean.FALSE)); 253 } 254 255 public String getHeaderFormat() { 256 if (headerFormat == null) { 257 headerFormat = NbBundle.getMessage(PrintSettings.class, "CTL_Header_format"); 258 } 259 260 return headerFormat; 261 } 262 263 public void setHeaderFormat(String s) { 264 if (s == null) { 265 return; 266 } 267 268 if (s.equals(headerFormat)) { 269 return; 270 } 271 272 String of = headerFormat; 273 headerFormat = s; 274 firePropertyChange(PROP_HEADER_FORMAT, of, headerFormat); 275 } 276 277 public String getFooterFormat() { 278 if (footerFormat == null) { 279 footerFormat = NbBundle.getMessage(PrintSettings.class, "CTL_Footer_format"); 280 } 281 282 return footerFormat; 283 } 284 285 public void setFooterFormat(String s) { 286 if (s == null) { 287 return; 288 } 289 290 if (s.equals(footerFormat)) { 291 return; 292 } 293 294 String of = footerFormat; 295 footerFormat = s; 296 firePropertyChange(PROP_FOOTER_FORMAT, of, footerFormat); 297 } 298 299 public Font getHeaderFont() { 300 if (headerFont == null) { 301 headerFont = new Font ("Monospaced", java.awt.Font.PLAIN, 6); } 303 304 return headerFont; 305 } 306 307 public void setHeaderFont(Font f) { 308 if (f == null) { 309 return; 310 } 311 312 if (f.equals(headerFont)) { 313 return; 314 } 315 316 Font old = headerFont; 317 headerFont = f; 318 firePropertyChange(PROP_HEADER_FONT, old, headerFont); 319 } 320 321 public Font getFooterFont() { 322 if (footerFont == null) { 323 footerFont = getHeaderFont(); 324 } 325 326 return footerFont; 327 } 328 329 public void setFooterFont(Font f) { 330 if (f == null) { 331 return; 332 } 333 334 if (f.equals(footerFont)) { 335 return; 336 } 337 338 Font old = headerFont; 339 footerFont = f; 340 firePropertyChange(PROP_FOOTER_FONT, old, footerFont); 341 } 342 343 public int getHeaderAlignment() { 344 return headerAlignment; 345 } 346 347 public void setHeaderAlignment(int alignment) { 348 if (alignment == headerAlignment) { 349 return; 350 } 351 352 if ((alignment != LEFT) && (alignment != CENTER) && (alignment != RIGHT)) { 353 throw new IllegalArgumentException (); 354 } 355 356 int old = headerAlignment; 357 headerAlignment = alignment; 358 firePropertyChange(PROP_HEADER_ALIGNMENT, new Integer (old), new Integer (headerAlignment)); 359 } 360 361 public int getFooterAlignment() { 362 return footerAlignment; 363 } 364 365 public void setFooterAlignment(int alignment) { 366 if (alignment == footerAlignment) { 367 return; 368 } 369 370 if ((alignment != LEFT) && (alignment != CENTER) && (alignment != RIGHT)) { 371 throw new IllegalArgumentException (); 372 } 373 374 int old = footerAlignment; 375 footerAlignment = alignment; 376 firePropertyChange(PROP_FOOTER_ALIGNMENT, new Integer (old), new Integer (footerAlignment)); 377 } 378 379 380 public float getLineAscentCorrection() { 381 return lineAscentCorrection; 382 } 383 384 388 public void setLineAscentCorrection(float correction) { 389 if (correction == lineAscentCorrection) { 390 return; 391 } else if (correction < 0) { 392 throw new IllegalArgumentException (); 393 } 394 395 float old = lineAscentCorrection; 396 lineAscentCorrection = correction; 397 firePropertyChange(PROP_LINE_ASCENT_CORRECTION, new Float (old), new Float (lineAscentCorrection)); 398 } 399 400 401 public static class AlignmentEditor extends java.beans.PropertyEditorSupport { 402 private String sCENTER; 403 private String sRIGHT; 404 private String sLEFT; 405 private String [] tags = new String [] { 406 sLEFT = NbBundle.getMessage(PrintSettings.class, "CTL_LEFT"), 407 sCENTER = NbBundle.getMessage(PrintSettings.class, "CTL_CENTER"), 408 sRIGHT = NbBundle.getMessage(PrintSettings.class, "CTL_RIGHT") 409 }; 410 411 public String [] getTags() { 412 return tags; 413 } 414 415 public String getAsText() { 416 return tags[((Integer ) getValue()).intValue()]; 417 } 418 419 public void setAsText(String s) { 420 if (s.equals(sLEFT)) { 421 setValue(new Integer (0)); 422 } else if (s.equals(sCENTER)) { 423 setValue(new Integer (1)); 424 } else { 425 setValue(new Integer (2)); 426 } 427 } 428 } 429 430 431 public static class PageFormatEditor extends java.beans.PropertyEditorSupport { 432 433 public String getAsText() { 434 return null; 435 } 436 437 438 public boolean supportsCustomEditor() { 439 return true; 440 } 441 442 445 public java.awt.Component getCustomEditor() { 446 PageFormat pf = (PageFormat ) getValue(); 447 PrinterJob pj = PrinterJob.getPrinterJob(); 448 PageFormat npf = pj.pageDialog(pf); 449 450 PrintSettings.findObject(PrintSettings.class).setPageFormat((PageFormat ) npf.clone()); 452 pj.cancel(); 453 454 return null; 455 } 456 } 457 } 458 | Popular Tags |