在前面我們已經可以自動生成一個文件并自動進入編輯狀態,但是不是圖形界面。而我們的代碼生成器使用的是可視化圖形界面。還記得我們上一篇生成的那個文件后綴名嗎?對,是.cg,那么要使得打開后綴名為cg文件時進入我們自定義的編輯環境中,只需在plugin.xml中加入:
<extension
point="org.eclipse.ui.editors">
<editor
default="true"
class="com.hymake.hyplat.hycodegen.editor.CgEditor"
contributorClass="com.hymake.hyplat.hycodegen.editor.CgEditorContributor"
extensions="cg"-----------------這里是要編輯的文件后綴名
icon="icons/logo1.png"
id="com.hymake.hyplat.hycodegen.editor.CgEditor"
name="代碼生成編輯器"/>
</extension>
而CgEditor則要繼承GraphicalEditorWithPalette,代碼如下:

接下來我們就要對這些Action進行相應的代碼編寫完成指定的工作。
友情小貼士:
1.在構造函數中一定要指定
setEditDomain(new DefaultEditDomain(this));才能調用getPaletteRoot并把視圖顯示出來。











而CgEditor則要繼承GraphicalEditorWithPalette,代碼如下:
1
public class CgEditor extends GraphicalEditorWithPalette
{
2
private boolean dirty;
3
4
/** *//**
5
* 構造函數
6
*/
7
public CgEditor()
{
8
dirty = false;
9
setEditDomain(new DefaultEditDomain(this));
10
}
11
12
/** *//**
13
* @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#getPaletteRoot()
14
*/
15
protected PaletteRoot getPaletteRoot()
{
16
PaletteRoot root = new PaletteRoot();
17
PaletteGroup toolGroup = new PaletteGroup("代碼生成器");
18
org.eclipse.gef.palette.ToolEntry tool = new SelectionToolEntry();
19
toolGroup.add(tool);
20
root.setDefaultEntry(tool);
21
tool = new MarqueeToolEntry();
22
toolGroup.add(tool);
23
PaletteDrawer drawer = new PaletteDrawer("類");
24
org.eclipse.jface.resource.ImageDescriptor descriptor = ImageFactory
25
.getImageDescriptor("table.gif");
26
CreationToolEntry creationEntry = new CreationToolEntry("新建一個表",
27
"新建一個表", new SimpleFactory(Table.class), descriptor, descriptor);
28
drawer.add(creationEntry);
29
descriptor = ImageFactory.getImageDescriptor("column.gif");
30
creationEntry = new CreationToolEntry("新建一個行", "新建一個行",
31
new SimpleFactory(Column.class), descriptor, descriptor);
32
drawer.add(creationEntry);
33
descriptor = ImageFactory.getImageDescriptor("connection.gif");
34
ConnectionCreationToolEntry connxCreationEntry = new ConnectionCreationToolEntry(
35
"新建一個連接", "新建一個連接", new SimpleFactory(Reference.class),
36
descriptor, descriptor);
37
drawer.add(connxCreationEntry);
38
root.add(toolGroup);
39
root.add(drawer);
40
return root;
41
}
42
43
44
45
/** *//**
46
* @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
47
*/
48
protected void initializeGraphicalViewer()
{
49
}
50
51
/** *//**
52
* @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
53
*/
54
public void doSave(IProgressMonitor monitor)
{
55
}
56
57
/** *//**
58
* @see org.eclipse.ui.part.EditorPart#doSaveAs()
59
*/
60
public void doSaveAs()
{
61
}
62
63
/** *//**
64
* @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
65
*/
66
public boolean isSaveAsAllowed()
{
67
return false;
68
}
69
70
}
這樣出來的圖如下:


2

3

4


5

6

7



8

9

10

11

12


13

14

15



16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45


46

47

48



49

50

51


52

53

54



55

56

57


58

59

60



61

62

63


64

65

66



67

68

69

70

接下來我們就要對這些Action進行相應的代碼編寫完成指定的工作。
友情小貼士:
1.在構造函數中一定要指定
setEditDomain(new DefaultEditDomain(this));才能調用getPaletteRoot并把視圖顯示出來。