Codebase list findbugs / 5fdfe956-1a81-48da-858f-070c116c6902/main src / gui / edu / umd / cs / findbugs / gui2 / PreferencesFrame.java
5fdfe956-1a81-48da-858f-070c116c6902/main

Tree @5fdfe956-1a81-48da-858f-070c116c6902/main (Download .tar.gz)

PreferencesFrame.java @5fdfe956-1a81-48da-858f-070c116c6902/mainraw · history · blame

  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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2006, University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
 */

package edu.umd.cs.findbugs.gui2;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.TreeModel;

import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.Plugin;
import edu.umd.cs.findbugs.PluginException;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.cloud.CloudPlugin;
import edu.umd.cs.findbugs.filter.Filter;
import edu.umd.cs.findbugs.filter.Matcher;
import edu.umd.cs.findbugs.util.LaunchBrowser;

@SuppressWarnings("serial")
public class PreferencesFrame extends FBDialog {
    private static final Logger LOGGER = Logger.getLogger(PreferencesFrame.class.getName());

    private static final int TAB_MIN = 1;
    private static final int TAB_MAX = 20;

    private static final int FONT_MIN = 10;
    private static final int FONT_MAX = 99;

    private static PreferencesFrame instance;

    private final CheckBoxList<MatchBox> filterCheckBoxList = new CheckBoxList<>();

    // Variables for Properties tab.
    private JTextField tabTextField;
    private JTextField fontTextField;

    private JTextField packagePrefixLengthTextField;

    private final Map<Plugin, EnabledSettings> pluginEnabledStatus = new HashMap<Plugin, EnabledSettings>();
    private JPanel pluginPanelCenter;

    private static class EnabledSettings {
        public boolean global;
        public Boolean project;

        private EnabledSettings(boolean global, Boolean project) {
            this.global = global;
            this.project = project;
        }
    }

    public static PreferencesFrame getInstance() {
        if (instance == null) {
            instance = new PreferencesFrame();
        }

        return instance;
    }

    private boolean pluginsAdded = false;
    private final JPanel filterPane;
    private final JTabbedPane mainTabPane;

