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.io.BufferedWriter;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.io.PipedWriter;
|
---|
24 | import java.io.Writer;
|
---|
25 |
|
---|
26 | import org.expeditee.gio.ClipboardManager.ClipboardData;
|
---|
27 | import org.expeditee.gio.EcosystemManager;
|
---|
28 |
|
---|
29 | public class ProxyWriter extends BufferedWriter {
|
---|
30 |
|
---|
31 | private StringBuffer _contents = null;
|
---|
32 |
|
---|
33 | public ProxyWriter(Writer out) {
|
---|
34 | super(out);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public ProxyWriter(Writer out, int sz) {
|
---|
38 | super(out, sz);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ProxyWriter(boolean useClipboard) {
|
---|
42 | // create a writer that does nothing
|
---|
43 | super(new PipedWriter());
|
---|
44 | _contents = new StringBuffer("");
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void write(String s) throws IOException {
|
---|
49 | if (_contents != null) {
|
---|
50 | _contents.append(s);
|
---|
51 | } else
|
---|
52 | super.write(s);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void flush() throws IOException {
|
---|
57 | if (_contents != null) {
|
---|
58 | EcosystemManager.getClipboardManager().set(ClipboardData.fromString(_contents.toString()));
|
---|
59 | } else {
|
---|
60 | super.flush();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void close() throws IOException {
|
---|
66 | if (_contents == null)
|
---|
67 | super.close();
|
---|
68 | }
|
---|
69 | }
|
---|