components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/model/SimpleModelControl.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.swing.model;
       
    27 
       
    28 import com.oracle.solaris.vp.panel.common.action.*;
       
    29 import com.oracle.solaris.vp.panel.common.model.*;
       
    30 import com.oracle.solaris.vp.panel.swing.control.SettingsControl;
       
    31 import com.oracle.solaris.vp.util.swing.SettingsPanel;
       
    32 
       
    33 /**
       
    34  * A basic control that loads a view with a model on start and saves the
       
    35  * view's settings to the model on save.
       
    36  * <br>
       
    37  * Separates out the initialization of the model so that subclasses (e.g.
       
    38  * wizard controls) can provide alternate implementations.
       
    39  */
       
    40 public class SimpleModelControl<M extends Model,
       
    41     P extends PanelDescriptor, V extends SettingsPanel & View<M>>
       
    42     extends SettingsControl<P, V> {
       
    43 
       
    44     //
       
    45     // Instance data
       
    46     //
       
    47 
       
    48     private M model_;
       
    49     private String helpMapID;
       
    50 
       
    51     //
       
    52     // Constructors
       
    53     //
       
    54 
       
    55     /**
       
    56      * Constructor for subclasses that override (@code createComponent}.
       
    57      */
       
    58     protected SimpleModelControl(String id, String name, P descriptor, M model)
       
    59     {
       
    60 	super(id, name, descriptor);
       
    61 	model_ = model;
       
    62     }
       
    63 
       
    64     public SimpleModelControl(String id, String name, P descriptor, M model,
       
    65 	V view) {
       
    66 
       
    67 	this(id, name, descriptor, model);
       
    68 	setComponent(view);
       
    69     }
       
    70 
       
    71     //
       
    72     // Control methods
       
    73     //
       
    74 
       
    75     @Override
       
    76     public String getHelpMapID() {
       
    77 	return helpMapID;
       
    78     }
       
    79 
       
    80     //
       
    81     // SwingControl methods
       
    82     //
       
    83 
       
    84     @Override
       
    85     protected V createComponent() {
       
    86 	throw new UnsupportedOperationException("Not implemented.");
       
    87     }
       
    88 
       
    89     @Override
       
    90     protected void initComponent() {
       
    91 	initModel();
       
    92 	getComponent().modelToView(model_);
       
    93     }
       
    94 
       
    95     @Override
       
    96     protected void save() throws ActionAbortedException, ActionFailedException,
       
    97 	ActionUnauthorizedException {
       
    98 
       
    99 	getComponent().viewToModel(model_);
       
   100 	saveModel();
       
   101 	super.save();
       
   102     }
       
   103 
       
   104     //
       
   105     // SimpleModelControl methods
       
   106     //
       
   107 
       
   108     public M getModel() {
       
   109 	return model_;
       
   110     }
       
   111 
       
   112     /**
       
   113      * Called when component is initialized.
       
   114      */
       
   115     protected void initModel() {
       
   116 	model_.load();
       
   117     }
       
   118 
       
   119     /**
       
   120      * Called from {@link #save}, once the model has been populated from the UI.
       
   121      */
       
   122     protected void saveModel() throws ActionFailedException,
       
   123 	ActionUnauthorizedException {
       
   124 
       
   125 	model_.save();
       
   126     }
       
   127 
       
   128     public void setHelpMapID(String helpMapID) {
       
   129 	this.helpMapID = helpMapID;
       
   130     }
       
   131 
       
   132     /*
       
   133      * Static methods
       
   134      */
       
   135 
       
   136     /**
       
   137      * Convenience factory method that spares the consumer the tedium of
       
   138      * specifying the type of the instance.
       
   139      */
       
   140     public static <M extends Model, P extends PanelDescriptor,
       
   141 	V extends SettingsPanel & View<M>> SimpleModelControl<M, P, V>
       
   142 	createControl(String id, String name, P descriptor, M model, V view)
       
   143     {
       
   144 	return (new SimpleModelControl<M, P, V>(id, name, descriptor, model,
       
   145 	    view));
       
   146     }
       
   147 }