OSDN Git Service

remove Revision tag
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / PropertyEditPane.java
1 package jp.sourceforge.stigmata.ui.swing;
2
3 /*
4  * $Id$
5  */
6
7 import java.awt.BorderLayout;
8 import java.awt.GridBagConstraints;
9 import java.awt.GridBagLayout;
10 import java.awt.Insets;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.MouseAdapter;
13 import java.awt.event.MouseEvent;
14 import java.beans.PropertyChangeEvent;
15 import java.beans.PropertyChangeListener;
16 import java.util.Iterator;
17
18 import javax.swing.AbstractAction;
19 import javax.swing.Action;
20 import javax.swing.Box;
21 import javax.swing.JButton;
22 import javax.swing.JLabel;
23 import javax.swing.JOptionPane;
24 import javax.swing.JPanel;
25 import javax.swing.JPopupMenu;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.JTextField;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
32 import javax.swing.table.DefaultTableModel;
33
34 import jp.sourceforge.stigmata.BirthmarkEnvironment;
35 import jp.sourceforge.stigmata.ui.swing.actions.PopupShowAction;
36 import jp.sourceforge.talisman.i18n.Messages;
37
38 /**
39  * 
40  * @author Haruaki Tamada
41  */
42 public class PropertyEditPane extends JPanel{
43     private static final long serialVersionUID = 12397342543653L;
44
45     private StigmataFrame stigmata;
46     private JTable table;
47     private DefaultTableModel model;
48
49     public PropertyEditPane(StigmataFrame stigmata){
50         this.stigmata = stigmata;
51
52         initLayouts();
53         initData();
54     }
55
56     public void updateEnvironment(BirthmarkEnvironment environment){
57         environment.clearProperties();
58         for(int i = 0; i < model.getRowCount(); i++){
59             environment.addProperty(
60                 (String)model.getValueAt(i, 0),
61                 (String)model.getValueAt(i, 1)
62             );
63         }
64     }
65
66     private void initData(){
67         BirthmarkEnvironment environment = stigmata.getEnvironment();
68         environment.addPropertyListener(new PropertyChangeListener(){
69             @Override
70             public void propertyChange(PropertyChangeEvent evt){
71                 String name = evt.getPropertyName();
72                 String value = (String)evt.getNewValue();
73                 if(value == null){
74                     removeProperty(name);
75                 }
76                 else{
77                     addOrUpdateProperty(name, value);
78                 }
79             }
80         });
81         for(Iterator<String> i = environment.propertyKeys(); i.hasNext(); ){
82             String key = i.next();
83             model.addRow(new Object[] { key, environment.getProperty(key), });
84         }
85     }
86
87     private void removeProperty(String name){
88         int index = findIndex(name);
89         if(index >= 0){
90             model.removeRow(index);
91         }
92         stigmata.setNeedToSaveSettings(true);
93     }
94
95     private void addOrUpdateProperty(String name, String value){
96         int index = findIndex(name);
97         if(index >= 0){
98             model.setValueAt(value, index, 1);
99         }
100         else{
101             model.addRow(new Object[] { name, value, });
102         }
103         stigmata.setNeedToSaveSettings(true);
104     }
105
106     private int findIndex(String name){
107         for(int i = 0; i < model.getRowCount(); i++){
108             String v = (String)model.getValueAt(i, 0);
109             if(v.equals(name)){
110                 return i;
111             }
112         }
113         return -1;
114     }
115
116     private void addNewProperty(int index){
117         final Messages messages = stigmata.getMessages();
118         JPanel panel = new JPanel(new GridBagLayout());
119         JLabel nameLabel = new JLabel(messages.get("propertyname.label"));
120         JLabel valueLabel = new JLabel(messages.get("propertyvalue.label"));
121         JTextField name = new JTextField(15);
122         JTextField value = new JTextField(15);
123
124         GridBagConstraints gbc = new GridBagConstraints();
125         gbc.gridx = 0; gbc.gridwidth = 1;
126         gbc.gridy = 0; gbc.gridheight = 1;
127         gbc.insets = new Insets(5, 5, 5, 0);
128         gbc.weightx = 0d;
129         panel.add(nameLabel, gbc);
130
131         gbc.gridy = 1;
132         panel.add(valueLabel, gbc);
133
134         gbc.gridx = 1;
135         gbc.weightx = 1d;
136         panel.add(value, gbc);
137
138         gbc.gridy = 0;
139         gbc.fill = GridBagConstraints.HORIZONTAL;
140         panel.add(name, gbc);
141
142         if(index >= 0){
143             String keyValue = String.valueOf(table.getValueAt(index, 0));
144             String valueValue = String.valueOf(table.getValueAt(index, 1));
145             if(keyValue != null)   name.setText(keyValue);
146             if(valueValue != null) value.setText(valueValue);
147         }
148
149         int val = JOptionPane.showConfirmDialog(
150             stigmata, panel, messages.get("propertyadd.dialog.title"),
151             JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE
152         );
153         if(val == JOptionPane.YES_OPTION){
154             if(index >= 0){
155                 model.setValueAt(name.getText(), index, 0);
156                 model.setValueAt(value.getText(), index, 1);
157             }
158             else{
159                 model.addRow(new Object[] {
160                     name.getText().trim(),
161                     value.getText()
162                 });
163             }
164             stigmata.setNeedToSaveSettings(true);
165         }
166     }
167
168     private void removeSelectedProperty(){
169         int[] indexes = table.getSelectedRows();
170         for(int i = indexes.length - 1; i >= 0; i--){
171             model.removeRow(indexes[i]);
172         }
173         stigmata.setNeedToSaveSettings(true);
174     }
175
176     private void initLayouts(){
177         final Messages messages = stigmata.getMessages();
178         model = new UneditableDefaultTableModel();
179         model.addColumn(messages.get("propertyname.label"));
180         model.addColumn(messages.get("propertyvalue.label"));
181         table = new JTable(model);
182         table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
183         table.setColumnSelectionAllowed(false);
184
185         Action addAction = new AbstractAction(){
186             private static final long serialVersionUID = 1283676936119122278L;
187
188             @Override
189             public void actionPerformed(ActionEvent e){
190                 addNewProperty(-1);
191             }
192         };
193         final Action removeAction = new AbstractAction(){
194             private static final long serialVersionUID = -411260949451039374L;
195
196             @Override
197             public void actionPerformed(ActionEvent e){
198                 removeSelectedProperty();
199             }
200         };
201         final Action editAction = new AbstractAction(){
202             private static final long serialVersionUID = -7406073660916286349L;
203
204             @Override
205             public void actionPerformed(ActionEvent e){
206                 addNewProperty(table.getSelectedRow());
207             }
208         };
209         table.addMouseListener(new MouseAdapter(){
210             @Override
211             public void mouseClicked(MouseEvent e){
212                 if(e.getClickCount() == 2){
213                     addNewProperty(table.getSelectedRow());
214                 }
215             }
216         });
217         JButton addButton = GUIUtility.createButton(messages, "propertyadd", addAction);
218         JButton changeButton = GUIUtility.createButton(messages, "propertychange", editAction);
219         JButton removeButton = GUIUtility.createButton(messages, "propertyremove", removeAction);
220
221         final JPopupMenu popup = new JPopupMenu();
222         popup.add(GUIUtility.createJMenuItem(messages, "propertyadd", addAction));
223         popup.add(GUIUtility.createJMenuItem(messages, "propertychange", editAction));
224         popup.add(GUIUtility.createJMenuItem(messages, "propertyremove", removeAction));
225
226         setLayout(new BorderLayout());
227         JScrollPane scroll = new JScrollPane(table);
228         Box box = Box.createHorizontalBox();
229         box.add(Box.createHorizontalGlue());
230         box.add(addButton);
231         box.add(Box.createHorizontalGlue());
232         box.add(changeButton);
233         box.add(Box.createHorizontalGlue());
234         box.add(removeButton);
235         box.add(Box.createHorizontalGlue());
236
237         add(scroll, BorderLayout.CENTER);
238         add(box, BorderLayout.SOUTH);
239
240         table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
241             @Override
242             public void valueChanged(ListSelectionEvent arg0){
243                 removeAction.setEnabled(table.getSelectedRowCount() != 0);
244                 editAction.setEnabled(table.getSelectedRowCount() == 1);
245             }
246         });
247         table.addMouseListener(new PopupShowAction(popup));
248         editAction.setEnabled(false);
249         removeAction.setEnabled(false);
250     }
251 }