com.htmlhifive.tools.jslint.dialog.OptionSettingDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.dialog.OptionSettingDialog.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed 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 com.htmlhifive.tools.jslint.dialog;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.htmlhifive.tools.jslint.engine.option.CheckOption;
import com.htmlhifive.tools.jslint.engine.option.CheckOptionFileWrapper;
import com.htmlhifive.tools.jslint.engine.option.Engine;
import com.htmlhifive.tools.jslint.event.CheckOptionChangeEvent;
import com.htmlhifive.tools.jslint.event.CheckOptionChangeListener;
import com.htmlhifive.tools.jslint.messages.Messages;
import com.htmlhifive.tools.jslint.view.JslintOptionComposite;

/**
 * ??.
 * 
 * @author NS Solutions Corporation
 * 
 */
public class OptionSettingDialog extends Dialog {

    /**
     * .
     */
    private IProject project;
    /**
     * .
     */
    private CLabel messageLabel;
    /**
     * ?.
     */
    private JslintOptionComposite comp;
    /**
     * .
     */
    private Engine engine;

    /**
     * .
     * 
     * @param parentShell 
     * @param project 
     * @param engine 
     */
    public OptionSettingDialog(Shell parentShell, IProject project, Engine engine) {

        super(parentShell);
        this.project = project;
        this.engine = engine;

    }

    /*
     * (? Javadoc)
     * 
     * @see
     * org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets
     * .Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {

        Composite composite = (Composite) super.createDialogArea(parent);
        createMessageArea(composite);

        comp = new JslintOptionComposite(composite, project);
        comp.addCheckOptionChangeListener(new CheckOptionChangeListener() {

            @Override
            public void modify(CheckOptionChangeEvent event) {

                CheckOptionFileWrapper optionFile = event.getOptionFile();
                String[] errorMessages = checkOption(optionFile);
                getButton(OK).setEnabled(errorMessages.length == 0);
                updateErrorMessage(errorMessages);

            }

        });
        GridData gdComp = new GridData(GridData.FILL_BOTH);
        gdComp.widthHint = 900;
        comp.setLayoutData(gdComp);
        return super.createDialogArea(parent);

    }

    /**
     * ???.
     * 
     * @param parent ?.
     * @return ?.
     */
    private Composite createMessageArea(Composite parent) {

        final Composite composite = new Composite(parent, SWT.NONE);
        final GridLayout layout = new GridLayout();
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.verticalSpacing = 0;
        layout.horizontalSpacing = 0;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // ?
        messageLabel = new CLabel(composite, SWT.HORIZONTAL);
        messageLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        messageLabel.setText(Messages.DT0006.getText());
        // 
        Label titleBarSeparator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
        titleBarSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        return composite;
    }

    /**
     * ??.
     * 
     * @param errorMessages 
     */
    private void updateErrorMessage(String[] errorMessages) {

        if (errorMessages == null || errorMessages.length == 0) {
            messageLabel.setText(Messages.DT0006.getText());
            messageLabel.setImage(null);
            return;
        }
        messageLabel.setText(StringUtils.join(errorMessages, " "));
        messageLabel.setImage(Dialog.getImage(DLG_IMG_MESSAGE_ERROR));
    }

    /**
     * ??.
     * 
     * @param optionFile .
     * @return 
     */
    private String[] checkOption(CheckOptionFileWrapper optionFile) {

        List<String> errorMessageList = new ArrayList<String>();

        CheckOption[] options = optionFile.getOptions(engine);
        for (CheckOption option : options) {
            if (option.getClazz() == Integer.class && option.isEnable()) {
                String value = option.getValue();
                if (!NumberUtils.isNumber(value)) {
                    errorMessageList.add(Messages.EM0005.format(option.getKey()));
                }
            }
        }
        return (String[]) errorMessageList.toArray(new String[errorMessageList.size()]);

    }

    @Override
    protected void okPressed() {

        super.okPressed();
        comp.getOptionFile().saveOption();
    }

    @Override
    protected boolean isResizable() {

        return true;
    }

    @Override
    protected void configureShell(Shell newShell) {

        super.configureShell(newShell);
        newShell.setText(Messages.DT0006.getText());
    }

}