1 THANK YOU 4 your help ! hm I changed yout test example with my classes and now it makes the same problems like my application there are classes that i use : i think the problem is in the EditorLayout or im trying to do something wrong with EditorDockable i dont know …
your main that i have changed :
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
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.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TooManyListenersException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.DefaultMultipleCDockable;
import bibliothek.gui.dock.common.MultipleCDockableFactory;
import bibliothek.gui.dock.common.MultipleCDockableLayout;
import bibliothek.gui.dock.control.relocator.DockRelocatorEvent;
import bibliothek.gui.dock.control.relocator.VetoableDockRelocatorListener;
import bibliothek.util.xml.XElement;
public class DragAndDropTest {
private static Image img=ImageHelper.loadImg("Blue-Display.png");
public static void main( String[] args ){
JFrame frame = new JFrame( "Test" );
CControl control = new CControl( frame );
frame.add( control.getContentArea() );
EditorFactory factory = new EditorFactory();
control.addMultipleDockableFactory( "editor", factory );
ArrayList <ImageComponent>pc =new ArrayList <ImageComponent>();
pc.add(new ImageComponent(null,img,null));
EditorDockable left= new EditorDockable( factory, new EditorLayout( "left" , pc ,new RaumPanel() ) );
EditorDockable right= new EditorDockable( factory, new EditorLayout( "right" , null ,new RaumPanel() ) );
control.addDockable( left );
control.addDockable( right );
left.setVisible( true );
left.setTitleText( "Left" );
right.setLocation( CLocation.base().normalEast( 0.5 ) );
right.setVisible( true );
right.setTitleText( "Right" );
DragSource dragSource = new DragSource();
DragDrop dndListener = new DragDrop() ;
initDockable( left, dragSource, dndListener );
initDockable( right, dragSource, dndListener );
frame.setBounds( 20, 20, 500, 400 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
public static void initDockable(EditorDockable editor, DragSource dragSource, DragDrop dndListener){
DropTargetDockable target = new DropTargetDockable(editor, DnDConstants.ACTION_MOVE, dndListener);
dragSource.createDefaultDragGestureRecognizer( editor.getContentPane().getComponent(0), DnDConstants.ACTION_MOVE, dndListener );
}
public static class DropTargetDockable extends DropTarget {
private EditorDockable container;
public DropTargetDockable(EditorDockable container, int ops, DropTargetListener dtl ){
setComponent( container.getContentPane() );
this.container=container;
setDefaultActions(ops);
try {
addDropTargetListener( dtl);
} catch (TooManyListenersException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public EditorDockable getDockable(){
return this.container;
}
}
}```
than my EditorDockable
```import javax.swing.JTextArea;
import bibliothek.gui.dock.common.DefaultMultipleCDockable;
class EditorDockable extends DefaultMultipleCDockable{
private EditorLayout itsLayout=null;
public EditorDockable( EditorFactory factory, EditorLayout layout ){
/* it is mandatory to set the factory, the EditorDockable cannot be created
* without it. */
super( factory );
this.itsLayout=layout;
/* and then we just set up the editor */
setTitleText( layout.getFileName() );
if(layout.getFileContent()!=null){
for(ImageComponent e: layout.getFileContent()){
e.setParentDockable(this);
add(e);
}
}
add(layout.getBackground());
}
/* This convenient method allows us to grab the entire content of this dockable
* in one step. */
public EditorLayout getLayout(){
return itsLayout;
}
}```
EditorLayout
```import java.awt.Color;
import java.awt.Cursor;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JPanel;
import bibliothek.gui.dock.common.MultipleCDockableLayout;
import bibliothek.util.xml.XElement;
class EditorLayout implements MultipleCDockableLayout {
private String fileName=null;
private JPanel background=null;
private ArrayList <ImageComponent>pc;
public EditorLayout(){
// nothing
}
public EditorLayout( String fileName, ArrayList <ImageComponent>pc, JPanel background ){
System.out.println("newEditorLayout + "+ background +" name "+fileName);
this.fileName = fileName;
this.pc=pc;
this.background=background;
}
public String getFileName(){
return fileName;
}
public ArrayList<ImageComponent> getFileContent(){
return pc;
}
public JPanel getBackground(){
return background;
}
@Override
public boolean equals( Object obj ){
if( this == obj ){
return true;
}
if( obj == null ){
return false;
}
if( getClass() != obj.getClass() ){
return false;
}
EditorLayout other = (EditorLayout) obj;
return equals( background, other.background ) &&
equals( fileName, other.fileName ); /*&&
/*equals( fileContent, other.fileContent );*/
}
private boolean equals( Object a, Object b ){
if( a == null ){
return b == null;
}
else{
return a.equals( b );
}
}
public void readStream( DataInputStream in ) throws IOException{
fileName = in.readUTF();
}
public void readXML( XElement element ){
fileName = element.getElement( "name" ).getString();
}
public void writeStream( DataOutputStream out ) throws IOException{
out.writeUTF( fileName );
}
public void writeXML( XElement element ){
element.addElement( "name" ).setString( fileName );
}
public EditorLayout clone(){
System.out.println("clone layout");
return this;
}
}
factor
/* This factory builds a link between EditorDockable and EditorLayout */
class EditorFactory implements MultipleCDockableFactory<EditorDockable, EditorLayout>{
/* An empty layout is required to read a layout from an XML file or from a byte stream */
public EditorLayout create(){
return new EditorLayout();
}
/* An optional method allowing to reuse 'dockable' when loading a new layout */
public boolean match( EditorDockable dockable, EditorLayout layout ){
System.out.println("factory");
return dockable.getLayout().equals( layout );
}
/* Called when applying a stored layout */
public EditorDockable read( EditorLayout layout ){
System.out.println("factory");
return new EditorDockable( this, layout );
}
/* Called when storing the current layout */
public EditorLayout write( EditorDockable dockable ){
System.out.println("factory");
return dockable.getLayout();
}
}```
ImageComponent class:
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ImageComponent extends Component {
private boolean dragging=false;
private Image image;
private Point offset;
private DataComputer itsPC=null;
public DataComputer getItsPC() {
return itsPC;
}
public void setItsPC(DataComputer itsPC) {
this.itsPC = itsPC;
}
private Control dataControl=null;
private EditorDockable parentDockable=null;
public ImageComponent(DataComputer PC, Image img, final Control dataControl){
this.dataControl=dataControl;
this.itsPC=PC;
//this.setBounds((int)PC.getItsPosition().getX() , (int)PC.getItsPosition().getY(), 50, 50);
this.setBounds(50,50,50,50);
this.image=img;
setVisible(true);
this.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent e) {
/* if(dragging)
{
Rectangle r = getBounds();
r.x += e.getX() - offset.x;
r.y += e.getY() - offset.y;
setBounds(r);
}
*/
}
public void mouseMoved(MouseEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
});
this.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
//beim Doppelclick ?ffne die PC Eigenschaften
if(e.getClickCount()==2 ){
try {
dataControl.showProperties(ImageComponent.this.itsPC);
} catch (CloneNotSupportedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
offset = e.getPoint();
dragging = true;
}
public void mouseReleased(MouseEvent arg0) {
dragging = false;
//ImageComponent.this.itsPC.setItsPosition(ImageComponent.this.getLocation());
}
});
}
public void paint (Graphics g) {
super.paint(g);
// g.drawImage(image, 0,0, 50,50,null);
g.setColor( Color.RED );
g.fillOval( 0, 0, 20, 20 );
}
public void updateComponent(){
this.setBounds((int)this.itsPC.getItsPosition().getX() , (int)this.itsPC.getItsPosition().getY(), 50, 50);
switch(itsPC.getItsStauts()){
case NEU: image=ImageHelper.loadImg("Blue-Display.png");
break;
case INTAKT: image=ImageHelper.loadImg("Green-Display.png");
break;
case DEFECT: image=ImageHelper.loadImg("Red-Display.png");
break;
case GEMELDET: image=ImageHelper.loadImg("Yellow-Display.png");
break;
}
}
public EditorDockable getParentDockable() {
return parentDockable;
}
public void setParentDockable(EditorDockable parentDockable) {
this.parentDockable = parentDockable;
}
}```
DnD class:
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.JButton;
public class DragDrop implements DragGestureListener,
DragSourceListener,
DropTargetListener, Transferable {
static final DataFlavor[] supportedFlavors = {null};
static {
try {
supportedFlavors[0] = new
DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Object object;
// Transferable methods.
public Object getTransferData(DataFlavor flavor) {
if (flavor.isMimeTypeEqual
(DataFlavor.javaJVMLocalObjectMimeType)) {
return object;
} else {
return null;
}
}
public DataFlavor[] getTransferDataFlavors() {
return supportedFlavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.isMimeTypeEqual
(DataFlavor.javaJVMLocalObjectMimeType);
}
// DragGestureListener method.
public void dragGestureRecognized(DragGestureEvent ev) {
System.out.println("start dnd "+ev.getSource());
ev.startDrag(null, this, this);
}
// DragSourceListener methods.
public void dragDropEnd(DragSourceDropEvent ev) {
}
public void dragEnter(DragSourceDragEvent ev) {
}
public void dragExit(DragSourceEvent ev) {
}
public void dragOver(DragSourceDragEvent ev) {
object = ev.getSource();
}
public void dropActionChanged(DragSourceDragEvent ev) {
}
// DropTargetListener methods.
public void dragEnter(DropTargetDragEvent ev) {
}
public void dragExit(DropTargetEvent ev) {
}
public void dragOver(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}
public void dropActionChanged(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}
void dropTargetDrag(DropTargetDragEvent ev) {
ev.acceptDrag(ev.getDropAction());
}
public void drop(DropTargetDropEvent ev) {
ev.acceptDrop(ev.getDropAction());
try {
Object target = ev.getSource();
Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);
//ImageComponet
Component component = ((DragSourceContext)source).getComponent();
ImageComponent imgComponent=(ImageComponent)component;
EditorDockable oldEditor = imgComponent.getParentDockable();
Container container = (Container) ((DropTarget)
target).getComponent();
container.add(imgComponent);
oldEditor.getContentPane().validate();
oldEditor.getContentPane().repaint();
container.validate();
container.repaint();
}
catch (Exception ex) {
ex.printStackTrace();
}
ev.dropComplete(true);
}
}```