1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.javafx.experiments.scheduleapp.control;
  33 
  34 import static com.javafx.experiments.scheduleapp.Theme.*;
  35 import javafx.scene.control.skin.ListCellSkin;
  36 import javafx.scene.control.skin.ListViewSkin;
  37 import javafx.event.EventHandler;
  38 import javafx.geometry.Bounds;
  39 import javafx.scene.control.ListCell;
  40 import javafx.scene.control.ListView;
  41 import javafx.scene.image.ImageView;
  42 import javafx.scene.input.MouseEvent;
  43 import javafx.util.Callback;
  44 
  45 /**
  46  * Special ListView designed to look like "Text... >" tree list. Perhaps we ought to have customized
  47  * a TreeView instead of a ListView (as the TreeView already has the data model all defined).
  48  *
  49  * This implementation minimizes classes by just having the PopoverTreeList implementing everything
  50  * (it is the Control, the Skin, and the CellFactory all in one).
  51  */
  52 public class PopoverTreeList<T> extends ListView<T> implements Callback<ListView<T>, ListCell<T>> {
  53     
  54     public PopoverTreeList(){
  55         getStyleClass().clear();
  56         setSkin(new ListViewSkin<T>(this));
  57         setCellFactory(this);
  58     }
  59 
  60     @Override public ListCell<T> call(ListView<T> p) {
  61         return new TreeItemListCell();
  62     }
  63     
  64     protected void itemClicked(T item) {}
  65 
  66     private class TreeItemListCell extends ListCell<T> implements EventHandler<MouseEvent> {
  67         private ImageView arrow = new ImageView(RIGHT_ARROW);
  68 
  69         private TreeItemListCell() {
  70             super();
  71             getStyleClass().setAll("popover-tree-list-cell");
  72             setSkin(new ListCellSkin(this));
  73             setFont(BASE_FONT);
  74             setTextFill(DARK_GREY);
  75             getChildren().add(arrow);
  76             setOnMouseClicked(this);
  77         }
  78         
  79         @Override public void handle(MouseEvent t) {
  80             itemClicked(getItem());
  81         }
  82 
  83         @Override protected double computePrefWidth(double height) {
  84             return 100;
  85         }
  86 
  87         @Override protected double computePrefHeight(double width) {
  88             return 44;
  89         }
  90 
  91         @Override protected void layoutChildren() {
  92             super.layoutChildren();
  93             final int w = (int)getWidth();
  94             final int h = (int)getHeight();
  95             final int centerX = (int)(w/2d);
  96             final int centerY = (int)(h/2d);
  97             final Bounds arrowBounds = arrow.getLayoutBounds();
  98             arrow.setLayoutX(w - arrowBounds.getWidth() - 12);
  99             arrow.setLayoutY((int)((h - arrowBounds.getHeight())/2d));
 100         }
 101         
 102         // CELL METHODS
 103         @Override protected void updateItem(T item, boolean empty) {
 104             // let super do its work
 105             super.updateItem(item,empty);
 106             // update our state
 107             if (item == null) { // empty item
 108                 setText(null);
 109                 arrow.setVisible(false);
 110             } else {
 111                 setText(item.toString());
 112                 arrow.setVisible(true);
 113             }
 114         }
 115     }
 116 }