Java設(shè)計(jì)模式之修飾模式篇(3)

字號(hào):

//Test.java
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Locale;
    import java.util.ResourceBundle;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Test extends JFrame {
    public static void main(String args[]) {
    SwingApp.launch(new Test(), "排序修飾者",
    300, 300, 450, 250);
    }
    public Test() {
    // 生成修飾者的實(shí)例,該實(shí)例用于修飾Swing Table原有的表模型
    // 該實(shí)例必須是final的,因?yàn)樗鼤?huì)被內(nèi)嵌類(lèi)引用。
    final TableSortDecorator decorator =
    new TableBubbleSortDecorator(table.getModel());
    // 將表的模型設(shè)定為修飾者。因?yàn)樾揎椪邔?shí)現(xiàn)了TableModel接口,
    // 因此Swing Table對(duì)象不知道修飾者和真實(shí)對(duì)象之間的差別。
    table.setModel(decorator);
    getContentPane().add(new JScrollPane(table),
    BorderLayout.CENTER);
    // 在界面中添加一個(gè)狀態(tài)區(qū)
    getContentPane().add(SwingApp.getStatusArea(),
    BorderLayout.SOUTH);
    SwingApp.showStatus("進(jìn)行排序前");
    // 獲得對(duì)表中列頭的引用。
    JTableHeader hdr = (JTableHeader)table.getTableHeader();
    // 當(dāng)單擊鼠標(biāo)單擊列頭時(shí),調(diào)用修飾者的sort()方法。
    hdr.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    TableColumnModel tcm = table.getColumnModel();
    int vc = tcm.getColumnIndexAtX(e.getX());
    int mc = table.convertColumnIndexToModel(vc);
    // 進(jìn)行排序
    decorator.sort(mc);
    // 更新?tīng)顟B(tài)區(qū)
    SwingApp.showStatus(headers[mc] + " 排序中");
    }
    });
    }
    final String[] headers = { "品名", "價(jià)格/每斤." };
    JTable table = new JTable(new Object[][] {
    {"蘋(píng)果", "1.2"}, {"芒果", "4"},
    {"檸檬", "2.5"},{"香蕉", "0.8"},
    {"桔子", "1.8"}, {"西瓜", "0.5"},
    {"橘子", "2.5"}, {"櫻桃", "3.6"},
    {"柚子", "0.8"}, {"葡萄", "2.2"},
    }, headers);
    }
    class SwingApp extends WindowAdapter {
    private SwingApp() {} // 該類(lèi)不能被初始化
    public static void launch(final JFrame f, String title,
    final int x, final int y,
    final int w, int h) {
    launch(f,title,x,y,w,h,null);
    }
    public static void launch(final JFrame f, String title,
    final int x, final int y,
    final int w, int h,
    String propertiesFilename) {
    statusArea.setBorder(BorderFactory.createEtchedBorder());
    statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
    statusArea.add(status);
    status.setHorizontalAlignment(JLabel.LEFT);
    if(propertiesFilename != null) {
    resources = ResourceBundle.getBundle(
    propertiesFilename, Locale.getDefault());
    }
    f.setTitle(title);
    f.setBounds(x,y,w,h);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    static public JPanel getStatusArea() {
    return statusArea;
    }
    static public void showStatus(String s) {
    status.setText(s);
    }
    static Object getResource(String key) {
    if(resources != null) {
    return resources.getString(key);
    }
    return null;
    }
    static private JPanel statusArea = new JPanel();
    static private JLabel status = new JLabel(" ");
    static private ResourceBundle resources;
    }