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

📄 notificationoption.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.plugin.notification;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.avalon.framework.configuration.ConfigurationException;
import org.rapla.components.layout.TableLayout;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.entities.domain.Allocatable;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.OptionPanel;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.TreeFactory;
import org.rapla.gui.toolkit.DialogUI;
import org.rapla.gui.toolkit.RaplaTree;

public class NotificationOption extends RaplaGUIComponent implements OptionPanel,ChangeListener {
    final static String ALLOCATIONLISTENERS_CONFIG = NotificationService.ALLOCATIONLISTENERS_CONFIG;
    final static String NOTIFY_IF_OWNER_CONFIG =  NotificationService.NOTIFY_IF_OWNER_CONFIG;
    boolean state = false;
    JPanel content= new JPanel();
    RaplaTree treeSelection;
    JPanel buttonPanel;
    JButton previewButton;
    JButton deleteButton;
    JButton addButton;
    JCheckBox notifyIfOwnerCheckBox;
    NotificationAction deleteAction;
    NotificationAction addAction;
    NotificationAction notifyIfOwnerAction;
    Preferences preferences;

    public NotificationOption(RaplaContext sm) throws RaplaException {
        super( sm);
        setChildBundleName( NotificationPlugin.RESOURCE_FILE);
    }

    public void create() throws RaplaException {
        treeSelection = new RaplaTree();
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton();
        addButton = new JButton();
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        double[][] sizes = new double[][] {
            {5,TableLayout.FILL,5}
            ,{TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.FILL}
        };
        TableLayout tableLayout = new TableLayout(sizes);
        content.setLayout(tableLayout);
        content.add(new JLabel(getStringAsHTML("notification.option.description")), "1,0");
        notifyIfOwnerCheckBox = new JCheckBox();
        content.add(notifyIfOwnerCheckBox, "1,2");
        content.add(buttonPanel, "1,4");
        content.add(treeSelection,"1,6");

        deleteAction = new NotificationAction().setDelete();
        addAction = new NotificationAction().setAdd();
        notifyIfOwnerAction = new NotificationAction().setNotifyIfOwner();
        deleteButton.setAction(deleteAction);
        addButton.setAction(addAction);
        notifyIfOwnerCheckBox.setAction(notifyIfOwnerAction);
        treeSelection.addChangeListener(this);
    }

    public JComponent getComponent() {
        return content;
    }
    public String getName(Locale locale) {
        return getString("notification_options");
    }

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

    private void delete(List selection)  throws RaplaException,ConfigurationException {
        RaplaMap allocatableList = (RaplaMap) preferences.getEntry(ALLOCATIONLISTENERS_CONFIG);
        if ( allocatableList == null)
            return;
        Set set= new HashSet();
        for (Iterator it = allocatableList.values().iterator();it.hasNext();) {
            Object allocatable = it.next();
            if ( !selection.contains( allocatable ) ) {
                set.add( allocatable);
            }
        }
        preferences.putEntry( ALLOCATIONLISTENERS_CONFIG, getModification().newRaplaMap( set) );
        update();
    }


    public void show() throws RaplaException {
        if (treeSelection== null)
            create();
        update();
    }

    private void update() throws RaplaException {
        RaplaMap allocatableList = (RaplaMap) preferences.getEntry(ALLOCATIONLISTENERS_CONFIG);
        notifyIfOwnerAction.setEnabled(false);

        boolean notify = preferences.getEntryAsBoolean( NOTIFY_IF_OWNER_CONFIG, false);
        notifyIfOwnerCheckBox.setSelected(notify);
        notifyIfOwnerAction.setEnabled(true);

        treeSelection.getTree().setCellRenderer(getTreeFactory().createRenderer());
        Allocatable[] allocatables = Allocatable.ALLOCATABLE_ARRAY;
        if ( allocatableList!= null) {
            Set set = new HashSet( allocatableList.values());
            allocatables = (Allocatable[]) set.toArray( Allocatable.ALLOCATABLE_ARRAY);
        }

        treeSelection.exchangeTreeModel(getTreeFactory().createClassifiableModel(allocatables));
    }



    private void updateNotifyIfOwner() throws RaplaException, ConfigurationException {
        preferences.putEntry( NOTIFY_IF_OWNER_CONFIG,  String.valueOf(notifyIfOwnerCheckBox.isSelected()));
    }

    private void add(Allocatable allocatable) throws ConfigurationException, RaplaException {
        RaplaMap raplaEntityList = (RaplaMap)preferences.getEntry( ALLOCATIONLISTENERS_CONFIG );
        Set list;
        if ( raplaEntityList != null ){
            list = new HashSet(raplaEntityList.values());
        } else {
            list = new HashSet();
        }
        list.add( allocatable );
        getLogger().info("Adding notificationEntry " + allocatable);
        preferences.putEntry( ALLOCATIONLISTENERS_CONFIG ,getModification().newRaplaMap( list ));
        update();
    }


    public void setPreferences(Preferences preferences) {
        this.preferences = preferences;
    }

    public void commit() {
    }


    public void stateChanged(ChangeEvent e) {
        deleteAction.update();
        //addAction.update();
    }

    class NotificationAction extends AbstractAction {
        private static final long serialVersionUID = 1L;
        
        List selection;
        int ADD = 1;
        int DELETE = 2;
        int NOTIFY_IF_OWNER = 3;
        int type;

        NotificationAction setDelete() {
            putValue(NAME,getString("delete"));
            putValue(SMALL_ICON,getIcon("icon.delete"));
            setEnabled(false);
            type = DELETE;
            return this;
        }

        NotificationAction setAdd() {
            putValue(NAME,getString("add"));
            putValue(SMALL_ICON,getIcon("icon.new"));
            type = ADD;
            return this;
        }

        NotificationAction setNotifyIfOwner() {
            putValue(NAME,getStringAsHTML("notify_if_owner"));
            type = NOTIFY_IF_OWNER;
            return this;
        }

        void update() {
            selection = treeSelection.getSelectedElements();
            setEnabled(selection.size() > 0);
        }

        public void actionPerformed(ActionEvent evt) {
            try {
                if (type == DELETE) {
                    delete(selection);
                } else if (type == NOTIFY_IF_OWNER) {
                    updateNotifyIfOwner();
                } else if (type == ADD) {
                    showAddDialog();
                }
            } catch (Exception ex) {
                showException(ex,getComponent());
            }
        }

        private void showAddDialog() throws RaplaException, ConfigurationException {
            DialogUI dialog;
            RaplaTree treeSelection = new RaplaTree();
            treeSelection.setMultiSelect(true);
            treeSelection.getTree().setCellRenderer(getTreeFactory().createRenderer());

            treeSelection.exchangeTreeModel(getTreeFactory().createClassifiableModel(getQuery().getAllocatables()));
            treeSelection.setMinimumSize(new java.awt.Dimension(300, 200));
            treeSelection.setPreferredSize(new java.awt.Dimension(400, 260));
            dialog = DialogUI.create(
                    getContext()
                    ,getComponent()
                    ,true
                    ,treeSelection
                    ,new String[] { getString("add"),getString("cancel")});
            dialog.setTitle("");
            dialog.start();
            if (dialog.getSelectedIndex() == 0) {
                Iterator it = treeSelection.getSelectedElements().iterator();
                while (it.hasNext()) {
                    add((Allocatable) it.next());
                }
            }
        }
    }

}

⌨️ 快捷键说明

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