001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.shell.ui;
021
022 import groovy.util.BuilderSupport;
023 import org.crsh.text.Color;
024 import org.crsh.text.Decoration;
025
026 import java.util.ArrayList;
027 import java.util.List;
028 import java.util.Map;
029
030 /**
031 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032 * @version $Revision$
033 */
034 public class UIBuilder extends BuilderSupport {
035
036 /** . */
037 private final List<Element> elements;
038
039 public UIBuilder() {
040 this.elements = new ArrayList<Element>();
041 }
042
043 public List<Element> getElements() {
044 return elements;
045 }
046
047 @Override
048 protected Object createNode(Object name) {
049 return createNode(name, (Object)null);
050 }
051
052 @Override
053 protected Object createNode(Object name, Object value) {
054
055 return initElement(name, value);
056
057 }
058
059 @Override
060 protected Object createNode(Object name, Map attributes, Object value) {
061 throw new UnsupportedOperationException();
062 }
063
064 @Override
065 protected Object createNode(Object name, Map attributes) {
066
067 Element e = initElement(name, attributes.get("value"));
068 setStyles(e, attributes);
069 return e;
070
071 }
072
073 private Element initElement(Object name, Object value) {
074
075 if ("node".equals(name)) {
076 if (value == null) {
077 return new TreeElement();
078 } else {
079 return new TreeElement(new LabelElement(value));
080 }
081 } else if ("label".equals(name)) {
082 return new LabelElement(value);
083 } else if ("table".equals(name)) {
084 return new TableElement();
085 } else if ("row".equals(name)) {
086 return new RowElement();
087 } else {
088 throw new UnsupportedOperationException("Cannot build object with name " + name + " and value " + value);
089 }
090
091 }
092
093 private void setStyles(Element e, Map attributes) {
094 e.setDecoration((Decoration) attributes.get("decoration"));
095 e.setForeground((Color) attributes.get("foreground"));
096 e.setBackground((Color) attributes.get("background"));
097 }
098
099 @Override
100 protected void setParent(Object parent, Object child) {
101 if (parent instanceof TreeElement) {
102 TreeElement parentElement = (TreeElement)parent;
103 Element childElement = (Element)child;
104 parentElement.addNode(childElement);
105 childElement.setParent(parentElement);
106 } else if (parent instanceof TableElement) {
107 TableElement parentElement = (TableElement)parent;
108 RowElement childElement = (RowElement)child;
109 parentElement.addRow(childElement);
110 childElement.setParent(parentElement);
111 } else if (parent instanceof RowElement) {
112 RowElement parentElement = (RowElement)parent;
113 Element childElement = (Element)child;
114 if (child instanceof TreeElement) {
115 throw new IllegalArgumentException("A table cannot contain node element");
116 }
117 parentElement.addValue(childElement);
118 childElement.setParent(parentElement);
119 } else {
120 throw new UnsupportedOperationException();
121 }
122 }
123
124 @Override
125 protected void nodeCompleted(Object parent, Object child) {
126 if (parent == null) {
127 elements.add((Element)child);
128 }
129 super.nodeCompleted(parent, child);
130 }
131 }