⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngwidget.java

📁 Novocode的 SWT 控件框架 丰富了MDI功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    else if("model".equals(ln))
    {
      ModelBinding mb = new ModelBinding(e, getUnexpandedChildren(null).size());
      ModelPropertyDescriptor pd = ((ModelPropertyManager)propertyManager).getModelPropertyDescriptor(mb.type);
      if(pd != null) pd.set(this, mb);
      else modelBindings.put(mb.type, mb);
    }
    else super.parseNAFChildElement(e);
  }


  private void addPopupMenu(Control ctrl, WindowInstance wi) throws NAFException
  {
    NGComponent popup = getFirstChild("popup");
    if(popup == null) return;
    if(!(popup instanceof NGMenu))
      throw new NAFException("Only NGMenu objects can be assigned to role \"popup\"");
    ctrl.setMenu(((NGMenu)popup).createMenu(wi, SWT.POP_UP));
  }


  protected final void addCommonMenuFeatures(MenuItem i, WindowInstance wi, String label,
    String accelerator, String imageResource) throws NAFException
  {
    // Set accelerator and compute label
    {
      int acc = SWTUtil.decodeAccelerator(accelerator);
      if(acc != 0)
      {
        i.setAccelerator(acc);
        label += '\t' + accelerator;
      }
    }
    
    SWTUtil.setRegisteredImage(imageResource, getResourceURL(), i, wi.imageManager, true);
    i.setText(label);
  }
  
  
  protected final void addCommonFeatures(Widget w, WindowInstance wi, NGWidget parentNGC) throws NAFException
  {
    if(tooltip != null && tooltip.length() > 0) SWTUtil.setTooltip(w, tooltip);

    addModels(w, wi, parentNGC);
    gatherPersistModels(wi);
  }


  private void gatherPersistModels(WindowInstance wi)
  {
    for(ModelBinding mb : modelBindings.values())
    {
      if(mb.persist) wi.addPersistModel(mb.id);
    }
  }


  protected WidgetData createWidgetData()
  {
    return new WidgetData(this);
  }


  protected void configureControl(final Control control, final ShellWindowInstance wi, WidgetData wd, WidgetData pwd)
  {
    if(fgcolor == null || fgcolor.getType() == ColorData.TYPE_INHERIT)
    {
      if(pwd != null && pwd.givenFG != null)
      {
        control.setForeground(pwd.givenFG);
        wd.givenFG = pwd.givenFG;
      }
    }
    else
    {
      SWTUtil.setRegisteredForeground(control, fgcolor);
      if(fgcolor.getType() != ColorData.TYPE_DEFAULT) wd.givenFG = control.getForeground();
    }

    if(bgcolor == null || bgcolor.getType() == ColorData.TYPE_INHERIT)
    {
      if(pwd != null && pwd.givenBG != null)
      {
        control.setBackground(pwd.givenBG);
        wd.givenBG = pwd.givenBG;
      }
    }
    else
    {
      SWTUtil.setRegisteredBackground(control, bgcolor);
      if(bgcolor.getType() != ColorData.TYPE_DEFAULT) wd.givenBG = control.getBackground();
    }

    addPopupMenu(control, wi);
    if(font != null)
    {
      FontData[] base = control.getFont().getFontData();
      FontData[] fd = FontManager.decodeFontSpec(base, font);
      SWTUtil.setRegisteredFont(fd, control, wi.fontManager);
    }
    if(focus) control.setFocus();
  }

  
  protected final void addDefaultChildren(final Composite composite, final ShellWindowInstance wi, final WidgetData wd)
  {
    final IItemListModel itemsModel = getModel("items", wi.models);
    final ModelBinding itemsModelBinding = getModelBinding("items");
    if(itemsModel != null)
    {
      final IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          try
          {
            composite.setRedraw(false);
            // [TODO] React to ItemListModel for partial updates
            for(Control c : composite.getChildren()) c.dispose();
            addDefaultChildrenStatic(composite, wi, itemsModel, itemsModelBinding, wd);
            composite.layout(true);
          }
          finally { composite.setRedraw(true); }
        }
      };
      SWTUtil.registerModel(composite, itemsModel, cl);
    }

    addDefaultChildrenStatic(composite, wi, itemsModel, itemsModelBinding, wd);
  }
  
  
  private void addDefaultChildrenStatic(Composite composite, ShellWindowInstance wi, IItemListModel itemsModel,
    ModelBinding itemsModelBinding, WidgetData wd)
  {
    HashMap<String, Control> chmap = new HashMap<String, Control>();

    List<NGComponent> list = getUnexpandedChildren(null);
    List<NGComponent> childList = new ArrayList<NGComponent>();
    for(int i=0; i<=list.size(); i++)
    {
      if(itemsModel != null && itemsModelBinding.widgetListIdx == i)
      {
        NGComponent[] dynamicWidgets = itemsModel.getItems();
        for(int j=0; j<dynamicWidgets.length; j++) dynamicWidgets[j].expandSelf(childList);
      }
      if(i != list.size())
      {
        list.get(i).expandSelf(childList);
      }
    }

    for(NGComponent ch : childList)
    {
      Control chctrl = ((NGWidget)ch).createAndConfigureControl(composite, this, wi);
      if(ch.getID() != null) chmap.put(ch.getID(), chctrl);
    }

    Control[] chctrls = composite.getChildren();
    for(int i=0; i<chctrls.length; i++)
    {
      NGWidget ch = (NGWidget)childList.get(i);
      if(nglayout != null)
      {
        Properties ldAttributes = new Properties(defaultLayoutDataAttributes);
        if(ch.layoutDataAttributes != null) ldAttributes.putAll(ch.layoutDataAttributes);
        nglayout.assignLayoutData(chctrls[i], ldAttributes, chmap, chctrls, i);
      }
    }
    
    nglayout.assignLayout(composite, wi);
  }


  protected Control createControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi, WidgetData pwd) throws NAFException
  {
    throw new NAFException("Cannot create a <"+getElementName()+"> control with a "+parent.getClass().getName()+" parent");
  }
  
  
  public final Control createAndConfigureControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi) throws NAFException
  {
    WidgetData pwd = WidgetData.forWidget(parent);
    Control c = createControl(parent, parentComp, wi, pwd);
    WidgetData wd = createWidgetData();
    wd.attachTo(c);
    addCommonFeatures(c, wi, (parentComp instanceof NGWidget ? (NGWidget)parentComp : null));
    configureControl(c, wi, wd, pwd);
    return c;
  }
  
  
  protected final void addModels(final Widget widget, final WindowInstance wi, final NGWidget parentNGC) // [TODO] Make addModels() private and ensure it is called exactly once for every widget
  {
    final IBooleanReadModel disabledModel = getModel("disabled", wi.models);
    final IBooleanReadModel enabledModel = getModel("enabled", wi.models);
    if(disabledModel != null && enabledModel != null)
      throw new NAFException("Models \"disabled\" and \"enabled\" may not be used together");
    if(disabledModel != null)
    {
      IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          SWTUtil.setEnabledIfChanged(widget, !disabledModel.getBoolean());
        }
      };
      SWTUtil.registerModel(widget, disabledModel, cl);
      cl.stateChanged(null);
    }
    if(enabledModel != null)
    {
      IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          SWTUtil.setEnabledIfChanged(widget, enabledModel.getBoolean());
        }
      };
      SWTUtil.registerModel(widget, enabledModel, cl);
      cl.stateChanged(null);
    }

    final IBooleanReadModel visibleModel = getModel("visible", wi.models);
    if(visibleModel != null && widget instanceof Control)
    {
      final Control ctrl = (Control)widget;
      IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          boolean b = visibleModel.getBoolean();
          if(ctrl.getVisible() != b)
          {
            if(parentNGC != null && parentNGC.nglayout != null)
              parentNGC.nglayout.setControlVisibility(ctrl, b, wi);
            else ctrl.setVisible(b);
          }
        }
      };
      SWTUtil.registerModel(widget, visibleModel, cl);
      cl.stateChanged(null);
    }

    final IObjectReadModel<String> tooltipModel = getModel("tooltip", wi.models);
    if(tooltipModel != null && (widget instanceof Control || widget instanceof ToolItem))
    {
      IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          String s = tooltipModel.getValue();
          if(s == null) s = "";
          if(!s.equals(SWTUtil.getTooltip(widget))) SWTUtil.setTooltip(widget, s);
        }
      };
      SWTUtil.registerModel(widget, tooltipModel, cl);
      cl.stateChanged(null);
    }
  }
  
  
  public final <T> T getModel(String type, ModelMap models)
  {
    // [TODO] Support dynamic menus
    if(modelBindings == null) return null;
    ModelBinding mb = modelBindings.get(type);
    if(mb == null) return null;
    T model = models.get(mb.id);
    if(model == null && (mb.create != ModelBinding.Create.NO))
    {
      model = (T)createDefaultModel(mb);
      models.put(mb.id, model);
    }
    return model;
  }
  
  
  public final ModelBinding getModelBinding(String type)
  {
    if(modelBindings == null) return null;
    ModelBinding mb = modelBindings.get(type);
    return mb;
  }


  public final Set<String> getModelTypes()
  {
    if(modelBindings == null) return EMPTY_STRING_SET;
    return modelBindings.keySet();
  }

  
  protected Object createDefaultModel(ModelBinding mb)
  {
    if("enabled".equals(mb.type) || "disabled".equals(mb.type) || "visible".equals(mb.type))
      return new DefaultBooleanModel(DataDecoder.decodeBoolean(mb.getAttribute("initial"), false));
    else if("tooltip".equals(mb.type)) return new DefaultStringModel();
    else if("items".equals(mb.type)) return new DefaultItemListModel();
    else throw new NAFException("Cannot create default model for model type \""+mb.type+"\"");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -