James Moger
2011-10-21 b75734f0600c333d70a3659af82be54caf3cfd3e
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/*
 * 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.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.FlowLayout;
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.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;
 
import com.gitblit.Constants.RpcRequest;
import com.gitblit.client.ClosableTabComponent.CloseTabListener;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.SettingModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.StringUtils;
 
/**
 * GitblitPanel performs the login, all business logic, and contains all widgets
 * to represent the state of a repository for the given account credentials.
 * 
 * @author James Moger
 * 
 */
public class GitblitPanel extends JPanel implements CloseTabListener {
 
    private static final long serialVersionUID = 1L;
 
    private final int margin = 5;
 
    private final Insets insets = new Insets(margin, margin, margin, margin);
 
    private GitblitModel gitblit;
 
    private JTabbedPane tabs;
 
    private JTable repositoriesTable;
 
    private RepositoriesModel repositoriesModel;
 
    private JTable usersTable;
 
    private UsersModel usersModel;
 
    private JTable settingsTable;
 
    private SettingsModel settingsModel;
 
    private JButton createRepository;
 
    private JButton delRepository;
 
    private NameRenderer nameRenderer;
 
    private IndicatorsRenderer typeRenderer;
 
    private DefaultTableCellRenderer ownerRenderer;
 
    private DefaultTableCellRenderer sizeRenderer;
 
    private TableRowSorter<RepositoriesModel> defaultRepositoriesSorter;
 
    private TableRowSorter<UsersModel> defaultUsersSorter;
 
    private TableRowSorter<SettingsModel> defaultSettingsSorter;
 
    private JButton editRepository;
 
    public GitblitPanel(GitblitRegistration reg) {
        this(reg.url, reg.account, reg.password);
    }
 
    public GitblitPanel(String url, String account, char[] password) {
        this.gitblit = new GitblitModel(url, account, password);
 
        tabs = new JTabbedPane(JTabbedPane.BOTTOM);
        tabs.addTab(Translation.get("gb.repositories"), createRepositoriesPanel());
        tabs.addTab(Translation.get("gb.users"), createUsersPanel());
        tabs.addTab(Translation.get("gb.settings"), createSettingsPanel());
 
        setLayout(new BorderLayout());
        add(tabs, BorderLayout.CENTER);
    }
 
    private JPanel createRepositoriesPanel() {
        final JButton browseRepository = new JButton(Translation.get("gb.browse"));
        browseRepository.setEnabled(false);
        browseRepository.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                RepositoryModel model = getSelectedRepositories().get(0);
                String u = MessageFormat.format("{0}/summary/{1}", gitblit.url,
                        StringUtils.encodeURL(model.name));
                try {
                    Desktop.getDesktop().browse(new URI(u));
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        });
 
