All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.visual.impl.WidgetMoveProviderAdapter Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.visual.impl;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.awt.Dimension;
import java.awt.Point;
import org.netbeans.api.visual.action.MoveProvider;
import org.netbeans.api.visual.model.ObjectScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.openide.nodes.Node;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.visual.NodeScene;
import it.tidalwave.netbeans.visual.NodeScene.DragEvent;
import static it.tidalwave.netbeans.visual.NodeScene.DragBehaviour.Confirmation.*;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class WidgetMoveProviderAdapter implements MoveProvider
  {
    private static final String CLASS = WidgetMoveProviderAdapter.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final ObjectScene scene;

    @Nonnull
    private final Node node;

    @Nonnull
    private final LayerWidget layer;

    @Nonnull
    private final NodeScene.DragBehaviour dragBehaviour;

    @CheckForNull
    private Point originalLocation = null;

    public WidgetMoveProviderAdapter (final @Nonnull ObjectScene scene,
                                      final @Nonnull LayerWidget layer,
                                      final @Nonnull Node node,
                                      final @Nonnull NodeScene.DragBehaviour dragBehaviour)
      {
        this.scene = scene;
        this.layer = layer;
        this.node = node;
        this.dragBehaviour = dragBehaviour;
      }

    @Override
    public void movementStarted (final @Nonnull Widget widget)
      {
        logger.fine("movementStarted(%s)", widget);
        originalLocation = widget.getPreferredLocation();
        dragBehaviour.nodeDragStarted(new DragEvent(node, widget, originalLocation));
      }

    @Override
    public void movementFinished (final @Nonnull Widget widget)
      {
        logger.fine("movementFinished(%s)", widget);
        final Point newLocation = widget.getPreferredLocation();
        logger.finer(">>>> original location: %s, new location: %s", originalLocation, newLocation);

        final DragEvent dragEvent = new DragEvent(node, widget, newLocation);

        if (newLocation.equals(originalLocation))
          {
            dragBehaviour.nodeDragFinished(dragEvent, CANCELLED);
          }
        else if (dragBehaviour.confirmDrag(dragEvent) == CONFIRMED)
          {
            dragBehaviour.nodeDragFinished(dragEvent, CONFIRMED);
          }
        else // roll back to original location
          {
            // TODO use an animation - the SceneAnimator is not good, since we aren't notified when the animation
            // has been completed. We need to know as the nodeDragFinished() call must be performed at that time,
            // otherwise the LocationProvider kicks in and sticks the widget to its coordinates.
            widget.setPreferredLocation(originalLocation);
            dragBehaviour.nodeDragFinished(dragEvent, CANCELLED);
          }

        originalLocation = null;
      }

    @Override
    public Point getOriginalLocation (final @Nonnull Widget widget)
      {
        return widget.getPreferredLocation();
      }

    @Override
    public void setNewLocation (final @Nonnull Widget widget, final @Nonnull Point location)
      {
        logger.fine("setNewLocation(%s, %s)", widget, location);

        widget.setPreferredLocation(location);
        final Point layerLocation = layer.convertLocalToScene(location);
        final Point viewLocation = scene.convertSceneToView(layerLocation);
        final Dimension dimension = widget.getPreferredSize();

        if (dimension != null)
          {
            viewLocation.translate(dimension.width / 2, dimension.height / 2);
          }

        dragBehaviour.nodeDragged(new DragEvent(node, widget, viewLocation));
      }
  };




© 2015 - 2025 Weber Informatics LLC | Privacy Policy