1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
5 *
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
8 *
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License.  When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
31 * Microsystems, Inc. All Rights Reserved.
32 *
33 * If you wish your version of this file to be governed by only the CDDL
34 * or only the GPL Version 2, indicate your decision by adding
35 * "[Contributor] elects to include this software in this distribution
36 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37 * single choice of license, a recipient has the option to distribute
38 * your version of this file under either the CDDL, the GPL Version 2 or
39 * to extend the choice of license to its licensees as provided above.
40 * However, if you add GPL Version 2 code and therefore, elected the GPL
41 * Version 2 license, then the option applies only if the new code is
42 * made subject to such option by the copyright holder.
43 */
44package com.sun.hotspot.igv.view.actions;
45
46import java.awt.Container;
47import java.awt.Point;
48import java.awt.Rectangle;
49import javax.swing.JComponent;
50import javax.swing.JScrollPane;
51import javax.swing.SwingUtilities;
52import org.netbeans.api.visual.action.WidgetAction;
53import org.netbeans.api.visual.action.WidgetAction.State;
54import org.netbeans.api.visual.action.WidgetAction.WidgetMouseEvent;
55import org.netbeans.api.visual.widget.Scene;
56import org.netbeans.api.visual.widget.Widget;
57
58/**
59 * @author David Kaspar
60 * @author Peter Hofer
61 */
62public class CustomizablePanAction extends WidgetAction.LockedAdapter {
63    private boolean enabled = true;
64
65    private Scene scene;
66    private JScrollPane scrollPane;
67    private Point lastLocation;
68
69    private final int modifiersExMask;
70    private final int modifiersEx;
71
72    public CustomizablePanAction(int modifiersExMask, int modifiersEx) {
73        this.modifiersExMask = modifiersExMask;
74        this.modifiersEx = modifiersEx;
75    }
76
77    @Override
78    protected boolean isLocked() {
79        return scrollPane != null;
80    }
81
82    public void setEnabled(boolean enabled) {
83        if (this.enabled != enabled) {
84            if (isLocked())
85                throw new IllegalStateException();
86
87            this.enabled = enabled;
88        }
89    }
90
91    @Override
92    public State mousePressed (Widget widget, WidgetMouseEvent event) {
93        if (isLocked ())
94            return State.createLocked (widget, this);
95        if (enabled && (event.getModifiersEx() & modifiersExMask) == modifiersEx) {
96            scene = widget.getScene ();
97            scrollPane = findScrollPane (scene.getView ());
98            if (scrollPane != null) {
99                lastLocation = scene.convertSceneToView (widget.convertLocalToScene (event.getPoint ()));
100                SwingUtilities.convertPointToScreen (lastLocation, scene.getView ());
101                return State.createLocked (widget, this);
102            }
103        }
104        return State.REJECTED;
105    }
106
107    private JScrollPane findScrollPane (JComponent component) {
108        for (;;) {
109            if (component == null)
110                return null;
111            if (component instanceof JScrollPane)
112                return ((JScrollPane) component);
113            Container parent = component.getParent ();
114            if (! (parent instanceof JComponent))
115                return null;
116            component = (JComponent) parent;
117        }
118    }
119
120    @Override
121    public State mouseReleased (Widget widget, WidgetMouseEvent event) {
122        boolean state = pan (widget, event.getPoint ());
123        if (state)
124            scrollPane = null;
125        return state ? State.createLocked (widget, this) : State.REJECTED;
126    }
127
128    @Override
129    public State mouseDragged (Widget widget, WidgetMouseEvent event) {
130        return pan (widget, event.getPoint ()) ? State.createLocked (widget, this) : State.REJECTED;
131    }
132
133    private boolean pan (Widget widget, Point newLocation) {
134        if (scrollPane == null  ||  scene != widget.getScene ())
135            return false;
136        newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
137        SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
138        JComponent view = scene.getView ();
139        Rectangle rectangle = view.getVisibleRect ();
140        rectangle.x += lastLocation.x - newLocation.x;
141        rectangle.y += lastLocation.y - newLocation.y;
142        view.scrollRectToVisible (rectangle);
143        lastLocation = newLocation;
144        return true;
145    }
146}
147