/*
 * WinPrinter.java
 *
 * Created on July 3, 2003, 9:29 AM
 */

package edu.columbia.print.ninja.prix.service.win;

import java.util.*;
import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

/**
 *
 * @author  joe
 */
public class WinPrinter implements PrintService {

    PrintService parent;

    /** Creates a new instance of WinPrinter */
    public WinPrinter(PrintService parent) {
        this.parent = parent;
    }
    
    public void addPrintServiceAttributeListener(javax.print.event.PrintServiceAttributeListener printServiceAttributeListener) {
        parent.addPrintServiceAttributeListener(printServiceAttributeListener);
    }
    
    public javax.print.DocPrintJob createPrintJob() {
        return new PrintJob(parent.createPrintJob());
    }
    
    public javax.print.attribute.PrintServiceAttribute getAttribute(Class clazz) {
        return parent.getAttribute(clazz);
    }
    
    public javax.print.attribute.PrintServiceAttributeSet getAttributes() {
        return parent.getAttributes();
    }
    
    public Object getDefaultAttributeValue(Class clazz) {
        return parent.getDefaultAttributeValue(clazz);
    }
    
    public String getName() {
        return parent.getName();
    }
    
    public javax.print.ServiceUIFactory getServiceUIFactory() {
        return parent.getServiceUIFactory();
    }
    
    public Class[] getSupportedAttributeCategories() {
        return parent.getSupportedAttributeCategories();
    }
    
    public Object getSupportedAttributeValues(Class clazz, javax.print.DocFlavor docFlavor, javax.print.attribute.AttributeSet attributeSet) {
        return parent.getSupportedAttributeValues(clazz, docFlavor, attributeSet);
    }
    
    public javax.print.DocFlavor[] getSupportedDocFlavors() {
        return parent.getSupportedDocFlavors();
    }
    
    public javax.print.attribute.AttributeSet getUnsupportedAttributes(javax.print.DocFlavor docFlavor, javax.print.attribute.AttributeSet attributeSet) {
        return parent.getUnsupportedAttributes(docFlavor, attributeSet);
    }
    
    public boolean isAttributeCategorySupported(Class clazz) {
        return parent.isAttributeCategorySupported(clazz);
    }
    
    public boolean isAttributeValueSupported(javax.print.attribute.Attribute attribute, javax.print.DocFlavor docFlavor, javax.print.attribute.AttributeSet attributeSet) {
        return parent.isAttributeValueSupported(attribute, docFlavor, attributeSet);
    }
    
    public boolean isDocFlavorSupported(javax.print.DocFlavor docFlavor) {
        return parent.isDocFlavorSupported(docFlavor);
    }
    
    public void removePrintServiceAttributeListener(javax.print.event.PrintServiceAttributeListener printServiceAttributeListener) {
        parent.removePrintServiceAttributeListener(printServiceAttributeListener);
    }

    class PrintJob implements DocPrintJob {
        
        DocPrintJob parent;
        Set listeners;
        Map attributeListeners;
        JobState state;

        public PrintJob(DocPrintJob parent) {
            this.parent = parent;
            attributeListeners = new HashMap();
            listeners = new HashSet();
        }

        public void addPrintJobAttributeListener(javax.print.event.PrintJobAttributeListener printJobAttributeListener, javax.print.attribute.PrintJobAttributeSet printJobAttributeSet) {
            attributeListeners.put(printJobAttributeListener, printJobAttributeSet);
            parent.addPrintJobAttributeListener(printJobAttributeListener, printJobAttributeSet);
        }
        
        public void addPrintJobListener(javax.print.event.PrintJobListener printJobListener) {
            listeners.add(printJobListener);
            parent.addPrintJobListener(printJobListener);
        }
        
        public javax.print.attribute.PrintJobAttributeSet getAttributes() {
            PrintJobAttributeSet set = new HashPrintJobAttributeSet(parent.getAttributes());
            if(state!=null) {
                set.add(state);
                JobMediaSheets jms = (JobMediaSheets )set.get(JobMediaSheets.class);
                set.add(new JobMediaSheetsCompleted(jms.getValue()));
            }
            return set;
        }
        
        public javax.print.PrintService getPrintService() {
            return parent.getPrintService();
        }
        
        public void print(javax.print.Doc doc, javax.print.attribute.PrintRequestAttributeSet printRequestAttributeSet) throws javax.print.PrintException {
            JobMediaSheets jms = (JobMediaSheets )printRequestAttributeSet.get(JobMediaSheets.class);
            if(jms!=null) {
                parent.print(doc, printRequestAttributeSet);
                //notify listeners
                PrintJobEvent pje = new PrintJobEvent(this, PrintJobEvent.JOB_COMPLETE);
                for(Iterator i=listeners.iterator(); i.hasNext();) {
                    PrintJobListener l = (PrintJobListener )i.next();
                    l.printJobCompleted(pje);
                }
                state = JobState.COMPLETED;
            }
            else state = JobState.ABORTED;
            PrintJobAttributeEvent pjae = new PrintJobAttributeEvent(this, getAttributes());
            for(Iterator i=attributeListeners.entrySet().iterator(); i.hasNext();) {
                Map.Entry entry = (Map.Entry )i.next();
                PrintJobAttributeSet set = (PrintJobAttributeSet )entry.getValue();
                PrintJobAttributeListener l = (PrintJobAttributeListener )entry.getKey();
                l.attributeUpdate(pjae);
            }
        }
        
        public void removePrintJobAttributeListener(javax.print.event.PrintJobAttributeListener printJobAttributeListener) {
            attributeListeners.remove(printJobAttributeListener);
            parent.removePrintJobAttributeListener(printJobAttributeListener);
        }
        
        public void removePrintJobListener(javax.print.event.PrintJobListener printJobListener) {
            listeners.remove(printJobListener);
            parent.removePrintJobListener(printJobListener);
        }
        
    }
}
