usr/src/java/vpanels/panels/svcs/org/opensolaris/os/vp/panels/svcs/client/swing/InstancesControl.java
author Stephen Talley <stephen.talley@sun.com>
Tue, 22 Sep 2009 16:46:54 -0400
changeset 360 8943c58ad2f5
parent 350 1e9744735024
child 366 610ee2561491
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.svcs.client.swing;

import org.opensolaris.os.vp.panel.swing.smf.EnableServiceAction;
import java.net.URL;
import java.util.*;
import org.opensolaris.os.vp.panel.common.action.StructuredAction;
import org.opensolaris.os.vp.panel.common.control.*;
import org.opensolaris.os.vp.panel.common.smf.InstanceManagedObject;
import org.opensolaris.os.vp.panel.swing.action.SimpleLaunchManagedObjectAction;
import org.opensolaris.os.vp.panel.swing.control.*;
import org.opensolaris.os.vp.util.misc.Finder;

public class InstancesControl
    extends SwingSettingsControl<SvcsPanelDescriptor, InstancesPanel> {

    /*
     * Static data
     */

    public static final String ID = "instances";
    private static final String NAME = Finder.getString("smf.instances.title");

    /*
     * Instance data
     */
    private StructuredAction<List<InstanceManagedObject>, ?, ?> editAction_ =
	new SimpleLaunchManagedObjectAction<InstanceManagedObject>(this) {

	@Override
	protected String constructPath(InstanceManagedObject obj) {
	    return String.format("/vp/smf/%s/%s",
		Control.encode("svc", Collections.singletonMap("service",
		obj.getServiceFmri().getService())),
		Control.encode("instance", Collections.singletonMap("instance",
		obj.getSMFFmri().getInstance())));
	}
    };

    private EnableServiceAction enableAction_ =
	new EnableServiceAction(this, true);

    private EnableServiceAction disableAction_ =
	new EnableServiceAction(this, false);

    @SuppressWarnings({"unchecked"})
    protected StructuredAction<List<InstanceManagedObject>, ?, ?>[] actions_ =
	new StructuredAction[] {
	    enableAction_, disableAction_, editAction_
	};

    private InstanceFilter filter_ = null;

    public static final String PARAM_FILTER = "filter";

    /*
     * Constructors
     */

    public InstancesControl(SvcsPanelDescriptor descriptor)
    {
	super(ID, NAME, descriptor);
    }

    //
    // Control methods
    //

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

    @Override
    public boolean isBrowsable() {
	// This Control requires init parameters
	return false;
    }

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

	String param = getParameter(parameters, PARAM_FILTER);

	InstanceFilter filter = getPanelDescriptor().getFilter(param);
	if (filter == null)
	    throw new InvalidParameterException(getId(), PARAM_FILTER, param);

	filter_ = filter;
	setName(filter.getName());

	super.start(navigator, parameters);
    }

    @Override
    public void stop(boolean isCancel) throws NavigationAbortedException {
	super.stop(isCancel);

	// Remove reference so it can be garbage collected if deleted
	filter_ = null;
    }

    /*
     * SwingControl methods
     */

    @Override
    protected InstancesPanel createComponent()
    {
	InstancesPanel panel =
	   new InstancesPanel(getPanelDescriptor(), actions_, editAction_);

	return panel;
    }

    @Override
    protected void initComponent()
    {
	try {
	    getComponent().getFmo().setManagedObject(
		filter_.getManagedObject());
	} catch (Throwable t) {
	    System.out.println("setManagedObject failed: " + t);
	    t.printStackTrace();
	}
    }

    @Override
    protected void deinitComponent()
    {
	// getComponent().getFmo().setPredicate(TruePredicate.getInstance());
    }

    public InstanceFilter getFilter()
    {
	return (filter_);
    }
}