From: James Moger Date: Sun, 2 Oct 2011 20:56:34 +0000 (-0400) Subject: Skeleton Gitblit Client app using the json rpc api. X-Git-Tag: v0.7.0~96 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=19c634873b49dea8b49fc54ca393153f7eb0eb35;p=gitblit.git Skeleton Gitblit Client app using the json rpc api. --- diff --git a/src/com/gitblit/client/DateCellRenderer.java b/src/com/gitblit/client/DateCellRenderer.java new file mode 100644 index 00000000..591926b7 --- /dev/null +++ b/src/com/gitblit/client/DateCellRenderer.java @@ -0,0 +1,44 @@ +/* + * 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.Component; +import java.text.SimpleDateFormat; +import java.util.Date; + +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; + +public class DateCellRenderer extends DefaultTableCellRenderer { + + private static final long serialVersionUID = 1L; + + private final String pattern; + + public DateCellRenderer(String pattern) { + this.pattern = (pattern == null ? "yyyy-MM-dd HH:mm" : pattern); + } + + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, + boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + if (value instanceof Date) { + String strDate = new SimpleDateFormat(pattern).format((Date) value); + this.setText(strDate); + } + return this; + } +} \ 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 index 00000000..d10cedeb --- /dev/null +++ b/src/com/gitblit/client/GitblitClient.java @@ -0,0 +1,103 @@ +/* + * 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.EventQueue; +import java.awt.Menu; +import java.awt.MenuBar; +import java.awt.MenuItem; +import java.awt.MenuShortcut; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.io.IOException; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTabbedPane; + +import com.gitblit.Constants; +import com.gitblit.utils.StringUtils; + +public class GitblitClient extends JFrame { + + private static final long serialVersionUID = 1L; + private JTabbedPane serverTabs; + + private GitblitClient() { + super(); + } + + private void initialize() { + setupMenu(); + setContentPane(getCenterPanel()); + + setTitle("Gitblit Client v" + Constants.VERSION + " (" + Constants.VERSION_DATE + ")"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(800, 600); + setLocationRelativeTo(null); + } + + private void setupMenu() { + MenuBar menuBar = new MenuBar(); + setMenuBar(menuBar); + Menu serversMenu = new Menu("Servers"); + menuBar.add(serversMenu); + MenuItem login = new MenuItem("Login...", new MenuShortcut(KeyEvent.VK_L, false)); + login.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { + String url = JOptionPane.showInputDialog(GitblitClient.this, + "Please enter Gitblit server URL", "https://localhost:8443"); + if (StringUtils.isEmpty(url)) { + return; + } + login(url, "admin", "admin".toCharArray()); + } + }); + serversMenu.add(login); + } + + private JPanel getCenterPanel() { + serverTabs = new JTabbedPane(JTabbedPane.TOP); + JPanel panel = new JPanel(new BorderLayout()); + panel.add(serverTabs, BorderLayout.CENTER); + return panel; + } + + private void login(String url, String account, char[] password) { + try { + GitblitPanel panel = new GitblitPanel(url, account, password); + panel.login(); + serverTabs.addTab(url.substring(url.indexOf("//") + 2), panel); + serverTabs.setSelectedIndex(serverTabs.getTabCount() - 1); + } catch (IOException e) { + JOptionPane.showMessageDialog(GitblitClient.this, e.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + GitblitClient frame = new GitblitClient(); + frame.initialize(); + frame.setVisible(true); + } + }); + } +} diff --git a/src/com/gitblit/client/GitblitPanel.java b/src/com/gitblit/client/GitblitPanel.java new file mode 100644 index 00000000..911ec0c6 --- /dev/null +++ b/src/com/gitblit/client/GitblitPanel.java @@ -0,0 +1,112 @@ +/* + * 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.Component; +import java.io.IOException; +import java.util.Date; +import java.util.Map; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTable; +import javax.swing.table.DefaultTableColumnModel; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumn; + +import com.gitblit.models.RepositoryModel; +import com.gitblit.utils.RpcUtils; + +public class GitblitPanel extends JPanel { + + private static final long serialVersionUID = 1L; + + String url; + String account; + char[] password; + + JTabbedPane tabs; + + private JTable repositoriesTable; + + public GitblitPanel(String url, String account, char[] password) { + this.url = url; + this.account = account; + this.password = password; + + tabs = new JTabbedPane(JTabbedPane.TOP); + repositoriesTable = new JTable(); + repositoriesTable.setDefaultRenderer(Date.class, new DateCellRenderer(null)); + + tabs.addTab("Repositories", new JScrollPane(repositoriesTable)); + + setLayout(new BorderLayout()); + add(tabs, BorderLayout.CENTER); + } + + public void login() throws IOException { + refreshRepositoriesTable(); + } + + private void refreshRepositoriesTable() throws IOException { + Map repositories = RpcUtils + .getRepositories(url, account, password); + repositoriesTable.setModel(new RepositoriesModel(repositories)); + + packColumns(repositoriesTable, 2); + } + + private void packColumns(JTable table, int margin) { + for (int c = 0; c < table.getColumnCount(); c++) { + packColumn(table, c, 4); + } + } + + // Sets the preferred width of the visible column specified by vColIndex. + // The column will be just wide enough to show the column head and the + // widest cell in the column. margin pixels are added to the left and right + // (resulting in an additional width of 2*margin pixels). + private void packColumn(JTable table, int vColIndex, int margin) { + DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); + TableColumn col = colModel.getColumn(vColIndex); + int width = 0; + + // Get width of column header + TableCellRenderer renderer = col.getHeaderRenderer(); + if (renderer == null) { + renderer = table.getTableHeader().getDefaultRenderer(); + } + Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, + false, 0, 0); + width = comp.getPreferredSize().width; + + // Get maximum width of column data + for (int r = 0; r < table.getRowCount(); r++) { + renderer = table.getCellRenderer(r, vColIndex); + comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), + false, false, r, vColIndex); + width = Math.max(width, comp.getPreferredSize().width); + } + + // Add margin + width += 2 * margin; + + // Set the width + col.setPreferredWidth(width); + } +} diff --git a/src/com/gitblit/client/RepositoriesModel.java b/src/com/gitblit/client/RepositoriesModel.java new file mode 100644 index 00000000..2a439fb9 --- /dev/null +++ b/src/com/gitblit/client/RepositoriesModel.java @@ -0,0 +1,101 @@ +/* + * 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.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.swing.table.AbstractTableModel; + +import com.gitblit.models.RepositoryModel; + +public class RepositoriesModel extends AbstractTableModel { + + private static final long serialVersionUID = 1L; + + Map repositories; + + List list; + + enum Columns { + Name, Description, Owner, Last_Change, Size; + + @Override + public String toString() { + return name().replace('_', ' '); + } + } + + public RepositoriesModel(Map repositories) { + this.repositories = repositories; + list = new ArrayList(repositories.values()); + Collections.sort(list); + } + + @Override + public int getRowCount() { + return repositories.size(); + } + + @Override + public int getColumnCount() { + return Columns.values().length; + } + + @Override + public String getColumnName(int column) { + Columns col = Columns.values()[column]; + return col.toString(); + } + + /** + * Returns Object.class regardless of columnIndex. + * + * @param columnIndex + * the column being queried + * @return the Object.class + */ + public Class getColumnClass(int columnIndex) { + Columns col = Columns.values()[columnIndex]; + switch (col) { + case Last_Change: + return Date.class; + } + return String.class; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + RepositoryModel model = list.get(rowIndex); + Columns col = Columns.values()[columnIndex]; + switch (col) { + case Name: + return model.name; + case Description: + return model.description; + case Owner: + return model.owner; + case Last_Change: + return model.lastChange; + case Size: + return model.size; + } + return null; + } +}