summaryrefslogtreecommitdiff
path: root/plugins/ui-designer/src/com/intellij/uiDesigner/SimpleTransferable.java
blob: 5a0e40a3709a21e24c30fe9b1bbf5c15162af43d (plain)
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
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.uiDesigner;

import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.Nullable;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;

/**
 * @author yole
 */
public final class SimpleTransferable<T> implements Transferable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.uiDesigner.SimpleTransferable");
  private static final Map<String, DataFlavor> ourDataFlavorMap = new HashMap<String, DataFlavor>();

  private final T myDataProxy;
  private final Class<T> myDataClass;
  private final DataFlavor myDataFlavor;

  public SimpleTransferable(final T data, final Class<T> dataClass) {
    myDataProxy = data;
    myDataClass = dataClass;
    myDataFlavor = getDataFlavor(myDataClass);
  }

  public SimpleTransferable(final T data, final Class<T> dataClass, DataFlavor flavor) {
    myDataProxy = data;
    myDataClass = dataClass;
    myDataFlavor = flavor;
  }

  @Nullable
  public Object getTransferData(final DataFlavor flavor) {
    try {
      if (!myDataFlavor.equals(flavor)) {
        return null;
      }
      return myDataProxy;
    }
    catch (Exception e) {
      LOG.error(e);
      return null;
    }
  }

  private static <T> DataFlavor getDataFlavor(final Class<T> dataClass) {
    DataFlavor result = ourDataFlavorMap.get(dataClass.getName());
    if (result == null) {
      try {
        result = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        ourDataFlavorMap.put(dataClass.getName(), result);
      }
      catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    }
    return result;
  }

  public DataFlavor[] getTransferDataFlavors() {
    try {
      return new DataFlavor[]{ myDataFlavor };
    }
    catch(Exception ex) {
      LOG.error(ex);
      return new DataFlavor[0];
    }
  }

  public boolean isDataFlavorSupported(final DataFlavor flavor) {
    try {
      return flavor.equals(myDataFlavor);
    }
    catch(Exception ex) {
      LOG.error(ex);
      return false;
    }
  }

  @Nullable public static <T> T getData(Transferable transferable, Class<T> dataClass) {
    try {
      final DataFlavor dataFlavor = getDataFlavor(dataClass);
      if (!transferable.isDataFlavorSupported(dataFlavor)) {
        return null;
      }
      final Object transferData = transferable.getTransferData(dataFlavor);
      if (!dataClass.isInstance(transferData)) {
        return null;
      }
      //noinspection unchecked
      return (T) transferData;
    }
    catch(IOException e) {
      return null;
    }
    catch (Exception e) {
      LOG.error(e);
      return null;
    }
  }
}