Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / SimpleMultiDoc.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/SimpleMultiDoc.java b/jspi/src/main/java/de/lohndirekt/print/SimpleMultiDoc.java
new file mode 100644 (file)
index 0000000..4146418
--- /dev/null
@@ -0,0 +1,135 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+\r
+import javax.print.Doc;\r
+import javax.print.MultiDoc;\r
+\r
+/**\r
+ * @author bpusch\r
+ *\r
+ */\r
+public class SimpleMultiDoc implements MultiDoc {\r
+\r
+    private List listeners;\r
+    Doc doc = null;\r
+    boolean lastDoc = false;\r
+    SimpleMultiDoc next = null;\r
+\r
+    public static SimpleMultiDoc create(Doc doc) {\r
+        return new SimpleMultiDoc(doc, false);\r
+    }\r
+\r
+    public static SimpleMultiDoc create(Doc doc, boolean lastDoc) {\r
+        return new SimpleMultiDoc(doc, lastDoc);\r
+    }\r
+\r
+    /**\r
+     * \r
+     */\r
+    protected SimpleMultiDoc(Doc doc, boolean lastDoc) {\r
+        if (doc == null) {\r
+            throw new NullPointerException("Doc must not be null");\r
+        }\r
+        this.doc = doc;\r
+        this.lastDoc = lastDoc;\r
+    }\r
+\r
+    /**\r
+     * Adds a new MultiDoc.\r
+     * If the next MultiDoc has already been set, it tries to add it there\r
+     * \r
+     * @param doc\r
+     * @param lastDoc\r
+     * @throws IllegalStateException if this has already been the last Document\r
+     */\r
+    public void setNext(Doc doc, boolean lastDoc) {\r
+        if (this.lastDoc) {\r
+            throw new IllegalStateException("Last Doc already sent");\r
+        }\r
+        if (doc == null) {\r
+            throw new NullPointerException("Doc must not be null");\r
+        }\r
+        if (next == null) {\r
+            this.next = new SimpleMultiDoc(doc, lastDoc);\r
+        } else {\r
+            this.next.setNext(doc, lastDoc);\r
+        }\r
+        this.notifyListeners();\r
+    }\r
+\r
+    public void setNext(Doc doc) {\r
+        setNext(doc, false);\r
+    }\r
+\r
+    /**\r
+     *\r
+     */\r
+\r
+    public Doc getDoc() throws IOException {\r
+        return this.doc;\r
+    }\r
+\r
+    /**\r
+     *\r
+     */\r
+    public MultiDoc next() throws IOException {\r
+        if (!this.available()) {\r
+            throw new IllegalStateException("Next MultiDoc is not yet available");\r
+        }\r
+        return this.next;\r
+    }\r
+\r
+    public boolean available() {\r
+        return (!this.lastDoc && this.next != null);\r
+    }\r
+\r
+    public boolean isLast() {\r
+        return lastDoc;\r
+    }\r
+\r
+    public String toString() {\r
+        return this.getClass() + " - " + this.doc.toString();\r
+    }\r
+\r
+    public void addMultiDocListener(MultiDocListener listener) {\r
+        if (this.listeners == null) {\r
+            this.listeners = new ArrayList();\r
+        }\r
+        this.listeners.add(listener);\r
+    }\r
+\r
+    private void notifyListeners() {\r
+        if (this.listeners != null) {\r
+            for (Iterator iter = this.listeners.iterator(); iter.hasNext();) {\r
+                MultiDocListener listener = (MultiDocListener) iter.next();\r
+                try {\r
+                    listener.processEvent(new MultiDocEvent(this.next));\r
+                } catch (IOException e) {\r
+                    //TODO handle exception appropriatly\r
+                }\r
+            }\r
+        }\r
+    }\r
+}\r