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 org.crsh.command.InvocationContext;
023    import org.crsh.shell.io.ShellWriter;
024    import org.crsh.text.Color;
025    import org.crsh.text.Decoration;
026    import org.crsh.text.Style;
027    
028    import java.io.IOException;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    public abstract class Element {
035    
036      /** . */
037      private Decoration decoration;
038    
039      /** . */
040      private Color foreground;
041    
042      /** . */
043      private Color background;
044      
045      /** . */
046      private Element parent;
047    
048      public void print(ShellWriter writer, InvocationContext context) throws IOException {
049        print(new UIWriterContext(context), writer);
050      }
051    
052      public void print(UIWriterContext ctx, ShellWriter writer) throws IOException {
053    
054        if (ctx == null) {
055          throw new NullPointerException();
056        }
057    
058        if (haveStyle()) {
059          new FormattingElement(Style.style(getDecoration(), getForeground(), getBackground())).print(ctx, writer);
060        }
061    
062        doPrint(ctx, writer);
063    
064        if (haveStyle()) {
065          new FormattingElement(Style.reset).print(ctx, writer);
066        }
067        
068      }
069    
070      abstract void doPrint(UIWriterContext ctx, ShellWriter writer) throws IOException;
071      
072      abstract int width();
073    
074      private boolean haveStyle() {
075        return (getDecoration() != null) || (getForeground() != null) || (getBackground() != null);
076      }
077      
078      public Decoration getDecoration() {
079    
080        if (decoration != null) {
081          return decoration;
082        } else if (parent != null) {
083          return parent.getDecoration();
084        } else {
085          return null;
086        }
087        
088      }
089    
090      public Color getForeground() {
091    
092        if (foreground != null) {
093          return foreground;
094        } else if (parent != null) {
095          return parent.getForeground();
096        } else {
097          return null;
098        }
099    
100      }
101    
102      public Color getBackground() {
103    
104        if (background != null) {
105          return background;
106        } else if (parent != null) {
107          return parent.getBackground();
108        } else {
109          return null;
110        }
111            
112      }
113    
114      public void setDecoration(Decoration decoration) {
115        this.decoration = decoration;
116      }
117    
118      public void setForeground(Color foreground) {
119        this.foreground = foreground;
120      }
121    
122      public void setBackground(Color background) {
123        this.background = background;
124      }
125    
126      public Element getParent() {
127        return parent;
128      }
129    
130      public void setParent(Element parent) {
131        this.parent = parent;
132      }
133    }