001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.components.calendarview.swing;
014:
015: import java.util.Date;
016: import java.awt.Point;
017: import java.awt.Component;
018: import javax.swing.SwingUtilities;
019: import java.awt.event.*;
020:
021: /** SelectionHandler handles the selection events and the Slot
022: * Context Menu (right click).
023: * This is internally used by the weekview to communicate with its slots.
024: */
025: class SelectionHandler extends MouseAdapter implements
026: MouseMotionListener {
027: Date start;
028: Date end;
029: boolean bPopupClicked = false;
030: boolean bSelecting = false;
031: public int selectionStart = -1;
032: public int selectionEnd = -1;
033: private int oldIndex = -1;
034: private int oldSlotNr = -1;
035: private int startSlot = -1;
036: private int endSlot = -1;
037: private int draggingSlot = -1;
038: private int draggingIndex = -1;
039: AbstractSwingCalendar m_wv;
040: boolean periodSelection;
041:
042: public SelectionHandler(AbstractSwingCalendar wv) {
043: m_wv = wv;
044: }
045:
046: public void mouseClicked(MouseEvent evt) {
047: if (evt.isPopupTrigger()) {
048: bPopupClicked = true;
049: slotPopup(evt);
050: } else {
051: /* We don't check click here
052: if (SwingUtilities.isLeftMouseButton(evt))
053: move(evt);
054: */
055: }
056: }
057:
058: public void mousePressed(MouseEvent evt) {
059: if (evt.isPopupTrigger()) {
060: bPopupClicked = true;
061: slotPopup(evt);
062: } else {
063: if (SwingUtilities.isLeftMouseButton(evt))
064: move(evt);
065: }
066: }
067:
068: public void mouseReleased(MouseEvent evt) {
069: if (evt.isPopupTrigger()) {
070: bPopupClicked = true;
071: slotPopup(evt);
072: }
073: if (SwingUtilities.isLeftMouseButton(evt) && !bPopupClicked)
074: move(evt);
075: bPopupClicked = false;
076: bSelecting = false;
077: }
078:
079: public void mouseDragged(MouseEvent evt) {
080: if (SwingUtilities.isLeftMouseButton(evt) && !bPopupClicked)
081: move(evt);
082: }
083:
084: public void mouseMoved(MouseEvent evt) {
085: }
086:
087: public void setPeriodSelection(boolean period) {
088: periodSelection = period;
089: }
090:
091: private void clearSelection() {
092: for (int i = 0; i < m_wv.getDayCount(); i++)
093: if (m_wv.getDay(i) != null)
094: m_wv.getDay(i).unselectAll();
095: }
096:
097: public void slotPopup(MouseEvent evt) {
098: Point p = new Point(evt.getX(), evt.getY());
099: DaySlot slot = (DaySlot) evt.getSource();
100: if (start == null || end == null) {
101: int index = slot.calcRow(evt.getY());
102: start = m_wv.createDate(slot, index, true);
103: end = m_wv.createDate(slot, index, false);
104: clearSelection();
105: slot.select(index, index);
106: }
107: m_wv.fireSelectionPopup((Component) slot, p, start, end, m_wv
108: .getSlotNr(slot));
109: }
110:
111: public void move(MouseEvent evt) {
112: if (!m_wv.isEditable())
113: return;
114: DaySlot source = (DaySlot) evt.getSource();
115: int slotNr = m_wv.calcSlotNr(source.getLocation().x
116: + evt.getX(), source.getLocation().y + evt.getY());
117: if (slotNr == -1)
118: return;
119:
120: int selectedIndex = source.calcRow(evt.getY());
121:
122: if (!bSelecting) {
123: clearSelection();
124: bSelecting = true;
125: selectionStart = selectedIndex;
126: selectionEnd = selectedIndex;
127: draggingSlot = slotNr;
128: draggingIndex = selectedIndex;
129: startSlot = slotNr;
130: endSlot = slotNr;
131: } else {
132: if (slotNr == draggingSlot) {
133: startSlot = endSlot = slotNr;
134: if (selectedIndex > draggingIndex) {
135: selectionStart = draggingIndex;
136: selectionEnd = selectedIndex;
137: } else if (selectedIndex < draggingIndex) {
138: selectionStart = selectedIndex;
139: selectionEnd = draggingIndex;
140: }
141: } else if (slotNr > draggingSlot) {
142: startSlot = draggingSlot;
143: selectionStart = draggingIndex;
144: endSlot = slotNr;
145: selectionEnd = selectedIndex;
146: } else if (slotNr < draggingSlot) {
147: startSlot = slotNr;
148: selectionStart = selectedIndex;
149: endSlot = draggingSlot;
150: selectionEnd = draggingIndex;
151: }
152: if (selectedIndex == oldIndex && slotNr == oldSlotNr)
153: return;
154: int rowsPerDay = m_wv.getRowsPerDay();
155: if (selectedIndex >= rowsPerDay - 1)
156: selectedIndex = rowsPerDay - 1;
157: }
158: oldSlotNr = slotNr;
159: oldIndex = selectedIndex;
160: setSelection();
161: m_wv.scrollTo(m_wv.getDay(slotNr).getLocation().x + evt.getX(),
162: m_wv.getDay(slotNr).getLocation().y + evt.getY());
163: }
164:
165: private void setSelection() {
166:
167: int startRow = selectionStart;
168: int endRow = m_wv.getRowsPerDay() - 1;
169:
170: for (int i = 0; i < startSlot; i++) {
171: if (m_wv.getDay(i) != null) {
172: m_wv.getDay(i).unselectAll();
173: }
174: }
175:
176: if (periodSelection) {
177: if (selectionStart > selectionEnd) {
178: startRow = selectionEnd;
179: endRow = selectionStart;
180: } else {
181: startRow = selectionStart;
182: endRow = selectionEnd;
183: }
184: int startSlotDay = startSlot % 7;
185: int endSlotDay = endSlot % 7;
186: int startDayOfWeek = Math.min(startSlotDay, endSlotDay);
187: int endDayOfWeek = Math.max(startSlotDay, endSlotDay);
188: int maxRow = m_wv.getRowsPerDay();
189: int realEndSlot = (endSlot / 7) * 7 + endDayOfWeek + 1;
190: // System.out.println("selectionStart=" + selectionStart + " selectionEnd=" + selectionEnd + " startslot=" + startSlot + " endSlot=" + endSlot + " maxrow=" + maxRow) ;
191: for (int i = (startSlot / 7) * 7; i <= realEndSlot; i++) {
192: if (m_wv.getDay(i) == null)
193: continue;
194: int dayOfWeek = i % 7;
195: if (dayOfWeek < startDayOfWeek
196: || dayOfWeek > endDayOfWeek)
197: m_wv.getDay(i).unselectAll();
198: else if (dayOfWeek > startDayOfWeek
199: && dayOfWeek < endDayOfWeek)
200: m_wv.getDay(i).select(0, 10000); // XXX Excoffier
201: else if (dayOfWeek == startDayOfWeek
202: && dayOfWeek == endDayOfWeek)
203: m_wv.getDay(i).select(startRow, endRow);
204: else if (dayOfWeek == startDayOfWeek
205: && dayOfWeek < endDayOfWeek)
206: m_wv.getDay(i).select(
207: (startRow + 7 * maxRow) % maxRow, 10000); // XXX
208: else if (dayOfWeek > startDayOfWeek
209: && dayOfWeek == endDayOfWeek)
210: m_wv.getDay(i).select(0, endRow % maxRow);
211: else
212: System.out.println("Should raise an error"); // XXX
213: }
214: for (int i = realEndSlot; i < m_wv.getDayCount(); i++) {
215: if (m_wv.getDay(i) != null)
216: m_wv.getDay(i).unselectAll();
217: }
218:
219: } else {
220: for (int i = startSlot; i <= endSlot; i++) {
221: if (i > startSlot)
222: startRow = 0;
223: if (i == endSlot)
224: endRow = selectionEnd;
225: if (m_wv.getDay(i) != null)
226: m_wv.getDay(i).select(startRow, endRow);
227: }
228: startRow = selectionStart;
229: endRow = selectionEnd;
230: for (int i = endSlot + 1; i < m_wv.getDayCount(); i++) {
231: if (m_wv.getDay(i) != null)
232: m_wv.getDay(i).unselectAll();
233: }
234: }
235:
236: start = m_wv.createDate(m_wv.getDay(startSlot), startRow, true);
237: end = m_wv.createDate(m_wv.getDay(endSlot), endRow, false);
238: m_wv.fireSelectionChanged(start, end);
239: }
240:
241: }
|