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

📄 appointmentlistedit.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/*--------------------------------------------------------------------------*
 | 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.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.container.ContainerUtil;
import org.rapla.components.layout.TableLayout;
import org.rapla.components.util.DateTools;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.AppointmentBlockArray;
import org.rapla.entities.domain.Repeating;
import org.rapla.entities.domain.Reservation;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.internal.edit.RaplaListEdit;
import org.rapla.gui.toolkit.RaplaWidget;

/** Default GUI for editing multiple appointments.*/
class AppointmentListEdit extends AbstractAppointmentEditor
    implements
        RaplaWidget
        ,Disposable
{

    private AppointmentController appointmentController;
    private RaplaListEdit listEdit;

    protected Reservation mutableReservation;
    private Listener listener = new Listener();
    DefaultListModel model = new DefaultListModel();

    AppointmentListEdit(RaplaContext sm) throws RaplaException {
        super( sm);
        appointmentController = new AppointmentController(sm);
        listEdit = new RaplaListEdit(getI18n(),appointmentController.getComponent(), listener);
        appointmentController.addChangeListener(listener);
        listEdit.getList().setModel(model);
        listEdit.setColoredBackgroundEnabled(true);
        listEdit.setMoveButtonVisible(false);
        JLabel status =listEdit.getStatusBar(); 
        status.setFont( status.getFont().deriveFont( (float)9.0)); 
        listEdit.getList().setCellRenderer(new AppointmentCellRenderer());
    }

    public JComponent getComponent() {
        return listEdit.getComponent();
    }

    public void setReservation(Reservation mutableReservation, Appointment appointment) {
        this.mutableReservation = mutableReservation;
        Appointment[] appointments = mutableReservation.getAppointments();
        model.clear();
        for (int i = 0; i<appointments.length; i++) {
            model.addElement(appointments[i]);
        }
        if ( appointment != null ) {
            listEdit.select( model.indexOf( appointment ) );
        } else if ( appointments.length> 0 ){
            listEdit.select(0);
        }
        updateStatus();
    }

    public void dispose() {
        ContainerUtil.dispose(  appointmentController );
    }

    private void removeAppointments() {
        Object[] objects = listEdit.getList().getSelectedValues();
        for (int i=0;i<objects.length;i++) {
            Appointment appointment = (Appointment) objects[i];
            model.removeElement(appointment);
            mutableReservation.removeAppointment(appointment);
            updateStatus();
            fireAppointmentRemoved(appointment);
        }
        listEdit.getList().requestFocus();
    }

    private void createNewAppointment() {
        try {
            Appointment[] appointments = mutableReservation.getAppointments();
            Appointment appointment;
            if (appointments.length == 0) {
                Date start = new Date(DateTools.cutDate(new Date()).getTime()
                                      + getCalendarOptions().getWorktimeStart()
                                      * DateTools.MILLISECONDS_PER_HOUR
                                      );
                Date end = new Date(start.getTime() + DateTools.MILLISECONDS_PER_HOUR);
                appointment = (Appointment)
                    getModification().newAppointment( start, end );
            } else {
                appointment = getReservationController().copyAppointment(appointments[appointments.length-1]);
                Repeating repeating = appointment.getRepeating();
                if (repeating != null) {
                    repeating.clearExceptions();
                }
            }
            mutableReservation.addAppointment(appointment);
            model.addElement(appointment);
            updateStatus();
            fireAppointmentAdded(appointment);
            listEdit.select(model.getSize()-1);
        } catch (RaplaException ex) {
            showException(ex, getComponent());
        }
    }

    private void updateStatus() {
        Reservation event = mutableReservation;
        Appointment[] appointments = event.getAppointments();
        int count = 0;
        for (int i = 0; i<appointments.length; i++) {
            Appointment appointment = appointments[i];
            Repeating repeating = appointment.getRepeating();
            if ( repeating == null ) {
                count ++;
                continue;
            }
            if ( repeating.getEnd() == null ){ // Repeats foreever ?
                count = -1;
                break;
            }
            AppointmentBlockArray blocks = new AppointmentBlockArray();
            appointment.createBlocks( appointment.getStart(), repeating.getEnd(), blocks);
            count += blocks.size();
        }
        String status = "";
        if (count >= 0)
            status = getString("total_occurances")+ ": " + count;
        listEdit.getStatusBar().setText( status );
    }

    class AppointmentCellRenderer implements ListCellRenderer {
        Border focusBorder = UIManager.getBorder("List.focusCellHighlightBorder");
        Border emptyBorder = new EmptyBorder(1,1,1,1);

        Color selectionBackground = UIManager.getColor("List.selectionBackground");
        Color background = UIManager.getColor("List.background");

        AppointmentRow row = new AppointmentRow();
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            row.setAppointment((Appointment) value,index);
            row.setBackground((isSelected) ? selectionBackground : background);
            row.setBorder((cellHasFocus) ? focusBorder : emptyBorder);
            return row;
        }
    };

    class AppointmentRow extends JPanel {
        private static final long serialVersionUID = 1L;

        JPanel content = new JPanel();
        AppointmentIdentifier identifier = new AppointmentIdentifier();
        AppointmentRow() {
            double fill = TableLayout.FILL;
            double pre = TableLayout.PREFERRED;
            this.setLayout(new TableLayout(new double[][] {{pre,5,fill,10,pre},{1,fill,1}}));
            this.add(identifier,"0,1,l,f");
            this.add(content,"2,1,f,c");

            this.setMaximumSize(new Dimension(500,40));
            content.setOpaque(false);
            identifier.setOpaque(true);
            identifier.setBorder(null);
        }

        public void setAppointment(Appointment appointment,int index) {
            identifier.setText(getRaplaLocale().formatNumber(index + 1));
            identifier.setIndex(index);
            content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
            content.removeAll();
            JLabel label1 = new JLabel(getAppointmentFormater().getSummary(appointment));
            content.add( label1 );
            if (appointment.getRepeating() != null) {
                label1.setIcon( getIcon("icon.repeating") );
                Repeating r = appointment.getRepeating();
                List periods = getPeriodModel().getPeriodsFor(appointment.getStart());
                String repeatingString =
                    getAppointmentFormater().getSummary(r,periods);
                content.add(new JLabel(repeatingString));
                if ( r.hasExceptions() ) {
                    content.add(new JLabel( getAppointmentFormater().getExceptionSummary( r ) ) );
                }
            } else {
                label1.setIcon( getIcon("icon.single") );
            }
        }
    }

    class Listener implements ActionListener,ChangeListener {
        public void actionPerformed(ActionEvent evt) {
            if (evt.getActionCommand().equals("remove")) {
                removeAppointments();
            } else if (evt.getActionCommand().equals("new")) {
                createNewAppointment();
            } else if (evt.getActionCommand().equals("edit")) {
                Appointment app = (Appointment) listEdit.getList().getSelectedValue();
                appointmentController.setAppointment(app);
            }
        }

        public void stateChanged(ChangeEvent evt) {
            Appointment appointment = appointmentController.getAppointment();
            model.set(model.indexOf(appointment),appointment);
            updateStatus();
            fireAppointmentChanged(appointment);
        }
    }
}


⌨️ 快捷键说明

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