alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.search;


import java.awt.Component;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.awt.Mnemonics;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openidex.search.SearchType;


/**
 * Panel which shows all enabled search types for user allowing them to
 * select appropriate criteria for a new search.
 *
 * @author  Peter Zavadsky
 * @see SearchTypePanel
 */
public class SearchPanel extends JPanel implements PropertyChangeListener {
    
    /** */
    public static final String PROP_DIALOG_TITLE
                               = "Find Files dialog title";             //NOI18N

    /** Return status code - returned if Cancel button has been pressed. */
    public static final int RET_CANCEL = 0;
    
    /** Return status code - returned if OK button has been pressed. */
    public static final int RET_OK = 1;

    /** Dialog descriptor. */
    private DialogDescriptor dialogDescriptor;

    /** OK button. */
    private JButton okButton;
    
    /** Cancel button. */
    private JButton cancelButton;

    /** Java equivalent. */
    private Dialog dialog;

    /** Return status. */
    private int returnStatus = RET_CANCEL;

    /** Ordered list of SearchTypePanel's. */
    private List orderedSearchTypePanels;

    /** Whether some criterion is customized. */
    private boolean customized;
    
    
    /**
     * Creates a new SearchPanel.
     *
     * @param  searchTypeList  list of SearchTypes to use
     */
    public SearchPanel(List searchTypeList) {
        this(searchTypeList, false);
    }
    
