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

📄 allocatableselection.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*--------------------------------------------------------------------------*
 | 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.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.EventObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.DefaultCellEditor;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.rapla.components.layout.TableLayout;
import org.rapla.components.treetable.AbstractTreeTableModel;
import org.rapla.components.treetable.JTreeTable;
import org.rapla.components.treetable.TableToolTipRenderer;
import org.rapla.components.treetable.TreeTableModel;
import org.rapla.components.util.SmallIntMap;
import org.rapla.entities.Named;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.facade.ModificationEvent;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.CalendarModel;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.TreeFactory;
import org.rapla.gui.internal.action.FilterAction;
import org.rapla.gui.internal.common.CalendarAction;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.toolkit.PopupEvent;
import org.rapla.gui.toolkit.PopupListener;
import org.rapla.gui.toolkit.RaplaButton;
import org.rapla.gui.toolkit.RaplaColorList;
import org.rapla.gui.toolkit.RaplaPopupMenu;
import org.rapla.gui.toolkit.RaplaWidget;

/**
<p>GUI for editing the allocations of a reservation.
   Presents two TreeTables. The left one displays all
   available Resources and Persons the right one all selected Resources and Persons.
</p>
<p>
    The second column of the first table contains information about the availability
    on the appointments of the reservation. In the second column of the second table the user
    can add special Restrictions on the selected Resources and Persons.
</p>
@see Reservation
@see Allocatable
 */
