Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Blog Archive
Stats
Cumulative sum
SELECT * FROM pg_account;
 
SELECT
    s.pg_id,
    s.pg_name,
    s.pg_entry,
    SUM(NVL(c.pg_entry,0)) cumulative,
    ROUND(s.pg_entry / (SELECT SUM(pg_entry) FROM pg_account)*100, 5) percentage,
    ROUND(SUM(NVL(c.pg_entry,0))/(SELECT SUM(pg_entry) FROM pg_account)*100,5) cumulative_percentage
FROM pg_account s, pg_account c
WHERE s.pg_id > c.pg_id OR (s.pg_id = c.pg_id AND s.pg_entry = c.pg_entry)
GROUP BY s.pg_id, s.pg_name, s.pg_entry
ORDER BY s.pg_id
;
 
SELECT
    s.pg_id,
    s.pg_name,
    s.pg_entry,
    COUNT(c.pg_id) ranking
FROM pg_account s, pg_account c
WHERE s.pg_entry < c.pg_entry OR (s.pg_id = c.pg_id AND s.pg_entry = c.pg_entry)
GROUP BY s.pg_id, s.pg_name, s.pg_entry, s.pg_entry
ORDER BY s.pg_entry DESC
;
PG_ID                  PG_NAME              PG_ENTRY               
---------------------- -------------------- ---------------------- 
1                      Bruce                22                     
2                      James                72                     
3                      Yilin                65                     
4                      Ted                  77                     
5                      Charles              35                     
6                      Sean                 43                     
7                      Paul                 57                     
8                      Ken                  35                     
8 個資料列已選取

PG_ID                  PG_NAME              PG_ENTRY               CUMULATIVE             PERCENTAGE             CUMULATIVE_PERCENTAGE  
---------------------- -------------------- ---------------------- ---------------------- ---------------------- ---------------------- 
1                      Bruce                22                     22                     5.41872                5.41872                
2                      James                72                     94                     17.73399               23.15271               
3                      Yilin                65                     159                    16.00985               39.16256               
4                      Ted                  77                     236                    18.96552               58.12808               
5                      Charles              35                     271                    8.62069                66.74877               
6                      Sean                 43                     314                    10.59113               77.3399                
7                      Paul                 57                     371                    14.03941               91.37931               
8                      Ken                  35                     406                    8.62069                100                    
8 個資料列已選取

PG_ID                  PG_NAME              PG_ENTRY               RANKING                
---------------------- -------------------- ---------------------- ---------------------- 
4                      Ted                  77                     1                      
2                      James                72                     2                      
3                      Yilin                65                     3                      
7                      Paul                 57                     4                      
6                      Sean                 43                     5                      
5                      Charles              35                     6                      
8                      Ken                  35                     6                      
1                      Bruce                22                     8                      
8 個資料列已選取

Change network configuration (windows)
Manual setting
@ECHO OFF
set varAdapterName="Local Area Connection"
set varIP=192.168.1.68
set varMask=255.255.255.0
set varGateway=192.168.1.1
set varDNS1=192.168.1.1
set varDNS2=192.168.1.1

ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = %varAdapterName% source = static addr = %varIP% mask = %varMask%

ECHO Setting Gateway
netsh int ip set address name = %varAdapterName% gateway = %varGateway% gwmetric = 1

ECHO Setting Primary DNS
netsh int ip set dns name = %varAdapterName% source = static addr = %varDNS1%

ECHO Setting Secondary DNS
netsh int ip add dns name = %varAdapterName% addr = %varDNS2%
DHCP
@set varAdapterName="Local Area Connection"

netsh interface ip set address name=%varAdapterName% source=dhcp
netsh interface ip set dns name=%varAdapterName% source=dhcp register=primary
netsh interface ip set wins name=%varAdapterName% source=dhcp
Select/update table where trigger against, ORA-04091 error occurs
CREATE OR REPLACE
TRIGGER udpate_sheet_processing 
AFTER INSERT ON sheet_item 
FOR EACH ROW


DECLARE
    PRAGMA AUTONOMOUS_TRANSACTION;  -- 自治事務 
    v_sheet_id VARCHAR2(50);
    v_processing_entry NUMBER(5);
    CURSOR get_processing_amount IS
        SELECT count(*)+1
        FROM sheet_item
        WHERE sheet_id = v_sheet_id
    ;
    
BEGIN
    v_sheet_id := :NEW.sheet_id;

    OPEN get_processing_amount;
    FETCH get_processing_amount INTO v_processing_entry;
    CLOSE get_processing_amount;

    UPDATE product_checksheet
    SET sheet_processing_entry = v_processing_entry
    WHERE sheet_id = v_sheet_id;
    
    COMMIT;  -- commit for update
