Changeset 29848 in osm


Ignore:
Timestamp:
2013-08-20T03:54:05+02:00 (11 years ago)
Author:
donvip
Message:

[josm_print] fix #josm8981 - Remember printer settings

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  
    4848        Shortcut.registerShortcut("print:print", tr("File: {0}", tr("Print...")),
    4949        KeyEvent.VK_P, Shortcut.CTRL),
    50         true, "print/print", true);
     50            true, "print/print", true);
    5151    }
    5252
  • applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintDialog.java

    r29846 r29848  
    3737import java.awt.print.PrinterException;
    3838import java.awt.print.PrinterJob;
     39import java.lang.reflect.Field;
     40import java.lang.reflect.InvocationTargetException;
     41import java.lang.reflect.Modifier;
    3942import java.text.ParseException;
     43import java.util.ArrayList;
     44import java.util.Arrays;
     45import java.util.Collection;
     46import java.util.Iterator;
     47import java.util.Locale;
    4048
    4149import javax.print.PrintService;
    4250import javax.print.PrintServiceLookup;
    4351import javax.print.attribute.Attribute;
     52import javax.print.attribute.EnumSyntax;
    4453import javax.print.attribute.HashPrintRequestAttributeSet;
     54import javax.print.attribute.HashPrintServiceAttributeSet;
     55import javax.print.attribute.IntegerSyntax;
    4556import javax.print.attribute.PrintRequestAttributeSet;
     57import javax.print.attribute.PrintServiceAttribute;
     58import javax.print.attribute.TextSyntax;
    4659import javax.print.attribute.standard.Media;
    4760import javax.print.attribute.standard.MediaPrintableArea;
     
    136149        job.setPrintable(mapView);
    137150        build();
     151        loadPrintSettings();
    138152        updateFields();
    139153        pack();
     
    166180                    )
    167181            ).applySafe(this);
    168         } else if (!visible && isShowing()){
     182        } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
    169183            new WindowGeometry(this).remember(getClass().getName() + ".geometry");
    170184            Main.pref.put("print.preview.enabled",previewCheckBox.isSelected());
     
    402416        String cmd = e.getActionCommand();
    403417        if (cmd.equals("printer-dialog")) {
    404             /*PrintService[] services =*/ PrintServiceLookup.lookupPrintServices(null, null);
    405             /*PrintService svc =*/ PrintServiceLookup.lookupDefaultPrintService();
    406418            if (job.printDialog(attrs)) {
    407419                updateFields();
     420                savePrintSettings();
    408421            }
    409422        }
     
    458471    }
    459472   
     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    }
    460565}
    461566
  • applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPlugin.java

    r29846 r29848  
    9191          "print.preview.enabled", new Boolean(false).toString());
    9292
    93         restorePrefs(); // Recover after crash if neccesseary
     93        restorePrefs(); // Recover after crash if necessary
    9494    }
    9595
  • applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintPreview.java

    r27252 r29848  
    302302        }
    303303
    304 
    305304        g2d.setTransform(at);
    306305    }
Note: See TracChangeset for help on using the changeset viewer.