Eclipse RCP:如何让我的代码更加强大?

时间:2017-01-09 08:53:43

标签: java eclipse performance optimization rcp

我被告知设计一棵树,点击Node应该提供一个人的详细信息,并且应该显示一个图像。我使用了If-Else语句来做到这一点并被告知它不是编码的标准,我怎样才能使它真正有效,以便遵循编码标准。

这是我的Eclipse RCP视图代码。当选择一个人的节点时,它会显示人员的表格和图像内容。 RCP Application Output here

package com.cerner.imagetagger.view;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

public class JViewer extends ViewPart{
    public JViewer() {}
    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout(SWT.HORIZONTAL));
        final Tree tree = new Tree(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        Label labelImg = new Label(parent,SWT.NONE);
        Table table = new Table(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
        final ImageLoader loader = new ImageLoader();
        loader.load(getClass().getClassLoader().getResourceAsStream("icons/default.png"));
        final Image image = new Image(Display.getCurrent(), loader.data[0]);
        labelImg.setImage(image);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        TreeItem item0 = new TreeItem(tree, 0);
        item0.setText("Designation");

        TreeItem item1 = new TreeItem(item0, 0);
        item1.setText("Software Engineer");

        TreeItem item2 = new TreeItem(item1, 0);
        item2.setText("Tansheet Izhad");

        TreeItem item21 = new TreeItem(item1, 0);
        item21.setText("Nikhil Gautam");

        String[] titles = {"Name", "Age"};

        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(titles[i]);
        }


        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, "");
        item.setText(1, "");

        for (int i=0; i<titles.length; i++) {
            table.getColumn (i).pack ();
        }     

        tree.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                String treeNode = "";
                String resultwb= "";
                String result="";
                TreeItem[] selection = tree.getSelection();

                for (int i = 0; i < selection.length; i++)

                    treeNode += selection[i] + " ";

                resultwb = treeNode.replaceAll("[{}]", "");
                result=resultwb.replace("TreeItem ","");      


                if(result.matches("(.*)Tansheet Izhad(.*)"))
                {
                    item.setText(0, "TI");
                    item.setText(1, "23");   
                    loader.load(getClass().getClassLoader().getResourceAsStream("icons/tansheet.png"));
                    final Image image = new Image(Display.getCurrent(), loader.data[0]);
                    labelImg.setImage(image);

                }

                else    
                {
                    item.setText(0, "NULL");
                    item.setText(1, "NULL");
                    loader.load(getClass().getClassLoader().getResourceAsStream("icons/default.png"));
                    final Image image = new Image(Display.getCurrent(), loader.data[0]);
                    labelImg.setImage(image);
                }           
            }
        });    

        tree.getItems()[0].setExpanded(true);
    }

    @Override
    public void setFocus() {}

}

有什么建议吗?

0 个答案:

没有答案