From 4ee3a4becc75e3fb7e3ab2d05e2d00ab5825cdc1 Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Fri, 10 Jan 2025 16:00:21 +0100 Subject: [PATCH 1/4] Implement file drag and drop --- .../igv/coordinator/OutlineTopComponent.java | 109 ++++++++++++++++-- .../igv/view/PlaceholderTopComponent.java | 47 ++++++++ 2 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/PlaceholderTopComponent.java diff --git a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java index 9508a5dfbca52..0b7ee4e2578dc 100644 --- a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java +++ b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,8 +37,18 @@ import com.sun.hotspot.igv.settings.Settings; import com.sun.hotspot.igv.util.LookupHistory; import com.sun.hotspot.igv.view.EditorTopComponent; +import com.sun.hotspot.igv.view.PlaceholderTopComponent; import java.awt.BorderLayout; import java.awt.Dimension; +import java.awt.HeadlessException; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DropTarget; +import java.awt.dnd.DropTargetDragEvent; +import java.awt.dnd.DropTargetDropEvent; +import java.awt.dnd.DropTargetEvent; +import java.awt.dnd.DropTargetListener; import java.io.*; import java.nio.channels.FileChannel; import java.nio.file.*; @@ -91,6 +101,66 @@ public final class OutlineTopComponent extends TopComponent implements ExplorerM private RemoveAllAction removeAllAction; private GraphNode[] selectedGraphs = new GraphNode[0]; private Path documentPath = null; + + private final DropTargetListener fileDropListener = new DropTargetListener() { + @Override + public void dragEnter(DropTargetDragEvent dtde) { + if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { + dtde.acceptDrag(DnDConstants.ACTION_COPY); + } else { + dtde.rejectDrag(); + } + } + + @Override + public void dragOver(DropTargetDragEvent dtde) { + dragEnter(dtde); + } + + @Override + public void dropActionChanged(DropTargetDragEvent dtde) {} + + @Override + public void dragExit(DropTargetEvent dte) {} + + @Override + public void drop(DropTargetDropEvent dtde) { + try { + if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { + dtde.acceptDrop(DnDConstants.ACTION_COPY); + + List droppedFiles = (List) dtde.getTransferable() + .getTransferData(DataFlavor.javaFileListFlavor); + + if (droppedFiles.isEmpty()) return; + if (droppedFiles.size() > 1) { + JOptionPane.showMessageDialog(null, + "Please only drag and drop one file as only one file can be open at a time.", + "Multiple Files Dropped", JOptionPane.WARNING_MESSAGE); + return; + } + + File file = droppedFiles.get(0); + + if (file.getName().endsWith(".xml") || file.getName().endsWith(".igv")) { + handleOpen(file); + } else { + JOptionPane.showMessageDialog(null, + "Unsupported file type: " + file.getName(), + "Unsupported File", JOptionPane.WARNING_MESSAGE); + } + + dtde.dropComplete(true); + } else { + dtde.rejectDrop(); + } + } catch (HeadlessException | UnsupportedFlavorException | IOException ex) { + ex.printStackTrace(); + dtde.dropComplete(false); + } + } + }; + private final PlaceholderTopComponent editorPlaceholder = new PlaceholderTopComponent(fileDropListener); private OutlineTopComponent() { initComponents(); @@ -100,8 +170,19 @@ private OutlineTopComponent() { initListView(); initToolbar(); server.startServer(); - } + + showEditorPlaceholder(); + + WindowManager.getDefault().invokeWhenUIReady(() -> { + new DropTarget(WindowManager.getDefault().getMainWindow(), fileDropListener); + }); + } + private void showEditorPlaceholder() { + editorPlaceholder.open(); + editorPlaceholder.requestActive(); + } + public static GraphDocument getDocument() { return document; } @@ -362,6 +443,7 @@ public void clearWorkspace() { root = new FolderNode(document); manager.setRootContext(root); EditorTopComponent.closeAllInstances(); + showEditorPlaceholder(); } /** @@ -372,15 +454,20 @@ public void openFile() { JFileChooser fc = new JFileChooser(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT)); fc.setFileFilter(graphFileFilter); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { - clearWorkspace(); - String path = fc.getSelectedFile().getAbsolutePath(); - Settings.get().put(Settings.DIRECTORY, path); - setDocumentPath(path); - try { - loadGraphDocument(path, true); - } catch (IOException e) { - throw new RuntimeException(e); - } + handleOpen(fc.getSelectedFile()); + } + } + + private void handleOpen(File file) { + clearWorkspace(); + editorPlaceholder.close(); + String path = file.getAbsolutePath(); + Settings.get().put(Settings.DIRECTORY, path); + setDocumentPath(path); + try { + loadGraphDocument(path, true); + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/PlaceholderTopComponent.java b/src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/PlaceholderTopComponent.java new file mode 100644 index 0000000000000..285ecbceece93 --- /dev/null +++ b/src/utils/IdealGraphVisualizer/View/src/main/java/com/sun/hotspot/igv/view/PlaceholderTopComponent.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ +package com.sun.hotspot.igv.view; + +import java.awt.BorderLayout; +import java.awt.dnd.DropTarget; +import java.awt.dnd.DropTargetListener; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingConstants; +import org.openide.windows.TopComponent; + +/** + * This TopComponent is displayed in the editor location if no graphs have been loaded + * and allows the user to easily drag and drop graph files to be opened. + */ +public class PlaceholderTopComponent extends TopComponent { + public PlaceholderTopComponent(DropTargetListener fileDropListener) { + setLayout(new BorderLayout()); + JPanel container = new JPanel(new BorderLayout()); + container.add(new JLabel("Drop file here to open", SwingConstants.CENTER), BorderLayout.CENTER); + container.setDropTarget(new DropTarget(container, fileDropListener)); + add(container, BorderLayout.CENTER); + setName("Welcome"); + } +} From f49fd4b0e42ce0c3920aba2bba5a7e4614d31da7 Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Fri, 10 Jan 2025 16:09:33 +0100 Subject: [PATCH 2/4] Improve show hide logic --- .../sun/hotspot/igv/coordinator/OutlineTopComponent.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java index 0b7ee4e2578dc..19298af695bbe 100644 --- a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java +++ b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java @@ -317,6 +317,11 @@ private void documentChanged() { saveAction.setEnabled(enableButton); saveAsAction.setEnabled(enableButton); removeAllAction.setEnabled(enableButton); + if (document.getElements().isEmpty()) { + showEditorPlaceholder(); + } else { + editorPlaceholder.close(); + } } @Override @@ -443,7 +448,6 @@ public void clearWorkspace() { root = new FolderNode(document); manager.setRootContext(root); EditorTopComponent.closeAllInstances(); - showEditorPlaceholder(); } /** From a011853dd314f41ba9f61a1a4962829bc4b351ee Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Fri, 10 Jan 2025 16:23:24 +0100 Subject: [PATCH 3/4] Fix whitespace --- .../hotspot/igv/coordinator/OutlineTopComponent.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java index 19298af695bbe..eadeb03259a89 100644 --- a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java +++ b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java @@ -101,7 +101,7 @@ public final class OutlineTopComponent extends TopComponent implements ExplorerM private RemoveAllAction removeAllAction; private GraphNode[] selectedGraphs = new GraphNode[0]; private Path documentPath = null; - + private final DropTargetListener fileDropListener = new DropTargetListener() { @Override public void dragEnter(DropTargetDragEvent dtde) { @@ -170,19 +170,19 @@ private OutlineTopComponent() { initListView(); initToolbar(); server.startServer(); - + showEditorPlaceholder(); - + WindowManager.getDefault().invokeWhenUIReady(() -> { new DropTarget(WindowManager.getDefault().getMainWindow(), fileDropListener); }); - } + } private void showEditorPlaceholder() { editorPlaceholder.open(); editorPlaceholder.requestActive(); } - + public static GraphDocument getDocument() { return document; } @@ -461,7 +461,7 @@ public void openFile() { handleOpen(fc.getSelectedFile()); } } - + private void handleOpen(File file) { clearWorkspace(); editorPlaceholder.close(); From 9be4df50f8f7a7ed9e3e82125e94bbc0214f6552 Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Fri, 10 Jan 2025 16:30:07 +0100 Subject: [PATCH 4/4] Clean up --- .../igv/coordinator/OutlineTopComponent.java | 118 +++++++++--------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java index eadeb03259a89..f000f279d5f2e 100644 --- a/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java +++ b/src/utils/IdealGraphVisualizer/Coordinator/src/main/java/com/sun/hotspot/igv/coordinator/OutlineTopComponent.java @@ -102,64 +102,7 @@ public final class OutlineTopComponent extends TopComponent implements ExplorerM private GraphNode[] selectedGraphs = new GraphNode[0]; private Path documentPath = null; - private final DropTargetListener fileDropListener = new DropTargetListener() { - @Override - public void dragEnter(DropTargetDragEvent dtde) { - if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { - dtde.acceptDrag(DnDConstants.ACTION_COPY); - } else { - dtde.rejectDrag(); - } - } - - @Override - public void dragOver(DropTargetDragEvent dtde) { - dragEnter(dtde); - } - - @Override - public void dropActionChanged(DropTargetDragEvent dtde) {} - - @Override - public void dragExit(DropTargetEvent dte) {} - - @Override - public void drop(DropTargetDropEvent dtde) { - try { - if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { - dtde.acceptDrop(DnDConstants.ACTION_COPY); - - List droppedFiles = (List) dtde.getTransferable() - .getTransferData(DataFlavor.javaFileListFlavor); - - if (droppedFiles.isEmpty()) return; - if (droppedFiles.size() > 1) { - JOptionPane.showMessageDialog(null, - "Please only drag and drop one file as only one file can be open at a time.", - "Multiple Files Dropped", JOptionPane.WARNING_MESSAGE); - return; - } - - File file = droppedFiles.get(0); - - if (file.getName().endsWith(".xml") || file.getName().endsWith(".igv")) { - handleOpen(file); - } else { - JOptionPane.showMessageDialog(null, - "Unsupported file type: " + file.getName(), - "Unsupported File", JOptionPane.WARNING_MESSAGE); - } - - dtde.dropComplete(true); - } else { - dtde.rejectDrop(); - } - } catch (HeadlessException | UnsupportedFlavorException | IOException ex) { - ex.printStackTrace(); - dtde.dropComplete(false); - } - } - }; + private final DropTargetListener fileDropListener = new FileDropListener(); private final PlaceholderTopComponent editorPlaceholder = new PlaceholderTopComponent(fileDropListener); private OutlineTopComponent() { @@ -681,6 +624,65 @@ public void setState(String state) { handle.finish(); } + private class FileDropListener implements DropTargetListener { + @Override + public void dragEnter(DropTargetDragEvent dtde) { + if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { + dtde.acceptDrag(DnDConstants.ACTION_COPY); + } else { + dtde.rejectDrag(); + } + } + + @Override + public void dragOver(DropTargetDragEvent dtde) { + dragEnter(dtde); + } + + @Override + public void dropActionChanged(DropTargetDragEvent dtde) {} + + @Override + public void dragExit(DropTargetEvent dte) {} + + @Override + public void drop(DropTargetDropEvent dtde) { + try { + if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { + dtde.acceptDrop(DnDConstants.ACTION_COPY); + + List droppedFiles = (List) dtde.getTransferable() + .getTransferData(DataFlavor.javaFileListFlavor); + + if (droppedFiles.isEmpty()) return; + if (droppedFiles.size() > 1) { + JOptionPane.showMessageDialog(null, + "Please only drag and drop one file as only one file can be open at a time.", + "Multiple Files Dropped", JOptionPane.WARNING_MESSAGE); + return; + } + + File file = droppedFiles.get(0); + + if (file.getName().endsWith(".xml") || file.getName().endsWith(".igv")) { + handleOpen(file); + } else { + JOptionPane.showMessageDialog(null, + "Unsupported file type: " + file.getName(), + "Unsupported File", JOptionPane.WARNING_MESSAGE); + } + + dtde.dropComplete(true); + } else { + dtde.rejectDrop(); + } + } catch (HeadlessException | UnsupportedFlavorException | IOException ex) { + ex.printStackTrace(); + dtde.dropComplete(false); + } + } + } + /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is