Friday, August 24, 2018

javafx - override root css attributes

I know it is possible to override dynamically css attributes defined for a node using StyleableObjectProperty. I'm asking now how can i change root properties declared in ".root" class in css stylesheet so all node will inherit this change.

I would like for example to change a font color attribute used for all text in my application. This color can be changed dynamically in the application and should apply on all nodes using it.

Thanks

Solved

In general, your best bet for figuring out CSS settings is to look at the source code for the default stylesheet.

For example, font colors are actually managed by automatically selecting from one of three fixed font colors, depending on the intensity of the background (so you don't end up with white text on a white background, for example):

/* One of these colors will be chosen based upon a ladder calculation
 * that uses the brightness of a background color.  Instead of using these
 * colors directly as -fx-text-fill values, the sections in this file should
 * use a derived color to match the background in use.  See also:
 *
 * -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color
 * -fx-text-background-color for text on top of -fx-background
 * -fx-text-inner-color for text on top of -fx-control-inner-color
 * -fx-selection-bar-text for text on top of -fx-selection-bar
 */
-fx-dark-text-color: black;
-fx-mid-text-color: #333;
-fx-light-text-color: white;

So you can override these with something like

.root {
    -fx-dark-text-color: navy;
    -fx-mid-text-color: blue;
    -fx-light-text-color: lightskyblue;
}

in an external style sheet, and it will change the font color for the whole application. (If you're certain your backgrounds will never be a problem, you could make them all the same color, but I wouldn't recommend that.)

The properties set here are actually "looked-up colors". Since values of looked-up colors are inherited from the parent node, these values will apply to the entire scene graph.

If you want to do this from code, you can achieve the same with

root.setStyle(
    "-fx-dark-text-color:  navy ;"+
    "-fx-mid-text-color:   blue ;"+
    "-fx-light-text-color: lightskyblue ;");

This is from modena.css @ jfxrt.jar(com/sun/javafx/scene/control/skin/) -- found in the JavaFX runtime JAR file. Although, caspian.css could be the default stylesheet.

The -fx-base: #ececec; has a huge overall effect on your app.

//add css example
scene.getStylesheets().add(getClass()
                 .getResource("/theshow/jimmylandstudios/gui/THESHOW5.css")
                 .toExternalForm());
/*******************************************************************************
 *                                                                             *
 * CSS Styles for core infrastructure bits.  The .root section provides the    *
 * overall default colors used by the rest of the sections.                    *
 *                                                                             *
 ******************************************************************************/

.root {
    /***************************************************************************
     *                                                                         *
     * The main color palette from which the rest of the colors are derived.   *
     *                                                                         *
     **************************************************************************/

    /* A light grey that is the base color for objects.  Instead of using
     * -fx-base directly, the sections in this file will typically use -fx-color.
     */
    -fx-base: #ececec;

    /* A very light grey used for the background of windows.  See also
     * -fx-text-background-color, which should be used as the -fx-text-fill
     * value for text painted on top of backgrounds colored with -fx-background.
     */
    -fx-background: derive(-fx-base,26.4%);

    /* Used for the inside of text boxes, password boxes, lists, trees, and
     * tables.  See also -fx-text-inner-color, which should be used as the
     * -fx-text-fill value for text painted on top of backgrounds colored
     * with -fx-control-inner-background.
     */
    -fx-control-inner-background: derive(-fx-base,80%);
    /* Version of -fx-control-inner-background for alternative rows */
    -fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%);

    /* One of these colors will be chosen based upon a ladder calculation
     * that uses the brightness of a background color.  Instead of using these
     * colors directly as -fx-text-fill values, the sections in this file should
     * use a derived color to match the background in use.  See also:
     *
     * -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color
     * -fx-text-background-color for text on top of -fx-background
     * -fx-text-inner-color for text on top of -fx-control-inner-color
     * -fx-selection-bar-text for text on top of -fx-selection-bar
     */
    -fx-dark-text-color: black;
    -fx-mid-text-color: #333;
    -fx-light-text-color: white;

    /* A bright blue for highlighting/accenting objects.  For example: selected
     * text; selected items in menus, lists, trees, and tables; progress bars */
    -fx-accent: #0096C9;

    /* Default buttons color, this is similar to accent but more subtle */
    -fx-default-button: #ABD8ED;

    /* A bright blue for the focus indicator of objects. Typically used as the
     * first color in -fx-background-color for the "focused" pseudo-class. Also
     * typically used with insets of -1.4 to provide a glowing effect.
     */
    -fx-focus-color: #039ED3;
    -fx-faint-focus-color: #039ED322;

    /* The color that is used in styling controls. The default value is based
     * on -fx-base, but is changed by pseudoclasses to change the base color.
     * For example, the "hover" pseudoclass will typically set -fx-color to
     * -fx-hover-base (see below) and the "armed" pseudoclass will typically
     * set -fx-color to -fx-pressed-base.
     */
    -fx-color: -fx-base;

    /* Chart Color Palette */
    CHART_COLOR_1: #f3622d;
    CHART_COLOR_2: #fba71b;
    CHART_COLOR_3: #57b757;
    CHART_COLOR_4: #41a9c9;
    CHART_COLOR_5: #4258c9;
    CHART_COLOR_6: #9a42c8;
    CHART_COLOR_7: #c84164;
    CHART_COLOR_8: #888888;
    /* Chart Color Palette Semi-Transparent
     * These are used by charts that need semi transparent versions of the above colors, such as BubbleChart. They
     * are exactly the same colors as above just with alpha
     *
     * 20% opacity
     */
    CHART_COLOR_1_TRANS_20: #f3622d33;
    CHART_COLOR_2_TRANS_20: #fba71b33;
    CHART_COLOR_3_TRANS_20: #57b75733;
    CHART_COLOR_4_TRANS_20: #41a9c933;
    CHART_COLOR_5_TRANS_20: #4258c933;
    CHART_COLOR_6_TRANS_20: #9a42c833;
    CHART_COLOR_7_TRANS_20: #c8416433;
    CHART_COLOR_8_TRANS_20: #88888833;
    /* 70% opacity */
    CHART_COLOR_1_TRANS_70: #f3622db3;
    CHART_COLOR_2_TRANS_70: #fba71bb3;
    CHART_COLOR_3_TRANS_70: #57b757b3;
    CHART_COLOR_4_TRANS_70: #41a9c9b3;
    CHART_COLOR_5_TRANS_70: #4258c9b3;
    CHART_COLOR_6_TRANS_70: #9a42c8b3;
    CHART_COLOR_7_TRANS_70: #c84164b3;
    CHART_COLOR_8_TRANS_70: #888888b3;

    /***************************************************************************
     *                                                                         *
     * Colors that are derived from the main color palette.                    *
     *                                                                         *
     **************************************************************************/

    /* A little lighter than -fx-base and used as the -fx-color for the
     * "hovered" pseudoclass state.
     */
    -fx-hover-base: ladder(
        -fx-base,
        derive(-fx-base,20%) 20%,
        derive(-fx-base,30%) 35%,
        derive(-fx-base,40%) 50%
     );

    /* A little darker than -fx-base and used as the -fx-color for the
     * "armed" pseudoclass state.
     *
     * TODO: should this be renamed to -fx-armed-base?
     */
    -fx-pressed-base: derive(-fx-base,-6%);

    /* The color to use for -fx-text-fill when text is to be painted on top of
     * a background filled with the -fx-background color.
     */
    -fx-text-background-color: ladder(
        -fx-background,
        -fx-light-text-color 45%,
        -fx-dark-text-color  46%,
        -fx-dark-text-color  59%,
        -fx-mid-text-color   60%
    );

    /* A little darker than -fx-color and used to draw boxes around objects such
     * as progress bars, scroll bars, scroll panes, trees, tables, and lists.
     */
    -fx-box-border: ladder(
        -fx-color,
        black 20%,
        derive(-fx-color,-15%) 30%
    );

    /* Darker than -fx-background and used to draw boxes around text boxes and
     * password boxes.
     */
    -fx-text-box-border: ladder(
        -fx-background,
        black 10%,
        derive(-fx-background, -15%) 30%
    );

    /* Lighter than -fx-background and used to provide a small highlight when
     * needed on top of -fx-background. This is never a shadow in Modena but
     * keep -fx-shadow-highlight-color name to be compatible with Caspian.
     */
    -fx-shadow-highlight-color: ladder(
        -fx-background,
        rgba(255,255,255,0.07) 0%,
        rgba(255,255,255,0.07) 20%,
        rgba(255,255,255,0.07) 70%,
        rgba(255,255,255,0.7) 90%,
        rgba(255,255,255,0.75) 100%
      );

    /* A gradient that goes from a little darker than -fx-color on the top to
     * even more darker than -fx-color on the bottom.  Typically is the second
     * color in the -fx-background-color list as the small thin border around
     * a control. It is typically the same size as the control (i.e., insets
     * are 0).
     */
    -fx-outer-border: derive(-fx-color,-23%);

    /* A gradient that goes from a bit lighter than -fx-color on the top to
     * a little darker at the bottom.  Typically is the third color in the
     * -fx-background-color list as a thin highlight inside the outer border.
     * Insets are typically 1.
     */
    -fx-inner-border: linear-gradient(to bottom,
                ladder(
                    -fx-color,
                    derive(-fx-color,30%) 0%,
                    derive(-fx-color,20%) 40%,
                    derive(-fx-color,25%) 60%,
                    derive(-fx-color,55%) 80%,
                    derive(-fx-color,55%) 90%,
                    derive(-fx-color,75%) 100%
                ),
                ladder(
                    -fx-color,
                    derive(-fx-color,20%) 0%,
                    derive(-fx-color,10%) 20%,
                    derive(-fx-color,5%) 40%,
                    derive(-fx-color,-2%) 60%,
                    derive(-fx-color,-5%) 100%
                ));
    -fx-inner-border-horizontal: linear-gradient(to right, derive(-fx-color,55%), derive(-fx-color,-5%));
    -fx-inner-border-bottomup: linear-gradient(to top, derive(-fx-color,55%), derive(-fx-color,-5%));

    /* A gradient that goes from a little lighter than -fx-color at the top to
     * a little darker than -fx-color at the bottom and is used to fill the
     * body of many controls such as buttons.
     */
    -fx-body-color: linear-gradient(to bottom,
            ladder(
                -fx-color,
                derive(-fx-color,8%) 75%,
                derive(-fx-color,10%) 80%
            ),
            derive(-fx-color,-8%));
    -fx-body-color-bottomup: linear-gradient(to top, derive(-fx-color,10%) ,derive(-fx-color,-6%));
    -fx-body-color-to-right: linear-gradient(to right, derive(-fx-color,10%) ,derive(-fx-color,-6%));

    /* The color to use as -fx-text-fill when painting text on top of
     * backgrounds filled with -fx-base, -fx-color, and -fx-body-color.
     */
    -fx-text-base-color: ladder(
        -fx-color,
        -fx-light-text-color 45%,
        -fx-dark-text-color  46%,
        -fx-dark-text-color  59%,
        -fx-mid-text-color   60%
    );

    /* The color to use as -fx-text-fill when painting text on top of
     * backgrounds filled with -fx-control-inner-background.
     */
    -fx-text-inner-color: ladder(
        -fx-control-inner-background,
        -fx-light-text-color 45%,
        -fx-dark-text-color  46%,
        -fx-dark-text-color  59%,
        -fx-mid-text-color   60%
    );

    /* The color to use for small mark-like objects such as checks on check
     * boxes, filled in circles in radio buttons, arrows on scroll bars, etc.
     */
    -fx-mark-color: ladder(
        -fx-color,
        white 30%,
        derive(-fx-color,-63%) 31%
    );

    /* The small thin light "shadow" for mark-like objects. Typically used in
     * conjunction with -fx-mark-color with an insets of 1 0 -1 0. */
    -fx-mark-highlight-color: ladder(
        -fx-color,
        derive(-fx-color,80%) 60%,
        white 70%
    );

    /* Background for items in list like things such as menus, lists, trees,
     * and tables. */
    -fx-selection-bar: -fx-accent;

    /* Background color to use for selection of list cells etc. This is when
     * the control doesn't have focus or the row of a previously selected item. */
    -fx-selection-bar-non-focused: lightgrey;

    /* The color to use as -fx-text-fill when painting text on top of
     * backgrounds filled with -fx-selection-bar.
     *
     * TODO: this can be removed
     */
    -fx-selection-bar-text: -fx-text-background-color;

    /* These are needed for Popup */
    -fx-background-color: inherit;
    -fx-background-radius: inherit;
    -fx-background-insets: inherit;
    -fx-padding: inherit;

    /* The color to use in ListView/TreeView/TableView to indicate hover. */
    -fx-cell-hover-color: #cce3f4;

    /** Focus line for keyboard focus traversal on cell based controls */
    -fx-cell-focus-inner-border: derive(-fx-selection-bar,30%);

    /* The colors to use in Pagination */
    -fx-page-bullet-border: #acacac;
    -fx-page-indicator-hover-border: #accee5;

    -fx-focused-text-base-color : ladder(
        -fx-selection-bar,
        -fx-light-text-color 45%,
        -fx-dark-text-color 46%,
        -fx-dark-text-color 59%,
        -fx-mid-text-color 60%
    );
    -fx-focused-mark-color : -fx-focused-text-base-color ;

    /***************************************************************************
     *                                                                         *
     * Set the default background color for the scene                          *
     *                                                                         *
     **************************************************************************/

    -fx-background-color: -fx-background;
}

