raplaguicomponent.java

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

JAVA
198
字号
/*--------------------------------------------------------------------------*
 | 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;

import java.awt.Color;
import java.awt.Component;
import java.util.Iterator;
import java.util.Map;

import org.rapla.client.ClientService;
import org.rapla.components.calendar.DateRenderer;
import org.rapla.components.calendar.RaplaCalendar;
import org.rapla.components.calendar.RaplaTime;
import org.rapla.components.calendar.TimeRenderer;
import org.rapla.components.util.DateTools;
import org.rapla.entities.DependencyException;
import org.rapla.entities.User;
import org.rapla.entities.configuration.RaplaConfiguration;
import org.rapla.facade.RaplaComponent;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.toolkit.ErrorDialog;
import org.rapla.gui.toolkit.FrameControllerList;

/**
    Base class for most components in the gui package. Eases
    access to frequently used services, e.g. {@link org.rapla.components.xmlbundle.I18nBundle}.
    It also provides some methods for Exception displaying.
 */
public class RaplaGUIComponent extends RaplaComponent
{

    public RaplaGUIComponent(RaplaContext context) throws RaplaException {
        super(context);
    }

    /** lookup FrameControllerList from the context */
    final protected FrameControllerList getFrameList() {
        checkServiceState();
        return (FrameControllerList) getService(FrameControllerList.ROLE);
    }

    /** Creates a new ErrorDialog with the specified owner and displays the exception
        @param ex the exception that should be displayed.
        @param owner the exception that should be displayed. Can be null, but providing
        a parent-component will lead to a more appropriate display.
    */
    public void showException(Exception ex,Component owner) {
        try {
            ErrorDialog dialog = new ErrorDialog(getContext());
            if (ex instanceof DependencyException) {
                dialog.showWarningDialog( getHTML( (DependencyException)ex ), owner);
            } else {
                dialog.showExceptionDialog(ex,owner);
            }
        } catch (RaplaException ex2) {
            getLogger().error(ex2.getMessage(),ex2);
        }
    }

    public boolean isInvokedOnAWTEventQueue() {
        return true;
    }

    private String getHTML(DependencyException ex){
        StringBuffer buf = new StringBuffer();
        buf.append(getString("error.dependencies")+":");
        buf.append("<br>");
        Iterator it = ex.getDependencies().iterator();
        int i = 0;
        while (it.hasNext()) {
            Object obj = it.next();
            buf.append((++i));
            buf.append(") ");
            buf.append( obj );
            buf.append("<br>");
            if (i == 10 && it.hasNext()) {
                buf.append("... " + (ex.getDependencies().size() -10) + " more");
                break;
            }
        }
        return buf.toString();
    }

    /** Creates a new ErrorDialog with the specified owner and displays the waring */
    public void showWarning(String warning,Component owner) {
        try {
            ErrorDialog dialog = new ErrorDialog(getContext());
            dialog.showWarningDialog(warning,owner);
        } catch (RaplaException ex2) {
            getLogger().error(ex2.getMessage(),ex2);
        }
    }

    public RaplaCalendar createRaplaCalendar() {
        RaplaCalendar cal = new RaplaCalendar( getI18n().getLocale() );
        cal.setDateRenderer(getDateRenderer());
        cal.setTimeZone(DateTools.getTimeZone());
        return cal;
    }

    /** lookup DateRenderer from the serviceManager */
    final protected DateRenderer getDateRenderer() {
        checkServiceState();
        return  (DateRenderer) getService(DateRenderer.class.getName());
    }

    static Color NON_WORKTIME = new Color(0xcc, 0xcc, 0xcc);

    final protected TimeRenderer getTimeRenderer() {
        final int start = getCalendarOptions().getWorktimeStart();
        final int end = getCalendarOptions().getWorktimeEnd();
        return new TimeRenderer() {
            public Color getBackgroundColor( int hourOfDay, int minute )
            {
                if ( start >= end)
                {
                    if ( hourOfDay >= end && hourOfDay < start)
                    {
                        return NON_WORKTIME;
                    }
                }
                else if ( hourOfDay < start || hourOfDay >= end) {
                    return NON_WORKTIME;
                }
                return null;
            }

            public String getToolTipText( int hourOfDay, int minute )
            {
                return null;
            }

        };
    }


    public RaplaTime createRaplaTime() {
        RaplaTime cal = new RaplaTime( getI18n().getLocale() );
        cal.setTimeRenderer( getTimeRenderer() );
        int rowsPerHour =getCalendarOptions().getRowsPerHour() ;
        cal.setRowsPerHour( rowsPerHour );
        cal.setTimeZone(DateTools.getTimeZone());
        return cal;
    }


    public CalendarOptions getCalendarOptions() {
        RaplaConfiguration conf = null;
        try {
            User user = getUser();
            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.class.getName());
    }

    public Map getSessionMap() {
        return (Map) getService( ClientService.SESSION_MAP);
    }

    protected InfoFactory getInfoFactory() {
        return (InfoFactory) getService( InfoFactory.ROLE );
    }

    protected EditController getEditController() {
        return (EditController) getService( EditController.ROLE );
    }

    protected ReservationController getReservationController() {
        return (ReservationController) getService( ReservationController.ROLE );
    }

    public Component getMainComponent() {
        return (Component) getService(ClientService.MAIN_COMPONENT);
    }

}

⌨️ 快捷键说明

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