OSDN Git Service

update for Java SE 6.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / ExtractedHistoryPane.java
1 package jp.sourceforge.stigmata.ui.swing;
2
3 /*
4  * $Id$
5  */
6
7 import java.awt.BorderLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
12
13 import javax.swing.AbstractAction;
14 import javax.swing.Action;
15 import javax.swing.Box;
16 import javax.swing.DefaultListModel;
17 import javax.swing.JButton;
18 import javax.swing.JComboBox;
19 import javax.swing.JList;
20 import javax.swing.JPanel;
21 import javax.swing.JPopupMenu;
22 import javax.swing.JScrollPane;
23 import javax.swing.ListSelectionModel;
24 import javax.swing.event.ListSelectionEvent;
25 import javax.swing.event.ListSelectionListener;
26
27 import jp.sourceforge.stigmata.ExtractionResultSet;
28 import jp.sourceforge.stigmata.result.history.ExtractedBirthmarkHistory;
29 import jp.sourceforge.stigmata.result.history.ExtractedBirthmarkServiceManager;
30 import jp.sourceforge.stigmata.ui.swing.actions.PopupShowAction;
31 import jp.sourceforge.talisman.i18n.Messages;
32
33 /**
34  * Birthmark extraction history viewer.
35  * 
36  * @author Haruaki Tamada
37  * @version $Revision$ $Date$
38  */
39 public class ExtractedHistoryPane extends JPanel{
40     private static final long serialVersionUID = 4070750464486981964L;
41
42     private StigmataFrame stigmata;
43     private JComboBox combo;
44     private JList list;
45     private DefaultListModel model;
46     private ExtractedBirthmarkServiceManager historyManager;
47     private ExtractedBirthmarkHistory currentHistory;
48
49     public ExtractedHistoryPane(StigmataFrame stigmata){
50         this.stigmata = stigmata;
51
52         initLayouts();
53         initData();
54     }
55
56     private void updateList(){
57         String historyId = (String)combo.getSelectedItem();
58         currentHistory = historyManager.getHistory(historyId);
59         model.clear();
60
61         for(String id: currentHistory){
62             model.addElement(id);
63         }
64     }
65
66     private void initData(){
67         historyManager = new ExtractedBirthmarkServiceManager(stigmata.getEnvironment());
68
69         for(String id: historyManager.getHistoryIds()){
70             combo.addItem(id);
71         }
72     }
73
74     private void showAction(String id){
75         ExtractionResultSet ers = currentHistory.getResultSet(id);
76         stigmata.showExtractionResult(ers);
77     }
78
79     private void initLayouts(){
80         final Messages messages = stigmata.getMessages();
81         setLayout(new BorderLayout());
82
83         final Action showAction = new AbstractAction(){
84             private static final long serialVersionUID = 2156350514762218963L;
85
86             public void actionPerformed(ActionEvent e){
87                 showAction((String)model.get(list.getSelectedIndex()));
88             }
89         };
90         final Action refreshAction = new AbstractAction(){
91             private static final long serialVersionUID = 214765021455345371L;
92
93             public void actionPerformed(ActionEvent e){
94                 updateList();
95             }
96         };
97         final Action deleteAction = new AbstractAction(){
98             private static final long serialVersionUID = 8145188292702648924L;
99
100             public void actionPerformed(ActionEvent e){
101                 int[] indeces = list.getSelectedIndices();
102                 for(int i = indeces.length - 1; i >= 0; i--){
103                     String id = (String)model.get(indeces[i]);
104                     currentHistory.deleteResultSet(id);
105                     model.remove(indeces[i]);
106                 }
107                 list.clearSelection();
108             }
109         };
110         model = new DefaultListModel();
111         list = new JList(model);
112         combo = new JComboBox();
113         list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
114         GUIUtility.decorateJComponent(messages, list, "historylist");
115         GUIUtility.decorateJComponent(messages, combo, "historylocation");
116
117         JButton showButton = GUIUtility.createButton(messages, "showhistory", showAction);
118         JButton refreshButton = GUIUtility.createButton(messages, "refreshhistory", refreshAction);
119         JButton deleteButton = GUIUtility.createButton(messages, "deletehistory", deleteAction);
120         deleteAction.setEnabled(false);
121         showAction.setEnabled(false);
122
123         list.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
124             public void valueChanged(ListSelectionEvent e){
125                 int[] indeces = list.getSelectedIndices();
126                 showAction.setEnabled(currentHistory != null && indeces.length == 1);
127                 deleteAction.setEnabled(currentHistory != null && indeces.length > 0);
128             }
129         });
130         list.addMouseListener(new MouseAdapter(){
131             @Override
132             public void mouseClicked(MouseEvent e){
133                 int index = list.locationToIndex(e.getPoint());
134                 if(index >= 0 && e.getClickCount() == 2){
135                     showAction((String)model.get(index));
136                 }
137             }
138         });
139         combo.addActionListener(new ActionListener(){
140             public void actionPerformed(ActionEvent e){
141                 updateList();
142             }
143         });
144         JScrollPane scroll = new JScrollPane();
145         scroll.setViewportView(list);
146
147         Box south = Box.createHorizontalBox();
148         south.add(Box.createHorizontalGlue());
149         south.add(showButton);
150         south.add(Box.createHorizontalGlue());
151         south.add(refreshButton);
152         south.add(Box.createHorizontalGlue());
153         south.add(deleteButton);
154         south.add(Box.createHorizontalGlue());
155
156         add(combo, BorderLayout.NORTH);
157         add(scroll, BorderLayout.CENTER);
158         add(south, BorderLayout.SOUTH);
159
160         JPopupMenu popup = new JPopupMenu();
161         popup.add(GUIUtility.createJMenuItem(messages, "showhistory"), showAction);
162         popup.add(GUIUtility.createJMenuItem(messages, "refreshhistory"), refreshAction);
163         popup.add(GUIUtility.createJMenuItem(messages, "deletehistory"), deleteAction);
164         list.addMouseListener(new PopupShowAction(popup));
165     }
166 }