changeset 7713:7c9dd9130dac

RT-38322: [Dialogs] Externalise default graphic file references
author jgiles
date Tue, 19 Aug 2014 13:33:52 +1200
parents 323fe1d9f2b8
children b87aff6ddefa
files modules/controls/src/main/java/javafx/scene/control/Alert.java modules/controls/src/main/java/javafx/scene/control/ChoiceDialog.java modules/controls/src/main/java/javafx/scene/control/DialogPane.java modules/controls/src/main/java/javafx/scene/control/TextInputDialog.java modules/controls/src/main/resources/com/sun/javafx/scene/control/skin/modena/modena.css
diffstat 5 files changed, 53 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/Alert.java	Tue Aug 19 12:50:52 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/Alert.java	Tue Aug 19 13:33:52 2014 +1200
@@ -25,6 +25,9 @@
 package javafx.scene.control;
 
 import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 import java.util.Optional;
 
 import com.sun.javafx.scene.control.skin.AccordionSkin;
@@ -176,14 +179,9 @@
     private boolean installingDefaults = false;
     private boolean hasCustomButtons = false;
     private boolean hasCustomTitle = false;
-    private boolean hasCustomGraphic = false;
     private boolean hasCustomHeaderText = false;
     
-    private final InvalidationListener graphicListener = o -> { 
-        if (!installingDefaults) hasCustomGraphic = true; 
-    };
-    
-    private final InvalidationListener headerTextListener = o -> { 
+    private final InvalidationListener headerTextListener = o -> {
         if (!installingDefaults) hasCustomHeaderText = true; 
     };
     
@@ -245,8 +243,10 @@
                  @NamedArg("contentText") String contentText,
                  ButtonType... buttons) {
         super();
+
         final DialogPane dialogPane = getDialogPane();
         dialogPane.setContentText(contentText);
+        getDialogPane().getStyleClass().add("alert");
         
         dialogPaneRef = new WeakReference<>(dialogPane);
         
@@ -284,10 +284,13 @@
      */
     // --- alertType
     private final ObjectProperty<AlertType> alertType = new SimpleObjectProperty<AlertType>(null) {
+        final String[] styleClasses = new String[] { "information", "warning", "error", "confirmation" };
+
         protected void invalidated() {
             String newTitle = "";
             String newHeader = "";
-            Node newGraphic = null;
+//            Node newGraphic = null;
+            String styleClass = "";
             ButtonType[] newButtons = new ButtonType[] { ButtonType.OK };
             switch (getAlertType()) {
                 case NONE: {
@@ -297,33 +300,25 @@
                 case INFORMATION: {
                     newTitle = ControlResources.getString("Dialog.info.title");
                     newHeader = ControlResources.getString("Dialog.info.header");
-
-                    // TODO extract out to CSS
-                    newGraphic = new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-information.png").toExternalForm()));
+                    styleClass = "information";
                     break;
                 }
                 case WARNING: {
                     newTitle = ControlResources.getString("Dialog.warning.title");
                     newHeader = ControlResources.getString("Dialog.warning.header");
-
-                    // TODO extract out to CSS
-                    newGraphic = new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-warning.png").toExternalForm()));
+                    styleClass = "warning";
                     break;
                 }
                 case ERROR: {
                     newTitle = ControlResources.getString("Dialog.error.title");
                     newHeader = ControlResources.getString("Dialog.error.header");
-
-                    // TODO extract out to CSS
-                    newGraphic = new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-error.png").toExternalForm()));
+                    styleClass = "error";
                     break;
                 }      
                 case CONFIRMATION: {
                     newTitle = ControlResources.getString("Dialog.confirm.title");
                     newHeader = ControlResources.getString("Dialog.confirm.header");
-
-                    // TODO extract out to CSS
-                    newGraphic = new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-confirm.png").toExternalForm()));
+                    styleClass = "confirmation";
                     newButtons = new ButtonType[] { ButtonType.OK, ButtonType.CANCEL };
                     break;
                 } 
@@ -332,8 +327,20 @@
             installingDefaults = true;
             if (!hasCustomTitle) setTitle(newTitle);
             if (!hasCustomHeaderText) setHeaderText(newHeader);
-            if (!hasCustomGraphic) setGraphic(newGraphic);
             if (!hasCustomButtons) getButtonTypes().setAll(newButtons);
+
+            // update the style class based on the alert type. We use this to
+            // specify the default graphic to use (i.e. via CSS).
+            DialogPane dialogPane = getDialogPane();
+            if (dialogPane != null) {
+                List<String> toRemove = new ArrayList<>(Arrays.asList(styleClasses));
+                toRemove.remove(styleClass);
+                dialogPane.getStyleClass().removeAll(toRemove);
+                if (! dialogPane.getStyleClass().contains(styleClass)) {
+                    dialogPane.getStyleClass().add(styleClass);
+                }
+            }
+
             installingDefaults = false;
         }
     };
@@ -381,7 +388,6 @@
         DialogPane oldPane = dialogPaneRef.get();
         
         if (oldPane != null) {
-            oldPane.graphicProperty().removeListener(graphicListener);
             oldPane.headerTextProperty().removeListener(headerTextListener);
             oldPane.getButtonTypes().removeListener(buttonsListener);
         }
@@ -393,7 +399,6 @@
         
         DialogPane newPane = getDialogPane();
         if (newPane != null) {
-            newPane.graphicProperty().addListener(graphicListener);
             newPane.headerTextProperty().addListener(headerTextListener);
             newPane.getButtonTypes().addListener(buttonsListener);
         }
--- a/modules/controls/src/main/java/javafx/scene/control/ChoiceDialog.java	Tue Aug 19 12:50:52 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/ChoiceDialog.java	Tue Aug 19 13:33:52 2014 +1200
@@ -126,9 +126,7 @@
         
         setTitle(ControlResources.getString("Dialog.confirm.title"));
         dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
-
-        // TODO extract out to CSS
-        dialogPane.setGraphic(new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-confirm.png").toExternalForm())));
+        dialogPane.getStyleClass().add("choice-dialog");
         dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
         
         final double MIN_WIDTH = 150;
--- a/modules/controls/src/main/java/javafx/scene/control/DialogPane.java	Tue Aug 19 12:50:52 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/DialogPane.java	Tue Aug 19 13:33:52 2014 +1200
@@ -1053,8 +1053,7 @@
         
         private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
         static {
-            final List<CssMetaData<? extends Styleable, ?>> styleables =
-                new ArrayList<CssMetaData<? extends Styleable, ?>>(Control.getClassCssMetaData());
+            final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Region.getClassCssMetaData());
             Collections.addAll(styleables,
                 GRAPHIC
             );
@@ -1069,4 +1068,9 @@
     public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
         return StyleableProperties.STYLEABLES;
     }
+
+    /** {@inheritDoc} */
+    @Override public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
+        return getClassCssMetaData();
+    }
 }
--- a/modules/controls/src/main/java/javafx/scene/control/TextInputDialog.java	Tue Aug 19 12:50:52 2014 +1200
+++ b/modules/controls/src/main/java/javafx/scene/control/TextInputDialog.java	Tue Aug 19 13:33:52 2014 +1200
@@ -101,9 +101,7 @@
         
         setTitle(ControlResources.getString("Dialog.confirm.title"));
         dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
-
-        // TODO extract out to CSS
-        dialogPane.setGraphic(new ImageView(new Image(AccordionSkin.class.getResource("modena/dialog-confirm.png").toExternalForm())));
+        dialogPane.getStyleClass().add("text-input-dialog");
         dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
         
         updateGrid();
--- a/modules/controls/src/main/resources/com/sun/javafx/scene/control/skin/modena/modena.css	Tue Aug 19 12:50:52 2014 +1200
+++ b/modules/controls/src/main/resources/com/sun/javafx/scene/control/skin/modena/modena.css	Tue Aug 19 13:33:52 2014 +1200
@@ -3397,3 +3397,21 @@
 .dialog > .dialog-pane > .button-bar > .container > .details-button:hover {
     -fx-underline: true;
 }
+
+.alert.confirmation.dialog-pane,
+.text-input-dialog.dialog-pane,
+.choice-dialog.dialog-pane {
+    -fx-graphic: url("dialog-confirm.png");
+}
+
+.alert.information.dialog-pane {
+    -fx-graphic: url("dialog-information.png");
+}
+
+.alert.error.dialog-pane {
+    -fx-graphic: url("dialog-error.png");
+}
+
+.alert.warning.dialog-pane {
+    -fx-graphic: url("dialog-warning.png");
+}
\ No newline at end of file