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

📄 ngwidget.java

📁 Novocode的 SWT 控件框架 丰富了MDI功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
 * Copyright (c) 2004 Stefan Zeiger and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.novocode.com/legal/epl-v10.html
 * 
 * Contributors:
 *     Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
 *******************************************************************************/

package com.novocode.naf.gui;

import java.util.*;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.w3c.dom.*;
import org.w3c.dom.NamedNodeMap;

import com.novocode.naf.app.NAFException;
import com.novocode.naf.color.*;
import com.novocode.naf.data.DataDecoder;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.ModelPropertyDescriptor;
import com.novocode.naf.data.ModelPropertyManager;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.data.XMLPropertyManager;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.gui.layout.*;
import com.novocode.naf.gui.layout.NGLayout;
import com.novocode.naf.model.*;
import com.novocode.naf.resource.*;


/**
 * Base class for all NAF GUI components.
 * 
 * @author Stefan Zeiger (szeiger@novocode.com)
 * @since Feb 20, 2004
 */

public abstract class NGWidget extends NGComponent
{
  private static final String LAYOUT_DATA_PREFIX = "ld.";
  private static final String MODEL_PREFIX = "m."; // length is hardcoded in ModelBinding constructor
  private static final int LAYOUT_DATA_PREFIX_LENGTH = LAYOUT_DATA_PREFIX.length();
  private static final Set<String> EMPTY_STRING_SET = new HashSet<String>();
  
  private static final String WIDGETDATA_NAME = WidgetData.class.getName();


  protected NGLayout nglayout;

  private final Map<String, ModelBinding> modelBindings = new HashMap<String, ModelBinding>();
  private String tooltip, font;
  private ColorData fgcolor, bgcolor;
  private boolean focus;
  private Element layoutDataE;
  private Properties layoutDataAttributes, defaultLayoutDataAttributes;


  protected static class WidgetData
  {
    private NGWidget ngw;
    
    protected WidgetData(NGWidget ngw) { this.ngw = ngw; }
    
    protected Color givenFG, givenBG;
    
    protected NGWidget getNGWidget() { return ngw; }
    
    protected static final WidgetData forWidget(Widget w)
    {
      if(w == null) return null;
      else return (WidgetData)w.getData(WIDGETDATA_NAME);
    }
    
    protected void attachTo(Widget w)
    {
      w.setData(WIDGETDATA_NAME, this);
    }
  }


  /**
   * Set this widget's tooltip.
   * 
   * Note that not all widgets will actually display tooltips.
   * 
   * @param tooltip The tooltip to set, or <em>null</em> for no tooltip.
   */

  @XMLProperty
  public void setTooltip(String tooltip)
  {
    this.tooltip = tooltip;
  }


  /**
   * Return this widget's tooltip.
   */

  public String getTooltip()
  {
    return tooltip;
  }


  /**
   * Return this widget's background color.
   */

  public ColorData getBackgroundColor()
  {
    return bgcolor;
  }


  /**
   * Return this widget's foreground color.
   */

  public ColorData getForegroundColor()
  {
    return fgcolor;
  }


  /**
   * Return true if this widget should request focus when it is shown.
   */

  public boolean getFocus()
  {
    return focus;
  }


  /**
   * Return this widget's font.
   */

  public String getFont()
  {
    return font;
  }


  /**
   * Set this widget's background color.
   * 
   * @param data the background color.
   */

  @XMLProperty("bgcolor")
  public void setBackgroundColor(ColorData data)
  {
    bgcolor = data;
  }


  /**
   * Set this widget's foreground color.
   * 
   * @param data the foreground color.
   */

  @XMLProperty("fgcolor")
  public void setForegroundColor(ColorData data)
  {
    fgcolor = data;
  }


  /**
   * Set the focus flag.
   * 
   * @param b true if this widget should request focus when it is shown.
   */

  @XMLProperty
  public void setFocus(boolean b)
  {
    focus = b;
  }


  /**
   * Set this widget's font.
   * 
   * Note that not all widgets actually use a font. The font can still be
   * inherited by children though.
   */

  @XMLProperty
  public void setFont(String string)
  {
    font = string;
    if(font == null) return;
    font = font.trim();
    if(font.length() == 0) font = null;
  }


  /**
   * Return the layout data attributes.
   */

  public Properties getLayoutDataAttributes()
  {
    return layoutDataAttributes;
  }


  /**
   * Set the layout data attributes.
   */

  public void setLayoutDataAttributes(Properties properties)
  {
    layoutDataAttributes = properties;
  }


  protected XMLPropertyManager createPropertyManager()
  {
    return ModelPropertyManager.forClass(getClass());
  }


  protected void init() throws NAFException
  {
    super.init();

    if(nglayout == null) nglayout = new NGFillLayout();

    if(layoutDataE != null)
    {
      NamedNodeMap nnm = layoutDataE.getAttributes();
      if(nnm != null)
      {
        int nnmLength = nnm.getLength();
        for(int i=0; i<nnmLength; i++)
        {
          Node n = nnm.item(i);
          if(layoutDataAttributes == null) layoutDataAttributes = new Properties();
          layoutDataAttributes.put(n.getNodeName(), n.getNodeValue());
        }
      }
      layoutDataE = null;
    }

    NamedNodeMap nnm = selfElement.getAttributes();
    if(nnm != null)
    {
      int nnmLength = nnm.getLength();
      for(int i=0; i<nnmLength; i++)
      {
        Node n = nnm.item(i);
        String nv = n.getNodeName();
        if(nv.startsWith(LAYOUT_DATA_PREFIX))
        {
          if(layoutDataAttributes == null) layoutDataAttributes = new Properties();
          layoutDataAttributes.put(nv.substring(LAYOUT_DATA_PREFIX_LENGTH), n.getNodeValue());
        }
        else if(nv.startsWith(MODEL_PREFIX))
        {
          ModelBinding mb = new ModelBinding((Attr)n);
          modelBindings.put(mb.type, mb);
        }
      }
    }
  }


  protected void parseNAFChildElement(Element e) throws NAFException
  {
    final String ln = e.getLocalName();
    if("layout".equals(ln))
    {
      nglayout = ((GUIResourceManager)resourceManager).createLayout(e, this);
      defaultLayoutDataAttributes = new Properties();
      if(e != null)
      {
        NamedNodeMap nnm = e.getAttributes();
        if(nnm != null)
        {
          int nnmLength = nnm.getLength();
          for(int i=0; i<nnmLength; i++)
          {
            Node n = nnm.item(i);
            String nv = n.getNodeName();
            if(nv.startsWith(LAYOUT_DATA_PREFIX))
              defaultLayoutDataAttributes.put(nv.substring(LAYOUT_DATA_PREFIX_LENGTH), n.getNodeValue());
          }
        }
      }
    }
    else if("layout-data".equals(ln)) layoutDataE = e;

⌨️ 快捷键说明

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