tinyosapplicationspropertypage.java

来自「plugin for eclipse」· Java 代码 · 共 201 行

JAVA
201
字号
package isis.tinydt.properties;

import isis.tinydt.TinydtProject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.PropertyPage;

public class TinyOSApplicationsPropertyPage extends PropertyPage
{
    Button                       buttonRemoveApp  = null;
    Button                       buttonBuildPath  = null;
    TinydtProject                proj             = null;
    org.eclipse.swt.widgets.List applicationNames = null;
    HashMap                      applications     = new HashMap();

    public TinyOSApplicationsPropertyPage()
    {
        super();
        noDefaultAndApplyButton();
    }

    protected void updateProjectData()
    {
        proj.setApplications(applications);
        int sel = applicationNames.getSelectionIndex();
        String new_app = null;
        if(sel >= 0)
            new_app = applicationNames.getItem(sel);
        
        String old_app = proj.getCurrentApp();
        proj.setCurrentApp(new_app);
        proj.writeTinydtFile();
                
        if( new_app!=null && !new_app.equalsIgnoreCase(old_app) )
            proj.parseAll();
    }

    protected void initDialog()
    {
        applicationNames.removeAll();
        applications.clear();

        HashMap apps = proj.getApplications();
        Iterator it = apps.entrySet().iterator();
        int i = 0;
        int sel = 0;
        while(it.hasNext())
        {
            Map.Entry e = (Map.Entry) it.next();           
            TinydtProject.Application app = (TinydtProject.Application) e.getValue();
            TinydtProject.Application appNew = new TinydtProject.Application(app);             
            applicationNames.add(appNew.configurationFileName);
            applications.put(appNew.configurationFileName, appNew);
            if(appNew.configurationFileName.equalsIgnoreCase(proj.getCurrentApp()))
                sel = i;
            i++;
        }
        applicationNames.select(sel);
    }

    protected Control createContents(Composite parent)
    {
        IProject p = (IProject) getElement();
        proj = TinydtProject.getTinydtProject(p);
        //System.out.println(proj);

        Composite c = new Composite(parent, SWT.NONE);

        // new Grid layout with 2 columns
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        c.setLayout(gridLayout);

        // the label of the listbox (full line)
        Label label = new Label(c, SWT.NULL);
        label.setText("Text");
        GridData gridData1 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        gridData1.horizontalSpan = 2;
        label.setLayoutData(gridData1);

        // listbox
        // first columnn
        applicationNames = new org.eclipse.swt.widgets.List(c, SWT.SINGLE | SWT.BORDER);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        // for unit in vertical
        gridData.verticalSpan = 4;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        applicationNames.setLayoutData(gridData);

        // 'add' button
        // second column
        Button buttonAdd = new Button(c, SWT.PUSH);
        buttonAdd.setText("Add...");
        // fill out the space
        buttonAdd.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        buttonAdd.addListener(SWT.Selection, new Listener()
        {
            public void handleEvent(Event event)
            {
                SelectApplicationDialog dlg = new SelectApplicationDialog(getShell(), proj);
                dlg.open();
                if(dlg.getReturnCode() == SelectApplicationDialog.OK)
                {
                    if(applications.get(dlg.selectedFile) == null)
                    {
                        //System.out.println(dlg.selectedFile.getName());
                        TinydtProject.Application app = new TinydtProject.Application();
                        app.configurationFileName = dlg.selectedFile.getFullPath().toString();
                        applicationNames.add(app.configurationFileName);
                        applications.put(app.configurationFileName, app);
                    }
                }
            }
        });

        // 'remove' button
        buttonRemoveApp = new Button(c, SWT.PUSH);
        buttonRemoveApp.setText("Remove");
        // fill out the space
        buttonRemoveApp.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        buttonRemoveApp.addListener(SWT.Selection, new Listener()
        {
            public void handleEvent(Event event)
            {
                int sel = applicationNames.getSelectionIndex();
                if( sel>=0 )
                {
                    String configFileName = applicationNames.getItem(sel);
                    //TinydtProject.Application app = (TinydtProject.Application) applications.get(configFileName);
                    applications.remove(configFileName);
                    applicationNames.remove(sel);                    
                    if(sel < applicationNames.getItemCount())
                        applicationNames.select(sel);
                    else
                        applicationNames.select(sel - 1);                    
                }
            }
        });

        // 'build path' button
        buttonBuildPath = new Button(c, SWT.PUSH);
        buttonBuildPath.setText("Properties...");
        // fill out the space
        buttonBuildPath.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        buttonBuildPath.addListener(SWT.Selection, new Listener()
        {
            public void handleEvent(Event event)
            {
                int sel = applicationNames.getSelectionIndex();
                if(sel >= 0)
                {
                    TinydtProject.Application app = (TinydtProject.Application) applications.get(applicationNames
                            .getItem(sel));
                    EditSearchPathDialog dlg = new EditSearchPathDialog(getShell(), app.additionalIncludeFolders, app.platform, app.sensorBoard );
                    dlg.open();
                    if(dlg.getReturnCode() == SelectApplicationDialog.OK)
                    {
                        // update search path of the application
                        app.additionalIncludeFolders.clear();
                        app.additionalIncludeFolders.addAll(dlg.getIncludeFolders());
                        app.platform = dlg.getPlatform();
                        app.sensorBoard = dlg.getSensorBoard();
                    }
                }
            }
        });

        c.pack();

        initDialog();

        return c;
    }

    protected void performDefaults()
    {
    }

    public boolean performOk()
    {
        updateProjectData();
        return true;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?