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