]> source.dussan.org Git - gitblit.git/commitdiff
Refactored all SwingWorkers into GitblitWorkers.
authorJames Moger <james.moger@gitblit.com>
Sat, 15 Oct 2011 18:39:07 +0000 (14:39 -0400)
committerJames Moger <james.moger@gitblit.com>
Sat, 15 Oct 2011 18:39:07 +0000 (14:39 -0400)
src/com/gitblit/client/GitblitModel.java [new file with mode: 0644]
src/com/gitblit/client/GitblitPanel.java
src/com/gitblit/client/GitblitWorker.java [new file with mode: 0644]
src/com/gitblit/client/Utils.java [new file with mode: 0644]

diff --git a/src/com/gitblit/client/GitblitModel.java b/src/com/gitblit/client/GitblitModel.java
new file mode 100644 (file)
index 0000000..92b0b1c
--- /dev/null
@@ -0,0 +1,180 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.client;\r
+\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import com.gitblit.GitBlitException.ForbiddenException;\r
+import com.gitblit.IStoredSettings;\r
+import com.gitblit.Keys;\r
+import com.gitblit.models.FederationModel;\r
+import com.gitblit.models.RepositoryModel;\r
+import com.gitblit.models.UserModel;\r
+import com.gitblit.utils.RpcUtils;\r
+\r
+public class GitblitModel implements Serializable {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       public final String url;\r
+\r
+       public final String account;\r
+\r
+       private final char[] password;\r
+\r
+       private volatile boolean isAdmin;\r
+\r
+       private volatile IStoredSettings settings;\r
+\r
+       private final List<RepositoryModel> allRepositories;\r
+\r
+       private final List<UserModel> allUsers;\r
+\r
+       private final List<FederationModel> federationRegistrations;\r
+\r
+       public GitblitModel(String url, String account, char[] password) {\r
+               this.url = url;\r
+               this.account = account;\r
+               this.password = password;\r
+\r
+               this.allUsers = new ArrayList<UserModel>();\r
+               this.allRepositories = new ArrayList<RepositoryModel>();\r
+               this.federationRegistrations = new ArrayList<FederationModel>();\r
+       }\r
+\r
+       public void login() throws IOException {\r
+               refreshRepositories();\r
+\r
+               try {\r
+                       settings = RpcUtils.getSettings(url, account, password);\r
+                       refreshUsers();\r
+                       refreshFederationRegistrations();\r
+                       isAdmin = true;\r
+               } catch (ForbiddenException e) {\r
+               } catch (IOException e) {\r
+                       System.err.println(e.getMessage());\r
+               }\r
+       }\r
+\r
+       public boolean allowAdmin() {\r
+               return isAdmin;\r
+       }\r
+\r
+       public boolean isOwner(RepositoryModel model) {\r
+               return account.equalsIgnoreCase(model.owner);\r
+       }\r
+\r
+       public IStoredSettings getSettings() {\r
+               return settings;\r
+       }\r
+\r
+       public List<RepositoryModel> refreshRepositories() throws IOException {\r
+               Map<String, RepositoryModel> repositories = RpcUtils\r
+                               .getRepositories(url, account, password);\r
+               allRepositories.clear();\r
+               allRepositories.addAll(repositories.values());\r
+               Collections.sort(allRepositories);\r
+               return allRepositories;\r
+       }\r
+\r
+       public List<UserModel> refreshUsers() throws IOException {\r
+               List<UserModel> users = RpcUtils.getUsers(url, account, password);\r
+               allUsers.clear();\r
+               allUsers.addAll(users);\r
+               return allUsers;\r
+       }\r
+\r
+       public List<FederationModel> refreshFederationRegistrations() throws IOException {\r
+               List<FederationModel> list = RpcUtils.getFederationRegistrations(url, account, password);\r
+               federationRegistrations.clear();\r
+               federationRegistrations.addAll(list);\r
+               return federationRegistrations;\r
+       }\r
+\r
+       public List<UserModel> getUsers() {\r
+               return allUsers;\r
+       }\r
+\r
+       public List<String> getUsernames() {\r
+               List<String> usernames = new ArrayList<String>();\r
+               for (UserModel user : this.allUsers) {\r
+                       usernames.add(user.username);\r
+               }\r
+               Collections.sort(usernames);\r
+               return usernames;\r
+       }\r
+\r
+       public List<String> getPermittedUsernames(RepositoryModel repository) {\r
+               List<String> usernames = new ArrayList<String>();\r
+               for (UserModel user : this.allUsers) {\r
+                       if (user.repositories.contains(repository.name)) {\r
+                               usernames.add(user.username);\r
+                       }\r
+               }\r
+               return usernames;\r
+       }\r
+\r
+       public List<String> getFederationSets() {\r
+               return settings.getStrings(Keys.federation.sets);\r
+       }\r
+\r
+       public List<RepositoryModel> getRepositories() {\r
+               return allRepositories;\r
+       }\r
+\r
+       public boolean createRepository(RepositoryModel repository, List<String> permittedUsers)\r
+                       throws IOException {\r
+               boolean success = true;\r
+               success &= RpcUtils.createRepository(repository, url, account, password);\r
+               if (permittedUsers.size() > 0) {\r
+                       // if new repository has named members, set them\r
+                       success &= RpcUtils.setRepositoryMembers(repository, permittedUsers, url, account,\r
+                                       password);\r
+               }\r
+               return success;\r
+       }\r
+\r
+       public boolean updateRepository(String name, RepositoryModel repository,\r
+                       List<String> permittedUsers) throws IOException {\r
+               boolean success = true;\r
+               success &= RpcUtils.updateRepository(name, repository, url, account, password);\r
+               // always set the repository members\r
+               success &= RpcUtils\r
+                               .setRepositoryMembers(repository, permittedUsers, url, account, password);\r
+               return success;\r
+       }\r
+\r
+       public boolean deleteRepository(RepositoryModel repository) throws IOException {\r
+               return RpcUtils.deleteRepository(repository, url, account, password);\r
+       }\r
+\r
+       public boolean createUser(UserModel user) throws IOException {\r
+               return RpcUtils.createUser(user, url, account, password);\r
+       }\r
+\r
+       public boolean updateUser(String name, UserModel user) throws IOException {\r
+               return RpcUtils.updateUser(name, user, url, account, password);\r
+       }\r
+\r
+       public boolean deleteUser(UserModel user) throws IOException {\r
+               return RpcUtils.deleteUser(user, url, account, password);\r
+       }\r
+}\r
index a6be15694274027803aa8be5bcbd1dcb645d8dcb..05ba415bd49fe66ac8d7202628e695b784cb1484 100644 (file)
@@ -28,10 +28,8 @@ import java.io.IOException;
 import java.net.URI;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
-import java.util.Collections;\r
 import java.util.Date;\r
 import java.util.List;\r
-import java.util.Map;\r
 \r
 import javax.swing.JButton;\r
 import javax.swing.JLabel;\r
@@ -44,25 +42,16 @@ import javax.swing.JTable;
 import javax.swing.JTextField;\r
 import javax.swing.RowFilter;\r
 import javax.swing.SwingConstants;\r
-import javax.swing.SwingWorker;\r
 import javax.swing.event.ListSelectionEvent;\r
 import javax.swing.event.ListSelectionListener;\r
 import javax.swing.table.DefaultTableCellRenderer;\r
-import javax.swing.table.DefaultTableColumnModel;\r
 import javax.swing.table.TableCellRenderer;\r
-import javax.swing.table.TableColumn;\r
 import javax.swing.table.TableRowSorter;\r
 \r
 import com.gitblit.Constants.RpcRequest;\r
-import com.gitblit.GitBlitException.ForbiddenException;\r
-import com.gitblit.GitBlitException.UnauthorizedException;\r
-import com.gitblit.IStoredSettings;\r
-import com.gitblit.Keys;\r
 import com.gitblit.client.ClosableTabComponent.CloseTabListener;\r
-import com.gitblit.models.FederationModel;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
-import com.gitblit.utils.RpcUtils;\r
 import com.gitblit.utils.StringUtils;\r
 \r
 /**\r
@@ -80,17 +69,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
        private final Insets insets = new Insets(margin, margin, margin, margin);\r
 \r
-       private final String url;\r
-\r
-       private final String account;\r
-\r
-       private final char[] password;\r
-\r
-       private volatile boolean isAdmin;\r
-\r
-       private volatile List<UserModel> allUsers;\r
-\r
-       private volatile IStoredSettings settings;\r
+       private GitblitModel gitblit;\r
 \r
        private JTabbedPane tabs;\r
 \r
@@ -116,23 +95,19 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
        private TableRowSorter<RepositoriesModel> defaultSorter;\r
 \r
-       private List<RepositoryModel> allRepositories;\r
-\r
        public GitblitPanel(GitblitRegistration reg) {\r
                this(reg.url, reg.account, reg.password);\r
        }\r
 \r
        public GitblitPanel(String url, String account, char[] password) {\r
-               this.url = url;\r
-               this.account = account;\r
-               this.password = password;\r
+               this.gitblit = new GitblitModel(url, account, password);\r
 \r
                final JButton browseRepository = new JButton(Translation.get("gb.browse"));\r
                browseRepository.setEnabled(false);\r
                browseRepository.addActionListener(new ActionListener() {\r
                        public void actionPerformed(ActionEvent e) {\r
                                RepositoryModel model = getSelectedRepositories().get(0);\r
-                               String u = MessageFormat.format("{0}/summary/{1}", GitblitPanel.this.url,\r
+                               String u = MessageFormat.format("{0}/summary/{1}", gitblit.url,\r
                                                StringUtils.encodeURL(model.name));\r
                                try {\r
                                        Desktop.getDesktop().browse(new URI(u));\r
@@ -145,15 +120,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                JButton refreshRepositories = new JButton(Translation.get("gb.refresh"));\r
                refreshRepositories.addActionListener(new ActionListener() {\r
                        public void actionPerformed(ActionEvent e) {\r
-                               try {\r
-                                       refreshRepositoriesTable();\r
-                               } catch (ForbiddenException x) {\r
-                                       explainForbidden(RpcRequest.LIST_REPOSITORIES);\r
-                               } catch (UnauthorizedException x) {\r
-                                       explainUnauthorized(RpcRequest.LIST_REPOSITORIES);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                               refreshRepositories();\r
                        }\r
                });\r
 \r
@@ -237,7 +204,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                                        RepositoryModel model = ((RepositoriesModel) repositoriesTable.getModel()).list\r
                                                        .get(modelRow);\r
                                        editRepository.setEnabled(singleSelection\r
-                                                       && (isAdmin || model.owner.equalsIgnoreCase(GitblitPanel.this.account)));\r
+                                                       && (gitblit.allowAdmin() || gitblit.isOwner(model)));\r
                                } else {\r
                                        editRepository.setEnabled(false);\r
                                }\r
@@ -260,6 +227,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                tablePanel.add(new JScrollPane(repositoriesTable), BorderLayout.CENTER);\r
 \r
                JPanel repositoryControls = new JPanel();\r
+               repositoryControls.add(refreshRepositories);\r
                repositoryControls.add(browseRepository);\r
                repositoryControls.add(cloneRepository);\r
                repositoryControls.add(createRepository);\r
@@ -275,15 +243,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                JButton refreshUsers = new JButton(Translation.get("gb.refresh"));\r
                refreshUsers.addActionListener(new ActionListener() {\r
                        public void actionPerformed(ActionEvent e) {\r
-                               try {\r
-                                       refreshUsersTable();\r
-                               } catch (ForbiddenException x) {\r
-                                       explainForbidden(RpcRequest.LIST_USERS);\r
-                               } catch (UnauthorizedException x) {\r
-                                       explainUnauthorized(RpcRequest.LIST_USERS);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                               refreshUsers();\r
                        }\r
                });\r
 \r
@@ -360,15 +320,20 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                return label;\r
        }\r
 \r
+       private void setRenderer(RepositoriesModel.Columns col, TableCellRenderer renderer) {\r
+               String name = repositoriesTable.getColumnName(col.ordinal());\r
+               repositoriesTable.getColumn(name).setCellRenderer(renderer);\r
+       }\r
+\r
        public void login() throws IOException {\r
-               refreshRepositoriesTable();\r
-\r
-               try {\r
-                       refreshUsersTable();\r
-                       refreshSettings();\r
-                       isAdmin = true;\r
-                       refreshFederationPanel();\r
-               } catch (ForbiddenException e) {\r
+               gitblit.login();\r
+\r
+               updateRepositoriesTable();\r
+               Utils.packColumns(repositoriesTable, 2);\r
+\r
+               if (gitblit.allowAdmin()) {\r
+                       updateUsersTable();\r
+               } else {\r
                        // user does not have administrator privileges\r
                        // hide admin repository buttons\r
                        createRepository.setVisible(false);\r
@@ -379,39 +344,17 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
                        // remove federation tab\r
                        tabs.removeTabAt(1);\r
-               } catch (IOException e) {\r
-                       System.err.println(e.getMessage());\r
                }\r
        }\r
 \r
-       private void refreshRepositoriesTable() throws IOException {\r
-               Map<String, RepositoryModel> repositories = RpcUtils\r
-                               .getRepositories(url, account, password);\r
-               allRepositories = new ArrayList<RepositoryModel>(repositories.values());\r
-               Collections.sort(allRepositories);\r
+       private void updateRepositoriesTable() {\r
                repositoriesModel.list.clear();\r
-               repositoriesModel.list.addAll(allRepositories);\r
+               repositoriesModel.list.addAll(gitblit.getRepositories());\r
                repositoriesModel.fireTableDataChanged();\r
-               packColumns(repositoriesTable, 2);\r
        }\r
 \r
-       private void setRenderer(RepositoriesModel.Columns col, TableCellRenderer renderer) {\r
-               String name = repositoriesTable.getColumnName(col.ordinal());\r
-               repositoriesTable.getColumn(name).setCellRenderer(renderer);\r
-       }\r
-\r
-       private void refreshUsersTable() throws IOException {\r
-               allUsers = RpcUtils.getUsers(url, account, password);\r
-               usersList.setListData(allUsers.toArray());\r
-       }\r
-\r
-       private void refreshSettings() throws IOException {\r
-               settings = RpcUtils.getSettings(url, account, password);\r
-       }\r
-\r
-       private void refreshFederationPanel() throws IOException {\r
-               List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,\r
-                               password);\r
+       private void updateUsersTable() {\r
+               usersList.setListData(gitblit.getUsers().toArray());\r
        }\r
 \r
        private void filterRepositories(final String fragment) {\r
@@ -455,45 +398,6 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                return users;\r
        }\r
 \r
-       private void packColumns(JTable table, int margin) {\r
-               for (int c = 0; c < table.getColumnCount(); c++) {\r
-                       packColumn(table, c, 4);\r
-               }\r
-       }\r
-\r
-       // Sets the preferred width of the visible column specified by vColIndex.\r
-       // The column will be just wide enough to show the column head and the\r
-       // widest cell in the column. margin pixels are added to the left and right\r
-       // (resulting in an additional width of 2*margin pixels).\r
-       private void packColumn(JTable table, int vColIndex, int margin) {\r
-               DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();\r
-               TableColumn col = colModel.getColumn(vColIndex);\r
-               int width = 0;\r
-\r
-               // Get width of column header\r
-               TableCellRenderer renderer = col.getHeaderRenderer();\r
-               if (renderer == null) {\r
-                       renderer = table.getTableHeader().getDefaultRenderer();\r
-               }\r
-               Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false,\r
-                               false, 0, 0);\r
-               width = comp.getPreferredSize().width;\r
-\r
-               // Get maximum width of column data\r
-               for (int r = 0; r < table.getRowCount(); r++) {\r
-                       renderer = table.getCellRenderer(r, vColIndex);\r
-                       comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex),\r
-                                       false, false, r, vColIndex);\r
-                       width = Math.max(width, comp.getPreferredSize().width);\r
-               }\r
-\r
-               // Add margin\r
-               width += 2 * margin;\r
-\r
-               // Set the width\r
-               col.setPreferredWidth(width);\r
-       }\r
-\r
        @Override\r
        public Insets getInsets() {\r
                return insets;\r
@@ -501,7 +405,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
        @Override\r
        public Dimension getPreferredSize() {\r
-               if (isAdmin) {\r
+               if (gitblit.allowAdmin()) {\r
                        return new Dimension(950, 550);\r
                }\r
                return new Dimension(775, 450);\r
@@ -509,6 +413,23 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
        @Override\r
        public void closeTab(Component c) {\r
+               gitblit = null;\r
+       }\r
+\r
+       protected void refreshRepositories() {\r
+               GitblitWorker worker = new GitblitWorker(GitblitPanel.this, RpcRequest.LIST_REPOSITORIES) {\r
+                       @Override\r
+                       protected Boolean doRequest() throws IOException {\r
+                               gitblit.refreshRepositories();\r
+                               return true;\r
+                       }\r
+\r
+                       @Override\r
+                       protected void onSuccess() {\r
+                               updateRepositoriesTable();\r
+                       }\r
+               };\r
+               worker.execute();\r
        }\r
 \r
        /**\r
@@ -517,14 +438,9 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
         * \r
         */\r
        protected void createRepository() {\r
-               List<String> usernames = new ArrayList<String>();\r
-               for (UserModel user : this.allUsers) {\r
-                       usernames.add(user.username);\r
-               }\r
-               Collections.sort(usernames);\r
                EditRepositoryDialog dialog = new EditRepositoryDialog();\r
-               dialog.setUsers(null, usernames, null);\r
-               dialog.setRepositories(allRepositories);\r
+               dialog.setUsers(null, gitblit.getUsernames(), null);\r
+               dialog.setRepositories(gitblit.getRepositories());\r
                dialog.setVisible(true);\r
                final RepositoryModel newRepository = dialog.getRepository();\r
                final List<String> permittedUsers = dialog.getPermittedUsers();\r
@@ -532,44 +448,30 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                        return;\r
                }\r
 \r
