/*
 * 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.web.client.samples.wizard.ui;

import javax.swing.event.ChangeListener;
import org.netbeans.modules.web.client.samples.wizard.WizardConstants;
import org.openide.WizardDescriptor;
import org.openide.WizardValidationException;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;

/**
 *
 * @author Martin Janicek
 */
public class OnlineSamplePanel implements WizardDescriptor.AsynchronousValidatingPanel<WizardDescriptor> {

    private WizardDescriptor descriptor;
    private OnlineSampleVisualPanel myPanel;


    public OnlineSamplePanel(WizardDescriptor descriptor) {
        this.descriptor = descriptor;
    }

    @Override
    public OnlineSampleVisualPanel getComponent() {
        if (myPanel == null) {
            myPanel = new OnlineSampleVisualPanel(descriptor);
        }
        return myPanel;
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        getComponent().addChangeListener(listener);
    }

    @Override
    public void removeChangeListener(ChangeListener listener) {
        getComponent().removeChangeListener(listener);
    }

    @Override
    public HelpCtx getHelp() {
        return null;
        //return new HelpCtx("html5.samples"); // NOI18N
    }

    @Override
    public boolean isValid() {
        String error = getComponent().getErrorMessage();
        if (error != null && !error.isEmpty()) {
            setErrorMessage(error);
            return false;
        }
        setErrorMessage(""); // NOI18N
        return true;
    }

    private void setErrorMessage(String message) {
        descriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, message);
    }

    @Override
    public void readSettings(WizardDescriptor descriptor) {
        this.descriptor = descriptor;
        descriptor.putProperty("NewProjectWizard_Title", NbBundle.getMessage(
                OnlineSamplePanel.class, "TTL_SamplePanel"));         // NOI18N
    }

    @Override
    public void storeSettings(WizardDescriptor descriptor) {
        descriptor.putProperty(WizardConstants.SAMPLE_PROJECT_URL, getComponent().getProjectURL());
        descriptor.putProperty(WizardConstants.SAMPLE_PROJECT_NAME, getComponent().getProjectName());
        descriptor.putProperty(WizardConstants.SAMPLE_PROJECT_DIR, getComponent().getProjectDirectory());
    }

    @Override
    public void prepareValidation() {
    }

    @Override
    public void validate() throws WizardValidationException {
        final String error = getComponent().prepareTemplate();
        if (error != null) {
            throw new WizardValidationException(getComponent(), "ERROR_PREPARE", error); // NOI18N
        }
    }
}
