Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateGeneralProjectPage.java
blob6d0be4c7f9a17563b8517ad150d8b52dd1f571b9
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;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
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 File projectFile = new File(myDirectory,
176 IProjectDescription.DESCRIPTION_FILE_NAME);
177 if (projectFile.exists()) {
178 setErrorMessage(NLS.bind(
179 UIText.GitCreateGeneralProjectPage_FileExistsInDirMessage,
180 IProjectDescription.DESCRIPTION_FILE_NAME,
181 myDirectory.getPath()));
182 return;
184 // project name empty
185 if (projectName.isEmpty()) {
186 setErrorMessage(UIText.GitCreateGeneralProjectPage_EnterProjectNameMessage);
187 return;
189 // project name valid (no strange chars...)
190 IStatus result = ResourcesPlugin.getWorkspace().validateName(
191 projectName, IResource.PROJECT);
192 if (!result.isOK()) {
193 setErrorMessage(result.getMessage());
194 return;
196 // project already exists
197 if (isProjectInWorkspace(projectName)) {
198 setErrorMessage(NLS
199 .bind(
200 UIText.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage,
201 projectName));
202 return;
204 if(!defaultLocation) {
205 IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
206 .getProject(projectName);
207 IStatus locationResult = ResourcesPlugin.getWorkspace()
208 .validateProjectLocation(newProject,
209 new Path(myDirectory.getPath()));
210 if (!locationResult.isOK()) {
211 setErrorMessage(locationResult.getMessage());
212 return;
215 } finally {
216 setPageComplete(getErrorMessage() == null);
221 private IProject[] getProjectsInWorkspace() {
222 if (wsProjects == null) {
223 wsProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
225 return wsProjects;
228 private boolean isProjectInWorkspace(String projectName) {
229 if (projectName == null) {
230 return false;
232 IProject[] workspaceProjects = getProjectsInWorkspace();
233 for (IProject workspaceProject : workspaceProjects) {
234 if (projectName.equals(workspaceProject.getName())) {
235 return true;
238 return false;