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
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.clone
;
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
;
36 * Allows to import a directory in the local file system as "General" project
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
;
51 * Creates a new project creation wizard page.
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
);
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
);
77 public void setPath(String path
) {
79 myDirectory
= new File(path
);
84 public void createControl(Composite parent
) {
86 initializeDialogUnits(parent
);
88 Composite workArea
= new Composite(parent
, SWT
.NONE
);
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
) {
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
);
117 public void setVisible(boolean visible
) {
119 projectText
.setText(myDirectory
.getName());
120 directoryText
.setText(myDirectory
.getPath());
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);
137 // make sure the directory exists
138 if (!myDirectory
.exists()) {
139 setErrorMessage(NLS
.bind(
140 UIText
.GitCreateGeneralProjectPage_DirNotExistMessage
,
141 myDirectory
.getPath()));
144 // make sure we don't have a file
145 if (!myDirectory
.isDirectory()) {
146 setErrorMessage(NLS
.bind(
147 UIText
.GitCreateGeneralProjectPage_FileNotDirMessage
,
148 myDirectory
.getPath()));
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$
162 UIText
.GitCreateGeneralProjectPage_FileExistsInDirMessage
,
163 ".project", myDirectory
.getPath())); //$NON-NLS-1$
166 // project name empty
167 if (projectName
.length() == 0) {
168 setErrorMessage(UIText
.GitCreateGeneralProjectPage_EnterProjectNameMessage
);
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());
178 // project already exists
179 if (isProjectInWorkspace(projectName
)) {
182 UIText
.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage
,
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());
196 setPageComplete(getErrorMessage() == null);
201 private IProject
[] getProjectsInWorkspace() {
202 if (wsProjects
== null) {
203 wsProjects
= ResourcesPlugin
.getWorkspace().getRoot().getProjects();
208 private boolean isProjectInWorkspace(String projectName
) {
209 if (projectName
== null) {
212 IProject
[] workspaceProjects
= getProjectsInWorkspace();
213 for (int i
= 0; i
< workspaceProjects
.length
; i
++) {
214 if (projectName
.equals(workspaceProjects
[i
].getName())) {