⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 raplacalendarviewlistener.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/**
 *
 */
package org.rapla.plugin.abstractcalendar;

import java.awt.Component;
import java.awt.Point;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JComponent;
import javax.swing.JMenuItem;

import org.rapla.components.calendarview.Block;
import org.rapla.components.calendarview.swing.ViewListener;
import org.rapla.entities.User;
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.dynamictype.DynamicType;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.CalendarModel;
import org.rapla.gui.MenuContext;
import org.rapla.gui.ObjectMenuFactory;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.ReservationEdit;
import org.rapla.gui.internal.action.AppointmentAction;
import org.rapla.gui.internal.action.ReservationAction;
import org.rapla.gui.internal.common.RaplaClipboard;
import org.rapla.gui.toolkit.MenuInterface;
import org.rapla.gui.toolkit.RaplaMenuItem;
import org.rapla.gui.toolkit.RaplaPopupMenu;
import org.rapla.plugin.RaplaExtensionPoints;

public class RaplaCalendarViewListener extends RaplaGUIComponent
implements
ViewListener
{
    JComponent calendarContainerComponent;
    CalendarModel model;
    public RaplaCalendarViewListener(RaplaContext context, CalendarModel model, JComponent calendarContainerComponent)  throws RaplaException {
        super( context);
        this.model = model;
        this.calendarContainerComponent = calendarContainerComponent;
    }

    protected CalendarModel getModel() {
        return model;
    }

    public void selectionChanged(Date start,Date end) {
    }

    public void selectionPopup(Component component,Point p,Date start,Date end, int slotNr) {
        showPopupMenu(component,p,start,end, slotNr);
    }

    public void blockPopup(Block block,Point p) {
        SwingRaplaBlock b = (SwingRaplaBlock) block;
        if ( !b.getContext().isEventSelected() ) {
            return;
        }
        showPopupMenu(b.getView(),p,b.getAppointment(),b.getStart(), b.isException());
    }

    public void blockEdit(Block block,Point p) {
        SwingRaplaBlock b = (SwingRaplaBlock) block;
        if ( !b.getContext().isEventSelected() ) {
            return;
        }
        try {
            if (!canModify(b.getReservation()))
                return;
            getReservationController().edit(b.getAppointment());
        } catch (RaplaException ex) {
            showException(ex,b.getView());
        }
    }

    public void moved(Block block,Point p,Date newStart) {
        SwingRaplaBlock b = (SwingRaplaBlock) block;
        try {
            newStart = calcStartDate( newStart, b.getAppointment());
            getReservationController().moveAppointment(b.getAppointment()
                                                       ,b.getStart()
                                                       ,newStart
                                                       ,calendarContainerComponent
                                                       ,p);
        } catch (RaplaException ex) {
            showException(ex,b.getView());
        }
    }

    public void resized(Block block,Point p,Date newStart, Date newEnd) {
        SwingRaplaBlock b = (SwingRaplaBlock) block;
        try {
            getReservationController().resizeAppointment(b.getAppointment()
                    ,b.getStart()
                    ,newStart
                    ,newEnd
                    ,calendarContainerComponent
                    ,p);
        } catch (RaplaException ex) {
            showException(ex,b.getView());
        }
    }

    /** this method should be overriden by subclasses who want to change the selected allocatable that are passed to the edit. @see SwingCompactWeekCalendar */
    protected void showPopupMenu(Component component,Point p,Date start,Date end, int slotNr)
    {
        Allocatable[] selectedAllocatables;
        try {
            selectedAllocatables = getModel().getSelectedAllocatables();
        } catch (RaplaException ex) {
            showException(ex, calendarContainerComponent);
            return;
        }
        Allocatable selectedAllocatable = null;
        if ( selectedAllocatables.length == 1 ) {
            selectedAllocatable = selectedAllocatables[0];
        }
        showPopupMenu( component, p, start,end, slotNr, selectedAllocatables, selectedAllocatable);
    }

    /** this method should be overriden by subclasses who want to change the start or end dates or the repeatings @see SwingPeriodCalendar */
    protected void showPopupMenu(Component component,Point p,Date start,Date end, int slotNr, Allocatable[] selectedAllocatables, Allocatable selectedAllocatable) {
        int repeatings = 1;
        showPopupMenu( component, p, start,end, slotNr,  selectedAllocatables, selectedAllocatable, repeatings);
    }

    protected void showPopupMenu(Component component,Point p,Date start,Date end, int slotNr, Allocatable[] selectedAllocatables, Allocatable selectedAllocatable, int repeatings)
    {
        try {
            User user = getUser();
            Date today = getQuery().today();
            boolean canAllocate = false;
            for ( int i=0;i< selectedAllocatables.length; i++) {
                if (selectedAllocatables[i].canAllocate( user, start, end, today))
                    canAllocate = true;
            }
            RaplaPopupMenu menu= new RaplaPopupMenu();

            if ( canAllocate || (selectedAllocatables.length == 0 && canUserAllocateSomething( getUser()))  ) {
                ReservationAction action = new ReservationAction(getContext(), component,p);
                Collection list = getModel().getSelectedObjectsAndChildren();
                if ( list.size() > 0) {
                    action.changeObject ( list.iterator().next());
                }
                DynamicType guessedType = action.guessType();
                addAppointmentAction(menu,component,p).setNew(start,end, selectedAllocatable, null, guessedType, repeatings);
                addAppointmentAction(menu,component,p).setNew(start,end, selectedAllocatable, RepeatingType.WEEKLY, guessedType, repeatings);

                ReservationEdit[] editWindows = getReservationController().getEditWindows();
                for ( int i=0;i<editWindows.length;i++ ) {
                    ReservationEdit reservationEdit = editWindows[i];
                    addAppointmentAction(menu,component,p).setAddTo( reservationEdit, start,end, selectedAllocatable, null, repeatings);
                    addAppointmentAction(menu,component,p).setAddTo( reservationEdit, start,end, selectedAllocatable, RepeatingType.WEEKLY, repeatings);
                }
            } else {
               JMenuItem cantAllocate = new JMenuItem(getString("permission.denied"));
               cantAllocate.setEnabled( false);
               menu.add( cantAllocate);
            }

            Appointment appointment = ((RaplaClipboard) getService(RaplaClipboard.ROLE)).getAppointment();
            if ( appointment != null ) {
                Date pasteStart = calcStartDate( start, appointment );
                addAppointmentAction(menu,component,p).setPaste( pasteStart );
                addAppointmentAction(menu,component,p).setPasteAsNew( pasteStart );
            }
            menu.show(component,p.x,p.y);
        } catch (RaplaException ex) {
            showException(ex, calendarContainerComponent);
        }
    }

    private void showPopupMenu(Component component,Point p,Appointment appointment,Date start, boolean isException)
    {
        try {
            RaplaPopupMenu menu= new RaplaPopupMenu();
            Date copyStart = calcStartDate( start, appointment );
            addAppointmentAction( menu, component, p).setCopy(appointment, copyStart);
            addAppointmentAction( menu, component, p ).setEdit(appointment);
            if ( !isException) {
                addAppointmentAction( menu, component, p).setDelete(appointment,start);
            }
            addAppointmentAction( menu, component, p).setView(appointment);

            Iterator it = getContainer().lookupServicesFor( RaplaExtensionPoints.OBJECT_MENU_EXTENSION).values().iterator();
            while (it.hasNext())
            {
                ObjectMenuFactory objectMenuFact = (ObjectMenuFactory) it.next();
                MenuContext menuContext = new MenuContext( getContext(), appointment);
                menuContext.put("selected_date", start);
                
                RaplaMenuItem[] items = objectMenuFact.create( menuContext, appointment );
                for ( int i =0;i<items.length;i++)
                {
                    RaplaMenuItem item =  items[i];
                    menu.add( item);
                }
            }

            menu.show(component,p.x,p.y);
        } catch (RaplaException ex) {
            showException(ex, calendarContainerComponent);
        }
    }

    public AppointmentAction addAppointmentAction(MenuInterface menu, Component parent, Point p) throws RaplaException {
        AppointmentAction action = new AppointmentAction(getContext(),parent,p);
        menu.add(new JMenuItem(action));
        return action;
    }

    private boolean canModify(Reservation reservation) throws RaplaException {
        User user =  getUser();
        return (user != null && (reservation.getOwner().equals(user) || user.isAdmin()));
    }

     /**
      * If the view is a week-view this method will just return the passed date.
      * returns the passed date as start-date. Override this method if you just want another date as start date of an appointment.
      * For Examples of override behaviour see month-view or compact-view
      */
    protected Date calcStartDate(Date date, Appointment appointment) {
        return date;
    }

}

⌨️ 快捷键说明

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