Monday, August 20, 2018

MYSQL Most Frequent Value Given a Certain Category

How do I find the most frequent flight type, given the Destination?

Table: Flight:
+-------------+---------------+
| Destination | Type          |
+-------------+---------------+
| Beijing     | First Class   |
| Beijing     | First Class   |
| Beijing     | First Class   |
| Beijing     | First Class   |
| Beijing     | Coach         |
| London      | Coach         |
| London      | First Class   |
| London      | Coach         |
+-------------+---------------+

Every query I've tried so far has given the total number of each flight type as opposed to the most frequent flight type for each destination.

Here is the desired output:

+-------------+--------------+-------+
| Destination | Type         | count |
+-------------+--------------+-------+
| Beijing     | First Class  |     4 |
| London      | Coach        |     2 |
+-------------+--------------+-------+

EDIT: So I found the easiest way was this:

SELECT destination, type, count FROM Flight WHERE total=
(
SELECT MAX(total) FROM Flight fl WHERE fl.destination = Flight.destination
);

Solved

You can try GROUP BY and HAVING. You have to select the rows which have the MAX total rows for each group (Destination, Type).

SELECT Destination, `Type`, COUNT(*) AS total
FROM your_table AS aa
GROUP BY Destination, `Type`
HAVING COUNT(*) = (
    SELECT MAX(each_total) 
    FROM (
        SELECT COUNT(*) AS each_total
        FROM your_table
        GROUP BY Destination, `Type`
    )
)
ORDER BY Destination ASC;

