usr/src/java/vpanels/panels/apache/org/opensolaris/os/vp/panels/apache/client/swing/VirtualHostGeneralControl.java
author Stephen Talley <stephen.talley@sun.com>
Tue, 22 Sep 2009 16:46:54 -0400
changeset 360 8943c58ad2f5
parent 358 dd6c77482585
child 361 d3611020acea
permissions -rw-r--r--
11508 Control, DefaultControl refactoring

/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

package org.opensolaris.os.vp.panels.apache.client.swing;

import java.awt.event.*;
import java.net.URL;
import java.util.Map;
import org.opensolaris.os.vp.panel.common.action.*;
import org.opensolaris.os.vp.panel.common.control.*;
import org.opensolaris.os.vp.panel.swing.control.SwingSettingsControl;
import org.opensolaris.os.vp.util.misc.Finder;
import org.opensolaris.os.vp.util.misc.property.MutableProperty;

public class VirtualHostGeneralControl
    extends SwingSettingsControl<ApachePanelDescriptor, VirtualHostGeneralPanel>
    implements HasMimeTypes {

    //
    // Static data
    //

    public static final String ID = "general";
    public static final String NAME = Finder.getString("vhost.general.name");

    //
    // Instance data
    //

    private VirtualHostControl parent;

    //
    // Constructors
    //

    public VirtualHostGeneralControl(VirtualHostControl parent) {
	super(ID, NAME, parent.getPanelDescriptor());
	this.parent = parent;
    }

    //
    // HasMimeTypes methods
    //

    @Override
    public MimeTypes getMimeTypes() {
	return parent.getVirtualHost().getMimeTypes();
    }

    //
    // Control methods
    //

    @Override
    public URL getHelpURL() {
	return buildHelpURL("help/apache.html", "#apache-vhosts-edit");
    }

    @Override
    protected UnsavedChangesAction getUnsavedChangesAction() {
	// Automatically save changes to the VirtualHost -- let MainControl
	// prompt the user to save them to the repo
	return UnsavedChangesAction.SAVE;
    }

    @Override
    protected boolean isSaveNeeded() {
	// Save unconditionally, since a property that reports itself as
	// unchanged in the panel may still be changed from the corresponding
	// property in the VirtualHost.  Alternatively, we could compare each
	// panel property to its VirtualHost counterpart and save only if there
	// are differences, but this is easier and save() has low overhead.
	return true;
    }

    @Override
    protected void save() throws ActionAbortedException, ActionFailedException,
	ActionUnauthorizedException {

	setPropertyChangeIgnore(true);

	try {
	    VirtualHost vHost = parent.getVirtualHost();
	    VirtualHostGeneralPanel panel = getComponent();

	    String domain = panel.getDomainProperty().getValue();
	    validateDomain(domain, vHost.getParent(), vHost);
	    vHost.getDomainProperty().setFirstValue(domain);

	    vHost.getEnabledProperty().setFirstValue(
		panel.getEnabledProperty().getValue());

	    vHost.getPortProperty().setFirstValue(
		panel.getPortProperty().getValue());

	    String docRoot = panel.getDocRootProperty().getValue();
	    validateDocRoot(docRoot);
	    vHost.getDocRootProperty().setFirstValue(
		panel.getDocRootProperty().getValue());

	    vHost.getServeHomeDirsProperty().setFirstValue(
		panel.getServeHomeDirsProperty().getValue());

	    MutableProperty<Boolean> customEnabledProperty =
		panel.getCustomEnabledProperty();

	    boolean customEnabled = customEnabledProperty.getValue();

	    MutableProperty<String> customFileProperty =
		panel.getCustomFileProperty();

	    String customFile = customFileProperty.getValue();

	    if (customEnabled && customFile.isEmpty()) {
		throw new ActionFailedException(Finder.getString(
		    "vhost.general.error.customfile.empty"));
	    }

	    vHost.getCustomEnabledProperty().setFirstValue(customEnabled);

	    vHost.getCustomFileProperty().setFirstValue(customFile);
	} finally {
	    setPropertyChangeIgnore(false);
	}
    }

    @Override
    public void start(Navigator navigator, Map<String, String> parameters)
	throws NavigationAbortedException, InvalidParameterException {

	setPropertyChangeSource(parent.getVirtualHost(),
	    VirtualHost.PROPERTY_ENABLED, VirtualHost.PROPERTY_PORT,
	    VirtualHost.PROPERTY_DOMAIN, VirtualHost.PROPERTY_DOCROOT,
	    VirtualHost.PROPERTY_SERVE_HOME_DIRS,
	    VirtualHost.PROPERTY_CUSTOM_ENABLED,
	    VirtualHost.PROPERTY_CUSTOM_FILE);

	super.start(navigator, parameters);
    }

    //
    // DefaultControl methods
    //

    @Override
    protected void ensureChildrenCreated() {
	if (children.size() == 0) {
	    addDialogChildren(new MimeTypesControl(getPanelDescriptor(), this));
	}
    }

    //
    // SwingControl methods
    //

    @Override
    protected VirtualHostGeneralPanel createComponent() {
	VirtualHostGeneralPanel panel = new VirtualHostGeneralPanel();

	panel.getMimeTypesButton().addActionListener(
	    new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
		    Navigable navigable = new SimpleNavigable(
			MimeTypesControl.ID, null);

		    getNavigator().goToAsync(false,
			VirtualHostGeneralControl.this, navigable);
		}
	    });

	return panel;
    }

    @Override
    protected void deinitComponent() {
	VirtualHost vHost = parent.getVirtualHost();
	VirtualHostGeneralPanel panel = getComponent();

	vHost.getMimeTypes().getChangeableAggregator().removeChangeListener(
	    panel.getMimeTypesButton());

	panel.init(null);
    }

    @Override
    protected void initComponent() {
	VirtualHost vHost = parent.getVirtualHost();
	VirtualHostGeneralPanel panel = getComponent();

	vHost.getMimeTypes().getChangeableAggregator().addChangeListener(
	    panel.getMimeTypesButton());

	panel.getChangeableAggregator().reset();
	panel.init(vHost);
    }

    //
    // Static methods
    //

    public static void validateDomain(String domain,
	ApachePanelDescriptor descriptor, VirtualHost vHost)
	throws ActionFailedException {

	if (domain.isEmpty()) {
	    throw new ActionFailedException(Finder.getString(
		"vhost.general.error.domain.empty"));
	}

	if (!VirtualHost.isValidDomainName(domain)) {
	    throw new ActionFailedException(Finder.getString(
		"vhost.general.error.domain.invalid", domain));
	}

	VirtualHost existing = descriptor.getVirtualHost(domain);
	if (existing != null && existing != vHost) {
	    throw new ActionFailedException(Finder.getString(
		"vhost.general.error.domain.duplicate", domain));
	}
    }

    public static void validateDocRoot(String docRoot)
	throws ActionFailedException {

	if (docRoot.isEmpty()) {
	    throw new ActionFailedException(Finder.getString(
		"vhost.general.error.docroot.empty"));
	}
    }
}