
package com.togethersoft.modules.qa.metrics.ExmMt2;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import com.togethersoft.openapi.sci.*;
import com.togethersoft.openapi.sci.enum.*;

import com.togethersoft.modules.qa.api.*;
import com.togethersoft.modules.qa.utils.*;

public class ExmMt2 extends MetricsPluginImpl {

    private boolean okPublic;
    private boolean okProtected;
    private boolean okPrivate;
    private boolean okPackage;

    // plugin entry point
    public boolean run(SciObject obj, MetricsResult result) {

        // we have no need to call AuditPluginImpl.process()
        // since we need only to enumerate class operations
        // and have no need of processing of their code
        // By the same reason, we can allow processing of byte codes
        // (see method canProcessByteCode below)

        // process only classes
        if( !(obj instanceof SciClass) )
            return true;

        SciClass cls = (SciClass)obj;

        int cnt = 0;

        // get class operations enumeration
        SciOperationEnumeration oprs = cls.operations();

        // go through all class operations
        while (oprs.hasMoreElements()) {
            SciOperation opr = oprs.nextSciOperation();

            if( opr.hasProperty(SciProperty.PUBLIC) ) {
                if( okPublic )
                    cnt++;
            } else
            if( opr.hasProperty(SciProperty.PROTECTED) ) {
                if( okProtected )
                    cnt ++;
            } else
            if( opr.hasProperty(SciProperty.PRIVATE) ) {
                if( okPrivate )
                    cnt ++;
            }
            else
            if( opr.hasProperty(SciProperty.PACKAGE_LOCAL) ) {
                if( okPackage )
                    cnt ++;
            }
        }
        result.setValue(cnt);
        return true;
    }

    // Plugin title - would be displayed in the plugins list
    public String getTitle ()
    {
        return "Metrics example: maximum number of operations within class";
    }

    // group name - used for grouping plugins
    public String getGroupName ()
    {
        return "Samples";
    }


    // don't aggregate results
    public String getDefaultAggregation() {
        return AGGR_NONE;
    }

    // allow processing of byte codes,
    // since we are only counting operations, we needn't examine source code -
    public boolean canProcessByteCode () {
        return true;
    }

    private  JPanel panOptions;

    private  JCheckBox chkPublic;
    private  JCheckBox chkProtected;
    private  JCheckBox chkPrivate;
    private  JCheckBox chkPackage;

    // return options pane, which is always created by createOptionsPane()
    public JComponent getOptionsPane () {
        return panOptions;
    }

    // creating options pane
    public JComponent createOptionsPane () {

        panOptions = new JPanel();

        chkPublic = new JCheckBox("Public");
        chkProtected = new JCheckBox("Protected");
        chkPrivate = new JCheckBox("Private");
        chkPackage = new JCheckBox("Package");

        panOptions.setLayout(new GridLayout(0, 1));

        panOptions.add(chkPublic);
        panOptions.add(chkProtected);
        panOptions.add(chkPrivate);
        panOptions.add(chkPackage);

        panOptions.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
        return panOptions;
    }

    // restoring options from properties file
    public void restoreOptions () {
        okPublic    = getProperty("Public",     true);
        okProtected = getProperty("Protected",  true);
        okPrivate   = getProperty("Private",    true);
        okPackage   = getProperty("Package",    true);

        chkPublic.setSelected(okPublic);
        chkProtected.setSelected(okProtected);
        chkPrivate.setSelected(okPrivate);
        chkPackage.setSelected(okPackage);

        super.restoreOptions();
    }

    // storing options to properties file
    public boolean storeOptions () {
        okPublic    = chkPublic.isSelected();
        okProtected = chkProtected.isSelected();
        okPrivate   = chkPrivate.isSelected();
        okPackage   = chkPackage.isSelected();

        putProperty("Public", okPublic);
        putProperty("Protected", okProtected);
        putProperty("Private", okPrivate);
        putProperty("Package", okPackage);

        return super.storeOptions();
    }
}
