Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateGeneralProjectPage.java
blobfc384cd6b0d3e58cfc15c1584e568c5dcacc930b
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 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * Mathias Kinzler (SAP AG) - initial implementation
12 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.clone;
15 import java.io.File;
16 import java.io.FilenameFilter;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.egit.ui.internal.UIText;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.layout.GridDataFactory;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
38 /**
39 * Allows to import a directory in the local file system as "General" project
40 * <p>
41 * Asks the user to provide a project name and shows the directory to be shared.
43 public class GitCreateGeneralProjectPage extends WizardPage {
45 private File myDirectory;
47 private Text projectText;
49 private Text directoryText;
51 private IProject[] wsProjects;
53 private boolean defaultLocation;
55 /**
56 * Creates a new project creation wizard page.
58 * @param path
59 * the path to a directory in the local file system
61 public GitCreateGeneralProjectPage(String path) {
62 super(GitCreateGeneralProjectPage.class.getName());
63 myDirectory = new File(path);
64 setPageComplete(false);
65 setTitle(UIText.WizardProjectsImportPage_ImportProjectsTitle);
66 setDescription(UIText.WizardProjectsImportPage_ImportProjectsDescription);
67 // check for default location: is workspace location parent of path?
68 IPath parent = new Path(path).removeLastSegments(1);
69 if(ResourcesPlugin.getWorkspace().getRoot().getLocation().equals(parent))
70 defaultLocation = true;
71 else
72 defaultLocation = false;
75 /**
76 * The path must be initialized using setPath()
78 public GitCreateGeneralProjectPage() {
79 super(GitCreateGeneralProjectPage.class.getName());
80 setPageComplete(false);
81 setTitle(UIText.WizardProjectsImportPage_ImportProjectsTitle);
82 setDescription(UIText.WizardProjectsImportPage_ImportProjectsDescription);
85 /**
86 * @param path
88 public void setPath(String path) {
89 if (path != null)
90 myDirectory = new File(path);
91 else
92 myDirectory = null;
95 @Override
96 public void createControl(Composite parent) {
98 initializeDialogUnits(parent);
100 Composite workArea = new Composite(parent, SWT.NONE);
101 setControl(workArea);
103 workArea.setLayout(new GridLayout(2, false));
104 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
105 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
107 new Label(workArea, SWT.NONE)
108 .setText(UIText.GitCreateGeneralProjectPage_ProjectNameLabel);
109 projectText = new Text(workArea, SWT.BORDER);
110 GridDataFactory.fillDefaults().grab(true, false).applyTo(projectText);
111 if(defaultLocation)
112 projectText.setEnabled(false);
113 else
114 projectText.addModifyListener(new ModifyListener() {
116 @Override
117 public void modifyText(ModifyEvent e) {
118 checkPage();
122 new Label(workArea, SWT.NONE)
123 .setText(UIText.GitCreateGeneralProjectPage_DirLabel);
124 directoryText = new Text(workArea, SWT.BORDER);
125 directoryText.setEnabled(false);
126 GridDataFactory.fillDefaults().grab(true, false).applyTo(directoryText);
128 Dialog.applyDialogFont(workArea);
132 @Override
133 public void setVisible(boolean visible) {
134 if (visible) {
135 projectText.setText(myDirectory.getName());
136 directoryText.setText(myDirectory.getPath());
137 checkPage();
139 super.setVisible(visible);
143 * @return the project name
145 public String getProjectName() {
146 return projectText.getText();
150 * @return true if the project has default location
152 public boolean isDefaultLocation() {
153 return defaultLocation;
156 private void checkPage() {
157 String projectName = projectText.getText();
158 setErrorMessage(null);
159 try {
160 // make sure the directory exists
161 if (!myDirectory.exists()) {
162 setErrorMessage(NLS.bind(
163 UIText.GitCreateGeneralProjectPage_DirNotExistMessage,
164 myDirectory.getPath()));
165 return;
167 // make sure we don't have a file
168 if (!myDirectory.isDirectory()) {
169 setErrorMessage(NLS.bind(
170 UIText.GitCreateGeneralProjectPage_FileNotDirMessage,
171 myDirectory.getPath()));
172 return;
174 // make sure there is not already a .project file
175 if (myDirectory.list(new FilenameFilter() {
177 @Override
178 public boolean accept(File dir, String name) {
179 if (name.equals(".project")) //$NON-NLS-1$
180 return true;
181 return false;
183 }).length > 0) {
184 setErrorMessage(NLS
185 .bind(
186 UIText.GitCreateGeneralProjectPage_FileExistsInDirMessage,
187 ".project", myDirectory.getPath())); //$NON-NLS-1$
188 return;
190 // project name empty
191 if (projectName.length() == 0) {
192 setErrorMessage(UIText.GitCreateGeneralProjectPage_EnterProjectNameMessage);
193 return;
195 // project name valid (no strange chars...)
196 IStatus result = ResourcesPlugin.getWorkspace().validateName(
197 projectName, IResource.PROJECT);
198 if (!result.isOK()) {
199 setErrorMessage(result.getMessage());
200 return;
202 // project already exists
203 if (isProjectInWorkspace(projectName)) {
204 setErrorMessage(NLS
205 .bind(
206 UIText.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage,
207 projectName));
208 return;
210 if(!defaultLocation) {
211 IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
212 .getProject(projectName);
213 IStatus locationResult = ResourcesPlugin.getWorkspace()
214 .validateProjectLocation(newProject,
215 new Path(myDirectory.getPath()));
216 if (!locationResult.isOK()) {
217 setErrorMessage(locationResult.getMessage());
218 return;
221 } finally {
222 setPageComplete(getErrorMessage() == null);
227 private IProject[] getProjectsInWorkspace() {
228 if (wsProjects == null) {
229 wsProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
231 return wsProjects;
234 private boolean isProjectInWorkspace(String projectName) {
235 if (projectName == null) {
236 return false;
238 IProject[] workspaceProjects = getProjectsInWorkspace();
239 for (int i = 0; i < workspaceProjects.length; i++) {
240 if (projectName.equals(workspaceProjects[i].getName())) {
241 return true;
244 return false;