GEF框架分析之一(转载)




<h2>&nbsp;</h2>
<p class="MsoNormal">
        <span style="font-family: SimSun;">首先我们来创建一个基于</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的浏览器(而不是编辑器),也就是说,它只能浏览一个模型,而不能够进行编辑。先关注于浏览而不是编辑,有助于理解</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的一些核心的概念。</span>
        <span style="" lang="EN-US">

                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="" lang="EN-US">
                <o:p>&nbsp;</o:p>
        </span>
</p>
<p class="MsoNormal">
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">GraphicalViewer</span>
                </b>
        </span>
        <b style="">
                <span style="font-family: SimSun;">和</span>
        </b>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">createControl</span>
                </b>
        </span>
        <b style="">
                <span style="" lang="EN-US">()</span>
        </b>
        <b style="">
                <span style="font-family: SimSun;">方法</span>
        </b>
        <b style="">
                <span style="" lang="EN-US">
                        <o:p>
                        </o:p>
                </span>
        </b>
</p>
<p class="MsoNormal">
        <span style="font-family: SimSun;">在开始之前,最先要弄明白的是,一个</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的<span class="GramE">图如何</span>能够显示在由</span>
        <span class="SpellE">
                <span style="" lang="EN-US">ViewPart</span>
        </span>
        <span style="font-family: SimSun;">在</span>
        <span class="SpellE">
                <span style="color: red;" lang="EN-US">createPartControl</span>
        </span>
        <span style="" lang="EN-US">(Component parent)</span>
        <span style="font-family: SimSun;">中提供的这个</span>
        <span style="" lang="EN-US">parent</span>
        <span style="font-family: SimSun;">控件上。这是通过</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的</span>
        <span class="SpellE">
                <span style="" lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">(准确的说应该是</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPartViewer</span>
        </span>
        <span style="font-family: SimSun;">,</span>
        <span class="SpellE">
                <span style="" lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">是</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPartViewer</span>
        </span>
        <span style="font-family: SimSun;">的子接口,</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPartViewer</span>
        </span>
        <span style="font-family: SimSun;">的名字可能更容易理解一些:是为</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">创建一个</span>
        <span style="" lang="EN-US">Viewer</span>
        <span style="font-family: SimSun;">进行显示)来进行的。</span>
        <span class="SpellE">
                <span style="" lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">可以将其自身适配到任何一个</span>
        <span style="" lang="EN-US">SWT Control</span>
        <span style="font-family: SimSun;">上。它的</span>
        <span class="SpellE">
                <span style="" lang="EN-US">createControl</span>
        </span>
        <span style="" lang="EN-US">()</span>
        <span style="font-family: SimSun;">方法接受一个</span>
        <span style="" lang="EN-US">SWT Control</span>
        <span class="GramE">
                <span style="font-family: SimSun;">来作</span>
        </span>
        <span style="font-family: SimSun;">为适配的对象,将</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">所生成的</span>
        <span style="" lang="EN-US">Figure</span>
        <span style="font-family: SimSun;">显示在这个</span>
        <span style="" lang="EN-US">SWT Control</span>
        <span style="font-family: SimSun;">之上。(在内部实现上,</span>
        <span class="SpellE">
                <span style="" lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">使用</span>
        <span style="" lang="EN-US">Draw2D</span>
        <span style="font-family: SimSun;">的</span>
        <span class="SpellE">
                <span style="" lang="EN-US">LightweightSystem</span>
        </span>
        <span style="font-family: SimSun;">来做这个事情,将</span>
        <span style="" lang="EN-US">Draw2d</span>
        <span style="font-family: SimSun;">的</span>
        <span style="" lang="EN-US">Figure</span>
        <span style="font-family: SimSun;">对象绘制在</span>
        <span style="" lang="EN-US">SWT</span>
        <span style="font-family: SimSun;">的</span>
        <span style="" lang="EN-US">Control</span>
        <span style="font-family: SimSun;">上)</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="" lang="EN-US">
                <o:p>&nbsp;</o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="font-family: SimSun;">在弄明白了这一点之后,我们就会知道,实际上是将</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">放在</span>
        <span class="SpellE">
                <span style="" lang="EN-US">ViewPart</span>
        </span>
        <span style="font-family: SimSun;">中还是放在</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditorPart</span>
        </span>
        <span style="font-family: SimSun;">对于</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">而言是没有本质的区别的,甚至将</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">放在任何的</span>
        <span style="" lang="EN-US">SWT Composite</span>
        <span style="font-family: SimSun;">上都是可以的。不过</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">框架中提供了对创建</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditorPart</span>
        </span>
        <span style="font-family: SimSun;">的缺省实现。而如果要在</span>
        <span style="" lang="EN-US">Editor</span>
        <span style="font-family: SimSun;">之外使用</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的话,需要额外的对</span>
        <span class="SpellE">
                <span style="" lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">做一些设置。</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal">
        <span lang="EN-US">
                <o:p>&nbsp;</o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="font-family: SimSun;">下面的这个图,给出了</span>
        <span lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">中的几个主要关系,下面会有对这个图的解释。</span>
