-------------------------------------------------------------------------------- -- Title: LWStyle.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LUObject = require( "LUObject" ) local LUBundle = require( "LUBundle" ) local LUList = require( "LUList" ) local LUMap = require( "LUMap" ) -- define the class local super = LUObject local self = super() -- class variable(s) local _attributes = nil -- method to return a style for a given component function self:styleWithComponent( aComponent ) if aComponent ~= nil then local aBuffer = LUList() local someBindings = aComponent:bindings() local someAttributes = self:attributes() for aKey, aValue in someAttributes:iterator() do local aValue = nil if aComponent:respondsTo( aKey ) == true then aValue = aComponent:invoke( aKey ) else aValue = someBindings:get( aKey ) end if aValue ~= nil then local LWString = require( "LWString" ) local anAttribute = someAttributes:get( aKey ) aValue = ( "%s = \"%s\"" ):format( anAttribute, LWString:encode( aValue ) ) aBuffer:add( aValue ) end end if aBuffer:hasData() == true then return aBuffer:join( " " ) end end return nil end -- method to access the style attributes function self:attributes() if _attributes == nil then local aBundle = LUBundle:bundleWithName( "LWStyle" ) local aFile = aBundle:fileWithName( "LWStyle", "txt" ) _attributes = LUMap():load( aFile ) end return _attributes end return self