2011年5月30日 星期一

[ ExtJS2.0 開發指南 ] CH4 : 常用表單 - 表單及表單元素 (3)


Ext.form.ComboBox 組合框 :
Ext.form.ComboBox (組合框) 擴展自 Ext.form.TriggerField 組件, 它支持自動完成, 遠程加載數據, 分頁等特性, 是在開發中使用頻繁的組件, 在所有表單中它的配置也是相對複雜. 在下表列出主要配置項, 因為功能複雜在一個範例不足以描述, 故接下來會分幾個範例進行說明 :




- 本地數據源的組合框
在這個範例中會創建一個本地模式的組合框, 這個範例是一個允許選擇郵政編碼的組合框, 該組框有特點 :
* 允許用戶輸入但是要求輸入值必須是列表中已存在的項目.
* 在用戶輸入過程中會自動匹配剩餘的文本.
* 用戶可以自由改變下拉列表的大小.

這三點分別透過設置 forceSelection, typeAhead 和 resizeable 得以實現. 請參考範例代碼如下 : 

執行結果 :


- 遠程數據源的組合框
這個範例將創建一個讀取遠端數據的組合框, 它通過向服務器發送查詢字符串來得到一份相關書籍的列表, 並具有以下三個特點 :
* 在組合框中輸入 3 個字母後開始搜尋書籍, 當輸入不滿3個字元不進行查詢.
* 單擊觸發按鈕將會查詢全部書籍列表, 不進行篩選.
* 指定服務器讀取查詢條件時使用的查詢名稱. 並指定組合框在加載數據時的提示訊息.

範例代碼如下 : 


執行結果 :

在上面代碼使用 Ext.data.HttpProxy 來讀取遠端數據, 最後在組合框的下拉列表中顯示出來.

- 帶分頁功能的組合框
在上述兩個範例中對於顯示數據較少的使用情況下是完美的, 但是在數據量非常大的情況下, 如 200 條數據同時顯示在一個列表中, 這對於用戶的查找和使用都是不方便的. 為此在這裡接下來的範例中創建一個具有分頁功能的組合框, 用來顯示較大的數量的訊息 : 

執行結果 :

這裡使用 Ext.data.JsonStore 數據源組件, 它是用來讀取 json 格式數據的數據源, 因為在分頁過程服務器將向頁面返回包含總條樹目與當前頁明細的相關數據, 使用原先的 Ext.data.SimpleStore 是無法達到的, 所以在這情況下使用了 json 格式作為服務器和客戶端交換的標準. 另外由於使用了 json 作為數據返回格式, 所以對 json 格式也是有要求的, 下面是服務器端返回的一段 json 格式的字符串 : 

- 轉換一個 HTML 標準組件 select 為 Ext.form.ComboBox
在上面的 3 個範例中每次都是新建一個組合框, 然後透過數據源組件為其提供數據. 其實 Ext.form.ComboBox 還支持另一種創建方式, 就是將一個現有的 HTML 標準組件 select 轉換成 Ext.form.ComboBox 組件. 這種方式對於改造一個已有項目中的下拉列表非常有用, 不但可以美化頁面也可以得到 Ext.form.ComboBox 組件才有的功能 : 

這裡有幾點要注意, 否則會出現非預期結果 :
必須設置 lazyRender 配置項為 true, 否則會出現如下圖怪異效果 :

注意 div  id='form' 與 ID="levelName" 在頁面出現的相對位置, 如果將兩個位置對調則在單擊組件會發生錯誤!

2011年5月29日 星期日

[ Java 代碼範本 ] 取得系統記憶體, CPU 相關使用資訊


參考自 這理
前言 :
最近做個項目,就是要取得cpu佔有率等等的系統信息,一開始以為要用動態鏈接庫了,但後來發現可以像下面這樣做,不去調用jni,這樣省去了很多JNI 所帶來的 effort. 在Java中,可以獲得總的物理內存、剩餘的物理內存、已使用的物理內存等信息,下面例子可以取得這些信息,並且獲得在Windows下的內存使用率.

