Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.98 KB | None | 0 0
  1. ackage net.runelite.client.plugins.config;
  2.  
  3. import com.google.common.base.Splitter;
  4. import com.google.common.base.Strings;
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.Component;
  8. import java.awt.Dimension;
  9. import java.awt.event.FocusAdapter;
  10. import java.awt.event.FocusEvent;
  11. import java.awt.event.ItemEvent;
  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.WindowAdapter;
  15. import java.awt.event.WindowEvent;
  16. import java.awt.image.BufferedImage;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. import java.util.List;
  21. import java.util.concurrent.ScheduledExecutorService;
  22. import java.util.stream.Collectors;
  23. import javax.swing.BorderFactory;
  24. import javax.swing.ImageIcon;
  25. import javax.swing.JButton;
  26. import javax.swing.JCheckBox;
  27. import javax.swing.JColorChooser;
  28. import javax.swing.JComboBox;
  29. import javax.swing.JComponent;
  30. import javax.swing.JFormattedTextField;
  31. import javax.swing.JFrame;
  32. import javax.swing.JLabel;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.JPanel;
  35. import javax.swing.JPasswordField;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JSpinner;
  38. import javax.swing.JTextArea;
  39. import javax.swing.ScrollPaneConstants;
  40. import javax.swing.SpinnerModel;
  41. import javax.swing.SpinnerNumberModel;
  42. import javax.swing.border.EmptyBorder;
  43. import javax.swing.event.ChangeListener;
  44. import javax.swing.event.DocumentEvent;
  45. import javax.swing.event.DocumentListener;
  46. import javax.swing.text.JTextComponent;
  47. import lombok.extern.slf4j.Slf4j;
  48. import net.runelite.client.config.ChatColorConfig;
  49. import net.runelite.client.config.Config;
  50. import net.runelite.client.config.ConfigDescriptor;
  51. import net.runelite.client.config.ConfigGroup;
  52. import net.runelite.client.config.ConfigItem;
  53. import net.runelite.client.config.ConfigItemDescriptor;
  54. import net.runelite.client.config.ConfigManager;
  55. import net.runelite.client.config.Keybind;
  56. import net.runelite.client.config.RuneLiteConfig;
  57. import net.runelite.client.plugins.Plugin;
  58. import net.runelite.client.plugins.PluginDescriptor;
  59. import net.runelite.client.plugins.PluginInstantiationException;
  60. import net.runelite.client.plugins.PluginManager;
  61. import net.runelite.client.ui.ColorScheme;
  62. import net.runelite.client.ui.DynamicGridLayout;
  63. import net.runelite.client.ui.PluginPanel;
  64. import net.runelite.client.ui.components.ComboBoxListRenderer;
  65. import net.runelite.client.ui.components.IconButton;
  66. import net.runelite.client.ui.components.IconTextField;
  67. import net.runelite.client.util.ImageUtil;
  68.  
  69. @Slf4j
  70. public class ConfigPanel extends PluginPanel
  71. {
  72.     private static final int SPINNER_FIELD_WIDTH = 6;
  73.     private static final int SCROLLBAR_WIDTH = 17;
  74.     private static final int OFFSET = 6;
  75.     private static final ImageIcon BACK_ICON;
  76.     private static final ImageIcon BACK_ICON_HOVER;
  77.  
  78.     private static final String RUNELITE_GROUP_NAME = RuneLiteConfig.class.getAnnotation(ConfigGroup.class).value();
  79.     private static final String PINNED_PLUGINS_CONFIG_KEY = "pinnedPlugins";
  80.     private static final String RUNELITE_PLUGIN = "RuneLite";
  81.     private static final String CHAT_COLOR_PLUGIN = "Chat Color";
  82.     private static final Splitter COMMA_SPLITTER = Splitter.on(',');
  83.  
  84.     private final PluginManager pluginManager;
  85.     private final ConfigManager configManager;
  86.     private final ScheduledExecutorService executorService;
  87.     private final RuneLiteConfig runeLiteConfig;
  88.     private final ChatColorConfig chatColorConfig;
  89.     private final List<PluginListItem> pluginList = new ArrayList<>();
  90.  
  91.     private final IconTextField searchBar = new IconTextField();
  92.     private final JPanel topPanel;
  93.     private final JPanel mainPanel;
  94.     private final JScrollPane scrollPane;
  95.  
  96.     private boolean showingPluginList = true;
  97.     private int scrollBarPosition = 0;
  98.  
  99.     static
  100.     {
  101.         final BufferedImage backIcon = ImageUtil.getResourceStreamFromClass(ConfigPanel.class, "config_back_icon.png");
  102.         BACK_ICON = new ImageIcon(backIcon);
  103.         BACK_ICON_HOVER = new ImageIcon(ImageUtil.alphaOffset(backIcon, -100));
  104.     }
  105.  
  106.     ConfigPanel(PluginManager pluginManager, ConfigManager configManager, ScheduledExecutorService executorService,
  107.         RuneLiteConfig runeLiteConfig, ChatColorConfig chatColorConfig)
  108.     {
  109.         super(false);
  110.         this.pluginManager = pluginManager;
  111.         this.configManager = configManager;
  112.         this.executorService = executorService;
  113.         this.runeLiteConfig = runeLiteConfig;
  114.         this.chatColorConfig = chatColorConfig;
  115.  
  116.         searchBar.setIcon(IconTextField.Icon.SEARCH);
  117.         searchBar.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH - 20, 30));
  118.         searchBar.setBackground(ColorScheme.DARKER_GRAY_COLOR);
  119.         searchBar.setHoverBackgroundColor(ColorScheme.DARK_GRAY_HOVER_COLOR);
  120.         searchBar.getDocument().addDocumentListener(new DocumentListener()
  121.         {
  122.             @Override
  123.             public void insertUpdate(DocumentEvent e)
  124.             {
  125.                 onSearchBarChanged();
  126.             }
  127.  
  128.             @Override
  129.             public void removeUpdate(DocumentEvent e)
  130.             {
  131.                 onSearchBarChanged();
  132.             }
  133.  
  134.             @Override
  135.             public void changedUpdate(DocumentEvent e)
  136.             {
  137.                 onSearchBarChanged();
  138.             }
  139.         });
  140.  
  141.         setLayout(new BorderLayout());
  142.         setBackground(ColorScheme.DARK_GRAY_COLOR);
  143.  
  144.         topPanel = new JPanel();
  145.         topPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
  146.         topPanel.setLayout(new BorderLayout(0, OFFSET));
  147.         add(topPanel, BorderLayout.NORTH);
  148.  
  149.         mainPanel = new FixedWidthPanel();
  150.         mainPanel.setBorder(new EmptyBorder(8, 10, 10, 10));
  151.         mainPanel.setLayout(new DynamicGridLayout(0, 1, 0, 5));
  152.         mainPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
  153.  
  154.         JPanel northPanel = new FixedWidthPanel();
  155.         northPanel.setLayout(new BorderLayout());
  156.         northPanel.add(mainPanel, BorderLayout.NORTH);
  157.  
  158.         scrollPane = new JScrollPane(northPanel);
  159.         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  160.         add(scrollPane, BorderLayout.CENTER);
  161.  
  162.         initializePluginList();
  163.         refreshPluginList();
  164.     }
  165.  
  166.     private void initializePluginList()
  167.     {
  168.         final List<String> pinnedPlugins = getPinnedPluginNames();
  169.  
  170.         // populate pluginList with all non-hidden plugins
  171.         pluginManager.getPlugins().stream()
  172.             .filter(plugin -> !plugin.getClass().getAnnotation(PluginDescriptor.class).hidden())
  173.             .forEach(plugin ->
  174.             {
  175.                 final PluginDescriptor descriptor = plugin.getClass().getAnnotation(PluginDescriptor.class);
  176.                 final Config config = pluginManager.getPluginConfigProxy(plugin);
  177.                 final ConfigDescriptor configDescriptor = config == null ? null : configManager.getConfigDescriptor(config);
  178.  
  179.                 final PluginListItem listItem = new PluginListItem(this, plugin, descriptor, config, configDescriptor);
  180.                 listItem.setPinned(pinnedPlugins.contains(listItem.getName()));
  181.                 pluginList.add(listItem);
  182.             });
  183.  
  184.         // add special entries for core client configurations
  185.         final PluginListItem runeLite = new PluginListItem(this, runeLiteConfig,
  186.             configManager.getConfigDescriptor(runeLiteConfig),
  187.             RUNELITE_PLUGIN, "RuneLite client settings", "client");
  188.         runeLite.setPinned(pinnedPlugins.contains(RUNELITE_PLUGIN));
  189.         pluginList.add(runeLite);
  190.  
  191.         final PluginListItem chatColor = new PluginListItem(this, chatColorConfig,
  192.             configManager.getConfigDescriptor(chatColorConfig),
  193.             CHAT_COLOR_PLUGIN, "Recolor chat text", "colour", "messages");
  194.         chatColor.setPinned(pinnedPlugins.contains(CHAT_COLOR_PLUGIN));
  195.         pluginList.add(chatColor);
  196.  
  197.         pluginList.sort(Comparator.comparing(PluginListItem::getName));
  198.     }
  199.  
  200.     void refreshPluginList()
  201.     {
  202.         // update enabled / disabled status of all items
  203.         pluginList.forEach(listItem ->
  204.         {
  205.             final Plugin plugin = listItem.getPlugin();
  206.             if (plugin != null)
  207.             {
  208.                 listItem.setPluginEnabled(pluginManager.isPluginEnabled(plugin));
  209.             }
  210.         });
  211.  
  212.         if (showingPluginList)
  213.         {
  214.             openConfigList();
  215.         }
  216.     }
  217.  
  218.     void openConfigList()
  219.     {
  220.         if (showingPluginList)
  221.         {
  222.             scrollBarPosition = scrollPane.getVerticalScrollBar().getValue();
  223.         }
  224.  
  225.         showingPluginList = true;
  226.  
  227.         topPanel.removeAll();
  228.         mainPanel.removeAll();
  229.         topPanel.add(searchBar, BorderLayout.CENTER);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement