Package jsynoptic.ui

Source Code of jsynoptic.ui.ShapesContainer$ShapesComponentDropListener

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: ShapesContainer.java,v 1.42 2009/01/19 17:44:32 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/

package jsynoptic.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.datatransfer.DataFlavor;
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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.CompoundEdit;

import jsynoptic.base.ContextualActionProvider;
import jsynoptic.base.DataSourceConsumer;
import jsynoptic.base.Linkable;
import jsynoptic.base.SelectionContextualActionProvider;
import jsynoptic.builtin.Abstract1DShape;
import simtools.data.DataSource;
import simtools.diagram.DiagramClipboard;
import simtools.diagram.DiagramComponent;
import simtools.diagram.DiagramParameters;
import simtools.diagram.DiagramSelection;
import simtools.diagram.Element;
import simtools.diagram.ElementSelection;
import simtools.shapes.AbstractShape;
import simtools.ui.BasicMessageWriter;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;

/**
* We're interested in extending the inner class, so extend the shapes
* container too.
*
* @author Nicolas Brodu
*
* @version 1.0 2001
*/

public class ShapesContainer extends simtools.shapes.ShapesContainer {

  static final long serialVersionUID = 1182853682573239711L;

  public static BasicMessageWriter messageWriter = ResourceFinder.getMessages(ShapesContainer.class);
  public static MenuResourceBundle resources = ResourceFinder.getMenu(ShapesContainer.class);
   
  static JColorChooser colorChooserPane = null;

  static JDialog colorChooserDialog = null;

  static class ColorListener implements ActionListener {
    Color currentColor = null;

    public void actionPerformed(ActionEvent e) {
      currentColor = colorChooserPane.getColor();
    }
  }

  static ColorListener colorListener = null;

  static Color showColorDialog(Color initialColor) {
    if (JSynoptic.gui == null) {
      return null;
    }
    if (colorChooserPane == null) {
      colorChooserPane = new JColorChooser(initialColor);
      colorListener = new ColorListener();
      colorChooserDialog = JColorChooser.createDialog(JSynoptic.gui.getOwner(),
          resources.getStringValue("backgroundColorTitle"),
          true, colorChooserPane, colorListener,null);
    } else {
      colorChooserPane.setColor(initialColor);
    }
    colorChooserDialog.show();
    return colorListener.currentColor;
  }

  static ShapeCreator getSelectedShape(){
    if(JSynoptic.gui!=null){
      AbstractList res=JSynoptic.gui.getSelectionList();
      if(res!=null){
        Iterator it=res.iterator();
        while(it.hasNext()){
          Object o=it.next();
          if(o instanceof ShapeCreator){
            return (ShapeCreator)o;
          }
        }
      }
    }
    return null;
  }
 
  public static final DataFlavor SHAPE_FLAVOR=new DataFlavor(ShapeCreator.class, "SHAPE");
 
  static DataSource getSelectedSource(){
    if(JSynoptic.gui!=null){
      AbstractList res=JSynoptic.gui.getSelectionList();
      if(res!=null){
        Iterator it=res.iterator();
        while(it.hasNext()){
          Object o=it.next();
          if(o instanceof DataSource){
            return (DataSource)o;
          }
        }
      }
    }
    return null;
  }

  public ShapesContainer(String name) {
    super(name);
  }

  /* (non-Javadoc)
   * @see simtools.shapes.ShapesContainer#createShapesComponent()
   */
  protected ShapesDiagramComponent createShapesComponent() {
    ShapesComponent ret= new ShapesComponent(_diagParam, this);
    if(JSynoptic.gui!=null && !JSynoptic.gui.canEdit()){
      ret.disableEditing(true);
    }
    return ret;
  }

  public class ShapesComponent extends simtools.shapes.ShapesContainer.ShapesDiagramComponent {

    private static final long serialVersionUID = 6144206622124284681L;

    ContextualActionProvider lastProviderOver;
   

    /* (non-Javadoc)
     * @see simtools.shapes.ShapesContainer.ShapesComponent#drawBounds(java.awt.Graphics2D, simtools.diagram.ShapeSelection)
     */
    protected void drawBounds(Graphics2D g2, ElementSelection selection) {
      Color c = g2.getColor();
     
      Shape shape = selection.getShape();
     
      if (shape instanceof Linkable) {
        String link = ((Linkable)shape).getLink();
        if ((link!=null) && (!link.equals(""))) {
          JSynoptic.setStatus(messageWriter.print1args("followLink",link));
          g2.setColor(Color.red);
        }
      }
      super.drawBounds(g2, selection);
      g2.setColor(c);
    }
   
    public ShapesComponent(DiagramParameters param, ShapesContainer sc){
      super(param, sc);
      lastProviderOver = null;

      setDropTarget(
        new DropTarget(
          this,
          new ShapesComponentDropListener(this)));
    }
   
    // May be called by external classes
    public JPopupMenu doPopup(int x, int y, int context) {
      double selX=((double)x/_param.scale)-_param.xmargin;
      double selY=((double)y/_param.scale)-_param.ymargin;
      if (getElementContainer().getSelection().isEmpty()&& getSelectedSource()!=null) {
        if (getElementContainer().getSelection().selectPoint((int)selX,(int)selY)) {
          repaint();
        }
      }
      if (getElementContainer().getSelection().isEmpty()&&(JSynoptic.gui!=null)){
        ShapeCreator o = getSelectedShape();
        if (o==null) return null;
        return new NewShapePopup(ShapesContainer.this, o, selX, selY);
      }
      if(getElementContainer().getSelection().isEmpty()){
        return null;
      }
      return new ActionPopup(this, selX, selY, getSelectedSource(), context);
    }
   
    protected boolean addCollectiveActions(JPopupMenu menu, int x, int y, int context){
      LinkedHashMap pactions=null;
      for(int i=0;i<getElementContainer().getSelection().getSelectedElements().size();i++){
        Object o=getElementContainer().getSelection().getSelectedElements().get(i);
        if (o instanceof SelectionContextualActionProvider) {
          SelectionContextualActionProvider provider = (SelectionContextualActionProvider)o;
          boolean locked = JSynoptic.gui==null ? false : JSynoptic.gui.getLockedShapes().contains(provider);
          if(locked){
            continue;
          }
          LinkedHashMap cactions = provider.getCollectiveActions(getElementContainer().getSelection(), x, y, getSelectedSource(), context);
          if (cactions != null){
                       if(pactions==null){
                            pactions=cactions;
                       
                        } else{
                            HashSet h=new HashSet(cactions.keySet());
                            h.retainAll(pactions.keySet());
                            Iterator it=pactions.keySet().iterator();
                            while(it.hasNext()){
                                if(!h.contains(it.next())){
                                    it.remove();
                                }
                            }
                        }
          }
        } else{
          // one element in the selection does
          // not provide collective actions
          return false;
        }
      }
      if(pactions==null || pactions.isEmpty()){
        // no common actions
        return false;
      }
      final LinkedHashMap commonActions=pactions;
      Iterator it=commonActions.keySet().iterator();
      while(it.hasNext()){
        final String actionName =(String)it.next();
        Icon icon =(commonActions.get(actionName) instanceof Icon)(Icon)commonActions.get(actionName) : null;
        JMenuItem jmi = ActionPopup.createMenuItem(menu, actionName, icon);
        jmi.addActionListener(new CollectiveActionListener(x, y, getSelectedSource(), actionName))
      }
      return true;
    }

    protected class CollectiveActionListener implements ActionListener {
        protected double actionX, actionY;
        protected Object actionTarget;
        protected String actionName;

        public CollectiveActionListener(
                double actionX,
                double actionY,
                Object actionTarget,
                String actionName
        ) {
            super();
            this.actionX = actionX;
            this.actionY = actionY;
            this.actionTarget = actionTarget;
            this.actionName = actionName;
        }

        public void actionPerformed(ActionEvent e) {
            // The first selected element is in charge of applying the collective action to the selection
            Shape s0 = getElementContainer().getSelection().getShape(0);
            if (s0 instanceof SelectionContextualActionProvider){
                CompoundEdit ce=new CompoundEdit();
                ((SelectionContextualActionProvider)getElementContainer().getSelection().getSelectedElements().get(0)).doCollectiveAction(getElementContainer().getSelection(), actionX, actionY, actionTarget, actionName, ce);
                ce.end();

                if (ce.isSignificant()) {
                    fireUndoableEditUpdate(new UndoableEditEvent(ShapesContainer.this, ce));
                }
            }
        }
    }
   
