简体   繁体   English

如何通过字符串类名在GWT中创建新对象?

[英]How to create new object in GWT by string class-name?

My goal is to create a flexible application-> to extend the functionality of my web-application I only to add a position to the database and upload the missing class to the server. 我的目标是创建一个灵活的应用程序 - >扩展我的Web应用程序的功能我只是为数据库添加一个位置并将缺少的类上传到服务器。

I created dynamic menu in Java-GWT. 我在Java-GWT中创建了动态菜单。 I use MenuBar() and loops, all positions, wchich were taken from db (array, string). 我使用MenuBar()和循环,所有位置,wchich都是从db(数组,字符串)中获取的。 Of course, 'end-option' in menu must do something, generally after clicking application will open ie FlowPanel with buttons, labels, textareas. 当然,菜单中的'end-option'必须做一些事情,一般在点击应用程序后会打开,即带有按钮,标签,textareas的FlowPanel。

My idea was: using java reflection to add Command for all positions in menu. 我的想法是:使用java反射为菜单中的所有位置添加Command。 Every Command take name of option (string), name is the same, like name of class -> next, after clicking I will automatically create object. 每个Command取名称选项(字符串),名称相同,如类名称 - >接下来,点击后我会自动创建对象。 Unfortunatelly Java Reflection don't work with GWT, so this way is not possible. 不幸的是,Java Reflection不能与GWT一起使用,所以这种方式是不可能的。

My question is: 我的问题是:

How to create object, when I have class name in string? 当我在字符串中有类名时,如何创建对象? Or if my idea is wrong, maybye is other way to create flexible menu/program? 或者,如果我的想法是错误的,maybye是另一种创建灵活菜单/程序的方法吗?

I thing, I found a small solution for this problem, but with another concept - only one class (and one interface); 我的事情,我找到了一个小问题的解决方案,但有另一个概念 - 只有一个类(和一个接口); in this class I have one main method, which choose correct 'method to open'; 在这个课程中,我有一个主要的方法,选择正确的'打开方法'; in this example I check by int: 在这个例子中,我通过int检查:

/ ------------------------------------------- / Example loop in main part of program: / ------------------------------------------- / (...) / ------------------------------------------- /主要部分的示例循环程序:/ ------------------------------------------- /(。 ..)

MenuBar menu = new MenuBar(true);

    for (int k = 0; k < 5; k++) {
            int ii=k;
            menu.addItem(tekst + k, new Command() {

                @Override
                public void execute() {
                    ClassMetodInterface metoda = (ClassMetodInterface) GWT.create(Method2.class);
                    vPanel.clear(); //clearing before I open a new widget.

/*I sent int (easier to tests), but I can sent string[i] too*/
/* logging - instance of main class to communicate with 'subclasses', in my project necessery*/

                    vPanel.add(metoda.defaultMethod(ii, logging));  
                }

            });
        }
(...)
/*-------------------------------------------*/
ClassMetodInterface, only for implementation class Method2 :
/*-------------------------------------------*/
    public interface ClassMetodInterface {
    Widget defaultMethod(int g, Logging logging);
}

/*-------------------------------------------*/
class Method2 with swich:
/*-------------------------------------------*/


public class Method2 implements ClassMetodInterface {

    public Widget zapisUsera(int g, Logging logging) {

        Logging logging;
        HorizontalPanel lPanel = new HorizontalPanel();

        switch(g) {
        case 1: {
            Window.alert("switch for test no:"+g);
            MenuWindow1(logging);

            break;

        }
        case 2:{
            Window.alert("switch for test no:"+g);
            break;
        }
        default:
            Window.alert("switch default - loop >2 && <1");
            break;
        }
        return lPanel;  //all methods will be void - they will be add widgets and Panels to lPanel

    }

    public Widget MenuWindow1(Logging logging) {
        this.logging = logging;

        lPanel.setBorderWidth(2);
        this.lPanel.setPixelSize(100, 50); 
        Button b1 = new Button("test button, not used");    
        lPanel.add(b1);

        Label lbl = new Label("label no "+logging.i);       

        lPanel.add(lbl);
        Button addGlobal = new Button("+");
        Button removeGlobal = new Button("-");
        lPanel.add(addGlobal);
        addGlobal.addClickHandler(new addGlobal());
        removeGlobal.addClickHandler(new removeGlobal());
        lPanel.add(removeGlobal);


        return lPanel;

    }

//for tests communication with class, where I have menu and global variable
    public class addGlobal implements ClickHandler {

        @Override
        public void onClick(ClickEvent event) {
            logging.i++;
        }

    }

    public class removeGlobal implements ClickHandler {

        @Override
        public void onClick(ClickEvent event) {
            logging.i--;
        }

    }
}

It is not finished, but you can show main idea. 它没有完成,但你可以显示主要想法。 If I need to add new options to menu, I will update db and replace one class for version with more methods. 如果我需要向菜单添加新选项,我将更新db并使用更多方法替换一个类的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM