001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package org.apache.harmony.awt.datatransfer;
021:
022: import java.awt.datatransfer.DataFlavor;
023: import java.awt.datatransfer.SystemFlavorMap;
024: import java.awt.dnd.DragGestureEvent;
025: import java.awt.dnd.DropTargetContext;
026: import java.awt.dnd.peer.DragSourceContextPeer;
027: import java.awt.dnd.peer.DropTargetContextPeer;
028: import java.nio.charset.Charset;
029:
030: import org.apache.harmony.awt.ContextStorage;
031: import org.apache.harmony.awt.internal.nls.Messages;
032: import org.apache.harmony.misc.SystemUtils;
033:
034: /**
035: * Data transfer ToolKit.
036: * Unites context-dependent and platform-dependent information of data transferring subsystem.
037: */
038: public abstract class DTK {
039:
040: private NativeClipboard nativeClipboard = null;
041: private NativeClipboard nativeSelection = null;
042:
043: protected SystemFlavorMap systemFlavorMap;
044:
045: protected final DataTransferThread dataTransferThread;
046:
047: protected DTK() {
048: dataTransferThread = new DataTransferThread(this );
049: dataTransferThread.start();
050: }
051:
052: /**
053: * Returns data transfer toolkit for current application context.
054: */
055: public static DTK getDTK() {
056: synchronized (ContextStorage.getContextLock()) {
057: if (ContextStorage.shutdownPending()) {
058: return null;
059: }
060:
061: DTK instance = ContextStorage.getDTK();
062:
063: if (instance == null) {
064: instance = createDTK();
065: ContextStorage.setDTK(instance);
066: }
067:
068: return instance;
069: }
070: }
071:
072: /**
073: * Returns system flavor map for current application context.
074: * For use from SystemFlavorMap.getDefaultFlavorMap() only
075: */
076: public synchronized SystemFlavorMap getSystemFlavorMap() {
077: return systemFlavorMap;
078: }
079:
080: /**
081: * Sets system flavor map for current application context.
082: * For use from SystemFlavorMap.getDefaultFlavorMap() only.
083: */
084: public synchronized void setSystemFlavorMap(
085: SystemFlavorMap newFlavorMap) {
086: this .systemFlavorMap = newFlavorMap;
087: }
088:
089: /**
090: * Returns native clipboard for current application context.
091: */
092: public NativeClipboard getNativeClipboard() {
093: if (nativeClipboard == null) {
094: nativeClipboard = newNativeClipboard();
095: }
096:
097: return nativeClipboard;
098: }
099:
100: /**
101: * Returns native selection for current application context.
102: */
103: public NativeClipboard getNativeSelection() {
104: if (nativeSelection == null) {
105: nativeSelection = newNativeSelection();
106: }
107:
108: return nativeSelection;
109: }
110:
111: /**
112: * Creates native clipboard for current native platform.
113: */
114: protected abstract NativeClipboard newNativeClipboard();
115:
116: /**
117: * Creates native selection for current native platform.
118: */
119: protected abstract NativeClipboard newNativeSelection();
120:
121: public abstract void initDragAndDrop();
122:
123: public abstract void runEventLoop();
124:
125: public abstract DropTargetContextPeer createDropTargetContextPeer(
126: DropTargetContext context);
127:
128: public abstract DragSourceContextPeer createDragSourceContextPeer(
129: DragGestureEvent dge);
130:
131: private static DTK createDTK() {
132: String name;
133: switch (SystemUtils.getOS()) {
134: case SystemUtils.OS_WINDOWS:
135: name = "org.apache.harmony.awt.datatransfer.windows.WinDTK"; //$NON-NLS-1$
136: break;
137: case SystemUtils.OS_LINUX:
138: name = "org.apache.harmony.awt.datatransfer.linux.LinuxDTK"; //$NON-NLS-1$
139: break;
140: default:
141: // awt.4E=Unknown native platform.
142: throw new RuntimeException(Messages.getString("awt.4E")); //$NON-NLS-1$
143: }
144: try {
145: DTK dtk = (DTK) Class.forName(name).newInstance();
146: return dtk;
147: } catch (Exception e) {
148: throw new RuntimeException(e);
149: }
150: }
151:
152: public String getDefaultCharset() {
153: return "unicode"; //$NON-NLS-1$
154: }
155:
156: protected String[] getCharsets() {
157: return new String[] {
158: "UTF-16", "UTF-8", "unicode", "ISO-8859-1", "US-ASCII" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
159: }
160:
161: public void initSystemFlavorMap(SystemFlavorMap fm) {
162: String[] charsets = getCharsets();
163:
164: appendSystemFlavorMap(fm, DataFlavor.stringFlavor,
165: DataProvider.FORMAT_TEXT);
166: appendSystemFlavorMap(fm, charsets, "plain", //$NON-NLS-1$
167: DataProvider.FORMAT_TEXT);
168:
169: appendSystemFlavorMap(fm, charsets, "html", //$NON-NLS-1$
170: DataProvider.FORMAT_HTML);
171:
172: appendSystemFlavorMap(fm, DataProvider.urlFlavor,
173: DataProvider.FORMAT_URL);
174: appendSystemFlavorMap(fm, charsets, "uri-list", //$NON-NLS-1$
175: DataProvider.FORMAT_URL);
176:
177: appendSystemFlavorMap(fm, DataFlavor.javaFileListFlavor,
178: DataProvider.FORMAT_FILE_LIST);
179:
180: appendSystemFlavorMap(fm, DataFlavor.imageFlavor,
181: DataProvider.FORMAT_IMAGE);
182: }
183:
184: protected void appendSystemFlavorMap(SystemFlavorMap fm,
185: DataFlavor flav, String nat) {
186: fm.addFlavorForUnencodedNative(nat, flav);
187: fm.addUnencodedNativeForFlavor(flav, nat);
188: }
189:
190: protected void appendSystemFlavorMap(SystemFlavorMap fm,
191: String[] charsets, String subType, String nat) {
192: TextFlavor.addUnicodeClasses(fm, nat, subType);
193: for (int i = 0; i < charsets.length; i++) {
194: if (charsets[i] != null && Charset.isSupported(charsets[i])) {
195: TextFlavor.addCharsetClasses(fm, nat, subType,
196: charsets[i]);
197: }
198: }
199: }
200: }
|