Commit dialog now shows information much more similar to git-commit.
[egit/egit-new.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / dialogs / CommitDialog.java
blobe4cf1bc100e3074f5502929c84c51c66f525b4f6
1 /*
2 * Copyright (C) 2007 David Watson <dwatson@mimvista.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 package org.spearce.egit.ui.internal.dialogs;
20 import java.util.ArrayList;
21 import java.util.Collections;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.layout.GridDataFactory;
28 import org.eclipse.jface.viewers.CheckboxTableViewer;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.ITableLabelProvider;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.swt.widgets.Table;
40 import org.eclipse.swt.widgets.TableColumn;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.model.WorkbenchLabelProvider;
43 import org.spearce.egit.core.project.GitProjectData;
44 import org.spearce.egit.core.project.RepositoryMapping;
45 import org.spearce.jgit.lib.GitIndex;
46 import org.spearce.jgit.lib.Repository;
47 import org.spearce.jgit.lib.Tree;
48 import org.spearce.jgit.lib.TreeEntry;
49 import org.spearce.jgit.lib.GitIndex.Entry;
51 /**
52 * @author dwatson Dialog is shown to user when they request to commit files.
53 * Changes in the selected portion of the tree are shown.
55 public class CommitDialog extends Dialog {
57 class CommitLabelProvider extends WorkbenchLabelProvider implements
58 ITableLabelProvider {
59 public String getColumnText(Object obj, int columnIndex) {
60 IFile file = (IFile) obj;
61 if (columnIndex == 1)
62 return file.getProject().getName() + ": "
63 + file.getProjectRelativePath();
65 else if (columnIndex == 0) {
66 String prefix = "Unknown";
68 final GitProjectData projectData = GitProjectData.get(file
69 .getProject());
70 try {
71 RepositoryMapping repositoryMapping = projectData
72 .getRepositoryMapping(file.getProject());
74 Repository repo = repositoryMapping.getRepository();
75 GitIndex index = repo.getIndex();
76 Tree headTree = repo.mapTree("HEAD");
78 String repoPath = repositoryMapping
79 .getRepoRelativePath(file);
80 TreeEntry headEntry = headTree.findBlobMember(repoPath);
81 boolean headExists = headTree.existsBlob(repoPath);
83 Entry indexEntry = index.getEntry(repoPath);
84 if (headEntry == null) {
85 prefix = "Added";
86 if (indexEntry.isModified(repositoryMapping.getWorkDir()))
87 prefix = "Added, index diff";
88 } else if (indexEntry == null) {
89 prefix = "Removed";
90 } else if (headExists
91 && !headEntry.getId().equals(
92 indexEntry.getObjectId())) {
93 prefix = "Modified";
96 if (indexEntry.isModified(repositoryMapping.getWorkDir()))
97 prefix = "Mod., index diff";
100 } catch (Exception e) {
103 return prefix;
105 return null;
108 public Image getColumnImage(Object element, int columnIndex) {
109 if (columnIndex == 0)
110 return getImage(element);
111 return null;
115 ArrayList<IFile> files;
118 * @param parentShell
120 public CommitDialog(Shell parentShell) {
121 super(parentShell);
124 @Override
125 protected void createButtonsForButtonBar(Composite parent) {
126 createButton(parent, IDialogConstants.SELECT_ALL_ID, "Select All", false);
127 createButton(parent, IDialogConstants.DESELECT_ALL_ID, "Deselect All", false);
129 createButton(parent, IDialogConstants.OK_ID, "Commit", true);
130 createButton(parent, IDialogConstants.CANCEL_ID,
131 IDialogConstants.CANCEL_LABEL, true);
134 Text commitText;
136 CheckboxTableViewer filesViewer;
138 @Override
139 protected Control createDialogArea(Composite parent) {
140 Composite container = (Composite) super.createDialogArea(parent);
141 parent.getShell().setText("Commit Changes");
143 GridLayout layout = new GridLayout(1, false);
144 container.setLayout(layout);
146 Label label = new Label(container, SWT.LEFT);
147 label.setText("Commit Message:");
148 label.setLayoutData(GridDataFactory.fillDefaults().span(1, 1).create());
150 commitText = new Text(container, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
151 commitText.setLayoutData(GridDataFactory.fillDefaults().span(1, 1)
152 .hint(600, 200).create());
154 Table resourcesTable = new Table(container, SWT.H_SCROLL | SWT.V_SCROLL
155 | SWT.FULL_SELECTION | SWT.MULTI | SWT.CHECK | SWT.BORDER);
156 resourcesTable.setLayoutData(GridDataFactory.fillDefaults().hint(600,
157 200).create());
159 resourcesTable.setHeaderVisible(true);
160 TableColumn statCol = new TableColumn(resourcesTable, SWT.LEFT);
161 statCol.setText("Status");
162 statCol.setWidth(150);
164 TableColumn resourceCol = new TableColumn(resourcesTable, SWT.LEFT);
165 resourceCol.setText("File");
166 resourceCol.setWidth(415);
168 filesViewer = new CheckboxTableViewer(resourcesTable);
169 filesViewer.setContentProvider(new IStructuredContentProvider() {
171 public void inputChanged(Viewer viewer, Object oldInput,
172 Object newInput) {
175 public void dispose() {
178 public Object[] getElements(Object inputElement) {
179 return files.toArray();
183 filesViewer.setLabelProvider(new CommitLabelProvider());
184 filesViewer.setInput(files);
185 filesViewer.setAllChecked(true);
187 container.pack();
188 return container;
191 public String getCommitMessage() {
192 return commitMessage;
195 public void setCommitMessage(String s) {
196 this.commitMessage = s;
199 private String commitMessage = "";
201 private ArrayList<IFile> selectedItems = new ArrayList<IFile>();
203 public void setSelectedItems(IFile[] items) {
204 Collections.addAll(selectedItems, items);
207 public IFile[] getSelectedItems() {
208 return selectedItems.toArray(new IFile[0]);
211 @Override
212 protected void okPressed() {
213 commitMessage = commitText.getText();
214 Object[] checkedElements = filesViewer.getCheckedElements();
215 selectedItems.clear();
216 for (Object obj : checkedElements)
217 selectedItems.add((IFile) obj);
219 if (commitMessage.trim().length() == 0) {
220 MessageDialog.openWarning(getShell(), "No message", "You must enter a commit message");
221 return;
224 if (selectedItems.isEmpty()) {
225 MessageDialog.openWarning(getShell(), "No items selected", "No items are currently selected to be committed.");
226 return;
228 super.okPressed();
231 public void setFileList(ArrayList<IFile> files) {
232 this.files = files;
235 @Override
236 protected void buttonPressed(int buttonId) {
237 if (IDialogConstants.SELECT_ALL_ID == buttonId) {
238 filesViewer.setAllChecked(true);
240 if (IDialogConstants.DESELECT_ALL_ID == buttonId) {
241 filesViewer.setAllChecked(false);
243 super.buttonPressed(buttonId);