Menu

[b98dae]: / G4P / src / g4p_controls / GClip.java  Maximize  Restore  History

Download this file

183 lines (161 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Part of the G4P library for Processing
http://www.lagers.org.uk/g4p/index.html
http://sourceforge.net/projects/g4p/files/?source=navbar
Copyright (c) 2008 Peter Lager
The actual code to create the clipbaord, copy and paste were
taken taken from a similar GUI library Interfascia ALPHA 002 --
http://superstable.net/interfascia/ produced by Brenden Berg
The main change is to provide static copy and paste methods to
separate the clipboard logic from the component logic and provide
global access.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package g4p_controls;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import sun.security.util.SecurityConstants;
/*
* I wanted to implement copying and pasting to the clipboard using static
* methods to simplify the sharing of a single clipboard over all classes.
* The need to implement the ClipboardOwner interface requires an object so
* this class creates an object the first time an attempt to copy or paste
* is used.
*
* All methods are private except copy() and paste() - lostOwnership()
* has to be public because of the Clipboard owner interface.
*
* @author Peter Lager
*
*/
/**
* Clipboard functionality for plain text <br>
*
* This provides clipboard functionality for text and is currently only used by
* the GTextField and GTextArea classes.
*
* @author Peter Lager
*
*/
public class GClip implements ClipboardOwner {
/**
* Static reference to enforce singleton pattern
*/
private static GClip gclip = null;
/**
* Class attribute to reference the programs clipboard
*/
private Clipboard clipboard = null;
/**
* Copy a string to the clipboard
*
* @param chars the characters to be stored on the clipboard
* @return true for a successful copy to clipboard
*/
public static boolean copy(String chars) {
if (gclip == null)
gclip = new GClip();
return gclip.copyString(chars);
}
/**
* Get a string from the clipboard
*
* @return the string on the clipboard
*/
public static String paste() {
if (gclip == null)
gclip = new GClip();
return gclip.pasteString();
}
/**
* Ctor is private so clipboard is only created when a copy or paste is
* attempted and one does not exist already.
*/
private GClip() {
if (clipboard == null) {
makeClipboardObject();
}
}
/**
* If security permits use the system clipboard otherwise create our own
* application clipboard.
*/
private void makeClipboardObject() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkPermission(SecurityConstants.ALL_PERMISSION);
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (SecurityException e) {
clipboard = new Clipboard("Application Clipboard");
}
} else {
try {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (Exception e) {
// THIS IS DUMB - true but is there another way - I think not
}
}
}
/**
* Copy a string to the clipboard. If the Clipboard has not been created then
* create it.
*
* @param chars the characters to be stored on the clipboard
* @return true for a successful copy to clipboard
*/
private boolean copyString(String chars) {
if (clipboard == null)
makeClipboardObject();
if (clipboard != null) {
StringSelection fieldContent = new StringSelection(chars);
clipboard.setContents(fieldContent, this);
return true;
}
return false;
}
/**
* Gets a string from the clipboard. If there is no Clipboard then create it.
*
* @return if possible the string on the clipboard else an empty string
*/
private String pasteString() {
// If there is no clipboard then there is nothing to paste
if (clipboard == null) {
makeClipboardObject();
return "";
}
// We have a clipboard so get the string if we can
Transferable clipboardContent = clipboard.getContents(this);
if ((clipboardContent != null) && (clipboardContent.isDataFlavorSupported(DataFlavor.stringFlavor))) {
try {
String tempString;
tempString = (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
return tempString;
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
/**
* Reqd by ClipboardOwner interface
*/
public void lostOwnership(Clipboard clipboard, Transferable contents) {
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.