From dc4d0a47b53808b2b517f099abf6c1379fbf5f2e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 8 Apr 2016 18:39:44 +0200 Subject: [PATCH] [platform] list models generified to match Swing interfaces --- .../openapi/ui/ComboBoxWithWidePopup.java | 26 ++++----------- .../intellij/ui/CollectionComboBoxModel.java | 10 +++--- .../com/intellij/ui/CollectionListModel.java | 4 +-- .../intellij/ui/ListCellRendererWrapper.java | 4 +-- .../openapi/fileTypes/ex/FileTypeChooser.java | 32 ++++++------------- 5 files changed, 25 insertions(+), 51 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java b/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java index 4e5a484506ae..fc282018179d 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.openapi.ui; import com.intellij.openapi.util.SystemInfo; @@ -21,37 +20,26 @@ import com.intellij.util.ui.UIUtil; import javax.swing.*; import java.awt.*; -import java.util.Vector; public class ComboBoxWithWidePopup extends JComboBox { - private boolean myLayingOut = false; private int myMinLength = 20; + public ComboBoxWithWidePopup() { } + public ComboBoxWithWidePopup(final ComboBoxModel aModel) { super(aModel); - if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25); } public ComboBoxWithWidePopup(final E[] items) { super(items); - if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25); } - public ComboBoxWithWidePopup(@SuppressWarnings("UseOfObsoleteCollectionType") final Vector items) { - super(items); - - if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25); - } - - public ComboBoxWithWidePopup() { - } - @SuppressWarnings("GtkPreferredJComboBoxRenderer") @Override - public void setRenderer(ListCellRenderer renderer) { + public void setRenderer(ListCellRenderer renderer) { super.setRenderer(new AdjustingListCellRenderer(this, renderer)); } @@ -112,8 +100,8 @@ public class ComboBoxWithWidePopup extends JComboBox { if (size.width == 0) { if (stringValue.length() > minLength) { - //noinspection unchecked - _value = (E)stringValue.substring(0, minLength); + @SuppressWarnings("unchecked") E e = (E)stringValue.substring(0, minLength); + _value = e; } } } @@ -121,4 +109,4 @@ public class ComboBoxWithWidePopup extends JComboBox { return myOldRenderer.getListCellRendererComponent(list, _value, index, isSelected, cellHasFocus); } } -} +} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/ui/CollectionComboBoxModel.java b/platform/platform-api/src/com/intellij/ui/CollectionComboBoxModel.java index 7ce6bf74097a..d1ad94d53bec 100644 --- a/platform/platform-api/src/com/intellij/ui/CollectionComboBoxModel.java +++ b/platform/platform-api/src/com/intellij/ui/CollectionComboBoxModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,12 +25,11 @@ import java.util.List; /** * @author traff */ -public class CollectionComboBoxModel extends CollectionListModel implements ComboBoxModel { +public class CollectionComboBoxModel extends CollectionListModel implements ComboBoxModel { protected T mySelection; public CollectionComboBoxModel() { super(); - mySelection = null; } @@ -40,15 +39,14 @@ public class CollectionComboBoxModel extends CollectionListModel implement public CollectionComboBoxModel(@NotNull List items, @Nullable T selection) { super(items, true); - mySelection = selection; } @Override public void setSelectedItem(@Nullable Object item) { if (mySelection != item) { - //noinspection unchecked - mySelection = (T)item; + @SuppressWarnings("unchecked") T t = (T)item; + mySelection = t; update(); } } diff --git a/platform/platform-api/src/com/intellij/ui/CollectionListModel.java b/platform/platform-api/src/com/intellij/ui/CollectionListModel.java index 945a2243d23a..c6ab4c0d6c16 100644 --- a/platform/platform-api/src/com/intellij/ui/CollectionListModel.java +++ b/platform/platform-api/src/com/intellij/ui/CollectionListModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ import java.util.*; /** * @author yole */ -public class CollectionListModel extends AbstractListModel implements EditableModel { +public class CollectionListModel extends AbstractListModel implements EditableModel { private final List myItems; public CollectionListModel(@NotNull final Collection items) { diff --git a/platform/platform-api/src/com/intellij/ui/ListCellRendererWrapper.java b/platform/platform-api/src/com/intellij/ui/ListCellRendererWrapper.java index cb5d4f2adff3..ddee6d36a89f 100644 --- a/platform/platform-api/src/com/intellij/ui/ListCellRendererWrapper.java +++ b/platform/platform-api/src/com/intellij/ui/ListCellRendererWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +36,7 @@ import static com.intellij.openapi.util.Pair.pair; * @author oleg * @since 30.09.2010 */ -public abstract class ListCellRendererWrapper implements ListCellRenderer { +public abstract class ListCellRendererWrapper implements ListCellRenderer { private final ListCellRenderer myDefaultRenderer; private boolean mySeparator; diff --git a/platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooser.java b/platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooser.java index ba49434fa5f8..7aadaee5d829 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooser.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooser.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,13 +40,12 @@ import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.event.MouseEvent; import java.util.Arrays; -import java.util.Comparator; import java.util.List; public class FileTypeChooser extends DialogWrapper { - private JList myList; + private JList myList; private JLabel myTitleLabel; - private ComboBox myPattern; + private ComboBox myPattern; private JPanel myPanel; private JRadioButton myOpenInIdea; private JRadioButton myOpenAsNative; @@ -54,32 +53,21 @@ public class FileTypeChooser extends DialogWrapper { private FileTypeChooser(@NotNull List patterns, @NotNull String fileName) { super(true); - myFileName = fileName; + myFileName = fileName; myOpenInIdea.setText("Open matching files in " + ApplicationNamesInfo.getInstance().getFullProductName() + ":"); FileType[] fileTypes = FileTypeManager.getInstance().getRegisteredFileTypes(); - Arrays.sort(fileTypes, new Comparator() { - @Override - public int compare(final FileType fileType1, final FileType fileType2) { - if (fileType1 == null) { - return 1; - } - if (fileType2 == null) { - return -1; - } - return fileType1.getDescription().compareToIgnoreCase(fileType2.getDescription()); - } - }); + Arrays.sort(fileTypes, (ft1, ft2) -> ft1 == null ? 1 : ft2 == null ? -1 : ft1.getDescription().compareToIgnoreCase(ft2.getDescription())); - final DefaultListModel model = new DefaultListModel(); + final DefaultListModel model = new DefaultListModel<>(); for (FileType type : fileTypes) { if (!type.isReadOnly() && type != FileTypes.UNKNOWN && !(type instanceof NativeFileType)) { model.addElement(type); } } myList.setModel(model); - myPattern.setModel(new CollectionComboBoxModel(ContainerUtil.map(patterns, FunctionUtil.id()), patterns.get(0))); + myPattern.setModel(new CollectionComboBoxModel<>(ContainerUtil.map(patterns, FunctionUtil.id()), patterns.get(0))); setTitle(FileTypesBundle.message("filetype.chooser.title")); init(); @@ -129,13 +117,13 @@ public class FileTypeChooser extends DialogWrapper { } public FileType getSelectedType() { - return myOpenAsNative.isSelected() ? NativeFileType.INSTANCE : (FileType) myList.getSelectedValue(); + return myOpenAsNative.isSelected() ? NativeFileType.INSTANCE : myList.getSelectedValue(); } /** * If fileName is already associated any known file type returns it. * Otherwise asks user to select file type and associates it with fileName extension if any selected. - * @return Known file type or null. Never returns {@link com.intellij.openapi.fileTypes.FileTypes#UNKNOWN}. + * @return Known file type or null. Never returns {@link FileTypes#UNKNOWN}. */ @Nullable public static FileType getKnownFileTypeOrAssociate(@NotNull VirtualFile file, @Nullable Project project) { @@ -205,4 +193,4 @@ public class FileTypeChooser extends DialogWrapper { protected String getHelpId() { return "reference.dialogs.register.association"; } -} +} \ No newline at end of file