Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import javafx.beans.property.BooleanProperty;
029import javafx.beans.property.BooleanPropertyBase;
030import javafx.event.ActionEvent;
031import javafx.scene.Cursor;
032import javafx.scene.Node;
033import javafx.css.CssMetaData;
034import javafx.css.PseudoClass;
035import com.sun.javafx.scene.control.skin.HyperlinkSkin;
036import javafx.css.StyleableProperty;
037
038
039/**
040 * <p>An HTML like label which can be a graphic and/or text which responds to rollovers and clicks.
041 * When a hyperlink is clicked/pressed {@link #isVisited} becomes {@code true}.  A Hyperlink behaves
042 * just like a {@link Button}.  When a hyperlink is pressed and released
043 * a {@link ActionEvent} is sent, and your application can perform some action based on this event.
044 * </p>
045 *
046 * <p>Example:</p>
047 * {@code Hyperlink link = new Hyperlink("www.oracle.com"); }
048 */
049public class Hyperlink extends ButtonBase {
050
051    /***************************************************************************
052     *                                                                         *
053     * Constructors                                                            *
054     *                                                                         *
055     **************************************************************************/
056
057    /**
058     * Creates a hyperlink with no label.
059     */
060    public Hyperlink() {
061        initialize();
062    }
063
064    /**
065     * Create a hyperlink with the specified text as its label.
066     *
067     * @param text A text string for its label.
068     */
069    public Hyperlink(String text) {
070        super(text);
071        initialize();
072    }
073
074    /**
075     * Create a hyperlink with the specified text and graphic as its label.
076     *
077     * @param text A text string for its label.
078     * @param graphic A graphic for its label
079     */
080    public Hyperlink(String text, Node graphic) {
081        super(text, graphic);
082        initialize();
083    }
084
085    private void initialize() {
086        // Initialize the style class to be 'hyperlink'.
087        getStyleClass().setAll(DEFAULT_STYLE_CLASS);
088        // cursor is styleable through css. Calling setCursor
089        // makes it look to css like the user set the value and css will not 
090        // override. Initializing cursor by calling applyStyle with null
091        // StyleOrigin ensures that css will be able to override the value.
092        ((StyleableProperty)cursorProperty()).applyStyle(null, Cursor.HAND);
093    }
094    
095    /***************************************************************************
096     *                                                                         *
097     * Properties                                                              *
098     *                                                                         *
099     **************************************************************************/
100    /**
101     * Indicates whether this link has already been "visited".
102     */
103    public final BooleanProperty visitedProperty() {
104        if (visited == null) {
105            visited = new BooleanPropertyBase() {
106                @Override protected void invalidated() {
107                    pseudoClassStateChanged(PSEUDO_CLASS_VISITED, get());
108                }
109
110                @Override
111                public Object getBean() {
112                    return Hyperlink.this;
113                }
114
115                @Override
116                public String getName() {
117                    return "visited";
118                }
119            };
120        }
121        return visited;
122    }
123    private BooleanProperty visited;
124    public final void setVisited(boolean value) {
125        visitedProperty().set(value);
126    }
127    public final boolean isVisited() {
128        return visited == null ? false : visited.get();
129    }
130
131    /***************************************************************************
132     *                                                                         *
133     * Methods                                                                 *
134     *                                                                         *
135     **************************************************************************/
136
137    /**
138     * Implemented to invoke the {@link ActionEvent} if one is defined. This
139     * function will also {@link #setVisited} to true.
140     */
141    @Override public void fire() {
142        // Avoid causing an exception in the case that visited was bound
143        if (visited == null || !visited.isBound()) {
144            setVisited(true);
145        }
146        fireEvent(new ActionEvent());
147    }
148
149    /** {@inheritDoc} */
150    @Override protected Skin<?> createDefaultSkin() {
151        return new HyperlinkSkin(this);
152    }
153
154
155    /***************************************************************************
156     *                                                                         *
157     * Stylesheet Handling                                                     *
158     *                                                                         *
159     **************************************************************************/
160
161    private static final String DEFAULT_STYLE_CLASS = "hyperlink";
162    private static final PseudoClass PSEUDO_CLASS_VISITED =
163            PseudoClass.getPseudoClass("visited");
164
165     /**
166      * Hyperlink uses HAND as the default value for cursor. 
167      * This method provides a way for css to get the correct initial value.
168      * @treatAsPrivate implementation detail
169      */
170    @Deprecated @Override
171    protected /*do not make final*/ Cursor impl_cssGetCursorInitialValue() {
172        return Cursor.HAND;
173    }
174    
175}