Refactor the class GitDecoratorPreferencePage
[egit/chris.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / preferences / GitProjectPropertyPage.java
blob2fc7c804e74b54d712ff50ae438f71817443c3c0
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 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.ui.internal.preferences;
11 import java.io.IOException;
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.egit.core.project.RepositoryMapping;
16 import org.eclipse.egit.ui.UIText;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25 import org.eclipse.ui.dialogs.PropertyPage;
26 import org.spearce.jgit.lib.ObjectId;
27 import org.spearce.jgit.lib.Repository;
29 /**
30 * Property page to be shown in project properties, if project is shared using
31 * git provider. Currently there aren't any modifiable element.
33 public class GitProjectPropertyPage extends PropertyPage {
35 private Text gitDir;
37 private Text branch;
39 private Text id;
41 private Text state;
43 private Text workDir;
45 @Override
46 protected Control createContents(Composite parent) {
47 final Composite composite = new Composite(parent, SWT.NULL);
49 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
50 final GridLayout layout = new GridLayout();
51 layout.numColumns = 2;
52 layout.marginHeight = 0;
53 layout.marginWidth = 0;
54 layout.horizontalSpacing = 0;
55 layout.verticalSpacing = 0;
56 composite.setLayout(layout);
58 gitDir = createLabeledReadOnlyText(composite, UIText.GitProjectPropertyPage_LabelGitDir);
59 workDir = createLabeledReadOnlyText(composite, UIText.GitProjectPropertyPage_LabelWorkdir);
60 branch = createLabeledReadOnlyText(composite, UIText.GitProjectPropertyPage_LabelBranch);
61 id = createLabeledReadOnlyText(composite, UIText.GitProjectPropertyPage_LabelId);
62 state = createLabeledReadOnlyText(composite, UIText.GitProjectPropertyPage_LabelState);
64 // Get the project that is the source of this property page
65 IProject project = null;
66 final IAdaptable element = getElement();
67 if (element instanceof IProject) {
68 project = (IProject) element;
69 } else {
70 Object adapter = element.getAdapter(IProject.class);
71 if (adapter instanceof IProject) {
72 project = (IProject) adapter;
76 Repository repository = RepositoryMapping.getMapping(project)
77 .getRepository();
79 if (repository != null) {
80 try {
81 fillValues(repository);
82 } catch (IOException e) {
83 e.printStackTrace();
87 return composite;
90 private void fillValues(Repository repository) throws IOException {
91 gitDir.setText(repository.getDirectory().getAbsolutePath());
92 branch.setText(repository.getBranch());
93 workDir.setText(repository.getWorkDir().getAbsolutePath());
95 state.setText(repository.getRepositoryState().getDescription());
97 final ObjectId objectId = repository
98 .resolve(repository.getFullBranch());
99 if (objectId == null) {
100 if (repository.getAllRefs().size() == 0)
101 id.setText(UIText.GitProjectPropertyPage_ValueEmptyRepository);
102 else
103 id.setText(UIText.GitProjectPropertyPage_ValueUnbornBranch);
104 } else
105 id.setText(objectId.name());
109 * Create a read only text field with a label
111 * @param parent
112 * the parent composite for new widgets
113 * @param labelText
114 * text for label
115 * @return the new read only text field
117 protected Text createLabeledReadOnlyText(Composite parent,
118 final String labelText) {
119 Label label = new Label(parent, SWT.LEFT);
120 label.setText(labelText);
121 GridData data = new GridData();
122 data.horizontalSpan = 1;
123 data.horizontalAlignment = GridData.FILL;
124 label.setLayoutData(data);
126 Text text = new Text(parent, SWT.LEFT | SWT.READ_ONLY);
127 text.setBackground(Display.getDefault().getSystemColor(
128 SWT.COLOR_WIDGET_BACKGROUND));
129 data = new GridData();
130 data.horizontalSpan = 1;
131 data.horizontalAlignment = GridData.FILL;
132 text.setLayoutData(data);
133 return text;