Git Import Wizard
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateGeneralProjectPage.java
blob77fc94b939acee02770adc7e7f20c0b7f7336730
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.clone;
13 import java.io.File;
14 import java.io.FilenameFilter;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.egit.ui.UIText;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.layout.GridDataFactory;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Text;
35 /**
36 * Allows to import a directory in the local file system as "General" project
37 * <p>
38 * Asks the user to provide a project name and shows the directory to be shared.
40 public class GitCreateGeneralProjectPage extends WizardPage {
42 private File myDirectory;
44 private Text projectText;
46 private Text directoryText;
48 private IProject[] wsProjects;
50 /**
51 * Creates a new project creation wizard page.
53 * @param path
54 * the path to a directory in the local file system
56 public GitCreateGeneralProjectPage(String path) {
57 super(GitCreateGeneralProjectPage.class.getName());
58 myDirectory = new File(path);
59 setPageComplete(false);
60 setTitle(UIText.WizardProjectsImportPage_ImportProjectsTitle);
61 setDescription(UIText.WizardProjectsImportPage_ImportProjectsDescription);
64 /**
65 * The path must be initialized using setPath()
67 public GitCreateGeneralProjectPage() {
68 super(GitCreateGeneralProjectPage.class.getName());
69 setPageComplete(false);
70 setTitle(UIText.WizardProjectsImportPage_ImportProjectsTitle);
71 setDescription(UIText.WizardProjectsImportPage_ImportProjectsDescription);
74 /**
75 * @param path
77 public void setPath(String path) {
78 if (path != null)
79 myDirectory = new File(path);
80 else
81 myDirectory = null;
84 public void createControl(Composite parent) {
86 initializeDialogUnits(parent);
88 Composite workArea = new Composite(parent, SWT.NONE);
89 setControl(workArea);
91 workArea.setLayout(new GridLayout(2, false));
92 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
93 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
95 new Label(workArea, SWT.NONE)
96 .setText(UIText.GitCreateGeneralProjectPage_ProjectNameLabel);
97 projectText = new Text(workArea, SWT.BORDER);
98 GridDataFactory.fillDefaults().grab(true, false).applyTo(projectText);
99 projectText.addModifyListener(new ModifyListener() {
101 public void modifyText(ModifyEvent e) {
102 checkPage();
106 new Label(workArea, SWT.NONE)
107 .setText(UIText.GitCreateGeneralProjectPage_DirLabel);
108 directoryText = new Text(workArea, SWT.BORDER);
109 directoryText.setEnabled(false);
110 GridDataFactory.fillDefaults().grab(true, false).applyTo(directoryText);
112 Dialog.applyDialogFont(workArea);
116 @Override
117 public void setVisible(boolean visible) {
118 if (visible) {
119 projectText.setText(myDirectory.getName());
120 directoryText.setText(myDirectory.getPath());
121 checkPage();
123 super.setVisible(visible);
127 * @return the project name
129 public String getProjectName() {
130 return projectText.getText();
133 private void checkPage() {
134 String projectName = projectText.getText();
135 setErrorMessage(null);
136 try {
137 // make sure the directory exists
138 if (!myDirectory.exists()) {
139 setErrorMessage(NLS.bind(
140 UIText.GitCreateGeneralProjectPage_DirNotExistMessage,
141 myDirectory.getPath()));
142 return;
144 // make sure we don't have a file
145 if (!myDirectory.isDirectory()) {
146 setErrorMessage(NLS.bind(
147 UIText.GitCreateGeneralProjectPage_FileNotDirMessage,
148 myDirectory.getPath()));
149 return;
151 // make sure there is not already a .project file
152 if (myDirectory.list(new FilenameFilter() {
154 public boolean accept(File dir, String name) {
155 if (name.equals(".project")) //$NON-NLS-1$
156 return true;
157 return false;
159 }).length > 0) {
160 setErrorMessage(NLS
161 .bind(
162 UIText.GitCreateGeneralProjectPage_FileExistsInDirMessage,
163 ".project", myDirectory.getPath())); //$NON-NLS-1$
164 return;
166 // project name empty
167 if (projectName.length() == 0) {
168 setErrorMessage(UIText.GitCreateGeneralProjectPage_EnterProjectNameMessage);
169 return;
171 // project name valid (no strange chars...)
172 IStatus result = ResourcesPlugin.getWorkspace().validateName(
173 projectName, IResource.PROJECT);
174 if (!result.isOK()) {
175 setErrorMessage(result.getMessage());
176 return;
178 // project already exists
179 if (isProjectInWorkspace(projectName)) {
180 setErrorMessage(NLS
181 .bind(
182 UIText.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage,
183 projectName));
184 return;
186 IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
187 .getProject(projectName);
188 IStatus locationResult = ResourcesPlugin.getWorkspace()
189 .validateProjectLocation(newProject,
190 new Path(myDirectory.getPath()));
191 if (!locationResult.isOK()) {
192 setErrorMessage(locationResult.getMessage());
193 return;
195 } finally {
196 setPageComplete(getErrorMessage() == null);
201 private IProject[] getProjectsInWorkspace() {
202 if (wsProjects == null) {
203 wsProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
205 return wsProjects;
208 private boolean isProjectInWorkspace(String projectName) {
209 if (projectName == null) {
210 return false;
212 IProject[] workspaceProjects = getProjectsInWorkspace();
213 for (int i = 0; i < workspaceProjects.length; i++) {
214 if (projectName.equals(workspaceProjects[i].getName())) {
215 return true;
218 return false;