001: /**
002: * JNCalendar.java Created on 13.02.2003, 21:26:38 Alex Package:
003: * net.sf.memoranda.ui
004: *
005: * @author Alex V. Alishevskikh, alex@openmechanics.net Copyright (c) 2003
006: * Memoranda Team. http://memoranda.sf.net
007: */package net.sf.memoranda.ui;
008:
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.util.Calendar;
012: import java.util.Vector;
013:
014: import javax.swing.JTable;
015: import javax.swing.ListSelectionModel;
016: import javax.swing.event.ListSelectionEvent;
017: import javax.swing.event.ListSelectionListener;
018: import javax.swing.table.AbstractTableModel;
019: import javax.swing.table.TableCellRenderer;
020:
021: import net.sf.memoranda.date.CalendarDate;
022: import net.sf.memoranda.date.CurrentDate;
023: import net.sf.memoranda.util.Local;
024: import net.sf.memoranda.util.Configuration;
025:
026: /**
027: *
028: */
029: /*$Id: JNCalendar.java,v 1.8 2004/11/05 07:38:10 pbielen Exp $*/
030: public class JNCalendar extends JTable {
031:
032: private CalendarDate _date = null;
033: private boolean ignoreChange = false;
034: private Vector selectionListeners = new Vector();
035: CalendarDate startPeriod = null;
036: CalendarDate endPeriod = null;
037: public JNCalendarCellRenderer renderer = new JNCalendarCellRenderer();
038:
039: public JNCalendar() {
040: this (CurrentDate.get());
041: }
042:
043: /**
044: * Constructor for JNCalendar.
045: */
046: public JNCalendar(CalendarDate date) {
047: super ();
048: /* table properties */
049: setCellSelectionEnabled(true);
050: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
051: getTableHeader().setReorderingAllowed(false);
052: getTableHeader().setResizingAllowed(false);
053: set(date);
054:
055: /* selection listeners */
056: final ListSelectionModel rowSM = getSelectionModel();
057: final ListSelectionModel colSM = getColumnModel()
058: .getSelectionModel();
059: ListSelectionListener lsl = new ListSelectionListener() {
060: public void valueChanged(ListSelectionEvent e) {
061: //Ignore extra messages.
062: if (e.getValueIsAdjusting())
063: return;
064: if (ignoreChange)
065: return;
066: int row = getSelRow();
067: int col = getSelCol();
068: Object val = getModel().getValueAt(row, col);
069: if (val != null) {
070: if (val.toString().equals(
071: new Integer(_date.getDay()).toString()))
072: return;
073: _date = new CalendarDate(
074: new Integer(val.toString()).intValue(),
075: _date.getMonth(), _date.getYear());
076: notifyListeners();
077: } else {
078: //getSelectionModel().clearSelection();
079: doSelection();
080: }
081: }
082:
083: };
084: rowSM.addListSelectionListener(lsl);
085: colSM.addListSelectionListener(lsl);
086: }
087:
088: int getSelRow() {
089: return this .getSelectedRow();
090: }
091:
092: int getSelCol() {
093: return this .getSelectedColumn();
094: }
095:
096: public JNCalendar(CalendarDate date, CalendarDate sd,
097: CalendarDate ed) {
098: this (date);
099: setSelectablePeriod(sd, ed);
100: }
101:
102: public void set(CalendarDate date) {
103: _date = date;
104: setCalendarParameters();
105: ignoreChange = true;
106: this .setModel(new JNCalendarModel());
107: ignoreChange = false;
108: doSelection();
109: }
110:
111: public CalendarDate get() {
112: return _date;
113: }
114:
115: public void addSelectionListener(ActionListener al) {
116: selectionListeners.add(al);
117: }
118:
119: public void setSelectablePeriod(CalendarDate sd, CalendarDate ed) {
120: startPeriod = sd;
121: endPeriod = ed;
122: }
123:
124: private void notifyListeners() {
125: for (int i = 0; i < selectionListeners.size(); i++) {
126: ((ActionListener) selectionListeners.get(i))
127: .actionPerformed(new ActionEvent(this , 0,
128: "Calendar event"));
129: }
130: }
131:
132: public TableCellRenderer getCellRenderer(int row, int column) {
133: Object d = this .getModel().getValueAt(row, column);
134: /*
135: * if (d != null) return new JNCalendarCellRenderer( new
136: * CalendarDate(new Integer(d.toString()).intValue(), _date.getMonth(),
137: * _date.getYear()));
138: */
139: if (d != null)
140: renderer.setDate(new CalendarDate(new Integer(d.toString())
141: .intValue(), _date.getMonth(), _date.getYear()));
142: else
143: renderer.setDate(null);
144: return renderer;
145: }
146:
147: void doSelection() {
148: ignoreChange = true;
149: int selRow = getRow(_date.getDay());
150: int selCol = getCol(_date.getDay());
151: this .setRowSelectionInterval(selRow, selRow);
152: this .setColumnSelectionInterval(selCol, selCol);
153: ignoreChange = false;
154: }
155:
156: int getRow(int day) {
157: return ((day - 1) + firstDay) / 7;
158: }
159:
160: int getCol(int day) {
161: return ((day - 1) + firstDay) % 7;
162: }
163:
164: int firstDay;
165: int daysInMonth;
166:
167: void setCalendarParameters() {
168: int d = 1;
169:
170: Calendar cal = _date.getCalendar();
171:
172: if (Configuration.get("FIRST_DAY_OF_WEEK").equals("mon")) {
173: cal.setFirstDayOfWeek(Calendar.MONDAY);
174: d = 2;
175: } else
176: cal.setFirstDayOfWeek(Calendar.SUNDAY);
177:
178: cal.set(Calendar.DAY_OF_MONTH, 1);
179: cal.getTime();
180: firstDay = cal.get(Calendar.DAY_OF_WEEK) - d;
181: if (firstDay == -1)
182: firstDay = 6;
183: daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
184: }
185:
186: /*$Id: JNCalendar.java,v 1.8 2004/11/05 07:38:10 pbielen Exp $*/
187: public class JNCalendarModel extends AbstractTableModel {
188:
189: private String[] dayNames = Local.getWeekdayNames();
190:
191: public JNCalendarModel() {
192: super ();
193: }
194:
195: public int getColumnCount() {
196: return 7;
197: }
198:
199: public Object getValueAt(int row, int col) {
200: //int pos = (row * 7 + col) - firstDay + 1;
201: int pos = (row * 7 + (col + 1)) - firstDay;
202: if ((pos > 0) && (pos <= daysInMonth))
203: return new Integer(pos);
204: else
205: return null;
206:
207: }
208:
209: public int getRowCount() {
210: return 6;
211: }
212:
213: public String getColumnName(int col) {
214: return dayNames[col];
215: }
216:
217: }
218:
219: }
|