END;
MySQL remote access by root
my.cnf
#bind-address = 127.0.0.1   (disable this setting for listening any address)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root_password' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
vi hot key guide

  • 第一部份:一般模式可用的按鈕說明,游標移動、複製貼上、搜尋取代等

    移動游標的方法
    h 或 向左方向鍵(←)游標向左移動一個字元
    j 或 向下方向鍵(↓)游標向下移動一個字元
    k 或 向上方向鍵(↑)游標向上移動一個字元
    l 或 向右方向鍵(→)游標向右移動一個字元
    如果你將右手放在鍵盤上的話,你會發現 hjkl 是排列在一起的,因此可以使用這四個按鈕來移動游標。 如果想要進行多次移動的話,例如向下移動 30 行,可以使用 "30j" 或 "30↓" 的組合按鍵, 亦即加上想要進行的次數(數字)後,按下動作即可!
    [Ctrl] + [f]螢幕『向下』移動一頁,相當於 [Page Down]按鍵 (常用)
    [Ctrl] + [b]螢幕『向上』移動一頁,相當於 [Page Up] 按鍵 (常用)
    [Ctrl] + [d]螢幕『向下』移動半頁
    [Ctrl] + [u]螢幕『向上』移動半頁
    +游標移動到非空白字元的下一列
    -游標移動到非空白字元的上一列
    n那個 n 表示『數字』,例如 20 。按下數字後再按空白鍵,游標會向右移動這一行的 n 個字元。例如 20 則游標會向後面移動 20 個字元距離。
    0 或功能鍵[Home]這是數字『 0 』:移動到這一行的最前面字元處 (常用)
    $ 或功能鍵[End]移動到這一行的最後面字元處(常用)
    H游標移動到這個螢幕的最上方那一行的第一個字元
    M游標移動到這個螢幕的中央那一行的第一個字元
    L游標移動到這個螢幕的最下方那一行的第一個字元
    G移動到這個檔案的最後一行(常用)
    nGn 為數字。移動到這個檔案的第 n 行。例如 20G 則會移動到這個檔案的第 20 行(可配合 :set nu)
    gg移動到這個檔案的第一行,相當於 1G 啊! (常用)
    nn 為數字。游標向下移動 n 行(常用)
    搜尋與取代
    /word向游標之下尋找一個名稱為 word 的字串。例如要在檔案內搜尋 vbird 這個字串,就輸入 /vbird 即可! (常用)
    ?word向游標之上尋找一個字串名稱為 word 的字串。
    n這個 n 是英文按鍵。代表『重複前一個搜尋的動作』。舉例來說, 如果剛剛我們執行 /vbird 去向下搜尋 vbird 這個字串,則按下 n 後,會向下繼續搜尋下一個名稱為 vbird 的字串。如果是執行 ?vbird 的話,那麼按下 n 則會向上繼續搜尋名稱為 vbird 的字串!
    N這個 N 是英文按鍵。與 n 剛好相反,為『反向』進行前一個搜尋動作。 例如 /vbird 後,按下 N 則表示『向上』搜尋 vbird 。
    使用 /word 配合 n 及 N 是非常有幫助的!可以讓你重複的找到一些你搜尋的關鍵字!
    :n1,n2s/word1/word2/gn1 與 n2 為數字。在第 n1 與 n2 行之間尋找 word1 這個字串,並將該字串取代為 word2 !舉例來說,在 100 到 200 行之間搜尋 vbird 並取代為 VBIRD 則:
    『:100,200s/vbird/VBIRD/g』。(常用)
    :1,$s/word1/word2/g從第一行到最後一行尋找 word1 字串,並將該字串取代為 word2 !(常用)
    :1,$s/word1/word2/gc從第一行到最後一行尋找 word1 字串,並將該字串取代為 word2 !且在取代前顯示提示字元給使用者確認 (confirm) 是否需要取代!(常用)
    刪除、複製與貼上
    x, X在一行字當中,x 為向後刪除一個字元 (相當於 [del] 按鍵), X 為向前刪除一個字元(相當於 [backspace] 亦即是倒退鍵) (常用)
    nxn 為數字,連續向後刪除 n 個字元。舉例來說,我要連續刪除 10 個字元, 『10x』。
    dd刪除游標所在的那一整列(常用)
    nddn 為數字。刪除游標所在的向下 n 列,例如 20dd 則是刪除 20 列 (常用)
    d1G刪除游標所在到第一行的所有資料
    dG刪除游標所在到最後一行的所有資料
    d$刪除游標所在處,到該行的最後一個字元
    d0那個是數字的 0 ,刪除游標所在處,到該行的最前面一個字元
    yy複製游標所在的那一行(常用)
    nyyn 為數字。複製游標所在的向下 n 列,例如 20yy 則是複製 20 列(常用)
    y1G複製游標所在列到第一列的所有資料
    yG複製游標所在列到最後一列的所有資料
    y0複製游標所在的那個字元到該行行首的所有資料
    y$複製游標所在的那個字元到該行行尾的所有資料
    p, Pp 為將已複製的資料在游標下一行貼上,P 則為貼在游標上一行! 舉例來說,我目前游標在第 20 行,且已經複製了 10 行資料。則按下 p 後, 那 10 行資料會貼在原本的 20 行之後,亦即由 21 行開始貼。但如果是按下 P 呢? 那麼原本的第 20 行會被推到變成 30 行。 (常用)
    J將游標所在列與下一列的資料結合成同一列
    c重複刪除多個資料,例如向下刪除 10 行,[ 10cj ]
    u復原前一個動作。(常用)
    [Ctrl]+r重做上一個動作。(常用)
    這個 u 與 [Ctrl]+r 是很常用的指令!一個是復原,另一個則是重做一次~ 利用這兩個功能按鍵,你的編輯,嘿嘿!很快樂的啦!
    .不要懷疑!這就是小數點!意思是重複前一個動作的意思。 如果你想要重複刪除、重複貼上等等動作,按下小數點『.』就好了! (常用)
  • 第二部份:一般模式切換到編輯模式的可用的按鈕說明

    進入插入或取代的編輯模式
    i, I進入插入模式(Insert mode):
    i 為『從目前游標所在處插入』, I 為『在目前所在行的第一個非空白字元處開始插入』。 (常用)
    a, A進入插入模式(Insert mode):
    a 為『從目前游標所在的下一個字元處開始插入』, A 為『從游標所在行的最後一個字元處開始插入』。(常用)
    o, O進入插入模式(Insert mode):
    這是英文字母 o 的大小寫。o 為『在目前游標所在的下一行處插入新的一行』; O 為在目前游標所在處的上一行插入新的一行!(常用)
    r, R進入取代模式(Replace mode):
    r 只會取代游標所在的那一個字元一次;R會一直取代游標所在的文字,直到按下 ESC 為止;(常用)
    上面這些按鍵中,在 vi 畫面的左下角處會出現『--INSERT--』或『--REPLACE--』的字樣。 由名稱就知道該動作了吧!!特別注意的是,我們上面也提過了,你想要在檔案裡面輸入字元時, 一定要在左下角處看到 INSERT 或 REPLACE 才能輸入喔!
    [Esc]退出編輯模式,回到一般模式中(常用)
  • 第三部份:一般模式切換到指令列模式的可用的按鈕說明

    指令列的儲存、離開等指令
    :w將編輯的資料寫入硬碟檔案中(常用)
    :w!若檔案屬性為『唯讀』時,強制寫入該檔案。不過,到底能不能寫入, 還是跟你對該檔案的檔案權限有關啊!
    :q離開 vi (常用)
    :q!若曾修改過檔案,又不想儲存,使用 ! 為強制離開不儲存檔案。
    注意一下啊,那個驚嘆號 (!) 在 vi 當中,常常具有『強制』的意思~
    :wq儲存後離開,若為 :wq! 則為強制儲存後離開 (常用)
    ZZ這是大寫的 Z 喔!若檔案沒有更動,則不儲存離開,若檔案已經被更動過,則儲存後離開!
    :w [filename]將編輯的資料儲存成另一個檔案(類似另存新檔)
    :r [filename]在編輯的資料中,讀入另一個檔案的資料。亦即將 『filename』 這個檔案內容加到游標所在行後面
    :n1,n2 w [filename]將 n1 到 n2 的內容儲存成 filename 這個檔案。
    :! command暫時離開 vi 到指令列模式下執行 command 的顯示結果!例如
    『:! ls /home』即可在 vi 當中察看 /home 底下以 ls 輸出的檔案資訊!
    vim 環境的變更
    :set nu顯示行號,設定之後,會在每一行的字首顯示該行的行號
    :set nonu與 set nu 相反,為取消行號!