-               final RpcRequest request = RpcRequest.CREATE_REPOSITORY;\r
-               SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+               GitblitWorker worker = new GitblitWorker(this, RpcRequest.CREATE_REPOSITORY) {\r
 \r
                        @Override\r
-                       protected Boolean doInBackground() throws IOException {\r
-                               boolean success = true;\r
-                               success &= RpcUtils.createRepository(newRepository, url, account, password);\r
-                               if (permittedUsers.size() > 0) {\r
-                                       // if new repository has named members, set them\r
-                                       success &= RpcUtils.setRepositoryMembers(newRepository, permittedUsers, url,\r
-                                                       account, password);\r
+                       protected Boolean doRequest() throws IOException {\r
+                               boolean success = gitblit.createRepository(newRepository, permittedUsers);\r
+                               if (success) {\r
+                                       gitblit.refreshRepositories();\r
+                                       if (permittedUsers.size() > 0) {\r
+                                               gitblit.refreshUsers();\r
+                                       }\r
                                }\r
                                return success;\r
                        }\r
 \r
                        @Override\r
-                       protected void done() {\r
-                               try {\r
-                                       boolean success = get();\r
-                                       if (success) {\r
-                                               refreshRepositoriesTable();\r
-                                               if (permittedUsers.size() > 0) {\r
-                                                       refreshUsersTable();\r
-                                               }\r
-                                       } else {\r
-                                               String msg = MessageFormat.format(\r
-                                                               "Failed to execute request \"{0}\" for repository \"{1}\".",\r
-                                                               request.name(), newRepository.name);\r
-                                               JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                               Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                       }\r
-                               } catch (ForbiddenException e) {\r
-                                       explainForbidden(request);\r
-                               } catch (UnauthorizedException e) {\r
-                                       explainUnauthorized(request);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                       protected void onSuccess() {\r
+                               updateRepositoriesTable();\r
+                               updateUsersTable();\r
+                       }\r
+\r
+                       @Override\r
+                       protected void onFailure() {\r
+                               showFailure("Failed to execute request \"{0}\" for repository \"{1}\".",\r
+                                               getRequestType(), newRepository.name);\r
                        }\r
                };\r
                worker.execute();\r
@@ -583,18 +485,10 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
         */\r
        protected void editRepository(final RepositoryModel repository) {\r
                EditRepositoryDialog dialog = new EditRepositoryDialog(repository);\r
-               List<String> members = new ArrayList<String>();\r
-               List<String> usernames = new ArrayList<String>();\r
-               for (UserModel user : this.allUsers) {\r
-                       usernames.add(user.username);\r
-                       if (user.repositories.contains(repository.name)) {\r
-                               members.add(user.username);\r
-                       }\r
-               }\r
-               Collections.sort(usernames);\r
+               List<String> usernames = gitblit.getUsernames();\r
+               List<String> members = gitblit.getPermittedUsernames(repository);\r
                dialog.setUsers(repository.owner, usernames, members);\r
-               dialog.setFederationSets(settings.getStrings(Keys.federation.sets),\r
-                               repository.federationSets);\r
+               dialog.setFederationSets(gitblit.getFederationSets(), repository.federationSets);\r
                dialog.setVisible(true);\r
                final RepositoryModel revisedRepository = dialog.getRepository();\r
                final List<String> permittedUsers = dialog.getPermittedUsers();\r
@@ -602,41 +496,29 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                        return;\r
                }\r
 \r
-               final RpcRequest request = RpcRequest.EDIT_REPOSITORY;\r
-               SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+               GitblitWorker worker = new GitblitWorker(this, RpcRequest.EDIT_REPOSITORY) {\r
 \r
                        @Override\r
-                       protected Boolean doInBackground() throws IOException {\r
-                               boolean success = true;\r
-                               success &= RpcUtils.updateRepository(repository.name, revisedRepository, url,\r
-                                               account, password);\r
-                               // always set the repository members\r
-                               success &= RpcUtils.setRepositoryMembers(repository, permittedUsers, url, account,\r
-                                               password);\r
+                       protected Boolean doRequest() throws IOException {\r
+                               boolean success = gitblit.updateRepository(repository.name, revisedRepository,\r
+                                               permittedUsers);\r
+                               if (success) {\r
+                                       gitblit.refreshRepositories();\r
+                                       gitblit.refreshUsers();\r
+                               }\r
                                return success;\r
                        }\r
 \r
                        @Override\r
-                       protected void done() {\r
-                               try {\r
-                                       boolean success = get();\r
-                                       if (success) {\r
-                                               refreshRepositoriesTable();\r
-                                               refreshUsersTable();\r
-                                       } else {\r
-                                               String msg = MessageFormat.format(\r
-                                                               "Failed to execute request \"{0}\" for repository \"{1}\".",\r
-                                                               request.name(), repository.name);\r
-                                               JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                               Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                       }\r
-                               } catch (ForbiddenException e) {\r
-                                       explainForbidden(request);\r
-                               } catch (UnauthorizedException e) {\r
-                                       explainUnauthorized(request);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                       protected void onSuccess() {\r
+                               updateRepositoriesTable();\r
+                               updateUsersTable();\r
+                       }\r
+\r
+                       @Override\r
+                       protected void onFailure() {\r
+                               showFailure("Failed to execute request \"{0}\" for repository \"{1}\".",\r
+                                               getRequestType(), repository.name);\r
                        }\r
                };\r
                worker.execute();\r
@@ -653,84 +535,85 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                int result = JOptionPane.showConfirmDialog(GitblitPanel.this, message.toString(),\r
                                "Delete Repositories?", JOptionPane.YES_NO_OPTION);\r
                if (result == JOptionPane.YES_OPTION) {\r
-                       final RpcRequest request = RpcRequest.DELETE_REPOSITORY;\r
-                       SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+                       GitblitWorker worker = new GitblitWorker(this, RpcRequest.DELETE_REPOSITORY) {\r
                                @Override\r
-                               protected Boolean doInBackground() throws Exception {\r
+                               protected Boolean doRequest() throws IOException {\r
                                        boolean success = true;\r
                                        for (RepositoryModel repository : repositories) {\r
-                                               success &= RpcUtils.deleteRepository(repository, url, account, password);\r
+                                               success &= gitblit.deleteRepository(repository);\r
+                                       }\r
+                                       if (success) {\r
+                                               gitblit.refreshUsers();\r
                                        }\r
                                        return success;\r
                                }\r
 \r
                                @Override\r
-                               protected void done() {\r
-                                       try {\r
-                                               boolean success = get();\r
-                                               if (success) {\r
-                                                       refreshRepositoriesTable();\r
-                                               } else {\r
-                                                       String msg = "Failed to delete specified repositories!";\r
-                                                       JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                                       Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                               }\r
-                                       } catch (ForbiddenException e) {\r
-                                               explainForbidden(request);\r
-                                       } catch (UnauthorizedException e) {\r
-                                               explainUnauthorized(request);\r
-                                       } catch (Throwable t) {\r
-                                               showException(t);\r
-                                       }\r
+                               protected void onSuccess() {\r
+                                       updateRepositoriesTable();\r
+                                       updateUsersTable();\r
+                               }\r
+\r
+                               @Override\r
+                               protected void onFailure() {\r
+                                       showFailure("Failed to delete specified repositories!");\r
                                }\r
                        };\r
                        worker.execute();\r
                }\r
        }\r
 \r
+       protected void refreshUsers() {\r
+               GitblitWorker worker = new GitblitWorker(GitblitPanel.this, RpcRequest.LIST_USERS) {\r
+                       @Override\r
+                       protected Boolean doRequest() throws IOException {\r
+                               gitblit.refreshUsers();\r
+                               return true;\r
+                       }\r
+\r
+                       @Override\r
+                       protected void onSuccess() {\r
+                               updateUsersTable();\r
+                       }\r
+               };\r
+               worker.execute();\r
+       }\r
+\r
        /**\r
         * Displays the create user dialog and fires a SwingWorker to update the\r
         * server, if appropriate.\r
         * \r
         */\r
        protected void createUser() {\r
-               EditUserDialog dialog = new EditUserDialog(settings);\r
-               dialog.setUsers(allUsers);\r
-               dialog.setRepositories(allRepositories, null);\r
+               EditUserDialog dialog = new EditUserDialog(gitblit.getSettings());\r
+               dialog.setUsers(gitblit.getUsers());\r
+               dialog.setRepositories(gitblit.getRepositories(), null);\r
                dialog.setVisible(true);\r
                final UserModel newUser = dialog.getUser();\r
                if (newUser == null) {\r
                        return;\r
                }\r
 \r
-               final RpcRequest request = RpcRequest.CREATE_USER;\r
-               SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+               GitblitWorker worker = new GitblitWorker(this, RpcRequest.CREATE_USER) {\r
 \r
                        @Override\r
-                       protected Boolean doInBackground() throws IOException {\r
-                               return RpcUtils.createUser(newUser, url, account, password);\r
+                       protected Boolean doRequest() throws IOException {\r
+                               boolean success = gitblit.createUser(newUser);\r
+                               if (success) {\r
+                                       gitblit.refreshUsers();\r
+                               }\r
+                               return success;\r
                        }\r
 \r
                        @Override\r
-                       protected void done() {\r
-                               try {\r
-                                       boolean success = get();\r
-                                       if (success) {\r
-                                               refreshUsersTable();\r
-                                       } else {\r
-                                               String msg = MessageFormat.format(\r
-                                                               "Failed to execute request \"{0}\" for user \"{1}\".",\r
-                                                               request.name(), newUser.username);\r
-                                               JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                               Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                       }\r
-                               } catch (ForbiddenException e) {\r
-                                       explainForbidden(request);\r
-                               } catch (UnauthorizedException e) {\r
-                                       explainUnauthorized(request);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                       protected void onSuccess() {\r
+                               updateUsersTable();\r
+                       }\r
+\r
+                       @Override\r
+                       protected void onFailure() {\r
+                               showFailure("Failed to execute request \"{0}\" for user \"{1}\".",\r
+                                               getRequestType(), newUser.username);\r
                        }\r
                };\r
                worker.execute();\r
@@ -743,42 +626,33 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
         * @param user\r
         */\r
        protected void editUser(final UserModel user) {\r
-               EditUserDialog dialog = new EditUserDialog(user, settings);\r
-               dialog.setRepositories(allRepositories, new ArrayList<String>(user.repositories));\r
+               EditUserDialog dialog = new EditUserDialog(user, gitblit.getSettings());\r
+               dialog.setRepositories(gitblit.getRepositories(), new ArrayList<String>(user.repositories));\r
                dialog.setVisible(true);\r
                final UserModel revisedUser = dialog.getUser();\r
                if (revisedUser == null) {\r
                        return;\r
                }\r
 \r
-               final RpcRequest request = RpcRequest.EDIT_USER;\r
-               SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+               GitblitWorker worker = new GitblitWorker(this, RpcRequest.EDIT_USER) {\r
+                       @Override\r
+                       protected Boolean doRequest() throws IOException {\r
+                               boolean success = gitblit.updateUser(user.username, revisedUser);\r
+                               if (success) {\r
+                                       gitblit.refreshUsers();\r
+                               }\r
+                               return success;\r
+                       }\r
 \r
                        @Override\r
-                       protected Boolean doInBackground() throws IOException {\r
-                               return RpcUtils.updateUser(user.username, revisedUser, url, account, password);\r
+                       protected void onSuccess() {\r
+                               updateUsersTable();\r
                        }\r
 \r
                        @Override\r
-                       protected void done() {\r
-                               try {\r
-                                       boolean success = get();\r
-                                       if (success) {\r
-                                               refreshUsersTable();\r
-                                       } else {\r
-                                               String msg = MessageFormat.format(\r
-                                                               "Failed to execute request \"{0}\" for user \"{1}\".",\r
-                                                               request.name(), user.username);\r
-                                               JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                               Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                       }\r
-                               } catch (ForbiddenException e) {\r
-                                       explainForbidden(request);\r
-                               } catch (UnauthorizedException e) {\r
-                                       explainUnauthorized(request);\r
-                               } catch (Throwable t) {\r
-                                       showException(t);\r
-                               }\r
+                       protected void onFailure() {\r
+                               showFailure("Failed to execute request \"{0}\" for user \"{1}\".",\r
+                                               getRequestType(), user.username);\r
                        }\r
                };\r
                worker.execute();\r
@@ -795,58 +669,30 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                int result = JOptionPane.showConfirmDialog(GitblitPanel.this, message.toString(),\r
                                "Delete Users?", JOptionPane.YES_NO_OPTION);\r
                if (result == JOptionPane.YES_OPTION) {\r
-                       final RpcRequest request = RpcRequest.DELETE_USER;\r
-                       SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {\r
+                       GitblitWorker worker = new GitblitWorker(this, RpcRequest.DELETE_USER) {\r
                                @Override\r
-                               protected Boolean doInBackground() throws Exception {\r
+                               protected Boolean doRequest() throws IOException {\r
                                        boolean success = true;\r
                                        for (UserModel user : users) {\r
-                                               success &= RpcUtils.deleteUser(user, url, account, password);\r
+                                               success &= gitblit.deleteUser(user);\r
+                                       }\r
+                                       if (success) {\r
+                                               gitblit.refreshUsers();\r
                                        }\r
                                        return success;\r
                                }\r
 \r
                                @Override\r
-                               protected void done() {\r
-                                       try {\r
-                                               boolean success = get();\r
-                                               if (success) {\r
-                                                       refreshUsersTable();\r
-                                               } else {\r
-                                                       String msg = "Failed to delete specified users!";\r
-                                                       JOptionPane.showMessageDialog(GitblitPanel.this, msg,\r
-                                                                       Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);\r
-                                               }\r
-                                       } catch (ForbiddenException e) {\r
-                                               explainForbidden(request);\r
-                                       } catch (UnauthorizedException e) {\r
-                                               explainUnauthorized(request);\r
-                                       } catch (Throwable t) {\r
-                                               showException(t);\r
-                                       }\r
+                               protected void onSuccess() {\r
+                                       updateUsersTable();\r
+                               }\r
+\r
+                               @Override\r
+                               protected void onFailure() {\r
+                                       showFailure("Failed to delete specified users!");\r
                                }\r
                        };\r
                        worker.execute();\r
                }\r
        }\r
-\r
-       private void explainForbidden(RpcRequest request) {\r
-               String msg = MessageFormat.format(\r
-                               "The request \"{0}\" has been forbidden by the Gitblit server @ {1}.",\r
-                               request.name(), url);\r
-               JOptionPane.showMessageDialog(GitblitPanel.this, msg, "Forbidden",\r
-                               JOptionPane.ERROR_MESSAGE);\r
-       }\r
-\r
-       private void explainUnauthorized(RpcRequest request) {\r
-               String msg = MessageFormat.format(\r
-                               "The account \"{0}\" is not authorized to execute the request \"{1}\".", account,\r
-                               request.name());\r
-               JOptionPane.showMessageDialog(GitblitPanel.this, msg, "Unauthorized",\r
-                               JOptionPane.ERROR_MESSAGE);\r
-       }\r
-\r
-       private void showException(Throwable t) {\r
-               // TODO show the unexpected exception\r
-       }\r
-}\r
+}
\ No newline at end of file
diff --git a/src/com/gitblit/client/GitblitWorker.java b/src/com/gitblit/client/GitblitWorker.java
new file mode 100644 (file)
index 0000000..45baf03
--- /dev/null
@@ -0,0 +1,80 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.client;\r
+\r
+import java.awt.Component;\r
+import java.io.IOException;\r
+import java.text.MessageFormat;\r
+\r
+import javax.swing.JOptionPane;\r
+import javax.swing.SwingWorker;\r
+\r
+import com.gitblit.Constants.RpcRequest;\r
+import com.gitblit.GitBlitException.ForbiddenException;\r
+import com.gitblit.GitBlitException.UnauthorizedException;\r
+\r
+public abstract class GitblitWorker extends SwingWorker<Boolean, Void> {\r
+\r
+       private final Component parent;\r
+\r
+       private final RpcRequest request;\r
+\r
+       public GitblitWorker(Component parent, RpcRequest request) {\r
+               this.parent = parent;\r
+               this.request = request;\r
+       }\r
+\r
+       protected RpcRequest getRequestType() {\r
+               return request;\r
+       }\r
+\r
+       @Override\r
+       protected Boolean doInBackground() throws IOException {\r
+               return doRequest();\r
+       }\r
+\r
+       protected void done() {\r
+               try {\r
+                       Boolean success = get();\r
+                       if (success) {\r
+                               onSuccess();\r
+                       } else {\r
+                               onFailure();\r
+                       }\r
+               } catch (Throwable t) {\r
+                       if (t instanceof ForbiddenException) {\r
+                               Utils.explainForbidden(parent, request);\r
+                       } else if (t instanceof UnauthorizedException) {\r
+                               Utils.explainUnauthorized(parent, request);\r
+                       } else {\r
+                               Utils.showException(parent, t);\r
+                       }\r
+               }\r
+       }\r
+\r
+       protected abstract Boolean doRequest() throws IOException;\r
+\r
+       protected abstract void onSuccess();\r
+\r
+       protected void onFailure() {\r
+       }\r
+\r
+       protected void showFailure(String message, Object... args) {\r
+               String msg = MessageFormat.format(message, args);\r
+               JOptionPane.showMessageDialog(parent, msg, Translation.get("gb.error"),\r
+                               JOptionPane.ERROR_MESSAGE);\r
+       }\r
+}\r
diff --git a/src/com/gitblit/client/Utils.java b/src/com/gitblit/client/Utils.java
new file mode 100644 (file)
index 0000000..c33c110
--- /dev/null
@@ -0,0 +1,85 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.client;\r
+\r
+import java.awt.Component;\r
+import java.text.MessageFormat;\r
+\r
+import javax.swing.JOptionPane;\r
+import javax.swing.JTable;\r
+import javax.swing.table.DefaultTableColumnModel;\r
+import javax.swing.table.TableCellRenderer;\r
+import javax.swing.table.TableColumn;\r
+\r
+import com.gitblit.Constants.RpcRequest;\r
+\r
+public class Utils {\r
+\r
+       public static void explainForbidden(Component c, RpcRequest request) {\r
+               String msg = MessageFormat.format(\r
+                               "The request \"{0}\" has been forbidden by the Gitblit server.", request.name());\r
+               JOptionPane.showMessageDialog(c, msg, "Forbidden", JOptionPane.ERROR_MESSAGE);\r
+       }\r
+\r
+       public static void explainUnauthorized(Component c, RpcRequest request) {\r
+               String msg = MessageFormat.format(\r
+                               "This account is not authorized to execute the request \"{0}\".", request.name());\r
+               JOptionPane.showMessageDialog(c, msg, "Unauthorized", JOptionPane.ERROR_MESSAGE);\r
+       }\r
+\r
+       public static void showException(Component c, Throwable t) {\r
+               // TODO show the unexpected exception\r
+       }\r
+\r
+       public static void packColumns(JTable table, int margin) {\r
+               for (int c = 0; c < table.getColumnCount(); c++) {\r
+                       packColumn(table, c, 4);\r
+               }\r
+       }\r
+\r
+       // Sets the preferred width of the visible column specified by vColIndex.\r
+       // The column will be just wide enough to show the column head and the\r
+       // widest cell in the column. margin pixels are added to the left and right\r
+       // (resulting in an additional width of 2*margin pixels).\r
+       private static void packColumn(JTable table, int vColIndex, int margin) {\r
+               DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();\r
+               TableColumn col = colModel.getColumn(vColIndex);\r
+               int width = 0;\r
+\r
+               // Get width of column header\r
+               TableCellRenderer renderer = col.getHeaderRenderer();\r
+               if (renderer == null) {\r
+                       renderer = table.getTableHeader().getDefaultRenderer();\r
+               }\r
+               Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false,\r
+                               false, 0, 0);\r
+               width = comp.getPreferredSize().width;\r
+\r
+               // Get maximum width of column data\r
+               for (int r = 0; r < table.getRowCount(); r++) {\r
+                       renderer = table.getCellRenderer(r, vColIndex);\r
+                       comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex),\r
+                                       false, false, r, vColIndex);\r
+                       width = Math.max(width, comp.getPreferredSize().width);\r
+               }\r
+\r
+               // Add margin\r
+               width += 2 * margin;\r
+\r
+               // Set the width\r
+               col.setPreferredWidth(width);\r
+       }\r
+}\r