simplify GTDUCollector a bit more
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / JavaResourceRootEditHandlerBase.java
1 // Copyright 2000-2020 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.openapi.roots.ui.configuration;
3
4 import com.intellij.icons.AllIcons;
5 import com.intellij.ide.JavaUiBundle;
6 import com.intellij.openapi.actionSystem.CustomShortcutSet;
7 import com.intellij.openapi.project.ProjectBundle;
8 import com.intellij.openapi.roots.SourceFolder;
9 import com.intellij.openapi.ui.DialogWrapper;
10 import com.intellij.openapi.util.io.FileUtil;
11 import com.intellij.openapi.util.text.StringUtil;
12 import com.intellij.ui.roots.IconActionComponent;
13 import com.intellij.util.ui.FormBuilder;
14 import org.jetbrains.annotations.NotNull;
15 import org.jetbrains.annotations.Nullable;
16 import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
17 import org.jetbrains.jps.model.java.JavaResourceRootProperties;
18 import org.jetbrains.jps.model.module.JpsModuleSourceRootType;
19
20 import javax.swing.*;
21 import java.awt.*;
22
23 public abstract class JavaResourceRootEditHandlerBase extends ModuleSourceRootEditHandler<JavaResourceRootProperties> {
24   public JavaResourceRootEditHandlerBase(JpsModuleSourceRootType<JavaResourceRootProperties> rootType) {
25     super(rootType);
26   }
27
28   @Nullable
29   @Override
30   public Icon getFolderUnderRootIcon() {
31     return null;
32   }
33
34   @Nullable
35   @Override
36   public CustomShortcutSet getMarkRootShortcutSet() {
37     return null;
38   }
39
40   @NotNull
41   @Override
42   public Icon getRootIcon(@NotNull JavaResourceRootProperties properties) {
43     return properties.isForGeneratedSources() ? getGeneratedRootIcon() : getRootIcon();
44   }
45
46   @NotNull
47   protected Icon getGeneratedRootIcon() {
48     return getRootIcon();
49   }
50
51   @Nullable
52   @Override
53   public String getPropertiesString(@NotNull JavaResourceRootProperties properties) {
54     StringBuilder buffer = new StringBuilder();
55     if (properties.isForGeneratedSources()) {
56       buffer.append(" [generated]");
57     }
58     String relativeOutputPath = properties.getRelativeOutputPath();
59     if (!relativeOutputPath.isEmpty()) {
60       buffer.append(" (").append(relativeOutputPath).append(")");
61     }
62     return buffer.length() > 0 ? buffer.toString() : null;
63   }
64
65   @Nullable
66   @Override
67   public JComponent createPropertiesEditor(@NotNull final SourceFolder folder,
68                                            @NotNull final JComponent parentComponent,
69                                            @NotNull final ContentRootPanel.ActionCallback callback) {
70     final IconActionComponent iconComponent = new IconActionComponent(AllIcons.General.Inline_edit,
71                                                                       AllIcons.General.Inline_edit_hovered,
72                                                                       ProjectBundle.message("module.paths.edit.properties.tooltip"),
73                                                                       () -> {
74                                                                         JavaResourceRootProperties properties = folder.getJpsElement().getProperties( JavaModuleSourceRootTypes.RESOURCES);
75                                                                         assert properties != null;
76                                                                         ResourceRootPropertiesDialog
77                                                                           dialog = new ResourceRootPropertiesDialog(parentComponent, properties);
78                                                                         if (dialog.showAndGet()) {
79                                                                           callback.onSourceRootPropertiesChanged(folder);
80                                                                         }
81                                                                       });
82     final JPanel panel = new JPanel(new BorderLayout());
83     panel.setOpaque(false);
84     panel.add(iconComponent, BorderLayout.CENTER);
85     panel.add(Box.createHorizontalStrut(3), BorderLayout.EAST);
86     return panel;
87   }
88
89   private static final class ResourceRootPropertiesDialog extends DialogWrapper {
90     private final JTextField myRelativeOutputPathField;
91     private final JCheckBox myIsGeneratedCheckBox;
92     private final JPanel myMainPanel;
93     @NotNull private final JavaResourceRootProperties myProperties;
94
95     private ResourceRootPropertiesDialog(@NotNull JComponent parentComponent, @NotNull JavaResourceRootProperties properties) {
96       super(parentComponent, true);
97       myProperties = properties;
98       setTitle(ProjectBundle.message("module.paths.edit.properties.title"));
99       myRelativeOutputPathField = new JTextField();
100       myIsGeneratedCheckBox = new JCheckBox(JavaUiBundle.message("for.generated.resources"));
101       myMainPanel = FormBuilder.createFormBuilder()
102         .addLabeledComponent(JavaUiBundle.message("label.relative.output.path"), myRelativeOutputPathField)
103         .addComponent(myIsGeneratedCheckBox)
104         .getPanel();
105       myRelativeOutputPathField.setText(myProperties.getRelativeOutputPath());
106       myRelativeOutputPathField.setColumns(25);
107       myIsGeneratedCheckBox.setSelected(myProperties.isForGeneratedSources());
108       init();
109     }
110
111     @Nullable
112     @Override
113     public JComponent getPreferredFocusedComponent() {
114       return myRelativeOutputPathField;
115     }
116
117     @Override
118     protected void doOKAction() {
119       myProperties.setRelativeOutputPath(normalizePath(myRelativeOutputPathField.getText()));
120       myProperties.setForGeneratedSources(myIsGeneratedCheckBox.isSelected());
121       super.doOKAction();
122     }
123
124     @NotNull
125     private static String normalizePath(String path) {
126       return StringUtil.trimEnd(StringUtil.trimStart(FileUtil.toSystemIndependentName(path.trim()), "/"), "/");
127     }
128
129     @Nullable
130     @Override
131     protected JComponent createCenterPanel() {
132       return myMainPanel;
133     }
134   }
135 }