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

📄 reservationcontrollerimpl.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | 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.edit.reservation;

import java.awt.Component;
import java.awt.Container;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.TableColumn;

import org.rapla.components.layout.TableLayout;
import org.rapla.components.util.Command;
import org.rapla.entities.EntityNotFoundException;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Reservation;
import org.rapla.facade.Conflict;
import org.rapla.facade.ModificationEvent;
import org.rapla.facade.ModificationListener;
import org.rapla.facade.ModificationModule;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.ReservationController;
import org.rapla.gui.ReservationEdit;
import org.rapla.gui.internal.view.ConflictInfoOldUI;
import org.rapla.gui.toolkit.DialogUI;

public class ReservationControllerImpl extends RaplaGUIComponent
    implements
    ModificationListener, ReservationController
{
    /** We store all open ReservationEditWindows with their reservationId
     * in a map, to lookup if the reservation is already beeing edited.
     That prevents editing the same Reservation in different windows
     */
    Collection editWindowList = new ArrayList();

    public ReservationControllerImpl(RaplaContext sm) throws RaplaException {
        super(sm);
        getUpdateModule().addModificationListener(this);
    }

    void addReservationEdit(ReservationEdit editWindow) {
        editWindowList.add(editWindow);
    }

    void removeReservationEdit(ReservationEdit editWindow) {
        editWindowList.remove(editWindow);
    }

    public void edit(Reservation reservation) throws RaplaException {
        startEdit(reservation,null);
    }

    public void edit(Appointment appointment)throws RaplaException {
        startEdit(appointment.getReservation(), appointment);
    }

    public ReservationEdit[] getEditWindows() {
        return (ReservationEdit[]) editWindowList.toArray( new ReservationEdit[] {});
    }

    private ReservationEditImpl newEditWindow() throws RaplaException {
        ReservationEditImpl c = new ReservationEditImpl(getContext());
        return c;
    }

    private ReservationEdit startEdit(Reservation reservation,Appointment appointment)
        throws RaplaException {
        // Lookup if the reservation is already beeing edited
        ReservationEditImpl c = null;
        Iterator it = editWindowList.iterator();
        while (it.hasNext()) {
            c = (ReservationEditImpl)it.next();
            if (c.getReservation().isIdentical(reservation))
                break;
            else
                c = null;
        }

        if (c != null) {
            c.frame.requestFocus();
            c.frame.toFront();
        } else {
            c = newEditWindow();
            ModificationModule mod = getModification();
            boolean bNew = false;
            if ( reservation.isPersistant()) {
                reservation = (Reservation) mod.edit(reservation);
            } else {
                try {
                    getModification().getPersistant( reservation);
                } catch ( EntityNotFoundException ex)  {
                    bNew = true;
                }
            }
            // only is allowed to exchange allocations
            c.editReservation(reservation, appointment, bNew);
            if ( !canModify( reservation) ) 
            {
            	c.deleteButton.setEnabled( false);
                disableComponentAndAllChildren(((ReservationEditImpl)c).appointmentEdit.getComponent());
                disableComponentAndAllChildren(((ReservationEditImpl)c).reservationInfo.getComponent());
            }
        }
        return c;
    }

    static void disableComponentAndAllChildren(Container component) {
        component.setEnabled( false );
        Component[] components = component.getComponents();
        for ( int i=0; i< components.length; i++)
        {
            if ( components[i] instanceof Container) {
                disableComponentAndAllChildren( (Container) components[i] );
            }
        }
    }



    public void deleteAppointment(Appointment appointment,Date from,Component sourceComponent,Point point) throws RaplaException {
        Reservation reservation = appointment.getReservation();
        Reservation mutableReservation = (Reservation)getModification().edit(reservation);
        Appointment app = mutableReservation.findAppointment(appointment);

        if (app.getRepeating() != null) {
            String dateString = getRaplaLocale().formatDate(from);
            DialogUI dialog = DialogUI.create(
                    getContext()
                    ,sourceComponent
                    ,true
                    ,getString("delete")
                    ,getI18n().format("delete_appointment.format"
                            ,dateString)
                            ,new String[] {
                            getString("serie")
                            ,getString("single_appointment")
                            ,getString("cancel")
                        }
                                              );
            dialog.setIcon(getIcon("icon.question"));
            dialog.getButton(0).setIcon(getIcon("icon.repeating"));
            dialog.getButton(2).setIcon(getIcon("icon.cancel"));
            dialog.start(point);
            int index = dialog.getSelectedIndex();
            if (index == 2)
                return;
            if (index == 1) {
                app.getRepeating().addException(from);
                save(mutableReservation,sourceComponent);
                return;
            }
            if (mutableReservation.getAppointments().length == 1) {
                getModification().remove( mutableReservation );
                return;
            }
        }


        // remove appointment if there are other appointments in the reservation
        if ( mutableReservation.getAppointments().length > 1) {
            // remove all allocatables that are restricted to the removed appointment
            Allocatable[] allocatables = mutableReservation.getAllocatables();
            for (int i=0;i<allocatables.length;i++) {
                 Appointment[] restriction =
                    mutableReservation.getRestriction( allocatables[i] );
                if (restriction.length == 1 && restriction[0].equals(app))
                    mutableReservation.removeAllocatable( allocatables[i] );
            }
            mutableReservation.removeAppointment(app);
            getModification().store(mutableReservation);
        } else {
            try {
                DialogUI dlg = getInfoFactory().createDeleteDialog(new Object[] {mutableReservation}
                                                                   ,sourceComponent);
                dlg.start();
                if (dlg.getSelectedIndex() == 0)
                    getModification().remove( mutableReservation);
             } catch (RaplaException ex) {
                showException(ex,sourceComponent);
            }
        } // end of else
    }

    public Appointment copyAppointment(Appointment appointment) throws RaplaException {
        return (Appointment) getModification().clone(appointment);
    }

    public Appointment copyAppointment(
                                       Appointment appointment
                                       ,Date from
                                       ,Component sourceComponent
                                       ,Point point)
        throws RaplaException
    {
        Reservation reservation = appointment.getReservation();
        getLogger().info("copy appointment '" + appointment + "' for reservation '" +  reservation + "'");
        Appointment copy = copyAppointment(appointment);
        if (appointment.getRepeating() != null) {
            String dateString = getRaplaLocale().formatDate(from);
            DialogUI dialog = DialogUI.create(
                    getContext()
                    ,sourceComponent
                    ,true
                    ,getString("copy")
                    ,getI18n().format("copy_appointment.format"
                            ,dateString)
                            ,new String[] {
                            getString("serie")
                            ,getString("single_appointment")
                    }
            );
            dialog.setIcon(getIcon("icon.question"));
            dialog.getButton(0).setIcon(getIcon("icon.repeating"));
            dialog.start(point);
            int index = dialog.getSelectedIndex();
            if (index == 1) {
                copy.setRepeatingEnabled(false);
                copy.move(from);
            }
        }

        return copy;
    }

    public void dataChanged(ModificationEvent evt) throws RaplaException {
        Iterator it = editWindowList.iterator();

        while (it.hasNext()) {
            ReservationEditImpl c = (ReservationEditImpl)it.next();
            c.refresh(evt);
        
            if (evt.hasChanged(c.getReservation())) {
                Iterator it2 = evt.getChanged().iterator();
                Reservation updatedReservation = null;
                
                while (it2.hasNext()) {
                    Object obj =it2.next();
                    if ( !( obj instanceof Reservation )) {
                        continue;
                    }
                    Reservation newReservation = (Reservation) obj;
                    if (newReservation.isIdentical(c.getReservation())) {
                        updatedReservation = newReservation;
                        break;
                    }
                }
                if ( updatedReservation != null)
                {

⌨️ 快捷键说明

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