Sunday, August 19, 2018

JsonConvert Exists in both Newtonsoft and System.Net.Http.Formatting Visual Studio 2017 for Mac

I have a project I have been working on using Xamarin's MonoDevelop.

I have been using Newtonsoft's Json nuget package.

I just downloaded Visual Studio 2017 for Mac.

I try to build my project in VS2017Mac and get the following error:

error CS0433: The type 'JsonConvert' exists in both 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' and 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I thought I should be able to fix that by just adding Newtonsoft.Json. to the front of JsonConvert, but that didn't work.

I don't want to remove Newtonsoft's implementation if possible because I think their library still has more functionality. Is there another way to resolve this? Why didn't adding the full assembly reference work?

Solved

  1. In the Properties window for the project's Newtonsoft.Json reference, change the value of Aliases from global to global, foo.

  2. Insert extern alias foo; as the first line of any class that consumes Newtonsoft.Json.

  3. Qualify members with foo.. Example: foo.Newtonsoft.Json.JsonConvert.SerializeObject(someObject)}.


I had the following error message but with another library(Ranet) in C#:

Error CS0433 The type 'JsonConvert' exists in both 'Microsoft.AnalysisServices.Tabular.Json, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' and 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I solved it using aliases, but I would like to provide a bit more detail as I struggled to implement using the instructions from these answers and other answers. This is how I did it:

  1. In the solution explorer, right click on the "Newtonsoft.json", add ", Newton" to the alias field, it should look something like this:

