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

📄 nglabel.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.Alignment;
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 text or image label.
 * 
 * @author Stefan Zeiger (szeiger@novocode.com)
 * @since Dec 6, 2003
 */

public final class NGLabel extends NGWidget
{
  private Alignment alignment = Alignment.LEFT;
  private boolean wrap;
  private String text, attrText, imageResource;
  private LinkedList<String> childTokens;


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


  @XMLProperty("align")
  public void setAlignment(Alignment a) { this.alignment = a; }
  public Alignment getAlignment() { return alignment; }

  
  @XMLProperty
  public void setWrap(boolean b) { this.wrap = b; }
  public boolean getWrap() { return wrap; }
  
  
  @XMLProperty("image")
  public void setImageResource(String s) { this.imageResource = s; }
  public String getImageResource() { return imageResource; }


  protected void init() throws NAFException
  {
    super.init();
    if(childTokens != null && attrText == 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 // [TODO] Don't decode backslash sequences
  {
    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())) && (!"\n".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 if(n.getNodeType() == Node.ELEMENT_NODE
            && GUIResourceManager.NAF_NAMESPACE_URI.equals(n.getNamespaceURI())
            && n.getLocalName().equals("br"))
    {
      if(childTokens == null) childTokens = new LinkedList<String>();
      if(childTokens.size() != 0 && " ".equals(childTokens.getLast())) childTokens.removeLast();
      childTokens.add("\n");
    }
    else super.parseChild(n);
  }


  public Control createControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi, WidgetData pwd) throws NAFException
  {
    int style = alignment.style;
    if(wrap) style |= SWT.WRAP;
    final Label label = new Label(parent, style);
    SWTUtil.setRegisteredImage(imageResource, getResourceURL(), label, wi.imageManager, false);
    if(text != null) label.setText(DataDecoder.decodeAccessKey(DataDecoder.decodeBackslashEscapes(text)));

    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 = "";
          if(s.equals(label.getText())) return;
          label.setText(s);
        }
      };
      SWTUtil.registerModel(label, textModel, cl);
      cl.stateChanged(null);
    }
    
    return label;
  }


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

⌨️ 快捷键说明

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