    // Parent's MouseListener extension
    public void mousePressed(MouseEvent e) {
      if(!canEdit){
        return;
      }
      ContextualActionProvider p = null;
      double selX=((double)e.getX()/_param.scale)-_param.xmargin;
      double selY=((double)e.getY()/_param.scale)-_param.ymargin;
      for (int i=ShapesContainer.this.size()-1;i>=0; i--) { // outer Vector API
        Object o = get(i);
        if (! (o instanceof Shape)) continue;
        if (! (o instanceof ContextualActionProvider)) continue;
        if (((Shape)o).contains(selX,selY)) {
          p = (ContextualActionProvider)o;
          break;
        }
      }
      if ((p!=null) && (lastProviderOver!=null)) {
        String[] s = lastProviderOver.getActions(selX,selY,e,ContextualActionProvider.MOUSE_PRESSED_CONTEXT);
        boolean actionDone = false;
        if (s!=null) {
          actionDone = true; // they must all be true, & combining
          for (int i = 0; i<s.length; ++i) {
            actionDone &= lastProviderOver.doAction(selX,selY,getElementContainer().getSelection(),s[i], null);
          }
        }
        if (actionDone) return; // stop there
      }
      if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
        JPopupMenu popup = doPopup(e.getX(), e.getY(), ContextualActionProvider.EDITOR_CONTEXT);
        if (popup==null) popup = new JPopupMenu();
        if (popup.getComponentCount()>0){
          popup.addSeparator();
        }
        boolean needSep=addCollectiveActions(popup, e.getX(), e.getY(), ContextualActionProvider.EDITOR_CONTEXT);
        if(getContextualDrawing()==null){
          if(needSep){
            popup.addSeparator();
          }
          if(JSynoptic.gui!=null)
            JSynoptic.gui.addEditMenu(popup);
        }
        if(getElementContainer().getSelection().isEmpty()){
          // no selection add background color menu
          popup.addSeparator();
          popup.add(new JMenuItem(new AbstractAction(
              resources.getStringValue("backgroundColorAction")) {
            public void actionPerformed(ActionEvent e) {
              Color c=showColorDialog(ShapesContainer.this._diagParam.backgr);
              if(c!=null){
                ShapesContainer.this._diagParam.backgr=c;
                ShapesContainer.this._diagParam.forgr=DiagramParameters.invertColor(c);
                repaint();
              }
            }
          }));
        }
        popup.show(this, e.getX(), e.getY());
      }
      super.mousePressed(e);
    }
   
    // Parent's MouseListener extension
    public void mouseExited(MouseEvent e) {
      if(!canEdit){
        return;
      }
      double selX=((double)e.getX()/_param.scale)-_param.xmargin;
      double selY=((double)e.getY()/_param.scale)-_param.ymargin;

      if (lastProviderOver != null) {
        if (JSynoptic.gui!=null && JSynoptic.gui.getLockedShapes().contains(lastProviderOver)) return;
        String[] s = lastProviderOver.getActions(selX,selY,getElementContainer().getSelection(),ContextualActionProvider.MOUSE_OUT_CONTEXT);
        if (s!=null) {
          for (int i = 0; i<s.length; ++i) {
            lastProviderOver.doAction(selX,selY,getElementContainer().getSelection(),s[i], null);
          }
        }
      }
      lastProviderOver = null;
      super.mouseExited(e);
    }
   
