Eclipse RCP Tutorial: Eclipse Rich Client Application with a View

 

Eclipse RCP provides an easy way to create desktop applications with industry standards. Once you understand how the framework works, it’s very straightforward to add more views, perspectives, etc.

This article shows how to create a simple rich client application with a view by using development wizard. It also explains the role of each file created. I assume you already have PDE installed. If you don't, you can quickly find out how to do that from the "How to install PDE" tutorial.

1. Steps for creating a simple RCP application with a View

The following 4 pictures show the 4 steps for creating a simple app with wizard.

1-create-a-plugin-project.jpg

 

2-set-plugin-project-name.jpg

 

 

3-data-required-to-generate-plugin.jpg

 

The template used is "RCP Application with a View".

4.eclipse-application-with-a-view.jpg

 

2. Files created and their functions

Here is the files created automatically by Eclipse.
5.RCPView.jpg

 

Eclipse RCP provides the concept of "extension points" and "extensions" to facilitate commercial desktop application development. It provides a way to use the whole framework easily through plugin.xml file which is under root directory of each directory.

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
 
   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="rcpview.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="Perspective"
            class="rcpview.Perspective"
            id="RCPView.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.views">
      <view
            name="View"
            class="rcpview.View"
            id="RCPView.view">
      </view>
   </extension>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="*">
         <view
               standalone="true"
               minimized="false"
               relative="org.eclipse.ui.editorss"
               relationship="left"
               id="RCPView.view">
         </view>
      </perspectiveExtension>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="File">
            <command
                  commandId="org.eclipse.ui.file.exit"
                  label="Exit">
            </command>
         </menu>
      </menuContribution>
   </extension>
 
</plugin>

During the startup of an Eclipse RCP application, the Eclipse runtime detects main entry point class through extension point defined in plugin.xml. The class implements IApplication interface, and is loaded first like the main function in a general Java application.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Application.java

package rcpview;
 
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
 
/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {
 
	public Object start(IApplicationContext context) {
		Display display = PlatformUI.createDisplay();
		try {
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART) {
				return IApplication.EXIT_RESTART;
			}
			return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}
	}
 
	public void stop() {
		if (!PlatformUI.isWorkbenchRunning())
			return;
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
}

Application class creates and runs a Workbench.

What is a Workbench?
The Workbench builds on top of the Runtime, SWT and JFace to provide a highly scalable, open-ended, multi-window environment for managing views, editors, perspectives (task-oriented layouts), actions, wizards, preference pages, and more.

The Workbench is configured via WorkbenchAdvisor. WorkbenchAdvisor tells the Workbench how to behave, how to draw, what to draw, and so forth. In particular, WorkbenchAdvisor identifies two things(check the code below):

1. The initial perspective to be shown.
2. which WorkbenchWindowAdvisor to use.

ApplicationWorkbenchAdvisor.java

package rcpview;
 
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
 
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
 
	private static final String PERSPECTIVE_ID = &quot;RCPView.perspective&quot;;
 
	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}
 
	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}
 
}

The workbench starts WorkbenchWindow which is configured via WorkbenchWindowAdvisor.

ApplicationWorkbenchWindowAdvisor.java

package rcpview;
 
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
 
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
 
	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}
 
	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}
 
        //preWindowOpen method configure the initial window size and positions.
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(400, 300));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(false);
		configurer.setTitle(&quot;RCP Application&quot;);
	}
}

WorkbenchWindowAdvisor creates the toolbar/menubar of the application which is configured via ActionBarAdvisor. The ApplicationActionBarAdvisor class in the following is too simple to implement any methods. But generally, the following two methods can be used to fill menu bar and tool bar:

protected void fillMenuBar(IMenuManager menuBar) { }
protected void fillCoolBar(ICoolBarManager coolBar) { }

ApplicationActionBarAdvisor.java

package rcpview;
 
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
 
/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
 
	// Actions - important to allocate these only in makeActions, and then use
	// them
	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.
 
	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}
 
}

On a typical appliation, when you start it, there is a window shown up. Perspective is like a page in a book, while the book is the window. A perspective is a container for visual elements like views, editors, and actions. Each perspective may contain 0 or several views depending on the design.

Perspective.java

package rcpview;
 
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
 
public class Perspective implements IPerspectiveFactory {
 
	public void createInitialLayout(IPageLayout layout) {
		layout.setEditorAreaVisible(false);
		layout.setFixed(true);
 
	}
 
}

A view is a workbench part to perform a visual task, such as navigating a tree directory, editing an email, etc. It provide a better way to organize visual components based on their functions or purposes.

View.java

package rcpview;
 
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
 
public class View extends ViewPart {
	public static final String ID = &quot;RCPView.view&quot;;
 
	private TableViewer viewer;
 
	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
 
		public void dispose() {
		}
 
		public Object[] getElements(Object parent) {
			if (parent instanceof Object[]) {
				return (Object[]) parent;
			}
	        return new Object[0];
		}
	}
 
	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
 
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
 
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
 
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		// Provide the input to the ContentProvider
		viewer.setInput(new String[] {&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;});
	}
 
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}

Click Eclipse RCP Development Tutorials to see more advanced topics.

 

 

[출처] http://www.programcreek.com/2011/03/eclipse-tutorial-for-a-simple-desktop-application/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
164 Java 8 개선 사항 관련 글 모음 졸리운_곰 2016.05.23 271
163 Building a Search Engine With Nutch Solr And Hadoop file 졸리운_곰 2016.04.21 219
162 Nutch and Hadoop Tutorial file 졸리운_곰 2016.04.21 209
161 Latest step by Step Installation guide for dummies: Nutch 0. file 졸리운_곰 2016.04.21 131
160 Nutch 초간단 빌드와 실행 졸리운_곰 2016.04.21 457
159 Nutch로 알아보는 Crawling 구조 - Joinc 졸리운_곰 2016.04.21 352
158 A tiny bittorrent library Java: 자바로 만든 작은 bittorrent 라이브러리 file 졸리운_곰 2016.04.20 242
157 Updating UI in Eclipse RCP 졸리운_곰 2015.11.07 183
156 Eclipse RCP: Display.getDefault().asyncExec still blocking my GUI 졸리운_곰 2015.11.07 135
155 The Eclipse RCP multi-threaded Job and UIJob, use 졸리운_곰 2015.11.07 243
154 SWT: Respond to Long Running Tasks 졸리운_곰 2015.11.07 91
153 SWT and multithreading 졸리운_곰 2015.11.07 73
152 Eclipse RCP multithreading 졸리운_곰 2015.11.07 146
151 Eclipse RCP - Prevent UI freezing while running long operation 졸리운_곰 2015.11.07 114
150 Eclipse RCP Tutorial: How to Add a Progress Bar file 졸리운_곰 2015.11.07 119
149 Eclipse RCP 에서 configuration 설정 파일 사용하지 않기 secret 졸리운_곰 2015.11.07 0
148 Gson User Guide 졸리운_곰 2015.10.26 318
147 Java Google Json (Gson) Type Adapter 졸리운_곰 2015.10.26 145
146 TypeAdapter (Gson 2.3.1 API) - Google Gson 졸리운_곰 2015.10.26 132
145 Gson TypeAdapter Example file 졸리운_곰 2015.10.26 86
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED