swingcompactweekcalendar.java

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

JAVA
174
字号

/*--------------------------------------------------------------------------*
 | 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.plugin.compactweekview;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JLabel;

import org.rapla.components.calendar.DateRenderer;
import org.rapla.components.calendar.DateRendererAdapter;
import org.rapla.components.calendarview.swing.AbstractSwingCalendar;
import org.rapla.components.calendarview.swing.SwingCompactWeekView;
import org.rapla.components.calendarview.swing.ViewListener;
import org.rapla.components.util.DateTools;
import org.rapla.entities.NamedComparator;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.CalendarModel;
import org.rapla.gui.CalendarOptions;
import org.rapla.plugin.abstractcalendar.AbstractRaplaSwingCalendar;
import org.rapla.plugin.abstractcalendar.GroupAllocatablesStrategy;
import org.rapla.plugin.abstractcalendar.RaplaBuilder;
import org.rapla.plugin.abstractcalendar.RaplaCalendarViewListener;
import org.rapla.plugin.abstractcalendar.SwingRaplaBuilder;


public class SwingCompactWeekCalendar extends AbstractRaplaSwingCalendar
{
    public SwingCompactWeekCalendar(RaplaContext sm,CalendarModel settings, boolean editable) throws RaplaException {
        super( sm, settings, editable);
    }
    protected AbstractSwingCalendar createView(boolean editable) {
        boolean showScrollPane = editable;
        final DateRendererAdapter dateRenderer = new DateRendererAdapter((DateRenderer) getService(DateRenderer.class.getName()), getRaplaLocale().getTimeZone(), getRaplaLocale().getLocale());
        return new SwingCompactWeekView( showScrollPane ) {
            private static final long serialVersionUID = 1L;
            
            protected JComponent createSlotHeader(Date date) {
                JLabel component = (JLabel) super.createSlotHeader(date);
                if ( date != null ) {
                    boolean today = DateTools.isSameDay(getQuery().today().getTime(), date.getTime());
                    if ( today)
                    {
                        component.setFont(component.getFont().deriveFont( Font.BOLD));
                    }
                    if (isEditable() && dateRenderer != null ) {
                        component.setOpaque(true);
                        Color color = dateRenderer.getBackgroundColor(date);
                        String toolTip = dateRenderer.getToolTipText(date);
                        component.setBackground(color);
                        component.setToolTipText(toolTip);
                    }
                }
                return component;
            }
        };

    }

    protected ViewListener createListener() throws RaplaException {
        return  new RaplaCalendarViewListener(getContext(), model, view) {
         /** override to change the allocatable to the row that is selected */
            protected void showPopupMenu(Component component,Point p,Date start,Date end, int slotNr)
            {
                Allocatable[] selectedAllocatables;
                try {
                    selectedAllocatables = getModel().getSelectedAllocatables();
                } catch (RaplaException ex) {
                    showException(ex, view);
                    return;
                }
                Allocatable selectedAllocatable = null;
                if ( selectedAllocatables.length == 1 ) {
                    selectedAllocatable = selectedAllocatables[0];
                }

                if ( end.getTime()- start.getTime() == DateTools.MILLISECONDS_PER_DAY ) {
                    Calendar cal = getRaplaLocale().createCalendar();
                    cal.setTime ( start );
                    cal.set( Calendar.HOUR_OF_DAY, getCalendarOptions().getWorktimeStart());
                    start = cal.getTime();
                    end = new Date ( start.getTime() + 30 * DateTools.MILLISECONDS_PER_MINUTE );
                }
                if ( selectedAllocatables.length > 0 ) {
                    int row = slotNr / DateTools.DAYS_PER_WEEK;
                    List sortedAllocatables = new ArrayList( Arrays.asList( selectedAllocatables));
                    Collections.sort(sortedAllocatables, new NamedComparator( getLocale() ));
                    selectedAllocatable = (Allocatable) sortedAllocatables.get(row);
                }

                showPopupMenu( component, p, start,end, slotNr, selectedAllocatables, selectedAllocatable);
            }

            /* if the selcted view is a month-view or compact-view, the start-time will not be the selected time,
            * but the time of the start-time of the appointment instead. The start-date is taken from the passed date.
            * */
            protected Date calcStartDate(Date date, Appointment appointment) {
                return getRaplaLocale().toDate( date, appointment.getStart() );
            }

        };
    }

    protected RaplaBuilder createBuilder() throws RaplaException {
        RaplaBuilder builder = new SwingRaplaBuilder(getContext());
        builder.setRepeatingVisible( view.isEditable());
        builder.setEditingUser( getUser() );
        builder.setExceptionsExcluded( !getCalendarOptions().isExceptionsVisible() || !view.isEditable());
        builder.setFromModel( model, view.getStartDate(), view.getEndDate() );

        builder.setSmallBlocks( true );
        builder.setSplitByAllocatables( true );

        GroupAllocatablesStrategy strategy = new GroupAllocatablesStrategy( getRaplaLocale().getLocale() );
        strategy.setFixedSlotsEnabled( true);
        strategy.setResolveConflictsEnabled( false );
        strategy.setAllocationGroups( builder.getAllocatables() ) ;
        builder.setBuildStrategy( strategy );
        Allocatable[] allocatables = (Allocatable[])builder.getAllocatables().toArray( Allocatable.ALLOCATABLE_ARRAY);
        String[] slotNames = new String[ allocatables.length ];
        for (int i = 0; i <allocatables.length; i++ ) {
            slotNames[i] = allocatables[i].getName( getRaplaLocale().getLocale() );
        }

        ((SwingCompactWeekView)view).setSlots( slotNames );
        return builder;
    }

    protected void configureView() throws RaplaException {
        CalendarOptions calendarOptions = getCalendarOptions();
        Set excludeDays = calendarOptions.getExcludeDays();

        view.setToDate(model.getSelectedDate());
        view.setExcludeDays( excludeDays );
        if ( !view.isEditable() ) {
            view.setSlotSize( model.getSize());
        } else {
            view.setSlotSize( 150 );
        }
    }

    public int getIncrementSize()
    {
        return Calendar.WEEK_OF_YEAR;
    }


}

⌨️ 快捷键说明

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