    public void showFilterPane() {
        mainTabPane.setSelectedComponent(filterPane);

    }
    private PreferencesFrame() {
        setTitle(edu.umd.cs.findbugs.L10N.getLocalString("dlg.fil_sup_ttl", "Preferences"));
        setModal(true);

        mainTabPane = new JTabbedPane();

        mainTabPane.add("General", createPropertiesPane());

        filterPane = createFilterPane();
        mainTabPane.add(edu.umd.cs.findbugs.L10N.getLocalString("pref.filters", "Filters"), filterPane);
        mainTabPane.add("Plugins", createPluginPane());

        MainFrame.getInstance().updateStatusBar();
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        JPanel top = new JPanel();
        top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
        top.add(Box.createHorizontalStrut(5));
        top.add(mainTabPane);
        top.add(Box.createHorizontalStrut(5));

        JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
        bottom.add(Box.createHorizontalGlue());
        bottom.add(new JButton(new AbstractAction(edu.umd.cs.findbugs.L10N.getLocalString("pref.close", "Close")) {
            @Override
            public void actionPerformed(ActionEvent evt) {
                handleWindowClose();
                PreferencesFrame.this.setVisible(false);
            }
        }));
        bottom.add(Box.createHorizontalStrut(5));

        add(Box.createVerticalStrut(5));
        add(top);
        add(Box.createVerticalStrut(5));
        add(bottom);
        add(Box.createVerticalStrut(5));

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                resetPropertiesPane();
            }
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                handleWindowClose();
            }
        });
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        pack();
    }

    private void handleWindowClose() {
        TreeModel bt = (MainFrame.getInstance().getTree().getModel());
        if (bt instanceof BugTreeModel) {
            ((BugTreeModel) bt).checkSorter();
        }
        Project project = getCurrentProject();

        boolean changed = pluginsAdded;
        pluginsAdded = false;
        List<String> enabledPlugins = new ArrayList<String>();
        List<String> disabledPlugins = new ArrayList<String>();
        for (Map.Entry<Plugin, EnabledSettings> entry : pluginEnabledStatus.entrySet()) {
            Plugin plugin = entry.getKey();
            EnabledSettings enabled = entry.getValue();
            if (project != null) {
                Boolean newSetting = enabled.project;
                Boolean existingSetting = project.getPluginStatus(plugin);
                boolean sameSettings = Objects.equals(existingSetting, newSetting);
                if (!sameSettings) {
                    project.setPluginStatusTrinary(plugin.getPluginId(), newSetting);
                    changed = true;
                }
            }
            if (enabled.global) {
                enabledPlugins.add(plugin.getPluginId());
            } else {
                disabledPlugins.add(plugin.getPluginId());
            }
            if (plugin.isGloballyEnabled() != enabled.global) {
                plugin.setGloballyEnabled(enabled.global);
                changed = true;
            }
        }

        if (changed && project != null) {
            MainFrame.getInstance().updateBugTree();
            MainFrame.getInstance().setProjectChanged(true);
        }
        if (project == null) {
            GUISaveState.getInstance().setPluginsEnabled(enabledPlugins, disabledPlugins);
            GUISaveState.getInstance().save();
        }
    }

    private Project getCurrentProject() {
        BugCollection bugCollection = MainFrame.getInstance().getBugCollection();
        return bugCollection == null ? null : bugCollection.getProject();
    }

    private JPanel createPluginPane() {
        final JPanel pluginPanel = new JPanel();
        pluginPanel.setLayout(new BorderLayout());
        pluginPanelCenter = new JPanel();

        pluginPanel.add(pluginPanelCenter, BorderLayout.CENTER);

        pluginPanelCenter.setBorder(new EmptyBorder(10,10,10,10));
        pluginPanelCenter.setLayout(new GridBagLayout());

        JButton addButton = new JButton("Install new plugin...");
        JPanel south = new JPanel();

        south.add(addButton);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.addChoosableFileFilter(new FileFilter() {

                    @Override
                    public String getDescription() {
                        return "FindBugs Plugin (*.jar)";
                    }

                    @Override
                    public boolean accept(File f) {
                        if (f.isDirectory()) {
                            return true;
                        }
                        if (!f.canRead()) {
                            return false;
                        }
                        if (f.getName().endsWith(".jar")) {
                            return true;
                        }
                        return false;
                    }
                });
                chooser.setDialogTitle("Select a FindBugs plugin jar");
                int retvalue = chooser.showDialog(PreferencesFrame.this, "Install");

                if (retvalue == JFileChooser.APPROVE_OPTION) {
                    File f = chooser.getSelectedFile();
                    try {
                        // load and enable for project (if loaded)
                        Plugin plugin = Plugin.loadCustomPlugin(f, PreferencesFrame.this.getCurrentProject());

                        GUISaveState guiSaveState = GUISaveState.getInstance();
                        URL url = f.toURI().toURL();
                        // add to FBGUI custom plugins list
                        guiSaveState.addCustomPlugin(url);
                        // add to list of enabled plugins
                        guiSaveState.setPluginEnabled(plugin.getPluginId());
                        plugin.setGloballyEnabled(true);
                        guiSaveState.save();
                        pluginsAdded = true;
                        rebuildPluginCheckboxes();

                    } catch (PluginException | MalformedURLException e1) {
                        LOGGER.log(Level.WARNING, "Could not load " + f.getPath(), e1);
                        JOptionPane.showMessageDialog(PreferencesFrame.this, "Could not load " + f.getPath()
                                + "\n\n"
                                + e1.getClass().getSimpleName() + ": " + e1.getMessage(),
                                "Error Loading Plugin", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });

        pluginPanel.add(south, BorderLayout.SOUTH);

        return pluginPanel;
    }

    EnabledSettings isEnabled(@CheckForNull Project project, Plugin plugin) {
        return new EnabledSettings(plugin.isGloballyEnabled(), project == null ? null : project.getPluginStatus(plugin));
    }

    private void rebuildPluginCheckboxes() {
        Project currentProject = getCurrentProject();
        pluginPanelCenter.removeAll();
        if (currentProject != null) {
            GridBagConstraints g = new GridBagConstraints();
            g.fill = GridBagConstraints.NONE;
            g.insets = new Insets(5,5,5,5);
            g.gridy = 0;
            //        g.anchor = GridBagConstraints.WEST;
            //        g.gridx = 1;
            //        pluginPanelCenter.add(new JLabel("Global Setting"), g);
            g.anchor = GridBagConstraints.CENTER;
            g.gridx = 2;
            pluginPanelCenter.add(new JLabel("Project Setting"), g);
        }
        Collection<Plugin> plugins = Plugin.getAllPlugins();
        int added = 0;
        for (final Plugin plugin : plugins) {
            if (plugin.isCorePlugin()) {
                continue;
            }
            String text = plugin.getShortDescription();
            String id = plugin.getPluginId();
            if (text == null) {
                text = id;
            }
            final URL url = plugin.getPluginLoader().getURL();
            String pluginUrlStr = url.toExternalForm();
            if ("file".equals(url.getProtocol())) {
                try {
                    pluginUrlStr = new File(URLDecoder.decode(url.getPath(), "UTF-8")).getAbsolutePath();
                } catch (UnsupportedEncodingException e) {

                }
            }
            text = String.format("<html>%s<br><font style='font-weight:normal;font-style:italic'>%s",
                    text, pluginUrlStr);

            EnabledSettings enabled = isEnabled(currentProject, plugin);
            final JCheckBox checkGlobal = new JCheckBox(text, enabled.global);
            final boolean cannotDisable = plugin.isEnabledByDefault() && plugin.cannotDisable();
            if (cannotDisable) {
                if (!enabled.global) {
                    throw new IllegalStateException(
                            plugin.getPluginId() + " is enabled by default and cannot be disabled, but is disabled");
                }
                checkGlobal.setEnabled(false);
            } else {
                checkGlobal.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mousePressed(MouseEvent e) {
                        if (SwingUtilities.isRightMouseButton(e)) {
                            JPopupMenu menu = new JPopupMenu();
                            JMenuItem item = new JMenuItem("Uninstall " + plugin.getShortDescription() + "...");
                            item.addActionListener(new UninstallClickListener(plugin, url));
                            menu.add(item);
                            menu.show(checkGlobal, e.getX(), e.getY());
                        }
                    }
                });
                checkGlobal.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pluginEnabledStatus.get(plugin).global = checkGlobal.isSelected();
                    }
                });
            }
            checkGlobal.setVerticalTextPosition(SwingConstants.TOP);
            String longText = plugin.getDetailedDescription();
            if (longText != null) {
                checkGlobal.setToolTipText("<html>" + longText + "</html>");
            }
            pluginEnabledStatus.put(plugin, enabled);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.gridx = 1;
            gbc.insets = new Insets(0,5,0,5);
            gbc.gridy = added+1;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            pluginPanelCenter.add(checkGlobal, gbc);

            if (currentProject != null && !cannotDisable) {
                final JComboBox<String> combo = new WideComboBox<>(new String[]{"DEFAULT", "DISABLED", "ENABLED"});
                if (enabled.project == null) {
                    combo.setSelectedIndex(0);
                } else {
                    combo.setSelectedIndex(enabled.project ? 2 : 1);
                }
                combo.setRenderer(new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                        if (index == -1) {
                            if (value.equals("DEFAULT")) {
                                value = "Default";
                            }
                            if (value.equals("DISABLED")) {
                                value = "Disabled";
                            }
                            if (value.equals("ENABLED")) {
                                value = "Enabled";
                            }
                        } else {
                            if (value.equals("DEFAULT")) {
                                value = "Default (use global setting)";
                            }
                            if (value.equals("DISABLED")) {
                                value = "Disabled for this project";
                            }
                            if (value.equals("ENABLED")) {
                                value = "Enabled for this project";
                            }
                        }
                        return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    }
                });
                combo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Boolean[] array = { null, false, true };
                        int i = combo.getSelectedIndex();
                        if (i < 0 || i > 2)
                        {
                            return; // ??
                        }
                        pluginEnabledStatus.get(plugin).project = array[i];
                    }
                });
                gbc.gridx = 2;
                gbc.fill = GridBagConstraints.NONE;
                gbc.weightx = 0;
                pluginPanelCenter.add(combo, gbc);
            }
            added++;
        }
        if (added == 0) {
            JLabel label = new JLabel("<html>No plugins are loaded.<br> " +
                    "Try installing <u><font color=blue>fb-contrib</font></u> - or write your own<br>" +
                    "plugin for your project's needs!");
            label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    try {
                        LaunchBrowser.showDocument(new URL("https://sourceforge.net/projects/fb-contrib/"));
                    } catch (MalformedURLException e1) {
                        throw new IllegalStateException(e1);
                    }
                }
            });
            label.setBorder(new EmptyBorder(10,10,10,10));
            pluginPanelCenter.add(label);
        }
        PreferencesFrame.this.pack();
    }

    private void addField(JPanel p, GridBagConstraints c, int y, String lbl, JComponent field) {
        c.gridy = y;
        JLabel l = new JLabel(lbl, SwingConstants.TRAILING);
        l.setLabelFor(field);
        c.anchor = GridBagConstraints.LINE_END;
        c.gridx = 0;
        p.add(l, c);
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 1;
        p.add(field, c);
    }
    private JPanel createPropertiesPane() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.ipadx = c.ipady = 5;


        float currFS = Driver.getFontSize();

        tabTextField = new JTextField(Integer.toString(GUISaveState.getInstance().getTabSize()));
        tabTextField.setPreferredSize(new Dimension((int) (currFS * 4), (int) (currFS * 2)));
        addField(mainPanel, c, 0, "Tab size", tabTextField);

        fontTextField = new JTextField(Float.toString(GUISaveState.getInstance().getFontSize()));
        fontTextField.setPreferredSize(new Dimension((int) (currFS * 6), (int) (currFS * 2)));
        addField(mainPanel, c, 1, "Font size", fontTextField);

        packagePrefixLengthTextField = new JTextField(Integer.toString(GUISaveState.getInstance().getPackagePrefixSegments()));
        packagePrefixLengthTextField.setPreferredSize(new Dimension((int) (currFS * 4), (int) (currFS * 2)));
        addField(mainPanel, c, 2, "Package prefix length", packagePrefixLengthTextField);

        contentPanel.add(mainPanel, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        bottomPanel.add(new JButton(new AbstractAction("Apply") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                changeTabSize();
                changeFontSize();
                changePackagePrefixLength();
            }
        }));

        bottomPanel.add(new JButton(new AbstractAction("Reset") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                resetPropertiesPane();
            }
        }));

        contentPanel.add(bottomPanel, BorderLayout.SOUTH);

        return contentPanel;
    }

    static class PluginWithDescription {

        public PluginWithDescription(String description, CloudPlugin plugin) {
            this.description = description;
            //            this.plugin = plugin;
        }
        final String description;
        //        final CloudPlugin plugin;

        @Override
        public String toString() {
            return description;
        }

    }
    private void changeTabSize() {
        int tabSize;

        try {
            tabSize = Integer.decode(tabTextField.getText());
        } catch (NumberFormatException exc) {
            JOptionPane.showMessageDialog(instance, "Error in tab size field.", "Tab Size Error",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (tabSize < TAB_MIN || tabSize > TAB_MAX) {
            JOptionPane.showMessageDialog(instance, "Tab size exceedes range (" + TAB_MIN + " - " + TAB_MAX + ").",
                    "Tab Size Excedes Range", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (tabSize != GUISaveState.getInstance().getTabSize()) {
            GUISaveState.getInstance().setTabSize(tabSize);
            MainFrame.getInstance().getSourceCodeDisplayer().clearCache();
            // This causes the GUI to redisplay the current code
            MainFrame.getInstance().syncBugInformation();
        }
    }

    private void changeFontSize() {
        float fontSize;

        try {
            fontSize = Float.parseFloat(fontTextField.getText());
        } catch (NumberFormatException exc) {
            JOptionPane.showMessageDialog(instance, "Error in font size field.", "Font Size Error",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (fontSize < FONT_MIN || fontSize > FONT_MAX) {
            JOptionPane.showMessageDialog(instance, "Font size exceedes range (" + FONT_MIN + " - " + FONT_MAX + ").",
                    "Font Size Exceedes Range", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (fontSize != GUISaveState.getInstance().getFontSize()) {
            GUISaveState.getInstance().setFontSize(fontSize);
            JOptionPane.showMessageDialog(instance, "To implement the new font size please restart FindBugs.",
                    "Changing Font",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void changePackagePrefixLength() {
        int value;

        try {
            value = Integer.parseInt(packagePrefixLengthTextField.getText());
        } catch (NumberFormatException exc) {
            JOptionPane.showMessageDialog(instance, "Error in package prefix length field.", "Package Prefix Length Error",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (value < 1 || value > 12) {
            JOptionPane.showMessageDialog(instance, "package prefix length exceedes range (" + 1 + " - " + 12 + ").",
                    "package prefix lengthe exceedes range", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        if (value != GUISaveState.getInstance().getPackagePrefixSegments()) {
            GUISaveState.getInstance().setPackagePrefixSegments(value);
            BugTreeModel bt = (BugTreeModel) (MainFrame.getInstance().getTree().getModel());
            bt.needToRebuild();
            bt.checkSorter();
        }

    }

    private void resetPropertiesPane() {
        tabTextField.setText(Integer.toString(GUISaveState.getInstance().getTabSize()));
        fontTextField.setText(Float.toString(GUISaveState.getInstance().getFontSize()));
        rebuildPluginCheckboxes();
    }

    /**
     * Create a JPanel to display the filtering controls.
     */
    private JPanel createFilterPane() {
        JButton addButton = new JButton(edu.umd.cs.findbugs.L10N.getLocalString("dlg.add_dot_btn", "Add..."));
        JButton removeButton = new JButton(edu.umd.cs.findbugs.L10N.getLocalString("dlg.remove_btn", "Remove"));
        JButton removeAllButton = new JButton(edu.umd.cs.findbugs.L10N.getLocalString("dlg.remove_all_btn", "Remove All"));
        JPanel filterPanel = new JPanel();
        filterPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1;
        gbc.weighty = 1;
        filterPanel.add(new JLabel("<HTML>These rules control which bugs are shown and which are hidden in this project"), gbc);

        gbc.gridheight = 4;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1;
        gbc.weighty = 1;
        filterPanel.add(new JScrollPane(filterCheckBoxList), gbc);
        updateFilterPanel();

        gbc.gridheight = 1;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0;
        gbc.weighty = 0;
        filterPanel.add(addButton, gbc);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                NewFilterFrame.open();
            }
        });

        gbc.gridy = 2;
        gbc.insets = new Insets(5, 0, 0, 0);
        filterPanel.add(removeButton, gbc);
        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                Object[] selected = filterCheckBoxList.getSelectedValues();
                if (selected.length == 0) {
                    return;
                }
                for (Object o : selected) {
                    MatchBox box = (MatchBox) o;
                    MainFrame.getInstance().getProject().getSuppressionFilter().removeChild(box.getMatcher());
                }
                FilterActivity.notifyListeners(FilterListener.Action.UNFILTERING, null);
                MainFrame.getInstance().setProjectChanged(true);
                updateFilterPanel();
            }
        });
        gbc.gridy = 3;
        gbc.weighty = 0;
        gbc.insets = new Insets(5, 0, 0, 0);
        filterPanel.add(removeAllButton, gbc);
        removeAllButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                boolean needsRebuild = false;
                Filter suppressionFilter = MainFrame.getInstance().getProject().getSuppressionFilter();
                if (!suppressionFilter.isEmpty()) {
                    needsRebuild = true;
                }
                suppressionFilter.clear();

                if (needsRebuild) {
                    // filters being cleared were disabled
                    FilterActivity.notifyListeners(FilterListener.Action.UNFILTERING, null);
                }
                MainFrame.getInstance().setProjectChanged(true);
                updateFilterPanel();
            }
        });
        gbc.gridy = 4;
        gbc.weighty = 1;
        gbc.insets = new Insets(0, 0, 0, 0);
        filterPanel.add(Box.createGlue(), gbc);

        return filterPanel;
    }

    private static class MatchBox extends JCheckBox {
        Matcher m;

        MatchBox(String text, Matcher m) {
            super(text);
            this.m = m;
        }

        Matcher getMatcher() {
            return m;
        }
    }

    void updateFilterPanel() {
        ArrayList<MatchBox> boxes = new ArrayList<MatchBox>();
        final Filter f = MainFrame.getInstance().getProject().getSuppressionFilter();

        for (final Matcher m : f.getChildren()) {
            MatchBox box = new MatchBox(m.toString(), m);
            box.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent evt) {
                    boolean isSelected = ((JCheckBox) evt.getSource()).isSelected();
                    boolean wasSelected = f.isEnabled(m);
                    if (isSelected == wasSelected) {
                        return;
                    }
                    f.setEnabled(m, isSelected);
                    updateFilters(isSelected);

                }
            });
            box.setSelected(f.isEnabled(m));
            boxes.add(box);
        }

        filterCheckBoxList.setListData(boxes.toArray(new MatchBox[boxes.size()]));
    }


    public static void updateFilters(boolean addedFilter) {
        FilterActivity.notifyListeners(addedFilter ? FilterListener.Action.FILTERING
                : FilterListener.Action.UNFILTERING, null);
        MainFrame.getInstance().setProjectChanged(true);
    }

    private class UninstallClickListener implements ActionListener {
        private final Plugin plugin;
        private final URL url;

        public UninstallClickListener(Plugin plugin, URL url) {
            this.plugin = plugin;
            this.url = url;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showOptionDialog(PreferencesFrame.this,
                    "Are you sure you want to uninstall " + plugin.getShortDescription() + "?" +
                            "\n\nNo files will be deleted from your computer.", "",
                            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                            new Object[]{"Uninstall", "Cancel"}, "Cancel");
            if (result == 0) {
                if (!GUISaveState.getInstance().removeCustomPlugin(url)) {
                    if ("file".equals(url.getProtocol())) {
                        String path = url.toExternalForm();
                        try {
                            path = new File(URLDecoder.decode(url.getPath(), "UTF-8")).getAbsolutePath();
                        } catch (UnsupportedEncodingException ex) {
                        }
                        try {
                            StringSelection contents = new StringSelection(path);
                            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, contents);
                        } catch (Exception e1) {
                        }
                        JOptionPane.showMessageDialog(PreferencesFrame.this,
                                "The plugin could not be uninstalled automatically.\n\n" +
                                        "You can try to delete this plugin manually: \n"
                                        + path + "\n\n(This path has been copied to your clipboard)",
                                        "Error", JOptionPane.ERROR_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(PreferencesFrame.this,
                                "This plugin is not actually in the list of plugins...\n" +
                                        "Not sure what to do...\n "
                                        + url.toExternalForm()
                                        + "\n\nPlugin URL's:\n" +
                                        GUISaveState.getInstance().getCustomPlugins(),
                                        "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(PreferencesFrame.this,
                            "Changes will take effect after you restart FindBugs.");
                }
            }
        }
    }
}