實作過程 :
首先編寫一個 MonitorInfoBean 類,用來裝載監控的一些信息,包括物理內存、剩餘的物理內存、已使用的物理內存、內存使用率等字段,該類的代碼如下 :
- MonitorInfoBean.java : 裝載監控的信息Bean
  1. package perf.beans;  
  2.   
  3. public class MonitorInfoBean {  
  4.       
  5.     /** */  
  6.     /** 可使用內存. */  
  7.     private long totalMemory;  
  8.   
  9.     /** */  
  10.     /** 剩餘內存. */  
  11.     private long freeMemory;  
  12.   
  13.     /** */  
  14.     /** 最大可使用內存. */  
  15.     private long maxMemory;  
  16.   
  17.     /** */  
  18.     /** 操作系統. */  
  19.     private String osName;  
  20.   
  21.     /** */  
  22.     /** 總物理內存. */  
  23.     private long totalMemorySize;  
  24.   
  25.     /** */  
  26.     /** 剩餘的物理內存. */  
  27.     private long freePhysicalMemorySize;  
  28.   
  29.     /** */  
  30.     /** 已使用的物理內存. */  
  31.     private long usedMemory;  
  32.   
  33.     /** */  
  34.     /** 線程總數. */  
  35.     private int totalThread;  
  36.   
  37.     /** */  
  38.     /** cpu使用率. */  
  39.     private double cpuRatio;  
  40.   
  41.     public long getFreeMemory() {  
  42.         return freeMemory;  
  43.     }  
  44.   
  45.     public void setFreeMemory(long freeMemory) {  
  46.         this.freeMemory = freeMemory;  
  47.     }  
  48.   
  49.     public long getFreePhysicalMemorySize() {  
  50.         return freePhysicalMemorySize;  
  51.     }  
  52.   
  53.     public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {  
  54.         this.freePhysicalMemorySize = freePhysicalMemorySize;  
  55.     }  
  56.   
  57.     public long getMaxMemory() {  
  58.         return maxMemory;  
  59.     }  
  60.   
  61.     public void setMaxMemory(long maxMemory) {  
  62.         this.maxMemory = maxMemory;  
  63.     }  
  64.   
  65.     public String getOsName() {  
  66.         return osName;  
  67.     }  
  68.   
  69.     public void setOsName(String osName) {  
  70.         this.osName = osName;  
  71.     }  
  72.   
  73.     public long getTotalMemory() {  
  74.         return totalMemory;  
  75.     }  
  76.   
  77.     public void setTotalMemory(long totalMemory) {  
  78.         this.totalMemory = totalMemory;  
  79.     }  
  80.   
  81.     public long getTotalMemorySize() {  
  82.         return totalMemorySize;  
  83.     }  
  84.   
  85.     public void setTotalMemorySize(long totalMemorySize) {  
  86.         this.totalMemorySize = totalMemorySize;  
  87.     }  
  88.   
  89.     public int getTotalThread() {  
  90.         return totalThread;  
  91.     }  
  92.   
  93.     public void setTotalThread(int totalThread) {  
  94.         this.totalThread = totalThread;  
  95.     }  
  96.   
  97.     public long getUsedMemory() {  
  98.         return usedMemory;  
  99.     }  
  100.   
  101.     public void setUsedMemory(long usedMemory) {  
  102.         this.usedMemory = usedMemory;  
  103.     }  
  104.   
  105.     public double getCpuRatio() {  
  106.         return cpuRatio;  
  107.     }  
  108.   
  109.     public void setCpuRatio(double cpuRatio) {  
  110.         this.cpuRatio = cpuRatio;  
  111.     }  
  112. }  

接著編寫一個獲得當前的監控信息的接口,該類的代碼如下所示 :
- IMonitorService.java : 提供解耦合的介面
  1. package perf.proto;  
  2.   
  3. import perf.beans.MonitorInfoBean;  
  4.   
  5. public interface IMonitorService {  
  6.     /** *//** 
  7.      * 獲得當前監控對像. 
  8.      * @return 返回監測對象 
  9.      * @throws Exception 
  10.      */  
  11.     public MonitorInfoBean getMonitorInfoBean() throws Exception;  
  12. }  