    /**
     * Creates a new SearchPanel.
     *
     * @param  searchTypeList  list of SearchTypes to use 
     * @param  isCustomized  sets customized flag indicating there is at least
     *                       one from SearchTypes already set and
     *                       search - okButton should be enabled
     */
    public SearchPanel(List searchTypeList, boolean isCustomized) {
        this.orderedSearchTypePanels = new ArrayList(searchTypeList.size());
        this.customized = isCustomized;

        // Default values of criteria.
        Iterator it;
        
        /* Create search type panels: */
        Map sortedCriteria;
        {
            SearchCriterion[] allCriteria = SearchProjectSettings.getInstance()
                                            .getSearchCriteria();
            sortedCriteria = Utils.sortCriteriaBySearchType(allCriteria);
        }
        Collection processedClassNames = new ArrayList();
        for (it = searchTypeList.iterator(); it.hasNext(); ) {
            SearchType searchType = (SearchType) it.next();
            String className = searchType.getClass().getName();
            if (processedClassNames.contains(className)) {
                continue;
            }
            processedClassNames.add(className);
            SearchTypePanel newPanel = new SearchTypePanel(searchType);
            Collection savedCriteria = (sortedCriteria == null)
                    ? null
                    : (Collection) sortedCriteria.get(className);
            
            int index = orderedSearchTypePanels.indexOf(newPanel);
            if (savedCriteria != null) {
                SearchTypePanel targetPanel = (index == -1)
                        ? newPanel
                        : (SearchTypePanel) orderedSearchTypePanels.get(index);
                targetPanel.addSavedCriteria(
                        Collections.unmodifiableCollection(savedCriteria));
            }
            if (index != -1) {
                continue;
            }
            orderedSearchTypePanels.add(newPanel);
            newPanel.addPropertyChangeListener(this);
        }
        
        initComponents();	

        // For each search type create one tab as its search type panel.
        for (it = orderedSearchTypePanels.iterator(); it.hasNext(); ) {
            tabbedPane.add((Component) it.next());
        }
        
        //prevents bug #43843 ("AIOOBE after push button Modify Search")
        tabbedPane.setSelectedIndex(0);

        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });
        
        setName(NbBundle.getBundle(SearchPanel.class)
                .getString("TEXT_TITLE_CUSTOMIZE"));                    //NOI18N

        okButton = new JButton(NbBundle.getBundle(SearchPanel.class)
                               .getString("TEXT_BUTTON_SEARCH"));       //NOI18N
        okButton.setEnabled(isCustomized());

        Mnemonics.setLocalizedText(cancelButton = new JButton(),
                                   NbBundle.getBundle(SearchPanel.class)
                                   .getString("TEXT_BUTTON_CANCEL"));   //NOI18N

        Object options[] = new Object[] {okButton, cancelButton};

        initAccessibility();
        
        // Creates representing dialog descriptor.
        dialogDescriptor = new DialogDescriptor(
            this, 
            getName(), 
            true, 
            options, 
            options[0],
            DialogDescriptor.BOTTOM_ALIGN, 
            getHelpCtx(),
            new ActionListener() {
                public void actionPerformed(final ActionEvent evt) {
                    if(evt.getSource() == okButton) {
                        doClose(RET_OK);
                    } else {
                        doClose(RET_CANCEL);
                    }
                }
        });
    }
    
    void setTitle(String title) {
        dialogDescriptor.setTitle(title);
    }

    private void initAccessibility() {
        this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_SearchPanel")); // NOI18N         
        tabbedPane.getAccessibleContext().setAccessibleName(NbBundle.getBundle(SearchPanel.class).getString("ACSN_Tabs")); // NOI18N         
        tabbedPane.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACSD_Tabs")); // NOI18N         
        okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_SEARCH")); // NOI18N         
        cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_TEXT_BUTTON_CANCEL")); // NOI18N         
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        tabbedPane = new javax.swing.JTabbedPane();

        setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(tabbedPane, gridBagConstraints);

    }//GEN-END:initComponents

    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        /* Only exec. code disabled, because of issue #17737
        Component component = getTypeCustomizer(tabbedPane.getSelectedIndex());
        if(component != null)
            component.requestFocus();
         */
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables

    /** @return true if some criterion customized. */
    public boolean isCustomized() {
        return customized;
    }
    
    /**
     * Gets ordered criterion panels.
     *
     * @return iterator over properly ordered SearchTypePanel's.
     */
    private List getOrderedSearchTypePanels() {
        return new ArrayList(orderedSearchTypePanels);
    }

    /** @return name of criterion at index is modified. */
    private String getTabText(int index) {
        try {
            return ((SearchTypePanel)getOrderedSearchTypePanels().get(index)).getName(); 
        } catch (ArrayIndexOutOfBoundsException ex) {
            return null;
        }
    }

    /**
     * Gets array of customized search types.
     *
     * @return current state of customized search types.
     */
    public SearchType[] getCustomizedSearchTypes() {
        
        List searchTypeList = new ArrayList(orderedSearchTypePanels.size());
        
        for (Iterator it = orderedSearchTypePanels.iterator(); it.hasNext(); ) {
            SearchTypePanel searchTypePanel = (SearchTypePanel) it.next(); 
            if (searchTypePanel.isCustomized()) {
                searchTypeList.add(searchTypePanel.getSearchType());
            }
        }
        
        return (SearchType[]) searchTypeList.toArray(
                new SearchType[searchTypeList.size()]);
    }
    
    /**
     * Getter for return status property.
     *
     * @return the return status of this dialog - one of RET_OK or RET_CANCEL
     */
    public int getReturnStatus () {
        return returnStatus;
    }

    /** Closes dialog. */
    private void doClose(int returnStatus) {

        Iterator it = orderedSearchTypePanels.iterator();
        while (it.hasNext()) {
            SearchTypePanel panel = (SearchTypePanel) it.next();
            panel.removePropertyChangeListener(this);
        }

        this.returnStatus = returnStatus;

        dialog.setVisible(false);
        dialog.dispose();
    }

    /** Shows dialog created from DialogDescriptor which wraps this instance. */
    public void showDialog()  {
        dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
        dialog.setModal(true);
        tabbedPane.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                int selectedIndex = tabbedPane.getSelectedIndex();
                if (selectedIndex < 0) {
                    selectedIndex = 0;
                }
                Component component = getTypeCustomizer(selectedIndex);
                if (component != null) {
                    component.requestFocus();
                }
                tabbedPane.removeFocusListener(this);
            }
        });
        
        dialog.pack();
        dialog.setVisible(true);
    }

    /** Implements PropertyChangeListener interface. */
    public void propertyChange(PropertyChangeEvent event) {
        if(SearchTypePanel.PROP_CUSTOMIZED.equals(event.getPropertyName())) {
            customized = getCustomizedSearchTypes().length != 0;            
            
            okButton.setEnabled(isCustomized());
        }

        for(int i = 0; i < tabbedPane.getTabCount(); i++) {
            tabbedPane.setTitleAt(i, getTabText(i));
            tabbedPane.setIconAt(i, null);
        }
    }

   
    /** Gets help context. */
    private HelpCtx getHelpCtx() {
        int index = tabbedPane.getModel().getSelectedIndex(); 
        SearchTypePanel panel = (SearchTypePanel)getOrderedSearchTypePanels().get(index);
        
        return panel.getHelpCtx();
    }    
    
    /**
     * Gets type of customizer.
     *
     * @param index index of tab we need.
     */
    private Component getTypeCustomizer(int index) {
        SearchTypePanel searchTypePanel = null; 
        
        Iterator it = getOrderedSearchTypePanels().iterator();
        while(index >= 0 && it.hasNext()) {
            searchTypePanel = (SearchTypePanel)it.next();
            index--;
        }
        
        return searchTypePanel != null ? searchTypePanel.getComponent() : null;
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.