鳥哥私房菜

JDateField
package bruce.lib.swing;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bruce.lib.swing.JDatePickPane.PickDate;

/**
 * @author Bruce (NaNashi)
 * 
 */
public class JDateField extends JPanel {

    private JButton btnPopup;
    private JTextField txtInput;
    private Calendar c;

    private String format;
    private String[] dayDisplayNames;
    private Locale locale;
    private Icon leftArrow;
    private Icon rightArrow;
    private boolean isCloseOnClick;

    public JDateField() {
        super(new GridBagLayout());

        this.locale = Locale.getDefault();
        this.format = JDatePickPane.DEFAULT_DATE_FORMAT;

        this.leftArrow = JDatePickPane.getImageIcon("/bruce/lib/swing/img/Arrow.blue.left.png", 20, 20);
        this.rightArrow = JDatePickPane.getImageIcon("/bruce/lib/swing/img/Arrow.blue.right.png", 20, 20);

        this.txtInput = new JTextField();
        this.txtInput.setEditable(false);
        this.add(this.txtInput, new GridBagConstraints(0, 0, 1, 1, 1, 0, 10, 2, new Insets(1, 1, 1, 1), 0, 0));

        try {
            btnPopup = new JButton(new ImageIcon(this.getClass().getResource("/bruce/lib/swing/img/calendar-date-icon.png")));
            btnPopup.setBorder(BorderFactory.createEmptyBorder());
            btnPopup.setBorderPainted(false);
        } catch (Exception e) {
            btnPopup = new JButton("P");
        }
        btnPopup.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                popup();
            }
        });
        this.add(btnPopup, new GridBagConstraints(1, 0, 1, 1, 0, 0, 10, 2, new Insets(1, 1, 1, 1), 0, 0));
    }

    public JDateField(int columns) {
        this();
        this.txtInput.setColumns(columns);
    }

    public JDateField(Calendar c) {
        this();
        this.setSelectedDateText(c);
    }

    public JDateField(Calendar c, int columns) {
        this(c);
        this.txtInput.setColumns(columns);
    }

    private void popup() {
        JDatePickDialog dialog = new JDatePickDialog(this.txtInput);
        dialog.setTodayFormat(this.format);
        dialog.setDayName(this.dayDisplayNames);
        dialog.setUILocale(this.locale);
        dialog.setLeftArrow(this.leftArrow);
        dialog.setRightArrow(this.rightArrow);

        dialog.setDateSelected(new PickDate() {
            @Override
            public void pick(Calendar c) {
                setSelectedDateText(c);
            }
        });

        try {
            dialog.view(txtInput.getText().trim());
        } catch (Exception ex) {
            dialog.view();
        }
    }

    public void setFormat(String format) {
        this.format = format;
        this.setSelectedDateText(this.c);
    }

    public String getFormat() {
        return this.format == null ? JDatePickPane.DEFAULT_DATE_FORMAT : this.format;
    }

    public String[] getDayDisplayNames() {
        return dayDisplayNames;
    }

    public void setDayDisplayNames(String[] dayDisplayNames) {
        this.dayDisplayNames = dayDisplayNames;
    }

    public Locale getUILocale() {
        return locale;
    }

    public void setUILocale(Locale locale) {
        this.locale = locale;
    }

    public Icon getLeftArrow() {
        return leftArrow;
    }

    public void setLeftArrow(Icon leftArrow) {
        this.leftArrow = leftArrow;
    }

    public Icon getRightArrow() {
        return rightArrow;
    }

    public void setRightArrow(Icon rightArrow) {
        this.rightArrow = rightArrow;
    }

    public void setCloseOnClick(boolean isCloseOnClick) {
        this.isCloseOnClick = isCloseOnClick;
    }

    public boolean isCloseOnClick() {
        return isCloseOnClick;
    }

    public int getColumns() {
        return this.txtInput.getColumns();
    }

    public void setColumns(int columns) {
        this.txtInput.setColumns(columns);
    }

    public void setButtonVisible(boolean b) {
        this.btnPopup.setVisible(b);
        if (b) {
            MouseListener[] ls = this.txtInput.getMouseListeners();
            for (MouseListener l : ls) {
                this.txtInput.removeMouseListener(l);
            }
        } else {
            this.txtInput.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    popup();
                }
            });
        }
    }

    public void setSelectedDateText(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        this.setSelectedDateText(c);
    }

    public void setSelectedDateText(Calendar c) {
        this.c = c;
        if (this.format == null) {
            this.txtInput.setText(String.format("%tF", c));
        } else {
            try {
                SimpleDateFormat f = new SimpleDateFormat(this.format);
                this.txtInput.setText(f.format(c.getTime()));
            } catch (Exception ex) {
                this.txtInput.setText(String.format("%tF", c));
            }
        }
    }

    public Calendar getSelectedCalender() {
        return this.c == null ? Calendar.getInstance() : this.c;
    }

}
package bruce.lib.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JDialog;

