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

📄 ngbutton.java

📁 Novocode的 SWT 控件框架 丰富了MDI功能
💻 JAVA
字号:
/*******************************************************************************
 * 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.LinkedList;

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

import com.novocode.naf.app.*;
import com.novocode.naf.data.DataDecoder;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.model.*;
import com.novocode.naf.resource.*;


/**
 * A push button / toolbar button / menu item.
 * 
 * @author Stefan Zeiger (szeiger@novocode.com)
 * @since Nov 26, 2003
 */

public final class NGButton extends NGWidget implements IToolItemWidget, IMenuItemWidget
{
  private String accelerator, text, attrText, imageResource;
  private boolean defaultButton;
  private Type type = Type.DEFAULT;
  private LinkedList<String> childTokens;

  public enum Type { DEFAULT, ARROW_UP, ARROW_DOWN, ARROW_LEFT, ARROW_RIGHT };
  private static final int[] typeStyles =
    new int[] { SWT.PUSH, SWT.ARROW|SWT.UP, SWT.ARROW|SWT.DOWN, SWT.ARROW|SWT.LEFT, SWT.ARROW|SWT.RIGHT };
  

  protected void init() throws NAFException
  {
    super.init();
    if(attrText == null && childTokens != null)
    {
      StringBuffer b = new StringBuffer();
      for(String s : childTokens) b.append(s);
      text = b.toString().trim().replace((char)160,' ');
      if(text.length() == 0) text = null;
    }
  }


  protected void parseChild(Node n) throws NAFException
  {
    if(n.getNodeType() == Node.TEXT_NODE)
    {
      if(childTokens == null) childTokens = new LinkedList<String>();
      String text = ((org.w3c.dom.Text)n).getData();
      text = text.replace('\r', ' ').replace('\n', ' ').replace('\r', ' ');
      if(text.startsWith(" ") && childTokens.size() != 0 && (!" ".equals(childTokens.getLast())))
        childTokens.add(" ");
      boolean first = true;
      for(StringTokenizer tok = new StringTokenizer(text); tok.hasMoreTokens();)
      {
        String s = tok.nextToken();
        if(s.length() == 0) continue;
        if(!first) childTokens.add(" ");
        childTokens.add(s);
        first = false;
      }
      if(text.endsWith(" ")) childTokens.add(" ");
    }
    else super.parseChild(n);
  }


  @XMLProperty("image") public void setImageResource(String imageResource) { this.imageResource = imageResource; }
  public String getImageResource() { return imageResource; }


  @XMLProperty public void setText(String text) { this.text = text; this.attrText = text; }
  public String getText() { return text; }


  @XMLProperty public void setAccelerator(String accelerator) { this.accelerator = accelerator; }
  public String getAccelerator() { return accelerator; }


  @XMLProperty public void setType(Type type) { this.type = type; }
  public Type getType() { return type; }


  @XMLProperty("default") public void setDefaultButton(boolean defaultButton) { this.defaultButton = defaultButton; }
  public boolean isDefaultButton() { return defaultButton; }


  public Control createControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi, WidgetData pwd) throws NAFException
  {
    Button b = new Button(parent, typeStyles[type.ordinal()] | SWT.CENTER);
    SWTUtil.setRegisteredImage(imageResource, getResourceURL(), b, wi.imageManager, false);
    if(text != null) b.setText(DataDecoder.decodeAccessKey(text));
    if(defaultButton) wi.shell.setDefaultButton(b);
    addButtonModels(b, wi);
    return b;
  }


  public ToolItem createToolItem(NGToolBar parentComp, final ToolBar bar, ShellWindowInstance wi) throws NAFException
  {
    ToolItem i = new ToolItem(bar, typeStyles[type.ordinal()]);
    if(text != null) i.setText(DataDecoder.decodeAccessKey(text));
    else if((bar.getStyle() & SWT.RIGHT) != 0) i.setText("");
    parentComp.addRegisteredImage(imageResource, i, wi.imageManager);
    addButtonModels(i, wi);
    //addModels(i, wi);
    return i;
  }


  public MenuItem createMenuItem(Menu parent, WindowInstance wi) throws NAFException
  {
    MenuItem i = new MenuItem(parent, SWT.PUSH);
    if(defaultButton) parent.setDefaultItem(i);
    addCommonMenuFeatures(i, wi, DataDecoder.decodeAccessKey(text), accelerator, imageResource);
    addButtonModels(i, wi);
    return i;
  }


  private void addButtonModels(final Widget widget, final WindowInstance wi)
  {
    final IActionListener actionModel = getModel("action", wi.models);
    if(actionModel != null)
    {
      final ActionEvent ae = new ActionEvent(this, getModelBinding("action").getAttribute("command"), wi);

      widget.addListener(SWT.Selection, new Listener()
      {
        public void handleEvent(Event e)
        {
          actionModel.performAction(ae);
        }
      });
    }
    
    final NGComponent openWin = getFirstChild("open");
    if(openWin != null)
    {
      if(!(openWin instanceof IWindowInstanceWidget))
        throw new NAFException("Cannot assign non-IWindowInstanceWidget resource to role \"open\" of NGButton");
      widget.addListener(SWT.Selection, new Listener()
      {
        public void handleEvent (Event e)
        {
          WindowInstance dwi = ((IWindowInstanceWidget)openWin).createWindowInstance(wi.application, wi.models, wi);
          dwi.open();
        }
      });
    }

    final IObjectReadModel<String> textModel = getModel("text", wi.models);
    if(textModel != null)
    {
      final IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          String s = textModel.getValue();
          if(s == null) s = "";
          else s = DataDecoder.decodeAccessKey(s);
          if(s.equals(SWTUtil.getText(widget))) return;
          SWTUtil.setText(widget, s);
        }
      };
      SWTUtil.registerModel(widget, textModel, cl);
      cl.stateChanged(null);
    }
  }


  protected Object createDefaultModel(ModelBinding mb)
  {
    if("text".equals(mb.type)) return new DefaultStringModel();
    else if("action".equals(mb.type)) return new DefaultActionBroadcaster(mb.id);
    else return super.createDefaultModel(mb);
  }
}

⌨️ 快捷键说明

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