BaNg@taobao

          Just Do It!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            20 Posts :: -1 Stories :: 202 Comments :: 0 Trackbacks
          最近發現一個很怪的問題,GEF的PaletteGroup在3.3及以下都有border,而在3.4卻消失了,我覺得好像是GEF 3.4 PaletteViewer的開發者Hudson和Shah認為不可見的邊框更“友好”,可是好多人都不習慣了,覺得是bug了,在給gef newsgroup上一個人回答問題的時候順便把這個hack了一下,效果還不錯,哈哈!下面是步驟:

          1 定義一個HackedGroupEditPart:

          import org.eclipse.draw2d.ColorConstants;
          import org.eclipse.draw2d.Graphics;
          import org.eclipse.draw2d.IFigure;
          import org.eclipse.draw2d.MarginBorder;
          import org.eclipse.draw2d.geometry.Insets;
          import org.eclipse.draw2d.geometry.Rectangle;
          import org.eclipse.gef.internal.ui.palette.editparts.GroupEditPart;
          import org.eclipse.gef.palette.PaletteContainer;
          import org.eclipse.jface.action.Separator;

          public class HackedGroupEditPart extends GroupEditPart {

              
          public HackedGroupEditPart(PaletteContainer group) {
                  
          super(group);
              }
              @Override
              
          protected void refreshVisuals() {
                  
          super.refreshVisuals();
                  Insets insets 
          = getContentPane().getBorder().getInsets(getContentPane());
                  getContentPane().setBorder(
          new MarginBorder(insets){
                      @Override
                      
          public void paint(IFigure figure, Graphics graphics, Insets insets) {
                          Rectangle r 
          = getPaintRectangle(figure, insets);
                          r.height
          --;
                          graphics.setForegroundColor(ColorConstants.buttonDarker);
                          graphics.drawLine(r.x, r.bottom(), r.right(), r.bottom());
                      }
                  });
              }
              
          }

          2 使用這個加了邊框的GroupEditPart,在編輯器定義里面重寫 createPaletteViewerProvider 方法,如下:

          public class LivingEditor extends GraphicalEditorWithFlyoutPalette {
          .
          .
          @Override
          protected PaletteViewerProvider createPaletteViewerProvider() {
                  
          return new PaletteViewerProvider(getEditDomain())
                  {
                      @Override
                      
          public PaletteViewer createPaletteViewer(Composite parent) {
                          
                          
          //add border for GroupEditPart
                          final PaletteEditPartFactory pepf = new PaletteEditPartFactory()
                          {
                              @Override
                              
          protected EditPart createGroupEditPart(
                                      EditPart parentEditPart, Object model) {
                                  
          return new HackedGroupEditPart((PaletteContainer)model);
                              }
                          };
                          PaletteViewer pViewer 
          = new PaletteViewer(){
                              {
                                  setEditPartFactory(pepf);
                              }
                          };
                          pViewer.createControl(parent);
                          configurePaletteViewer(pViewer);
                          hookPaletteViewer(pViewer);
                          
          return pViewer;
                      }
                  };
              }


          }

          我的Palette定義,使用了三個PaletteGroup,但不改一下根本看不出來group之間的分隔

          public class PaletteProvider {

              
          public static PaletteRoot provide(LivingEditor livingEditor) {
                  PaletteRoot root 
          = new PaletteRoot();
                  createStandardTool(root);
                  createConnectionTool(root);
                  createModelTool(root);
                  
          return root;
              }

              
          private static void createModelTool(PaletteRoot root) {
                  PaletteGroup model_group 
          = new PaletteGroup("model");
                  CombinedTemplateCreationEntry person_entry 
          = new CombinedTemplateCreationEntry("Person",
                          
          "Create a person",
                          
          new SimpleFactory(Person.class),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/person16.png"),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/person24.png")
                          );
                  model_group.add(person_entry);
                  CombinedTemplateCreationEntry dog_entry 
          = new CombinedTemplateCreationEntry("Dog",
                          
          "Create a dog",
                          
          new SimpleFactory(Dog.class),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/dog16.gif"),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/dog24.gif"));
                  model_group.add(dog_entry);
                  root.add(model_group);
              }

              
          private static void createConnectionTool(PaletteRoot root) {
                  ConnectionCreationToolEntry relation 
          = new ConnectionCreationToolEntry("relation",
                          
          "relation between person and dog",
                          
          new SimpleFactory(Relation.class),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/connection16.gif"),
                          Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
          "icon/connection24.gif")
                          );
                  PaletteGroup conn_group 
          = new PaletteGroup("relations");
                  conn_group.add(relation);
                  root.add(conn_group);
                  
              }

              
          private static void createStandardTool(PaletteRoot root) {
                  PaletteGroup std 
          = new PaletteGroup("Standard");
                  SelectionToolEntry selection 
          = new SelectionToolEntry();
                  std.add(selection);
                  
                  root.add(std);
              }

          可以看一下修改后的對比:



          不過這種方法使用了internal API,不適應版本更新,只適合救急。我已經報告了這個誤會,3.5應該不會有了。

          如果你也有這個困擾,就試試吧。



          posted on 2008-08-17 22:40 Always BaNg. 閱讀(1867) 評論(0)  編輯  收藏 所屬分類: JavaEclipse源代碼分析
          主站蜘蛛池模板: 古浪县| 南城县| 蕲春县| 浮梁县| 承德市| 彭山县| 姜堰市| 习水县| 民权县| 疏附县| 宣化县| 济源市| 遂平县| 衡山县| 寿光市| 永康市| 阜平县| 新源县| 台安县| 河北省| 巴中市| 萍乡市| 哈密市| 元阳县| 雷州市| 原平市| 恩平市| 宝坻区| 黎川县| 黎平县| 哈尔滨市| 改则县| 黄平县| 泾源县| 耒阳市| 名山县| 都兰县| 收藏| 怀安县| 玛纳斯县| 襄城县|