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 org
.jetbrains
.idea
.eclipse
.export
;
18 import com
.intellij
.openapi
.actionSystem
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.module
.Module
;
23 import com
.intellij
.openapi
.module
.ModuleManager
;
24 import com
.intellij
.openapi
.options
.ConfigurationException
;
25 import com
.intellij
.openapi
.project
.DumbAware
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
28 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
29 import com
.intellij
.openapi
.roots
.impl
.storage
.ClasspathStorage
;
30 import com
.intellij
.openapi
.ui
.DialogWrapper
;
31 import com
.intellij
.openapi
.ui
.Messages
;
32 import com
.intellij
.openapi
.util
.WriteExternalException
;
33 import com
.intellij
.openapi
.util
.io
.FileUtil
;
34 import com
.intellij
.openapi
.util
.text
.StringUtil
;
35 import com
.intellij
.openapi
.vfs
.VirtualFile
;
36 import com
.intellij
.util
.Function
;
37 import org
.jdom
.Document
;
38 import org
.jdom
.Element
;
39 import org
.jdom
.output
.EclipseJDOMUtil
;
40 import org
.jetbrains
.idea
.eclipse
.EclipseBundle
;
41 import org
.jetbrains
.idea
.eclipse
.EclipseXml
;
42 import org
.jetbrains
.idea
.eclipse
.IdeaXml
;
43 import org
.jetbrains
.idea
.eclipse
.config
.EclipseClasspathStorageProvider
;
44 import org
.jetbrains
.idea
.eclipse
.conversion
.*;
47 import java
.io
.IOException
;
48 import java
.util
.ArrayList
;
49 import java
.util
.List
;
51 public class ExportEclipseProjectsAction
extends AnAction
implements DumbAware
{
52 private static final Logger LOG
= Logger
.getInstance("#" + ExportEclipseProjectsAction
.class.getName());
54 public void update(final AnActionEvent e
) {
55 final Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
56 e
.getPresentation().setEnabled( project
!= null );
59 public void actionPerformed(AnActionEvent e
) {
60 final Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
61 if ( project
== null ) return;
62 project
.save(); // to flush iml files
64 final List
<Module
> modules
= new ArrayList
<Module
>();
65 final List
<Module
> incompatibleModules
= new ArrayList
<Module
>();
66 for (Module module
: ModuleManager
.getInstance(project
).getModules()) {
67 if (!EclipseClasspathStorageProvider
.ID
.equals(ClasspathStorage
.getStorageType(module
))) {
68 final ModifiableRootModel model
= ModuleRootManager
.getInstance(module
).getModifiableModel();
70 ClasspathStorage
.getProvider(EclipseClasspathStorageProvider
.ID
).assertCompatible(model
);
73 catch (ConfigurationException e1
) {
74 incompatibleModules
.add(module
);
81 //todo suggest smth with hierarchy modules
82 if (!incompatibleModules
.isEmpty()) {
83 if (Messages
.showOkCancelDialog(project
, "<html><body>Eclipse incompatible modules found:<ul><br><li>" + StringUtil
.join(incompatibleModules
, new Function
<Module
, String
>() {
84 public String
fun(Module module
) {
85 return module
.getName();
87 }, "<br><li>") + "</ul><br>Would you like to proceed and possibly lose your configurations?</body></html>", "Eclipse Incompatible Modules Found", Messages
.getWarningIcon()) != DialogWrapper
.OK_EXIT_CODE
) {
90 } else if (modules
.isEmpty()){
91 Messages
.showInfoMessage(project
, EclipseBundle
.message("eclipse.export.nothing.to.do"), EclipseBundle
.message("eclipse.export.dialog.title"));
95 modules
.addAll(incompatibleModules
);
96 final ExportEclipseProjectsDialog dialog
= new ExportEclipseProjectsDialog(project
, modules
);
99 if (dialog
.isLink()) {
100 for (Module module
: dialog
.getSelectedModules()) {
101 final ModifiableRootModel model
= ModuleRootManager
.getInstance(module
).getModifiableModel();
102 ClasspathStorage
.setStorageType(model
, EclipseClasspathStorageProvider
.ID
);
107 for (Module module
: dialog
.getSelectedModules()) {
108 final ModifiableRootModel model
= ModuleRootManager
.getInstance(module
).getModifiableModel();
109 final VirtualFile
[] contentRoots
= model
.getContentRoots();
110 final String storageRoot
= contentRoots
.length
== 1 ? contentRoots
[0].getPath() : ClasspathStorage
.getStorageRootFromOptions(module
);
112 final Element classpathEleemnt
= new Element(EclipseXml
.CLASSPATH_TAG
);
114 final EclipseClasspathWriter classpathWriter
= new EclipseClasspathWriter(model
);
115 classpathWriter
.writeClasspath(classpathEleemnt
, null);
116 final File classpathFile
= new File(storageRoot
, EclipseXml
.CLASSPATH_FILE
);
117 if (!FileUtil
.createIfDoesntExist(classpathFile
)) continue;
118 EclipseJDOMUtil
.output(new Document(classpathEleemnt
), classpathFile
, project
);
120 final Element ideaSpecific
= new Element(IdeaXml
.COMPONENT_TAG
);
121 if (IdeaSpecificSettings
.writeIDEASpecificClasspath(ideaSpecific
, model
)) {
122 final File emlFile
= new File(storageRoot
, module
.getName() + EclipseXml
.IDEA_SETTINGS_POSTFIX
);
123 if (!FileUtil
.createIfDoesntExist(emlFile
)) continue;
124 EclipseJDOMUtil
.output(new Document(ideaSpecific
), emlFile
, project
);
127 DotProjectFileHelper
.saveDotProjectFile(module
, storageRoot
);
129 catch (ConversionException e1
) {
132 catch (IOException e1
) {
135 catch (WriteExternalException e1
) {
144 EclipseUserLibrariesHelper
.appendProjectLibraries(project
, dialog
.getUserLibrariesFile());
146 catch (IOException e1
) {