| 1 package snow.sortabletable; 2 3 import snow.utils.gui.*; 4 import SnowMailClient.Language.Language; 5 6 import java.awt.*; 7 import java.awt.event.*; 8 import javax.swing.*; 9 import javax.swing.border.*; 10 import javax.swing.event.*; 11 import java.util.*; 12 13 15 public final class AdvancedSearchPanel extends AnimatedColorPanel 16 { 17 final protected JTextField searchFT = new JTextField(5); 18 final protected JTextField searchFT2 = new JTextField(5); 19 20 final protected JButton activateAdvancedButton = new JButton(new SortDirectionSelfDrawingIcon(SortDirectionSelfDrawingIcon.Right)); 21 final protected JButton deactivateAdvancedButton = new JButton(new SortDirectionSelfDrawingIcon(SortDirectionSelfDrawingIcon.Left)); 22 23 final protected JComboBox searchOperationCB = new JComboBox(new String []{ "&", "||"}); 24 final protected JComboBox searchColumnCB = new JComboBox(); 25 final protected JCheckBox regExSearchCB = new JCheckBox(Language.translate("RegEx"), false); 26 27 28 29 protected SortableTableModel stm; 30 31 boolean advancedMode = false; 32 boolean allowRegEx = false; 33 34 private Vector<ChangeListener> searchChangeListeners = new Vector<ChangeListener>(); 35 36 39 public AdvancedSearchPanel(final String searchLabelText, final Icon searchIcon, 40 final SortableTableModel stm, boolean allowRegEx) 41 { 42 super(new FlowLayout(FlowLayout.LEFT), 100); 43 this.setOpaque(false); 44 this.stm = stm; 45 this.allowRegEx = allowRegEx; 46 47 Vector<String > colNames = new Vector<String >(); 48 colNames.addElement(Language.translate("All Columns")); 49 if(stm!=null) 50 { 51 for(int i=0; i<stm.getBasicTableModel().getColumnCount(); i++) 52 { 53 colNames.add( stm.getBasicTableModel().getColumnName(i)); 54 } 55 searchColumnCB.setModel(new DefaultComboBoxModel(colNames)); 56 } 57 58 add(new JLabel(searchLabelText, searchIcon, JLabel.LEFT)); 61 add(searchFT); 62 63 add(activateAdvancedButton); 64 activateAdvancedButton.setToolTipText(Language.translate("Advanced Search")); 65 activateAdvancedButton.setFocusPainted(false); 66 activateAdvancedButton.addActionListener(new ActionListener() 67 { 68 public void actionPerformed(ActionEvent e) 69 { 70 setAdvancedMode(true); 71 } 72 }); 73 74 add(searchOperationCB); 75 add(searchFT2); 76 77 add(searchColumnCB); 78 searchColumnCB.setMaximumRowCount(35); 80 add(deactivateAdvancedButton); 81 deactivateAdvancedButton.setToolTipText(Language.translate("Return to Simple Search")); 82 deactivateAdvancedButton.setFocusPainted(false); 83 deactivateAdvancedButton.addActionListener(new ActionListener() 84 { 85 public void actionPerformed(ActionEvent e) 86 { 87 setAdvancedMode(false); 88 } 89 }); 90 91 if(allowRegEx) 92 { 93 add(regExSearchCB); 94 regExSearchCB.setOpaque(false); 95 } 96 97 KeyAdapter kad = new KeyAdapter() 100 { 101 @Override public void keyReleased(KeyEvent ee) 102 { 103 doSearch(); 104 } 105 }; 106 107 searchOperationCB.addActionListener(new ActionListener() 108 { 109 public void actionPerformed(ActionEvent e) 110 { 111 doSearch(); 112 } 113 }); 114 115 searchColumnCB.addActionListener(new ActionListener() 116 { 117 public void actionPerformed(ActionEvent e) 118 { 119 doSearch(); 120 } 121 }); 122 123 searchFT.addKeyListener(kad); 124 searchFT2.addKeyListener(kad); 125 126 129 searchFT.addFocusListener(new FocusAdapter() 130 { 131 @Override public void focusGained(FocusEvent e) 132 { 133 searchFT.selectAll(); 134 } 135 }); 136 searchFT2.addFocusListener(new FocusAdapter() 137 { 138 public void focusGained(FocusEvent e) 139 { 140 searchFT2.selectAll(); 141 } 142 }); 143 144 147 this.registerKeyboardAction(new ActionListener() 148 { 149 public void actionPerformed(ActionEvent e) 150 { 151 searchFT.requestFocus(); 152 } 153 }, 154 KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK), 155 JComponent.WHEN_IN_FOCUSED_WINDOW 156 ); 159 160 162 activateAdvancedButton.setPreferredSize(new Dimension( 163 (int)searchFT2.getPreferredSize().getHeight()+0, 164 (int)searchFT2.getPreferredSize().getHeight()+0)); 165 166 deactivateAdvancedButton.setPreferredSize(new Dimension( 167 (int)searchFT2.getPreferredSize().getHeight()+0, 168 (int)searchFT2.getPreferredSize().getHeight()+0)); 169 170 this.searchOperationCB.setPreferredSize(new Dimension( 171 (int) searchOperationCB.getPreferredSize().getWidth()+0, 172 (int)searchFT2.getPreferredSize().getHeight()+0)); 173 174 this.searchColumnCB.setPreferredSize(new Dimension( 175 (int) searchColumnCB.getPreferredSize().getWidth()+0, 176 (int)searchFT2.getPreferredSize().getHeight()+0)); 177 178 EventQueue.invokeLater(new Runnable (){public void run() 180 { 181 setAdvancedMode(advancedMode); 182 }}); 183 184 185 } 187 188 public void setSortableTableModel(SortableTableModel stm) 189 { 190 int fontSize = searchColumnCB.getFont().getSize(); 191 int maxWidth = 0; 192 193 this.stm = stm; 194 if(stm!=null) 195 { 196 Vector<String > colNames = new Vector<String >(); 197 colNames.addElement(Language.translate("All Columns")); 198 for(int i=0; i<stm.getBasicTableModel().getColumnCount(); i++) 199 { 200 String name = stm.getBasicTableModel().getColumnName(i); 201 colNames.add( name ); 202 203 if(name.length()>maxWidth) maxWidth = name.length(); 204 } 205 searchColumnCB.setModel(new DefaultComboBoxModel(colNames)); 206 } 207 else 208 { 209 searchColumnCB.setModel(new DefaultComboBoxModel(new Vector<String >())); 210 } 211 Dimension dim = new Dimension(fontSize*maxWidth, (int) searchColumnCB.getPreferredSize().getHeight()); 212 searchColumnCB.setSize(dim); 213 searchColumnCB.setPreferredSize(dim); 214 215 doSearch(); 216 } 217 218 219 221 public void setAdvancedMode(boolean active) 222 { 223 advancedMode = active; 224 searchFT2.setVisible(active); 225 deactivateAdvancedButton.setVisible(active); 226 searchOperationCB.setVisible(active); 227 searchColumnCB.setVisible(active); 228 229 this.regExSearchCB.setVisible( allowRegEx && active ); 230 231 activateAdvancedButton.setVisible(!active); 232 233 doSearch(); 234 } 235 236 237 240 public void doSearch() 241 { 242 if(!SwingUtilities.isEventDispatchThread()) 243 { 244 new Throwable ("Should be called from EDT !").printStackTrace(); 245 } 246 247 248 if(searchFT.getText().length()>0 || (this.advancedMode && searchFT2.getText().length()>0) ) 249 { 250 this.activateAnimation(true); 251 this.setOpaque(true); 252 } 253 else 254 { 255 this.activateAnimation(false); 256 this.setOpaque(false); 257 } 258 259 if(advancedMode) 260 { 261 boolean andSearch = searchOperationCB.getSelectedIndex()==0; 262 int col = searchColumnCB.getSelectedIndex()-1; if(stm!=null) 264 { 265 stm.advancedSearch(searchFT.getText(), searchFT2.getText(), andSearch, regExSearchCB.isSelected(), col); 266 } 267 } 268 else 269 { 270 if(stm!=null) 272 { 273 stm.search(searchFT.getText(), "", regExSearchCB.isSelected()); 274 } 275 } 276 277 notifySearchChanged(); 278 } 279 280 public boolean hasSearchText() { return searchFT.getText().length()>0 || searchFT2.getText().length()>0; } 281 282 283 285 public void setSearchText(String t1, String t2) 286 { 287 searchFT.setText(t1); 288 searchFT2.setText(t2); 289 doSearch(); 291 } 292 293 297 public void addSearchChangeListener(ChangeListener cl ) 298 { 299 searchChangeListeners.addElement(cl); 300 } 301 302 private void notifySearchChanged() 303 { 304 for(ChangeListener cl : searchChangeListeners) 305 { 306 cl.stateChanged(new ChangeEvent(this)); 307 } 308 } 309 310 } | Popular Tags |