appointmentaction.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 259 行

JAVA
259
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas, Bettina Lademann                |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.gui.internal.action;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.Date;

import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.RepeatingType;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.domain.ReservationHelper;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaAction;
import org.rapla.gui.ReservationEdit;
import org.rapla.gui.internal.common.RaplaClipboard;

public class AppointmentAction extends RaplaAction {
    public final static int DELETE = 1;
    public final static int COPY = 2;
    public final static int PASTE = 3;
    public final static int CUT = 4;
    public final static int NEW = 5;
    public final static int EDIT = 6;
    public final static int VIEW = 7;
    public final static int CHANGE_ALLOCATABLE = 8;
    public final static int ADD_TO_RESERVATION = 9;
    public final static int PASTE_AS_NEW = 10;
    
    Component parent;
    Point point;
    RepeatingType repeatingType = null;
    int type;
    int repeatings = 1;
    Date start;
    Date end;
    Allocatable allocatable;
    Appointment appointment;
    ReservationEdit reservationEdit;
    DynamicType dynamicType;

    public AppointmentAction(RaplaContext sm,Component parent,Point point) throws RaplaException {
        super( sm);
        this.parent = parent;
        this.point = point;
    }

    public AppointmentAction setNew(Date start,Date end,Allocatable allocatable, RepeatingType repeatingType,DynamicType dynamicType, int repeatings ) {
        this.start = start;
        this.end = end;
        this.repeatings = repeatings;
        this.dynamicType = dynamicType;
        this.allocatable = allocatable;
        this.repeatingType = repeatingType;
        this.type = NEW;
        putValue(NAME, getString("new_reservation") + getRepeatingString( repeatingType ) );
        if ( repeatingType != null)
        {
            putValue(SMALL_ICON, getIcon("icon.new_repeating"));
        }
        else
        {
            putValue(SMALL_ICON, getIcon("icon.new"));
        }
        setEnabled( canAllocate( start, end, allocatable));
        return this;
    }

    private String getRepeatingString(RepeatingType repeatingType) {
    	if ( repeatingType == null)
            return "";
        return " (" + getString( repeatingType.toString() ) + ")";
    }

    public AppointmentAction setAddTo(ReservationEdit reservationEdit,Date start,Date end,Allocatable allocatable, RepeatingType repeatingType, int repeatings) {
    	this.reservationEdit = reservationEdit;
        this.repeatings = repeatings;
        this.start = start;
        this.end = end;
        this.allocatable = allocatable;
        this.repeatingType = repeatingType;
        this.type = ADD_TO_RESERVATION;
        putValue(NAME, getString("new_appointment") +
                    " " + getString("for") + " '" + getName(reservationEdit.getReservation()) + "'"
                    + getRepeatingString( repeatingType ));
        putValue(SMALL_ICON, getIcon("icon.new"));
        setEnabled( canAllocate( start, end, allocatable));
        return this;
    }

    private boolean canAllocate(Date start, Date end, Allocatable allocatables) {
        if ( allocatable == null) {
            return true;
        }
        try {
            return allocatable.canAllocate( getUser(), start, end, getQuery().today() );
        } catch (RaplaException ex) {
            return false;
        }
    }

    public AppointmentAction setCopy(Appointment appointment,Date from) {
        this.appointment = appointment;
        this.start = from;
        this.type = COPY;
        putValue(NAME, getString("copy"));
        putValue(SMALL_ICON, getIcon("icon.copy"));
        setEnabled(canModify(appointment.getReservation()));
        return this;
    }

    public AppointmentAction setPaste(Date start) {
        this.start = start;
        this.type = PASTE;
        putValue(NAME, getString("paste"));
        putValue(SMALL_ICON, getIcon("icon.paste"));
        setEnabled(isAppointmentOnClipboard());
        return this;
    }

