001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas, Bettina Lademann |
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.gui.internal.action;
014:
015: import java.awt.Component;
016: import java.awt.Point;
017: import java.awt.event.ActionEvent;
018: import java.util.Date;
019:
020: import org.rapla.entities.domain.Allocatable;
021: import org.rapla.entities.domain.Appointment;
022: import org.rapla.entities.domain.RepeatingType;
023: import org.rapla.entities.domain.Reservation;
024: import org.rapla.entities.domain.ReservationHelper;
025: import org.rapla.entities.dynamictype.DynamicType;
026: import org.rapla.framework.RaplaContext;
027: import org.rapla.framework.RaplaException;
028: import org.rapla.gui.RaplaAction;
029: import org.rapla.gui.ReservationEdit;
030: import org.rapla.gui.internal.common.RaplaClipboard;
031:
032: public class AppointmentAction extends RaplaAction {
033: public final static int DELETE = 1;
034: public final static int COPY = 2;
035: public final static int PASTE = 3;
036: public final static int CUT = 4;
037: public final static int NEW = 5;
038: public final static int EDIT = 6;
039: public final static int VIEW = 7;
040: public final static int CHANGE_ALLOCATABLE = 8;
041: public final static int ADD_TO_RESERVATION = 9;
042: public final static int PASTE_AS_NEW = 10;
043:
044: Component parent;
045: Point point;
046: RepeatingType repeatingType = null;
047: int type;
048: int repeatings = 1;
049: Date start;
050: Date end;
051: Allocatable allocatable;
052: Appointment appointment;
053: ReservationEdit reservationEdit;
054: DynamicType dynamicType;
055:
056: public AppointmentAction(RaplaContext sm, Component parent,
057: Point point) throws RaplaException {
058: super (sm);
059: this .parent = parent;
060: this .point = point;
061: }
062:
063: public AppointmentAction setNew(Date start, Date end,
064: Allocatable allocatable, RepeatingType repeatingType,
065: DynamicType dynamicType, int repeatings) {
066: this .start = start;
067: this .end = end;
068: this .repeatings = repeatings;
069: this .dynamicType = dynamicType;
070: this .allocatable = allocatable;
071: this .repeatingType = repeatingType;
072: this .type = NEW;
073: putValue(NAME, getString("new_reservation")
074: + getRepeatingString(repeatingType));
075: if (repeatingType != null) {
076: putValue(SMALL_ICON, getIcon("icon.new_repeating"));
077: } else {
078: putValue(SMALL_ICON, getIcon("icon.new"));
079: }
080: setEnabled(canAllocate(start, end, allocatable));
081: return this ;
082: }
083:
084: private String getRepeatingString(RepeatingType repeatingType) {
085: if (repeatingType == null)
086: return "";
087: return " (" + getString(repeatingType.toString()) + ")";
088: }
089:
090: public AppointmentAction setAddTo(ReservationEdit reservationEdit,
091: Date start, Date end, Allocatable allocatable,
092: RepeatingType repeatingType, int repeatings) {
093: this .reservationEdit = reservationEdit;
094: this .repeatings = repeatings;
095: this .start = start;
096: this .end = end;
097: this .allocatable = allocatable;
098: this .repeatingType = repeatingType;
099: this .type = ADD_TO_RESERVATION;
100: putValue(NAME, getString("new_appointment") + " "
101: + getString("for") + " '"
102: + getName(reservationEdit.getReservation()) + "'"
103: + getRepeatingString(repeatingType));
104: putValue(SMALL_ICON, getIcon("icon.new"));
105: setEnabled(canAllocate(start, end, allocatable));
106: return this ;
107: }
108:
109: private boolean canAllocate(Date start, Date end,
110: Allocatable allocatables) {
111: if (allocatable == null) {
112: return true;
113: }
114: try {
115: return allocatable.canAllocate(getUser(), start, end,
116: getQuery().today());
117: } catch (RaplaException ex) {
118: return false;
119: }
120: }
121:
122: public AppointmentAction setCopy(Appointment appointment, Date from) {
123: this .appointment = appointment;
124: this .start = from;
125: this .type = COPY;
126: putValue(NAME, getString("copy"));
127: putValue(SMALL_ICON, getIcon("icon.copy"));
128: setEnabled(canModify(appointment.getReservation()));
129: return this ;
130: }
131:
132: public AppointmentAction setPaste(Date start) {
133: this .start = start;
134: this .type = PASTE;
135: putValue(NAME, getString("paste"));
136: putValue(SMALL_ICON, getIcon("icon.paste"));
137: setEnabled(isAppointmentOnClipboard());
138: return this ;
139: }
140:
141: public AppointmentAction setPasteAsNew(Date start) {
142: this .start = start;
143: this .type = PASTE_AS_NEW;
144: putValue(NAME, getString("paste_as") + " "
145: + getString("new_reservation"));
146: putValue(SMALL_ICON, getIcon("icon.paste_new"));
147: setEnabled(isAppointmentOnClipboard());
148: return this ;
149: }
150:
151: public AppointmentAction setDelete(Appointment appointment,
152: Date from) {
153: this .appointment = appointment;
154: this .start = from;
155: this .type = DELETE;
156: putValue(NAME, getString("delete"));
157: putValue(SMALL_ICON, getIcon("icon.delete"));
158: setEnabled(canModify(appointment.getReservation()));
159: return this ;
160: }
161:
162: public AppointmentAction setView(Appointment appointment) {
163: this .appointment = appointment;
164: this .type = VIEW;
165: putValue(NAME, getString("view"));
166: putValue(SMALL_ICON, getIcon("icon.help"));
167: return this ;
168: }
169:
170: public AppointmentAction setEdit(Appointment appointment) {
171: this .appointment = appointment;
172: this .type = EDIT;
173: putValue(NAME, getString("edit"));
174: putValue(SMALL_ICON, getIcon("icon.edit"));
175: setEnabled(canModify(appointment.getReservation())
176: || getQuery().canExchangeAllocatables(
177: appointment.getReservation()));
178: return this ;
179: }
180:
181: public void actionPerformed(ActionEvent evt) {
182: try {
183: switch (type) {
184: case DELETE:
185: delete();
186: break;
187: case COPY:
188: copy();
189: break;
190: case PASTE:
191: paste(false);
192: break;
193: case PASTE_AS_NEW:
194: paste(true);
195: break;
196: case NEW:
197: newReservation();
198: break;
199: case ADD_TO_RESERVATION:
200: addToReservation();
201: break;
202: case EDIT:
203: edit();
204: break;
205: case VIEW:
206: view();
207: break;
208: }
209: } catch (RaplaException ex) {
210: showException(ex, parent);
211: } // end of try-catch
212: }
213:
214: public void view() throws RaplaException {
215: getInfoFactory().showInfoDialog(appointment.getReservation(),
216: parent, point);
217: }
218:
219: public void edit() throws RaplaException {
220: getReservationController().edit(appointment);
221: }
222:
223: private void delete() throws RaplaException {
224: getReservationController().deleteAppointment(appointment,
225: start, parent, point);
226: }
227:
228: private void copy() throws RaplaException {
229: Reservation event = appointment.getReservation();
230: Appointment copy = getReservationController().copyAppointment(
231: appointment, start, parent, point);
232: // store on clibboard
233: getClipboard().setAppointment(copy, event,
234: event.getRestrictedAllocatables(appointment));
235: }
236:
237: private RaplaClipboard getClipboard() {
238: return (RaplaClipboard) getService(RaplaClipboard.ROLE);
239: }
240:
241: private void paste(boolean asNewReservation) throws RaplaException {
242: if (!isAppointmentOnClipboard())
243: return;
244: Appointment appointment = getClipboard().getAppointment();
245: Reservation reservation = getClipboard().getReservation();
246: getReservationController().pasteAppointment(appointment,
247: reservation, start, parent, point,
248: getClipboard().getRestrictedAllocatables(),
249: asNewReservation);
250: }
251:
252: private void newReservation() throws RaplaException {
253: create(start, end, allocatable, repeatingType, dynamicType,
254: repeatings);
255: }
256:
257: /* (non-Javadoc)
258: * @see org.rapla.gui.edit.reservation.IReservationController#startNew(java.util.Date, java.util.Date, org.rapla.entities.domain.Allocatable, java.lang.String, org.rapla.entities.dynamictype.DynamicType, int)
259: */
260: public void create(Date start, Date end, Allocatable allocatable,
261: RepeatingType repeatingType, DynamicType type,
262: int repeatings) throws RaplaException {
263: Reservation mutableReservation = getModification()
264: .newReservation();
265: if (type != null) {
266: mutableReservation.setClassification(type
267: .newClassification());
268: }
269: Appointment appointment = getModification().newAppointment(
270: start, end);
271: if (repeatingType != null) {
272: ReservationHelper.makeRepeatingForPeriod(getPeriodModel(),
273: appointment, repeatingType, repeatings);
274: }
275: mutableReservation.addAppointment(appointment);
276: if (allocatable != null)
277: mutableReservation.addAllocatable(allocatable);
278:
279: getReservationController().edit(appointment);
280: }
281:
282: private void addToReservation() throws RaplaException {
283: reservationEdit.addAppointment(start, end, repeatingType,
284: repeatings);
285: }
286:
287: public boolean isAppointmentOnClipboard() {
288: return (getClipboard().getAppointment() != null);
289: }
290:
291: }
|