Label.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:14k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit;
  26. import com.sun.lwuit.animations.Animation;
  27. import com.sun.lwuit.geom.*;
  28. import com.sun.lwuit.plaf.DefaultLookAndFeel;
  29. import com.sun.lwuit.plaf.LookAndFeel;
  30. import com.sun.lwuit.plaf.Style;
  31. import com.sun.lwuit.plaf.UIManager;
  32. import java.util.Hashtable;
  33. /**
  34.  * Allows displaying labels and images with different alignment options, this class
  35.  * is a base class for several components allowing them to declare alignement/icon
  36.  * look in a similar way.
  37.  * 
  38.  * @author Chen Fishbein
  39.  */
  40. public class Label extends Component {
  41.     
  42.     private String text = "";
  43.     
  44.     private Image icon;
  45.     
  46.     private int align = LEFT;
  47.     private int valign = BOTTOM;
  48.     private int textPosition = RIGHT;
  49.     
  50.     private int gap = 2;
  51.     
  52.     private int shiftText = 0;
  53.     
  54.     private boolean tickerRunning = false;
  55.     
  56.     private boolean tickerEnabled = true;
  57.     
  58.     private long tickerStartTime;
  59.     
  60.     private long tickerDelay;
  61.     
  62.     private boolean rightToLeft;
  63.     
  64.     private boolean endsWith3Points = true;
  65.     
  66.     /** 
  67.      * Constructs a new label with the specified string of text, left justified.
  68.      * 
  69.      * @param text the string that the label presents.
  70.      */
  71.     public Label(String text) {
  72.         setUIID("Label");
  73.         this.text = text;
  74.         localize();
  75.         setFocusable(false);
  76.     }
  77.     Label(String text, String uiid) {
  78.         this.text = text;
  79.         localize();
  80.         setFocusable(false);
  81.         setAlignment(CENTER);
  82.         setUIID(uiid);
  83.     }
  84.     
  85.     /**
  86.      * Construct an empty label
  87.      */
  88.     public Label() {
  89.         this("");
  90.     }
  91.     /** 
  92.      * Constructs a new label with the specified icon
  93.      * 
  94.      * @param icon the image that the label presents.
  95.      */
  96.     public Label(Image icon) {
  97.         this("");
  98.         this.icon = icon;
  99.     }
  100.     /**
  101.      * @inheritDoc
  102.      */
  103.     public int getBaselineResizeBehavior() {
  104.         switch(valign) {
  105.         case TOP:
  106.             return BRB_CONSTANT_ASCENT;
  107.         case BOTTOM:
  108.             return BRB_CONSTANT_DESCENT;
  109.         case CENTER:
  110.             return BRB_CENTER_OFFSET;
  111.         }
  112.         return BRB_OTHER;
  113.     }
  114.     /**
  115.      * Sets the Label text
  116.      * 
  117.      * @param text the string that the label presents.
  118.      */
  119.     public void setText(String text){
  120.         this.text = text;
  121.         localize();
  122.         setShouldCalcPreferredSize(true);
  123.         repaint();
  124.     }
  125.     
  126.     private void localize() {
  127.         Hashtable t =  UIManager.getInstance().getResourceBundle();
  128.         if(t != null && text != null) {
  129.             Object o = t.get(text);
  130.             if(o != null) {
  131.                 this.text = (String)o;
  132.             }
  133.         } 
  134.     }
  135.     
  136.     /**
  137.      * @inheritDoc
  138.      */
  139.     void initComponentImpl() {
  140.         super.initComponentImpl();
  141.         if(hasFocus()) {
  142.             LookAndFeel lf = UIManager.getInstance().getLookAndFeel();
  143.             if(lf instanceof DefaultLookAndFeel) {
  144.                 ((DefaultLookAndFeel)lf).focusGained(this);
  145.             }
  146.         }
  147.         // solves the case of a user starting a ticker before adding the component
  148.         // into the container
  149.         if(isTickerEnabled() && isTickerRunning() && !isCellRenderer()) {
  150.             getComponentForm().registerAnimatedInternal(this);
  151.         }
  152.     }
  153.     
  154.     /**
  155.      * Returns the label text
  156.      * 
  157.      * @return the label text
  158.      */
  159.     public String getText(){
  160.         return text;
  161.     }
  162.     
  163.     /**
  164.      * Sets the Label icon, if the icon is unmodified a repaint would not be triggered
  165.      * 
  166.      * @param icon the image that the label presents.
  167.      */
  168.     public void setIcon(Image icon){
  169.         if(this.icon == icon) {
  170.             return;
  171.         }
  172.         this.icon = icon;
  173.         setShouldCalcPreferredSize(true);
  174.         checkAnimation();
  175.         repaint();
  176.     }
  177.     
  178.     void checkAnimation() {
  179.         super.checkAnimation();
  180.         if(icon != null && icon.isAnimation()) {
  181.             Form parent = getComponentForm();
  182.             if(parent != null) {
  183.                 // animations are always running so the internal animation isn't
  184.                 // good enough. We never want to stop this sort of animation
  185.                 parent.registerAnimated(this);
  186.             }
  187.         }
  188.     }
  189.     
  190.     /**
  191.      * Returns the labels icon
  192.      * 
  193.      * @return the labels icon
  194.      */
  195.     public Image getIcon(){
  196.         return icon;
  197.     }
  198.     
  199.     /**
  200.      * Sets the Alignment of the Label to one of: CENTER, LEFT, RIGHT
  201.      * 
  202.      * @param align alignment value
  203.      * @see #CENTER
  204.      * @see #LEFT
  205.      * @see #RIGHT
  206.      */
  207.     public void setAlignment(int align){
  208.         if(align != CENTER && align != RIGHT && align != LEFT){
  209.             throw new IllegalArgumentException("Alignment can't be set to " + align);
  210.         }
  211.         this.align = align;
  212.     }
  213.     
  214.     /**
  215.      * Sets the vertical alignment of the Label to one of: CENTER, TOP, BOTTOM
  216.      * 
  217.      * @param valign alignment value
  218.      * @see #CENTER
  219.      * @see #TOP
  220.      * @see #BOTTOM
  221.      */
  222.     public void setVerticalAlignment(int valign) {
  223.         if(valign != CENTER && valign != TOP && valign != BOTTOM){
  224.             throw new IllegalArgumentException("Alignment can't be set to " + valign);
  225.         }
  226.         this.valign = valign;
  227.     }
  228.     /**
  229.      * Returns the vertical alignment of the Label, this will only work when the icon
  230.      * is in the side of the text and not above or bellow it.
  231.      * 
  232.      * @return the vertical alignment of the Label one of: CENTER, TOP, BOTTOM
  233.      * @see #CENTER
  234.      * @see #TOP
  235.      * @see #BOTTOM
  236.      */
  237.     public int getVerticalAlignment(){
  238.         return valign;
  239.     }
  240.     
  241.     /**
  242.      * Returns the alignment of the Label
  243.      * 
  244.      * @return the alignment of the Label one of: CENTER, LEFT, RIGHT
  245.      * @see #CENTER
  246.      * @see #LEFT
  247.      * @see #RIGHT
  248.      */
  249.     public int getAlignment(){
  250.         return align;
  251.     }
  252.     /**
  253.      * Sets the position of the text relative to the icon if exists
  254.      *
  255.      * @param textPosition alignment value (LEFT, RIGHT, BOTTOM or TOP)
  256.      * @see #LEFT
  257.      * @see #RIGHT
  258.      * @see #BOTTOM
  259.      * @see #TOP
  260.      */
  261.     public void setTextPosition(int textPosition) {
  262.         if (textPosition != LEFT && textPosition != RIGHT && textPosition != BOTTOM && textPosition != TOP) {
  263.             throw new IllegalArgumentException("Text position can't be set to " + textPosition);
  264.         }
  265.         this.textPosition = textPosition;
  266.     }
  267.     
  268.     /**
  269.      * Returns The position of the text relative to the icon
  270.      * 
  271.      * @return The position of the text relative to the icon, one of: LEFT, RIGHT, BOTTOM, TOP
  272.      * @see #LEFT
  273.      * @see #RIGHT
  274.      * @see #BOTTOM
  275.      * @see #TOP
  276.      */
  277.     public int getTextPosition(){
  278.         return textPosition;
  279.     }
  280.     
  281.     /**
  282.      * Set the gap in pixels between the icon/text to the Label boundaries
  283.      * 
  284.      * @param gap the gap in pixels
  285.      */
  286.     public void setGap(int gap) {
  287.         this.gap = gap;
  288.     }
  289.     
  290.     /**
  291.      * Returns the gap in pixels between the icon/text to the Label boundaries
  292.      * 
  293.      * @return the gap in pixels between the icon/text to the Label boundaries
  294.      */
  295.     public int getGap() {
  296.         return gap;
  297.     }
  298.     
  299.     /**
  300.      * @inheritDoc
  301.      */
  302.     protected String paramString() {
  303.         return super.paramString() + ", text = " +getText() + ", gap = " + gap;
  304.     }
  305.     
  306.     /**
  307.      * @inheritDoc
  308.      */
  309.     public void paint(Graphics g) {
  310.         UIManager.getInstance().getLookAndFeel().drawLabel(g, this);
  311.     }
  312.     /**
  313.      * @inheritDoc
  314.      */
  315.     protected Dimension calcPreferredSize(){
  316.         return UIManager.getInstance().getLookAndFeel().getLabelPreferredSize(this);
  317.     }
  318.     
  319.     /**
  320.      * Simple getter to return how many pixels to shift the text inside the Label
  321.      * @return number of pixels to shift
  322.      */
  323.     public int getShiftText() {
  324.         return shiftText;
  325.     }
  326.     /**
  327.      * This method shifts the text from it's position in pixels.
  328.      * The value can be positive/negative to move the text to the right/left
  329.      * 
  330.      * @param shiftText The number of pixels to move the text
  331.      */
  332.     public void setShiftText(int shiftText) {
  333.         this.shiftText = shiftText;
  334.     }
  335.     
  336.     /**
  337.      * Returns true if a ticker should be started since there is no room to show
  338.      * the text in the label.
  339.      * 
  340.      * @return true if a ticker should start running
  341.      */
  342.     public boolean shouldTickerStart() {
  343.         Style style = getStyle();
  344.         int txtW = style.getFont().stringWidth(getText());
  345.         int textSpaceW = getAvaliableSpaceForText();
  346.         return txtW > textSpaceW && textSpaceW > 0;
  347.     }
  348.     Image getIconFromState() {
  349.         return getIcon();
  350.     }
  351.     int getAvaliableSpaceForText() {
  352.         Style style = getStyle();
  353.         int textSpaceW = getWidth() - style.getPadding(isRTL(), Label.RIGHT) - style.getPadding(isRTL(), Label.LEFT);
  354.         Image icon = getIconFromState();
  355.         if (icon != null && (getTextPosition() == Label.RIGHT || getTextPosition() == Label.LEFT)) {
  356.             textSpaceW = textSpaceW - icon.getWidth();
  357.         }
  358.         int preserveSpaceForState = 0;
  359.         textSpaceW = textSpaceW - preserveSpaceForState;
  360.         return textSpaceW;
  361.     }
  362.     /**
  363.      * This method will start the text ticker
  364.      * 
  365.      * @param delay the delay in millisecods between animation intervals
  366.      * @param rightToLeft if true move the text to the left
  367.      */
  368.     public void startTicker(long delay, boolean rightToLeft){
  369.         //return if ticker is not enabled
  370.         if(!tickerEnabled){
  371.             return;
  372.         }
  373.         if(!isCellRenderer()){
  374.             Form parent = getComponentForm();
  375.             if(parent != null) {
  376.                 parent.registerAnimatedInternal(this);
  377.             }
  378.         }
  379.         tickerStartTime = System.currentTimeMillis();
  380.         tickerDelay = delay;
  381.         tickerRunning = true;
  382.         this.rightToLeft = rightToLeft;
  383.         if (isRTL()) {
  384.          this.rightToLeft = !this.rightToLeft;
  385.         }
  386.     }
  387.     
  388.     /**
  389.      * Stops the text ticker
  390.      */
  391.     public void stopTicker(){
  392.         tickerRunning = false;
  393.         setShiftText(0);
  394.         deregisterAnimatedInternal();
  395.     }
  396.     /**
  397.      * Returns true if the ticker is running
  398.      * 
  399.      * @return true if the ticker is running
  400.      */
  401.     public boolean isTickerRunning() {
  402.         return tickerRunning;
  403.     }
  404.     /**
  405.      * Sets the Label to allow ticking of the text.
  406.      * By default is true
  407.      * 
  408.      * @param tickerEnabled
  409.      */
  410.     public void setTickerEnabled(boolean tickerEnabled) {
  411.         this.tickerEnabled = tickerEnabled;
  412.     }
  413.     /**
  414.      * This method return true if the ticker is enabled on this Label
  415.      * 
  416.      * @return tickerEnabled
  417.      */
  418.     public boolean isTickerEnabled() {
  419.         return tickerEnabled;
  420.     }
  421.    
  422.     /**
  423.      * If the Label text is too long fit the text to the widget and add "..."
  424.      * points at the end.
  425.      * By default this is set to true
  426.      * 
  427.      * @param endsWith3Points true if text should add "..." at the end
  428.      */
  429.     public void setEndsWith3Points(boolean endsWith3Points){
  430.         this.endsWith3Points = endsWith3Points;
  431.     }
  432.     /**
  433.      * Simple getter
  434.      * 
  435.      * @return true if this Label adds "..." when the text is too long
  436.      */
  437.     public boolean isEndsWith3Points() {
  438.         return endsWith3Points;
  439.     }
  440.     
  441.     
  442.     
  443.     /**
  444.      * @inheritDoc
  445.      */
  446.     public boolean animate() {
  447.         boolean animateTicker = false;
  448.         if(tickerRunning && tickerStartTime + tickerDelay < System.currentTimeMillis()){
  449.             tickerStartTime = System.currentTimeMillis();
  450.             if(rightToLeft){
  451.                 shiftText-=2;
  452.             }else{
  453.                 shiftText+=2;
  454.             }     
  455.             animateTicker = true;
  456.         }                
  457.         // if we have an animated icon then just let it do its thing...
  458.         boolean val = icon != null && icon.isAnimation() && ((Animation)icon).animate();
  459.         boolean parent = super.animate();
  460.         return  val || parent || animateTicker;
  461.     }
  462. }