該類的實現類MonitorServiceImpl如下所示 :
- MonitorServiceImpl.java : 獲取系統訊息的實作類別
  1. package perf.utils;  
  2.   
  3. import perf.beans.MonitorInfoBean;  
  4. import perf.proto.IMonitorService;  
  5. import java.io.InputStreamReader;  
  6. import java.io.LineNumberReader;  
  7. import sun.management.ManagementFactory;  
  8. import com.sun.management.OperatingSystemMXBean;  
  9.   
  10.   
  11. public class MonitorServiceImpl implements IMonitorService{  
  12.     public  static final int CPUTIME = 5000;  
  13.     private static final int PERCENT = 100;  
  14.     private static final int FAULTLENGTH = 10;  
  15.     private static String PROC_CMD = System.getenv("windir")  
  16.                                     + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"  
  17.                                     + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";  
  18.     private long[] initCpuInfo = null;  
  19.       
  20.     public MonitorServiceImpl(){  
  21.         try{  
  22.             initCpuInfo = readCpu(Runtime.getRuntime().exec(PROC_CMD));  
  23.         }catch(Exception e){  
  24.             e.printStackTrace();  
  25.             initCpuInfo = null;  
  26.         }  
  27.     }  
  28.       
  29.     @Override  
  30.     public MonitorInfoBean getMonitorInfoBean() throws Exception {  
  31.         int kb = 1024;  
  32.           
  33.         // 可使用內存  
  34.         long totalMemory = Runtime.getRuntime().totalMemory() / kb;  
  35.         // 剩餘內存  
  36.         long freeMemory = Runtime.getRuntime().freeMemory() / kb;  
  37.         // 最大可使用內存  
  38.         long maxMemory = Runtime.getRuntime().maxMemory() / kb;  
  39.   
  40.         OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory  
  41.                 .getOperatingSystemMXBean();  
  42.   
  43.         // 操作系統  
  44.         String osName = System.getProperty("os.name");  
  45.         // 總物理內存  
  46.         long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;  
  47.         // 剩餘的物理內存  
  48.         long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;  
  49.         // 已使用的物理內存  
  50.         long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb  
  51.                 .getFreePhysicalMemorySize())  
  52.                 / kb;  
  53.   
  54.         // 獲得線程總數  
  55.         ThreadGroup parentThread;  
  56.         for (parentThread = Thread.currentThread().getThreadGroup(); parentThread  
  57.                 .getParent() != null; parentThread = parentThread.getParent())  
  58.             ;  
  59.         int totalThread = parentThread.activeCount();  
  60.   
  61.         double cpuRatio = 0;  
  62.         if (osName.toLowerCase().startsWith("windows")) {             
  63.             cpuRatio = this.getCpuRatioForWindows();  
  64.         }  
  65.           
  66.         // 返回對象  
  67.         MonitorInfoBean infoBean = new MonitorInfoBean();  
  68.         infoBean.setFreeMemory(freeMemory);  
  69.         infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);  
  70.         infoBean.setMaxMemory(maxMemory);  
  71.         infoBean.setOsName(osName);  
  72.         infoBean.setTotalMemory(totalMemory);  
  73.         infoBean.setTotalMemorySize(totalMemorySize);  
  74.         infoBean.setTotalThread(totalThread);  
  75.         infoBean.setUsedMemory(usedMemory);  
  76.         infoBean.setCpuRatio(cpuRatio);  
  77.         return infoBean;  
  78.     }  
  79.       
  80.     private double getCpuRatioForWindows() {  
  81.         try {  
  82.             if(initCpuInfo==nullreturn 0.0;  
  83.             // 取得進程信息  
  84.             //long[] c0 = readCpu(Runtime.getRuntime().exec(PROC_CMD));  
  85.             //Thread.sleep(CPUTIME);  
  86.             long[] c1 = readCpu(Runtime.getRuntime().exec(PROC_CMD));  
  87.             if (c1 != null) {  
  88.                 long idletime = c1[0] - initCpuInfo[0];  
  89.                 long busytime = c1[1] - initCpuInfo[1];  
  90.                 return Double.valueOf(  
  91.                         PERCENT * (busytime) / (busytime + idletime))  
  92.                         .doubleValue();  
  93.             } else {  
  94.                 return 0.0;  
  95.             }  
  96.         } catch (Exception ex) {  
  97.             ex.printStackTrace();  
  98.             return 0.0;  
  99.         }  
  100.     }  
  101.       
  102.     private long[] readCpu(final Process proc) {  
  103.         long[] retn = new long[2];  
  104.         try {  
  105.             proc.getOutputStream().close();  
  106.             InputStreamReader ir = new InputStreamReader(proc.getInputStream());  
  107.             LineNumberReader input = new LineNumberReader(ir);  
  108.             String line = input.readLine();  
  109.             if (line == null || line.length() < FAULTLENGTH) {  
  110.                 return null;  
  111.             }  
  112.             int capidx = line.indexOf("Caption");  
  113.             int cmdidx = line.indexOf("CommandLine");  
  114.             int rocidx = line.indexOf("ReadOperationCount");  
  115.             int umtidx = line.indexOf("UserModeTime");  
  116.             int kmtidx = line.indexOf("KernelModeTime");  
  117.             int wocidx = line.indexOf("WriteOperationCount");  
  118.             long idletime = 0;  
  119.             long kneltime = 0;  
  120.             long usertime = 0;  
  121.             while ((line = input.readLine()) != null) {  
  122.                 if (line.length() < wocidx) {  
  123.                     continue;  
  124.                 }  
  125.                 // 字段出現順序:Caption,CommandLine,KernelModeTime,ReadOperationCount,  
  126.                 // ThreadCount,UserModeTime,WriteOperation  
  127.                 String caption = Bytes.substring(line, capidx, cmdidx - 1).trim();  
  128.                 String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();  
  129.                 if (cmd.indexOf("wmic.exe") >= 0) {  
  130.                     continue;  
  131.                 }  
  132.                 // log.info("line="+line);  
  133.                 if (caption.equals("System Idle Process")  
  134.                         || caption.equals("System")) {  
  135.                     idletime += Long.valueOf(  
  136.                             Bytes.substring(line, kmtidx, rocidx - 1).trim())  
  137.                             .longValue();  
  138.                     idletime += Long.valueOf(  
  139.                             Bytes.substring(line, umtidx, wocidx - 1).trim())  
  140.                             .longValue();  
  141.                     continue;  
  142.                 }  
  143.   
  144.                 kneltime += Long.valueOf(  
  145.                         Bytes.substring(line, kmtidx, rocidx - 1).trim())  
  146.                         .longValue();  
  147.                 usertime += Long.valueOf(  
  148.                         Bytes.substring(line, umtidx, wocidx - 1).trim())  
  149.                         .longValue();  
  150.             }  
  151.             retn[0] = idletime;  
  152.             retn[1] = kneltime + usertime;  
  153.             return retn;  
  154.         } catch (Exception ex) {  
  155.             ex.printStackTrace();  
  156.         } finally {  
  157.             try {  
  158.                 proc.getInputStream().close();  
  159.             } catch (Exception e) {  
  160.                 e.printStackTrace();  
  161.             }  
  162.         }  
  163.         return null;  
  164.     }  
  165. }  

