1 | /**
|
---|
2 | * ProxyWriter.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.awt.Toolkit;
|
---|
22 | import java.awt.datatransfer.StringSelection;
|
---|
23 | import java.io.BufferedWriter;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.PipedWriter;
|
---|
26 | import java.io.Writer;
|
---|
27 |
|
---|
28 | public class ProxyWriter extends BufferedWriter {
|
---|
29 |
|
---|
30 | private StringBuffer _contents = null;
|
---|
31 |
|
---|
32 | public ProxyWriter(Writer out) {
|
---|
33 | super(out);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public ProxyWriter(Writer out, int sz) {
|
---|
37 | super(out, sz);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ProxyWriter(boolean useClipboard) {
|
---|
41 | // create a writer that does nothing
|
---|
42 | super(new PipedWriter());
|
---|
43 | _contents = new StringBuffer("");
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void write(String s) throws IOException {
|
---|
48 | if (_contents != null) {
|
---|
49 | _contents.append(s);
|
---|
50 | } else
|
---|
51 | super.write(s);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void flush() throws IOException {
|
---|
56 | if (_contents != null) {
|
---|
57 | StringSelection selection = new StringSelection(_contents
|
---|
58 | .toString());
|
---|
59 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
|
---|
60 | selection, null);
|
---|
61 | } else
|
---|
62 | super.flush();
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void close() throws IOException {
|
---|
67 | if (_contents == null)
|
---|
68 | super.close();
|
---|
69 | }
|
---|
70 | }
|
---|