/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.dtv.lwuit;
import com.sun.dtv.lwuit.geom.Dimension;
import com.sun.dtv.lwuit.events.ActionEvent;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.plaf.UIManager;
import java.util.Date;
/**
*
*/
class MonthView extends Component {
public static final long MINUTE = 1000 * 60;
public static final long HOUR = MINUTE * 60;
public static final long DAY = HOUR * 24;
public static final long WEEK = DAY * 7;
private long selectedDay;
private EventDispatcher dispatcher = new EventDispatcher();
private Calendar parent;
public MonthView(long time, Calendar parent) {
this.selectedDay = time;
this.parent = parent;
}
/**
* @inheritDoc
*/
protected String getUIID() {
return "MonthView";
}
void focusGainedInternal() {
setHandlesInput(true);
}
/**
* @inheritDoc
*/
public void keyReleased(int keyCode) {
boolean right = true;
boolean down = true;
if(Display.getInstance().getGameAction(keyCode) != Display.GAME_FIRE){
if(handlesInput()){
switch (Display.getInstance().getGameAction(keyCode)) {
case Display.GAME_UP:
down = false;
case Display.GAME_DOWN:
upDown(down);
break;
case Display.GAME_LEFT:
right = false;
// fall
case Display.GAME_RIGHT:
leftRight(right);
break;
}
}
} else {
setHandlesInput(!handlesInput());
}
repaint();
}
private void upDown(boolean down) {
selectedDay += ((down ? 1: -1) * WEEK);
fireActionEvent();
}
private void leftRight(boolean forward) {
selectedDay += (forward ? 1 : -1) * DAY;
fireActionEvent();
}
public long getSelectedDay() {
return selectedDay;
}
public void setSelectedDay(long day){
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(new Date(selectedDay));
int yearOld = cal.get(java.util.Calendar.YEAR);
int monthOld = cal.get(java.util.Calendar.MONTH);
int dayOld = cal.get(java.util.Calendar.DAY_OF_MONTH);
cal.setTime(new Date(day));
int yearNew = cal.get(java.util.Calendar.YEAR);
int monthNew = cal.get(java.util.Calendar.MONTH);
int dayNew = cal.get(java.util.Calendar.DAY_OF_MONTH);
if(yearNew!=yearOld ||monthNew != monthOld ||dayNew != dayOld){
this.selectedDay = day;
repaint();
fireActionEvent();
}
}
public int getDayOfMonth(){
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(new Date(selectedDay));
return cal.get(java.util.Calendar.DAY_OF_MONTH);
}
public int getMonth(){
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(new Date(selectedDay));
return cal.get(java.util.Calendar.MONTH);
}
public int getYear(){
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(new Date(selectedDay));
return cal.get(java.util.Calendar.YEAR);
}
public void addActionListener(ActionListener l){
dispatcher.addListener(l);
}
public void removeActionListener(ActionListener l){
dispatcher.removeListener(l);
}
protected void fireActionEvent() {
parent.componentChanged();
super.fireActionEvent();
dispatcher.fireActionEvent(new ActionEvent(parent));
}
/**
* @inheritDoc
*/
public void pointerReleased(int x, int y) {
setHandlesInput(true);
setSelectedDay(UIManager.getInstance().getLookAndFeel().findDayAt(x, y, (Calendar)getParent(), this));
}
/**
* @inheritDoc
*/
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawMonthView(g, (Calendar)getParent(), this);
}
/**
* @inheritDoc
*/
protected Dimension calcPreferredSize(){
return UIManager.getInstance().getLookAndFeel().getMonthViewPreferredSize(this);
}
}