001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-09-10 20:14:27 +0000 (Thu, 10 Sep 2015) $' 007 * '$Revision: 33881 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.ecoinformatics.seek.datasource; 031 032import ptolemy.kernel.util.IllegalActionException; 033import ptolemy.kernel.util.Location; 034import ptolemy.kernel.util.NameDuplicationException; 035import ptolemy.kernel.util.NamedObj; 036import ptolemy.vergil.icon.EditorIcon; 037import ptolemy.vergil.kernel.attributes.EllipseAttribute; 038import ptolemy.vergil.kernel.attributes.LineAttribute; 039import ptolemy.vergil.kernel.attributes.ResizablePolygonAttribute; 040import ptolemy.vergil.kernel.attributes.TextAttribute; 041 042public class DataSourceIcon { 043 044 private static final String TRANSPARENT = "{0.0, 0.0, 0.0, 0.0}"; 045 private static final String WHITE = "{1.0, 1.0, 1.0, 1.0}"; 046 047 private static final String READY_TEXT = ""; 048 private static final String READY_TEXT_COLOR = TRANSPARENT; 049 private static final String READY_BG_COLOR = "{1.0, 0.81, 0.54, 1.0}"; 050 private static final String READY_OUTLINE_COLOR = READY_BG_COLOR; 051 052 private static final String BUSY_TEXT = "BUSY"; 053 private static final String BUSY_TEXT_COLOR = "{0.0, 0.28, 0.54, 1.0}"; 054 private static final String BUSY_BG_COLOR = TRANSPARENT; 055 private static final String BUSY_OUTLINE_COLOR = BUSY_TEXT_COLOR; 056 057 private static final String ERROR_TEXT = "ERROR"; 058 private static final String ERROR_TEXT_COLOR = "{0.85, 0.11, 0.11, 1.0}"; 059 private static final String ERROR_BG_COLOR = TRANSPARENT; 060 private static final String ERROR_OUTLINE_COLOR = ERROR_TEXT_COLOR; 061 062 // size in pixels of blank margin or buffer-zone to be drawn around the 063 // icon: 064 private static final int MARGIN_WIDTH = 5; 065 066 private static final int CYL_WIDTH = 40; 067 private static final int CYL_DIA = 10; 068 private static final int CYL_HEIGHT = 35; 069 070 /** Icon indicating the communication region. */ 071 private EllipseAttribute _topEllipse, _bottomEllipse; 072 073 private LineAttribute _leftLine, _rightLine; 074 075 ResizablePolygonAttribute _mainFill; 076 077 private TextAttribute _text; 078 079 private EditorIcon node_icon; 080 081 /** 082 * Create the DataSource Icon. 083 * 084 * @throws NameDuplicationException 085 * @throws IllegalActionException 086 * @param parent 087 * NamedObj 088 */ 089 public DataSourceIcon(NamedObj parent) throws NameDuplicationException, 090 IllegalActionException { 091 092 node_icon = new EditorIcon(parent, "_icon"); 093 094 _leftLine = new LineAttribute(node_icon, "left"); 095 _leftLine.x.setToken("0"); 096 _leftLine.y.setToken(String.valueOf(CYL_HEIGHT)); 097 Location _location2 = new Location(_leftLine, "_location"); 098 _location2.setExpression("-" + ((long) CYL_WIDTH) / 2 + ", 0.0"); 099 _leftLine.lineColor.setToken(BUSY_OUTLINE_COLOR); 100 101 final String halfCylWidthStr = String.valueOf(((long) CYL_WIDTH) / 2); 102 103 _rightLine = new LineAttribute(node_icon, "right"); 104 _rightLine.x.setToken("0"); 105 _rightLine.y.setToken(String.valueOf(CYL_HEIGHT)); 106 Location _location3 = new Location(_rightLine, "_location"); 107 _location3.setExpression(halfCylWidthStr + ", 0.0"); 108 _rightLine.lineColor.setToken(BUSY_OUTLINE_COLOR); 109 110 _bottomEllipse = new EllipseAttribute(node_icon, "bottom"); 111 _bottomEllipse.width.setToken(String.valueOf(CYL_WIDTH)); 112 _bottomEllipse.height.setToken(String.valueOf(CYL_DIA)); 113 // center the shape by setting this to true. previously, 114 // due to a bug in diva, centering was done with a false value. 115 // see https://projects.ecoinformatics.org/ecoinfo/issues/6834 116 _bottomEllipse.centered.setToken("true"); 117 Location _location4 = new Location(_bottomEllipse, "_location"); 118 _location4.setExpression("0.0, " + CYL_HEIGHT); 119 _bottomEllipse.lineColor.setToken(BUSY_OUTLINE_COLOR); 120 121 _mainFill = new ResizablePolygonAttribute(node_icon, "mainFill"); 122 _mainFill.vertices.setToken("{-" + halfCylWidthStr + ", 0, " 123 + halfCylWidthStr + ", 0, " + halfCylWidthStr + ", " 124 + CYL_HEIGHT + ", " + "-" + halfCylWidthStr + ", " + CYL_HEIGHT 125 + ", " + "-" + halfCylWidthStr + ", 0 }"); 126 _mainFill.width.setToken(String.valueOf(CYL_WIDTH)); 127 _mainFill.height.setToken(String.valueOf(CYL_HEIGHT)); 128 _mainFill.centered.setToken("false"); 129 _mainFill.lineColor.setToken(TRANSPARENT); 130 _mainFill.fillColor.setToken(TRANSPARENT); 131 132 _topEllipse = new EllipseAttribute(node_icon, "top"); 133 _topEllipse.width.setToken(String.valueOf(CYL_WIDTH)); 134 _topEllipse.height.setToken(String.valueOf(CYL_DIA)); 135 // center the shape by setting this to true. previously, 136 // due to a bug in diva, centering was done with a false value. 137 // see https://projects.ecoinformatics.org/ecoinfo/issues/6834 138 _topEllipse.centered.setToken("true"); 139 Location _location1 = new Location(_topEllipse, "_location"); 140 _location1.setExpression("0.0, 0.0"); 141 _topEllipse.lineColor.setToken(BUSY_OUTLINE_COLOR); 142 143 // outline is a transparent box that acts as a buffer zone or spacer 144 // around the entire folder, so the ports don't get too close. The size 145 // of this buffer, in pixels, can be set using the "MARGIN_WIDTH" 146 // 147 // NOTE - bad code - values showing size of folder are hard-coded, 148 // because this code will ultimately be replaced by an SVG image file... 149 // 150 ResizablePolygonAttribute outline = new ResizablePolygonAttribute( 151 node_icon, "outline"); 152 153 outline.vertices.setToken("{-1, 0, 1, 0, 1, 1, -1, 1, -1, 0 }"); 154 155 outline.width.setToken("" + (2 * MARGIN_WIDTH + CYL_WIDTH)); 156 outline.height.setToken("" + (2 * MARGIN_WIDTH + CYL_HEIGHT)); 157 outline.centered.setToken("false"); 158 outline.lineColor.setToken(TRANSPARENT); 159 outline.fillColor.setToken(TRANSPARENT); 160 161 _text = new TextAttribute(node_icon, "text"); 162 _text.textSize.setExpression("11"); 163 _text.bold.setExpression("true"); 164 165 int textVertOffset = (2 * CYL_DIA); 166 Location _location5 = new Location(_text, "_location"); 167 _location5 168 .setExpression("-" + halfCylWidthStr + ", -" + textVertOffset); 169 170 // this is the view that shows up as the tree icon: 171 setReady(); 172 173 node_icon.setPersistent(false); 174 } 175 176 /** 177 * Set the icon to Busy state 178 * 179 */ 180 public void setBusy() { 181 setIconStatus(BUSY_TEXT, BUSY_TEXT_COLOR, BUSY_BG_COLOR, 182 BUSY_OUTLINE_COLOR); 183 } 184 185 /** 186 * Set the icon to Ready state 187 * 188 */ 189 public void setReady() { 190 setIconStatus(READY_TEXT, READY_TEXT_COLOR, READY_BG_COLOR, 191 READY_OUTLINE_COLOR); 192 try { 193 _topEllipse.lineColor.setToken(WHITE); 194 _topEllipse.lineWidth.setToken("2"); 195 } catch (Exception e) { 196 e.printStackTrace(); 197 } 198 } 199 200 /** 201 * Set the icon to Error state 202 * 203 */ 204 public void setError() { 205 setIconStatus(ERROR_TEXT, ERROR_TEXT_COLOR, ERROR_BG_COLOR, 206 ERROR_OUTLINE_COLOR); 207 } 208 209 /** 210 * Set the text and color of the icon 211 * 212 * @param displayText 213 * String 214 * @param textColor 215 * String 216 * @param bgColor 217 * String 218 * @param outlineColor 219 * String 220 */ 221 private void setIconStatus(String displayText, String textColor, 222 String bgColor, String outlineColor) { 223 try { 224 _text.textColor.setToken(textColor); 225 _text.text.setExpression(displayText); 226 _topEllipse.fillColor.setToken(bgColor); 227 _topEllipse.lineColor.setToken(outlineColor); 228 _topEllipse.lineWidth.setToken("1"); 229 _leftLine.lineColor.setToken(outlineColor); 230 _rightLine.lineColor.setToken(outlineColor); 231 _bottomEllipse.fillColor.setToken(bgColor); 232 _bottomEllipse.lineColor.setToken(outlineColor); 233 _mainFill.fillColor.setToken(bgColor); 234 235 } catch (Exception e) { 236 e.printStackTrace(); 237 } 238 } 239 240}