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

📄 raplabuilder.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Gereon Fassbender, 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).             |
 *--------------------------------------------------------------------------*/
/** rapla-specific implementation of builder.
* Splits the appointments into day-blocks.
*/

package org.rapla.plugin.abstractcalendar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.apache.avalon.framework.logger.Logger;
import org.rapla.components.calendarview.Block;
import org.rapla.components.calendarview.BuildStrategy;
import org.rapla.components.calendarview.Builder;
import org.rapla.components.calendarview.CalendarView;
import org.rapla.components.util.Assert;
import org.rapla.components.util.DateTools;
import org.rapla.components.util.Tools;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.entities.Category;
import org.rapla.entities.CategoryAnnotations;
import org.rapla.entities.NamedComparator;
import org.rapla.entities.User;
import org.rapla.entities.configuration.RaplaConfiguration;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.AppointmentBlockArray;
import org.rapla.entities.domain.Permission;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.Classifiable;
import org.rapla.entities.dynamictype.Classification;
import org.rapla.facade.RaplaComponent;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.framework.RaplaLocale;
import org.rapla.gui.CalendarModel;
import org.rapla.gui.CalendarOptions;
import org.rapla.gui.CalendarOptionsImpl;
import org.rapla.gui.toolkit.RaplaColorList;

public abstract class RaplaBuilder extends RaplaComponent
    implements
        Builder
        ,Cloneable
{

    private Collection selectedReservations;
    private List selectedAllocatables = new ArrayList();

    private boolean enabled= true;
    private boolean bExceptionsExcluded = false;
    private boolean bResourceVisible = true;
    private boolean bPersonVisible = true;
    private boolean bRepeatingVisible = true;
    private boolean bTimeVisible = false;
    private boolean splitByAllocatables = false;
    protected boolean isEmptyBlockVisible = false;
    private HashMap colors = new HashMap(); //This currently only works with HashMap
    private User editingUser;
    private boolean isResourceColoring;
    /** default buildStrategy is {@link GroupAllocatablesStrategy}.*/
    BuildStrategy buildStrategy;

    HashSet allReservationsForAllocatables = new HashSet();
    int max =0;
    int min =0;

    AppointmentBlockArray preparedBlocks = null;

    public RaplaBuilder(RaplaContext sm) throws RaplaException {
        super(sm);
        buildStrategy = new GroupAllocatablesStrategy( getRaplaLocale().getLocale() );
    }

    public void setFromModel(CalendarModel model, Date startDate, Date endDate) throws RaplaException {
        Collection reservations = new HashSet(Arrays.asList( model.getReservations(  startDate, endDate )));
        Allocatable[] all = model.getSelectedAllocatables();
        Collection allocatables = Arrays.asList( all);
        if ( model.getStartDate() != null && allocatables.size()> 0) {
            allReservationsForAllocatables.addAll( Arrays.asList(getQuery().getReservations( all, startDate, endDate)));
        }
        isEmptyBlockVisible = allocatables.size() == 0;
        isResourceColoring =getCalendarOptions( model.getUser()).isResourceColoring();
        selectReservations( reservations );


        /* Uncomment this to color allocatables in the reservation view
        if ( allocatables.size() == 0) {
            allocatables = new ArrayList();
            for (int i=0;i< reservations.size();i++) {
                Reservation r = (Reservation) reservations.get( i );
                Allocatable[] a = r.getAllocatables();
                for (int j=0;j<a.length;j++) {
                    if ( !allocatables.contains( a[j] )) {
                        allocatables.add( a[j]);
                    }
                }
            }
        }*/
        selectAllocatables( allocatables );
    }

    private CalendarOptions getCalendarOptions(User user) {
        RaplaConfiguration conf = null;
        try {
            if ( user != null)
            {
                conf = (RaplaConfiguration)getQuery().getPreferences( user ).getEntry(CalendarOptionsImpl.CALENDAR_OPTIONS);
            }
            if ( conf == null)
            {
                conf = (RaplaConfiguration)getQuery().getPreferences( null ).getEntry(CalendarOptionsImpl.CALENDAR_OPTIONS);
            }
            if ( conf != null)
            {
                return new CalendarOptionsImpl( conf.getConfig());
            }
        } catch (RaplaException ex) {

        }
        return (CalendarOptions)getService( CalendarOptions.ROLE);
    }


    public void setSmallBlocks( boolean isSmallView) {
        setTimeVisible(  isSmallView );
        setPersonVisible( !isSmallView );
        setResourceVisible( !isSmallView );
    }

    public void setSplitByAllocatables( boolean enable) {
        splitByAllocatables = enable;
    }

    public void setExceptionsExcluded( boolean exclude) {
        this.bExceptionsExcluded = exclude;
    }

    boolean isExceptionsExcluded() {
        return bExceptionsExcluded;
    }

    public void setEditingUser(User editingUser) {
        this.editingUser = editingUser;
    }

    public User getEditingUser() {
        return this.editingUser;
    }

    public boolean isEnabled()  {
        return enabled;
    }

    public void setEnabled(boolean enable) {
        this.enabled = enable;
    }

    public void setBuildStrategy(BuildStrategy strategy) {
        this.buildStrategy = strategy;
    }

    public void setTimeVisible(boolean bTimeVisible) {
        this.bTimeVisible = bTimeVisible;
    }

    public boolean isTimeVisible() {
        return bTimeVisible;
    }

    public void setResourceVisible(boolean bVisible) {
        this.bResourceVisible = bVisible;
    }

    public void setPersonVisible(boolean bVisible) {
        this.bPersonVisible = bVisible;
    }

    public void setRepeatingVisible(boolean bVisible) {
        this.bRepeatingVisible = bVisible;
    }

    public List getAllocatables() {
        return selectedAllocatables;
    }

    /**
        Map enthaelt die fuer die resourcen-darstellung benutzten farben
        mit resource-id (vom typ Integer) als schluessel.
        erst nach rebuild() verfuegbar.
    */
    Map getColorMap() {
        return colors;
    }

    private void createColorMap()
    {
        colors.clear();
        List arrayList = new ArrayList(selectedAllocatables);
        Comparator comp =new Comparator() {
                public int compare(Object o1, Object o2) {
                    if (o1.hashCode()>o2.hashCode())
                        return -1;
                    if (o1.hashCode()<o2.hashCode())
                        return 1;
                    return 0;
                }
            };
        Collections.sort(arrayList,comp);
        Iterator it = arrayList.iterator();
        int i=0;
        while (it.hasNext()) {
            Allocatable allocatable = (Allocatable) it.next();
            String color = getColorForClassifiable( allocatable );
            if ( color == null )
                color = RaplaColorList.getHexForColor( RaplaColorList.getResourceColor(i++));
            colors.put(allocatable, color);
        }
    }

    static String getColorForClassifiable( Classifiable classifiable ) {
        Classification c = classifiable.getClassification();
        Attribute colorAttribute = c.getAttribute("color");
        String color = null;
        if ( colorAttribute != null) {
            Object hexValue = c.getValue( colorAttribute );
            if ( hexValue != null) {
                if ( hexValue instanceof Category) {
                    hexValue = ((Category) hexValue).getAnnotation( CategoryAnnotations.KEY_NAME_COLOR );
                }
                if ( hexValue != null) {
                    color = hexValue.toString();
                }
            }
        }
        return color;
    }

    /**
       diese reservierungen werden vom WeekView angezeigt.
    */
    public void selectReservations(Collection reservations)
    {
        this.selectedReservations= reservations;
    }

    /**
       nur diese resourcen werden angezeigt.
    */
    public void selectAllocatables(Collection allocatables)
    {
        selectedAllocatables.clear();
        if (allocatables != null ) {
            selectedAllocatables.addAll(new HashSet(allocatables));
            Collections.sort( selectedAllocatables, new NamedComparator( getRaplaLocale().getLocale() ));
        }
        createColorMap();
    }
    /** @param emptyBlockVisible indicates that blocks that don't contain one of the selected
    * allocatables should also be visible.
    * */

    static public AppointmentBlockArray getAffectedAppointments(Collection selectedReservations, Collection selectedAllocatables,
                                           Date start, Date end, boolean emptyBlockVisible, boolean excludeExceptions) {
        if ( selectedAllocatables == null )
            selectedAllocatables = Tools.EMPTY_LIST;
        AppointmentBlockArray preparedBlocks = new AppointmentBlockArray();
        // The appointments that are explictly connected to
        // a selected allocatable
        ArrayList connectedAppointments = new ArrayList();
        Iterator it = selectedReservations.iterator();
        while (it.hasNext()){
            Reservation r= (Reservation) it.next();
            //System.out.println("Selected : " + r.getName(Locale.getDefault()));
            Appointment[] appointments= r.getAppointments();
            Allocatable[] allocatables = r.getAllocatables();

            // this flag is set true if one of the allocatables of a
            // reservation matches a selected allocatable.
            boolean allocatableMatched = false;

            // a reservation is wildcardConnected, if at least one of its
            // allocatables is connected to all appointments and
            // this allocatable is selected.
            boolean wildcardConnected = false;

            connectedAppointments.clear();
            for (int i=0; i<allocatables.length; i++)   {
                if (selectedAllocatables.contains(allocatables[i])) {
                    Appointment[] restriction = r.getRestriction(allocatables[i]);
                    if (restriction.length == 0) {
                        // this allocatable is connected to all appointments.
                        allocatableMatched = true;
                        wildcardConnected = true;
                        break;
                    } else {
                        allocatableMatched = true;
                        for (int j=0; j<appointments.length; j++)   {
                            for (int k = 0; k < restriction.length; k++ ) {
                                if ( appointments[j].equals( restriction[k] ) ) {
                                    // This appointment is explictly connected to
                                    // the selected allocatable
                                    connectedAppointments.add(appointments[j]);
                                }
                            }

⌨️ 快捷键说明

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