import bruce.lib.swing.JDatePickPane.PickDate;

/**
 * @author Bruce (NaNashi)
 * 
 */
public class JDatePickDialog extends JDialog {

    private boolean isCloseOnClick;

    public JDatePickDialog() {
        this.init();

        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(new Point((size.width - this.getWidth()) / 2, (size.height - this.getHeight()) / 2));
    }

    public JDatePickDialog(Component c) {
        this.setUndecorated(true);
        this.init();

        this.getContent().setBorder(BorderFactory.createEtchedBorder());

        this.addWindowFocusListener(new WindowFocusListener() {
            @Override
            public void windowLostFocus(WindowEvent e) {
                setVisible(false);
                dispose();
            }

            @Override
            public void windowGainedFocus(WindowEvent e) {
                // TODO Auto-generated method stub

            }
        });

        if (c != null) {
            Point p = c.getLocationOnScreen();
            this.setLocation(new Point(p.x, p.y + c.getHeight()));
        } else {
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((int) (dim.getWidth() - this.getWidth()) / 2, (int) (dim.getHeight() - this.getHeight()) / 2);
        }
    }

    private void init() {
        this.isCloseOnClick = true;

        this.setContentPane(new JDatePickPane());

        this.setDateSelected(new PickDate() {
            @Override
            public void pick(Calendar c) {
                // 預設值
            }
        });

        this.pack();
    }

    private JDatePickPane getContent() {
        return (JDatePickPane) this.getContentPane();
    }

    public void setCloseOnClick(boolean isCloseOnClick) {
        this.isCloseOnClick = isCloseOnClick;
    }

    public boolean isCloseOnClick() {
        return isCloseOnClick;
    }

    // ========== panel method ================
    /**
     * 選取日期的觸發動作
     * 
     * @param pick
     */
    public void setDateSelected(final PickDate pick) {
        PickDate pd = new PickDate() {
            @Override
            public void pick(Calendar c) {
                pick.pick(c);
                if (isCloseOnClick) {
                    setVisible(false);
                    dispose();
                }
            }
        };
        this.getContent().setDateSelected(pd);
    }

    /**
     * 目前選取日期
     * 
     * @return
     */
    public Calendar getSelectedCalendar() {
        return this.getContent().getSelectedCalendar();
    }

    /**
     * 設定選取的日期
     * 
     * @param c
     */
    public void setSelectedCalendar(Calendar c) {
        this.getContent().setSelectedCalendar(c);
    }

