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

it.tidalwave.netbeans.visual.impl.MouseEventForwarder 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 java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.Serializable;
import javax.annotation.Nonnull;
import javax.swing.JComponent;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public final class MouseEventForwarder implements MouseListener, MouseMotionListener, MouseWheelListener, Serializable
  {
    private static final long serialVersionUID = 5756835431534262L;

    @Nonnull
    private final Scene scene;

    @Nonnull
    private final Component component;
    
    public MouseEventForwarder (final @Nonnull Scene scene, @Nonnull final Component component)
      {
        this.scene = scene;
        this.component = component;
      }
    
    @Override
    public void mouseClicked (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mousePressed (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mouseReleased (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mouseEntered (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mouseExited (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mouseDragged (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }

    @Override
    public void mouseMoved (final @Nonnull MouseEvent event)
      {
        forwardEvent(event);
      }
  
    @Override
    public void mouseWheelMoved (final @Nonnull MouseWheelEvent event)
      {
        forwardEvent(event);
      }

    private void forwardEvent (final @Nonnull MouseEvent event)
      {
        // if (!isHit(event.getPoint(), scene))
        if (!isHit(scene.convertViewToScene(event.getPoint()), scene))
          {
            if (component instanceof JComponent)
              {
                for (final Component child : ((JComponent)component).getComponents())
                  {
                    System.err.println("FORWARDING TO " + child);
                    child.dispatchEvent(event);  
                  }
              }

            component.dispatchEvent(event);
          }
      }

    private boolean isHit (final @Nonnull Point point, final @Nonnull Widget widget)
      {
        // First check children, widget at last
        for (final Widget child : widget.getChildren())
          {
            if (isHit(point, child))
              {
                return true;   
              }
          }

        if (!(widget instanceof Scene) && widget.isHitAt(widget.convertSceneToLocal(point)))
          {
            return true;
          }

        return false;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy