001    /*
002     * Copyright (c) 2005, romain guy (romain.guy@jext.org) and craig wickesser (craig@codecraig.com)
003     * All rights reserved.
004     * 
005     * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
006     * 
007     *     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
008     *     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
009     *     * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
010     * 
011     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
012     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
013     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
015     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
016     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
017     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
018     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
019     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
020     * POSSIBILITY OF SUCH DAMAGE.
021     */
022    
023    package net.java.swingfx.jdraggable;
024    
025    import java.awt.Component;
026    
027    /**
028     * Abstract class used to define a "Drag Policy".
029     * <br>
030     * <br>
031     * A "Drag Policy" allows the ability for some restrictions to be put in place
032     * by a developer.  Currently <code>DragPolicy</code> is setup to only modify
033     * the behavior of which {@link java.awt.Component}'s can and can not be dragged.
034     * It may be feasible to add more features to <code>DragPolicy</code> in the future.
035     * 
036     * @author craig
037     * @since v0.1
038     * <br>
039     * $Header: /cvs/swingfx/docs/api/src-html/net/java/swingfx/jdraggable/DragPolicy.html,v 1.1 2005/06/23 00:24:44 codecraig Exp $
040     */
041    abstract public class DragPolicy {
042            /**
043             * any {@link java.awt.Component} is eligible to be dragged
044             */
045            private static final byte POLICY_OPEN   = 1 << 0;
046            /**
047             * only {@link Draggable} objects are eligible to be dragged 
048             */
049            private static final byte POLICY_STRICT = 1 << 1;
050            
051            private byte policy;
052            
053            private DragPolicy(byte policy) {
054                    this.policy = policy;
055            }
056            
057            /**
058             * Determines if the given <code>Component</code> is an eligible 
059             * component to be dragged
060             * 
061             * @param comp  the component to check for dragability
062             * 
063             * @return      <code>true</code> if the <code>Component</code> is
064             *                      draggable, <code>false</code> otherwise
065             * 
066             * @see Draggable
067             */
068            abstract public boolean isDraggable(Component comp);
069            
070            /**
071             * The default drag policy which suggests that all components
072             * are eligible to be dragged
073             * 
074             * @see #OPEN
075             * @see #STRICT
076             */
077            public static final DragPolicy DEFAULT = new DragPolicy(POLICY_OPEN) {
078                    public boolean isDraggable(Component comp) {
079                            return OPEN.isDraggable(comp);
080                    }
081            };
082            /**
083             * The drag policy which suggests that all components are eligible to be
084             * dragged
085             * 
086             * @see #DEFAULT
087             * @see #STRICT
088             */
089            public static final DragPolicy OPEN = new DragPolicy(POLICY_OPEN) {
090                    public boolean isDraggable(Component comp) {
091                            return !(comp == null);
092                    }
093            };
094            /**
095             * The drag policy which suggests that only {@link Draggable} components
096             * are eligible to be dragged
097             * 
098             * @see #DEFAULT
099             * @see #OPEN
100             */
101            public static final DragPolicy STRICT = new DragPolicy(POLICY_STRICT) {
102                    public boolean isDraggable(Component comp) {
103                            if (comp instanceof Draggable) {
104                                    if (((Draggable) comp).getComponent() != null) {
105                                            return true;
106                                    }
107                            }
108                            return false;
109                    }
110            };
111    }