    /**
     * 取得星期的顯示名稱
     * 
     * @return
     */
    public String[] getDayNames() {
        return this.getContent().getDayNames();
    }

    /**
     * 設定星期的顯示名稱
     * 
     * @param names
     */
    public void setDayName(String[] names) {
        this.getContent().setDayName(names);
    }

    /**
     * 設定今天的顯示格式
     * 
     * @param format
     */
    public void setTodayFormat(String format) {
        this.getContent().setTodayFormat(format);
    }

    public String getTodayFormat() {
        return this.getContent().getTodayFormat();
    }

    public void setUILocale(Locale l) {
        this.getContent().setUILocale(l);
    }

    public Locale getUILocale() {
        return this.getContent().getUILocale();
    }

    public void setLeftArrow(Icon icon) {
        this.getContent().setLeftArrow(icon);
    }

    public void setRightArrow(Icon icon) {
        this.getContent().setRightArrow(icon);
    }

    /**
     * 顯示選擇的日期
     * 
     * @param c
     */
    public void view(Calendar c) {
        this.getContent().view(c);
        this.setVisible(true);
    }

    public void view(String date) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(this.getContent().getTodayFormat());
        Calendar c = Calendar.getInstance();
        c.setTime(f.parse(date));
        this.getContent().setSelectedCalendar(c);
        this.view(c);
    }

    /**
     * 顯示今天的日期
     */
    public void view() {
        this.getContent().view();
        this.setVisible(true);
    }
}
package bruce.lib.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * @author Bruce (NaNashi)
 * 
 */
public class JDatePickPane extends JPanel {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    public static Icon getImageIcon(String location, double width, double height) {
        try {
            BufferedImage icon = ImageIO.read(String.class.getResource(location));
            // resize image
            AffineTransform transform = AffineTransform.getScaleInstance(width / icon.getWidth(), height / icon.getHeight());
            AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
            return new ImageIcon(op.filter(icon, null));
        } catch (Exception e) {
            return null;
        }
    }

    public interface PickDate {
        void pick(Calendar c);
    }

    class DayCell extends JButton {
        private long stamp;