</p>
<p class="MsoNormal">
        <span lang="EN-US">
                <o:p>&nbsp;<span style="font-family: SimSun;"><img alt="image001.png" src="http://www.blogjava.net/images/blogjava_net/eclipshine/blogimages/image001.png" border="0" height="373" width="763"></span></o:p>
        </span>
        <span lang="EN-US">
        </span>
</p>
<p class="MsoNormal">
        <span lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal">
        <span class="SpellE">
                <span class="GramE">
                        <b style="">
                                <span lang="EN-US">
                                        <br>setContent</span>
                        </b>
                </span>
        </span>
        <span class="GramE">
                <b style="">
                        <span lang="EN-US">()</span>
                </b>
        </span>
        <b style="">
                <span lang="EN-US">
                        <o:p>
                        </o:p>
                </span>
        </b>
</p>
<p class="MsoNormal">
        <span class="SpellE">
                <span lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">的思想,类似于</span>
        <span class="SpellE">
                <span lang="EN-US">JFace</span>
        </span>
        <span style="font-family: SimSun;">中所实现的</span>
        <span lang="EN-US">Viewer</span>
        <span style="font-family: SimSun;">思想:要显示一个控件,需要输入的是一个模型对象,要显示的内容根据输入的模型对象来进行的。因此,</span>
        <span class="SpellE">
                <span lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">同所有</span>
        <span class="SpellE">
                <span lang="EN-US">JFace</span>
        </span>
        <span lang="EN-US">Viewer</span>
        <span style="font-family: SimSun;">一样有一个方法,叫做</span>
        <span class="SpellE">
                <span lang="EN-US">setContent</span>
        </span>
        <span lang="EN-US">(Object o)</span>
        <span style="font-family: SimSun;">来接受一个模型对象,而对这个模型对象如何进行处理并显示,就是</span>
        <span lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">的工作了。</span>
</p>
<p class="MsoNormal">
        <span lang="EN-US">
                <o:p>&nbsp;</o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="font-family: SimSun;">在创建一个</span>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">GraphicalViewer</span>
                </b>
        </span>
        <span style="font-family: SimSun;">之后,要使</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">能够运转起来,还需要做如下几件事情:</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;">
        <span style="" lang="EN-US">
                <span style="">1.<span style="font-family: &apos;Times New Roman&apos;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>
        </span>
        <b style="">
                <span style="font-family: SimSun;">设置一个</span>
        </b>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">RootEditPart</span>
                </b>
        </span>
        <span style="font-family: SimSun;">:</span>
        <span class="SpellE">
                <span style="" lang="EN-US">RootEditPart</span>
        </span>
        <span style="font-family: SimSun;">的是使整个</span>
        <span style="" lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">框架运行起来的关键之一。</span>
        <span class="SpellE">
                <span style="" lang="EN-US">RootEditPart</span>
        </span>
        <span style="font-family: SimSun;">并不对应于任何的模型对象,它将从</span>
        <span class="SpellE">
                <span style="" lang="EN-US">setContents</span>
        </span>
        <span style="" lang="EN-US">()</span>
        <span style="font-family: SimSun;">方法中接收到的模型对象进行转换,并添加到整个的</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">体系中去,具体的添加过程后面会有详细讨论。</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;">
        <span style="" lang="EN-US">
                <span style="">2.<span style="font-family: &apos;Times New Roman&apos;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>
        </span>
        <b style="">
                <span style="font-family: SimSun;">设置其</span>
        </b>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">EditPartFactory</span>
                </b>
        </span>
        <span style="font-family: SimSun;">:负责从模型到</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">的转换。一般来说一个模型对象对应于一个</span>
        <span class="SpellE">
                <span style="" lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">。这个类的</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;">
        <span style="" lang="EN-US">
                <span style="">3.<span style="font-family: &apos;Times New Roman&apos;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>
        </span>
        <b style="">
                <span style="font-family: SimSun;">设置</span>
        </b>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">EditDomain</span>
                </b>
        </span>
        <span style="font-family: SimSun;">:用来接收事件并选择恰当的事件处理函数进行处理,因为我们尚不需要进行编辑,因此这一个可以暂时略过。</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;">
        <span style="" lang="EN-US">
                <span style="">4.<span style="font-family: &apos;Times New Roman&apos;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>
        </span>
        <b style="">
                <span style="font-family: SimSun;">调用</span>
        </b>
        <span class="SpellE">
                <b style="">
                        <span style="" lang="EN-US">setContents</span>
                </b>
        </span>
        <b style="">
                <span style="" lang="EN-US">()</span>
        </b>
        <b style="">
                <span style="font-family: SimSun;">方法</span>
        </b>
        <span style="font-family: SimSun;">:为其设置要显示的内容。</span>
        <span style="" lang="EN-US">
                <o:p>
                </o:p>
        </span>
</p>
<p class="MsoNormal">
        <span lang="EN-US">
                <o:p>&nbsp;</o:p>
        </span>
</p>
<p class="MsoNormal">
        <span style="font-family: SimSun;">由</span>
        <span class="SpellE">
                <span lang="EN-US">GraphicalViewer</span>
        </span>
        <span style="font-family: SimSun;">所显示的内容,肯定是一个</span>
        <span lang="EN-US">Draw2d</span>
        <span style="font-family: SimSun;">中的</span>
        <span class="SpellE">
                <span lang="EN-US">IFigure</span>
        </span>
        <span style="font-family: SimSun;">对象。而如何从一个模型对象到一个</span>
        <span lang="EN-US">Figure</span>
        <span style="font-family: SimSun;">对象呢?这中间的联系就是通过</span>
        <span class="SpellE">
                <span lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">来完成的。</span>
        <span lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">使用的是</span>
        <span lang="EN-US">MVC</span>
        <span style="font-family: SimSun;">模式,</span>
        <span lang="EN-US">Model</span>
        <span style="font-family: SimSun;">部分可以使任意的模型对象;</span>
        <span lang="EN-US">View</span>
        <span style="font-family: SimSun;">部分就是</span>
        <span lang="EN-US">Draw2d</span>
        <span style="font-family: SimSun;">的</span>
        <span class="SpellE">
                <span lang="EN-US">IFigure</span>
        </span>
        <span style="font-family: SimSun;">对象,联系</span>
        <span lang="EN-US">Model</span>
        <span style="font-family: SimSun;">和</span>
        <span lang="EN-US">View</span>
        <span style="font-family: SimSun;">的</span>
        <span lang="EN-US">Controller</span>
        <span style="font-family: SimSun;">,在</span>
        <span lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">中称为</span>
        <span class="SpellE">
                <span lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">。简单的说,</span>
        <span lang="EN-US">GEF</span>
        <span style="font-family: SimSun;">中的</span>
        <span class="SpellE">
                <span lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">,负责将模型转换为</span>
        <span class="SpellE">
                <span lang="EN-US">IFigure</span>
        </span>
        <span style="font-family: SimSun;">的显示,它从模型中读取信息,生成</span>
        <span lang="EN-US">Figure</span>
        <span style="font-family: SimSun;">;另一方面,</span>
        <span class="SpellE">
                <span lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">负责从</span>
        <span lang="EN-US">View</span>
        <span style="font-family: SimSun;">接收事件,并将这些事件转化为对模型的修改。在这一部分中,我们暂时只关注于第一个方面:</span>
        <span class="SpellE">
                <span lang="EN-US">EditPart</span>
        </span>
        <span style="font-family: SimSun;">是如何将一个模型转换为</span>
        <span lang="EN-US">Figure</span>
        <span style="font-family: SimSun;">以进行显示的。</span>
</p>