KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > SystemSelection


1 /*
2  * SystemSelection.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: SystemSelection.java,v 1.3 2003/06/28 01:49:39 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.AWTEvent JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.awt.datatransfer.Clipboard JavaDoc;
27 import java.awt.datatransfer.ClipboardOwner JavaDoc;
28 import java.awt.datatransfer.DataFlavor JavaDoc;
29 import java.awt.datatransfer.StringSelection JavaDoc;
30 import java.awt.datatransfer.Transferable JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import javax.swing.undo.CompoundEdit JavaDoc;
33
34 public final class SystemSelection implements ClipboardOwner JavaDoc, Constants
35 {
36     private static SystemSelection systemSelection =
37         getSystemSelection();
38
39     private final Clipboard JavaDoc clipboard;
40     private String JavaDoc primarySelection;
41
42     private SystemSelection(Clipboard JavaDoc clipboard)
43     {
44         this.clipboard = clipboard;
45     }
46
47     private static SystemSelection getSystemSelection()
48     {
49         try {
50             Clipboard JavaDoc clipboard =
51                 Toolkit.getDefaultToolkit().getSystemSelection();
52             return new SystemSelection(clipboard);
53         }
54         catch (Exception JavaDoc e) {
55             return null;
56         }
57     }
58
59     public void lostOwnership(Clipboard JavaDoc clipboard, Transferable JavaDoc contents)
60     {
61         primarySelection = null;
62     }
63
64     public void update(Editor editor)
65     {
66         try {
67             if (clipboard != null) {
68                 StringSelection JavaDoc ss = null;
69                 if (editor.getMark() != null && !editor.isColumnSelection()) {
70                     primarySelection = new Region(editor).toString();
71                     ss = new StringSelection JavaDoc(primarySelection);
72                 } else if (primarySelection != null) {
73                     // We own the primary selection.
74
ss = new StringSelection JavaDoc("");
75                 }
76                 if (ss != null)
77                     clipboard.setContents(ss, this);
78             }
79         }
80         catch (OutOfMemoryError JavaDoc e) {
81             Log.error("SystemSelection.update() OutOfMemoryError");
82         }
83     }
84
85     public String JavaDoc getPrimarySelection()
86     {
87         if (primarySelection != null) {
88             // We own the primary selection.
89
return primarySelection;
90         }
91         Transferable JavaDoc t = clipboard.getContents(this);
92         if (t != null) {
93             try {
94                 return (String JavaDoc) t.getTransferData(DataFlavor.stringFlavor);
95             }
96             catch (Exception JavaDoc e) {}
97         }
98         return null;
99     }
100
101     public static void updateSystemSelection(Editor editor)
102     {
103         if (systemSelection != null)
104             systemSelection.update(editor);
105     }
106
107     public static void pastePrimarySelection()
108     {
109         final Editor editor = Editor.currentEditor();
110         if (!editor.checkReadOnly())
111             return;
112         if (systemSelection == null) {
113             Log.debug("pastePrimarySelection systemSelection is null");
114             return;
115         }
116         String JavaDoc s = systemSelection.getPrimarySelection();
117         if (s == null || s.length() == 0) {
118             Log.debug("pastePrimarySelection no selection");
119             return;
120         }
121         KillRing killRing = Editor.getKillRing();
122         killRing.appendNew(s);
123         // We MUST call killRing.pop() here so that killRing.indexOfNextPop
124
// and killRing.lastPaste are set correctly.
125
killRing.pop();
126         AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
127         if (e instanceof MouseEvent JavaDoc) {
128             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
129             editor.mouseMoveDotToPoint((MouseEvent JavaDoc) e);
130             editor.paste(s);
131             editor.endCompoundEdit(compoundEdit);
132         } else
133             editor.paste(s);
134         editor.setCurrentCommand(COMMAND_PASTE);
135     }
136 }
137
Popular Tags