    // Parent's MouseMotionListener extension
    public void mouseMoved(MouseEvent e) {
      if(!canEdit){
        return;
      }
      double selX=((double)e.getX()/_param.scale)-_param.xmargin;
      double selY=((double)e.getY()/_param.scale)-_param.ymargin;
      ContextualActionProvider p = null;
      boolean isLink = false;
      for (int i=ShapesContainer.this.size()-1;i>=0; i--) { // outer Vector API
        Object o = get(i);
        if (! (o instanceof Shape)) continue;
        if (((Shape)o).contains(selX,selY)) {
          if (o instanceof Linkable) {
            String l = ((Linkable)o).getLink();
            if ((l!=null) && !l.equals("")) isLink = true;
          }
          if (! (o instanceof ContextualActionProvider)) continue;
          p = (ContextualActionProvider)o;
          break;
        }
      }
      if ((lastProviderOver != p) && (lastProviderOver != null)) {
        updateCursor();
        if (JSynoptic.gui!=null && !JSynoptic.gui.getLockedShapes().contains(lastProviderOver)) {
          String[] s = lastProviderOver.getActions(selX,selY,getElementContainer().getSelection(),ContextualActionProvider.MOUSE_OUT_CONTEXT);
          if (s!=null) {
            for (int i = 0; i<s.length; ++i) {
              lastProviderOver.doAction(selX,selY,getElementContainer().getSelection(),s[i], null);
            }
          }
        }
      }
      if (p!=null) {
        if (JSynoptic.gui!=null && JSynoptic.gui.getLockedShapes().contains(p)) {
          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        } else {
          String[] s = p.getActions(selX,selY,getElementContainer().getSelection(),ContextualActionProvider.MOUSE_OVER_CONTEXT);
          if (s!=null) {
            for (int i = 0; i<s.length; ++i) {
              p.doAction(selX,selY,getElementContainer().getSelection(),s[i], null);
            }
          }
        }
      }
      if (isLink) {
          setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      }
      else if (p==null) {
          updateCursor();
      }
      lastProviderOver = p;
      super.mouseMoved(e);
    }

    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent e) {
      // Monitor doubleclicks on links
      if (e.getClickCount()==2 &&
          (e.getModifiers()&MouseEvent.BUTTON1_MASK)==MouseEvent.BUTTON1_MASK) {
        double selX=((double)e.getX()/_param.scale)-_param.xmargin;
        double selY=((double)e.getY()/_param.scale)-_param.ymargin;
       
        for (int i=ShapesContainer.this.size()-1;i>=0; i--) { // outer Vector API
          Object o = get(i);
          if (! (o instanceof Shape)) continue;
          Shape s=(Shape)o;
          if (s.contains(selX,selY)) {
            if((getElementContainer().getSelection().getShapeCount()!=1) || !getElementContainer().getSelection().isSelected(s)){
              getElementContainer().getSelection().selectPoint((int)selX,(int)selY);
              repaint();
              if(getElementContainer().getSelection().getSelectedShape(s)==null){
                continue; // should not occur but ...
              }
            }

            // If we are in edit mode, display the properties panel
            // of the shape below the mouse pointer
            if(canEdit
            && o instanceof ContextualActionProvider) {
              ((ContextualActionProvider)o).doAction(selX, selY, o, Abstract1DShape.resources.getString("properties"), null);
           
            } else if (o instanceof Linkable) {
              String l = ((Linkable)o).getLink();
              if ((l!=null) && !l.equals("")) {
                if(JSynoptic.gui!=null){
                  JSynoptic.gui.open(new File(l));
                }
              }
            }
            break;
          }
        }
      }
      else{
        // Or pass the event to the parent
        super.mouseClicked(e);
      }
    }
   
    // Parent's ActionListener extension
    public void actionPerformed(ActionEvent e){
      if(canEdit && getContextualDrawing()!=null){
        return; // disable all the actions
     
      String cmd=e.getActionCommand();

      // On enter, follow first link found, if any
      if ((cmd.equals("enter"))) {
        Vector v = getSelection();
        String link = null;
        if (v!=null) for (Iterator it = v.iterator(); it.hasNext(); ) {
          Object o = it.next();
          if (o instanceof Linkable) {
            String lnk = ((Linkable)o).getLink();
            if ((lnk!=null) && (!lnk.equals(""))) {
              link = lnk;
            }
          }
        }
        if ((JSynoptic.gui!=null) && (link!=null))
          JSynoptic.gui.open(new File(link));
      }

      // In case of a locked shape in selection, disable those actions
      if ((cmd.equals("c") || cmd.equals("x") || cmd.equals("d")))
        if ((getElementContainer().getSelection()!=null) && (JSynoptic.gui!=null))
          for (Iterator it = JSynoptic.gui.getLockedShapes().iterator(); it.hasNext();)
            if (getElementContainer().getSelection().isSelected((Shape)it.next()))
              return;

      super.actionPerformed(e);
    }

    /* (non-Javadoc)
     * @see simtools.diagram.DiagramComponent#registerActions()
     */
    protected void registerActions() {
      super.registerActions();
      KeyStroke ks;
      ks=KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_MASK);
      getInputMap().remove(ks);// don't use ctrlS to select connectors
      ks=KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK);
      getInputMap().remove(ks);// don't use ctrlN to create component
      ks=KeyStroke.getKeyStroke(KeyEvent.VK_SPACE ,KeyEvent.CTRL_MASK);
      registerKeyboardAction(this, "n", ks, WHEN_FOCUSED); // use ctrl SPACE
     
      ks=KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
      registerKeyboardAction(this, "enter", ks, WHEN_FOCUSED);
    }


    /* (non-Javadoc)
     * @see simtools.diagram.DiagramComponent#executeNewElement()
     */
    public void executeNewElement(){
      ShapeCreator o = getSelectedShape();
      if (o==null) return;
      ShapeCreator sc=(ShapeCreator)o;
      Vector v1=new Vector();
      v1.add(sc.create());
      DiagramClipboard.set(v1);
      executeClipboardPaste();
    }
   
    public void setDragPoint(Point pt){
      _dragPoint.x=(int)((double)pt.x/_param.scale)-_param.xmargin;
      _dragPoint.y=(int)((double)pt.y/_param.scale)-_param.ymargin;
      repaint();
    }
   
    public void drop(Point pt){
      int x=(int)((double)pt.x/_param.scale)-_param.xmargin;
      int y=(int)((double)pt.y/_param.scale)-_param.ymargin;
      Vector v=DiagramClipboard.get();
      if(v.size()>0){
        if(v.get(0) instanceof AbstractShape){
          AbstractShape as=(AbstractShape)v.get(0);
          as.translate(x,y);
          ShapesContainer.this.addElement(as);
        }
      }
      DiagramClipboard.get().clear();
      _dragging=false;
     
      updateCursor();
      repaint();
    }
   
    public void cancelDrop(){
      DiagramClipboard.get().clear();
      _dragging=false;
      repaint();
    }
 
 
    /* (non-Javadoc)
     * @see simtools.shapes.ShapesContainer.ShapesComponent#fireUndoableEditUpdate(javax.swing.event.UndoableEditEvent)
     */
    public void fireUndoableEditUpdate(UndoableEditEvent event) {
      //  Set component modification status as modified
      if ((JSynoptic.gui!=null) && (!hasBeenModified)){
        setName("*" + getName());
        JSynoptic.gui.updateComponentName(this);
      }
      super.fireUndoableEditUpdate(event);
    }
 
  }

  public class ShapesComponentDropListener implements DropTargetListener{

    final ShapesComponent comp;
    Element as;
    DataSource data;
    ShapesComponentDropListener(ShapesComponent c){
      comp=c;
      as=null;
      data=null;
    }
   
    boolean isValidDataSourceDrag(Point p){
      DiagramSelection ds=comp.getDiagramSelection();
      Shape s=null;
      int x=(int)((double)p.x/comp.getParameters().scale)-
      comp.getParameters().xmargin;
      int y=(int)((double)p.y/comp.getParameters().scale)-
      comp.getParameters().ymargin;
      if(ds.isEmpty()){
        s=comp.getShapeAt(x,y);
      }
      else{
        s=ds.getSelectedShapeAt(x,y);
      }
      if((s!=null) && (s instanceof DataSourceConsumer)){
        if(((DataSourceConsumer)s).canAddDataSource(data)){
          return true;
        }
      }
      return false;
    }
   
    Object getDragObject(DropTargetEvent dtde){
            try {
                // since Java 1.5 getTansferable method is provided by both
                // DropTargetDragEvent and DropTargetDropEvent
                Method m = dtde.getClass().getMethod("getTransferable", new Class[0]);
                Object o = m.invoke(dtde, null);
               
              m = o.getClass().getMethod("getTransferDataFlavors", new Class[0]);
              Object dfso=m.invoke(o, null);
              DataFlavor[] dfs=(DataFlavor[])dfso;
              if(dfs.length==0){
                return null;
              }
              Class[] cargs={DataFlavor.class};
                m = o.getClass().getMethod("getTransferData", cargs);
                Object[] args={dfs[0]};
                o = m.invoke(o, args);
                if (o != null) {
                    return o;
                }
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            return null;
    }
   
      ShapeCreator getDragShape(DropTargetEvent dtde, DataFlavor df) {
            // try with java1.4 method
            // TODO remove and clean when target updated
            String v=df.getHumanPresentableName();
            ShapeCreator o=getSelectedShape();
            if(o!=null && df.getRepresentationClass().equals(ShapeCreator.class)){
                if(o.toString().equals(v)){
                    return o;
                }
            }
            return null;
        }
       

    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
     */
    public void dragEnter(DropTargetDragEvent dtde) {
      if(comp.isEditingDisbled()
          || comp.getContextualDrawing()!=null){
        dtde.rejectDrag();
        return;
      }

            // try with 1.5 DND
            Object no=getDragObject(dtde);
      Point pt=dtde.getLocation();
            if(no instanceof ShapeCreator){
        as=((ShapeCreator)no).create();
        Vector v1=new Vector();
        v1.add(as);
        DiagramClipboard.set(v1);
        comp.executeClipboardPaste();
        comp.setDragPoint(pt);
        return; // => accepted
            }
            else if(no instanceof DataSource){
              data=(DataSource)no;
              if(isValidDataSourceDrag(pt)){
                return; // => accepted
              }
            }
            // use old method
            DataFlavor[] dfs=dtde.getCurrentDataFlavors();
            if(dfs==null || dfs.length==0){
              dtde.rejectDrag();
                return;
            }
            ShapeCreator o = getDragShape(dtde, dfs[0]);
      if(o!=null){
        as=o.create();
        Vector v1=new Vector();
        v1.add(as);
        DiagramClipboard.set(v1);
        comp.executeClipboardPaste();
        comp.setDragPoint(pt);
        return; // => accepted
      }
      else if((data=getSelectedSource())!=null){
        if(isValidDataSourceDrag(pt)){
          return; // => accepted
        }
      }
      dtde.rejectDrag();
    }

    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
     */
    public void dragOver(DropTargetDragEvent dtde) {
      if(comp.isEditingDisbled()
          || comp.getContextualDrawing()!=null){
        dtde.rejectDrag();
        return;
      }
            // use old method
            DataFlavor[] dfs=dtde.getCurrentDataFlavors();
            if(dfs==null || dfs.length==0){
                dtde.rejectDrag();
                return;
            }
          Point pt=dtde.getLocation();
            if(data==null){
              comp.setDragPoint(pt);
            }
            else{
              if(isValidDataSourceDrag(pt)){
                dtde.acceptDrag(DnDConstants.ACTION_COPY);
              }
              else{
                dtde.rejectDrag();
              }
            }
    }

    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
     */
    public void dropActionChanged(DropTargetDragEvent dtde) {
      // not used 
    }

    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
     */
    public void dragExit(DropTargetEvent dte) {
      if(as!=null && DiagramClipboard.get().contains(as)){
        comp.cancelDrop();
        as=null;
      }
      data=null;
    }

    private boolean dropData(DropTargetDropEvent dtde){
      if(data!=null){
        String dsInfo = data.getInformation().toString();
        if (data.isAuxiliary()){
          int dot = dsInfo.lastIndexOf(".");
          if (dot!=-1)
            dsInfo = dsInfo.substring(dot+1);
        }
        Point p=dtde.getLocation();
        DiagramSelection ds=comp.getDiagramSelection();
        int x=(int)((double)p.x/comp.getParameters().scale)-
            comp.getParameters().xmargin;
        int y=(int)((double)p.y/comp.getParameters().scale)-
            comp.getParameters().ymargin;
        if((ds.isEmpty() && ds.selectPoint(x,y))
            || ds.isSelected(x,y)){
          Vector vd=ds.getSelectedElements();
          boolean oneOk=false;
          for(int i=0;i<vd.size();i++){
            Object fvd=vd.get(i);
            if(fvd instanceof DataSourceConsumer){
              if(((DataSourceConsumer)fvd).addDataSource(data)){
                oneOk=true;
              }
            }
          }
          if(oneOk){
            dtde.dropComplete(true);
            return true;
          }
          else{
            dtde.dropComplete(false);
            return true;
          }
        }
      }
      return false;
    }
   
    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
     */
    public void drop(DropTargetDropEvent dtde) {
          if(data!=null){
            boolean accepted=dropData(dtde);
            data=null;
            if(!accepted){
              dtde.rejectDrop();
            }
          }
          else{
        comp.drop(dtde.getLocation());
        dtde.dropComplete(true);
          }
    }
  }
}
TOP

Related Classes of jsynoptic.ui.ShapesContainer$ShapesComponentDropListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.