[IDEA-314549] Refactoring in order to get rid of constructors with side effects

GitOrigin-RevId: 6b6b7b5d6ea8f9a435ee613f53b0045d96590c67
This commit is contained in:
Alex Plate
2023-03-02 16:28:04 +00:00
committed by intellij-monorepo-bot
parent 1d8b7a26f7
commit 1d7f08cb28
35 changed files with 95 additions and 44 deletions
@@ -134,7 +134,7 @@ public class MoveInnerToUpperOrMembersHandler extends MoveHandlerDelegate {
gr.add(myRbMoveInner);
gr.add(myRbMoveMembers);
new RadioUpDownListener(myRbMoveInner, myRbMoveMembers);
RadioUpDownListener.installOn(myRbMoveInner, myRbMoveMembers);
Box box = Box.createVerticalBox();
box.add(Box.createVerticalStrut(5));
@@ -110,7 +110,7 @@ public class BaseAnalysisActionDialog extends DialogWrapper {
getAdditionalActionSettings(myProject));
preselectButton();
new RadioUpDownListener(radioButtons.toArray(new JRadioButton[0]));
RadioUpDownListener.installOn(radioButtons.toArray(new JRadioButton[0]));
panel.setPreferredSize(panel.getMinimumSize());
return panel;
@@ -30,6 +30,7 @@ public final class Util{
@Override
public boolean canClose(String inputString) {
try {
//noinspection ResultOfObjectAllocationIgnored
new URL(inputString);
return true;
}
@@ -38,7 +38,7 @@ public class DefaultValueChooser extends DialogWrapper{
public DefaultValueChooser(Project project, String name, String defaultValue) {
super(project);
new RadioUpDownListener(myLeaveBlankRadioButton, myFeelLuckyRadioButton, myUseValueRadioButton);
RadioUpDownListener.installOn(myLeaveBlankRadioButton, myFeelLuckyRadioButton, myUseValueRadioButton);
final ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -87,7 +87,7 @@ public abstract class InlineOptionsDialog extends RefactoringDialog implements I
for (JRadioButton button : buttons) {
bg.add(button);
}
new RadioUpDownListener(buttons);
RadioUpDownListener.installOn(buttons);
myRbInlineThisOnly.setEnabled(myInvokedOnReference);
myRbInlineAll.setEnabled(writable);
@@ -172,7 +172,7 @@ public class RenameHandlerRegistry {
bg.add(rb);
radioPanel.add(rb);
}
new RadioUpDownListener(myRButtons);
RadioUpDownListener.installOn(myRButtons);
return radioPanel;
}
@@ -11,9 +11,18 @@ import java.awt.event.KeyEvent;
public class RadioUpDownListener extends KeyAdapter {
private final JRadioButton[] myRadioButtons;
public RadioUpDownListener(final JRadioButton... radioButtons) {
private RadioUpDownListener(final JRadioButton... radioButtons) {
myRadioButtons = radioButtons;
for (JRadioButton radioButton : radioButtons) {
}
public static RadioUpDownListener installOn(final JRadioButton... radioButtons) {
RadioUpDownListener listener = new RadioUpDownListener(radioButtons);
listener.setupListeners();
return listener;
}
private void setupListeners() {
for (JRadioButton radioButton : myRadioButtons) {
radioButton.addKeyListener(this);
}
}
@@ -250,6 +250,7 @@ public final class DnDSupport implements DnDTarget, DnDSource, DnDDropHandler.Wi
@Override
public void install() {
//noinspection ResultOfObjectAllocationIgnored
new DnDSupport(myComponent,
beanProvider.get(),
imageProvider.get(),
@@ -94,7 +94,7 @@ public class ComponentWithBrowseButton<Comp extends JComponent> extends JPanel i
} else if (Registry.is("ide.browse.button.always.focusable", false)) {
myBrowseButton.setFocusable(true);
}
new LazyDisposable(this);
LazyDisposable.installOn(this);
Insets insets = myComponent.getInsets();
Gaps visualPaddings = new Gaps(insets.top, insets.left, insets.bottom, inlineBrowseButton ? insets.right : myBrowseButton.getInsets().right);
@@ -329,7 +329,11 @@ public class ComponentWithBrowseButton<Comp extends JComponent> extends JPanel i
private LazyDisposable(ComponentWithBrowseButton<?> component) {
reference = new WeakReference<>(component);
UiNotifyConnector.Once.installOn(component, this);
}
private static void installOn(ComponentWithBrowseButton<?> component) {
LazyDisposable disposable = new LazyDisposable(component);
UiNotifyConnector.Once.installOn(component, disposable);
}
@Override
@@ -177,7 +177,7 @@ public class JBTable extends JTable implements ComponentWithEmptyText, Component
myUiUpdating = false;
new MyCellEditorRemover();
new MyCellEditorRemover().setupListeners();
}
protected void onTableChanged(@NotNull TableModelEvent e) {
@@ -798,7 +798,7 @@ public class JBTable extends JTable implements ComponentWithEmptyText, Component
private final class MyCellEditorRemover implements PropertyChangeListener, Activatable {
private boolean myIsActive = false;
MyCellEditorRemover() {
private void setupListeners() {
addPropertyChangeListener("tableCellEditor", this);
UiNotifyConnector.installOn(JBTable.this, this);
}
@@ -389,7 +389,7 @@ public class JBTabsImpl extends JComponent
}
});
new LazyUiDisposable<>(parentDisposable, this, this) {
LazyUiDisposable<JBTabsImpl> listener1 = new LazyUiDisposable<>(parentDisposable, this, this) {
@Override
protected void initialize(@NotNull Disposable parent, @NotNull JBTabsImpl child, @Nullable Project project) {
if (myProject == null && project != null) {
@@ -418,6 +418,7 @@ public class JBTabsImpl extends JComponent
}
}
};
listener1.setupListeners();
ComponentUtil.putClientProperty(this, UIUtil.NOT_IN_HIERARCHY_COMPONENTS,
(Iterable<? extends Component>)(Iterable<JComponent>)() -> {
return JBIterable.from(getVisibleInfos())
@@ -81,6 +81,7 @@ public final class DateTimeFormatManager implements PersistentStateComponent<Ele
public void setDateFormatPattern(@NotNull String pattern) {
try {
//noinspection ResultOfObjectAllocationIgnored
new SimpleDateFormat(pattern);
myPattern = pattern;
} catch (Exception ignored) {
@@ -26,8 +26,10 @@ public abstract class LazyUiDisposable<T> implements Activatable {
myUI = new AtomicReference<>(ui);
myParent = parent;
myChild = child;
}
UiNotifyConnector.Once.installOn(ui, this);
public void setupListeners() {
UiNotifyConnector.Once.installOn(myUI.get(), this);
}
@Override
@@ -136,6 +136,7 @@ public final class DnDManagerImpl extends DnDManager {
@Override
public void registerTarget(DnDTarget target, JComponent component) {
component.putClientProperty(TARGET_KEY, target);
//noinspection ResultOfObjectAllocationIgnored
new DropTarget(component, DnDConstants.ACTION_COPY_OR_MOVE, myDropTargetListener);
}
@@ -555,6 +555,7 @@ final class ComponentPanelTestAction extends DumbAwareAction {
ComponentWithBrowseButton<EditorTextField> etfbb = new ComponentWithBrowseButton<>(editor, e -> System.out.println("JTextField browse button pressed"));
new ComponentValidator(getDisposable()).withValidator(() -> {
try {
//noinspection ResultOfObjectAllocationIgnored
new URL(etfbb.getChildComponent().getDocument().getText());
return null;
} catch (MalformedURLException mex) {
@@ -1142,14 +1142,16 @@ public final class NotificationsManagerImpl extends NotificationsManager {
private boolean myHandleDispose = true;
private BalloonPopupSupport(@NotNull JPopupMenu popupMenu,
@NotNull Balloon balloon,
@NotNull JComponent component,
@NotNull Alarm popupAlarm) {
myPopupMenu = popupMenu;
myComponent = component;
myAlarm = popupAlarm;
popupAlarm.cancelAllRequests();
popupMenu.addPopupMenuListener(this);
}
private void setupListeners(@NotNull Balloon balloon) {
myAlarm.cancelAllRequests();
myPopupMenu.addPopupMenuListener(this);
Disposer.register(balloon, this);
}
@@ -1213,7 +1215,7 @@ public final class NotificationsManagerImpl extends NotificationsManager {
JPopupMenu menu = showPopup(link, group);
Balloon balloon = notification.getBalloon();
if (menu != null && balloon != null) {
new BalloonPopupSupport(menu, balloon, link, popupAlarm);
new BalloonPopupSupport(menu, link, popupAlarm).setupListeners(balloon);
}
}
@@ -130,12 +130,13 @@ public class FileTextFieldImpl implements FileTextField, Disposable {
myCancelAction = new CancelAction();
new LazyUiDisposable<>(parent, field, this) {
LazyUiDisposable<FileTextFieldImpl> disposable = new LazyUiDisposable<>(parent, field, this) {
@Override
protected void initialize(@NotNull Disposable parent, @NotNull FileTextFieldImpl child, @Nullable Project project) {
Disposer.register(child, myUiUpdater);
}
};
disposable.setupListeners();
}
@SuppressWarnings("unused") //used by rider
@@ -69,7 +69,7 @@ public class LocalFileSystemImpl extends LocalFileSystemBase implements Disposab
myWatchRootsManager = new WatchRootsManager(myWatcher, this);
Disposer.register(ApplicationManager.getApplication(), this);
new SymbolicLinkRefresher(this);
new SymbolicLinkRefresher(this).refresh();
}
public @NotNull FileWatcher getFileWatcher() {
@@ -35,7 +35,10 @@ final class SymbolicLinkRefresher {
SymbolicLinkRefresher(LocalFileSystemImpl system) {
mySystem = system;
ApplicationManager.getApplication().getMessageBus().connect(system).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
}
void refresh() {
ApplicationManager.getApplication().getMessageBus().connect(mySystem).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
@Override
public void after(@NotNull List<? extends @NotNull VFileEvent> events) {
analyzeEvents(events);
@@ -88,6 +88,7 @@ public class SingleRemoteServerConfigurable extends NamedConfigurable<RemoteServ
myConnectionTester.testConnection();
}
};
myRunner.queueChangesCheck();
}
private static <C extends ServerConfiguration> RemoteServerConfigurable createConfigurable(RemoteServer<C> server, C configuration) {
@@ -33,12 +33,14 @@ public abstract class DelayedRunner implements Disposable {
private int myChangesPastTime = NO_CHANGES;
/**
* Call {@link DelayedRunner#queueChangesCheck()} to start the runner
*/
public DelayedRunner(@NotNull JComponent activationComponent) {
myAlarm = new Alarm(activationComponent, this);
queueChangesCheck();
}
private void queueChangesCheck() {
public void queueChangesCheck() {
if (myAlarm.isDisposed()) {
return;
}
@@ -243,6 +243,7 @@ public final class InjectorUtils {
}
if (hasReChars) {
try {
//noinspection ResultOfObjectAllocationIgnored
new URL(s);
}
catch (MalformedURLException e) {
@@ -289,7 +289,7 @@ public final class AntBuildMessageView extends JPanel implements DataProvider, O
ijMessageView.getContentManager().addContent(content);
ijMessageView.getContentManager().setSelectedContent(content);
content.setDisposer(() -> Disposer.dispose(messageView));
new CloseListener(content, ijMessageView.getContentManager(), project);
new CloseListener(content, ijMessageView.getContentManager(), project).setupListeners();
if (!buildFile.isRunInBackground()) {
final ToolWindow tw = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.MESSAGES_WINDOW);
@@ -564,10 +564,13 @@ public final class AntBuildMessageView extends JPanel implements DataProvider, O
myContent = content;
myContentManager = contentManager;
myProject = project;
contentManager.addContentManagerListener(this);
}
private void setupListeners() {
myContentManager.addContentManagerListener(this);
ProjectManager.getInstance().addProjectManagerListener(myProject, this);
Disposer.register(content, () -> {
Disposer.register(myContent, () -> {
myContentManager.removeContentManagerListener(this);
ProjectManager.getInstance().removeProjectManagerListener(myProject, this);
});
@@ -104,7 +104,7 @@ public class GrIntroduceFieldDialog extends DialogWrapper implements GrIntroduce
for (JRadioButton init : inits) {
initialization.add(init);
}
new RadioUpDownListener(inits.toArray(new JRadioButton[0]));
RadioUpDownListener.installOn(inits.toArray(new JRadioButton[0]));
if (clazz instanceof GroovyScriptClass) {
myClassConstructorSRadioButton.setEnabled(false);
@@ -231,7 +231,7 @@ public class GrIntroduceFieldDialog extends DialogWrapper implements GrIntroduce
else {
myPrivateRadioButton.setSelected(true);
}
new RadioUpDownListener(myPrivateRadioButton, myProtectedRadioButton, myPublicRadioButton, myPropertyRadioButton);
RadioUpDownListener.installOn(myPrivateRadioButton, myProtectedRadioButton, myPublicRadioButton, myPropertyRadioButton);
}
private static boolean isAlwaysInvokedConstructor(@Nullable PsiMethod method, @NotNull PsiClass clazz) {
@@ -145,7 +145,7 @@ public final class RenamePropertyUtil {
box.add(myRbRenameMember);
panel.add(box, BorderLayout.CENTER);
new RadioUpDownListener(myRbRenameMember, myRbRenameProperty);
RadioUpDownListener.installOn(myRbRenameMember, myRbRenameProperty);
return panel;
}
@@ -48,7 +48,7 @@ internal class KotlinSelectNestedClassRefactoringDialog private constructor(
add(moveMembersButton)
}
RadioUpDownListener(moveToUpperLevelButton, moveMembersButton)
RadioUpDownListener.installOn(moveToUpperLevelButton, moveMembersButton)
return JPanel(BorderLayout()).apply {
val box = Box.createVerticalBox().apply {
@@ -47,7 +47,7 @@ public final class ComponentTreeBuilder implements ComponentSelectionListener, D
tree.setArea(myTreeArea);
designer.handleTreeArea(myTreeArea);
new TreeDropListener(tree, myTreeArea, designer.getToolProvider());
TreeDropListener.installOn(tree, myTreeArea, designer.getToolProvider());
selectFromSurface();
expandFromState();
@@ -51,21 +51,30 @@ public class TreeDropListener extends DropTargetAdapter {
private boolean myExecuteEnabled;
private boolean myShowFeedback;
public TreeDropListener(ComponentTree tree, EditableArea area, ToolProvider provider) {
this(tree, area, provider, TreeDropListener.class, PaletteItem.class);
if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
tree.setDragEnabled(true);
tree.setTransferHandler(new TreeTransfer(TreeDropListener.class));
}
private TreeDropListener(EditableArea area, ToolProvider provider) {
this(area, provider, TreeDropListener.class, PaletteItem.class);
}
public TreeDropListener(JComponent component, EditableArea area, ToolProvider provider, Class... dragTargets) {
private TreeDropListener(EditableArea area, ToolProvider provider, Class... dragTargets) {
myArea = area;
myContext.setArea(area);
myToolProvider = provider;
myDragTargets = dragTargets;
}
public static void installOn(ComponentTree tree, EditableArea area, ToolProvider provider) {
TreeDropListener listener = new TreeDropListener(area, provider);
if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
component.setDropTarget(new DropTarget(component, this));
tree.setDragEnabled(true);
tree.setTransferHandler(new TreeTransfer(TreeDropListener.class));
tree.setDropTarget(new DropTarget(tree, listener));
}
}
public static void installOn(JComponent component, EditableArea area, ToolProvider provider, Class... dragTargets) {
TreeDropListener listener = new TreeDropListener(area, provider, dragTargets);
if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
component.setDropTarget(new DropTarget(component, listener));
}
}
@@ -40,7 +40,7 @@ public class GlassLayer extends JComponent implements DataProvider {
myToolProvider = provider;
myArea = area;
enableEvents(EVENT_FLAGS);
new TreeDropListener(this, area, provider, PaletteItem.class);
TreeDropListener.installOn(this, area, provider, PaletteItem.class);
}
@Override
@@ -20,7 +20,6 @@ public abstract class SelectionWatcher {
myEditor = editor;
myChangeListener = new MyPropertyChangeListener();
myRootContainer = editor.getRootContainer();
install(myRootContainer);
myHierarchyChangeListener = new HierarchyChangeListener() {
@Override
@@ -32,7 +31,11 @@ public abstract class SelectionWatcher {
}
}
};
editor.addHierarchyChangeListener(myHierarchyChangeListener);
}
public void setupListeners() {
install(myRootContainer);
myEditor.addHierarchyChangeListener(myHierarchyChangeListener);
}
public void dispose() {
@@ -55,6 +55,7 @@ public final class ComponentTreeBuilder implements Disposable {
myEditor = editor;
mySelectionWatcher = new MySelectionWatcher(editor);
mySelectionWatcher.setupListeners();
syncSelection();
@@ -39,7 +39,7 @@ final class ActiveDecorationLayer extends JComponent implements FeedbackLayer {
}
public void installSelectionWatcher() {
new MyNavigateButtonSelectionWatcher(myEditor);
new MyNavigateButtonSelectionWatcher(myEditor).setupListeners();
}
@Override
@@ -339,6 +339,7 @@ public final class GuiEditor extends JPanel implements DesignerEditorPanelFacade
myDropTargetListener = new DesignDropTargetListener(this);
if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
//noinspection ResultOfObjectAllocationIgnored
new DropTarget(getGlassLayer(), DnDConstants.ACTION_COPY_OR_MOVE, myDropTargetListener);
}
@@ -58,7 +58,7 @@ public abstract class QuickFixManager <T extends JComponent>{
myComponent.addFocusListener(new FocusListenerImpl(this));
// Alt+Enter
new ShowHintAction(this, component);
new ShowHintAction(this).registerShortcutSet(component);
viewPort.addChangeListener(new ChangeListener() {
@Override
@@ -12,8 +12,11 @@ import javax.swing.*;
final class ShowHintAction extends AnAction {
private final QuickFixManager myManager;
ShowHintAction(@NotNull final QuickFixManager manager, @NotNull final JComponent component) {
ShowHintAction(@NotNull final QuickFixManager manager) {
myManager = manager;
}
void registerShortcutSet(@NotNull JComponent component) {
registerCustomShortcutSet(
ActionManager.getInstance().getAction(IdeActions.ACTION_SHOW_INTENTION_ACTIONS).getShortcutSet(),
component