|
| 1 | +/* |
| 2 | + * Copyright 2016 Google, Inc. |
| 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.google.classyshark.gui.panel; |
| 18 | + |
| 19 | +import com.google.classyshark.gui.panel.io.CurrentFolderConfig; |
| 20 | +import com.google.classyshark.gui.panel.io.RecentArchivesConfig; |
| 21 | +import java.awt.datatransfer.DataFlavor; |
| 22 | +import java.awt.datatransfer.UnsupportedFlavorException; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.List; |
| 26 | +import javax.swing.JComponent; |
| 27 | +import javax.swing.TransferHandler; |
| 28 | + |
| 29 | +import static com.google.classyshark.gui.panel.io.FileChooserUtils.isSupportedArchiveFile; |
| 30 | + |
| 31 | +public class FileTransferHandler extends TransferHandler { |
| 32 | + |
| 33 | + private final ArchiveDisplayer archiveDisplayer; |
| 34 | + |
| 35 | + public FileTransferHandler(ArchiveDisplayer archiveDisplayer) { |
| 36 | + this.archiveDisplayer = archiveDisplayer; |
| 37 | + } |
| 38 | + |
| 39 | + public int getSourceActions(JComponent c) { |
| 40 | + return COPY_OR_MOVE; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean canImport(TransferSupport ts) { |
| 44 | + return ts.isDataFlavorSupported(DataFlavor.javaFileListFlavor); |
| 45 | + } |
| 46 | + |
| 47 | + public boolean importData(TransferSupport ts) { |
| 48 | + try { |
| 49 | + @SuppressWarnings("rawtypes") |
| 50 | + List data = (List) ts.getTransferable().getTransferData( |
| 51 | + DataFlavor.javaFileListFlavor); |
| 52 | + if (data.size() < 1) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + for (Object item : data) { |
| 57 | + File file = (File) item; |
| 58 | + |
| 59 | + if(isSupportedArchiveFile(file)) { |
| 60 | + CurrentFolderConfig.INSTANCE.setCurrentDirectory(file.getParentFile()); |
| 61 | + RecentArchivesConfig.INSTANCE.addArchive(file.getName(), |
| 62 | + file.getParentFile()); |
| 63 | + archiveDisplayer.displayArchive(file); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + |
| 69 | + } catch (UnsupportedFlavorException e) { |
| 70 | + return false; |
| 71 | + } catch (IOException e) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments