首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Eclipse 嵌入式富客戶端平台 (eRCP) 深度探索:移動設備上的 Eclipse

     編輯:Java 相關知識

eRCP 將 Eclipse RCP 引入嵌入式/移動領域,包含 eSWT (核心/擴展/移動擴展)、eJFace、eWorkbench、eUpdate 四大組件。本文通過代碼示例展示在 PocketPC/智能手機上構建原生觀感應用。

架構組件

  • eSWT:SWT 子集 + 移動擴展 (IBM/Nokia/Motorola 共同規范)。採用 UGL (通用圖形層) 犧牲可移植性換取性能
  • eJFace:JFace 子集,封裝 eSWT 簡化開發 (Viewer、Action、Preferences)
  • eWorkbench:單視角工作台,自動適配設備屏幕尺寸 (Normal/Large/Status 三視圖)
  • eUpdate:更新管理器邏輯與 UI,支持插件動態安裝/調度

eSWT 組件結構

eSWT 核心示例:HelloWorld

public class HelloWorldeSWT {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    
    Text text = new Text(shell, SWT.SINGLE);
    text.setText("This is a text");
    
    Button buttonleft = new Button(shell, SWT.PUSH);
    buttonleft.setText("Left Push!");
    
    Button buttonright = new Button(shell, SWT.PUSH);
    buttonright.setText("Right Push!");
    buttonright.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        MessageBox mb = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.YES | SWT.NO);
        mb.setText("MessageBox");
        mb.setMessage("Can you see me?");
        mb.open();
      }
      public void widgetDefaultSelected(SelectionEvent e) {}
    });
    
    List list = new List(shell, SWT.MULTI | SWT.BORDER);
    for (int i=0; i<5; i++) list.add("item " + i);
    
    shell.setText("HelloWorld from eSWT");
    FormLayout layout = new FormLayout();
    layout.spacing = 5; layout.marginHeight = layout.marginWidth = 9;
    shell.setLayout(layout);
    // FormData 佈局代碼省略...
    
    shell.setSize(240, 320);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
}

PocketPC 上運行效果

原生觀感 MessageBox

關鍵點

  • PocketPC 頂級窗口自動最大化,`setSize()` 無效
  • 事件循環 `readAndDispatch()` + `sleep()` 模式
  • 原生觀感:利用底層圖形系統而非自繪

eSWT 擴展示例:簡易瀏覽器

Browser 控件實現前進/後退/重載/跳轉 URL。

public class BrowserTest {
  static Button prev, reload, next, go;
  static Text url;
  static Browser browser;
  // ... 佈局與事件綁定代碼 ...
  browser = new Browser(shell, SWT.NONE);
  browser.setUrl("https://www.google.com");
  // ...
}

簡易瀏覽器

eSWT 移動擴展示例:MobileShell 全屏與 ListView

  • `MobileShell` 替代 `Shell`,支持 `setFullScreenMode(true)`
  • `SortedList`:可過濾、排序的列表
  • `ListView`:支持圖標、密度切換 (LOW/MEDIUM) 的列表視圖
  • `Command` 綁定軟鍵/菜單操作
final MobileShell shell = new MobileShell(display);
Command fsCmd = new Command(shell, Command.SELECT, 0);
fsCmd.setText("FullScreen");
fsCmd.addSelectionListener(... shell.setFullScreenMode(true) ...);

SortedList sl = new SortedList(shell, SWT.MULTI|SWT.V_SCROLL|SWT.BORDER, SortedList.FILTER);
sl.add("banana"); sl.add("123"); // ...

ListView lv = new ListView(shell, SWT.MULTI|SWT.BORDER);
for (int i=0; i<20; i++) lv.add("item"+i, image[i%4]);

Command lowCmd = new Command(lv, Command.SELECT, 0);
lowCmd.setText("LOW");
lowCmd.addSelectionListener(... lv.setLayoutDensity(ListView.LOW) ...);

MobileShell 正常模式

MobileShell 全屏模式

eJFace:Viewer 體系

CheckBoxTableViewer、CheckBoxTreeViewer、ComboViewer、ListViewer、TableViewer、TreeViewer。參考 JFace 教程快速上手。

eWorkbench 應用開發

步驟 1:定義視圖 (擴展 ViewPart)

public class DefaultView extends ViewPart {
  public void createPartControl(Composite parent) {
    Composite c = new Composite(parent, SWT.NONE);
    c.setLayout(new FillLayout());
    new Label(c, SWT.CENTER).setText("Hello eWorkbench!");
  }
  public void setFocus() {}
}

步驟 2:plugin.xml 註冊視圖

<extension point="org.eclipse.ui.views">
  <view id="com.ibm.ercp.app.defaultView" name="Sample DefaultView"
        class="com.ibm.ercp.app.views.DefaultView" icon="icons/sample.gif"
        category="org.eclipse.ercp.eworkbench.viewCategory" />
</extension>

步驟 3:定義 eWorkbench Application

<extension point="org.eclipse.ercp.eworkbench.applications">
  <application id="com.ibm.ercp.application" name="IBM Sample Application" singleton="true">
    <views normal="com.ibm.ercp.app.views.normal" />
  </application>
</extension>

eWorkbench 應用啟動

eUpdate:更新管理

提供邏輯與 UI,支持插件動態安裝、調度、OGSI 整合。開發中,可編寫自定義更新功能或使用工作台應用配置。

總結

eRCP 為嵌入式設備帶來 Eclipse 級開發體驗:原生性能、標準組件模型、工作台框架、更新管理。源碼:配套源碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved