James Moger
2011-10-02 19c634873b49dea8b49fc54ca393153f7eb0eb35
Skeleton Gitblit Client app using the json rpc api.
4 files added
360 ■■■■■ changed files
src/com/gitblit/client/DateCellRenderer.java 44 ●●●●● patch | view | raw | blame | history
src/com/gitblit/client/GitblitClient.java 103 ●●●●● patch | view | raw | blame | history
src/com/gitblit/client/GitblitPanel.java 112 ●●●●● patch | view | raw | blame | history
src/com/gitblit/client/RepositoriesModel.java 101 ●●●●● patch | view | raw | blame | history
src/com/gitblit/client/DateCellRenderer.java
New file
@@ -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;
    }
}
src/com/gitblit/client/GitblitClient.java
New file
@@ -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);
            }
        });
    }
}
src/com/gitblit/client/GitblitPanel.java
New file
@@ -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<String, RepositoryModel> 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);
    }
}
src/com/gitblit/client/RepositoriesModel.java
New file
@@ -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<String, RepositoryModel> repositories;
    List<RepositoryModel> list;
    enum Columns {
        Name, Description, Owner, Last_Change, Size;
        @Override
        public String toString() {
            return name().replace('_', ' ');
        }
    }
    public RepositoriesModel(Map<String, RepositoryModel> repositories) {
        this.repositories = repositories;
        list = new ArrayList<RepositoryModel>(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 <code>Object.class</code> regardless of <code>columnIndex</code>.
     *
     * @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;
    }
}