components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/time/TimeSpinnerModel.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing.time;
       
    27 
       
    28 import java.beans.*;
       
    29 import java.util.Calendar;
       
    30 import javax.swing.AbstractSpinnerModel;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class TimeSpinnerModel extends AbstractSpinnerModel {
       
    34     //
       
    35     // Instance data
       
    36     //
       
    37 
       
    38     private TimeModel model;
       
    39     private TimeSelectionModel selModel;
       
    40 
       
    41     private PropertyChangeListener timeListener =
       
    42 	new PropertyChangeListener() {
       
    43 	    @Override
       
    44 	    public void propertyChange(PropertyChangeEvent event) {
       
    45 		fireStateChanged();
       
    46 	    }
       
    47 	};
       
    48 
       
    49     //
       
    50     // Constructors
       
    51     //
       
    52 
       
    53     /**
       
    54      * Constructs a {@code TimeSpinnerModel} for the given model.
       
    55      */
       
    56     public TimeSpinnerModel(TimeModel model) {
       
    57 	setTimeSelectionModel(new SimpleTimeSelectionModel());
       
    58 	setTimeModel(model);
       
    59     }
       
    60 
       
    61     /**
       
    62      * Constructs a {@code TimeSpinnerModel} with a zero {@link
       
    63      * TimeModel#getTimeOffset offset} from the system clock.
       
    64      */
       
    65     public TimeSpinnerModel() {
       
    66 	this(new SimpleTimeModel());
       
    67     }
       
    68 
       
    69     //
       
    70     // SpinnerModel methods
       
    71     //
       
    72 
       
    73     @Override
       
    74     public void setValue(Object value) {
       
    75 	model.setTimeOffset((Long)value);
       
    76     }
       
    77 
       
    78     @Override
       
    79     public Object getNextValue() {
       
    80         return getPreviousOrNextValue(true);
       
    81     }
       
    82 
       
    83     @Override
       
    84     public Object getPreviousValue() {
       
    85         return getPreviousOrNextValue(false);
       
    86     }
       
    87 
       
    88     @Override
       
    89     public Object getValue() {
       
    90         return model.getTimeOffset();
       
    91     }
       
    92 
       
    93     //
       
    94     // TimeSpinnerModel methods
       
    95     //
       
    96 
       
    97     public TimeModel getTimeModel() {
       
    98 	return model;
       
    99     }
       
   100 
       
   101     public TimeSelectionModel getTimeSelectionModel() {
       
   102 	return selModel;
       
   103     }
       
   104 
       
   105     public void setTimeModel(TimeModel model) {
       
   106 	if (this.model != model) {
       
   107 	    if (this.model != null) {
       
   108 		this.model.removePropertyChangeListener(timeListener);
       
   109 	    }
       
   110 
       
   111 	    model.addPropertyChangeListener(TimeModel.PROPERTY_TIME,
       
   112 		timeListener);
       
   113 
       
   114 	    this.model = model;
       
   115 	    fireStateChanged();
       
   116 	}
       
   117     }
       
   118 
       
   119     public void setTimeSelectionModel(TimeSelectionModel selModel) {
       
   120 	this.selModel = selModel;
       
   121     }
       
   122 
       
   123     //
       
   124     // Private methods
       
   125     //
       
   126 
       
   127     private long getPreviousOrNextValue(boolean next) {
       
   128 	long delta = 0;
       
   129 	int field = selModel.getSelectedField();
       
   130 
       
   131 	if (field >= 0 && field < Calendar.FIELD_COUNT) {
       
   132 	    if (field == Calendar.AM_PM) {
       
   133 		// AM_PM is a special field -- toggle regardless of direction
       
   134 		next = model.getCalendar().get(field) == Calendar.AM;
       
   135 	    }
       
   136 
       
   137 	    Calendar cal = model.getCalendar();
       
   138 	    long before = cal.getTimeInMillis();
       
   139 	    cal.add(field, next ? 1 : -1);
       
   140 	    long after = cal.getTimeInMillis();
       
   141 	    delta = after - before;
       
   142 	}
       
   143 
       
   144         return model.getTimeOffset() + delta;
       
   145     }
       
   146 }