use nio.Path instead of io.File in jps project serializers with check on file existence
[idea/community.git] / java / idea-ui / src / com / intellij / ide / util / importProject / RootsDetectionStep.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.ide.util.importProject;
3
4 import com.intellij.ide.JavaUiBundle;
5 import com.intellij.ide.util.newProjectWizard.StepSequence;
6 import com.intellij.ide.util.projectWizard.AbstractStepWithProgress;
7 import com.intellij.ide.util.projectWizard.WizardContext;
8 import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
9 import com.intellij.ide.util.projectWizard.importSources.impl.ProjectFromSourcesBuilderImpl;
10 import com.intellij.openapi.application.ApplicationNamesInfo;
11 import com.intellij.openapi.ui.MultiLineLabelUI;
12 import com.intellij.openapi.ui.ex.MultiLineLabel;
13 import com.intellij.util.ui.JBInsets;
14 import com.intellij.util.ui.JBUI;
15 import org.jetbrains.annotations.NonNls;
16 import org.jetbrains.annotations.Nullable;
17
18 import javax.swing.*;
19 import java.awt.*;
20 import java.io.File;
21 import java.util.Collections;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 public class RootsDetectionStep extends AbstractStepWithProgress<List<DetectedRootData>> {
27   private static final String ROOTS_FOUND_CARD = "roots_found";
28   private static final String ROOTS_NOT_FOUND_CARD = "roots_not_found";
29   private final ProjectFromSourcesBuilderImpl myBuilder;
30   private final WizardContext myContext;
31   private final StepSequence mySequence;
32   private final Icon myIcon;
33   private final String myHelpId;
34   private DetectedRootsChooser myDetectedRootsChooser;
35   private String myCurrentBaseProjectPath;
36   private JPanel myResultPanel;
37
38   public RootsDetectionStep(ProjectFromSourcesBuilderImpl builder,
39                             WizardContext context,
40                             StepSequence sequence,
41                             Icon icon,
42                             @NonNls String helpId) {
43     super(JavaUiBundle.message("prompt.stop.searching.for.sources", ApplicationNamesInfo.getInstance().getProductName()));
44     myBuilder = builder;
45     myContext = context;
46     mySequence = sequence;
47     myIcon = icon;
48     myHelpId = helpId;
49   }
50
51   @Override
52   protected JComponent createResultsPanel() {
53     final JPanel panel = new JPanel(new GridBagLayout());
54     myDetectedRootsChooser = new DetectedRootsChooser();
55     myDetectedRootsChooser.addSelectionListener(() -> updateSelectedTypes());
56     final String text = JavaUiBundle.message("label.project.roots.have.been.found");
57     final JLabel label = new JLabel(text);
58     label.setUI(new MultiLineLabelUI());
59     panel.add(label, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 2, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
60                                             GridBagConstraints.HORIZONTAL, JBUI.insets(8, 10, 0, 10), 0, 0));
61     panel.add(myDetectedRootsChooser.getComponent(),
62               new GridBagConstraints(0, GridBagConstraints.RELATIVE, 2, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
63                                      JBInsets.create(8, 10), 0, 0));
64
65     final JButton markAllButton = new JButton(JavaUiBundle.message("button.mark.all"));
66     panel.add(markAllButton,
67               new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
68                                      JBUI.insets(0, 10, 8, 2), 0, 0));
69
70     final JButton unmarkAllButton = new JButton(JavaUiBundle.message("button.unmark.all"));
71     panel.add(unmarkAllButton,
72               new GridBagConstraints(1, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
73                                      JBUI.insets(0, 0, 8, 10), 0, 0));
74
75     markAllButton.addActionListener(e -> myDetectedRootsChooser.setAllElementsMarked(true));
76     unmarkAllButton.addActionListener(e -> myDetectedRootsChooser.setAllElementsMarked(false));
77
78     myResultPanel = new JPanel(new CardLayout());
79     myResultPanel.add(ROOTS_FOUND_CARD, panel);
80     JPanel notFoundPanel = new JPanel(new BorderLayout());
81     notFoundPanel.setBorder(JBUI.Borders.empty(5));
82     notFoundPanel.add(BorderLayout.NORTH, new MultiLineLabel(JavaUiBundle.message("label.project.roots.not.found")));
83     myResultPanel.add(ROOTS_NOT_FOUND_CARD, notFoundPanel);
84     return myResultPanel;
85   }
86
87   @Override
88   public JComponent getPreferredFocusedComponent() {
89     return myDetectedRootsChooser.getComponent();
90   }
91
92   @Override
93   public void updateDataModel() {
94     final List<DetectedRootData> selectedElements = myDetectedRootsChooser.getMarkedElements();
95     myBuilder.setupProjectStructure(RootDetectionProcessor.createRootsMap(selectedElements));
96     updateSelectedTypes();
97   }
98
99   private void updateSelectedTypes() {
100     Set<String> selectedTypes = new LinkedHashSet<>();
101
102     selectedTypes.add(JavaUiBundle.message("existing.sources"));
103
104     for (DetectedRootData rootData : myDetectedRootsChooser.getMarkedElements()) {
105       for (ProjectStructureDetector detector : rootData.getSelectedDetectors()) {
106         selectedTypes.add(detector.getDetectorId());
107       }
108     }
109
110     mySequence.setTypes(selectedTypes);
111     myContext.requestWizardButtonsUpdate();
112   }
113
114   @Override
115   protected boolean shouldRunProgress() {
116     final String baseProjectPath = getBaseProjectPath();
117     return myCurrentBaseProjectPath == null ? baseProjectPath != null : !myCurrentBaseProjectPath.equals(baseProjectPath);
118   }
119
120   @Override
121   protected void onFinished(final List<DetectedRootData> foundRoots, final boolean canceled) {
122     final CardLayout layout = (CardLayout)myResultPanel.getLayout();
123     if (!foundRoots.isEmpty() && !canceled) {
124       myCurrentBaseProjectPath = getBaseProjectPath();
125       myDetectedRootsChooser.setElements(foundRoots);
126       updateSelectedTypes();
127       layout.show(myResultPanel, ROOTS_FOUND_CARD);
128     }
129     else {
130       myCurrentBaseProjectPath = null;
131       layout.show(myResultPanel, ROOTS_NOT_FOUND_CARD);
132     }
133     myResultPanel.revalidate();
134   }
135
136   @Override
137   protected List<DetectedRootData> calculate() {
138     final String baseProjectPath = getBaseProjectPath();
139     if (baseProjectPath == null) {
140       return Collections.emptyList();
141     }
142
143     return RootDetectionProcessor.detectRoots(new File(baseProjectPath));
144   }
145
146
147   @Nullable
148   private String getBaseProjectPath() {
149     return myBuilder.getBaseProjectPath();
150   }
151
152   @Override
153   protected String getProgressText() {
154     final String root = getBaseProjectPath();
155     return JavaUiBundle.message("progress.searching.for.sources", root != null ? root.replace('/', File.separatorChar) : "");
156   }
157
158   @Override
159   public Icon getIcon() {
160     return myIcon;
161   }
162
163   @Override
164   public String getHelpId() {
165     return myHelpId;
166   }
167 }