Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing help please

Status
Not open for further replies.

andrewbadera

Programmer
Jan 27, 2002
43
0
0
US
I'm fairly new to Java, having written a few HTTP web-bots and some console JDBC stuff, and have only been using Swing for the last week or so. I'm putting together an labor and resources scheduling application, and chose to use a third party calendar Swing component.

the first one I used had too many flaws and was too restrictive, so I picked up the Kiwi ui toolkit. unfortunately, I don't know enough about Swing, and/or don't understand kiwi.ui.DateChooser well enough to figure out how, or if I even can, produce the DateChooser calendar in fullscreeen.

can anyone help? TIA.

the DateChooser class:
/* ----------------------------------------------------------------------------
The Kiwi Toolkit
Copyright (C) 1998-99 Mark A. Lindner

This file is part of Kiwi.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

The author may be contacted at:

frenzy@ix.netcom.com
----------------------------------------------------------------------------

$Log: $
----------------------------------------------------------------------------
*/

package kiwi.ui;

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

import kiwi.util.KiwiUtils;

/** This class represents a date chooser. The chooser allows an arbitrary date
* to be selected by presenting a calendar with day, month and year selectors.
*
* <p><center>
* <img src=&quot;snapshot/DateChooser.gif&quot;><br>
* <i>An example DateChooser.</i>
* </center>
*
* @author Mark Lindner
* @author David Croy
* @author PING Software Group
* @version 2.1 (1/99)
*/