該實現類中需要用到一個自己編寫byte的工具類,該類的代碼如下所示 :
- Bytes.java : 解決 String.subString() 處理中文的問題 (將中文試為一個byte)
  1. package perf.utils;  
  2.   
  3. public class Bytes {  
  4.     public static String substring(String src, int start_idx, int end_idx){  
  5.         byte[] b = src.getBytes();  
  6.         String tgt = "";  
  7.         for(int i=start_idx; i<=end_idx; i++){  
  8.             tgt +=(char)b[i];  
  9.         }  
  10.         return tgt;  
  11.     }  
  12. }  

測試結果 :
測試我們可以建立一個 MonitorServiceImpl 類別的實例, 在一建立時例後 MonitorServiceImpl 會立即取得當下的 CPU 相關初始訊息. 接著我們讓當前線程 Sleep 6 秒後在呼叫 getMonitorInfoBean() 函式已取的相關系統訊息, 測試代碼如下 :
- TMain.java : 測試類別
  1. package perf.test;  
  2.   
  3. import perf.beans.MonitorInfoBean;  
  4. import perf.utils.MonitorServiceImpl;  
  5.   
  6. public class TMain {  
  7.     public static void main(String args[])  
  8.     {  
  9.         MonitorServiceImpl MSImpl = new MonitorServiceImpl();  
  10.         try {  
  11.             Thread.sleep(MonitorServiceImpl.CPUTIME);  
  12.             MonitorInfoBean bean = MSImpl.getMonitorInfoBean();  
  13.             System.out.println("OSName: "+bean.getOsName());  
  14.             System.out.println("CPU Ratio: "+bean.getCpuRatio());  
  15.             System.out.println("Available Mem in JVM: "+String.valueOf(bean.getTotalMemory()/1000.0)+" MB");  
  16.             System.out.println("Free Mem in JVM: "+bean.getFreeMemory()/1000.0+" MB");  
  17.             System.out.println("Max Available Mem for JVM: "+bean.getMaxMemory()/1000.0+" MB");  
  18.             System.out.println("============================");  
  19.             System.out.println("Total Pyysical Mem Size: "+bean.getTotalMemorySize()/1000.0+" MB");  
  20.             System.out.println("Total Physical Free Mem: "+bean.getFreePhysicalMemorySize()/1000.0+" MB");  
  21.             System.out.println("Total Physical Used Memory: "+bean.getUsedMemory()/1000.0+" MB");  
  22.             System.out.println("============================");  
  23.             System.out.println("Total Thread in Java: "+bean.getTotalThread());  
  24.         } catch (Exception e) {           
  25.             e.printStackTrace();  
  26.         }  
  27.           
  28.     }  
  29. }  

執行結果 :
OSName: Windows 7
CPU Ratio: 12.0
Available Mem in JVM: 79.296 MB
Free Mem in JVM: 55.281 MB
Max Available Mem for JVM: 932.096 MB
============================
Total Pyysical Mem Size: 4191.412 MB
Total Physical Free Mem: 1311.06 MB
Total Physical Used Memory: 2880.352 MB
============================
Total Thread in Java: 5

補充說明 :
[Windows 技巧] wmic 使用簡介
wmic 是 Microsoft Windows Management Instrument 的縮寫. 提供了 Console mode 一個強而有力的 administrator 管理工具. 包或系統管理, 遠端主機訊息獲取到你想不到的更改主機名稱等功能都可以達成. 而 wmic 可以在路徑 C:\Windows\System32\wbem 下找到 (wmic.exe)...
This message was edited 3 times. Last update was at 26/05/2011 15:07:17

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...