-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathComponentWithBrowseButton.java
More file actions
400 lines (354 loc) · 15 KB
/
ComponentWithBrowseButton.java
File metadata and controls
400 lines (354 loc) · 15 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.ui;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.actionSystem.ShortcutSet;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileSaverDescriptor;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.ui.GuiUtils;
import com.intellij.ui.UIBundle;
import com.intellij.ui.components.fields.ExtendableTextComponent;
import com.intellij.ui.dsl.builder.DslComponentProperty;
import com.intellij.ui.dsl.builder.VerticalComponentGap;
import com.intellij.ui.dsl.gridLayout.UnscaledGaps;
import com.intellij.ui.dsl.gridLayout.UnscaledGapsKt;
import com.intellij.util.SlowOperations;
import com.intellij.util.ui.StartupUiUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.accessibility.ScreenReader;
import com.intellij.util.ui.update.Activatable;
import com.intellij.util.ui.update.UiNotifyConnector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.ref.WeakReference;
import static com.intellij.openapi.actionSystem.PlatformDataKeys.UI_DISPOSABLE;
public class ComponentWithBrowseButton<Comp extends JComponent> extends JPanel implements Disposable {
private final Comp myComponent;
private final FixedSizeButton myBrowseButton;
private final ExtendableTextComponent.Extension myInlineButtonExtension;
private boolean myButtonEnabled = true;
public ComponentWithBrowseButton(@NotNull Comp component, @Nullable ActionListener browseActionListener) {
super(new BorderLayout(SystemInfo.isMac || StartupUiUtil.isUnderDarcula() ? 0 : 2, 0));
myComponent = component;
// required! otherwise JPanel will occasionally gain focus instead of the component
setFocusable(false);
boolean inlineBrowseButton = myComponent instanceof ExtendableTextComponent;
if (inlineBrowseButton) {
myInlineButtonExtension = ExtendableTextComponent.Extension.create(
getDefaultIcon(), getHoveredIcon(), getIconTooltip(), true, this::notifyActionListeners);
((ExtendableTextComponent)myComponent).addExtension(myInlineButtonExtension);
new DumbAwareAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
notifyActionListeners();
}
}.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)), myComponent);
} else {
myInlineButtonExtension = null;
}
add(myComponent, BorderLayout.CENTER);
myBrowseButton = new FixedSizeButton(myComponent);
if (isBackgroundSet()) {
myBrowseButton.setBackground(getBackground());
}
if (browseActionListener != null) {
myBrowseButton.addActionListener(browseActionListener);
}
if (!inlineBrowseButton) {
add(myBrowseButton, BorderLayout.EAST);
}
myBrowseButton.setToolTipText(getIconTooltip());
// FixedSizeButton isn't focusable but it should be selectable via keyboard.
if (ApplicationManager.getApplication() != null) { // avoid crash at design time
new MyDoClickAction(myBrowseButton).registerShortcut(myComponent);
}
if (ScreenReader.isActive()) {
myBrowseButton.setFocusable(true);
myBrowseButton.getAccessibleContext().setAccessibleName(UIBundle.message("component.with.browse.button.accessible.name"));
} else if (Registry.is("ide.browse.button.always.focusable", false)) {
myBrowseButton.setFocusable(true);
}
LazyDisposable.installOn(this);
Insets insets = myComponent.getInsets();
if (!inlineBrowseButton) {
insets.right = myBrowseButton.getInsets().right;
}
UnscaledGaps visualPaddings = UnscaledGapsKt.toUnscaledGaps(insets);
putClientProperty(DslComponentProperty.INTERACTIVE_COMPONENT, component);
putClientProperty(DslComponentProperty.VERTICAL_COMPONENT_GAP, VerticalComponentGap.BOTH);
putClientProperty(DslComponentProperty.VISUAL_PADDINGS, visualPaddings);
}
protected @NotNull Icon getDefaultIcon() {
return AllIcons.General.OpenDisk;
}
protected @NotNull Icon getHoveredIcon() {
return AllIcons.General.OpenDiskHover;
}
protected @NotNull @NlsContexts.Tooltip String getIconTooltip() {
return getTooltip();
}
public static @NotNull @NlsContexts.Tooltip String getTooltip() {
return UIBundle.message("component.with.browse.button.browse.button.tooltip.text") + " (" +
KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)) + ")";
}
private void notifyActionListeners() {
ActionEvent event = new ActionEvent(myComponent, ActionEvent.ACTION_PERFORMED, "action");
for (ActionListener listener: myBrowseButton.getActionListeners()) {
try (AccessToken ignore = SlowOperations.startSection(SlowOperations.ACTION_PERFORM)) {
listener.actionPerformed(event);
}
}
}
public final @NotNull Comp getChildComponent() {
return myComponent;
}
public void setTextFieldPreferredWidth(final int charCount) {
JComponent comp = getChildComponent();
Dimension size = GuiUtils.getSizeByChars(charCount, comp);
comp.setPreferredSize(size);
Dimension preferredSize = myBrowseButton.getPreferredSize();
@SuppressWarnings("removal") boolean keepHeight = UIUtil.isUnderWin10LookAndFeel();
preferredSize.setSize(size.width + preferredSize.width + 2, keepHeight ? preferredSize.height : preferredSize.height + 2);
setPreferredSize(preferredSize);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
myBrowseButton.setEnabled(enabled && myButtonEnabled);
myComponent.setEnabled(enabled);
}
public void setButtonEnabled(boolean buttonEnabled) {
myButtonEnabled = buttonEnabled;
setEnabled(isEnabled());
}
public void setButtonVisible(boolean buttonVisible) {
myBrowseButton.setVisible(buttonVisible);
if (myInlineButtonExtension != null && myComponent instanceof ExtendableTextComponent) {
if (buttonVisible) {
((ExtendableTextComponent)myComponent).addExtension(myInlineButtonExtension);
} else {
((ExtendableTextComponent)myComponent).removeExtension(myInlineButtonExtension);
}
}
}
public void setButtonIcon(@NotNull Icon icon) {
myBrowseButton.setIcon(icon);
myBrowseButton.setDisabledIcon(IconLoader.getDisabledIcon(icon));
}
@Override
public void setBackground(Color color) {
super.setBackground(color);
if (myBrowseButton != null) {
myBrowseButton.setBackground(color);
}
}
/**
* Adds specified {@code listener} to the browse button.
*/
public void addActionListener(ActionListener listener){
myBrowseButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
myBrowseButton.removeActionListener(listener);
}
public void addBrowseFolderListener(
@Nullable Project project,
FileChooserDescriptor fileChooserDescriptor,
TextComponentAccessor<? super Comp> accessor
) {
addActionListener(new BrowseFolderActionListener<>(this, project, fileChooserDescriptor, accessor));
}
public void addFileSaverDialog(@Nullable Project project,
@NotNull FileSaverDescriptor descriptor,
TextComponentAccessor<? super Comp> accessor)
{
addActionListener(new FileSaverRunnable<>(project, descriptor, getChildComponent(), accessor));
}
/**
* @deprecated use {@link #addBrowseFolderListener(Project, FileChooserDescriptor, TextComponentAccessor)}
* together with {@link FileChooserDescriptor#withTitle} and {@link FileChooserDescriptor#withDescription}
*/
@Deprecated(forRemoval = true)
public void addBrowseFolderListener(
@Nullable @NlsContexts.DialogTitle String title,
@Nullable @NlsContexts.Label String description,
@Nullable Project project,
FileChooserDescriptor fileChooserDescriptor,
TextComponentAccessor<? super Comp> accessor
) {
addBrowseFolderListener(project, fileChooserDescriptor.withTitle(title).withDescription(description), accessor);
}
/**
* @deprecated use {@link #addBrowseFolderListener(Project, FileChooserDescriptor, TextComponentAccessor)}
* together with {@link FileChooserDescriptor#withTitle} and {@link FileChooserDescriptor#withDescription}
*/
@Deprecated(forRemoval = true)
@SuppressWarnings("unused")
public void addBrowseFolderListener(
@Nullable @NlsContexts.DialogTitle String title,
@Nullable @NlsContexts.Label String description,
@Nullable Project project,
FileChooserDescriptor fileChooserDescriptor,
TextComponentAccessor<? super Comp> accessor,
boolean autoRemoveOnHide
) {
addBrowseFolderListener(title, description, project, fileChooserDescriptor, accessor);
}
@Override
public void dispose() {
ActionListener[] listeners = myBrowseButton.getActionListeners();
for (ActionListener listener : listeners) {
myBrowseButton.removeActionListener(listener);
}
}
/**
* @deprecated The implementation may attach the button via the
* {@link ExtendableTextComponent#addExtension(ExtendableTextComponent.Extension)}
* so that the returned button may not be visible to the users
*
* @see #setButtonVisible
* @see #setButtonEnabled
*/
@Deprecated(forRemoval = true)
public FixedSizeButton getButton() {
return myBrowseButton;
}
/**
* Do not use this class directly it is public just to hack other implementation of controls similar to TextFieldWithBrowseButton.
*/
public static final class MyDoClickAction extends DumbAwareAction {
private final FixedSizeButton myBrowseButton;
public MyDoClickAction(FixedSizeButton browseButton) {
myBrowseButton = browseButton;
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(myBrowseButton.isVisible() && myBrowseButton.isEnabled());
}
@Override
public void actionPerformed(@NotNull AnActionEvent e){
myBrowseButton.doClick();
}
public void registerShortcut(JComponent textField) {
ShortcutSet shiftEnter = new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK));
registerCustomShortcutSet(shiftEnter, textField);
myBrowseButton.setToolTipText(KeymapUtil.getShortcutsText(shiftEnter.getShortcuts()));
}
public static void addTo(FixedSizeButton browseButton, JComponent aComponent) {
new MyDoClickAction(browseButton).registerShortcut(aComponent);
}
}
public static class BrowseFolderActionListener<T extends JComponent> extends BrowseFolderRunnable <T> implements ActionListener {
public BrowseFolderActionListener(
@Nullable ComponentWithBrowseButton<T> textField,
@Nullable Project project,
FileChooserDescriptor fileChooserDescriptor,
TextComponentAccessor<? super T> accessor
) {
super(project, fileChooserDescriptor, textField != null ? textField.getChildComponent() : null, accessor);
}
/**
* @deprecated use {@link #BrowseFolderActionListener(ComponentWithBrowseButton, Project, FileChooserDescriptor, TextComponentAccessor)}
* together with {@link FileChooserDescriptor#withTitle} and {@link FileChooserDescriptor#withDescription}
*/
@Deprecated(forRemoval = true)
public BrowseFolderActionListener(
@Nullable @NlsContexts.DialogTitle String title,
@Nullable @NlsContexts.Label String description,
@Nullable ComponentWithBrowseButton<T> textField,
@Nullable Project project,
FileChooserDescriptor fileChooserDescriptor,
TextComponentAccessor<? super T> accessor
) {
this(textField, project, fileChooserDescriptor.withTitle(title).withDescription(description), accessor);
}
@Override
public void actionPerformed(ActionEvent e) {
run();
}
}
@Override
public final void requestFocus() {
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() ->
IdeFocusManager.getGlobalInstance().requestFocus(myComponent, true));
}
@SuppressWarnings("deprecation")
@Override
public final void setNextFocusableComponent(Component aComponent) {
super.setNextFocusableComponent(aComponent);
myComponent.setNextFocusableComponent(aComponent);
}
private KeyEvent myCurrentEvent = null;
@Override
protected final boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
if (condition == WHEN_FOCUSED && myCurrentEvent != e) {
try {
myCurrentEvent = e;
myComponent.dispatchEvent(e);
}
finally {
myCurrentEvent = null;
}
}
if (e.isConsumed()) return true;
return super.processKeyBinding(ks, e, condition, pressed);
}
private static final class LazyDisposable implements Activatable {
private final WeakReference<ComponentWithBrowseButton<?>> reference;
private LazyDisposable(ComponentWithBrowseButton<?> component) {
reference = new WeakReference<>(component);
}
private static void installOn(ComponentWithBrowseButton<?> component) {
LazyDisposable disposable = new LazyDisposable(component);
UiNotifyConnector.Once.installOn(component, disposable);
}
@Override
public void showNotify() {
ComponentWithBrowseButton<?> component = reference.get();
if (component == null) return; // component is collected
Application app = ApplicationManager.getApplication();
if (app != null) {
DataManager dataManager = app.getServiceIfCreated(DataManager.class);
if (dataManager != null) {
Disposable disposable = UI_DISPOSABLE.getData(dataManager.getDataContext(component));
if (disposable != null) {
Disposer.register(disposable, component);
}
}
}
}
}
}