1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* Copyright 2011 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import com.gitblit.Constants.RpcRequest;
import com.gitblit.models.SettingModel;
import com.gitblit.utils.StringUtils;
/**
* Settings panel displays a list of server settings and their associated
* metadata. This panel also allows editing of a setting.
*
* @author James Moger
*
*/
public class SettingsPanel extends JPanel {
private static final long serialVersionUID = 1L;
private final GitblitClient gitblit;
private HeaderPanel header;
private JTable table;
private SettingsTableModel tableModel;
private TableRowSorter<SettingsTableModel> defaultSorter;
private JTextField filterTextfield;
public SettingsPanel(GitblitClient gitblit) {
super();
this.gitblit = gitblit;
initialize();
}
private void initialize() {
JButton refreshSettings = new JButton(Translation.get("gb.refresh"));
refreshSettings.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
refreshSettings();
}
});
final JButton editSetting = new JButton(Translation.get("gb.edit"));
editSetting.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int viewRow = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel(viewRow);
String key = tableModel.keys.get(modelRow);
SettingModel setting = tableModel.settings.get(key);
editSetting(setting);
}
});
NameRenderer nameRenderer = new NameRenderer();
final SettingPanel settingPanel = new SettingPanel();
tableModel = new SettingsTableModel();
defaultSorter = new TableRowSorter<SettingsTableModel>(tableModel);
table = Utils.newTable(tableModel, Utils.DATE_FORMAT);
table.setDefaultRenderer(SettingModel.class, new SettingCellRenderer());
String name = table.getColumnName(UsersTableModel.Columns.Name.ordinal());
table.getColumn(name).setCellRenderer(nameRenderer);
table.setRowSorter(defaultSorter);
table.getRowSorter().toggleSortOrder(SettingsTableModel.Columns.Name.ordinal());
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
boolean singleSelection = table.getSelectedRows().length == 1;
editSetting.setEnabled(singleSelection);
if (singleSelection) {
int viewRow = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel(viewRow);
SettingModel setting = tableModel.get(modelRow);
settingPanel.setSetting(setting);
} else {
settingPanel.clear();
}
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int viewRow = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel(viewRow);
SettingModel setting = tableModel.get(modelRow);
editSetting(setting);
}
}
});
filterTextfield = new JTextField();
filterTextfield.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
filterSettings(filterTextfield.getText());
}
});
filterTextfield.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
filterSettings(filterTextfield.getText());
}
});
JPanel settingFilterPanel = new JPanel(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
settingFilterPanel.add(new JLabel(Translation.get("gb.filter")), BorderLayout.WEST);
settingFilterPanel.add(filterTextfield, BorderLayout.CENTER);
JPanel settingsTablePanel = new JPanel(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
settingsTablePanel.add(settingFilterPanel, BorderLayout.NORTH);
settingsTablePanel.add(new JScrollPane(table), BorderLayout.CENTER);
settingsTablePanel.add(settingPanel, BorderLayout.SOUTH);
JPanel settingsControls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
settingsControls.add(refreshSettings);
settingsControls.add(editSetting);
setLayout(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
header = new HeaderPanel(Translation.get("gb.settings"), "settings_16x16.png");
add(header, BorderLayout.NORTH);
add(settingsTablePanel, BorderLayout.CENTER);
add(settingsControls, BorderLayout.SOUTH);
}
@Override
public void requestFocus() {
filterTextfield.requestFocus();
}
@Override
public Insets getInsets() {
return Utils.INSETS;
}
protected void updateTable(boolean pack) {
tableModel.setSettings(gitblit.getSettings());
tableModel.fireTableDataChanged();
header.setText(Translation.get("gb.settings"));
if (pack) {
Utils.packColumns(table, Utils.MARGIN);
}
}
private void filterSettings(final String fragment) {
if (StringUtils.isEmpty(fragment)) {
table.setRowSorter(defaultSorter);
return;
}
RowFilter<SettingsTableModel, Object> containsFilter = new RowFilter<SettingsTableModel, Object>() {
@Override
public boolean include(Entry<? extends SettingsTableModel, ? extends Object> entry) {
for (int i = entry.getValueCount() - 1; i >= 0; i--) {
if (entry.getStringValue(i).toLowerCase().contains(fragment.toLowerCase())) {
return true;
}
}
return false;
}
};
TableRowSorter<SettingsTableModel> sorter = new TableRowSorter<SettingsTableModel>(
tableModel);
sorter.setRowFilter(containsFilter);
table.setRowSorter(sorter);
}
protected void refreshSettings() {
GitblitWorker worker = new GitblitWorker(SettingsPanel.this, RpcRequest.LIST_SETTINGS) {
@Override
protected Boolean doRequest() throws IOException {
gitblit.refreshSettings();
return true;
}
@Override
protected void onSuccess() {
updateTable(false);
}
};
worker.execute();
}
protected void editSetting(final SettingModel settingModel) {
final JTextField textField = new JTextField(settingModel.currentValue);
JPanel editPanel = new JPanel(new GridLayout(0, 1));
editPanel.add(new JLabel("New Value"));
editPanel.add(textField);
JPanel settingPanel = new JPanel(new BorderLayout());
settingPanel.add(new SettingPanel(settingModel), BorderLayout.CENTER);
settingPanel.add(editPanel, BorderLayout.SOUTH);
settingPanel.setPreferredSize(new Dimension(800, 200));
String[] options;
if (settingModel.currentValue.equals(settingModel.defaultValue)) {
options = new String[] { Translation.get("gb.cancel"), Translation.get("gb.save") };
} else {
options = new String[] { Translation.get("gb.cancel"),
Translation.get("gb.setDefault"), Translation.get("gb.save") };
}
String defaultOption = options[0];
int selection = JOptionPane.showOptionDialog(SettingsPanel.this, settingPanel,
settingModel.name, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
new ImageIcon(getClass().getResource("/settings_16x16.png")), options,
defaultOption);
if (selection <= 0) {
return;
}
if (options[selection].equals(Translation.get("gb.setDefault"))) {
textField.setText(settingModel.defaultValue);
}
final Map<String, String> newSettings = new HashMap<String, String>();
newSettings.put(settingModel.name, textField.getText().trim());
GitblitWorker worker = new GitblitWorker(SettingsPanel.this, RpcRequest.EDIT_SETTINGS) {
@Override
protected Boolean doRequest() throws IOException {
boolean success = gitblit.updateSettings(newSettings);
if (success) {
gitblit.refreshSettings();
}
return success;
}
@Override
protected void onSuccess() {
updateTable(false);
}
};
worker.execute();
}
}
|