forked from Coded/SIPRP
git-svn-id: https://svn.coded.pt/svn/SIPRP@1375 bb69d46d-e84e-40c8-a05a-06db0d633741
parent
fe1675b26c
commit
b1ebcb4c7f
@ -0,0 +1,71 @@
|
||||
package leaf.ui;
|
||||
|
||||
import java.awt.Insets;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
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 javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.TreeModel;
|
||||
import javax.swing.tree.TreePath;
|
||||
|
||||
import com.evolute.entity.evo.EvoDataException;
|
||||
import com.evolute.utils.error.ErrorLogger;
|
||||
|
||||
import siprp.data.outer.HsRiscoData;
|
||||
import siprp.data.outer.HsRiscoMedidaData;
|
||||
import siprp.data.outer.HsRiscoTemaData;
|
||||
|
||||
|
||||
public abstract class DraggableLeafTree extends LeafTree
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DraggableLeafTree( TreeModel model )
|
||||
{
|
||||
super(model);
|
||||
setDragEnabled( true );
|
||||
setAutoscrolls( true );
|
||||
setDropTarget();
|
||||
}
|
||||
|
||||
public abstract void setDropTarget( );
|
||||
|
||||
public void autoscroll( Point cursorLocation )
|
||||
{
|
||||
Insets insets = getAutoscrollInsets();
|
||||
Rectangle outer = getVisibleRect();
|
||||
Rectangle inner = new Rectangle( outer.x + insets.left, outer.y + insets.top, outer.width - ( insets.left+insets.right ), outer.height - ( insets.top+insets.bottom ) );
|
||||
|
||||
if ( !inner.contains(cursorLocation ) )
|
||||
{
|
||||
Rectangle scrollRect = new Rectangle( cursorLocation.x - insets.left, cursorLocation.y - insets.top, insets.left + insets.right, insets.top + insets.bottom );
|
||||
scrollRectToVisible(scrollRect);
|
||||
}
|
||||
}
|
||||
|
||||
public Insets getAutoscrollInsets()
|
||||
{
|
||||
return new Insets( 20, 20, 20, 20 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Object getSelectedObject()
|
||||
{
|
||||
DefaultMutableTreeNode node = getSelectedNode();
|
||||
return node == null ? null : node.getUserObject();
|
||||
}
|
||||
|
||||
public DefaultMutableTreeNode getSelectedNode()
|
||||
{
|
||||
TreePath path = getSelectionPath();
|
||||
return path == null ? null : (DefaultMutableTreeNode) path.getLastPathComponent();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,183 @@
|
||||
package siprp.higiene.gestao.riscos;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.awt.dnd.DropTargetDragEvent;
|
||||
import java.awt.dnd.DropTargetDropEvent;
|
||||
import java.awt.dnd.DropTargetEvent;
|
||||
import java.awt.dnd.DropTargetListener;
|
||||
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.TreePath;
|
||||
|
||||
import siprp.data.outer.HsRiscoData;
|
||||
import siprp.data.outer.HsRiscoMedidaData;
|
||||
import siprp.data.outer.HsRiscoTemaData;
|
||||
|
||||
import com.evolute.entity.evo.EvoDataException;
|
||||
import com.evolute.utils.error.ErrorLogger;
|
||||
|
||||
public class DnDRiscosListener implements DropTargetListener
|
||||
{
|
||||
private TreeRiscos tree;
|
||||
|
||||
private boolean drag = true;
|
||||
|
||||
public DnDRiscosListener( TreeRiscos tree )
|
||||
{
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void dragEnter(DropTargetDragEvent dtde)
|
||||
{
|
||||
DefaultMutableTreeNode node = ( ( DefaultMutableTreeNode ) tree.getSelectionPath().getParentPath().getLastPathComponent() );
|
||||
if ( node.getUserObject() == null )
|
||||
{
|
||||
|
||||
drag = false;
|
||||
dtde.rejectDrag();
|
||||
}
|
||||
else
|
||||
{
|
||||
drag = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dragExit(DropTargetEvent dte)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dragOver(DropTargetDragEvent dtde)
|
||||
{
|
||||
Point pt = dtde.getLocation();
|
||||
|
||||
tree.autoscroll( pt );
|
||||
|
||||
if( drag )
|
||||
{
|
||||
TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
|
||||
Object destObject = ( (DefaultMutableTreeNode )parentpath.getLastPathComponent() ).getUserObject();
|
||||
|
||||
if( tree.getSelectedMedida() != null && ( destObject instanceof HsRiscoTemaData ) )
|
||||
{
|
||||
tree.expandPath( parentpath );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(DropTargetDropEvent dtde)
|
||||
{
|
||||
System.out.println( "Drop" );
|
||||
|
||||
Point pt = dtde.getLocation();
|
||||
TreePath destinyPath = tree.getClosestPathForLocation(pt.x, pt.y);
|
||||
|
||||
Object destinyComponent = destinyPath.getLastPathComponent();
|
||||
System.out.println( destinyPath );
|
||||
System.out.println( );
|
||||
|
||||
if( destinyComponent instanceof DefaultMutableTreeNode )
|
||||
{
|
||||
System.out.println("moving node ...");
|
||||
DefaultMutableTreeNode destinyNode = ( DefaultMutableTreeNode ) destinyComponent;
|
||||
Object destinyObject = destinyNode.getUserObject();
|
||||
|
||||
TreePath parentPath = tree.getSelectionPath();
|
||||
Object parentComponent = parentPath.getLastPathComponent();
|
||||
|
||||
if( parentComponent instanceof DefaultMutableTreeNode )
|
||||
{
|
||||
DefaultMutableTreeNode parentNode = ( DefaultMutableTreeNode ) parentComponent;
|
||||
Object parentObject = parentNode.getUserObject();
|
||||
|
||||
if( destinyObject instanceof HsRiscoTemaData )
|
||||
{
|
||||
System.out.println("\tmove risco -> tema");
|
||||
HsRiscoTemaData destTema = ( HsRiscoTemaData ) destinyObject;
|
||||
|
||||
if ( parentObject instanceof HsRiscoData )
|
||||
{
|
||||
moveRisco( destinyNode, parentNode, parentObject, destTema );
|
||||
}
|
||||
}
|
||||
else if( destinyObject instanceof HsRiscoData )
|
||||
{
|
||||
if ( parentObject instanceof HsRiscoData )
|
||||
{
|
||||
destinyComponent = destinyPath.getParentPath().getLastPathComponent();
|
||||
destinyNode = ( DefaultMutableTreeNode )destinyComponent;
|
||||
destinyObject = destinyNode.getUserObject();
|
||||
HsRiscoTemaData destTema = ( HsRiscoTemaData ) destinyObject;
|
||||
|
||||
moveRisco( destinyNode, parentNode, parentObject, destTema );
|
||||
}
|
||||
}
|
||||
else if( destinyObject instanceof HsRiscoData )
|
||||
{
|
||||
System.out.println("\tmove medida -> risco");
|
||||
HsRiscoData destRisco = ( HsRiscoData ) destinyObject;
|
||||
|
||||
if ( parentObject instanceof HsRiscoMedidaData )
|
||||
{
|
||||
moveMedida( destinyNode, parentNode, parentObject, destRisco );
|
||||
}
|
||||
}
|
||||
else if( destinyObject instanceof HsRiscoMedidaData )
|
||||
{
|
||||
destinyComponent = destinyPath.getParentPath().getLastPathComponent();
|
||||
destinyNode = ( DefaultMutableTreeNode ) destinyComponent;
|
||||
destinyObject = destinyNode.getUserObject();
|
||||
HsRiscoData destRisco = ( HsRiscoData ) destinyObject;
|
||||
|
||||
if ( parentObject instanceof HsRiscoMedidaData )
|
||||
{
|
||||
moveMedida( destinyNode, parentNode, parentObject, destRisco );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropActionChanged(DropTargetDragEvent dtde)
|
||||
{
|
||||
}
|
||||
|
||||
private void moveMedida( DefaultMutableTreeNode destinyNode, DefaultMutableTreeNode parentNode, Object parentObject, HsRiscoData destRisco )
|
||||
{
|
||||
HsRiscoMedidaData parent = ( HsRiscoMedidaData ) parentObject;
|
||||
parent.setToRisco_id( destRisco );
|
||||
|
||||
try
|
||||
{
|
||||
parent.save();
|
||||
destinyNode.add( parentNode );
|
||||
tree.updateUI();
|
||||
}
|
||||
catch ( EvoDataException e )
|
||||
{
|
||||
ErrorLogger.logException( e );
|
||||
}
|
||||
}
|
||||
|
||||
private void moveRisco( DefaultMutableTreeNode destinyNode, DefaultMutableTreeNode parentNode, Object parentObject, HsRiscoTemaData destTema )
|
||||
{
|
||||
HsRiscoData parent = ( HsRiscoData ) parentObject;
|
||||
parent.setToTema_id( destTema );
|
||||
try
|
||||
{
|
||||
parent.save();
|
||||
destinyNode.add( parentNode );
|
||||
tree.updateUI();
|
||||
}
|
||||
catch ( EvoDataException e )
|
||||
{
|
||||
ErrorLogger.logException( e );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package siprp.higiene.gestao.riscos;
|
||||
|
||||
import java.awt.dnd.DropTarget;
|
||||
|
||||
import javax.swing.tree.TreeModel;
|
||||
|
||||
import siprp.data.outer.HsRiscoData;
|
||||
import siprp.data.outer.HsRiscoMedidaData;
|
||||
import siprp.data.outer.HsRiscoTemaData;
|
||||
|
||||
import leaf.ui.DraggableLeafTree;
|
||||
|
||||
public class TreeRiscos extends DraggableLeafTree
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TreeRiscos( TreeModel model )
|
||||
{
|
||||
super( model );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDropTarget()
|
||||
{
|
||||
setDropTarget( new DropTarget( this , new DnDRiscosListener( this ) ) );
|
||||
}
|
||||
|
||||
public HsRiscoTemaData getSelectedTema()
|
||||
{
|
||||
Object object = getSelectedObject();
|
||||
return object == null ? null : ((object instanceof HsRiscoTemaData) ? (HsRiscoTemaData) object : null);
|
||||
}
|
||||
|
||||
public HsRiscoData getSelectedRisco()
|
||||
{
|
||||
Object object = getSelectedObject();
|
||||
return object == null ? null : ((object instanceof HsRiscoData) ? (HsRiscoData) object : null);
|
||||
}
|
||||
|
||||
public HsRiscoMedidaData getSelectedMedida()
|
||||
{
|
||||
Object object = getSelectedObject();
|
||||
return object == null ? null : ((object instanceof HsRiscoMedidaData) ? (HsRiscoMedidaData) object : null);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue