Make Git property page cope with empty repositories and unborn branches
[egit/imyousuf.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / preferences / GitProjectPropertyPage.java
blobd3afd97da1e39e5b192e085af9a81db152aa7179
1 /*******************************************************************************
2 * Copyright (C) 2008, Tomi Pakarinen <tomi.pakarinen@iki.fi>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.spearce.egit.ui.internal.preferences;
10 import java.io.IOException;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Text;
22 import org.eclipse.ui.dialogs.PropertyPage;
23 import org.spearce.egit.core.project.RepositoryMapping;
24 import org.spearce.jgit.lib.ObjectId;
25 import org.spearce.jgit.lib.Repository;
27 /**
28 * Property page to be shown in project properties, if project is shared using
29 * git provider. Currently there aren't any modifiable element.
31 public class GitProjectPropertyPage extends PropertyPage {
33 private Text gitDir;
35 private Text branch;
37 private Text id;
39 private Text state;
41 private Text workDir;
43 @Override
44 protected Control createContents(Composite parent) {
45 final Composite composite = new Composite(parent, SWT.NULL);
47 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
48 final GridLayout layout = new GridLayout();
49 layout.numColumns = 2;
50 layout.marginHeight = 0;
51 layout.marginWidth = 0;
52 layout.horizontalSpacing = 0;
53 layout.verticalSpacing = 0;
54 composite.setLayout(layout);
56 gitDir = createLabeledReadOnlyText(composite, "Git directory:");
57 workDir = createLabeledReadOnlyText(composite, "Working directory:");
58 branch = createLabeledReadOnlyText(composite, "Branch:");
59 id = createLabeledReadOnlyText(composite, "Id:");
60 state = createLabeledReadOnlyText(composite, "Current state:");
62 // Get the project that is the source of this property page
63 IProject project = null;
64 final IAdaptable element = getElement();
65 if (element instanceof IProject) {
66 project = (IProject) element;
67 } else {
68 Object adapter = element.getAdapter(IProject.class);
69 if (adapter instanceof IProject) {
70 project = (IProject) adapter;
74 Repository repository = RepositoryMapping.getMapping(project)
75 .getRepository();
77 if (repository != null) {
78 try {
79 fillValues(repository);
80 } catch (IOException e) {
81 e.printStackTrace();
85 return composite;
88 private void fillValues(Repository repository) throws IOException {
89 gitDir.setText(repository.getDirectory().getAbsolutePath());
90 branch.setText(repository.getBranch());
91 workDir.setText(repository.getWorkDir().getAbsolutePath());
93 state.setText(repository.getRepositoryState().getDescription());
95 final ObjectId objectId = repository
96 .resolve(repository.getFullBranch());
97 if (objectId == null) {
98 if (repository.getAllRefs().size() == 0)
99 id.setText("None (empty repository)");
100 else
101 id.setText("None (unborn branch)");
102 } else
103 id.setText(objectId.name());
107 * Create a read only text field with a label
109 * @param parent
110 * the parent composite for new widgets
111 * @param labelText
112 * text for label
113 * @return the new read only text field
115 protected Text createLabeledReadOnlyText(Composite parent,
116 final String labelText) {
117 Label label = new Label(parent, SWT.LEFT);
118 label.setText(labelText);
119 GridData data = new GridData();
120 data.horizontalSpan = 1;
121 data.horizontalAlignment = GridData.FILL;
122 label.setLayoutData(data);
124 Text text = new Text(parent, SWT.LEFT | SWT.READ_ONLY);
125 text.setBackground(Display.getDefault().getSystemColor(
126 SWT.COLOR_WIDGET_BACKGROUND));
127 data = new GridData();
128 data.horizontalSpan = 1;
129 data.horizontalAlignment = GridData.FILL;
130 text.setLayoutData(data);
131 return text;