1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.MouseAdapter ; 31 import java.awt.event.MouseEvent ; 32 import java.awt.event.MouseListener ; 33 import java.awt.event.WindowAdapter ; 34 import java.awt.event.WindowEvent ; 35 import java.awt.event.WindowListener ; 36 import java.util.ArrayList ; 37 import java.util.Arrays ; 38 39 import javax.swing.Box ; 40 import javax.swing.BoxLayout ; 41 import javax.swing.ComboBoxModel ; 42 import javax.swing.JButton ; 43 import javax.swing.JComboBox ; 44 import javax.swing.JFrame ; 45 import javax.swing.JLabel ; 46 import javax.swing.JList ; 47 import javax.swing.JPanel ; 48 import javax.swing.JScrollPane ; 49 import javax.swing.ListCellRenderer ; 50 51 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair; 52 53 59 @SuppressWarnings ("serial") 60 public class NewFilterFrame extends FBDialog 61 { 62 63 private JList list = new JList (); 64 65 private static NewFilterFrame instance; 66 public static void open() 67 { 68 if (instance == null) 69 { 70 instance = new NewFilterFrame(); 71 instance.setVisible(true); 72 } 73 else 74 { 75 instance.toFront(); 76 } 77 } 78 public static void close() 79 { 80 instance.dispose(); 81 instance = null; 82 } 83 84 private NewFilterFrame() 85 { 86 super(PreferencesFrame.getInstance()); 87 setContentPane(new JPanel () 88 { 89 public Insets getInsets() 90 { 91 return new Insets (3, 3, 3, 3); 92 } 93 }); 94 setLayout(new BorderLayout ()); 95 96 JPanel north = new JPanel (); 97 north.setLayout(new BoxLayout (north, BoxLayout.X_AXIS)); 98 north.add(Box.createHorizontalStrut(3)); 99 north.add(new JLabel (edu.umd.cs.findbugs.L10N.getLocalString("dlg.filter_bugs_lbl", "Filter out all bugs whose") + " ")); 100 101 Sortables[] valuesWithoutDivider=new Sortables[Sortables.values().length-1]; 103 int index=0; 104 105 for (int x=0; x<Sortables.values().length;x++) 106 { 107 if (Sortables.values()[x]!=Sortables.DIVIDER) 108 { 109 valuesWithoutDivider[index]=Sortables.values()[x]; 110 index++; 111 } 112 } 113 114 final JComboBox comboBox = new JComboBox (valuesWithoutDivider); 115 comboBox.setRenderer(new ListCellRenderer () 116 { 117 final Color SELECTED_BACKGROUND = new Color (183, 184, 204); 118 119 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 120 { 121 JLabel result = new JLabel (); 122 result.setFont(result.getFont().deriveFont(Driver.getFontSize())); 123 result.setOpaque(true); 124 result.setText(value.toString().toLowerCase()); 125 if (isSelected) 126 result.setBackground(SELECTED_BACKGROUND); 127 return result; 128 } 129 }); 130 comboBox.addActionListener(new ActionListener () 131 { 132 public void actionPerformed(ActionEvent evt) 133 { 134 Sortables filterBy = (Sortables) comboBox.getSelectedItem(); 135 String [] listData = filterBy.getAllSorted(); 136 for (int i = 0; i < listData.length; i++) 137 listData[i] = filterBy.formatValue(listData[i]); 138 list.setListData(listData); 139 } 140 }); 141 comboBox.validate(); 142 north.add(comboBox); 143 north.add(new JLabel (" " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " ")); 144 String [] filterModes = {edu.umd.cs.findbugs.L10N.getLocalString("mode.equal_to", "equal to"), edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_after", "at or after"), edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_before", "at or before")}; 145 final JComboBox filterModeComboBox = new JComboBox (filterModes); 146 north.add(filterModeComboBox); 147 north.add(new JLabel (":")); 148 north.add(Box.createHorizontalGlue()); 149 JPanel south = new JPanel (); 150 JButton okButton = new JButton (edu.umd.cs.findbugs.L10N.getLocalString("dlg.ok_btn", "OK")); 151 okButton.addActionListener(new ActionListener () 152 { 153 public void actionPerformed(ActionEvent evt) 154 { 155 Sortables key = (Sortables) comboBox.getSelectedItem(); 156 String [] values = key.getAllSorted(); 157 FilterMatcher[] newFilters = new FilterMatcher[list.getSelectedIndices().length]; 164 for (int i = 0; i < newFilters.length; i++) 165 newFilters[i] = new FilterMatcher(key, values[list.getSelectedIndices()[i]], filterModeComboBox.getSelectedIndex()); 166 ProjectSettings.getInstance().addFilters(newFilters); 167 PreferencesFrame.getInstance().updateFilterPanel(); 168 MainFrame.getInstance().setProjectChanged(newFilters.length > 0); 169 close(); 170 } 171 }); 172 JButton cancelButton = new JButton (edu.umd.cs.findbugs.L10N.getLocalString("dlg.cancel_btn", "Cancel")); 173 cancelButton.addActionListener(new ActionListener () 174 { 175 public void actionPerformed(ActionEvent evt) 176 { 177 close(); 178 } 179 }); 180 south.setLayout(new BoxLayout (south, BoxLayout.X_AXIS)); 181 south.add(Box.createHorizontalGlue()); 182 south.add(okButton); 183 south.add(Box.createHorizontalStrut(2)); 184 south.add(cancelButton); 185 south.add(Box.createHorizontalStrut(3)); 186 187 list.addMouseListener(new MouseAdapter () 188 { 189 public void mouseClicked(MouseEvent evt) 190 { 191 if (evt.getClickCount() == 2) 192 { 193 Sortables key = (Sortables) comboBox.getSelectedItem(); 195 String [] values = key.getAllSorted(); 196 FilterMatcher[] newFilters = new FilterMatcher[list.getSelectedIndices().length]; 203 for (int i = 0; i < newFilters.length; i++) 204 newFilters[i] = new FilterMatcher(key, values[list.getSelectedIndices()[i]]); 205 ProjectSettings.getInstance().addFilters(newFilters); 206 PreferencesFrame.getInstance().updateFilterPanel(); 207 close(); 208 } 209 } 210 }); 211 212 add(north, BorderLayout.NORTH); 213 add(Box.createHorizontalStrut(2), BorderLayout.WEST); 214 add(new JScrollPane (list), BorderLayout.CENTER); 215 add(Box.createHorizontalStrut(2), BorderLayout.EAST); 216 add(south, BorderLayout.SOUTH); 217 218 comboBox.getActionListeners()[0].actionPerformed(null); 220 221 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 222 addWindowListener(new WindowAdapter () 223 { 224 @Override 225 public void windowClosing(WindowEvent arg0) 226 { 227 close(); 228 } 229 }); 230 231 setTitle(edu.umd.cs.findbugs.L10N.getLocalString("dlg.create_new_filter_ttl", "Create New Filter")); 232 pack(); 233 } 234 235 public static void main(String [] args) 236 { 237 new NewFilterFrame().setDefaultCloseOperation(EXIT_ON_CLOSE); 238 } 239 } 240 | Popular Tags |