autoexportoption.java

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

JAVA
242
字号
/*--------------------------------------------------------------------------*
 | 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.autoexport;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.avalon.framework.configuration.ConfigurationException;
import org.rapla.components.layout.TableLayout;
import org.rapla.entities.configuration.CalendarModelConfiguration;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.framework.StartupEnvironment;
import org.rapla.gui.OptionPanel;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.internal.common.CalendarModelImpl;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.internal.print.PrintAction;

public class AutoExportOption extends RaplaGUIComponent implements OptionPanel,ListSelectionListener {
    JPanel content= new JPanel();
    JList list;
    JPanel buttonPanel;
    JTextArea urlLabel;
    
    JButton previewButton;
    JButton deleteButton;
    ArrayList settingsList;
    DeleteAction deleteAction;
    PrintAction printAction;
    Preferences preferences;
    boolean addButtons = true;

    public AutoExportOption( RaplaContext sm) throws RaplaException {
        super( sm);
        setChildBundleName( AutoExportPlugin.RESOURCE_FILE);
    }

    public void create() throws RaplaException 
    {
        list = new JList();
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        previewButton = new JButton();
        deleteButton = new JButton();
        buttonPanel.add(previewButton);
        buttonPanel.add(deleteButton);
        double[][] sizes = new double[][] {
            {5,TableLayout.FILL,5}
            ,{TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.FILL, 5, TableLayout.PREFERRED}
        };
        TableLayout tableLayout = new TableLayout(sizes);
        content.setLayout(tableLayout);
        content.add(new JLabel("<html>" + getString("autoexport.option.description") +"</html>"), "1,0");
        if ( addButtons )
        {
            content.add(buttonPanel, "1,2");
        }
        content.add(new JScrollPane(list),"1,4");
        JPanel status = new JPanel();
        status.setLayout( new BorderLayout());
        urlLabel = new JTextArea();
        urlLabel.setText( "");
        urlLabel.setEditable( false );
        urlLabel.setFont( urlLabel.getFont().deriveFont( (float)10.0));
        status.add( new JLabel("URL: "), BorderLayout.WEST );
        status.add( urlLabel, BorderLayout.CENTER );
        content.add( status,"1,6");

        printAction =  new PrintAction(getContext());
        deleteAction = new DeleteAction();
        deleteButton.setAction(deleteAction);
        previewButton.setAction(printAction);
        list.addListSelectionListener(this);
    }

    public JComponent getComponent() {
        return content;
    }

    public String toString() {
        return getString("autoexport");
    }

    String getAddress(String filename) {
        if ( filename == null) 
        {
            return "";
        }
        try 
        {
            StartupEnvironment env = (StartupEnvironment)getContext().lookup( StartupEnvironment.ROLE );
            URL codeBase = env.getDownloadURL();
            filename =URLEncoder.encode( filename, "UTF-8" );
            return new URL( codeBase,"rapla?page=calendar&user=" + getUser().getUsername() + "&file=" + filename).toExternalForm();
        } 
        catch (Exception ex)
        {
            return "Not in webstart mode. Exportname is " + filename  ;
        }
    }

    private void delete(List deleteList) throws RaplaException,ConfigurationException {
        if ( deleteList.size() <= 0)
            return;
        Map exportMap= ((RaplaMap)preferences.getEntry(AutoExportPlugin.PLUGIN_ENTRY));
        Map newMap = new TreeMap();
        for (Iterator it= exportMap.keySet().iterator();it.hasNext();) {
            String filename = (String)it.next();
            if (!deleteList.contains(filename)) {
                newMap.put( filename, exportMap.get( filename ));
            }
        }
        preferences.putEntry( AutoExportPlugin.PLUGIN_ENTRY, getModification().newRaplaMap( newMap ));
        updateList();
    }

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


    public void show() throws RaplaException {
        if (list== null)
            create();
        updateList();
    }

    private void updateList() throws RaplaException {
        settingsList = new ArrayList();
        DefaultListModel model = new DefaultListModel();
        Map exportMap= ((RaplaMap)preferences.getEntry(AutoExportPlugin.PLUGIN_ENTRY));
        if ( exportMap != null) {
            for (Iterator it= exportMap.keySet().iterator();it.hasNext();) {
                String filename = (String) it.next();
                CalendarModelConfiguration conf = (CalendarModelConfiguration)exportMap.get( filename );
                CalendarModelImpl calendarModel = new CalendarModelImpl(getContext());
                calendarModel.setConfiguration( conf );
                settingsList.add( calendarModel );
                model.addElement(filename);
            }
        }
        list.setModel(model);
    }

    public void commit() {
    }

    public CalendarSelectionModel getSelectedCalendarModel() {
        int selectedIndex = list.getMinSelectionIndex();
        if (selectedIndex>=0)
        {
            return (CalendarSelectionModel)settingsList.get(selectedIndex);
        }
        else
        {
            return null;
        }
    }

    public String getSelectedFilename() {
        return (String) list.getSelectedValue(  );
    }

    public void valueChanged(ListSelectionEvent e) {
        deleteAction.update();
        printAction.setModel( getSelectedCalendarModel() );
        urlLabel.setText( getAddress( getSelectedFilename() ));
    }

    class DeleteAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        DeleteAction() {
            putValue(NAME,getString("delete"));
            putValue(SMALL_ICON,getIcon("icon.delete"));
            setEnabled(false);
        }

        void update() {
            setEnabled(list.getSelectedIndex()>=0);
        }

        public void actionPerformed(ActionEvent evt) {
            try {
                ArrayList deleteList = new ArrayList();
                int size = list.getModel().getSize();
                for (int i=0;i<size;i++) {
                    if (list.isSelectedIndex(i)) {
                        deleteList.add(list.getModel().getElementAt(i));
                    }
                }
                delete(deleteList);
            } catch (Exception ex) {
                showException(ex,getComponent());

            }

        }
    }

    /**
     * @see org.rapla.entities.Named#getName(java.util.Locale)
     */
    public String getName(Locale locale) {
        return toString();
    }
}

⌨️ 快捷键说明

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