/******************************************************************* Copyright (c) 2001-2010 Apprisant Technologies Inc. All rights reserved. Apprisant Technologies permits non-exclusive royalty free use, copying and modification of this demo program source code for the purposes of evaluating the Apprisant Toolkit or building applications that use it. This software and documentation is provided "AS IS". APPRISANT TECHNOLOGIES DISCLAIMS ANY REPRESENTATIONS AND WARRANTIES, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY AND FITNESS OF THIS PRODUCT FOR ANY PARTICULAR PURPOSE. APPRISANT TECHNOLOGIES SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY ANY PARTY, INCLUDING LOST PROFITS, ARISING FROM THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ITS DERIVATIONS. ********************************************************************/ package red; import java.awt.Shape; import java.util.*; import javax.swing.Icon; import apprisant.diagram.*; import apprisant.diagram.render.*; import apprisant.treelayout.*; /** The element class for the Red demo. The data object is the renderer this element represents in the diagram. This class builds the description string and provides the parentKey property which TreeLayout uses to position the elements. This class has to extend TreeElement to work with the TreeLayout. */ public class RedElement extends TreeElement { // This element's parent element RedElement parent = null; // The value our renderer is associated with in its parent private String selector; // The description text private String description; public RedElement (String key, AGRenderer ren, RedElement parent, String selector) { super(key, ren); this.parent = parent; this.selector = selector; setDescription(ren); } /* The parent element reference can be used as the parent key for the TreeLayout because the model hashes elements by both key and reference. */ public RedElement getParentKey() { return parent; } public String getSelector() { return selector; } public String getDescription() { return description; } /* Build a description string based on the type of renderer. The result contains formatting directives because it will be displayed by a FormattedTextRenderer. */ private void setDescription (AGRenderer ren) { if (ren == null) { this.description = "<null>"; return; } StringBuffer desc = new StringBuffer(); // ID in red if it has one, then the renderer class String id = ren.getId(); if (id != null && id.length() > 0) { desc.append("{paint:keyColor}"); desc.append(id).append(": "); desc.append("{paint:normal}"); } desc.append(getRendererName(ren)); // The descriptor if any can be found String d = null; DeferredValue dv = null; if (ren instanceof BoundsRenderer) { d = ((BoundsRenderer)ren).getReferenceId(); } else if (ren instanceof ShapeRenderer) { dv = ren.getDeferredValue("shape"); if (dv == null) { Shape s = ((ShapeRenderer)ren).getShape(); if (s != null) { d = RedModel.trimClassname(s.getClass(), true); } } } else if (ren instanceof TextRenderer) { dv = ren.getDeferredValue("text"); if (dv == null) { String t = ((TextRenderer)ren).getText(); if (t != null && t.length() > 0) { if (ren instanceof FormattedTextRenderer) { d = " <format string> "; } else d = " <text> "; } } } else if (ren instanceof ImageRenderer) { dv = ren.getDeferredValue("image"); if (dv == null) { d = RedModel.trimPathname(((ImageRenderer)ren).getImagePath()); } } else if (ren instanceof IconRenderer) { dv = ren.getDeferredValue("icon"); if (dv == null) { Icon t = ((IconRenderer)ren).getIcon(); if (t != null) { d = RedModel.trimClassname(t.getClass(), false); } } } else if (ren instanceof OptionRenderer) { d = ((OptionRenderer)ren).getSelector(); } else if (ren instanceof BooleanRenderer) { d = ((BooleanRenderer)ren).getSelector(); } if (dv != null) d = getShortDesc(dv); if (d != null) desc.append(" (").append(d).append(")"); this.description = desc.toString(); } private String getRendererName (AGRenderer ren) { String s = RedModel.trimClassname(ren.getClass(), false); if (s.endsWith("Renderer")) { s = s.substring(0, s.length() - "Renderer".length()); } return s; } private String getShortDesc (DeferredValue dv) { String desc = null; if (dv instanceof ElementValue) { desc = ((ElementValue) dv).getAttribute(); } else if (dv instanceof ValueMap) { desc = ((ValueMap) dv).getSelector(); } else desc = " <script> "; return desc; } }