/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.modules.java.editor.codegen.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import java.util.List;
import javax.lang.model.element.Element;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.modules.java.editor.codegen.GeneratorUtils;
import org.netbeans.modules.java.editor.codegen.GetterSetterGenerator;
import org.openide.explorer.ExplorerManager;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;

/**
 *
 * @author  Dusan Balek
 */
public class GetterSetterPanel extends JPanel implements PropertyChangeListener {
    
    private ElementSelectorPanel elementSelector;
    private HashSet<ElementHandle<? extends Element>> thisFields;
    private RequestProcessor.Task currentTask;
    private final RequestProcessor RP = new RequestProcessor(GetterSetterPanel.class);
    
    
    /** Creates new form GetterSetterPanel */
    public GetterSetterPanel(ElementNode.Description description, int type) {
        initComponents();
        elementSelector = new ElementSelectorPanel(description, false);
        elementSelector.getExplorerManager().addPropertyChangeListener(this);
        thisFields = new HashSet<ElementHandle<? extends Element>>();
        for (ElementNode.Description desc:description.getSubs().get(0).getSubs()) {
            thisFields.add(desc.getElementHandle());
        }
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 12);
        add(elementSelector, gridBagConstraints);
        if (type == GeneratorUtils.GETTERS_ONLY)
            selectorLabel.setText(NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter_field_select")); //NOI18N
        else if (type == GeneratorUtils.SETTERS_ONLY)
            selectorLabel.setText(NbBundle.getMessage(GetterSetterGenerator.class, "LBL_setter_field_select")); //NOI18N
        else
            selectorLabel.setText(NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter_and_setter_field_select")); //NOI18N
        selectorLabel.setLabelFor(elementSelector);
        
        elementSelector.doInitialExpansion(1);
	
	this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GetterSetterGenerator.class, "A11Y_Generate_GetterSetter"));
    }
    
    public List<ElementHandle<? extends Element>> getVariables() {
        return ((ElementSelectorPanel)elementSelector).getSelectedElements();
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        selectorLabel = new javax.swing.JLabel();
        performEncapsulate = new javax.swing.JCheckBox();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12);
        add(selectorLabel, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(performEncapsulate, org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "GetterSetterPanel.performEncapsulate.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
        add(performEncapsulate, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox performEncapsulate;
    private javax.swing.JLabel selectorLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (ExplorerManager.PROP_NODE_CHANGE.equals(evt.getPropertyName()))
            updateEncapsulateCheckBox();
        firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
    }

    private void updateEncapsulateCheckBox() {
        if (currentTask!=null) {
            currentTask.cancel();
        }
        currentTask = RP.post(new Runnable() {

            @Override
            public void run() {
                final List<ElementHandle<? extends Element>> selected = elementSelector.getSelectedElements();
                selected.removeAll(thisFields);
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        performEncapsulate.setEnabled(selected.isEmpty());
                    }
                });
            }
        });
    }
    
    public boolean isPerformEnsapsulate() {
        return performEncapsulate.isEnabled() && performEncapsulate.isSelected();
    }
}
