Changeset 29848 in osm for applications/editors/josm/plugins/print/src/org/openstreetmap
- Timestamp:
- 2013-08-20T03:54:05+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintAction.java
r29529 r29848 48 48 Shortcut.registerShortcut("print:print", tr("File: {0}", tr("Print...")), 49 49 KeyEvent.VK_P, Shortcut.CTRL), 50 true, "print/print", true); 50 true, "print/print", true); 51 51 } 52 52 -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintDialog.java
r29846 r29848 37 37 import java.awt.print.PrinterException; 38 38 import java.awt.print.PrinterJob; 39 import java.lang.reflect.Field; 40 import java.lang.reflect.InvocationTargetException; 41 import java.lang.reflect.Modifier; 39 42 import java.text.ParseException; 43 import java.util.ArrayList; 44 import java.util.Arrays; 45 import java.util.Collection; 46 import java.util.Iterator; 47 import java.util.Locale; 40 48 41 49 import javax.print.PrintService; 42 50 import javax.print.PrintServiceLookup; 43 51 import javax.print.attribute.Attribute; 52 import javax.print.attribute.EnumSyntax; 44 53 import javax.print.attribute.HashPrintRequestAttributeSet; 54 import javax.print.attribute.HashPrintServiceAttributeSet; 55 import javax.print.attribute.IntegerSyntax; 45 56 import javax.print.attribute.PrintRequestAttributeSet; 57 import javax.print.attribute.PrintServiceAttribute; 58 import javax.print.attribute.TextSyntax; 46 59 import javax.print.attribute.standard.Media; 47 60 import javax.print.attribute.standard.MediaPrintableArea; … … 136 149 job.setPrintable(mapView); 137 150 build(); 151 loadPrintSettings(); 138 152 updateFields(); 139 153 pack(); … … 166 180 ) 167 181 ).applySafe(this); 168 } else if ( !visible &&isShowing()){182 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775 169 183 new WindowGeometry(this).remember(getClass().getName() + ".geometry"); 170 184 Main.pref.put("print.preview.enabled",previewCheckBox.isSelected()); … … 402 416 String cmd = e.getActionCommand(); 403 417 if (cmd.equals("printer-dialog")) { 404 /*PrintService[] services =*/ PrintServiceLookup.lookupPrintServices(null, null);405 /*PrintService svc =*/ PrintServiceLookup.lookupDefaultPrintService();406 418 if (job.printDialog(attrs)) { 407 419 updateFields(); 420 savePrintSettings(); 408 421 } 409 422 } … … 458 471 } 459 472 473 protected void savePrintSettings() { 474 // Save only one printer service attribute: printer name 475 PrintService service = job.getPrintService(); 476 if (service != null) { 477 Collection<Collection<String>> serviceAttributes = new ArrayList<Collection<String>>(); 478 for (Attribute a : service.getAttributes().toArray()) { 479 if ("printer-name".equals(a.getName()) && a instanceof TextSyntax) { 480 serviceAttributes.add(marshallPrintSetting(a, TextSyntax.class, ((TextSyntax)a).getValue())); 481 } 482 } 483 Main.pref.putArray("print.settings.service-attributes", serviceAttributes); 484 } 485 486 // Save all request attributes 487 Collection<String> ignoredAttributes = Arrays.asList("media-printable-area"); 488 Collection<Collection<String>> requestAttributes = new ArrayList<Collection<String>>(); 489 for (Attribute a : attrs.toArray()) { 490 Collection<String> setting = null; 491 if (a instanceof TextSyntax) { 492 setting = marshallPrintSetting(a, TextSyntax.class, ((TextSyntax)a).getValue()); 493 } else if (a instanceof EnumSyntax) { 494 setting = marshallPrintSetting(a, EnumSyntax.class, Integer.toString(((EnumSyntax)a).getValue())); 495 } else if (a instanceof IntegerSyntax) { 496 setting = marshallPrintSetting(a, IntegerSyntax.class, Integer.toString(((IntegerSyntax)a).getValue())); 497 } else if (!ignoredAttributes.contains(a.getName())) { 498 // TODO: Add support for DateTimeSyntax, SetOfIntegerSyntax, ResolutionSyntax if needed 499 Main.warn("Print request attribute not supported: "+a.getName() +": "+a.getCategory()+" - "+a.toString()); 500 } 501 if (setting != null) { 502 requestAttributes.add(setting); 503 } 504 } 505 Main.pref.putArray("print.settings.request-attributes", requestAttributes); 506 } 507 508 protected Collection<String> marshallPrintSetting(Attribute a, Class<?> syntaxClass, String value) { 509 return new ArrayList<String>(Arrays.asList(a.getCategory().getName(), a.getClass().getName(), syntaxClass.getName(), value)); 510 } 511 512 @SuppressWarnings("unchecked") 513 protected Attribute unmarshallPrintSetting(Collection<String> setting) throws 514 IllegalArgumentException, ClassNotFoundException, SecurityException, NoSuchMethodException, 515 InstantiationException, IllegalAccessException, InvocationTargetException { 516 517 if (setting == null || setting.size() != 4) { 518 throw new IllegalArgumentException("Invalid setting: "+setting); 519 } 520 Iterator<String> it = setting.iterator(); 521 Class<? extends Attribute> category = (Class<? extends Attribute>) Class.forName(it.next()); 522 Class<? extends Attribute> realClass = (Class<? extends Attribute>) Class.forName(it.next()); 523 Class<?> syntax = Class.forName(it.next()); 524 String value = it.next(); 525 if (syntax.equals(TextSyntax.class)) { 526 return realClass.getConstructor(String.class, Locale.class).newInstance(value, null); 527 } else if (syntax.equals(EnumSyntax.class)) { 528 int intValue = Integer.parseInt(value); 529 for (Field f : realClass.getFields()) { 530 if (Modifier.isStatic(f.getModifiers()) && category.isAssignableFrom(f.getType())) { 531 EnumSyntax es = (EnumSyntax) f.get(null); 532 if (es.getValue() == intValue) { 533 return (Attribute) es; 534 } 535 } 536 } 537 throw new IllegalArgumentException("Invalid enum: "+realClass+" - "+value); 538 } else if (syntax.equals(IntegerSyntax.class)) { 539 return realClass.getConstructor(int.class).newInstance(Integer.parseInt(value)); 540 } else { 541 Main.warn("Attribute syntax not supported: "+syntax); 542 } 543 return null; 544 } 545 546 protected void loadPrintSettings() { 547 for (Collection<String> setting : Main.pref.getArray("print.settings.service-attributes")) { 548 try { 549 PrintServiceAttribute a = (PrintServiceAttribute) unmarshallPrintSetting(setting); 550 if ("printer-name".equals(a.getName())) { 551 job.setPrintService(PrintServiceLookup.lookupPrintServices(null, new HashPrintServiceAttributeSet(a))[0]); 552 } 553 } catch (Exception e) { 554 Main.warn(e.getClass().getSimpleName()+": "+e.getMessage()); 555 } 556 } 557 for (Collection<String> setting : Main.pref.getArray("print.settings.request-attributes")) { 558 try { 559 attrs.add(unmarshallPrintSetting(setting)); 560 } catch (Exception e) { 561 Main.warn(e.getClass().getSimpleName()+": "+e.getMessage()); 562 } 563 } 564 } 460 565 } 461 566 -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPlugin.java
r29846 r29848 91 91 "print.preview.enabled", new Boolean(false).toString()); 92 92 93 restorePrefs(); // Recover after crash if nec cesseary93 restorePrefs(); // Recover after crash if necessary 94 94 } 95 95 -
applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPreview.java
r27252 r29848 302 302 } 303 303 304 305 304 g2d.setTransform(at); 306 305 }
Note:
See TracChangeset
for help on using the changeset viewer.