public class DateChooser extends KPanel implements ActionListener
{
private JLabel l_date;
private JLabel l_year, l_month;
private SimpleDateFormat datefmt = new SimpleDateFormat(&quot;E d MMM yyyy&quot;);
private RepeaterButton b_lyear, b_ryear, b_lmonth, b_rmonth;
private CalendarPane cal;
// The end calendar will always be the same month
private Calendar startDate, endDate;
private int startDay, endDay, firstDay;
private static final int cellSize = 25;
private static final String[] labels
= { &quot;M&quot;, &quot;Tu&quot;, &quot;W&quot;, &quot;Th&quot;, &quot;F&quot;, &quot;Sa&quot;, &quot;Su&quot; };
private static final int[] daysInMonth
= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private static final String[] months = { &quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;,
&quot;Jun&quot;, &quot;Jul&quot;, &quot;Aug&quot;, &quot;Sep&quot;, &quot;Oct&quot;,
&quot;Nov&quot;, &quot;Dec&quot; };
private static Color weekendColor = Color.red.darker();


/** Construct a new <code>DateChooser</code>. The selection will be
* initialized to the current date.
*/

public DateChooser()
{
startDate = Calendar.getInstance();
endDate = (Calendar)startDate.clone();
_init();
}

/** Construct a new <code>DateChooser</code> with the specified selected
* start date.
*
* @param <code>date</code> The date for the selection.
*/
public DateChooser(Calendar date)
{
startDate = (Calendar)date.clone();
endDate = (Calendar)date.clone();
_init();
}

/** Construct a new <code>DateChooser</code> with the specified selected
* start and end dates.
*
* @param <code>start</code> The start date for the selection.
* @param <code>end</code> The end date for the selection.
*/

public DateChooser(Calendar start, Calendar end)
{
startDate = (Calendar)start.clone();
endDate = (Calendar)end.clone();
_init();
}

/* initialization code */

private void _init()
{
setLayout(new BorderLayout(5, 5));
setOpaque(false);

KPanel top = new KPanel();
top.setLayout(new BorderLayout(0, 0));

KPanel p1 = new KPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
top.add(&quot;West&quot;, p1);

b_lmonth = new RepeaterButton(KiwiUtils.getResourceManager()
.getIcon(&quot;repeater-left.gif&quot;));
b_lmonth.setMargin(KiwiUtils.emptyInsets);
b_lmonth.setFocusPainted(false);
b_lmonth.setOpaque(false);
b_lmonth.addActionListener(this);
p1.add(b_lmonth);

l_month = new JLabel();
l_month.setForeground(Color.black);
l_month.setOpaque(false);
p1.add(l_month);

b_rmonth = new RepeaterButton(KiwiUtils.getResourceManager()
.getIcon(&quot;repeater-right.gif&quot;));
b_rmonth.setMargin(KiwiUtils.emptyInsets);
b_rmonth.setFocusPainted(false);
b_rmonth.setOpaque(false);
b_rmonth.addActionListener(this);
p1.add(b_rmonth);

KPanel p2 = new KPanel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
top.add(&quot;East&quot; ,p2);

b_lyear = new RepeaterButton(KiwiUtils.getResourceManager()
.getIcon(&quot;repeater-left.gif&quot;));
b_lyear.setMargin(KiwiUtils.emptyInsets);
b_lyear.setFocusPainted(false);
b_lyear.setOpaque(false);
b_lyear.addActionListener(this);
p2.add(b_lyear);

l_year = new JLabel();
l_year.setForeground(Color.black);
l_year.setOpaque(false);
p2.add(l_year);

b_ryear = new RepeaterButton(KiwiUtils.getResourceManager()
.getIcon(&quot;repeater-right.gif&quot;));
b_ryear.setMargin(KiwiUtils.emptyInsets);
b_ryear.setFocusPainted(false);
b_ryear.setOpaque(false);
b_ryear.addActionListener(this);
p2.add(b_ryear);

add(&quot;North&quot;, top);

startDay = startDate.get(Calendar.DAY_OF_MONTH);
endDay = endDate.get(Calendar.DAY_OF_MONTH);

cal = new CalendarPane();
cal.setOpaque(false);
add(&quot;Center&quot;, cal);

l_date = new JLabel(&quot;Date&quot;, SwingConstants.CENTER);
l_date.setForeground(Color.black);
add(&quot;South&quot;, l_date);

_refresh();

Font f = getFont();
setFont(new Font(f.getName(), Font.BOLD, f.getSize()));
}

/** Get a copy of the <code>Calendar</code> object that represents the
* currently selected start date.
*
* @return The currently selected start date.
*/

public Calendar getStartDate()
{
return((Calendar)startDate.clone());
}

/** Get the day-of-month of the currently selected start date.
*
* @return The day-of-month.
*/

private int getStartDay()
{
return(startDay);
}

/** Get the month of the currently selected start date.
*
* @return The month.
*/
private int getMonth()
{
return(startDate.get(Calendar.MONTH));
}

/**
* Set the selected date for the chooser.
*
* @param date The date to select.
*/

public void setSelectedDate(Calendar date)
{
startDate = (Calendar)date.clone();
startDay = endDay = date.get(Calendar.DAY_OF_MONTH);

_refresh();
}

/**
* Set the selected date range for the chooser. The start and end date must
* fall within the same month and year, of course.
*
* @param start The start date for the selection.
* @param end The end date for the selection.
*/

public void setSelectedDateRange(Calendar start, Calendar end)
{
startDate = (Calendar)start.clone();
startDay = start.get(Calendar.DAY_OF_MONTH);
endDay = end.get(Calendar.DAY_OF_MONTH);

_refresh();
}

/**
* Set the format for the textual date display at the bottom of the
* component.
*
* @param <code>format</code> The new date format to use.
*/

public void setDateFormat(SimpleDateFormat format)
{
datefmt = format;

_refresh();
}

/** Handle events. This method is public as an implementation side-effect. */

public void actionPerformed(ActionEvent evt)
{
Object o = evt.getSource();

if(o == b_lmonth)
startDate.add(Calendar.MONTH, -1);
else if(o == b_rmonth)
startDate.add(Calendar.MONTH, 1);
else if(o == b_lyear)
startDate.add(Calendar.YEAR, -1);
else if(o == b_ryear)
startDate.add(Calendar.YEAR, 1);

_refresh();
}

private void _computeFirstDay()
{
int d = startDate.get(Calendar.DAY_OF_MONTH);
startDate.set(Calendar.DAY_OF_MONTH, 1);
firstDay = startDate.get(Calendar.DAY_OF_WEEK);
startDate.set(Calendar.DAY_OF_MONTH, d);
}

private void _refresh()
{
l_date.setText(datefmt.format(startDate.getTime()));
l_year.setText(String.valueOf(startDate.get(Calendar.YEAR)));
l_month.setText(months[startDate.get(Calendar.MONTH)]);

_computeFirstDay();

cal.repaint();
}

// inner class to draw the calendar itself

private class CalendarPane extends JComponent
{
private int ww = 0, hh = 0, dp = 0;
private Color highlightColor;

/** Construct a new <code>CalendarView</code>. */

CalendarPane()
{
highlightColor = new Color(200, 255, 200);
addMouseListener(new _MouseListener2());
}

/** Paint the component. */

public void paint(Graphics gc)
{
FontMetrics fm = gc.getFontMetrics();
Insets ins = getInsets();
int h = fm.getMaxAscent();
dp = ((firstDay + 6) % 7) - 1;
if(dp < 0) dp += 7;
int x = dp, y = 0;
int y0 = ((getSize().height - getPreferredSize().height) / 2);
int yp = y0;
int x0 = ((getSize().width - getPreferredSize().width) / 2);
int xp = x0;

paintBorder(gc);
gc.setColor(Color.black);
gc.clipRect(ins.left, ins.top, (getSize().width - ins.left - ins.right),
(getSize().height - ins.top - ins.bottom));
gc.translate(ins.left, ins.top);

for(int i = 0; i < 7; i++)
gc.drawString(labels, xp + 5 + i * (cellSize + 2), yp + h);

yp += 20;
xp += dp * (cellSize + 2);

for(int d = 1; d <= daysInMonth[DateChooser.this.getMonth()]; d++)
{
if((d >= startDay) && (d <= endDay))
paintSelected(gc, xp, yp);
else
paintUnselected(gc, xp, yp);

gc.setColor((x > 4) ? weekendColor : Color.black);
String ss = String.valueOf(d);
int sw = fm.stringWidth(ss);
gc.drawString(ss, xp - 3 + (cellSize - sw), yp + 3 + h);

if(++x == 7)
{
x = 0;
xp = x0;
y++;
yp += (cellSize + 2);
}
else
xp += (cellSize + 2);
}
}

private void paintSelected(Graphics gc, int xp, int yp)
{
gc.setColor(highlightColor);
gc.fill3DRect(xp, yp, cellSize, cellSize, true);
}

private void paintUnselected(Graphics gc, int xp, int yp)
{
gc.setColor(Color.lightGray);
gc.draw3DRect(xp, yp, cellSize, cellSize, true);
}

/* Get the preferred size of the component. */

public Dimension getPreferredSize()
{
Insets ins = getInsets();
return(new Dimension((((cellSize + 2) * 7) + ins.left + ins.right),
(((cellSize + 2) * 6) + 20) + ins.top
+ ins.bottom));
}

/* mouse listener */

private class _MouseListener2 extends MouseAdapter
{
public void mouseClicked(MouseEvent evt)
{
int d = getDay(evt);
if(d == -1)
return;

setDay(d, d);
_refresh();
}

public void mousePressed(MouseEvent evt)
{
setDay(getDay(evt), endDay);
}

public void mouseReleased(MouseEvent evt)
{
endDay = getDay(evt);

// In case the User dragged the mouse backwards

if(startDay > endDay)
setDay(endDay, startDay);
else
setDay(startDay, endDay);

_refresh();
}

private void setDay(int start, int end)
{
startDay = start;
startDate.set(Calendar.DAY_OF_MONTH, start);
endDay = end;
endDate.set(Calendar.DAY_OF_MONTH, end);
}

/**
* Figure out which object/day the event ocurred on.
*
* @param evt a value of type 'MouseEvent'
* @return 'int' The day that this event ocurred on. -1 if
* the event happened out of range.
*/

private int getDay(MouseEvent evt)
{
Insets ins = getInsets();
int x = evt.getX() - ins.left;
int y = evt.getY() - ins.top - 20;
int maxw = (cellSize + 2) * 7;
int maxh = (cellSize + 2) * 6;

// check if totally out of range.

if((x < 0) || (x > maxw) || (y < 0) || (y > maxh))
return -1;

y /= (cellSize + 2);
x /= (cellSize + 2);

int d = (7 * y) + x - (dp - 1);
if(d < 1 || d > startDate.getMaximum(Calendar.DAY_OF_MONTH))
return -1;
return d;
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top