1 /*
   2  * Copyright (c) 2012, 2014, 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.oracle.javafx.scenebuilder.kit.metadata.klass;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.metadata.property.PropertyMetadata;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import java.util.HashSet;
  37 import java.util.Iterator;
  38 import java.util.Set;
  39 
  40 /**
  41  *
  42  *
  43  */
  44 public class ComponentClassMetadata extends ClassMetadata {
  45 
  46     private final Set<PropertyMetadata> properties = new HashSet<>();
  47     private final boolean freeChildPositioning;
  48     private final ComponentClassMetadata parentMetadata;
  49 
  50     public ComponentClassMetadata(Class<?> klass, ComponentClassMetadata parentMetadata) {
  51         super(klass);
  52         this.parentMetadata = parentMetadata;
  53         this.freeChildPositioning = false; // TODO(elp)
  54     }
  55 
  56     public Set<PropertyMetadata> getProperties() {
  57         return properties;
  58     }
  59 
  60     public PropertyName getSubComponentProperty() {
  61         PropertyName result = null;
  62         Class<?> componentClass = getKlass();
  63 
  64         if (componentClass == javafx.scene.layout.BorderPane.class) {
  65             // We consider that BorderPane has no subcomponents.
  66             // left, right, bottom and top components are treated as "accessories".
  67             result = null;
  68         } else if (componentClass == javafx.scene.control.DialogPane.class) {
  69             // We consider that DialogPane has no subcomponents.
  70             // content, expanded content, header and graphic components are treated as "accessories".
  71             result = null;
  72         } else {
  73             while ((result == null) && (componentClass != null)) {
  74                 result = getSubComponentProperty(componentClass);
  75                 componentClass = componentClass.getSuperclass();
  76             }
  77         }
  78 
  79         return result;
  80     }
  81 
  82     public boolean isFreeChildPositioning() {
  83         return freeChildPositioning;
  84     }
  85 
  86     public ComponentClassMetadata getParentMetadata() {
  87         return parentMetadata;
  88     }
  89 
  90     public PropertyMetadata lookupProperty(PropertyName propertyName) {
  91         PropertyMetadata result = null;
  92 
  93         assert propertyName != null;
  94 
  95         final Iterator<PropertyMetadata> it = properties.iterator();
  96         while ((result == null) && it.hasNext()) {
  97             final PropertyMetadata pm = it.next();
  98             if (pm.getName().equals(propertyName)) {
  99                 result = pm;
 100             }
 101         }
 102 
 103         return result;
 104     }
 105 
 106     /*
 107      * Object
 108      */
 109 
 110     @Override
 111     public int hashCode() {
 112         return super.hashCode(); // Only to please FindBugs
 113     }
 114 
 115     @Override
 116     public boolean equals(Object obj) {
 117         return super.equals(obj); // Only to please FindBugs
 118     }
 119 
 120 
 121     /*
 122      * Private
 123      */
 124 
 125     private static PropertyName getSubComponentProperty(Class<?> componentClass) {
 126         final PropertyName result;
 127 
 128         assert componentClass != javafx.scene.layout.BorderPane.class
 129                 && componentClass != javafx.scene.control.DialogPane.class;
 130 
 131         /*
 132          * Component Class -> Sub Component Property
 133          * =========================================
 134          *
 135          * Accordion        panes
 136          * ButtonBar        buttons
 137          * ContextMenu      items
 138          * Menu             items
 139          * MenuBar          menus
 140          * MenuButton       items
 141          * Path             elements
 142          * SplitPane        items
 143          * SplitMenuButton  items
 144          * TableColumn      columns
 145          * TableView        columns
 146          * TabPane          tabs
 147          * ToolBar          items
 148          * TreeTableColumn  columns
 149          * TreeTableView    columns
 150          *
 151          * Group            children
 152          * Panes            children
 153          * Other            null
 154          */
 155 
 156         if (componentClass == javafx.scene.control.Accordion.class) {
 157             result = panesName;
 158         } else if (componentClass == javafx.scene.control.ButtonBar.class) {
 159             result = buttonsName;
 160         } else if (componentClass == javafx.scene.control.ContextMenu.class) {
 161             result = itemsName;
 162         } else if (componentClass == javafx.scene.control.Menu.class) {
 163             result = itemsName;
 164         } else if (componentClass == javafx.scene.control.MenuBar.class) {
 165             result = menusName;
 166         } else if (componentClass == javafx.scene.control.MenuButton.class) {
 167             result = itemsName;
 168         } else if (componentClass == javafx.scene.shape.Path.class) {
 169             result = elementsName;
 170         } else if (componentClass == javafx.scene.control.SplitMenuButton.class) {
 171             result = itemsName;
 172         } else if (componentClass == javafx.scene.control.SplitPane.class) {
 173             result = itemsName;
 174         } else if (componentClass == javafx.scene.control.TableColumn.class) {
 175             result = columnsName;
 176         } else if (componentClass == javafx.scene.control.TableView.class) {
 177             result = columnsName;
 178         } else if (componentClass == javafx.scene.control.TabPane.class) {
 179             result = tabsName;
 180         } else if (componentClass == javafx.scene.control.ToolBar.class) {
 181             result = itemsName;
 182         } else if (componentClass == javafx.scene.control.TreeTableColumn.class) {
 183             result = columnsName;
 184         } else if (componentClass == javafx.scene.control.TreeTableView.class) {
 185             result = columnsName;
 186         } else if (componentClass == javafx.scene.Group.class) {
 187             result = childrenName;
 188         } else if (componentClass == javafx.scene.layout.Pane.class) {
 189             result = childrenName;
 190         } else {
 191             result = null;
 192         }
 193 
 194         return result;
 195     }
 196 
 197     private static final PropertyName buttonsName = new PropertyName("buttons");
 198     private static final PropertyName columnsName = new PropertyName("columns");
 199     private static final PropertyName elementsName = new PropertyName("elements");
 200     private static final PropertyName itemsName = new PropertyName("items");
 201     private static final PropertyName menusName = new PropertyName("menus");
 202     private static final PropertyName panesName = new PropertyName("panes");
 203     private static final PropertyName tabsName = new PropertyName("tabs");
 204     private static final PropertyName childrenName = new PropertyName("children");
 205 
 206 }