-
Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy pathGoAutoImportConfigurable.java
More file actions
179 lines (157 loc) · 6.75 KB
/
Copy pathGoAutoImportConfigurable.java
File metadata and controls
179 lines (157 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Mihai Toader, Florin Patan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.goide.codeInsight.imports;
import com.goide.project.GoExcludedPathsSettings;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.*;
import com.intellij.ui.components.JBList;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class GoAutoImportConfigurable implements SearchableConfigurable {
private JCheckBox myCbShowImportPopup;
private JCheckBox myCbOptimizeImportsOnTheFly;
private JCheckBox myCbAddUnambiguousImports;
private JBList myExcludePackagesList;
private DefaultListModel myExcludePackagesModel;
@NotNull private GoCodeInsightSettings myCodeInsightSettings;
@NotNull private GoExcludedPathsSettings myExcludedSettings;
private final boolean myIsDefaultProject;
private final boolean myIsDialog;
public GoAutoImportConfigurable(@NotNull Project project, boolean dialogMode) {
myCodeInsightSettings = GoCodeInsightSettings.getInstance();
myExcludedSettings = GoExcludedPathsSettings.getInstance(project);
myIsDefaultProject = project.isDefault();
myIsDialog = dialogMode;
}
@Nullable
@Override
public JComponent createComponent() {
FormBuilder builder = FormBuilder.createFormBuilder();
myCbShowImportPopup = new JCheckBox(ApplicationBundle.message("checkbox.show.import.popup"));
myCbOptimizeImportsOnTheFly = new JCheckBox(ApplicationBundle.message("checkbox.optimize.imports.on.the.fly"));
myCbAddUnambiguousImports = new JCheckBox(ApplicationBundle.message("checkbox.add.unambiguous.imports.on.the.fly"));
builder.addComponent(myCbShowImportPopup);
builder.addComponent(myCbOptimizeImportsOnTheFly);
builder.addComponent(myCbAddUnambiguousImports);
myExcludePackagesList = new JBList();
JComponent excludedPanel = new JPanel(new BorderLayout());
excludedPanel.add(ToolbarDecorator.createDecorator(myExcludePackagesList)
.setAddAction(new AddImportExclusionAction()).disableUpDownActions().createPanel(), BorderLayout.CENTER);
excludedPanel.setBorder(IdeBorderFactory.createTitledBorder(ApplicationBundle.message("exclude.from.completion.group"), true));
if (!myIsDefaultProject) {
builder.addComponent(excludedPanel);
}
JPanel result = new JPanel(new BorderLayout());
result.add(builder.getPanel(), BorderLayout.NORTH);
if (myIsDialog) result.setPreferredSize(new Dimension(300, -1));
return result;
}
public void focusList() {
myExcludePackagesList.setSelectedIndex(0);
myExcludePackagesList.requestFocus();
}
private String[] getExcludedPackages() {
String[] excludedPackages = new String[myExcludePackagesModel.size()];
for (int i = 0; i < myExcludePackagesModel.size(); i++) {
excludedPackages[i] = (String)myExcludePackagesModel.elementAt(i);
}
Arrays.sort(excludedPackages);
return excludedPackages;
}
@Override
public boolean isModified() {
return myCodeInsightSettings.isShowImportPopup() != myCbShowImportPopup.isSelected() ||
myCodeInsightSettings.isOptimizeImportsOnTheFly() != myCbOptimizeImportsOnTheFly.isSelected() ||
myCodeInsightSettings.isAddUnambiguousImportsOnTheFly() != myCbAddUnambiguousImports.isSelected() ||
!Arrays.equals(getExcludedPackages(), myExcludedSettings.getExcludedPackages());
}
@Override
public void apply() throws ConfigurationException {
myCodeInsightSettings.setShowImportPopup(myCbShowImportPopup.isSelected());
myCodeInsightSettings.setOptimizeImportsOnTheFly(myCbOptimizeImportsOnTheFly.isSelected());
myCodeInsightSettings.setAddUnambiguousImportsOnTheFly(myCbAddUnambiguousImports.isSelected());
myExcludedSettings.setExcludedPackages(getExcludedPackages());
}
@Override
public void reset() {
myCbShowImportPopup.setSelected(myCodeInsightSettings.isShowImportPopup());
myCbOptimizeImportsOnTheFly.setSelected(myCodeInsightSettings.isOptimizeImportsOnTheFly());
myCbAddUnambiguousImports.setSelected(myCodeInsightSettings.isAddUnambiguousImportsOnTheFly());
myExcludePackagesModel = new DefaultListModel();
for (String name : myExcludedSettings.getExcludedPackages()) {
myExcludePackagesModel.add(myExcludePackagesModel.size(), name);
}
myExcludePackagesList.setModel(myExcludePackagesModel);
}
@NotNull
@Override
public String getId() {
return "go.autoimport";
}
@Nullable
@Override
public Runnable enableSearch(String option) {
return null;
}
@Nls
@Override
public String getDisplayName() {
return "Auto Import";
}
@Nullable
@Override
public String getHelpTopic() {
return null;
}
@Override
public void disposeUIResources() {
UIUtil.dispose(myExcludePackagesList);
myExcludePackagesList = null;
myExcludePackagesModel = null;
}
private class AddImportExclusionAction implements AnActionButtonRunnable {
@Override
public void run(AnActionButton button) {
String packageName =
Messages.showInputDialog("Enter the import path to exclude from auto-import and completion:",
"Exclude Import Path",
Messages.getWarningIcon());
addExcludedPackage(packageName);
}
private void addExcludedPackage(@Nullable String packageName) {
if (StringUtil.isEmpty(packageName)) return;
int index = -Arrays.binarySearch(myExcludePackagesModel.toArray(), packageName) - 1;
if (index >= 0) {
myExcludePackagesModel.add(index, packageName);
ListScrollingUtil.ensureIndexIsVisible(myExcludePackagesList, index, 0);
}
myExcludePackagesList.clearSelection();
myExcludePackagesList.setSelectedValue(packageName, true);
myExcludePackagesList.requestFocus();
}
}
}