]> source.dussan.org Git - gitblit.git/commitdiff
Skeleton Gitblit Client app using the json rpc api.
authorJames Moger <james.moger@gitblit.com>
Sun, 2 Oct 2011 20:56:34 +0000 (16:56 -0400)
committerJames Moger <james.moger@gitblit.com>
Sun, 2 Oct 2011 20:56:34 +0000 (16:56 -0400)
src/com/gitblit/client/DateCellRenderer.java [new file with mode: 0644]
src/com/gitblit/client/GitblitClient.java [new file with mode: 0644]
src/com/gitblit/client/GitblitPanel.java [new file with mode: 0644]
src/com/gitblit/client/RepositoriesModel.java [new file with mode: 0644]

diff --git a/src/com/gitblit/client/DateCellRenderer.java b/src/com/gitblit/client/DateCellRenderer.java
new file mode 100644 (file)
index 0000000..591926b
--- /dev/null
@@ -0,0 +1,44 @@
+/*\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.SimpleDateFormat;\r
+import java.util.Date;\r
+\r
+import javax.swing.JTable;\r
+import javax.swing.table.DefaultTableCellRenderer;\r
+\r
+public class DateCellRenderer extends DefaultTableCellRenderer {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       private final String pattern;\r
+\r
+       public DateCellRenderer(String pattern) {\r
+               this.pattern = (pattern == null ? "yyyy-MM-dd HH:mm" : pattern);\r
+       }\r
+\r
+       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,\r
+                       boolean hasFocus, int row, int column) {\r
+               super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);\r
+               if (value instanceof Date) {\r
+                       String strDate = new SimpleDateFormat(pattern).format((Date) value);\r
+                       this.setText(strDate);\r
+               }\r
+               return this;\r
+       }\r
+}
\ No newline at end of file
diff --git a/src/com/gitblit/client/GitblitClient.java b/src/com/gitblit/client/GitblitClient.java
new file mode 100644 (file)
index 0000000..d10cede
--- /dev/null
@@ -0,0 +1,103 @@
+/*\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.BorderLayout;\r
+import java.awt.EventQueue;\r
+import java.awt.Menu;\r
+import java.awt.MenuBar;\r
+import java.awt.MenuItem;\r
+import java.awt.MenuShortcut;\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.KeyEvent;\r
+import java.io.IOException;\r
+\r
+import javax.swing.JFrame;\r
+import javax.swing.JOptionPane;\r
+import javax.swing.JPanel;\r
+import javax.swing.JTabbedPane;\r
+\r
+import com.gitblit.Constants;\r
+import com.gitblit.utils.StringUtils;\r
+\r
+public class GitblitClient extends JFrame {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+       private JTabbedPane serverTabs;\r
+\r
+       private GitblitClient() {\r
+               super();\r
+       }\r
+\r
+       private void initialize() {\r
+               setupMenu();\r
+               setContentPane(getCenterPanel());\r
+\r
+               setTitle("Gitblit Client v" + Constants.VERSION + " (" + Constants.VERSION_DATE + ")");\r
+               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r
+               setSize(800, 600);\r
+               setLocationRelativeTo(null);\r
+       }\r
+\r
+       private void setupMenu() {\r
+               MenuBar menuBar = new MenuBar();\r
+               setMenuBar(menuBar);\r
+               Menu serversMenu = new Menu("Servers");\r
+               menuBar.add(serversMenu);\r
+               MenuItem login = new MenuItem("Login...", new MenuShortcut(KeyEvent.VK_L, false));\r
+               login.addActionListener(new ActionListener() {\r
+                       public void actionPerformed(ActionEvent event) {\r
+                               String url = JOptionPane.showInputDialog(GitblitClient.this,\r
+                                               "Please enter Gitblit server URL", "https://localhost:8443");\r
+                               if (StringUtils.isEmpty(url)) {\r
+                                       return;\r
+                               }\r
+                               login(url, "admin", "admin".toCharArray());\r
+                       }\r
+               });\r
+               serversMenu.add(login);\r
+       }\r
+\r
+       private JPanel getCenterPanel() {\r
+               serverTabs = new JTabbedPane(JTabbedPane.TOP);\r
+               JPanel panel = new JPanel(new BorderLayout());\r
+               panel.add(serverTabs, BorderLayout.CENTER);\r
+               return panel;\r
+       }\r
+\r
+       private void login(String url, String account, char[] password) {\r
+               try {\r
+                       GitblitPanel panel = new GitblitPanel(url, account, password);\r
+                       panel.login();\r
+                       serverTabs.addTab(url.substring(url.indexOf("//") + 2), panel);\r
+                       serverTabs.setSelectedIndex(serverTabs.getTabCount() - 1);\r
+               } catch (IOException e) {\r
+                       JOptionPane.showMessageDialog(GitblitClient.this, e.getMessage(), "Error",\r
+                                       JOptionPane.ERROR_MESSAGE);\r
+               }\r
+       }\r
+\r
+       public static void main(String[] args) {\r
+               EventQueue.invokeLater(new Runnable() {\r
+                       public void run() {\r
+                               GitblitClient frame = new GitblitClient();\r
+                               frame.initialize();\r
+                               frame.setVisible(true);\r
+                       }\r
+               });\r
+       }\r
+}\r
diff --git a/src/com/gitblit/client/GitblitPanel.java b/src/com/gitblit/client/GitblitPanel.java
new file mode 100644 (file)
index 0000000..911ec0c
--- /dev/null
@@ -0,0 +1,112 @@
+/*\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.BorderLayout;\r
+import java.awt.Component;\r
+import java.io.IOException;\r
+import java.util.Date;\r
+import java.util.Map;\r
+\r
+import javax.swing.JPanel;\r
+import javax.swing.JScrollPane;\r
+import javax.swing.JTabbedPane;\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.models.RepositoryModel;\r
+import com.gitblit.utils.RpcUtils;\r
+\r
+public class GitblitPanel extends JPanel {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       String url;\r
+       String account;\r
+       char[] password;\r
+\r
+       JTabbedPane tabs;\r
+\r
+       private JTable repositoriesTable;\r
+\r
+       public GitblitPanel(String url, String account, char[] password) {\r
+               this.url = url;\r
+               this.account = account;\r
+               this.password = password;\r
+\r
+               tabs = new JTabbedPane(JTabbedPane.TOP);\r
+               repositoriesTable = new JTable();\r
+               repositoriesTable.setDefaultRenderer(Date.class, new DateCellRenderer(null));\r
+\r
+               tabs.addTab("Repositories", new JScrollPane(repositoriesTable));\r
+\r
+               setLayout(new BorderLayout());\r
+               add(tabs, BorderLayout.CENTER);\r
+       }\r
+\r
+       public void login() throws IOException {\r
+               refreshRepositoriesTable();\r
+       }\r
+\r
+       private void refreshRepositoriesTable() throws IOException {\r
+               Map<String, RepositoryModel> repositories = RpcUtils\r
+                               .getRepositories(url, account, password);\r
+               repositoriesTable.setModel(new RepositoriesModel(repositories));\r
+\r
+               packColumns(repositoriesTable, 2);\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
diff --git a/src/com/gitblit/client/RepositoriesModel.java b/src/com/gitblit/client/RepositoriesModel.java
new file mode 100644 (file)
index 0000000..2a439fb
--- /dev/null
@@ -0,0 +1,101 @@
+/*\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.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.table.AbstractTableModel;\r
+\r
+import com.gitblit.models.RepositoryModel;\r
+\r
+public class RepositoriesModel extends AbstractTableModel {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       Map<String, RepositoryModel> repositories;\r
+\r
+       List<RepositoryModel> list;\r
+\r
+       enum Columns {\r
+               Name, Description, Owner, Last_Change, Size;\r
+\r
+               @Override\r
+               public String toString() {\r
+                       return name().replace('_', ' ');\r
+               }\r
+       }\r
+\r
+       public RepositoriesModel(Map<String, RepositoryModel> repositories) {\r
+               this.repositories = repositories;\r
+               list = new ArrayList<RepositoryModel>(repositories.values());\r
+               Collections.sort(list);\r
+       }\r
+\r
+       @Override\r
+       public int getRowCount() {\r
+               return repositories.size();\r
+       }\r
+\r
+       @Override\r
+       public int getColumnCount() {\r
+               return Columns.values().length;\r
+       }\r
+\r
+       @Override\r
+       public String getColumnName(int column) {\r
+               Columns col = Columns.values()[column];\r
+               return col.toString();\r
+       }\r
+\r
+       /**\r
+        * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.\r
+        * \r
+        * @param columnIndex\r
+        *            the column being queried\r
+        * @return the Object.class\r
+        */\r
+       public Class<?> getColumnClass(int columnIndex) {\r
+               Columns col = Columns.values()[columnIndex];\r
+               switch (col) {\r
+               case Last_Change:\r
+                       return Date.class;\r
+               }\r
+               return String.class;\r
+       }\r
+\r
+       @Override\r
+       public Object getValueAt(int rowIndex, int columnIndex) {\r
+               RepositoryModel model = list.get(rowIndex);\r
+               Columns col = Columns.values()[columnIndex];\r
+               switch (col) {\r
+               case Name:\r
+                       return model.name;\r
+               case Description:\r
+                       return model.description;\r
+               case Owner:\r
+                       return model.owner;\r
+               case Last_Change:\r
+                       return model.lastChange;\r
+               case Size:\r
+                       return model.size;\r
+               }\r
+               return null;\r
+       }\r
+}\r