public class AllocatableSelection extends RaplaGUIComponent
implements
    AppointmentListener
    , PopupListener
    ,RaplaWidget
{
    JSplitPane      content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    JPanel            leftPanel= new JPanel();
    JTreeTable          completeTable;
    RaplaButton         btnAdd = new RaplaButton(RaplaButton.SMALL);
    RaplaButton         btnFilter = new RaplaButton(RaplaButton.SMALL);
    RaplaButton         btnCalendar1 = new RaplaButton(RaplaButton.SMALL);
    JPanel            rightPanel = new JPanel();
    JTreeTable          selectedTable;
    RaplaButton         btnRemove = new RaplaButton(RaplaButton.SMALL);
    RaplaButton         btnCalendar2 = new RaplaButton(RaplaButton.SMALL);

    Reservation mutableReservation;

    AllocatablesModel completeModel = new CompleteModel();
    AllocatablesModel selectedModel = new SelectedModel();
    AppointmentCellEditor appointmentCellEditor;
    Map appointmentMap = new HashMap();
    Appointment[] appointments;
    String[] appointmentStrings;
    String[] appointmentIndexStrings;

    CalendarSelectionModel calendarModel;
    EventListenerList listenerList = new EventListenerList();
    Listener listener = new Listener();


    FilterAction filterAction;
    AllocatableAction addAction;
    AllocatableAction removeAction;
    AllocatableAction calendarAction1;
    AllocatableAction calendarAction2;

    User user;

    public AllocatableSelection(RaplaContext sm) throws RaplaException {
        this(sm,false);
    }

    public AllocatableSelection(RaplaContext sm,boolean addCalendarButton) throws RaplaException {
        super( sm);
        double pre =TableLayout.PREFERRED;
        double fill =TableLayout.FILL;
        double tableSize[][] =
            {{ pre,12,pre,3,pre,fill },  // Columns
             {pre, fill}}; // Rows
        leftPanel.setLayout(new TableLayout(tableSize));
        leftPanel.add(btnFilter,"0,0,l,f");
        if (addCalendarButton)
            leftPanel.add(btnCalendar1,"2,0,l,f");
        leftPanel.add(btnAdd,"5,0,r,f");
        rightPanel.setLayout(new TableLayout(tableSize));
        rightPanel.add(btnRemove,"0,0,l,f");
        if (addCalendarButton)
            rightPanel.add(btnCalendar2,"2,0,c,c");
        content.setLeftComponent(leftPanel);
        content.setRightComponent(rightPanel);
        content.setResizeWeight(0.3);

        appointmentCellEditor = new AppointmentCellEditor(new RestrictionTextField());

        btnAdd.setEnabled(false);
        btnCalendar1.setEnabled(false);
        btnRemove.setEnabled(false);
        btnCalendar2.setEnabled(false);

        addAction = new AllocatableAction("add");
        removeAction = new AllocatableAction("remove");
        calendarAction1 = new AllocatableAction("calendar1");
        calendarAction2 = new AllocatableAction("calendar2");

        btnAdd.setAction(addAction);
        btnRemove.setAction(removeAction);
        btnCalendar1.setAction(calendarAction1);
        btnCalendar2.setAction(calendarAction2);

        completeTable = new JTreeTable(completeModel);
        Color tableBackground = completeTable.getTree().getBackground();
        JScrollPane leftScrollpane = new JScrollPane(completeTable);
        leftScrollpane.getViewport().setBackground( tableBackground);
        leftPanel.add(leftScrollpane,"0,1,5,1,f,f");
        completeTable.setGridColor( RaplaColorList.darken( tableBackground, 20 ));
        completeTable.setToolTipRenderer(new RaplaToolTipRenderer());
        completeTable.getSelectionModel().addListSelectionListener(listener);
        completeTable.setDefaultRenderer(Allocatable.class,new AllocationCellRenderer());
        completeTable.addMouseListener(listener);

        selectedTable = new JTreeTable(selectedModel);
        JScrollPane rightScrollpane = new JScrollPane(selectedTable);
        rightScrollpane.getViewport().setBackground( tableBackground);
        rightPanel.add(rightScrollpane,"0,1,5,1,f,f");
        selectedTable.setToolTipRenderer(new RaplaToolTipRenderer());
        selectedTable.getSelectionModel().addListSelectionListener(listener);
        selectedTable.setGridColor( RaplaColorList.darken( tableBackground, 20 ));
        selectedTable.setDefaultRenderer(Appointment[].class,new RestrictionCellRenderer());
        selectedTable.addMouseListener(listener);

        selectedTable.setDefaultEditor(Appointment[].class,appointmentCellEditor);
        completeTable.getTree().setCellRenderer(new AllocationTreeCellRenderer(false));
        selectedTable.getTree().setCellRenderer(new AllocationTreeCellRenderer(true));
        completeTable.getColumnModel().getColumn(0).setMinWidth(60);
        completeTable.getColumnModel().getColumn(0).setPreferredWidth(120);
        completeTable.getColumnModel().getColumn(1).sizeWidthToFit();
        selectedTable.getColumnModel().getColumn(0).setMinWidth(60);
        selectedTable.getColumnModel().getColumn(0).setPreferredWidth(120);
        selectedTable.getColumnModel().getColumn(1).sizeWidthToFit();
        content.setDividerLocation(0.3);

        calendarModel =(CalendarSelectionModel)(( CalendarSelectionModel)getService( CalendarSelectionModel.ROLE)).clone();
        filterAction = new FilterAction(getContext(),getComponent(),null);
        filterAction.setFilter( calendarModel );
        filterAction.setResourceOnly( true );
   }


    public void addChangeListener(ChangeListener listener) {
        listenerList.add(ChangeListener.class,listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        listenerList.remove(ChangeListener.class,listener);
    }

    final private TreeFactory getTreeFactory() {
        return (TreeFactory) getService(TreeFactory.ROLE);
    }

    protected void fireAllocationsChanged() {
        ChangeEvent evt = new ChangeEvent(this);
        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==ChangeListener.class) {
                ((ChangeListener)listeners[i+1]).stateChanged(evt);
            }
        }
    }

    public void refresh(ModificationEvent evt) throws RaplaException {
        boolean updateBindings = false;
        {
            if (evt.isModified( Allocatable.TYPE )) {
                updateBindings = true;
                completeModel.setAllocatables( getAllAllocatables(), completeTable.getTree());
            }
        }
        {
            Set allocatables = new HashSet(selectedModel.getAllocatables());
            Set removed = evt.getRemoved(allocatables);
            Set changed = evt.getChanged(allocatables);
            if (changed.size()>0 || removed.size()>0) {
                updateBindings = true;
                Iterator it = removed.iterator();
                while (it.hasNext()) {
                    Object obj = it.next();
                    mutableReservation.removeAllocatable( (Allocatable) obj );
                    allocatables.remove( obj );
                }

                selectedModel.setAllocatables(allocatables, selectedTable.getTree());
            }
            updateButtons();
        }
        if (updateBindings
            || evt.isModified( Reservation.TYPE))
            updateBindings(mutableReservation.getAppointments());

    }

    /** Implementation of appointment listener */
    public void appointmentAdded(Appointment appointment) {
        setAppointments(mutableReservation.getAppointments());
        updateBindings(new Appointment[] {appointment});
    }

    public void appointmentChanged(Appointment appointment) {
        setAppointments(mutableReservation.getAppointments());
        updateBindings(new Appointment[] {appointment});
    }

    private void updateBindings(Appointment[] appointments) {
        try {
            //      System.out.println("getting allocated resources");
            for (int i=0;i<appointments.length;i++) {
                Appointment appointment = appointments[i];
                Set allocatables = new HashSet(Arrays.asList(getQuery().getAllocatableBindings(appointment)));
                appointmentMap.put(appointment,allocatables);
            }
            completeModel.treeDidChange();
            selectedModel.treeDidChange();
        } catch (RaplaException ex) {
            showException(ex,content);
        }
    }

    public void appointmentRemoved(Appointment appointment) {
        appointmentMap.remove(appointment);
        setAppointments(mutableReservation.getAppointments());
        updateBindings(appointments);
    }

    public JComponent getComponent() {
        return content;

⌨️ 快捷键说明

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