/* (@)JInternalFrameDnDTestGui.java */
/* Copyright 2010 Sebastian Haufe
* 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
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* 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.ebenius;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.Arrays;
import javax.swing.*;
/**
* @author Sebastian Haufe
*/
public class JInternalFrameDnDTestGui {
/** Creates the GUI. Call on EDT, only! */
static void createAndShowGui() {
final JDesktopPane desktopPane = new JDesktopPane();
final DesktopManager desktopManager = desktopPane.getDesktopManager();
desktopPane.setPreferredSize(new Dimension(640, 480));
desktopPane.setDesktopManager(new DesktopManager() {
public void setBoundsForFrame(
JComponent f,
int newX,
int newY,
int newWidth,
int newHeight) {
desktopManager.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
}
public void resizeFrame(
JComponent f,
int newX,
int newY,
int newWidth,
int newHeight) {
desktopManager.resizeFrame(f, newX, newY, newWidth, newHeight);
}
public void openFrame(JInternalFrame f) {
desktopManager.openFrame(f);
}
public void minimizeFrame(JInternalFrame f) {
desktopManager.minimizeFrame(f);
}
public void maximizeFrame(JInternalFrame f) {
desktopManager.maximizeFrame(f);
}
public void iconifyFrame(JInternalFrame f) {
desktopManager.iconifyFrame(f);
}
public void endResizingFrame(JComponent f) {
desktopManager.endResizingFrame(f);
}
public void endDraggingFrame(JComponent f) {
desktopManager.endDraggingFrame(f);
}
public void dragFrame(JComponent f, int newX, int newY) {
desktopManager.dragFrame(f, newX, newY);
final JDesktopPane desktopPane =
(JDesktopPane) SwingUtilities.getAncestorOfClass(
JDesktopPane.class, f);
if (desktopPane != null) {
final TransferHandler tf = desktopPane.getTransferHandler();
if (tf != null) {
final AWTEvent currentEvent = EventQueue.getCurrentEvent();
final MouseEvent me =
currentEvent instanceof MouseEvent
? (MouseEvent) currentEvent
: null;
if (me != null) {
final Point point =
SwingUtilities.convertPoint(me.getComponent(), me
.getPoint(), desktopPane);
if (point.x < 0
|| point.y < 0
|| point.x > desktopPane.getWidth()
|| point.y > desktopPane.getHeight()) {
tf.exportAsDrag(desktopPane, me, TransferHandler.LINK);
}
}
}
}
}
public void deiconifyFrame(JInternalFrame f) {
desktopManager.deiconifyFrame(f);
}
public void deactivateFrame(JInternalFrame f) {
desktopManager.deactivateFrame(f);
}
public void closeFrame(JInternalFrame f) {
desktopManager.closeFrame(f);
}
public void beginResizingFrame(JComponent f, int direction) {
desktopManager.beginResizingFrame(f, direction);
}
public void beginDraggingFrame(JComponent f) {
desktopManager.beginDraggingFrame(f);
}
public void activateFrame(JInternalFrame f) {
desktopManager.activateFrame(f);
}
});
desktopPane.setTransferHandler(new TransferHandler("selectedFrame") {
/** Serial version UID */
private static final long serialVersionUID = 1L;
@Override
public int getSourceActions(JComponent c) {
return c instanceof JDesktopPane ? TransferHandler.LINK : super
.getSourceActions(c);
}
@Override
public void exportAsDrag(JComponent comp, InputEvent e, int action) {
super.exportAsDrag(comp, e, action);
}
});
/* internal frame */
final JInternalFrame internalFrame =
new JInternalFrame("Huhu", true, true, true, true);
internalFrame.getContentPane().add(new JScrollPane(new JTable(9, 3)));
internalFrame.pack();
desktopPane.add(internalFrame);
internalFrame.setVisible(true);
try {
internalFrame.setSelected(true);
} catch (PropertyVetoException ex) {}
final JLabel dropContainer = new JLabel("Drop Frame here!");
dropContainer.setTransferHandler(new TransferHandler() {
/** Serial version UID */
private static final long serialVersionUID = 1L;
private DataFlavor df =
new DataFlavor("application/x-java-jvm-local-objectref;"
+ "class=javax.swing.JInternalFrame", "Internal Frame");
@Override
public boolean canImport(TransferSupport support) {
System.out.println(df);
System.out.println(Arrays.asList(support.getDataFlavors()));
return support.isDataFlavorSupported(df) && support.isDrop();
}
@Override
public boolean importData(TransferSupport support) {
try {
((JLabel) support.getComponent()).setText(((JInternalFrame) support
.getTransferable().getTransferData(df)).getTitle()
+ " dropped.");
return true;
} catch (Exception ex) {
ex.printStackTrace();
((JLabel) support.getComponent()).setText(ex.getMessage());
return false;
}
}
});
/* test ui */
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(desktopPane);
contentPane.add(dropContainer, BorderLayout.LINE_END);
final JFrame f = new JFrame("Test Frame: JInternalFrameDnDTestGui"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
/** @param args ignored */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}