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

GEF 入門系列(九):提升編輯器易用性——調色板、縮放、網格、對齊、標尺全攻略

     編輯:Java 相關知識

1. 可折疊調色板

繼承 GraphicalEditorWithFlyoutPalette 替代 GraphicalEditorWithPalette,獲得可摺疊工具條、與 Eclipse Palette 視圖聯動能力。

可折疊調色板

關鍵實現

  • getPalettePreferences() 返回 FlyoutPreferences,持久化位置、大小、展開狀態 (使用 PreferenceStore)
  • 覆寫 createPaletteViewerProvider() 為 PaletteViewer 添加 TemplateTransferDragSourceListener 支持拖放
  • 可選:配置 PaletteCustomizer 允許用戶隱藏/重命名工具 (參考 Logic 示例)

2. 縮放功能

Draw2D 原生支持縮放。使用 GEF 內置 Action:

  • ZoomInAction / ZoomOutAction (構造需 ZoomManager,從 ScalableFreeformRootEditPart 獲取)
  • 在 configureGraphicalViewer() 中創建 Action 並註冊到 ActionRegistry、KeyBindingService
  • DiagramActionBarContributor 中添加 ZoomInRetargetAction/ZoomOutRetargetAction 到工具欄
  • 額外添加 ZoomComboContributionItem 提供百分比下拉框

縮放工具欄

持久化縮放比例

// 初始化時恢復
ZoomManager manager = (ZoomManager) getGraphicalViewer().getProperty(ZoomManager.class.toString());
if (manager != null) manager.setZoom(diagram.getZoom());

// 保存前記錄
if (manager != null) diagram.setZoom(manager.getZoom());

3. 輔助網格

輔助網格

  • 控制屬性:SnapToGrid.PROPERTY_GRID_VISIBLE、PROPERTY_GRID_ENABLED (Boolean)
  • ToggleGridAction 同步切換顯示與吸附
  • 無現成 RetargetAction,建議僅放入菜單或自行設置圖標
  • 模型增加布爾字段持久化網格狀態

4. 幾何對齊

幾何對齊

  • 屬性:SnapToGeometry.PROPERTY_SNAP_ENABLED
  • Action:ToggleSnapToGeometryAction
  • 關鍵步驟:在畫布 EditPart (DiagramPart) 的 getAdapter() 中返回 CompoundSnapToHelper 組合多種吸附策略
public Object getAdapter(Class adapter) {
    if (adapter == SnapToHelper.class) {
        List snapStrategies = new ArrayList();
        // Ruler Guides
        if (getViewer().getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY) == Boolean.TRUE)
            snapStrategies.add(new SnapToGuides(this));
        // Geometry
        if (getViewer().getProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED) == Boolean.TRUE)
            snapStrategies.add(new SnapToGeometry(this));
        // Grid
        if (getViewer().getProperty(SnapToGrid.PROPERTY_GRID_ENABLED) == Boolean.TRUE)
            snapStrategies.add(new SnapToGrid(this));

        if (snapStrategies.isEmpty()) return null;
        if (snapStrategies.size() == 1) return snapStrategies.get(0);
        return new CompoundSnapToHelper(snapStrategies.toArray(new SnapToHelper[0]));
    }
    return super.getAdapter(adapter);
}

5. 標尺與輔助線

標尺與輔助線 模型結構

模型擴展

  • Diagram 新增 topRuler、leftRuler、rulerVisibility
  • 新增 Ruler (包含 Guides 列表)、Guide (位置、方向、吸附映射 map)

視圖層集成

private RulerComposite rulerComp;
protected void createGraphicalViewer(Composite parent) {
    rulerComp = new RulerComposite(parent, SWT.NONE);
    super.createGraphicalViewer(rulerComp);
    rulerComp.setGraphicalViewer((ScrollingGraphicalViewer) getGraphicalViewer());
}
protected Control getGraphicalControl() { return rulerComp; }

配置 GraphicalViewer 屬性

getGraphicalViewer().setProperty(RulerProvider.PROPERTY_VERTICAL_RULER, 
    new SubjectRulerProvider(diagram.getLeftRuler()));
getGraphicalViewer().setProperty(RulerProvider.PROPERTY_HORIZONTAL_RULER, 
    new SubjectRulerProvider(diagram.getTopRuler()));
getGraphicalViewer().setProperty(RulerProvider.PROPERTY_RULER_VISIBILITY, 
    new Boolean(diagram.isRulerVisibility()));

SubjectRulerProvider 核心實現

  • 繼承 RulerProvider,維護 Ruler 模型
  • 實現 getCreateGuideCommand()/getDeleteGuideCommand()/getMoveGuideCommand()
  • 為 Ruler/Guide 添加 EMF Adapter 監聽器,觸發 RulerChangeListener 通知 EditPart 更新
  • 實現 getGuidePositions()、getGuidePosition(Object) 等查詢方法

切換顯示

使用 ToggleRulerVisibilityAction 加入主菜單控制標尺/輔助線可見性。

6. 位置與尺寸對齊

對齊工具欄

  • AlignmentAction:參數 PositionConstants.LEFT/CENTER/RIGHT/TOP/MIDDLE/BOTTOM
  • MatchWidthAction/MatchHeightAction (繼承 MatchSizeAction)
  • 在編輯器 createActions() 創建並註冊,加入 getSelectionActions() 響應選中
  • Contributor 中添加對應 RetargetAction 到工具欄/菜單
  • 多選時以「主選中」 (六黑點包圍) 為基準對齊

總結

GEF 3.0+ 提供豐富的易用性組件,通過標準擴展點 (Action、Property、Adapter) 集成,幾十行代碼即可賦予編輯器專業級交互體驗。建議按需啟用,避免界面過載。

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