Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 042be64

Browse files
committed
Merge pull request #68 from google/drag-and-drop-file
Drag and drop file
2 parents 108ea53 + d18b44c commit 042be64

11 files changed

+128
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 java.io.File;
20+
21+
public interface ArchiveDisplayer {
22+
void displayArchive(File file);
23+
}

ClassySharkWS/src/com/google/classyshark/gui/panel/ClassySharkPanel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public ClassySharkPanel(JFrame frame, File archive, String fullClassName) {
8686

8787
public ClassySharkPanel(JFrame frame, File archive) {
8888
this(frame);
89-
updateUiAfterArchiveRead(archive);
89+
displayArchive(archive);
9090
}
9191

9292
public ClassySharkPanel(JFrame frame) {
@@ -164,7 +164,7 @@ public String getDescription() {
164164
CurrentFolderConfig.INSTANCE.setCurrentDirectory(fc.getCurrentDirectory());
165165
RecentArchivesConfig.INSTANCE.addArchive(resultFile.getName(),
166166
fc.getCurrentDirectory());
167-
updateUiAfterArchiveRead(resultFile);
167+
displayArchive(resultFile);
168168
}
169169
}
170170

@@ -216,7 +216,7 @@ public void onChangeLeftPaneVisibility(boolean visible) {
216216
}
217217

218218
@Override
219-
public void updateUiAfterArchiveRead(File binaryArchive) {
219+
public void displayArchive(File binaryArchive) {
220220
if (parentFrame != null) {
221221
parentFrame.setTitle(binaryArchive.getName());
222222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

ClassySharkWS/src/com/google/classyshark/gui/panel/ViewerController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.classyshark.silverghost.methodscounter.ClassNode;
2020

21-
public interface ViewerController {
21+
public interface ViewerController extends ArchiveDisplayer {
2222
void onSelectedClassName(String className);
2323

2424
void onSelectedImportFromMouseClick(String classNameFromImportStatement);

ClassySharkWS/src/com/google/classyshark/gui/panel/chart/RingChartPanel.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.classyshark.gui.panel.chart;
1818

19+
import com.google.classyshark.gui.panel.FileTransferHandler;
1920
import com.google.classyshark.gui.panel.ViewerController;
2021
import com.google.classyshark.silverghost.methodscounter.ClassNode;
2122

@@ -86,6 +87,8 @@ public void mouseExited(MouseEvent e) {
8687

8788
}
8889
});
90+
91+
setTransferHandler(new FileTransferHandler(viewerController));
8992
}
9093

9194
@Override

ClassySharkWS/src/com/google/classyshark/gui/panel/displayarea/DisplayArea.java

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.classyshark.gui.panel.displayarea;
1818

19+
import com.google.classyshark.gui.panel.FileTransferHandler;
1920
import com.google.classyshark.gui.panel.displayarea.doodles.Doodle;
2021
import com.google.classyshark.silverghost.translator.Translator;
2122
import com.google.classyshark.silverghost.translator.java.JavaTranslator;
@@ -57,6 +58,10 @@ private enum DisplayDataState {
5758

5859
public DisplayArea(final ViewerController viewerController) {
5960
jTextPane = new JTextPane();
61+
62+
jTextPane.setDragEnabled(true);
63+
jTextPane.setTransferHandler(new FileTransferHandler(viewerController));
64+
6065
jTextPane.setEditable(false);
6166
jTextPane.setBackground(ColorScheme.BACKGROUND);
6267

ClassySharkWS/src/com/google/classyshark/gui/panel/io/FileChooserUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static String getFileChooserDescription() {
3333
return "dex, jar, apk, class, aar";
3434
}
3535

36-
private static boolean isSupportedArchiveFile(File f) {
36+
public static boolean isSupportedArchiveFile(File f) {
3737
String filename = f.getName().toLowerCase();
3838
return filename.endsWith(".dex")
3939
|| filename.endsWith(".jar")

ClassySharkWS/src/com/google/classyshark/gui/panel/methodscount/MethodsCountPanel.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616

1717
package com.google.classyshark.gui.panel.methodscount;
1818

19+
import com.google.classyshark.gui.panel.ColorScheme;
20+
import com.google.classyshark.gui.panel.FileTransferHandler;
1921
import com.google.classyshark.gui.panel.ViewerController;
20-
import com.google.classyshark.silverghost.methodscounter.RootBuilder;
2122
import com.google.classyshark.silverghost.methodscounter.ClassNode;
22-
import com.google.classyshark.gui.panel.ColorScheme;
23-
24-
import javax.swing.JFrame;
23+
import com.google.classyshark.silverghost.methodscounter.RootBuilder;
24+
import java.awt.BorderLayout;
25+
import java.awt.Font;
26+
import java.awt.HeadlessException;
27+
import java.io.File;
2528
import javax.swing.JPanel;
2629
import javax.swing.JScrollPane;
2730
import javax.swing.JTree;
2831
import javax.swing.SwingWorker;
29-
import javax.swing.WindowConstants;
3032
import javax.swing.border.EmptyBorder;
3133
import javax.swing.event.TreeSelectionEvent;
3234
import javax.swing.event.TreeSelectionListener;
3335
import javax.swing.tree.DefaultMutableTreeNode;
3436
import javax.swing.tree.DefaultTreeCellRenderer;
3537
import javax.swing.tree.DefaultTreeModel;
3638
import javax.swing.tree.TreeNode;
37-
import java.awt.BorderLayout;
38-
import java.awt.Dimension;
39-
import java.awt.Font;
40-
import java.awt.HeadlessException;
41-
import java.io.File;
4239

4340
public class MethodsCountPanel extends JPanel {
4441
private DefaultTreeModel treeModel;
@@ -85,6 +82,9 @@ public void valueChanged(TreeSelectionEvent e) {
8582
JScrollPane jScrollPane = new JScrollPane(jTree);
8683
this.setBorder(new EmptyBorder(0,0,0,0));
8784
this.add(jScrollPane, BorderLayout.CENTER);
85+
86+
jTree.setDragEnabled(true);
87+
jTree.setTransferHandler(new FileTransferHandler(viewerController));
8888
}
8989

9090
private void addNodes(ClassNode parent, DefaultMutableTreeNode jTreeParent) {

ClassySharkWS/src/com/google/classyshark/gui/panel/toolbar/RecentArchivesButton.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public RecentFilesListener(String archiveName) {
9494

9595
@Override
9696
public void actionPerformed(ActionEvent e) {
97-
panel.updateUiAfterArchiveRead(
97+
panel.displayArchive(
9898
new File(RecentArchivesConfig.INSTANCE.getFilePath(archiveName),
9999
archiveName));
100100
buildPopup();

ClassySharkWS/src/com/google/classyshark/gui/panel/toolbar/ToolbarController.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.classyshark.gui.panel.toolbar;
1818

19-
import java.io.File;
19+
import com.google.classyshark.gui.panel.ArchiveDisplayer;
2020

21-
public interface ToolbarController {
21+
public interface ToolbarController extends ArchiveDisplayer {
2222
void onChangedTextFromTypingArea(String text);
2323

2424
void openArchive();
@@ -30,7 +30,4 @@ public interface ToolbarController {
3030
void onExportButtonPressed();
3131

3232
void onChangeLeftPaneVisibility(boolean selected);
33-
34-
void updateUiAfterArchiveRead(File file);
35-
3633
}

ClassySharkWS/src/com/google/classyshark/gui/panel/tree/FilesTree.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.classyshark.gui.panel.tree;
1818

1919
import com.google.classyshark.gui.panel.ColorScheme;
20+
import com.google.classyshark.gui.panel.FileTransferHandler;
2021
import com.google.classyshark.gui.panel.ViewerController;
2122
import com.google.classyshark.silverghost.contentreader.ContentReader;
2223
import com.google.classyshark.gui.panel.reducer.Reducer;
@@ -44,10 +45,12 @@ public class FilesTree {
4445
private JTree jTree = null;
4546

4647
public FilesTree(ViewerController viewerPanel) {
48+
this.viewerController = viewerPanel;
4749
treeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
4850
jTree = new JTree(treeModel);
51+
jTree.setDragEnabled(true);
52+
jTree.setTransferHandler(new FileTransferHandler(viewerController));
4953
configureJTree(jTree);
50-
this.viewerController = viewerPanel;
5154
}
5255

5356
public void fillArchive(File loadedFile, List<String> displayedClassNames, List<ContentReader.Component> allComponets) {
@@ -57,8 +60,6 @@ public void fillArchive(File loadedFile, List<String> displayedClassNames, List<
5760
return;
5861
}
5962
TreeNode rootNode = createTreeModel(loadedFile, displayedClassNames, allComponets);
60-
61-
6263
treeModel.setRoot(rootNode);
6364
}
6465

0 commit comments

Comments
 (0)