OSDN Git Service

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