enter image description here

  1. Add the following code to the first line of you file(before all using statements):
extern alias Newton;
  1. Add the following reference to the end of your using statements:
using NewtonReference = Newton::Newtonsoft.Json;
  1. Now you can call Newtonsoft method using the following code:
NewtonReference.JsonConvert.DeserializeObject("");
  1. A final example would look something like this:
extern alias Newton;

using System;
using NewtonReference = Newton::Newtonsoft.Json;

public class Test {     
    public static List TestMethod() {
        NewtonReference.JsonConvert.DeserializeObject("");  
    }  
}

Hopefully this will be helpful to someone else :)


install previous ver of newtonsoft.json package.


Saturday, August 18, 2018

How to create Array with Key Value Pairs in PHP

Anybody could help me with this issue in PHP?

My code:

for ($i=0; $i < count($respuestajson['options']); $i++) {
    foreach ($respuestajson['options'][$i] as $id => $nombre) {
        if($id == "label" || $id == "value"){
            $proyectosZP[$i][$id] = $nombre;
        }
    }
}

My current output is:

array (size=2)
  0 => 
    array (size=2)
      'label' => 'CEV'
      'value' => '10100'
  1 => 
    array (size=2)
      'label' => 'CEX'
      'value' => '10004'

I need this output:

array (size=2)
  'CEV' => '10100'
  'CEX' => '10004'

Thanks very much in advance.

Solved

Simply traverse your array in foreach and combine them

$input_array = array(
    array("label"=>"CEV",'value' => '10100'),array("label"=>"CEX",'value' => '10004')
    );

$new_array = array();
foreach($input_array as $elements)
{
    $new_array[$elements['label']]=$elements['value'];
}
print_r($new_array);

Or from your code:

for ($i=0; $i < count($respuestajson['options']); $i++) {   
            $proyectosZP[$respuestajson['options'][$i]['label']] = $respuestajson['options'][$i]['value'];
}

looking to your data

You should use just $id and $nombre

    foreach ($respuestajson['options'] as $id => $nombre) {
         $proyectosZP[$id] =  $nombre;
    }

You are testing the keys but you can access them directly :

foreach ($respuestajson['options'] as $opt) {
    $proyectosZP[$opt['label']] = $opt['value'];
}

Or, from the current output :

$output = array_combine(array_column($input_array, 'label'),
                        array_column($input_array, 'value'));