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
11 * Mathias Kinzler (SAP AG) - initial implementation
12 *******************************************************************************/
13 package org
.eclipse
.egit
.ui
.internal
.clone
;
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
;
39 * Allows to import a directory in the local file system as "General" project
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
;
56 * Creates a new project creation wizard page.
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;
72 defaultLocation
= false;
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
);
88 public void setPath(String path
) {
90 myDirectory
= new File(path
);
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
);
112 projectText
.setEnabled(false);
114 projectText
.addModifyListener(new ModifyListener() {
117 public void modifyText(ModifyEvent e
) {
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
);
133 public void setVisible(boolean visible
) {
135 projectText
.setText(myDirectory
.getName());
136 directoryText
.setText(myDirectory
.getPath());
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);
160 // make sure the directory exists
161 if (!myDirectory
.exists()) {
162 setErrorMessage(NLS
.bind(
163 UIText
.GitCreateGeneralProjectPage_DirNotExistMessage
,
164 myDirectory
.getPath()));
167 // make sure we don't have a file
168 if (!myDirectory
.isDirectory()) {
169 setErrorMessage(NLS
.bind(
170 UIText
.GitCreateGeneralProjectPage_FileNotDirMessage
,
171 myDirectory
.getPath()));
174 // make sure there is not already a .project file
175 if (myDirectory
.list(new FilenameFilter() {
178 public boolean accept(File dir
, String name
) {
179 if (name
.equals(".project")) //$NON-NLS-1$
186 UIText
.GitCreateGeneralProjectPage_FileExistsInDirMessage
,
187 ".project", myDirectory
.getPath())); //$NON-NLS-1$
190 // project name empty
191 if (projectName
.length() == 0) {
192 setErrorMessage(UIText
.GitCreateGeneralProjectPage_EnterProjectNameMessage
);
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());
202 // project already exists
203 if (isProjectInWorkspace(projectName
)) {
206 UIText
.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage
,
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());
222 setPageComplete(getErrorMessage() == null);
227 private IProject
[] getProjectsInWorkspace() {
228 if (wsProjects
== null) {
229 wsProjects
= ResourcesPlugin
.getWorkspace().getRoot().getProjects();
234 private boolean isProjectInWorkspace(String projectName
) {
235 if (projectName
== null) {
238 IProject
[] workspaceProjects
= getProjectsInWorkspace();
239 for (int i
= 0; i
< workspaceProjects
.length
; i
++) {
240 if (projectName
.equals(workspaceProjects
[i
].getName())) {