1 19 20 21 package org.netbeans.modules.search; 22 23 24 import java.awt.Component ; 25 import java.awt.Dialog ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.FocusAdapter ; 29 import java.awt.event.FocusEvent ; 30 import java.awt.event.FocusListener ; 31 import java.beans.Customizer ; 32 import java.beans.PropertyChangeEvent ; 33 import java.beans.PropertyChangeListener ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Collections ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 import javax.swing.JButton ; 41 import javax.swing.JPanel ; 42 import javax.swing.event.ChangeEvent ; 43 import javax.swing.event.ChangeListener ; 44 45 import org.openide.DialogDescriptor; 46 import org.openide.DialogDisplayer; 47 import org.openide.awt.Mnemonics; 48 import org.openide.util.HelpCtx; 49 import org.openide.util.NbBundle; 50 import org.openidex.search.SearchType; 51 52 53 61 public final class SearchPanel extends JPanel 62 implements PropertyChangeListener , 63 FocusListener , 64 ChangeListener , 65 ActionListener { 66 67 68 public static final String PROP_DIALOG_TITLE 69 = "Find Files dialog title"; 71 72 public static final int RET_CANCEL = 0; 73 74 75 public static final int RET_OK = 1; 76 77 78 private DialogDescriptor dialogDescriptor; 79 80 81 private final JButton okButton; 82 83 84 private final JButton cancelButton; 85 86 87 private Dialog dialog; 88 89 90 private int returnStatus = RET_CANCEL; 91 92 93 private List orderedSearchTypePanels; 94 95 96 private boolean customized; 97 98 99 104 public SearchPanel(List searchTypeList) { 105 this(searchTypeList, false); 106 } 107 108 116 public SearchPanel(List searchTypeList, boolean activateWithPreviousValues) { 117 this.orderedSearchTypePanels = new ArrayList (searchTypeList.size()); 118 119 Iterator it; 121 122 123 Map sortedCriteria; 124 { 125 SearchCriterion[] allCriteria = SearchProjectSettings.getInstance() 126 .getSearchCriteria(); 127 sortedCriteria = Utils.sortCriteriaBySearchType(allCriteria); 128 } 129 Collection processedClassNames = new ArrayList (); 130 for (it = searchTypeList.iterator(); it.hasNext(); ) { 131 SearchType searchType = (SearchType) it.next(); 132 String className = searchType.getClass().getName(); 133 if (processedClassNames.contains(className)) { 134 continue; 135 } 136 processedClassNames.add(className); 137 138 143 final boolean initPanelFromHistory = 144 !activateWithPreviousValues 145 && FindDialogMemory.getDefault() 146 .wasSearchTypeUsed(searchType.getClass().getName()); 147 SearchTypePanel newPanel = new SearchTypePanel(searchType, 148 initPanelFromHistory); 149 Collection savedCriteria = (sortedCriteria == null) 150 ? null 151 : (Collection ) sortedCriteria.get(className); 152 153 int index = orderedSearchTypePanels.indexOf(newPanel); 154 if (savedCriteria != null) { 155 SearchTypePanel targetPanel = (index == -1) 156 ? newPanel 157 : (SearchTypePanel) orderedSearchTypePanels.get(index); 158 targetPanel.addSavedCriteria( 159 Collections.unmodifiableCollection(savedCriteria)); 160 } 161 if (index != -1) { 162 continue; 163 } 164 orderedSearchTypePanels.add(newPanel); 165 newPanel.addPropertyChangeListener(this); 166 } 167 168 initComponents(); 169 170 for (it = orderedSearchTypePanels.iterator(); it.hasNext(); ) { 172 tabbedPane.add((Component ) it.next()); 173 } 174 175 int index = 0; 182 if(activateWithPreviousValues){ 183 index = getIndexOfSearchType(FindDialogMemory.getDefault().getLastSearchType()); 184 if(index<0){ 185 index=0; 187 } 188 } 189 tabbedPane.setSelectedIndex(index); 190 191 setName(NbBundle.getBundle(SearchPanel.class) 192 .getString("TEXT_TITLE_CUSTOMIZE")); 194 okButton = new JButton (NbBundle.getBundle(SearchPanel.class) 195 .getString("TEXT_BUTTON_SEARCH")); updateIsCustomized(); 197 198 Mnemonics.setLocalizedText(cancelButton = new JButton (), 199 NbBundle.getBundle(SearchPanel.class) 200 .getString("TEXT_BUTTON_CANCEL")); 202 Object options[] = new Object [] {okButton, cancelButton}; 203 204 initAccessibility(); 205 206 dialogDescriptor = new DialogDescriptor( 208 this, 209 getName(), 210 true, 211 options, 212 options[0], 213 DialogDescriptor.BOTTOM_ALIGN, 214 null, this); 216 } 217 218 225 public void actionPerformed(final ActionEvent evt) { 226 doClose(evt.getSource() == okButton ? RET_OK : RET_CANCEL); 227 } 228 229 void setTitle(String title) { 230 dialogDescriptor.setTitle(title); 231 } 232 233 private void initAccessibility() { 234 this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_SearchPanel")); tabbedPane.getAccessibleContext().setAccessibleName(NbBundle.getBundle(SearchPanel.class).getString("ACSN_Tabs")); tabbedPane.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACSD_Tabs")); okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_SEARCH")); cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_CANCEL")); } 240 241 246 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 248 249 tabbedPane = new javax.swing.JTabbedPane (); 250 251 setLayout(new java.awt.GridBagLayout ()); 252 253 gridBagConstraints = new java.awt.GridBagConstraints (); 254 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 255 gridBagConstraints.weightx = 1.0; 256 gridBagConstraints.weighty = 1.0; 257 add(tabbedPane, gridBagConstraints); 258 259 } 261 262 private javax.swing.JTabbedPane tabbedPane; 264 266 267 public boolean isCustomized() { 268 return customized; 269 } 270 271 276 private List getOrderedSearchTypePanels() { 277 return new ArrayList (orderedSearchTypePanels); 278 } 279 280 281 private String getTabText(int index) { 282 try { 283 return ((SearchTypePanel)getOrderedSearchTypePanels().get(index)).getName(); 284 } catch (ArrayIndexOutOfBoundsException ex) { 285 return null; 286 } 287 } 288 289 294 public SearchType[] getCustomizedSearchTypes() { 295 296 List searchTypeList = new ArrayList (orderedSearchTypePanels.size()); 297 298 for (Iterator it = orderedSearchTypePanels.iterator(); it.hasNext(); ) { 299 SearchTypePanel searchTypePanel = (SearchTypePanel) it.next(); 300 if (searchTypePanel.isCustomized()) { 301 searchTypeList.add(searchTypePanel.getSearchType()); 302 } 303 } 304 305 return (SearchType[]) searchTypeList.toArray( 306 new SearchType[searchTypeList.size()]); 307 } 308 309 314 public int getReturnStatus () { 315 return returnStatus; 316 } 317 318 319 private void doClose(int returnStatus) { 320 321 Iterator it = orderedSearchTypePanels.iterator(); 322 while (it.hasNext()) { 323 SearchTypePanel panel = (SearchTypePanel) it.next(); 324 panel.removePropertyChangeListener(this); 325 } 326 327 if (returnStatus == RET_OK) { 328 FindDialogMemory.getDefault().clearSearchTypesUsed(); 329 } 330 331 int selectedIndex = tabbedPane.getSelectedIndex(); 332 if (selectedIndex >= 0) { 333 SearchTypePanel panel = getSearchTypePanel(selectedIndex); 334 if (returnStatus == RET_OK){ 335 FindDialogMemory.getDefault().setLastUsedSearchType(panel.getSearchType()); 336 panel.onOk(); 337 } 338 else { 339 panel.onCancel(); 340 } 341 } 342 343 this.returnStatus = returnStatus; 344 345 dialog.setVisible(false); 346 dialog.dispose(); 347 } 348 349 350 public void showDialog() { 351 dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 352 dialog.setModal(true); 353 tabbedPane.addFocusListener(this); 354 355 dialog.pack(); 356 dialog.setVisible(true); 357 } 358 359 364 public void focusGained(FocusEvent e) { 365 tabbedPane.removeFocusListener(this); 366 367 int selectedIndex = tabbedPane.getSelectedIndex(); 368 if (selectedIndex >= 0) { 369 SearchTypePanel panel = getSearchTypePanel(selectedIndex); 370 if ((panel != null) && (panel.customizerComponent != null)) { 371 panel.customizerComponent.requestFocus(); 372 } 373 } 374 375 tabbedPane.addChangeListener(this); 376 } 377 378 385 public void focusLost(FocusEvent e) { 386 } 388 389 393 public void stateChanged(ChangeEvent e) { 394 int selectedIndex = tabbedPane.getSelectedIndex(); 395 if (selectedIndex >= 0) { 396 SearchTypePanel panel = getSearchTypePanel(selectedIndex); 397 if (panel != null) { 398 panel.initializeWithObject(); 399 } 400 } 401 } 402 403 404 public void propertyChange(PropertyChangeEvent event) { 405 if(SearchTypePanel.PROP_CUSTOMIZED.equals(event.getPropertyName())) { 406 updateIsCustomized(); 407 } 408 409 for(int i = 0; i < tabbedPane.getTabCount(); i++) { 410 tabbedPane.setTitleAt(i, getTabText(i)); 411 tabbedPane.setIconAt(i, null); 412 } 413 } 414 416 private void updateIsCustomized() { 417 customized = getCustomizedSearchTypes().length != 0; 418 419 okButton.setEnabled(isCustomized()); 420 } 421 422 423 430 private SearchTypePanel getSearchTypePanel(int index) { 431 SearchTypePanel searchTypePanel = null; 432 433 Iterator it = getOrderedSearchTypePanels().iterator(); 434 while(index >= 0 && it.hasNext()) { 435 searchTypePanel = (SearchTypePanel)it.next(); 436 index--; 437 } 438 439 return searchTypePanel; 440 } 441 442 448 private int getIndexOfSearchType(SearchType searchTypeToFind) { 449 450 if(searchTypeToFind==null){ 451 return -1; 452 } 453 454 SearchTypePanel searchTypePanel = null; 455 Iterator it = getOrderedSearchTypePanels().iterator(); 456 457 int index = -1; 458 while(it.hasNext()) { 459 index++; 460 searchTypePanel = (SearchTypePanel) it.next(); 461 462 if(searchTypePanel.getSearchType().getClass() == searchTypeToFind.getClass()){ 463 return index; 464 } 465 } 466 467 return -1; 468 } 469 470 } 471 | Popular Tags |