1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javafx.scene.control;
  26 
  27 import com.sun.javafx.scene.control.skin.resources.ControlResources;
  28 
  29 import javafx.beans.NamedArg;
  30 import javafx.scene.control.Button;
  31 import javafx.scene.control.ButtonBar.ButtonData;
  32 
  33 /**
  34  * The ButtonType class is used as part of the JavaFX {@link Dialog} API (more
  35  * specifically, the {@link DialogPane} API) to specify which buttons should be
  36  * shown to users in the dialogs. Refer to the {@link DialogPane} class javadoc
  37  * for more information on how to use this class.
  38  *
  39  * @see Alert
  40  * @see Dialog
  41  * @see DialogPane
  42  * @since JavaFX 8u40
  43  */
  44 public final class ButtonType {
  45 
  46     /**
  47      * A pre-defined {@link ButtonType} that displays "Apply" and has a
  48      * {@link ButtonData} of {@link ButtonData#APPLY}.
  49      */
  50     public static final ButtonType APPLY = new ButtonType(
  51             "Dialog.apply.button", null, ButtonData.APPLY);
  52 
  53     /**
  54      * A pre-defined {@link ButtonType} that displays "OK" and has a
  55      * {@link ButtonData} of {@link ButtonData#OK_DONE}.
  56      */
  57     public static final ButtonType OK = new ButtonType(
  58             "Dialog.ok.button", null, ButtonData.OK_DONE);
  59 
  60     /**
  61      * A pre-defined {@link ButtonType} that displays "Cancel" and has a
  62      * {@link ButtonData} of {@link ButtonData#CANCEL_CLOSE}.
  63      */
  64     public static final ButtonType CANCEL = new ButtonType(
  65             "Dialog.cancel.button", null, ButtonData.CANCEL_CLOSE);
  66 
  67     /**
  68      * A pre-defined {@link ButtonType} that displays "Close" and has a
  69      * {@link ButtonData} of {@link ButtonData#CANCEL_CLOSE}.
  70      */
  71     public static final ButtonType CLOSE = new ButtonType(
  72             "Dialog.close.button", null, ButtonData.CANCEL_CLOSE);
  73 
  74     /**
  75      * A pre-defined {@link ButtonType} that displays "Yes" and has a
  76      * {@link ButtonData} of {@link ButtonData#YES}.
  77      */
  78     public static final ButtonType YES = new ButtonType(
  79             "Dialog.yes.button", null, ButtonData.YES);
  80 
  81     /**
  82      * A pre-defined {@link ButtonType} that displays "No" and has a
  83      * {@link ButtonData} of {@link ButtonData#NO}.
  84      */
  85     public static final ButtonType NO = new ButtonType(
  86             "Dialog.no.button", null, ButtonData.NO);
  87 
  88     /**
  89      * A pre-defined {@link ButtonType} that displays "Finish" and has a
  90      * {@link ButtonData} of {@link ButtonData#FINISH}.
  91      */
  92     public static final ButtonType FINISH = new ButtonType(
  93             "Dialog.finish.button", null, ButtonData.FINISH);
  94 
  95     /**
  96      * A pre-defined {@link ButtonType} that displays "Next" and has a
  97      * {@link ButtonData} of {@link ButtonData#NEXT_FORWARD}.
  98      */
  99     public static final ButtonType NEXT = new ButtonType(
 100             "Dialog.next.button", null, ButtonData.NEXT_FORWARD);
 101 
 102     /**
 103      * A pre-defined {@link ButtonType} that displays "Previous" and has a
 104      * {@link ButtonData} of {@link ButtonData#BACK_PREVIOUS}.
 105      */
 106     public static final ButtonType PREVIOUS = new ButtonType(
 107             "Dialog.previous.button", null, ButtonData.BACK_PREVIOUS);
 108 
 109     private final String key;
 110     private final String text;
 111     private final ButtonData buttonData;
 112 
 113 
 114     /**
 115      * Creates a ButtonType instance with the given text, and the ButtonData set
 116      * as {@link ButtonData#OTHER}.
 117      *
 118      * @param text The string to display in the text property of controls such
 119      *      as {@link Button#textProperty() Button}.
 120      */
 121     public ButtonType(@NamedArg("text") String text) {
 122         this(text, ButtonData.OTHER);
 123     }
 124 
 125     /**
 126      * Creates a ButtonType instance with the given text, and the ButtonData set
 127      * as specified.
 128      *
 129      * @param text The string to display in the text property of controls such
 130      *      as {@link Button#textProperty() Button}.
 131      * @param buttonData The type of button that should be created from this ButtonType.
 132      */
 133     public ButtonType(@NamedArg("text") String text,
 134                         @NamedArg("buttonData") ButtonData buttonData) {
 135         this(null, text, buttonData);
 136     }
 137 
 138     /**
 139      * Provide key or text. The other one should be null.
 140      */
 141     private ButtonType(String key, String text, ButtonData buttonData) {
 142         this.key = key;
 143         this.text = text;
 144         this.buttonData = buttonData;
 145     }
 146 
 147     /**
 148      * Returns the ButtonData specified for this ButtonType in the constructor.
 149      * @return the ButtonData specified for this ButtonType in the constructor
 150      */
 151     public final ButtonData getButtonData() { return this.buttonData; }
 152 
 153     /**
 154      * Returns the text specified for this ButtonType in the constructor.
 155      * @return the text specified for this ButtonType in the constructor
 156      */
 157     public final String getText() {
 158         if (text == null && key != null) {
 159             return ControlResources.getString(key);
 160         } else {
 161             return text;
 162         }
 163     }
 164 
 165     /** {@inheritDoc} */
 166     @Override public String toString() {
 167         return "ButtonType [text=" + getText() + ", buttonData=" + getButtonData() + "]";
 168     }
 169 }