[IDEA-314549] Refactoring TableSpeedSearchBase to avoid constructors with side effects

GitOrigin-RevId: aa483ba5d8fde5369c9ef002e518a64f47e2fbd8
This commit is contained in:
Alex Plate
2023-03-02 13:36:41 +02:00
committed by intellij-monorepo-bot
parent 49a72875dd
commit e7235fc1c5
28 changed files with 129 additions and 29 deletions

View File

@@ -49,7 +49,7 @@ public class CompilerModuleOptionsComponent extends JPanel {
ExpandableTextField editor = new ExpandableTextField();
InsertPathAction.addTo(editor, null, false);
optionsColumn.setCellEditor(new DefaultCellEditor(editor));
new TableSpeedSearch(myTable);
TableSpeedSearch.installOn(myTable);
JPanel table = ToolbarDecorator.createDecorator(myTable)
.disableUpAction()

View File

@@ -77,7 +77,7 @@ public class TargetOptionsComponent extends JPanel {
targetLevelColumn.setMinWidth(width);
targetLevelColumn.setMaxWidth(width);
new TableSpeedSearch(myTable);
TableSpeedSearch.installOn(myTable);
JLabel label = new JLabel(JavaCompilerBundle.message("settings.project.bytecode.version"));
label.setLabelFor(myCbProjectTargetLevel);

View File

@@ -99,7 +99,7 @@ public class DirDiffPanel implements Disposable, DataProvider {
myModel.setPanel(this);
Disposer.register(this, myModel);
myTable.setModel(myModel);
new TableSpeedSearch(myTable);
TableSpeedSearch.installOn(myTable);
myTable.setExpandableItemsEnabled(false);
myTable.getTableHeader().setReorderingAllowed(false);

View File

@@ -105,7 +105,7 @@ final class ProjectStartupConfigurable implements SearchableConfigurable, Config
myTable = new JBTable(myModel);
myTable.setShowGrid(false);
myTable.getEmptyText().setText(ExecutionBundle.message("settings.project.startup.add.run.configurations.with.the.button"));
new TableSpeedSearch(myTable);
TableSpeedSearch.installOn(myTable);
DefaultCellEditor defaultEditor = (DefaultCellEditor)myTable.getDefaultEditor(Object.class);
defaultEditor.setClickCountToStart(1);

View File

@@ -12,7 +12,7 @@ import com.intellij.openapi.util.RecursionManager
import com.intellij.ui.ColoredTableCellRenderer
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.TableSpeedSearch
import com.intellij.ui.layout.*
import com.intellij.ui.layout.panel
import com.intellij.ui.scale.JBUIScale
import com.intellij.ui.table.JBTable
import com.intellij.util.ui.ColumnInfo
@@ -124,7 +124,7 @@ class CommandLineDialog(
val nameColumn = columnModel.getColumn(0)
val descriptionColumn = columnModel.getColumn(1)
val search = TableSpeedSearch(this)
val search = TableSpeedSearch.installOn(this)
nameColumn.cellRenderer = Renderer(search, tableInfo.dataColumnIcon)
descriptionColumn.cellRenderer = Renderer(search, tableInfo.descriptionColumnIcon)

View File

@@ -5,13 +5,13 @@ import com.intellij.execution.util.ListTableWithButtons
import com.intellij.ide.ui.search.SearchUtil
import com.intellij.openapi.externalSystem.service.ui.properties.PropertiesTable.Property
import com.intellij.openapi.observable.properties.ObservableMutableProperty
import com.intellij.openapi.observable.util.lockOrSkip
import com.intellij.openapi.observable.util.whenTableChanged
import com.intellij.ui.ColoredTableCellRenderer
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.TableSpeedSearch
import com.intellij.ui.components.JBViewport
import com.intellij.ui.scale.JBUIScale
import com.intellij.openapi.observable.util.lockOrSkip
import com.intellij.util.ui.ColumnInfo
import com.intellij.util.ui.ListTableModel
import java.util.concurrent.atomic.AtomicBoolean
@@ -52,7 +52,7 @@ class PropertiesTable : ListTableWithButtons<Property>() {
val nameColumn = tableView.columnModel.getColumn(0)
val descriptionColumn = tableView.columnModel.getColumn(1)
val search = TableSpeedSearch(tableView)
val search = TableSpeedSearch.installOn(tableView)
nameColumn.cellRenderer = Renderer(search)
descriptionColumn.cellRenderer = Renderer(search)

View File

@@ -132,7 +132,7 @@ public class RegistryUi implements Disposable {
tb.setTargetComponent(myTable);
myContent.add(tb.getComponent(), BorderLayout.NORTH);
final TableSpeedSearch search = new TableSpeedSearch(myTable);
final TableSpeedSearch search = TableSpeedSearch.installOn(myTable);
search.setFilteringMode(true);
myTable.setRowSorter(new TableRowSorter<>(myTable.getModel()));
myTable.registerKeyboardAction(

View File

@@ -99,7 +99,7 @@ public abstract class AbstractMemberSelectionTable<T extends PsiElement, M exten
setIntercellSpacing(new Dimension(0, 0));
new MyEnableDisableAction().register();
new TableSpeedSearch(this);
TableSpeedSearch.installOn(this);
}
@NotNull

View File

@@ -472,7 +472,7 @@ public abstract class PerFileConfigurableBase<T> implements SearchableConfigurab
sorter.setSortsOnUpdates(true);
myTable.setRowSorter(sorter);
myTable.getRowSorter().setSortKeys(Collections.singletonList(new RowSorter.SortKey(0, SortOrder.ASCENDING)));
new TableSpeedSearch(myTable, o -> keyToString(o));
TableSpeedSearch.installOn(myTable, o -> keyToString(o));
FontMetrics metrics = myTable.getFontMetrics(myTable.getFont());
int maxValueWidth = 2 * metrics.stringWidth(myTable.getModel().getColumnName(1));

View File

@@ -85,7 +85,7 @@ public class InstalledPackagesPanel extends JPanel {
};
myPackagesTable.setShowGrid(false);
myPackagesTable.getTableHeader().setReorderingAllowed(false);
new TableSpeedSearch(myPackagesTable);
TableSpeedSearch.installOn(myPackagesTable);
myUpgradeButton = new DumbAwareActionButton(IdeBundle.messagePointer("action.AnActionButton.text.upgrade"), IconUtil.getMoveUpIcon()) {
@Override

View File

@@ -92,7 +92,7 @@ public abstract class PropertyTable extends JBTable {
TableHoverListener.DEFAULT.removeFrom(this);
mySpeedSearch = new TableSpeedSearch(this, (object, cell) -> {
mySpeedSearch = new TableSpeedSearch(this, null, (object, cell) -> {
if (cell.column != 0) return null;
if (object instanceof GroupProperty) return null;
return ((Property<?>)object).getName();
@@ -103,6 +103,7 @@ public abstract class PropertyTable extends JBTable {
repaint(PropertyTable.this.getVisibleRect());
}
};
mySpeedSearch.setupListeners();
mySpeedSearch.setComparator(new SpeedSearchComparator(false, false));
// TODO: Updates UI after LAF updated

View File

@@ -99,7 +99,7 @@ public class MultiStateElementsChooser<T, S> extends JPanel implements Component
columnModel.getColumn(myTableModel.ELEMENT_COLUMN_INDEX).setCellRenderer(new MyElementColumnCellRenderer());
add(pane, BorderLayout.CENTER);
TableSpeedSearch speedSearch = new TableSpeedSearch(myTable);
TableSpeedSearch speedSearch = TableSpeedSearch.installOn(myTable);
speedSearch.setFilteringMode(true);
myTable.setRowSorter(new TableRowSorter<>(myTable.getModel()));
myTable.registerKeyboardAction(

View File

@@ -83,7 +83,7 @@ final class InspectorTable extends JBSplitter implements DataProvider, Disposabl
setSplitterProportionKey("UiInspector.table.splitter.proportion");
myTable = new StripeTable(myModel);
new TableSpeedSearch(myTable);
TableSpeedSearch.installOn(myTable);
TableColumnModel columnModel = myTable.getColumnModel();
TableColumn propertyColumn = columnModel.getColumn(0);

View File

@@ -251,7 +251,7 @@ public class ShowUIDefaultsAction extends AnAction implements DumbAware {
}
});
new TableSpeedSearch(table, (o, cell) -> cell.column == 1 ? null : String.valueOf(o));
TableSpeedSearch.installOn(table, (o, cell) -> cell.column == 1 ? null : String.valueOf(o));
table.setShowGrid(false);
TableHoverListener.DEFAULT.removeFrom(table);
myTable = table;

View File

@@ -26,14 +26,71 @@ public class TableSpeedSearch extends TableSpeedSearchBase<JTable> {
private static final PairFunction<Object, Cell, String> TO_STRING = (o, cell) -> o == null || o instanceof Boolean ? "" : o.toString();
private final PairFunction<Object, ? super Cell, String> myToStringConvertor;
/**
* @param sig parameter is used to avoid clash with the deprecated constructor
*/
protected TableSpeedSearch(JTable table, Void sig, final Convertor<Object, String> toStringConvertor) {
this(table, sig, (o, c) -> toStringConvertor.convert(o));
}
/**
* @param sig parameter is used to avoid clash with the deprecated constructor
*/
protected TableSpeedSearch(JTable table, Void sig, final PairFunction<Object, ? super Cell, String> toStringConvertor) {
super(table, sig);
myToStringConvertor = toStringConvertor;
}
public static @NotNull TableSpeedSearch installOn(JTable table) {
return installOn(table, TO_STRING);
}
public static @NotNull TableSpeedSearch installOn(JTable table, final Convertor<Object, String> toStringConvertor) {
return installOn(table, (o, c) -> toStringConvertor.convert(o));
}
public static @NotNull TableSpeedSearch installOn(JTable table, final PairFunction<Object, ? super Cell, String> toStringConvertor) {
TableSpeedSearch search = new TableSpeedSearch(table, null, toStringConvertor);
search.setupListeners();
return search;
}
/**
* @deprecated Use the static method {@link TableSpeedSearch#installOn(JTable)} to install a speed search.
* <p>
* For inheritance use the non-deprecated constructor.
* <p>
* Also, note that non-deprecated constructor is side effect free, and you should call for {@link TableSpeedSearch#setupListeners()}
* method to enable speed search
*/
@Deprecated
public TableSpeedSearch(JTable table) {
this(table, TO_STRING);
}
/**
* @deprecated Use the static method {@link TableSpeedSearch#installOn(JTable, Convertor)} to install a speed search.
* <p>
* For inheritance use the non-deprecated constructor.
* <p>
* Also, note that non-deprecated constructor is side effect free, and you should call for {@link TableSpeedSearch#setupListeners()}
* method to enable speed search
*/
@Deprecated
public TableSpeedSearch(JTable table, final Convertor<Object, String> toStringConvertor) {
this(table, (o, c) -> toStringConvertor.convert(o));
}
/**
* @deprecated Use the static method {@link TableSpeedSearch#installOn(JTable, PairFunction)} to install a speed search.
* <p>
* For inheritance use the non-deprecated constructor.
* <p>
* Also, note that non-deprecated constructor is side effect free, and you should call for {@link TableSpeedSearch#setupListeners()}
* method to enable speed search
*/
@Deprecated
public TableSpeedSearch(JTable table, final PairFunction<Object, ? super Cell, String> toStringConvertor) {
super(table);
@@ -44,6 +101,15 @@ public class TableSpeedSearch extends TableSpeedSearchBase<JTable> {
new MySelectAllAction(table, this).registerCustomShortcutSet(table, null);
}
@Override
public void setupListeners() {
super.setupListeners();
// edit on F2 & double click, do not interfere with quick search
myComponent.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
new MySelectAllAction(myComponent, this).registerCustomShortcutSet(myComponent, null);
}
@Override
protected boolean isSpeedSearchEnabled() {
boolean tableIsNotEmpty = myComponent.getRowCount() != 0 && myComponent.getColumnCount() != 0;

View File

@@ -20,6 +20,20 @@ public abstract class TableSpeedSearchBase<Comp extends JTable> extends SpeedSea
private boolean myFilteringMode;
/**
* @param sig parameter is used to avoid clash with the deprecated constructor
*/
protected TableSpeedSearchBase(Comp component, Void sig) {
super(component, sig);
}
/**
* @deprecated For inheritance use the non-deprecated constructor.
* <p>
* Also, note that non-deprecated constructor is side effect free, and you should call for {@link SpeedSearchBase#setupListeners()}
* method to enable speed search
*/
@Deprecated
public TableSpeedSearchBase(Comp component) {
super(component);
}

View File

@@ -25,6 +25,20 @@ import java.util.List;
* @author Gregory.Shrago
*/
public abstract class TableViewSpeedSearch<Item> extends TableSpeedSearchBase<TableView<Item>> {
/**
* @param sig parameter is used to avoid clash with the deprecated constructor
*/
protected TableViewSpeedSearch(TableView<Item> component, Void sig) {
super(component, sig);
setComparator(new SpeedSearchComparator(false));
}
/**
* @deprecated For inheritance use the non-deprecated constructor.
* <p>
* Also, note that non-deprecated constructor is side effect free, and you should call for {@link SpeedSearchBase#setupListeners()}
* method to enable speed search
*/
public TableViewSpeedSearch(TableView<Item> component) {
super(component);
setComparator(new SpeedSearchComparator(false));

View File

@@ -58,7 +58,7 @@ public class TableModelEditor<T> extends CollectionModelEditor<T, CollectionItem
table.setEnableAntialiasing(true);
table.setPreferredScrollableViewportSize(JBUI.size(200, -1));
table.setVisibleRowCount(JBTable.PREFERRED_SCROLLABLE_VIEWPORT_HEIGHT_IN_ROWS);
new TableSpeedSearch(table);
TableSpeedSearch.installOn(table);
ColumnInfo firstColumn = columns[0];
if ((firstColumn.getColumnClass() == boolean.class || firstColumn.getColumnClass() == Boolean.class) && firstColumn.getName().isEmpty()) {
TableUtil.setupCheckboxColumn(table.getColumnModel().getColumn(0), 0);

View File

@@ -143,12 +143,13 @@ public final class ShowFeatureUsageStatisticsDialog extends DialogWrapper {
}
}
TableView<FeatureDescriptor> table = new TableView<>(new ListTableModel<>(COLUMNS, features, 0));
new TableViewSpeedSearch<>(table) {
TableViewSpeedSearch<FeatureDescriptor> search = new TableViewSpeedSearch<>(table, null) {
@Override
protected String getItemText(@NotNull FeatureDescriptor element) {
return element.getDisplayName();
}
};
search.setupListeners();
JPanel controlsPanel = new JPanel(new VerticalFlowLayout());

View File

@@ -244,7 +244,7 @@ public class VcsDirectoryConfigurationPanel extends JPanel implements Disposable
myDirectoryMappingTable = new TableView<>();
myDirectoryMappingTable.setShowGrid(false);
myDirectoryMappingTable.setIntercellSpacing(JBUI.emptySize());
new TableSpeedSearch(myDirectoryMappingTable, info -> {
TableSpeedSearch.installOn(myDirectoryMappingTable, info -> {
return info instanceof MapInfo ? MyDirectoryRenderer.getPresentablePath(myProject, ((MapInfo)info).mapping) : "";
});

View File

@@ -151,7 +151,7 @@ public final class FileHistoryPanelImpl extends JPanel implements DataProvider,
storageKey, myVcs.getProject());
myDualView.switchToTheFlatMode();
}
new TableSpeedSearch(myDualView.getFlatView()).setComparator(new SpeedSearchComparator(false));
TableSpeedSearch.installOn(myDualView.getFlatView()).setComparator(new SpeedSearchComparator(false));
final TableLinkMouseListener listener = new TableLinkMouseListener();
listener.installOn(myDualView.getFlatView());
listener.installOn(myDualView.getTreeView());

View File

@@ -39,7 +39,10 @@ import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.ui.DoubleClickListener
import com.intellij.ui.TableSpeedSearch
import com.intellij.ui.UIBundle
import com.intellij.ui.dsl.builder.*
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.AlignY
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns
import com.intellij.ui.treeStructure.treetable.TreeTable
import com.intellij.ui.treeStructure.treetable.TreeTableModel
@@ -119,7 +122,7 @@ open class MultipleFileMergeDialog(
}
}.installOn(table.tree)
TableSpeedSearch(table, Convertor { (it as? VirtualFile)?.name })
TableSpeedSearch.installOn(table, Convertor { (it as? VirtualFile)?.name })
val modalityState = ModalityState.stateForComponent(descriptionLabel)
BackgroundTaskUtil.executeOnPooledThread(disposable, Runnable {

View File

@@ -511,13 +511,14 @@ public final class InjectionsSettingsUI extends SearchableConfigurable.Parent.Ab
getColumnModel().getColumn(2).setMinWidth(preferred);
getColumnModel().getColumn(2).setPreferredWidth(preferred);
getColumnModel().getColumn(2).setMaxWidth(preferred);
new TableViewSpeedSearch<>(this) {
TableViewSpeedSearch<InjInfo> search = new TableViewSpeedSearch<>(this, null) {
@Override
protected String getItemText(@NotNull InjInfo element) {
final BaseInjection injection = element.injection;
return injection.getSupportId() + " " + injection.getInjectedLanguageId() + " " + injection.getDisplayName();
}
};
search.setupListeners();
}
}

View File

@@ -81,7 +81,7 @@ internal open class GitRebaseCommitsTableView(
}
private fun installSpeedSearch() {
TableSpeedSearch(this) { o, cell -> o.toString().takeIf { cell.column == GitRebaseCommitsTableModel.SUBJECT_COLUMN } }
TableSpeedSearch.installOn(this) { o, cell -> o.toString().takeIf { cell.column == GitRebaseCommitsTableModel.SUBJECT_COLUMN } }
}
private fun prepareCommitIconColumn() {

View File

@@ -20,7 +20,6 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.TableSpeedSearch;
import com.intellij.ui.UIBundle;
import com.intellij.ui.table.JBTable;
import com.intellij.util.concurrency.annotations.RequiresEdt;
import com.intellij.util.containers.ContainerUtil;
@@ -87,7 +86,7 @@ public class HgMqUnAppliedPatchesPanel extends JPanel implements DataProvider, H
myPatchTable.getEmptyText().setText(IdeCoreBundle.message("message.nothingToShow"));
myPatchTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), START_EDITING);
myPatchTable.setDragEnabled(true);
new TableSpeedSearch(myPatchTable);
TableSpeedSearch.installOn(myPatchTable);
myPatchTable.setDropMode(DropMode.INSERT_ROWS);
myPatchTable.setTransferHandler(new TableRowsTransferHandler(myPatchTable));

View File

@@ -32,7 +32,7 @@ class MavenCatalogsTable(private val project: Project) : ListTableWithButtons<Ma
val typeColumn = tableView.columnModel.getColumn(1)
val locationColumn = tableView.columnModel.getColumn(2)
val search = TableSpeedSearch(tableView)
val search = TableSpeedSearch.installOn(tableView)
nameColumn.cellRenderer = CatalogNameRenderer(search)
typeColumn.cellRenderer = Renderer(search)
locationColumn.cellRenderer = Renderer(search)

View File

@@ -212,7 +212,7 @@ internal class PackagesTable(
}
})
TableSpeedSearch(this) { item, _ ->
TableSpeedSearch.installOn(this) { item, _ ->
if (item is PackagesTableItem<*>) {
val rawIdentifier = item.packageModel.identifier.rawValue
val name = item.packageModel.remoteInfo?.name?.takeIf { !it.equals(rawIdentifier, ignoreCase = true) }

View File

@@ -236,12 +236,13 @@ public class ToBeMergedDialog extends DialogWrapper {
}
};
myRevisionsList.setExpandableItemsEnabled(false);
new TableViewSpeedSearch<>(myRevisionsList) {
TableViewSpeedSearch<SvnChangeList> search = new TableViewSpeedSearch<>(myRevisionsList, null) {
@Override
protected String getItemText(@NotNull SvnChangeList element) {
return element.getComment();
}
};
search.setupListeners();
myRevisionsList.setModelAndUpdateColumns(myRevisionsModel);
myRevisionsList.setTableHeader(null);
myRevisionsList.setShowGrid(false);