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