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
.egit
.ui
.UIText
;
21 import org
.eclipse
.jface
.dialogs
.Dialog
;
22 import org
.eclipse
.jface
.layout
.GridDataFactory
;
23 import org
.eclipse
.jface
.wizard
.WizardPage
;
24 import org
.eclipse
.osgi
.util
.NLS
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.ModifyEvent
;
27 import org
.eclipse
.swt
.events
.ModifyListener
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Text
;
35 * Allows to import a directory in the local file system as "General" project
37 * Asks the user to provide a project name and shows the directory to be shared.
39 * TODO String externalization
41 public class GitCreateGeneralProjectPage
extends WizardPage
{
43 private final File myDirectory
;
45 private Text projectText
;
47 private Text directoryText
;
49 private IProject
[] wsProjects
;
52 * Creates a new project creation wizard page.
55 * the path to a directory in the local file system
57 public GitCreateGeneralProjectPage(String path
) {
58 super(GitCreateGeneralProjectPage
.class.getName());
59 myDirectory
= new File(path
);
60 setPageComplete(false);
61 setTitle(UIText
.WizardProjectsImportPage_ImportProjectsTitle
);
62 setDescription(UIText
.WizardProjectsImportPage_ImportProjectsDescription
);
65 public void createControl(Composite parent
) {
67 initializeDialogUnits(parent
);
69 Composite workArea
= new Composite(parent
, SWT
.NONE
);
72 workArea
.setLayout(new GridLayout(2, false));
73 workArea
.setLayoutData(new GridData(GridData
.FILL_BOTH
74 | GridData
.GRAB_HORIZONTAL
| GridData
.GRAB_VERTICAL
));
76 new Label(workArea
, SWT
.NONE
).setText(UIText
.GitCreateGeneralProjectPage_ProjectNameLabel
);
77 projectText
= new Text(workArea
, SWT
.BORDER
);
78 GridDataFactory
.fillDefaults().grab(true, false).applyTo(projectText
);
79 projectText
.addModifyListener(new ModifyListener() {
81 public void modifyText(ModifyEvent e
) {
86 new Label(workArea
, SWT
.NONE
).setText(UIText
.GitCreateGeneralProjectPage_DirLabel
);
87 directoryText
= new Text(workArea
, SWT
.BORDER
);
88 directoryText
.setEnabled(false);
89 GridDataFactory
.fillDefaults().grab(true, false).applyTo(directoryText
);
91 Dialog
.applyDialogFont(workArea
);
96 public void setVisible(boolean visible
) {
98 projectText
.setText(myDirectory
.getName());
99 directoryText
.setText(myDirectory
.getPath());
102 super.setVisible(visible
);
106 * @return the project name
108 public String
getProjectName() {
109 return projectText
.getText();
112 private void checkPage() {
113 String projectName
= projectText
.getText();
114 setErrorMessage(null);
116 // make sure the directory exists
117 if (!myDirectory
.exists()) {
118 setErrorMessage(NLS
.bind(UIText
.GitCreateGeneralProjectPage_DirNotExistMessage
,
119 myDirectory
.getPath()));
122 // make sure we don't have a file
123 if (!myDirectory
.isDirectory()) {
124 setErrorMessage(NLS
.bind(UIText
.GitCreateGeneralProjectPage_FileNotDirMessage
,
125 myDirectory
.getPath()));
128 // make sure there is not already a .project file
129 if (myDirectory
.list(new FilenameFilter() {
131 public boolean accept(File dir
, String name
) {
132 if (name
.equals(".project")) //$NON-NLS-1$
137 setErrorMessage(NLS
.bind(
138 UIText
.GitCreateGeneralProjectPage_FileExistsInDirMessage
,
139 ".project", myDirectory
.getPath())); //$NON-NLS-1$
142 // project name empty
143 if (projectName
.length() == 0) {
144 setErrorMessage(UIText
.GitCreateGeneralProjectPage_EnterProjectNameMessage
);
147 // project name valid (no strange chars...)
148 IStatus result
= ResourcesPlugin
.getWorkspace().validateName(
149 projectName
, IResource
.PROJECT
);
150 if (!result
.isOK()) {
151 setErrorMessage(result
.getMessage());
154 // project already exists
155 if (isProjectInWorkspace(projectName
)) {
156 setErrorMessage(NLS
.bind(UIText
.GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage
,
161 setPageComplete(getErrorMessage() == null);
166 private IProject
[] getProjectsInWorkspace() {
167 if (wsProjects
== null) {
168 wsProjects
= ResourcesPlugin
.getWorkspace().getRoot().getProjects();
173 private boolean isProjectInWorkspace(String projectName
) {
174 if (projectName
== null) {
177 IProject
[] workspaceProjects
= getProjectsInWorkspace();
178 for (int i
= 0; i
< workspaceProjects
.length
; i
++) {
179 if (projectName
.equals(workspaceProjects
[i
].getName())) {