2011年12月6日

jQuery事件縮寫



n   blur
n   change
n   click
n   dblclick
n   error
n   focus
n   keydown
n   keypress
n   keyup
n   load
n   mousedown
n   mousemove
n   mouseout
n   mouseover
n   mouseup
n   resize
n   scroll
n   select
n   submit
n   unload

2011年12月5日

TextLayoutFramework 簡單測試



TextLayoutFramework是Flex上一功能齊全的文字編輯器。
它比RichTextEditor的功能還要齊全,可以顯示使用者電腦上的字體,排版等。


// 宣告一個TextArea對象                   
var textarea:TextArea = new TextArea();
textarea.editable = false;
textarea.percentHeight = 100;
textarea.percentWidth = 100;
                      
// 用TextLayout Framework編輯出來的字串
var string:String = "<flow:TextFlow direction='ltr' blockProgression='tb' whiteSpaceCollapse='preserve' xmlns:flow='http://ns.adobe.com/textLayout/2008'><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'><flow:tab/>測試</flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p textAlign='center'><flow:span fontSize='48' fontFamily='華康巧風體W1'>測</flow:span><flow:span fontSize='48' fontFamily='Mixed'>試</flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='華康巧風體W1'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p><flow:p><flow:span fontSize='48' fontFamily='文泉驛微米黑'></flow:span></flow:p></flow:TextFlow>";
                      
// 宣告一個TextFlow對象
var textflow:TextFlow = new TextFlow();
textflow = TextFlowUtil.importFromString(string, WhiteSpaceCollapse.PRESERVE);
                      
// 將TextFlow對象設定給TextArea
textarea.textFlow = textflow;




預覽圖片 :

2011年11月20日

Java 源代碼分析系列 - ArrayList remove(Object object)


  public boolean remove(Object paramObject)
  {
    int i;
    if (paramObject == null)
      for (i = 0; i < this.size; ++i)
      {
        if (this.elementData[i] != null)
          continue;
        fastRemove(i);
        return true;
      }
    else
      for (i = 0; i < this.size; ++i)
      {
        if (!(paramObject.equals(this.elementData[i])))
          continue;
        fastRemove(i);
        return true;
      }
    return false;
  }

從代碼可以看到,remove這個方法可以刪除目標之object物件,並且可以使用它來刪除我們陣列null值,null刪除與非null刪除使用不同的判斷式來完成。
當迴圈中找到刪除目標時,會呼叫fastRemove(int)這個方法將該位置的元素刪除,fastRemove這個方法與ArrayList.remove(int)方法極為相似(參考Java 源代碼分析系列 - ArrayList remove(int pst)),但fastRemove如其名,式快速的刪除,因此裡面並沒有多餘的判斷(超出大小等),即沒有回傳值。代碼如下:

  private void fastRemove(int paramInt)
  {
    this.modCount += 1;
    int i = this.size - paramInt - 1;
    if (i > 0)
      System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);
    this.elementData[(--this.size)] = null;
  }


欲刪除paramInt位置











使用System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);將剩餘的陣列連接起來














 +







該方法會將欲刪除部分之後(黃色)陣列連接到原來陣列欲刪除的位置(紅色)上。











this.elementData[(--this.size)] = null;
刪除最後一個元素












Java 源代碼分析系列 - ArrayList remove(int pst)


  public E remove(int paramInt)
  {
    RangeCheck(paramInt);
    this.modCount += 1;
    Object localObject = this.elementData[paramInt];
    int i = this.size - paramInt - 1;
    if (i > 0)
      System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);
    this.elementData[(--this.size)] = null;
    return localObject;
  }


  private void RangeCheck(int paramInt)
  {
    if (paramInt < this.size)
      return;
    throw new IndexOutOfBoundsException("Index: " + paramInt + ", Size: " + this.size);
  }

首先必須先確定使用者要刪除的位置是否有超出陣列範圍,這裡使用RangeCheck(int)來檢查,若是超出範圍則會丟出IndexOutOfBoundsException例外。

如果範圍合理沒有超出範圍,這時候會算出刪除位置後所剩下的元素個數,接著使用System.arraycopy()這個方法將欲刪除元素後的所有元素加入到欲刪除的位置上。
實際操作如下面圖解:

欲刪除紅色











使用System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);將剩餘的陣列連接起來














 +







該方法會將欲刪除部分之後(黃色)陣列連接到原來陣列欲刪除的位置(紅色)上。











this.elementData[(--this.size)] = null;
刪除最後一個元素










return localObject;
回傳值為取代後,位置pst上的新元素。






下一篇 :  Java 源代碼分析系列 - ArrayList remove(Object object)



ShareThis