RAP Step by step--三、与spring,hibernate一起工作 5

8)修改Activator.java,粗体字为新增的内容

package com.sword.rapdemo;

 

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.ui.plugin.AbstractUIPlugin;

import org.osgi.framework.BundleContext;

 

/**

 * The activator class controls the plug-in life cycle

 */

public class Activator extends AbstractUIPlugin {

 

       // The plug-in ID

       public static final String PLUGIN_ID = "com.sword.rapdemo";

 

       // The shared instance

       private BundleContext context;

      

       private static Activator plugin;

      

       /**

        * The constructor

        */

       public Activator() {

       }

 

       /*

        * (non-Javadoc)

        * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)

        */

       public void start(BundleContext context) throws Exception {

              super.start(context);

              plugin = this;

              this.context = context;       

       }

 

       /*

        * (non-Javadoc)

        * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)

        */

       public void stop(BundleContext context) throws Exception {

              plugin = null;

              super.stop(context);

       }

 

       /**

        * Returns the shared instance

        *

        * @return the shared instance

        */

       public static Activator getDefault() {

              return plugin;

       }

      

       public BundleContext getContext() {

                return context;

       }

       /**

        * Returns an image descriptor for the image file at the given

        * plug-in relative path

        *

        * @param path the path

        * @return the image descriptor

        */

       public static ImageDescriptor getImageDescriptor(String path) {

              return imageDescriptorFromPlugin(PLUGIN_ID, path);

       }

}

9)修改ClientPart.java增加测试代码,查看一下spring的功能是否可用.

代码如下,加粗的为新增内容

package com.sword.rapdemo;

 

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.IToolBarManager;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.events.SelectionListener;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.ui.part.ViewPart;

import org.osgi.framework.BundleContext;

 

public class ClientPart extends ViewPart {

 

       public static final String ID = "com.sword.rapdemo.ClientPart"; //$NON-NLS-1$

 

       /**

        * Create contents of the view part

        * @param parent

        */

       private BundleContext context = Activator.getDefault().getContext();

       @Override

       public void createPartControl(Composite parent) {

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

              container.setLayout(new GridLayout());

              Button button=new Button(container,SWT.NONE);

              button.addSelectionListener(new SelectionListener(){

 

                     public void widgetDefaultSelected(SelectionEvent e) {

                           

                     }

 

                     public void widgetSelected(SelectionEvent e) {

                            System.out.println("This is a test :-) ...");

                            try {

                                   EmployeeService service = (EmployeeService) context.getService(context

                                                 .getServiceReference(EmployeeService.class.getName()));

                                  List<Employee>emplist=service.findAll();

                                   for(int i=0;i<emplist.size();i++){

                                         System.out.println("Employee:"+((Employee)emplist.get(i)).getEmployeeid()+","+((Employee)emplist.get(i)).getEmployeename());

                                   }

                                                                     

                            } catch (Exception ex) {

                                   ex.printStackTrace();

                            }                          

                     }

                    

                    

              });

              button.setText("Click me :)");

              //

              createActions();

              initializeToolBar();

              initializeMenu();

       }

 

       /**

        * Create the actions

        */

       private void createActions() {

              // Create the actions

       }

 

       /**

        * Initialize the toolbar

        */

       private void initializeToolBar() {

              IToolBarManager toolbarManager = getViewSite().getActionBars()

                            .getToolBarManager();

       }

 

       /**

        * Initialize the menu

        */

       private void initializeMenu() {

              IMenuManager menuManager = getViewSite().getActionBars()

                            .getMenuManager();

       }

 

       @Override

       public void setFocus() {

              // Set the focus

       }

 

}

 

你可能感兴趣的:(eclipse,spring,工作,Hibernate,osgi)