2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.ide
.impl
;
18 import com
.intellij
.CommonBundle
;
19 import com
.intellij
.ide
.GeneralSettings
;
20 import com
.intellij
.ide
.IdeBundle
;
21 import com
.intellij
.ide
.highlighter
.ModuleFileType
;
22 import com
.intellij
.ide
.highlighter
.ProjectFileType
;
23 import com
.intellij
.ide
.highlighter
.WorkspaceFileType
;
24 import com
.intellij
.openapi
.components
.StorageScheme
;
25 import com
.intellij
.openapi
.components
.impl
.stores
.IProjectStore
;
26 import com
.intellij
.openapi
.diagnostic
.Logger
;
27 import com
.intellij
.openapi
.extensions
.Extensions
;
28 import com
.intellij
.openapi
.fileTypes
.FileType
;
29 import com
.intellij
.openapi
.project
.Project
;
30 import com
.intellij
.openapi
.project
.ProjectManager
;
31 import com
.intellij
.openapi
.project
.ex
.ProjectEx
;
32 import com
.intellij
.openapi
.project
.ex
.ProjectManagerEx
;
33 import com
.intellij
.openapi
.ui
.Messages
;
34 import com
.intellij
.openapi
.util
.ActionCallback
;
35 import com
.intellij
.openapi
.util
.Disposer
;
36 import com
.intellij
.openapi
.util
.InvalidDataException
;
37 import com
.intellij
.openapi
.util
.io
.FileUtil
;
38 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
39 import com
.intellij
.openapi
.vfs
.VirtualFile
;
40 import com
.intellij
.openapi
.wm
.FocusCommand
;
41 import com
.intellij
.openapi
.wm
.IdeFocusManager
;
42 import com
.intellij
.openapi
.wm
.WindowManager
;
43 import com
.intellij
.projectImport
.ProjectOpenProcessor
;
44 import org
.jdom
.JDOMException
;
45 import org
.jetbrains
.annotations
.NonNls
;
46 import org
.jetbrains
.annotations
.NotNull
;
47 import org
.jetbrains
.annotations
.Nullable
;
51 import java
.io
.IOException
;
54 * @author Eugene Belyaev
56 public class ProjectUtil
{
57 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ide.impl.ProjectUtil");
58 @NonNls public static final String DIRECTORY_BASED_PROJECT_DIR
= ".idea";
60 private ProjectUtil() {
63 public static void updateLastProjectLocation(final String projectFilePath
) {
64 File lastProjectLocation
= new File(projectFilePath
);
65 if (lastProjectLocation
.isFile()) {
66 lastProjectLocation
= lastProjectLocation
.getParentFile(); //for directory based project storages
68 if (lastProjectLocation
== null) { // the immediate parent of the ipr file
71 lastProjectLocation
= lastProjectLocation
.getParentFile(); // the candidate directory to be saved
72 if (lastProjectLocation
== null) {
75 String path
= lastProjectLocation
.getPath();
77 path
= FileUtil
.resolveShortWindowsName(path
);
79 catch (IOException e
) {
83 GeneralSettings
.getInstance().setLastProjectLocation(path
.replace(File
.separatorChar
, '/'));
87 * @param project cannot be null
89 public static boolean closeProject(@NotNull Project project
) {
90 if (!ProjectManagerEx
.getInstanceEx().closeProject(project
)) return false;
91 Disposer
.dispose(project
);
96 * @param path project file path
97 * @param projectToClose currently active project
98 * @param forceOpenInNewFrame forces opening in new frame
99 * @return project by path if the path was recognized as IDEA project file or one of the project formats supported by
100 * installed importers (regardless of opening/import result)
104 public static Project
openOrImport(@NotNull final String path
, final Project projectToClose
, boolean forceOpenInNewFrame
) {
105 final VirtualFile virtualFile
= LocalFileSystem
.getInstance().refreshAndFindFileByPath(path
);
107 if (virtualFile
== null) return null;
109 if (path
.endsWith(ProjectFileType
.DOT_DEFAULT_EXTENSION
) ||
110 virtualFile
.isDirectory() && virtualFile
.findChild(DIRECTORY_BASED_PROJECT_DIR
) != null) {
111 return openProject(path
, projectToClose
, forceOpenInNewFrame
);
113 ProjectOpenProcessor provider
= getImportProvider(virtualFile
);
114 if (provider
!= null) {
115 return provider
.doOpenProject(virtualFile
, projectToClose
, forceOpenInNewFrame
);
121 public static ProjectOpenProcessor
getImportProvider(VirtualFile file
) {
122 for (ProjectOpenProcessor provider
: Extensions
.getExtensions(ProjectOpenProcessor
.EXTENSION_POINT_NAME
)) {
123 if (provider
.canOpenProject(file
)) {
131 public static Project
openProject(final String path
, Project projectToClose
, boolean forceOpenInNewFrame
) {
132 File file
= new File(path
);
133 if (!file
.exists()) {
134 Messages
.showMessageDialog(IdeBundle
.message("error.project.file.does.not.exist", path
), CommonBundle
.getErrorTitle(),
135 Messages
.getErrorIcon());
139 if (file
.isDirectory() && !new File(file
, DIRECTORY_BASED_PROJECT_DIR
).exists()) {
140 Messages
.showMessageDialog(IdeBundle
.message("error.project.file.does.not.exist", new File(file
, DIRECTORY_BASED_PROJECT_DIR
).getPath()), CommonBundle
.getErrorTitle(),
141 Messages
.getErrorIcon());
145 Project
[] openProjects
= ProjectManager
.getInstance().getOpenProjects();
146 for (Project project
: openProjects
) {
147 if (isSameProject(path
, project
)) {
148 focusProjectWindow(project
);
153 if (!forceOpenInNewFrame
&& openProjects
.length
> 0) {
154 int exitCode
= Messages
.showDialog(IdeBundle
.message("prompt.open.project.in.new.frame"), IdeBundle
.message("title.open.project"),
155 new String
[]{IdeBundle
.message("button.newframe"), IdeBundle
.message("button.existingframe"),
156 CommonBundle
.getCancelButtonText()}, 1, Messages
.getQuestionIcon());
157 if (exitCode
== 1) { // "No" option
158 if (!closeProject(projectToClose
!= null ? projectToClose
: openProjects
[openProjects
.length
- 1])) return null;
160 else if (exitCode
!= 0) { // not "Yes"
165 ProjectManagerEx projectManager
= ProjectManagerEx
.getInstanceEx();
166 Project project
= null;
168 project
= projectManager
.loadAndOpenProject(path
);
170 catch (IOException e
) {
171 Messages
.showMessageDialog(IdeBundle
.message("error.cannot.load.project", e
.getMessage()),
172 IdeBundle
.message("title.cannot.load.project"), Messages
.getErrorIcon());
174 catch (JDOMException e
) {
176 Messages
.showMessageDialog(IdeBundle
.message("error.project.file.is.corrupted"), IdeBundle
.message("title.cannot.load.project"),
177 Messages
.getErrorIcon());
179 catch (InvalidDataException e
) {
181 Messages
.showMessageDialog(IdeBundle
.message("error.project.file.is.corrupted"), IdeBundle
.message("title.cannot.load.project"),
182 Messages
.getErrorIcon());
187 private static boolean isSameProject(String path
, Project p
) {
188 final IProjectStore projectStore
= ((ProjectEx
)p
).getStateStore();
190 String toOpen
= FileUtil
.toSystemIndependentName(path
);
191 String existing
= FileUtil
.toSystemIndependentName(projectStore
.getProjectFilePath());
193 final VirtualFile existingBaseDir
= projectStore
.getProjectBaseDir();
194 if (existingBaseDir
== null) return false; // could be null if not yet initialized
196 final File openFile
= new File(toOpen
);
197 if (openFile
.isDirectory()) {
198 return FileUtil
.pathsEqual(toOpen
, existingBaseDir
.getPath());
200 if (StorageScheme
.DIRECTORY_BASED
== projectStore
.getStorageScheme()) {
201 // todo: check if IPR is located not under the project base dir
202 return FileUtil
.pathsEqual(FileUtil
.toSystemIndependentName(openFile
.getParentFile().getPath()), existingBaseDir
.getPath());
206 return FileUtil
.pathsEqual(toOpen
, existing
);
209 public static void focusProjectWindow(final Project p
) {
210 IdeFocusManager
.getInstance(p
).requestFocus(new FocusCommand() {
212 public ActionCallback
run() {
213 JFrame f
= WindowManager
.getInstance().getFrame(p
);
218 return new ActionCallback
.Done();
223 public static boolean isProjectOrWorkspaceFile(final VirtualFile file
) {
224 return isProjectOrWorkspaceFile(file
, file
.getFileType());
227 public static boolean isProjectOrWorkspaceFile(final VirtualFile file
,
228 final FileType fileType
) {
229 final boolean iprBased
= fileType
instanceof WorkspaceFileType
|| fileType
instanceof ProjectFileType
|| fileType
instanceof ModuleFileType
;
230 if (iprBased
) return true;
231 final VirtualFile parent
= file
.getParent();
232 return parent
!= null && parent
.getName().equals(DIRECTORY_BASED_PROJECT_DIR
);