    public AppointmentAction setPasteAsNew(Date start) {
        this.start = start;
        this.type = PASTE_AS_NEW;
        putValue(NAME, getString("paste_as") + " " + getString( "new_reservation" ) );
        putValue(SMALL_ICON, getIcon("icon.paste_new"));
        setEnabled(isAppointmentOnClipboard());
        return this;
    }

    public AppointmentAction setDelete(Appointment appointment,Date from) {
        this.appointment = appointment;
        this.start = from;
        this.type = DELETE;
        putValue(NAME, getString("delete"));
        putValue(SMALL_ICON, getIcon("icon.delete"));
        setEnabled(canModify(appointment.getReservation()));
        return this;
    }

    public AppointmentAction setView(Appointment appointment) {
        this.appointment = appointment;
        this.type = VIEW;
        putValue(NAME, getString("view"));
        putValue(SMALL_ICON, getIcon("icon.help"));
        return this;
    }

    public AppointmentAction setEdit(Appointment appointment) {
        this.appointment = appointment;
        this.type = EDIT;
        putValue(NAME, getString("edit"));
        putValue(SMALL_ICON, getIcon("icon.edit"));
        setEnabled(canModify(appointment.getReservation()) || getQuery().canExchangeAllocatables(appointment.getReservation()));
        return this;
    }


    public void actionPerformed(ActionEvent evt) {
        try {
            switch (type) {
            case DELETE: delete();break;
            case COPY: copy();break;
            case PASTE: paste(false);break;
            case PASTE_AS_NEW: paste( true);break;
            case NEW: newReservation();break;
            case ADD_TO_RESERVATION: addToReservation();break;
            case EDIT: edit();break;
            case VIEW: view();break;
            }
        } catch (RaplaException ex) {
            showException(ex,parent);
        } // end of try-catch
    }

    public void view() throws RaplaException {
        getInfoFactory().showInfoDialog(appointment.getReservation(),parent,point);
    }

    public void edit() throws RaplaException {
        getReservationController().edit( appointment );
    }

    private void delete() throws RaplaException {
        getReservationController().deleteAppointment(appointment,start,parent,point);
    }

    private void copy() throws RaplaException {
        Reservation event = appointment.getReservation();
        Appointment copy = getReservationController().copyAppointment(appointment,start,parent,point);
        // store on clibboard
        getClipboard().setAppointment(copy,event, event.getRestrictedAllocatables(appointment));
    }

    private RaplaClipboard getClipboard() {
        return (RaplaClipboard) getService(RaplaClipboard.ROLE);
    }

    private void paste(boolean asNewReservation) throws RaplaException {
        if (!isAppointmentOnClipboard())
            return;
        Appointment appointment = getClipboard().getAppointment();
        Reservation reservation = getClipboard().getReservation();
        getReservationController().pasteAppointment(
                                              appointment
                                               ,reservation 
                                               ,start
                                               ,parent
                                               ,point
                                               ,getClipboard().getRestrictedAllocatables()
                                               ,asNewReservation);
    }

    private void newReservation() throws RaplaException {
        create(start,end,allocatable, repeatingType, dynamicType, repeatings);
    }

    /* (non-Javadoc)
     * @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)
     */
    public void create(Date start,Date end,Allocatable allocatable, RepeatingType repeatingType, DynamicType type, int repeatings) throws RaplaException {
        Reservation mutableReservation = getModification().newReservation();
        if ( type != null) {
            mutableReservation.setClassification( type.newClassification());
        }
        Appointment appointment = getModification().newAppointment( start, end );
        if ( repeatingType != null ) {
            ReservationHelper.makeRepeatingForPeriod(getPeriodModel(), appointment, repeatingType, repeatings );
        }
        mutableReservation.addAppointment(appointment);
        if (allocatable != null)
            mutableReservation.addAllocatable(allocatable);

        getReservationController().edit( appointment );
    }

    private void addToReservation() throws RaplaException {
    	reservationEdit.addAppointment(start,end, repeatingType, repeatings);
    }

    public boolean isAppointmentOnClipboard() {
        return (getClipboard().getAppointment() != null);
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?