在Vaadin中动态注入CSS

时间:2013-01-29 06:39:07

标签: java vaadin

我正在使用Java Vaadin框架开发一个应用程序,我正在使用它的Calendar Add-on。该日历上有一些名为“基本事件”的组件。现在我想通过在运行时动态创建CSS类来在运行时为该组件着色。我怎样才能实现这个功能?任何帮助! 谢谢!

3 个答案:

答案 0 :(得分:4)

我已尝试并成功使用以下代码。

   package com.example.cssinject;

   import org.vaadin.cssinject.CSSInject;

   import com.vaadin.Application;
   import com.vaadin.ui.*;

    public class CssinjectApplication extends Application {
    @Override
    public void init() {

    final Window mainWindow = new Window("Cssinject Application");
    final Label label = new Label("Hello Vaadin user");
    mainWindow.addComponent(label);

    CSSInject css = new CSSInject();
    css.setValue(".custom-style { color: rgb(100, 200, 0); }");
    mainWindow.addComponent(css);

    label.setStyleName("custom-style");

    setMainWindow(mainWindow);  
}
}

答案 1 :(得分:3)

我使用了Sumit Singh提到的插件,当前"本地" -way是https://vaadin.com/wiki/-/wiki/Main/Dynamically%20injecting%20CSS

简要说明:

import os,sys
from pyrouge import Rouge155
import json
from optparse import OptionParser

def get_opts():
    parser = OptionParser()
    parser.add_option("--stringa", dest="str_a",help="First string")
    parser.add_option("--stringb", dest= "str_b",help="second string")
    parser.add_option("--writedir", dest="write_dir", help="Tmp write directory for rouge")

    (options, args) = parser.parse_args()

    if options.str_a is None:
        print("Error: requires string")
        parser.print_help()
        sys.exit(-1)

    if options.str_b is  None:
        print("Error:requires string")
        parser.print_help()
        sys.exit(-1)

    if options.write_dir is  None:
        print("Error:requires write directory for rouge")
        parser.print_help()
        sys.exit(-1)    

    return (options, args)

def readTextFile(Filename): 
        f = open(Filename, "r", encoding='utf-8')
        TextLines=f.readlines()
        f.close()
        return TextLines

def writeTextFile(Filename,Lines): 
        f = open(Filename, "w",encoding='utf-8')
        f.writelines(Lines)
        f.close()

def rougue(stringa, stringb, writedirRouge):
    newrow={}
    r = Rouge155()
    count=0
    dirname_sys= writedirRouge +"rougue/System/"
    dirname_mod=writedirRouge +"rougue/Model/"
    if not os.path.exists(dirname_sys):
        os.makedirs(dirname_sys)
    if not os.path.exists(dirname_mod):  
        os.makedirs(dirname_mod)
    Filename=dirname_sys +"string_."+str(count)+".txt"
    LinesA=list()
    LinesA.append(stringa)
    writeTextFile(Filename, LinesA)
    LinesB=list()
    LinesB.append(stringb)
    Filename=dirname_mod+"string_.A."+str(count)+ ".txt"
    writeTextFile(Filename, LinesB)
    r.system_dir = dirname_sys
    r.model_dir = dirname_mod
    r.system_filename_pattern = 'string_.(\d+).txt'
    r.model_filename_pattern = 'string_.[A-Z].#ID#.txt'
    output = r.convert_and_evaluate()
    output_dict = r.output_to_dict(output)
    newrow["rouge_1_f_score"]=output_dict["rouge_1_f_score"]
    newrow["rouge_2_f_score"]=output_dict["rouge_2_f_score"]
    newrow["rouge_3_f_score"]=output_dict["rouge_3_f_score"]
    newrow["rouge_4_f_score"]=output_dict["rouge_4_f_score"]
    newrow["rouge_l_f_score"]=output_dict["rouge_l_f_score"]
    newrow["rouge_s*_f_score"]=output_dict["rouge_s*_f_score"]
    newrow["rouge_su*_f_score"]=output_dict["rouge_su*_f_score"]
    newrow["rouge_w_1.2_f_score"]=output_dict["rouge_w_1.2_f_score"]
    rouge_dict=json.dumps(newrow)
    print (rouge_dict)


def run():
    (options, args) = get_opts()   
    stringa=options.str_a
    stringb=options.str_b
    writedir=options.write_dir 
    rougue(stringa, stringb, writedir)


if __name__ == '__main__':
    run()

答案 2 :(得分:0)

对此主题不太了解,但您可以看一下以下内容。

  1. CSSInject add-on