menufactoryimpl.java

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

JAVA
346
字号
/*--------------------------------------------------------------------------*
 | 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;
import java.awt.Component;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.Action;
import javax.swing.JMenuItem;

import org.rapla.components.util.DateTools;
import org.rapla.entities.Category;
import org.rapla.entities.RaplaObject;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Period;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
import org.rapla.facade.Conflict;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.MenuContext;
import org.rapla.gui.MenuFactory;
import org.rapla.gui.ObjectMenuFactory;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.ReservationWizard;
import org.rapla.gui.internal.action.DynamicTypeAction;
import org.rapla.gui.internal.action.RaplaObjectAction;
import org.rapla.gui.internal.action.ReservationAction;
import org.rapla.gui.internal.action.user.PasswordChangeAction;
import org.rapla.gui.internal.action.user.UserAction;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.toolkit.MenuInterface;
import org.rapla.gui.toolkit.RaplaMenuItem;
import org.rapla.gui.toolkit.RaplaSeparator;
import org.rapla.plugin.RaplaExtensionPoints;

public class MenuFactoryImpl extends RaplaGUIComponent implements MenuFactory
{
    public MenuInterface addReservationWizards( MenuInterface menu, MenuContext context, String afterId ) throws RaplaException
    {
        Component parent = context.getComponent();
        Object focusedObject = context.getFocusedObject();
        Point p = context.getPoint();
        Iterator it = getContainer().lookupServicesFor( RaplaExtensionPoints.RESERVATION_WIZARD_EXTENSION).values().iterator();
        while (it.hasNext())
        {
            ReservationWizard wizard = (ReservationWizard) it.next();
            addReservationAction(menu,parent,p, afterId).setNew(wizard).changeObject( focusedObject );
        }
        return menu;
    }


    public MenuFactoryImpl(RaplaContext sm) throws RaplaException {
       super(sm);
    }

    public MenuInterface addNew( MenuInterface menu, MenuContext context,String afterId ) throws RaplaException
    {
        // Do nothing if the user can't allocate anything
        if (!canUserAllocateSomething( getUser()) )
            return menu;

        addReservationWizards(menu, context, afterId);

        Component parent = context.getComponent();
        Object focusedObject = context.getFocusedObject();
        Point p = context.getPoint();
        if ( isRegisterer() || isAdmin()) {
            menu.addSeparator();
            addAllocatableMenuNew( menu, parent,p, focusedObject);
        }
        if ( isAdmin() )
        {
            menu.addSeparator();
            addUserMenuNew( menu , parent, p, focusedObject);
            addPeriodMenuNew(  menu , parent, p, focusedObject );
        }

        addTypeMenuNew(menu, parent, p, focusedObject);

        return menu;
    }

    public MenuInterface addObjectMenu( MenuInterface menu, MenuContext context) throws RaplaException {
        return addObjectMenu( menu, context, "EDIT_BEGIN");
    }

    public MenuInterface addObjectMenu( MenuInterface menu, MenuContext context, String afterId ) throws RaplaException
    {
        Component parent = context.getComponent();
        Object focusedObject = context.getFocusedObject();
        Point p = context.getPoint();
        Collection list = context.getSelectedObjects();
       
        if ( focusedObject == null || !(focusedObject instanceof RaplaObject)) {
            addAction(menu,parent,p, afterId).setDeleteSelection(getDeletableObjects(list));
            return menu;
        }
        RaplaObject obj = (RaplaObject) focusedObject;

        RaplaType type = obj.getRaplaType();

        Iterator it = getContainer().lookupServicesFor( RaplaExtensionPoints.OBJECT_MENU_EXTENSION).values().iterator();
        while (it.hasNext())
        {
            ObjectMenuFactory objectMenuFact = (ObjectMenuFactory) it.next();
            RaplaMenuItem[] items = objectMenuFact.create( context, obj);
            for ( int i =0;i<items.length;i++)
            {
                RaplaMenuItem item =  items[i];
                menu.insertAfterId( item, afterId);
            }
        }

        
        if ( type.equals(Allocatable.TYPE))
        {
            addAllocatableMenuEdit(menu,  parent, p, (Allocatable)obj, list, afterId);
        }
        else if ( type.equals(Reservation.TYPE) )
        {
            addReservationMenuEdit(menu, parent, p, (Reservation)obj, list, getStartDateForNewReservations(), afterId);
        }
        else if ( type.equals(User.TYPE) )
        {
            addUserMenuEdit( menu , parent, p, obj, list, afterId);
        }
        else if ( type.equals(Period.TYPE) )
        {
            addPeriodMenuEdit(  menu , parent, p, obj, list, afterId);
        }
        else if ( type.equals(Category.TYPE) )
        {
            addCategoryMenu(  menu , parent, p, obj, list, afterId);
        }
        else if ( type.equals(Conflict.TYPE) )
        {
            addConflictMenu(  menu , parent, p, obj, list, afterId);
        }
        if ( type.equals(DynamicType.TYPE))
        {
            if ( isAdmin())
            {
                addAction(menu,parent,p, afterId).setDeleteSelection( list );
                addAction(menu,parent,p,afterId).setView(obj);
                addAction(menu,parent,p, afterId).setEdit(obj);
            }
        }
        
        return menu;
    }

    private Date getStartDateForNewReservations() {
        Date startDate = getModel().getSelectedDate();
        if ( startDate != null ) {
            Date time = new Date (DateTools.MILLISECONDS_PER_HOUR * getCalendarOptions().getWorktimeStart());
            startDate = getRaplaLocale().toDate(startDate,time);
        }
        return startDate;
    }

    private CalendarSelectionModel getModel() {
        return (CalendarSelectionModel) getService( CalendarSelectionModel.ROLE);
    }

    private void addAllocatableMenuEdit(MenuInterface menu,Component parent,Point p,Allocatable obj,Collection selection, String id) throws RaplaException {
        addAction(menu,parent,p, id).setDeleteSelection(getDeletableObjects(selection));
        addAction(menu,parent,p, id).setView(obj);
        addAction(menu,parent,p, id).setEdit(obj);
    }

    private void addAllocatableMenuNew(MenuInterface menu,Component parent,Point p,Object obj) throws RaplaException {
        RaplaObjectAction newResource = addAction(menu,parent,p).setNew( Allocatable.TYPE );
        newResource.changeObject( obj );
        newResource.putValue(Action.NAME,getString("resource"));

        RaplaObjectAction newPerson = addAction(menu,parent,p).setNew( Allocatable.TYPE );
        newPerson.setPerson( true );
        newPerson.changeObject( obj );
        newPerson.putValue(Action.NAME,getString("person"));

    }


    private void addReservationMenuEdit(MenuInterface menu,Component parent,Point p,Reservation obj,Collection selection,Date startDate, String afterId) throws RaplaException
    {
        addAction(menu,parent,p, afterId).setDeleteSelection(getDeletableObjects(selection));
        addReservationAction(menu,parent,p, afterId).setView(obj);
        addReservationAction(menu,parent,p, afterId).setEdit(obj);
    }


    private void addTypeMenuNew(MenuInterface menu,Component parent,Point p,Object obj) throws RaplaException {
        if ( isAdmin() ) {
            menu.addSeparator();

            DynamicTypeAction newResourceType = newDynamicTypeAction(parent,p);
            menu.add(new JMenuItem(newResourceType));
            newResourceType.setNewClassificationType( DynamicTypeAnnotations.VALUE_RESOURCE_CLASSIFICATION);
            newResourceType.putValue(Action.NAME,getString("resource_type"));

            DynamicTypeAction newPersonType = newDynamicTypeAction(parent,p);
            menu.add(new JMenuItem(newPersonType));
            newPersonType.setNewClassificationType(DynamicTypeAnnotations.VALUE_PERSON_CLASSIFICATION);
            newPersonType.putValue(Action.NAME,getString("person_type"));

            DynamicTypeAction newReservationType = newDynamicTypeAction(parent,p);
            menu.add(new JMenuItem(newReservationType));
            newReservationType.setNewClassificationType(DynamicTypeAnnotations.VALUE_RESERVATION_CLASSIFICATION);
            newReservationType.putValue(Action.NAME,getString("reservation_type"));
        }
    }



    private void addUserMenuEdit(MenuInterface menu,Component parent,Point p,Object obj,Collection selection, String afterId) throws RaplaException {
        if ( isAdmin()) {
            addAction(menu,parent,p, afterId).setEdit(obj);
            addAction(menu,parent,p, afterId).setView(obj);
            menu.insertAfterId( new RaplaSeparator("sep1"),afterId);
        }
        addAction(menu,parent,p, afterId).setDeleteSelection(getDeletableObjects(selection));
        menu.insertAfterId( new RaplaSeparator("sep2"), afterId);
        PasswordChangeAction passwordChangeAction = new PasswordChangeAction(getContext(),parent,p);
        passwordChangeAction.changeObject( obj );
        menu.insertAfterId( new JMenuItem( passwordChangeAction ), "sep2");

        UserAction switchUserAction = newUserAction(parent,p);
        switchUserAction.setSwitchToUser();
        switchUserAction.changeObject( obj );
        menu.insertAfterId( new JMenuItem( switchUserAction ), "sep2");
    }

    private void addUserMenuNew(MenuInterface menu,Component parent,Point p,Object obj) throws RaplaException {
        UserAction newUserAction = newUserAction(parent,p);
        newUserAction.setNew();
        menu.add( new JMenuItem( newUserAction ));
    }

    private void addCategoryMenu(MenuInterface menu,Component parent,Point p,Object obj,Collection selection, String afterId) throws RaplaException {
        addAction(menu,parent,p, afterId).setEdit(obj);
        addAction(menu,parent,p, afterId).setDeleteSelection(getDeletableObjects(selection));
    }

    private void addConflictMenu(MenuInterface menu,Component parent,Point p,Object obj,Collection selection, String afterId) throws RaplaException {
        addReservationAction(menu,parent,p, afterId).setEdit( ((Conflict)obj).getReservation1());
        addAction(menu,parent,p, afterId).setView(obj);
    }

    private void addPeriodMenuEdit(MenuInterface menu, Component parent, Point p, Object obj, Collection selection, String afterId) throws RaplaException {
        addAction(menu,parent,p, afterId).setEdit(obj);
        addAction(menu,parent,p, afterId).setView(obj);
        addAction(menu,parent,p, afterId).setDeleteSelection(getDeletableObjects(selection));
    }

    private void addPeriodMenuNew(MenuInterface menu, Component parent, Point p, Object obj) throws RaplaException {
        Action newAction = addAction(menu,parent,p).setNew( Period.TYPE );
        newAction.putValue(Action.NAME,getString("period"));

    }

    private RaplaObjectAction addAction(MenuInterface menu, Component parent,Point p) throws RaplaException {
        RaplaObjectAction action = newObjectAction(parent,p);
        menu.add(new JMenuItem(action));
        return action;
    }

    private RaplaObjectAction addAction(MenuInterface menu, Component parent,Point p,String id) throws RaplaException {
        RaplaObjectAction action = newObjectAction(parent,p);
        menu.insertAfterId( new JMenuItem(action), id);
        return action;
    }


    private ReservationAction addReservationAction(MenuInterface menu, Component parent, Point p,String afterId) throws RaplaException {
        ReservationAction action = new ReservationAction(getContext(),parent,p);
        menu.insertAfterId(new JMenuItem(action), afterId);
        return action;
    }

    private RaplaObjectAction newObjectAction(Component parent,Point point) throws RaplaException {
        RaplaObjectAction action = new RaplaObjectAction(getContext(),parent);
        return action;
    }


    private DynamicTypeAction newDynamicTypeAction(Component parent,Point point) throws RaplaException {
        DynamicTypeAction action = new DynamicTypeAction(getContext(),parent,point);
        return action;
    }



    private UserAction newUserAction(Component parent,Point point) throws RaplaException {
        UserAction action = new UserAction(getContext(),parent,point);
        return action;
    }


    // This will exclude DynamicTypes and non editable Objects from the list
    private List getDeletableObjects(Collection list) {
        Iterator it = list.iterator();
        ArrayList editableObjects = new ArrayList();
        while (it.hasNext()) {
            Object o = it.next();
            if (canModify(o) )
                editableObjects.add(o);
        }
        return editableObjects;
    }







}









⌨️ 快捷键说明

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