View Javadoc
1   /*
2    * Copyright (C) 2010, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.pgm;
13  
14  import java.awt.BorderLayout;
15  import java.awt.FlowLayout;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.WindowAdapter;
18  import java.awt.event.WindowEvent;
19  import java.io.File;
20  
21  import javax.swing.JButton;
22  import javax.swing.JFrame;
23  import javax.swing.JPanel;
24  import javax.swing.JScrollPane;
25  
26  import org.eclipse.jgit.awtui.CommitGraphPane;
27  import org.eclipse.jgit.lib.Constants;
28  import org.eclipse.jgit.pgm.internal.CLIText;
29  import org.eclipse.jgit.revplot.PlotWalk;
30  import org.eclipse.jgit.revwalk.RevCommit;
31  import org.eclipse.jgit.revwalk.RevSort;
32  import org.eclipse.jgit.revwalk.RevWalk;
33  
34  @Command(usage = "usage_Glog")
35  class Glog extends RevWalkTextBuiltin {
36  	final JFrame frame;
37  
38  	final CommitGraphPane graphPane;
39  
40  	Glog() {
41  		frame = new JFrame();
42  		frame.addWindowListener(new WindowAdapter() {
43  			@Override
44  			public void windowClosing(WindowEvent e) {
45  				frame.dispose();
46  			}
47  		});
48  
49  		graphPane = new CommitGraphPane();
50  
51  		final JScrollPane graphScroll = new JScrollPane(graphPane);
52  
53  		final JPanel buttons = new JPanel(new FlowLayout());
54  		final JButton repaint = new JButton();
55  		repaint.setText(CLIText.get().repaint);
56  		repaint.addActionListener((ActionEvent e) -> {
57  			graphPane.repaint();
58  		});
59  		buttons.add(repaint);
60  
61  		final JPanel world = new JPanel(new BorderLayout());
62  		world.add(buttons, BorderLayout.SOUTH);
63  		world.add(graphScroll, BorderLayout.CENTER);
64  
65  		frame.getContentPane().add(world);
66  	}
67  
68  	/** {@inheritDoc} */
69  	@Override
70  	protected int walkLoop() throws Exception {
71  		graphPane.getCommitList().source(walk);
72  		graphPane.getCommitList().fillTo(Integer.MAX_VALUE);
73  
74  		frame.setTitle("[" + repoName() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
75  		frame.pack();
76  		frame.setVisible(true);
77  		return graphPane.getCommitList().size();
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	protected void show(RevCommit c) throws Exception {
83  		throw new UnsupportedOperationException();
84  	}
85  
86  	/** {@inheritDoc} */
87  	@Override
88  	protected RevWalk createWalk() {
89  		if (objects)
90  			throw die(CLIText.get().cannotUseObjectsWithGlog);
91  		final PlotWalkPlotWalk.html#PlotWalk">PlotWalk w = new PlotWalk(db);
92  		w.sort(RevSort.BOUNDARY, true);
93  		return w;
94  	}
95  
96  	private String repoName() {
97  		final File gitDir = db.getDirectory();
98  		if (gitDir == null)
99  			return db.toString();
100 		String n = gitDir.getName();
101 		if (Constants.DOT_GIT.equals(n))
102 			n = gitDir.getParentFile().getName();
103 		return n;
104 	}
105 }