| 1 | /**\r |
| 2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
| 3 | *\r |
| 4 | * This library is free software; you can redistribute it and/or\r |
| 5 | * modify it under the terms of the GNU Lesser General Public\r |
| 6 | * License as published by the Free Software Foundation; either\r |
| 7 | * version 2.1 of the License, or (at your option) any later version.\r |
| 8 | * \r |
| 9 | * This library is distributed in the hope that it will be useful,\r |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
| 12 | * Lesser General Public License for more details.\r |
| 13 | * \r |
| 14 | * You should have received a copy of the GNU Lesser General Public\r |
| 15 | * License along with this library; if not, write to the Free Software\r |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
| 17 | * \r |
| 18 | */\r |
| 19 | package de.lohndirekt.print;\r |
| 20 | \r |
| 21 | import java.io.IOException;\r |
| 22 | import java.util.ArrayList;\r |
| 23 | import java.util.Iterator;\r |
| 24 | import java.util.List;\r |
| 25 | \r |
| 26 | import javax.print.Doc;\r |
| 27 | import javax.print.MultiDoc;\r |
| 28 | \r |
| 29 | /**\r |
| 30 | * @author bpusch\r |
| 31 | *\r |
| 32 | */\r |
| 33 | public class SimpleMultiDoc implements MultiDoc {\r |
| 34 | \r |
| 35 | private List listeners;\r |
| 36 | Doc doc = null;\r |
| 37 | boolean lastDoc = false;\r |
| 38 | SimpleMultiDoc next = null;\r |
| 39 | \r |
| 40 | public static SimpleMultiDoc create(Doc doc) {\r |
| 41 | return new SimpleMultiDoc(doc, false);\r |
| 42 | }\r |
| 43 | \r |
| 44 | public static SimpleMultiDoc create(Doc doc, boolean lastDoc) {\r |
| 45 | return new SimpleMultiDoc(doc, lastDoc);\r |
| 46 | }\r |
| 47 | \r |
| 48 | /**\r |
| 49 | * \r |
| 50 | */\r |
| 51 | protected SimpleMultiDoc(Doc doc, boolean lastDoc) {\r |
| 52 | if (doc == null) {\r |
| 53 | throw new NullPointerException("Doc must not be null");\r |
| 54 | }\r |
| 55 | this.doc = doc;\r |
| 56 | this.lastDoc = lastDoc;\r |
| 57 | }\r |
| 58 | \r |
| 59 | /**\r |
| 60 | * Adds a new MultiDoc.\r |
| 61 | * If the next MultiDoc has already been set, it tries to add it there\r |
| 62 | * \r |
| 63 | * @param doc\r |
| 64 | * @param lastDoc\r |
| 65 | * @throws IllegalStateException if this has already been the last Document\r |
| 66 | */\r |
| 67 | public void setNext(Doc doc, boolean lastDoc) {\r |
| 68 | if (this.lastDoc) {\r |
| 69 | throw new IllegalStateException("Last Doc already sent");\r |
| 70 | }\r |
| 71 | if (doc == null) {\r |
| 72 | throw new NullPointerException("Doc must not be null");\r |
| 73 | }\r |
| 74 | if (next == null) {\r |
| 75 | this.next = new SimpleMultiDoc(doc, lastDoc);\r |
| 76 | } else {\r |
| 77 | this.next.setNext(doc, lastDoc);\r |
| 78 | }\r |
| 79 | this.notifyListeners();\r |
| 80 | }\r |
| 81 | \r |
| 82 | public void setNext(Doc doc) {\r |
| 83 | setNext(doc, false);\r |
| 84 | }\r |
| 85 | \r |
| 86 | /**\r |
| 87 | *\r |
| 88 | */\r |
| 89 | \r |
| 90 | public Doc getDoc() throws IOException {\r |
| 91 | return this.doc;\r |
| 92 | }\r |
| 93 | \r |
| 94 | /**\r |
| 95 | *\r |
| 96 | */\r |
| 97 | public MultiDoc next() throws IOException {\r |
| 98 | if (!this.available()) {\r |
| 99 | throw new IllegalStateException("Next MultiDoc is not yet available");\r |
| 100 | }\r |
| 101 | return this.next;\r |
| 102 | }\r |
| 103 | \r |
| 104 | public boolean available() {\r |
| 105 | return (!this.lastDoc && this.next != null);\r |
| 106 | }\r |
| 107 | \r |
| 108 | public boolean isLast() {\r |
| 109 | return lastDoc;\r |
| 110 | }\r |
| 111 | \r |
| 112 | public String toString() {\r |
| 113 | return this.getClass() + " - " + this.doc.toString();\r |
| 114 | }\r |
| 115 | \r |
| 116 | public void addMultiDocListener(MultiDocListener listener) {\r |
| 117 | if (this.listeners == null) {\r |
| 118 | this.listeners = new ArrayList();\r |
| 119 | }\r |
| 120 | this.listeners.add(listener);\r |
| 121 | }\r |
| 122 | \r |
| 123 | private void notifyListeners() {\r |
| 124 | if (this.listeners != null) {\r |
| 125 | for (Iterator iter = this.listeners.iterator(); iter.hasNext();) {\r |
| 126 | MultiDocListener listener = (MultiDocListener) iter.next();\r |
| 127 | try {\r |
| 128 | listener.processEvent(new MultiDocEvent(this.next));\r |
| 129 | } catch (IOException e) {\r |
| 130 | //TODO handle exception appropriatly\r |
| 131 | }\r |
| 132 | }\r |
| 133 | }\r |
| 134 | }\r |
| 135 | }\r |