@@ -46,16 +46,16 @@ public class ClipBoardManager {
46
46
private static java .awt .datatransfer .Clipboard primary ;
47
47
private static ImportFormatReader importFormatReader ;
48
48
49
+ public ClipBoardManager () {
50
+ this (Clipboard .getSystemClipboard (), Toolkit .getDefaultToolkit ().getSystemSelection (), Globals .IMPORT_FORMAT_READER );
51
+ }
52
+
49
53
public ClipBoardManager (Clipboard clipboard , java .awt .datatransfer .Clipboard primary , ImportFormatReader importFormatReader ) {
50
54
ClipBoardManager .clipboard = clipboard ;
51
55
ClipBoardManager .primary = primary ;
52
56
ClipBoardManager .importFormatReader = importFormatReader ;
53
57
}
54
58
55
- public ClipBoardManager () {
56
- this (Clipboard .getSystemClipboard (), Toolkit .getDefaultToolkit ().getSystemSelection (), Globals .IMPORT_FORMAT_READER );
57
- }
58
-
59
59
/**
60
60
* Add X11 clipboard support to a text input control.
61
61
* It is necessary to call this method in every input where you want to use it:
@@ -67,7 +67,7 @@ public ClipBoardManager() {
67
67
*/
68
68
public static void addX11Support (TextInputControl input ) {
69
69
input .selectedTextProperty ().addListener ((observable , oldValue , newValue ) -> {
70
- if (!newValue .isEmpty ()) {
70
+ if (!newValue .isEmpty () && primary != null ) {
71
71
primary .setContents (new StringSelection (newValue ), null );
72
72
}
73
73
});
@@ -92,17 +92,19 @@ public static String getContents() {
92
92
}
93
93
94
94
/**
95
- * Get the String residing on the primary clipboard.
95
+ * Get the String residing on the primary clipboard (if it exists) .
96
96
*
97
97
* @return any text found on the primary Clipboard; if none found, try with the system clipboard.
98
98
*/
99
99
public static String getContentsPrimary () {
100
- Transferable contents = primary .getContents (null );
101
- if (contents != null && contents .isDataFlavorSupported (DataFlavor .stringFlavor )) {
102
- try {
103
- return (String ) contents .getTransferData (DataFlavor .stringFlavor );
104
- } catch (UnsupportedFlavorException | IOException e ) {
105
- LOGGER .warn (e .getMessage ());
100
+ if (primary != null ) {
101
+ Transferable contents = primary .getContents (null );
102
+ if (contents != null && contents .isDataFlavorSupported (DataFlavor .stringFlavor )) {
103
+ try {
104
+ return (String ) contents .getTransferData (DataFlavor .stringFlavor );
105
+ } catch (UnsupportedFlavorException | IOException e ) {
106
+ LOGGER .warn (e .getMessage ());
107
+ }
106
108
}
107
109
}
108
110
return getContents ();
@@ -119,12 +121,14 @@ public void setContent(ClipboardContent content) {
119
121
}
120
122
121
123
/**
122
- * Puts content onto the primary clipboard.
124
+ * Puts content onto the primary clipboard (if it exists) .
123
125
*
124
126
* @param content the ClipboardContent to set as current value of the primary clipboard.
125
127
*/
126
128
public void setPrimaryClipboardContent (ClipboardContent content ) {
127
- primary .setContents (new StringSelection (content .getString ()), null );
129
+ if (primary != null ) {
130
+ primary .setContents (new StringSelection (content .getString ()), null );
131
+ }
128
132
}
129
133
130
134
public void setHtmlContent (String html ) {
0 commit comments