| | |
| | | import java.awt.EventQueue;
|
| | | import java.awt.Font;
|
| | | import java.awt.GridLayout;
|
| | | import java.awt.Point;
|
| | | import java.awt.event.ActionEvent;
|
| | | import java.awt.event.ActionListener;
|
| | | import java.awt.event.KeyEvent;
|
| | | import java.awt.event.WindowAdapter;
|
| | | import java.awt.event.WindowEvent;
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | | import java.text.MessageFormat;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | |
|
| | | import javax.swing.ImageIcon;
|
| | | import javax.swing.JFrame;
|
| | |
| | | import javax.swing.KeyStroke;
|
| | | import javax.swing.UIManager;
|
| | |
|
| | | import org.eclipse.jgit.errors.ConfigInvalidException;
|
| | | import org.eclipse.jgit.lib.StoredConfig;
|
| | | import org.eclipse.jgit.storage.file.FileBasedConfig;
|
| | | import org.eclipse.jgit.util.Base64;
|
| | | import org.eclipse.jgit.util.FS;
|
| | |
|
| | | import com.gitblit.Constants;
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | | /**
|
| | | * Sample RPC application.
|
| | | * Gitblit Manager issues JSON RPC requests to a Gitblit server.
|
| | | *
|
| | | * @author James Moger
|
| | | *
|
| | |
| | |
|
| | | private static final long serialVersionUID = 1L;
|
| | | private JTabbedPane serverTabs;
|
| | | private File configFile = new File(System.getProperty("user.home"), ".gitblit/config");
|
| | | private GitblitRegistration localhost = new GitblitRegistration("default",
|
| | | "https://localhost:8443", "admin", "admin".toCharArray());
|
| | |
|
| | | private List<GitblitRegistration> registrations = new ArrayList<GitblitRegistration>();
|
| | | private Map<String, GitblitRegistration> registrations = new LinkedHashMap<String, GitblitRegistration>();
|
| | | private JMenu recentMenu;
|
| | |
|
| | | private GitblitManager() {
|
| | |
| | | private void initialize() {
|
| | | setContentPane(getCenterPanel());
|
| | | setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
|
| | |
|
| | | setTitle("Gitblit Manager v" + Constants.VERSION + " (" + Constants.VERSION_DATE + ")");
|
| | | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
| | | setSize(800, 500);
|
| | | addWindowListener(new WindowAdapter() {
|
| | | @Override
|
| | | public void windowClosing(WindowEvent event) {
|
| | | saveSizeAndPosition();
|
| | | }
|
| | | });
|
| | |
|
| | | setSizeAndPosition();
|
| | | loadRegistrations();
|
| | | rebuildRecentMenu();
|
| | | }
|
| | |
|
| | | private void setSizeAndPosition() {
|
| | | String sz = null;
|
| | | String pos = null;
|
| | | try {
|
| | | StoredConfig config = getConfig();
|
| | | sz = config.getString("ui", null, "size");
|
| | | pos = config.getString("ui", null, "position");
|
| | | } catch (Throwable t) {
|
| | | t.printStackTrace();
|
| | | }
|
| | |
|
| | | // try to restore saved window size
|
| | | if (StringUtils.isEmpty(sz)) {
|
| | | setSize(850, 500);
|
| | | } else {
|
| | | String[] chunks = sz.split("x");
|
| | | int width = Integer.parseInt(chunks[0]);
|
| | | int height = Integer.parseInt(chunks[1]);
|
| | | setSize(width, height);
|
| | | }
|
| | |
|
| | | // try to restore saved window position
|
| | | if (StringUtils.isEmpty(pos)) {
|
| | | setLocationRelativeTo(null);
|
| | | } else {
|
| | | String[] chunks = pos.split(",");
|
| | | int x = Integer.parseInt(chunks[0]);
|
| | | int y = Integer.parseInt(chunks[1]);
|
| | | setLocation(x, y);
|
| | | }
|
| | | }
|
| | |
|
| | | private void saveSizeAndPosition() {
|
| | | try {
|
| | | // save window size and position
|
| | | StoredConfig config = getConfig();
|
| | | Dimension sz = GitblitManager.this.getSize();
|
| | | config.setString("ui", null, "size",
|
| | | MessageFormat.format("{0,number,0}x{1,number,0}", sz.width, sz.height));
|
| | | Point pos = GitblitManager.this.getLocationOnScreen();
|
| | | config.setString("ui", null, "position",
|
| | | MessageFormat.format("{0,number,0},{1,number,0}", pos.x, pos.y));
|
| | | config.save();
|
| | | } catch (Throwable t) {
|
| | | Utils.showException(GitblitManager.this, t);
|
| | | }
|
| | | }
|
| | |
|
| | | public void setVisible(boolean value) {
|
| | |
| | | loginPrompt(localhost);
|
| | | } else if (registrations.size() == 1) {
|
| | | // single registration prompt
|
| | | loginPrompt(registrations.get(0));
|
| | | GitblitRegistration reg = registrations.values().iterator().next();
|
| | | loginPrompt(reg);
|
| | | }
|
| | | super.setVisible(value);
|
| | | setLocationRelativeTo(null);
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | reg = new GitblitRegistration(nameField.getText(), url, accountField.getText(),
|
| | | passwordField.getPassword());
|
| | | boolean success = login(reg);
|
| | | registrations.add(0, reg);
|
| | | registrations.put(reg.name, reg);
|
| | | rebuildRecentMenu();
|
| | | return success;
|
| | | }
|
| | |
| | | serverTabs.setSelectedIndex(idx);
|
| | | serverTabs.setTabComponentAt(idx, new ClosableTabComponent(reg.name, null, serverTabs,
|
| | | panel));
|
| | | saveRegistration(reg);
|
| | | return true;
|
| | | } catch (IOException e) {
|
| | | JOptionPane.showMessageDialog(GitblitManager.this, e.getMessage(),
|
| | |
| | |
|
| | | private void rebuildRecentMenu() {
|
| | | recentMenu.removeAll();
|
| | | for (final GitblitRegistration reg : registrations) {
|
| | | JMenuItem item = new JMenuItem(reg.name);
|
| | | ImageIcon icon = new ImageIcon(getClass().getResource("/gitblt-favicon.png"));
|
| | | for (final GitblitRegistration reg : registrations.values()) {
|
| | | JMenuItem item = new JMenuItem(reg.name, icon);
|
| | | item.addActionListener(new ActionListener() {
|
| | | public void actionPerformed(ActionEvent e) {
|
| | | login(reg);
|
| | | loginPrompt(reg);
|
| | | }
|
| | | });
|
| | | recentMenu.add(item);
|
| | | }
|
| | | }
|
| | |
|
| | | private void loadRegistrations() {
|
| | | try {
|
| | | StoredConfig config = getConfig();
|
| | | Set<String> servers = config.getSubsections("servers");
|
| | | for (String server : servers) {
|
| | | String url = config.getString("servers", server, "url");
|
| | | String account = config.getString("servers", server, "account");
|
| | | char[] password = new String(Base64.decode(config.getString("servers", server,
|
| | | "password"))).toCharArray();
|
| | | GitblitRegistration reg = new GitblitRegistration(server, url, account, password);
|
| | | registrations.put(reg.name, reg);
|
| | | }
|
| | | } catch (Throwable t) {
|
| | | Utils.showException(GitblitManager.this, t);
|
| | | }
|
| | | }
|
| | |
|
| | | private void saveRegistration(GitblitRegistration reg) {
|
| | | try {
|
| | | StoredConfig config = getConfig();
|
| | | config.setString("servers", reg.name, "url", reg.url);
|
| | | config.setString("servers", reg.name, "account", reg.account);
|
| | | config.setString("servers", reg.name, "password",
|
| | | Base64.encodeBytes(new String(reg.password).getBytes("UTF-8")));
|
| | | config.save();
|
| | | } catch (Throwable t) {
|
| | | Utils.showException(GitblitManager.this, t);
|
| | | }
|
| | | }
|
| | |
|
| | | private StoredConfig getConfig() throws IOException, ConfigInvalidException {
|
| | | FileBasedConfig config = new FileBasedConfig(configFile, FS.detect());
|
| | | config.load();
|
| | | return config;
|
| | | }
|
| | |
|
| | | public static void main(String[] args) {
|
| | | EventQueue.invokeLater(new Runnable() {
|
| | | public void run() {
|