Home | History | Annotate | Download | only in swing
      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 2010 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 package org.opensolaris.os.vp.panels.example.time2.client.swing;
     28 
     29 import java.util.Date;
     30 import org.opensolaris.os.rad.ObjectException;
     31 import org.opensolaris.os.vp.panel.common.action.*;
     32 import org.opensolaris.os.vp.panel.swing.control.SwingSettingsControl;
     33 
     34 public class TimeControl
     35     extends SwingSettingsControl<TimePanelDescriptor, TimePanel> {
     36 
     37     //
     38     // Instance data
     39     //
     40 
     41     private Date date;
     42 
     43     //
     44     // Constructors
     45     //
     46 
     47     public TimeControl(TimePanelDescriptor descriptor) {
     48 	super(descriptor.getId(), descriptor.getName(), descriptor);
     49     }
     50 
     51     //
     52     // Control methods
     53     //
     54 
     55     @Override
     56     protected void save() throws ActionAbortedException, ActionFailedException,
     57 	ActionUnauthorizedException {
     58 
     59 	Date newDate = (Date)getComponent().getSpinnerDateModel().getValue();
     60 
     61 	if (!newDate.equals(date)) {
     62 	    long time = date.getTime();
     63 	    try {
     64 		getPanelDescriptor().getTimeBean().setTime(time);
     65 	    } catch (ObjectException e) {
     66 		throw new ActionFailedException(e);
     67 	    } catch (SecurityException e) {
     68 		throw new ActionUnauthorizedException(e);
     69 	    }
     70 	}
     71     }
     72 
     73     //
     74     // SwingControl methods
     75     //
     76 
     77     @Override
     78     protected TimePanel createComponent() {
     79 	TimePanel panel = new TimePanel();
     80 
     81 	addDefaultApplyAction(panel);
     82 	addDefaultCancelAction(panel, true);
     83 	addDefaultOkayAction(panel, true);
     84 
     85 	return panel;
     86     }
     87 
     88     @Override
     89     protected void initComponent() {
     90 	try {
     91 	    long time = getPanelDescriptor().getTimeBean().getTime();
     92 	    date = new Date(time);
     93 	} catch (ObjectException e) {
     94 	    /* Shouldn't happen */
     95 	    date = new Date();
     96 	}
     97 	getComponent().getSpinnerDateModel().setValue(date);
     98     }
     99 }
    100