        DayCell(Calendar c, Calendar currentMonth, Calendar selected) {
            super(String.valueOf(c.get(Calendar.DATE)));

            stopWatch("Day Cell");

            this.stamp = c.getTimeInMillis();
            Calendar now = Calendar.getInstance();
            if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
                this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                this.setForeground(Color.BLUE);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));
            } else if (c.get(Calendar.YEAR) != currentMonth.get(Calendar.YEAR) || c.get(Calendar.MONTH) != currentMonth.get(Calendar.MONTH)) {
                this.setForeground(Color.GRAY);
                this.setBorder(BorderFactory.createEtchedBorder());
            } else {
                this.setBorder(BorderFactory.createEtchedBorder());
            }
            if (c.get(Calendar.YEAR) == selected.get(Calendar.YEAR) && c.get(Calendar.MONTH) == selected.get(Calendar.MONTH) && c.get(Calendar.DATE) == selected.get(Calendar.DATE)) {
                this.setBorder(BorderFactory.createLineBorder(Color.RED));
                this.setForeground(Color.RED);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));
            }

            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseExited(MouseEvent e) {
                    setBackground(null);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    setBackground(Color.white);
                }
            });
            this.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JDatePickPane pick = (JDatePickPane) getParent().getParent();
                    pick.selectedDate.setTimeInMillis(stamp);

                    pick.view(pick.selectedDate);

                    if (pick.pickAtion != null) {
                        pick.pickAtion.pick(pick.selectedDate);
                    }
                }
            });
        }
    }

    class MonthCell extends JButton {
        private long stamp;

        public MonthCell(Calendar c, Calendar selected) {
            super(c.getDisplayName(Calendar.MONTH, Calendar.SHORT, loc));

            stopWatch("Month Cell");

            this.stamp = c.getTimeInMillis();
            this.setBorder(BorderFactory.createEtchedBorder());
            this.setPreferredSize(new Dimension(50, 50));

            Calendar now = Calendar.getInstance();
            if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
                this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                this.setForeground(Color.BLUE);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));

            } else {
                this.setBorder(BorderFactory.createEtchedBorder());
            }
            if (c.get(Calendar.YEAR) == selected.get(Calendar.YEAR) && c.get(Calendar.MONTH) == selected.get(Calendar.MONTH)) {
                this.setBorder(BorderFactory.createLineBorder(Color.RED));
                this.setForeground(Color.RED);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));
            }

            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseExited(MouseEvent e) {
                    setBackground(null);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    setBackground(Color.white);
                }
            });
            this.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(stamp);

                    JDatePickPane pick = (JDatePickPane) getParent().getParent();
                    pick.view(c);
                }
            });
        }
    }

    class YearCell extends JButton {
        private long stamp;

        public YearCell(Calendar c, Calendar selected) {
            super(String.valueOf(c.get(Calendar.YEAR)));

            stopWatch("Year Cell");

            this.stamp = c.getTimeInMillis();
            this.setBorder(BorderFactory.createEtchedBorder());
            this.setPreferredSize(new Dimension(50, 50));

            Calendar now = Calendar.getInstance();
            if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
                this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                this.setForeground(Color.BLUE);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));

            } else {
                this.setBorder(BorderFactory.createEtchedBorder());
            }
            if (c.get(Calendar.YEAR) == selected.get(Calendar.YEAR)) {
                this.setBorder(BorderFactory.createLineBorder(Color.RED));
                this.setForeground(Color.RED);
                this.setFont(this.getFont().deriveFont(Font.BOLD, this.getFont().getSize()));
            }

            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseExited(MouseEvent e) {
                    setBackground(null);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    setBackground(Color.white);
                }
            });
            this.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(stamp);

                    JDatePickPane pick = (JDatePickPane) getParent().getParent();
                    pick.viewMonth(c);
                }
            });
        }
    }

    private String format;

    private Calendar selectedDate;

    private PickDate pickAtion;

    private String[] dayDisplayNames;

    private Locale loc;

    private Icon leftArrow;
    private Icon rightArrow;

    public JDatePickPane() {
        this.selectedDate = Calendar.getInstance();
        this.setLayout(new BorderLayout());
        this.selectedDate = Calendar.getInstance();
        this.loc = Locale.getDefault();
        this.format = JDatePickPane.DEFAULT_DATE_FORMAT;

        this.leftArrow = getImageIcon("/bruce/lib/swing/img/Arrow.blue.left.png", 20, 20);
        this.rightArrow = getImageIcon("/bruce/lib/swing/img/Arrow.blue.right.png", 20, 20);

        this.view();
    }

    private void putToday() {
        stopWatch("Today Bar");

        JPanel panel = new JPanel(new FlowLayout());
        String today = String.format("%tF", Calendar.getInstance());
        try {
            if (this.format != null) {
                SimpleDateFormat f = new SimpleDateFormat(this.format);
                today = f.format(new Date());
            }
        } catch (Exception e) {
        }
        final JLabel lbToday = new JLabel(today);
        lbToday.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                lbToday.setFont(lbToday.getFont().deriveFont(Font.BOLD, lbToday.getFont().getSize()));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                lbToday.setFont(lbToday.getFont().deriveFont(Font.PLAIN, lbToday.getFont().getSize()));
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                selectedDate = Calendar.getInstance();
                view(selectedDate);

                if (pickAtion != null) {
                    pickAtion.pick(selectedDate);
                }
            }
        });
        panel.add(lbToday);
        this.add(panel, BorderLayout.SOUTH);
    }

    private void setYearBarText(JLabel label, Calendar c) {
        label.setText(String.format("%d - %d", //
                c.get(Calendar.YEAR) - c.get(Calendar.YEAR) % 12 + 1,//
                c.get(Calendar.YEAR) - c.get(Calendar.YEAR) % 12 + 12));
    }

    private void putYearBar(final Calendar c) {
        stopWatch("Year Bar");

        JPanel panel = new JPanel(new BorderLayout());

        final JLabel lbPeriod = new JLabel();
        lbPeriod.setHorizontalAlignment(SwingConstants.CENTER);
        this.setYearBarText(lbPeriod, c);
        panel.add(lbPeriod, BorderLayout.CENTER);

        JButton btnLeft = this.leftArrow == null ? new JButton("<") : new JButton(this.leftArrow);
        btnLeft.setBorderPainted(false);
        btnLeft.setBorder(BorderFactory.createEmptyBorder());
        btnLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.YEAR, -12);

                setYearBarText(lbPeriod, c);
                viewYear(s);
            }
        });
        panel.add(btnLeft, BorderLayout.WEST);

        JButton btnRight = this.rightArrow == null ? new JButton(">") : new JButton(this.rightArrow);
        btnRight.setBorderPainted(false);
        btnRight.setBorder(BorderFactory.createEmptyBorder());
        btnRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.YEAR, 12);

                setYearBarText(lbPeriod, c);
                viewYear(s);
            }
        });
        panel.add(btnRight, BorderLayout.EAST);

        this.add(panel, BorderLayout.NORTH);
    }

    private void setMonthBarText(JButton label, Calendar c) {
        label.setText(String.valueOf(c.get(Calendar.YEAR)));
    }

    private void putMonthBar(final Calendar c) {
        stopWatch("Month Bar");

        JPanel panel = new JPanel(new BorderLayout());

        final JButton btnPeriod = new JButton();
        btnPeriod.setHorizontalAlignment(SwingConstants.CENTER);
        btnPeriod.setBorder(BorderFactory.createEmptyBorder());
        btnPeriod.setBorderPainted(false);

        this.setMonthBarText(btnPeriod, c);
        btnPeriod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewYear(c);
            }
        });
        btnPeriod.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                btnPeriod.setFont(btnPeriod.getFont().deriveFont(Font.BOLD, btnPeriod.getFont().getSize()));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                btnPeriod.setFont(btnPeriod.getFont().deriveFont(Font.PLAIN, btnPeriod.getFont().getSize()));
            }
        });
        panel.add(btnPeriod, BorderLayout.CENTER);

        JButton btnLeft = this.leftArrow == null ? new JButton("<") : new JButton(this.leftArrow);
        btnLeft.setBorderPainted(false);
        btnLeft.setBorder(BorderFactory.createEmptyBorder());
        btnLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.YEAR, -1);

                setMonthBarText(btnPeriod, c);
                viewMonth(s);
            }
        });
        panel.add(btnLeft, BorderLayout.WEST);

        JButton btnRight = this.rightArrow == null ? new JButton(">") : new JButton(this.rightArrow);
        btnRight.setBorderPainted(false);
        btnRight.setBorder(BorderFactory.createEmptyBorder());
        btnRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.YEAR, 1);
                setMonthBarText(btnPeriod, c);

                viewMonth(s);
            }
        });
        panel.add(btnRight, BorderLayout.EAST);

        this.add(panel, BorderLayout.NORTH);
    }

    private void setDateBarText(JButton label, Calendar c) {
        label.setText(String.format("%s, %d", //
                c.getDisplayName(Calendar.MONTH, Calendar.LONG, loc),//
                c.get(Calendar.YEAR)));
    }

    private void putDateBar(final Calendar c) {
        stopWatch("Date Bar");

        JPanel panel = new JPanel(new BorderLayout());

        final JButton btnPeriod = new JButton();
        btnPeriod.setHorizontalAlignment(SwingConstants.CENTER);
        btnPeriod.setBorder(BorderFactory.createEmptyBorder());
        btnPeriod.setBorderPainted(false);

        this.setDateBarText(btnPeriod, c);
        btnPeriod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewMonth(c);
            }
        });
        btnPeriod.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                btnPeriod.setFont(btnPeriod.getFont().deriveFont(Font.BOLD, btnPeriod.getFont().getSize()));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                btnPeriod.setFont(btnPeriod.getFont().deriveFont(Font.PLAIN, btnPeriod.getFont().getSize()));
            }
        });
        panel.add(btnPeriod, BorderLayout.CENTER);

        JButton btnLeft = this.leftArrow == null ? new JButton("<") : new JButton(this.leftArrow);
        btnLeft.setBorderPainted(false);
        btnLeft.setBorder(BorderFactory.createEmptyBorder());
        btnLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.MONTH, -1);

                view(s);

                setDateBarText(btnPeriod, c);
            }
        });
        panel.add(btnLeft, BorderLayout.WEST);

        JButton btnRight = this.rightArrow == null ? new JButton(">") : new JButton(this.rightArrow);
        btnRight.setBorderPainted(false);
        btnRight.setBorder(BorderFactory.createEmptyBorder());
        btnRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar s = (Calendar) c.clone();
                s.add(Calendar.MONTH, 1);

                view(s);

                setDateBarText(btnPeriod, c);
            }
        });
        panel.add(btnRight, BorderLayout.EAST);

        this.add(panel, BorderLayout.NORTH);
    }

    private void putYearContent(Calendar c) {
        stopWatch("Year Bar");

        JPanel panel = new JPanel(new GridLayout(3, 4));

        Calendar s = (Calendar) c.clone();
        s.set(Calendar.YEAR, s.get(Calendar.YEAR) - s.get(Calendar.YEAR) % 12);
        for (int i = 0; i < 12; i++) {
            s.add(Calendar.YEAR, 1);
            panel.add(new YearCell(s, this.selectedDate));
        }

        this.add(panel, BorderLayout.CENTER);
    }

    private void putMonthContent(Calendar c) {
        stopWatch("Month Bar");

        JPanel panel = new JPanel(new GridLayout(3, 4));

        Calendar s = (Calendar) c.clone();
        for (int i = 0; i < 12; i++) {
            s.set(Calendar.MONTH, i);
            panel.add(new MonthCell(s, this.selectedDate));
        }

        this.add(panel, BorderLayout.CENTER);
    }

    private void putDateContent(Calendar c) {
        stopWatch("Date Content");

        JPanel panel = new JPanel(new GridBagLayout());
        Insets insets = new Insets(1, 1, 1, 1);
        this.putHeaders(panel, insets);

        int rows = this.getMonthRowsCount(c);
        Calendar d = this.getFirstDate(c);

        for (int y = 1; y < rows + 1; y++) {
            for (int x = 1; x < 8; x++) {
                panel.add(new DayCell(d, c, this.selectedDate), new GridBagConstraints(x, y, 1, 1, 0, 0, 10, 1, insets, 10, 0));
                d.add(Calendar.DATE, 1);
            }
        }

        this.add(panel, BorderLayout.CENTER);
    }

    private void viewYear(Calendar c) {
        this.clear();
        this.putYearContent(c);
        this.putYearBar(c);
        this.pack();
    }

    private void viewMonth(Calendar c) {
        this.clear();
        this.putMonthContent(c);
        this.putMonthBar(c);

        this.pack();
    }

    /**
     * 處理星期顯示格
     * 
     * @param panel
     * @param insets
     */
    private void putHeaders(JPanel panel, Insets insets) {
        if (this.dayDisplayNames == null) {
            Calendar s = Calendar.getInstance();
            s.set(Calendar.DAY_OF_WEEK, 1);

            Color c = null;
            for (int i = 0; i < 7; i++) {
                // 星期表示法
                String h = s.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, loc);
                s.add(Calendar.DATE, 1);
                if (loc == Locale.TAIWAN || loc == Locale.CHINA || loc == Locale.CHINESE || loc == Locale.SIMPLIFIED_CHINESE || loc == Locale.TRADITIONAL_CHINESE) {
                    h = h.substring(h.length() - 1);
                } else if (loc == Locale.JAPAN || loc == Locale.JAPANESE) {
                    h = h.substring(0, 1);
                } else {
                    h = h.substring(0, 3);
                }

                switch (i) {
                case 0:
                    c = Color.red;
                    break;
                case 6:
                    c = Color.BLUE;
                    break;
                default:
                    c = Color.BLACK;
                    break;
                }
                panel.add(this.createHeader(h, c), new GridBagConstraints(i + 1, 0, 1, 1, 0, 0, 10, 1, insets, 10, 0));
            }
        } else {
            String[] headers = Arrays.copyOf(this.dayDisplayNames, 7);
            Color c = null;
            for (int i = 0; i < 7; i++) {
                switch (i) {
                case 0:
                    c = Color.red;
                    break;
                case 6:
                    c = Color.BLUE;
                    break;
                default:
                    c = Color.BLACK;
                    break;
                }
                panel.add(this.createHeader(headers[i], c), new GridBagConstraints(i + 1, 0, 1, 1, 0, 0, 10, 1, insets, 10, 0));
            }
        }
    }

    /**
     * 星期
     * 
     * @param text
     * @param fontColor
     * @return
     */
    private JLabel createHeader(String text, Color fontColor) {
        JLabel l = new JLabel(text, SwingConstants.CENTER);
        l.setBorder(BorderFactory.createEtchedBorder());
        l.setOpaque(true);
        l.setBackground(Color.WHITE);
        l.setForeground(fontColor);
        return l;
    }

    /**
     * 計算月份的列數
     * 
     * @param c
     * @return
     */
    private int getMonthRowsCount(Calendar c) {
        Calendar s = (Calendar) c.clone();
        s.set(Calendar.DATE, 1);
        return (int) Math.ceil((s.get(Calendar.DAY_OF_WEEK) + s.getActualMaximum(Calendar.DAY_OF_MONTH) - 1) / 7.0);
    }

    /**
     * 計算顯示的第一個日期
     * 
     * @param c
     * @return
     */
    private Calendar getFirstDate(Calendar c) {
        Calendar s = (Calendar) c.clone();
        s.set(Calendar.DATE, 1);
        s.add(Calendar.DATE, -1 * (s.get(Calendar.DAY_OF_WEEK) - 1));
        return s;
    }

    /**
     * 清除
     */
    private void clear() {
        stopWatch("Clear");

        for (int i = this.getComponentCount() - 1; i > -1; i--) {
            this.remove(i);
        }

        this.putToday();
        System.gc();
    }

    /**
     * 重整大小
     */
    private void pack() {
        stopWatch("Pack");

        this.revalidate();
        // resize
        Container cc = this.getParent();
        while (cc != null) {
            if (cc instanceof Window) {
                ((Window) cc).pack();
                break;
            }
            cc = cc.getParent();
        }
    }

    /**
     * 測試時間
     * 
     * @param what
     */
    private void stopWatch(String what) {
        // test time execute
        // System.out.printf("%2$s : %1$tY/%1$tm/%1$td %1$tH:%1$tM:%1$tS %1$tL%n",
        // new Date(), what);
    }

    /**
     * 選取日期的觸發動作
     * 
     * @param pick
     */
    public void setDateSelected(PickDate pick) {
        this.pickAtion = pick;
    }

    /**
     * 目前選取日期
     * 
     * @return
     */
    public Calendar getSelectedCalendar() {
        return (Calendar) this.selectedDate.clone();
    }

    /**
     * 設定選取的日期
     * 
     * @param c
     */
    public void setSelectedCalendar(Calendar c) {
        this.selectedDate = c;
    }

    /**
     * 取得星期的顯示名稱
     * 
     * @return
     */
    public String[] getDayNames() {
        return this.dayDisplayNames;
    }

    /**
     * 設定星期的顯示名稱
     * 
     * @param names
     */
    public void setDayName(String[] names) {
        this.dayDisplayNames = names;
        this.view(this.selectedDate);
    }

    /**
     * 設定今天的顯示格式
     * 
     * @param format
     */
    public void setTodayFormat(String format) {
        this.format = format;
        this.putToday();
    }

    public String getTodayFormat() {
        return this.format;
    }

    public void setUILocale(Locale l) {
        this.loc = l;
        this.view(this.selectedDate);
    }

    public Locale getUILocale() {
        return this.loc;
    }

    public void setLeftArrow(Icon icon) {
        this.leftArrow = icon;
        this.view(this.selectedDate);
    }

    public void setRightArrow(Icon icon) {
        this.rightArrow = icon;
        this.view(this.selectedDate);
    }

    /**
     * 顯示選擇的日期
     * 
     * @param c
     */
    public void view(Calendar c) {
        // claer
        this.clear();

        this.putDateBar(c);
        this.putDateContent(c);

        this.pack();
    }

    /**
     * 顯示今天的日期
     */
    public void view() {
        this.view(Calendar.getInstance());
    }
}