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    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030     * @version $Revision$
031     */
032    public class UIBuilder extends BuilderSupport {
033    
034      /** . */
035      private final List<Element> elements;
036    
037      public UIBuilder() {
038        this.elements = new ArrayList<Element>();
039      }
040    
041      public List<Element> getElements() {
042        return elements;
043      }
044    
045      @Override
046      protected Object createNode(Object name) {
047        return createNode(name, (Object)null);
048      }
049    
050      @Override
051      protected Object createNode(Object name, Object value) {
052        if ("node".equals(name)) {
053          if (value == null) {
054            return new TreeElement();
055          } else {
056            return new TreeElement(new LabelElement(value));
057          }
058        } else if ("label".equals(name)) {
059          return new LabelElement(value);
060        } else {
061          throw new UnsupportedOperationException("Cannot build object with name " + name + " and value " + value);
062        }
063      }
064    
065      @Override
066      protected Object createNode(Object name, Map attributes, Object value) {
067        throw new UnsupportedOperationException();
068      }
069    
070      @Override
071      protected Object createNode(Object name, Map attributes) {
072        throw new UnsupportedOperationException();
073      }
074    
075      @Override
076      protected void setParent(Object parent, Object child) {
077        if (parent instanceof TreeElement) {
078          TreeElement parentElement = (TreeElement)parent;
079          Element childElement = (Element)child;
080          parentElement.addNode(childElement);
081        } else {
082          throw new UnsupportedOperationException();
083        }
084      }
085    
086      @Override
087      protected void nodeCompleted(Object parent, Object child) {
088        if (parent == null) {
089          elements.add((Element)child);
090        }
091        super.nodeCompleted(parent, child);
092      }
093    }