1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.draw9patch.ui; 18 19 import javax.swing.TransferHandler; 20 import javax.swing.JComponent; 21 import java.awt.datatransfer.Transferable; 22 import java.awt.datatransfer.DataFlavor; 23 import java.awt.datatransfer.UnsupportedFlavorException; 24 import java.io.File; 25 import java.io.IOException; 26 import java.util.List; 27 import java.net.MalformedURLException; 28 29 class ImageTransferHandler extends TransferHandler { 30 private final MainFrame mainFrame; 31 ImageTransferHandler(MainFrame mainFrame)32 ImageTransferHandler(MainFrame mainFrame) { 33 this.mainFrame = mainFrame; 34 } 35 36 @Override importData(JComponent component, Transferable transferable)37 public boolean importData(JComponent component, Transferable transferable) { 38 try { 39 for (DataFlavor flavor : transferable.getTransferDataFlavors()) { 40 if (flavor.isFlavorJavaFileListType()) { 41 Object data = transferable.getTransferData(DataFlavor.javaFileListFlavor); 42 //noinspection unchecked 43 final File file = ((List<File>) data).get(0); 44 mainFrame.open(file).execute(); 45 return true; 46 } else if (flavor.isFlavorTextType()) { 47 if (flavor.getRepresentationClass() == String.class) { 48 String mime = flavor.getMimeType(); 49 DataFlavor flave = new DataFlavor(mime); 50 Object data = transferable.getTransferData(flave); 51 final String path = convertPath(data.toString()); 52 mainFrame.open(new File(path)).execute(); 53 return true; 54 } 55 } 56 } 57 } catch (UnsupportedFlavorException e) { 58 // Ignore 59 } catch (MalformedURLException e) { 60 e.printStackTrace(); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 67 return false; 68 } 69 convertPath(String path)70 private static String convertPath(String path) { 71 if (path.startsWith("file://")) path = path.substring("file://".length()); 72 if (path.indexOf('\n') != -1) path = path.substring(0, path.indexOf('\n')); 73 if (path.indexOf('\r') != -1) path = path.substring(0, path.indexOf('\r')); 74 return path; 75 } 76 77 @Override canImport(JComponent component, DataFlavor[] dataFlavors)78 public boolean canImport(JComponent component, DataFlavor[] dataFlavors) { 79 for (DataFlavor flavor : dataFlavors) { 80 if (flavor.isFlavorJavaFileListType() || flavor.isFlavorTextType()) { 81 return true; 82 } 83 } 84 return false; 85 } 86 } 87