        JButton refreshRepositories = new JButton(Translation.get("gb.refresh"));
        refreshRepositories.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                refreshRepositories();
            }
        });
 
        createRepository = new JButton(Translation.get("gb.create"));
        createRepository.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createRepository();
            }
        });
 
        editRepository = new JButton(Translation.get("gb.edit"));
        editRepository.setEnabled(false);
        editRepository.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editRepository(getSelectedRepositories().get(0));
            }
        });
 
        delRepository = new JButton(Translation.get("gb.delete"));
        delRepository.setEnabled(false);
        delRepository.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteRepositories(getSelectedRepositories());
            }
        });
 
        nameRenderer = new NameRenderer();
        typeRenderer = new IndicatorsRenderer();
 
        sizeRenderer = new DefaultTableCellRenderer();
        sizeRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
        sizeRenderer.setForeground(new Color(0, 0x80, 0));
 
        ownerRenderer = new DefaultTableCellRenderer();
        ownerRenderer.setForeground(Color.gray);
        ownerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
 
        repositoriesModel = new RepositoriesModel();
        defaultRepositoriesSorter = new TableRowSorter<RepositoriesModel>(repositoriesModel);
        repositoriesTable = Utils.newTable(repositoriesModel);
        repositoriesTable.setRowHeight(nameRenderer.getFont().getSize() + 8);
        repositoriesTable.setRowSorter(defaultRepositoriesSorter);
        repositoriesTable.getRowSorter().toggleSortOrder(RepositoriesModel.Columns.Name.ordinal());
 
        setRepositoryRenderer(RepositoriesModel.Columns.Name, nameRenderer, -1);
        setRepositoryRenderer(RepositoriesModel.Columns.Indicators, typeRenderer, 100);
        setRepositoryRenderer(RepositoriesModel.Columns.Owner, ownerRenderer, -1);
        setRepositoryRenderer(RepositoriesModel.Columns.Size, sizeRenderer, 60);
 
        repositoriesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }
                boolean singleSelection = repositoriesTable.getSelectedRowCount() == 1;
                boolean selected = repositoriesTable.getSelectedRow() > -1;
                browseRepository.setEnabled(singleSelection);
                delRepository.setEnabled(selected);
                if (selected) {
                    int viewRow = repositoriesTable.getSelectedRow();
                    int modelRow = repositoriesTable.convertRowIndexToModel(viewRow);
                    RepositoryModel model = ((RepositoriesModel) repositoriesTable.getModel()).list
                            .get(modelRow);
                    editRepository.setEnabled(singleSelection
                            && (gitblit.allowAdmin() || gitblit.isOwner(model)));
                } else {
                    editRepository.setEnabled(false);
                }
            }
        });
 
        repositoriesTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2 && gitblit.allowAdmin()) {
                    editRepository(getSelectedRepositories().get(0));
                }
            }
        });
 
        final JTextField repositoryFilter = new JTextField();
        repositoryFilter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                filterRepositories(repositoryFilter.getText());
            }
        });
        repositoryFilter.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                filterRepositories(repositoryFilter.getText());
            }
        });
 
        JPanel repositoryFilterPanel = new JPanel(new BorderLayout(margin, margin));
        repositoryFilterPanel.add(new JLabel(Translation.get("gb.filter")), BorderLayout.WEST);
        repositoryFilterPanel.add(repositoryFilter, BorderLayout.CENTER);
 
        JPanel repositoryTablePanel = new JPanel(new BorderLayout(margin, margin));
        repositoryTablePanel.add(repositoryFilterPanel, BorderLayout.NORTH);
        repositoryTablePanel.add(new JScrollPane(repositoriesTable), BorderLayout.CENTER);
 
        JPanel repositoryControls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        repositoryControls.add(refreshRepositories);
        repositoryControls.add(browseRepository);
        repositoryControls.add(createRepository);
        repositoryControls.add(editRepository);
        repositoryControls.add(delRepository);
 
        JPanel repositoriesPanel = new JPanel(new BorderLayout(margin, margin)) {
 
            private static final long serialVersionUID = 1L;
 
            public Insets getInsets() {
                return insets;
            }
        };
        repositoriesPanel.add(new HeaderPanel(Translation.get("gb.repositories"),
                "gitweb-favicon.png"), BorderLayout.NORTH);
        repositoriesPanel.add(repositoryTablePanel, BorderLayout.CENTER);
        repositoriesPanel.add(repositoryControls, BorderLayout.SOUTH);
 
        return repositoriesPanel;
    }
 
    private void setRepositoryRenderer(RepositoriesModel.Columns col, TableCellRenderer renderer,
            int maxWidth) {
        String name = repositoriesTable.getColumnName(col.ordinal());
        repositoriesTable.getColumn(name).setCellRenderer(renderer);
        if (maxWidth > 0) {
            repositoriesTable.getColumn(name).setMinWidth(maxWidth);
            repositoriesTable.getColumn(name).setMaxWidth(maxWidth);
        }
    }
 
    private JPanel createUsersPanel() {
        JButton refreshUsers = new JButton(Translation.get("gb.refresh"));
        refreshUsers.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                refreshUsers();
            }
        });
 
        JButton createUser = new JButton(Translation.get("gb.create"));
        createUser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createUser();
            }
        });
 
        final JButton editUser = new JButton(Translation.get("gb.edit"));
        editUser.setEnabled(false);
        editUser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editUser(getSelectedUsers().get(0));
            }
        });
 
        final JButton delUser = new JButton(Translation.get("gb.delete"));
        delUser.setEnabled(false);
        delUser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteUsers(getSelectedUsers());
            }
        });
 
        usersModel = new UsersModel();
        defaultUsersSorter = new TableRowSorter<UsersModel>(usersModel);
        usersTable = Utils.newTable(usersModel);
        String name = usersTable.getColumnName(UsersModel.Columns.Name.ordinal());
        usersTable.setRowHeight(nameRenderer.getFont().getSize() + 8);
        usersTable.getColumn(name).setCellRenderer(nameRenderer);
        usersTable.setRowSorter(defaultUsersSorter);
        usersTable.getRowSorter().toggleSortOrder(UsersModel.Columns.Name.ordinal());
        usersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }
                boolean selected = usersTable.getSelectedRow() > -1;
                boolean singleSelection = usersTable.getSelectedRows().length == 1;
                editUser.setEnabled(singleSelection && selected);
                delUser.setEnabled(selected);
            }
        });
 
        usersTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    editUser(getSelectedUsers().get(0));
                }
            }
        });
 
        final JTextField userFilter = new JTextField();
        userFilter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                filterUsers(userFilter.getText());
            }
        });
        userFilter.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                filterUsers(userFilter.getText());
            }
        });
 
        JPanel userFilterPanel = new JPanel(new BorderLayout(margin, margin));
        userFilterPanel.add(new JLabel(Translation.get("gb.filter")), BorderLayout.WEST);
        userFilterPanel.add(userFilter, BorderLayout.CENTER);
 
        JPanel userTablePanel = new JPanel(new BorderLayout(margin, margin));
        userTablePanel.add(userFilterPanel, BorderLayout.NORTH);
        userTablePanel.add(new JScrollPane(usersTable), BorderLayout.CENTER);
 
        JPanel userControls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        userControls.add(refreshUsers);
        userControls.add(createUser);
        userControls.add(editUser);
        userControls.add(delUser);
 
        JPanel usersPanel = new JPanel(new BorderLayout(margin, margin)) {
 
            private static final long serialVersionUID = 1L;
 
            public Insets getInsets() {
                return insets;
            }
        };
        usersPanel.add(new HeaderPanel(Translation.get("gb.users"), "user_16x16.png"),
                BorderLayout.NORTH);
        usersPanel.add(userTablePanel, BorderLayout.CENTER);
        usersPanel.add(userControls, BorderLayout.SOUTH);
 
        return usersPanel;
    }
 
    private JPanel createSettingsPanel() {
        final SettingPanel settingPanel = new SettingPanel();
        settingsModel = new SettingsModel();
        defaultSettingsSorter = new TableRowSorter<SettingsModel>(settingsModel);
        settingsTable = Utils.newTable(settingsModel);
        settingsTable.setDefaultRenderer(SettingModel.class, new SettingCellRenderer());
        String name = settingsTable.getColumnName(UsersModel.Columns.Name.ordinal());
        settingsTable.setRowHeight(nameRenderer.getFont().getSize() + 8);
        settingsTable.getColumn(name).setCellRenderer(nameRenderer);
        settingsTable.setRowSorter(defaultSettingsSorter);
        settingsTable.getRowSorter().toggleSortOrder(SettingsModel.Columns.Name.ordinal());
        settingsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }
                boolean selected = settingsTable.getSelectedRow() > -1;
                boolean singleSelection = settingsTable.getSelectedRows().length == 1;
                // TODO enable/disable setting buttons
                if (singleSelection) {
                    int viewRow = settingsTable.getSelectedRow();
                    int modelRow = settingsTable.convertRowIndexToModel(viewRow);
                    SettingModel setting = settingsModel.get(modelRow);
                    settingPanel.setSetting(setting);
                } else {
                    settingPanel.clear();
                }
            }
        });
 
        final JTextField settingFilter = new JTextField();
        settingFilter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                filterSettings(settingFilter.getText());
            }
        });
        settingFilter.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                filterSettings(settingFilter.getText());
            }
        });
 
        JPanel settingFilterPanel = new JPanel(new BorderLayout(margin, margin));
        settingFilterPanel.add(new JLabel(Translation.get("gb.filter")), BorderLayout.WEST);
        settingFilterPanel.add(settingFilter, BorderLayout.CENTER);
 
        JPanel settingsTablePanel = new JPanel(new BorderLayout(margin, margin));
        settingsTablePanel.add(settingFilterPanel, BorderLayout.NORTH);
        settingsTablePanel.add(new JScrollPane(settingsTable), BorderLayout.CENTER);
        settingsTablePanel.add(settingPanel, BorderLayout.SOUTH);
 
        JPanel settingsControls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        // TODO update setting?
 
        JPanel settingsPanel = new JPanel(new BorderLayout(margin, margin)) {
 
            private static final long serialVersionUID = 1L;
 
            public Insets getInsets() {
                return insets;
            }
        };
        settingsPanel.add(new HeaderPanel(Translation.get("gb.settings"), "settings_16x16.png"),
                BorderLayout.NORTH);
        settingsPanel.add(settingsTablePanel, BorderLayout.CENTER);
        settingsPanel.add(settingsControls, BorderLayout.SOUTH);
 
        return settingsPanel;
    }
 
    public void login() throws IOException {
        gitblit.login();
 
        updateRepositoriesTable();
        Utils.packColumns(repositoriesTable, 2);
 
        if (gitblit.allowAdmin()) {
            updateUsersTable();
            updateSettingsTable();
        } else {
            // user does not have administrator privileges
            // hide admin repository buttons
            createRepository.setVisible(false);
            editRepository.setVisible(false);
            delRepository.setVisible(false);
 
            while (tabs.getTabCount() > 1) {
                // remove admin tabs
                tabs.removeTabAt(1);
            }
        }
    }
 
    private void updateRepositoriesTable() {
        repositoriesModel.list.clear();
        repositoriesModel.list.addAll(gitblit.getRepositories());
        repositoriesModel.fireTableDataChanged();
    }
 
    private void updateUsersTable() {
        usersModel.list.clear();
        usersModel.list.addAll(gitblit.getUsers());
        usersModel.fireTableDataChanged();
    }
 
    private void updateSettingsTable() {
        settingsModel.setSettings(gitblit.getSettings());
        settingsModel.fireTableDataChanged();
    }
 
    private void filterRepositories(final String fragment) {
        if (StringUtils.isEmpty(fragment)) {
            repositoriesTable.setRowSorter(defaultRepositoriesSorter);
            return;
        }
        RowFilter<RepositoriesModel, Object> containsFilter = new RowFilter<RepositoriesModel, Object>() {
            public boolean include(Entry<? extends RepositoriesModel, ? 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<RepositoriesModel> sorter = new TableRowSorter<RepositoriesModel>(
                repositoriesModel);
        sorter.setRowFilter(containsFilter);
        repositoriesTable.setRowSorter(sorter);
    }
 
    private void filterUsers(final String fragment) {
        if (StringUtils.isEmpty(fragment)) {
            usersTable.setRowSorter(defaultUsersSorter);
            return;
        }
        RowFilter<UsersModel, Object> containsFilter = new RowFilter<UsersModel, Object>() {
            public boolean include(Entry<? extends UsersModel, ? 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<UsersModel> sorter = new TableRowSorter<UsersModel>(usersModel);
        sorter.setRowFilter(containsFilter);
        usersTable.setRowSorter(sorter);
    }
 
    private void filterSettings(final String fragment) {
        if (StringUtils.isEmpty(fragment)) {
            settingsTable.setRowSorter(defaultSettingsSorter);
            return;
        }
        RowFilter<SettingsModel, Object> containsFilter = new RowFilter<SettingsModel, Object>() {
            public boolean include(Entry<? extends SettingsModel, ? 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<SettingsModel> sorter = new TableRowSorter<SettingsModel>(settingsModel);
        sorter.setRowFilter(containsFilter);
        settingsTable.setRowSorter(sorter);
    }
 
    private List<RepositoryModel> getSelectedRepositories() {
        List<RepositoryModel> repositories = new ArrayList<RepositoryModel>();
        for (int viewRow : repositoriesTable.getSelectedRows()) {
            int modelRow = repositoriesTable.convertRowIndexToModel(viewRow);
            RepositoryModel model = repositoriesModel.list.get(modelRow);
            repositories.add(model);
        }
        return repositories;
    }
 
    private List<UserModel> getSelectedUsers() {
        List<UserModel> users = new ArrayList<UserModel>();
        for (int viewRow : usersTable.getSelectedRows()) {
            int modelRow = usersTable.convertRowIndexToModel(viewRow);
            UserModel model = usersModel.list.get(modelRow);
            users.add(model);
        }
        return users;
    }
 
    @Override
    public Insets getInsets() {
        return insets;
    }
 
    @Override
    public void closeTab(Component c) {
        gitblit = null;
    }
 
    protected void refreshRepositories() {
        GitblitWorker worker = new GitblitWorker(GitblitPanel.this, RpcRequest.LIST_REPOSITORIES) {
            @Override
            protected Boolean doRequest() throws IOException {
                gitblit.refreshRepositories();
                return true;
            }
 
            @Override
            protected void onSuccess() {
                updateRepositoriesTable();
            }
        };
        worker.execute();
    }
 
    /**
     * Displays the create repository dialog and fires a SwingWorker to update
     * the server, if appropriate.
     * 
     */
    protected void createRepository() {
        EditRepositoryDialog dialog = new EditRepositoryDialog();
        dialog.setLocationRelativeTo(GitblitPanel.this);
        dialog.setUsers(null, gitblit.getUsernames(), null);
        dialog.setRepositories(gitblit.getRepositories());
        dialog.setVisible(true);
        final RepositoryModel newRepository = dialog.getRepository();
        final List<String> permittedUsers = dialog.getPermittedUsers();
        if (newRepository == null) {
            return;
        }
 
        GitblitWorker worker = new GitblitWorker(this, RpcRequest.CREATE_REPOSITORY) {
 
            @Override
            protected Boolean doRequest() throws IOException {
                boolean success = gitblit.createRepository(newRepository, permittedUsers);
                if (success) {
                    gitblit.refreshRepositories();
                    if (permittedUsers.size() > 0) {
                        gitblit.refreshUsers();
                    }
                }
                return success;
            }
 
            @Override
            protected void onSuccess() {
                updateRepositoriesTable();
                updateUsersTable();
            }
 
            @Override
            protected void onFailure() {
                showFailure("Failed to execute request \"{0}\" for repository \"{1}\".",
                        getRequestType(), newRepository.name);
            }
        };
        worker.execute();
    }
 
    /**
     * Displays the edit repository dialog and fires a SwingWorker to update the
     * server, if appropriate.
     * 
     * @param repository
     */
    protected void editRepository(final RepositoryModel repository) {
        EditRepositoryDialog dialog = new EditRepositoryDialog(repository);
        dialog.setLocationRelativeTo(GitblitPanel.this);
        List<String> usernames = gitblit.getUsernames();
        List<String> members = gitblit.getPermittedUsernames(repository);
        dialog.setUsers(repository.owner, usernames, members);
        dialog.setFederationSets(gitblit.getFederationSets(), repository.federationSets);
        dialog.setVisible(true);
        final RepositoryModel revisedRepository = dialog.getRepository();
        final List<String> permittedUsers = dialog.getPermittedUsers();
        if (revisedRepository == null) {
            return;
        }
 
        GitblitWorker worker = new GitblitWorker(this, RpcRequest.EDIT_REPOSITORY) {
 
            @Override
            protected Boolean doRequest() throws IOException {
                boolean success = gitblit.updateRepository(repository.name, revisedRepository,
                        permittedUsers);
                if (success) {
                    gitblit.refreshRepositories();
                    gitblit.refreshUsers();
                }
                return success;
            }
 
            @Override
            protected void onSuccess() {
                updateRepositoriesTable();
                updateUsersTable();
            }
 
            @Override
            protected void onFailure() {
                showFailure("Failed to execute request \"{0}\" for repository \"{1}\".",
                        getRequestType(), repository.name);
            }
        };
        worker.execute();
    }
 
    protected void deleteRepositories(final List<RepositoryModel> repositories) {
        if (repositories == null || repositories.size() == 0) {
            return;
        }
        StringBuilder message = new StringBuilder("Delete the following repositories?\n\n");
        for (RepositoryModel repository : repositories) {
            message.append(repository.name).append("\n");
        }
        int result = JOptionPane.showConfirmDialog(GitblitPanel.this, message.toString(),
                "Delete Repositories?", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            GitblitWorker worker = new GitblitWorker(this, RpcRequest.DELETE_REPOSITORY) {
                @Override
                protected Boolean doRequest() throws IOException {
                    boolean success = true;
                    for (RepositoryModel repository : repositories) {
                        success &= gitblit.deleteRepository(repository);
                    }
                    if (success) {
                        gitblit.refreshRepositories();
                        gitblit.refreshUsers();
                    }
                    return success;
                }
 
                @Override
                protected void onSuccess() {
                    updateRepositoriesTable();
                    updateUsersTable();
                }
 
                @Override
                protected void onFailure() {
                    showFailure("Failed to delete specified repositories!");
                }
            };
            worker.execute();
        }
    }
 
    protected void refreshUsers() {
        GitblitWorker worker = new GitblitWorker(GitblitPanel.this, RpcRequest.LIST_USERS) {
            @Override
            protected Boolean doRequest() throws IOException {
                gitblit.refreshUsers();
                return true;
            }
 
            @Override
            protected void onSuccess() {
                updateUsersTable();
            }
        };
        worker.execute();
    }
 
    /**
     * Displays the create user dialog and fires a SwingWorker to update the
     * server, if appropriate.
     * 
     */
    protected void createUser() {
        EditUserDialog dialog = new EditUserDialog(gitblit.getSettings());
        dialog.setLocationRelativeTo(GitblitPanel.this);
        dialog.setUsers(gitblit.getUsers());
        dialog.setRepositories(gitblit.getRepositories(), null);
        dialog.setVisible(true);
        final UserModel newUser = dialog.getUser();
        if (newUser == null) {
            return;
        }
 
        GitblitWorker worker = new GitblitWorker(this, RpcRequest.CREATE_USER) {
 
            @Override
            protected Boolean doRequest() throws IOException {
                boolean success = gitblit.createUser(newUser);
                if (success) {
                    gitblit.refreshUsers();
                }
                return success;
            }
 
            @Override
            protected void onSuccess() {
                updateUsersTable();
            }
 
            @Override
            protected void onFailure() {
                showFailure("Failed to execute request \"{0}\" for user \"{1}\".",
                        getRequestType(), newUser.username);
            }
        };
        worker.execute();
    }
 
    /**
     * Displays the edit user dialog and fires a SwingWorker to update the
     * server, if appropriate.
     * 
     * @param user
     */
    protected void editUser(final UserModel user) {
        EditUserDialog dialog = new EditUserDialog(user, gitblit.getSettings());
        dialog.setLocationRelativeTo(GitblitPanel.this);
        dialog.setRepositories(gitblit.getRepositories(), new ArrayList<String>(user.repositories));
        dialog.setVisible(true);
        final UserModel revisedUser = dialog.getUser();
        if (revisedUser == null) {
            return;
        }
 
        GitblitWorker worker = new GitblitWorker(this, RpcRequest.EDIT_USER) {
            @Override
            protected Boolean doRequest() throws IOException {
                boolean success = gitblit.updateUser(user.username, revisedUser);
                if (success) {
                    gitblit.refreshUsers();
                }
                return success;
            }
 
            @Override
            protected void onSuccess() {
                updateUsersTable();
            }
 
            @Override
            protected void onFailure() {
                showFailure("Failed to execute request \"{0}\" for user \"{1}\".",
                        getRequestType(), user.username);
            }
        };
        worker.execute();
    }
 
    protected void deleteUsers(final List<UserModel> users) {
        if (users == null || users.size() == 0) {
            return;
        }
        StringBuilder message = new StringBuilder("Delete the following users?\n\n");
        for (UserModel user : users) {
            message.append(user.username).append("\n");
        }
        int result = JOptionPane.showConfirmDialog(GitblitPanel.this, message.toString(),
                "Delete Users?", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            GitblitWorker worker = new GitblitWorker(this, RpcRequest.DELETE_USER) {
                @Override
                protected Boolean doRequest() throws IOException {
                    boolean success = true;
                    for (UserModel user : users) {
                        success &= gitblit.deleteUser(user);
                    }
                    if (success) {
                        gitblit.refreshUsers();
                    }
                    return success;
                }
 
                @Override
                protected void onSuccess() {
                    updateUsersTable();
                }
 
                @Override
                protected void onFailure() {
                    showFailure("Failed to delete specified users!");
                }
            };
            worker.execute();
        }
    }
}