viewtable.java

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

JAVA
134
字号
/*--------------------------------------------------------------------------*
 | 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.view;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import org.rapla.components.util.Assert;
import org.rapla.entities.RaplaObject;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.toolkit.HTMLView;
import org.rapla.gui.toolkit.RaplaWidget;

/**Information of the entity-classes displayed in an HTML-Component */
public class ViewTable extends RaplaGUIComponent
    implements
        HyperlinkListener
        ,RaplaWidget
        ,LinkController
{
    String title;
    HTMLView htmlView = new HTMLView();
    JScrollPane pane = new JScrollPane(htmlView, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER) {
        private static final long serialVersionUID = 1L;
        
        public Dimension getPreferredSize() {
            Dimension pref = super.getPreferredSize();
            Dimension max = getMaximumSize();
            //System.out.println( "PREF: " + pref + "  MAX: " + max);
            if  ( pref.height > max.height )
                return max;
            else
                return pref;
        }
    };
    Map linkMap;
    int linkId = 0;
    boolean packText = true;

    public ViewTable(RaplaContext sm) throws RaplaException {
        super( sm);
        linkMap = new HashMap(7);
        htmlView.addHyperlinkListener(this);
        htmlView.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        pane.setMaximumSize( new Dimension( 600, 500 ));
    }

    /** HTML-text-component should be sized according to the displayed text. Default is true. */
    public void setPackText(boolean packText) {
        this.packText = packText;
    }

    public JComponent getComponent() {
        return pane;
    }

    public String getDialogTitle() {
        return title;
    }

    public void updateInfo(Object object) throws RaplaException {
        updateInfo(object,((InfoFactoryImpl)getInfoFactory()).createView((RaplaObject)object));
    }

    public void updateInfo(Object object, HTMLInfo info) throws RaplaException {
        linkMap.clear();
        if (object !=null ) {
            setText( info.getHTML( object, this));
        } else {
            setText(getString("nothing_selected"));
            htmlView.revalidate();
            htmlView.repaint();
        }
    }

    public void setTitle(String text) {
        this.title = text;
    }

    public void setText(String text) {
        String message = HTMLView.createHTMLPage(text);
        htmlView.setText(message, packText);
    }

    public void createLink(Object object,String link,StringBuffer buf) {
        linkMap.put(new Integer(linkId),object);
        buf.append("<A href=\"");
        buf.append(linkId++);
        buf.append("\">");
        HTMLInfo.encode(link,buf);
        buf.append("</A>");
    }

    public String createLink(Object object,String link) {
        StringBuffer buf = new StringBuffer();
        createLink(object,link,buf);
        return  buf.toString();
    }

    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            String link = e.getDescription();
            getLogger().debug("Hyperlink pressed: " + link);
            Object object = linkMap.get(new Integer(link));
            Assert.notNull(object,"link was not found in linkMap");
            Assert.notNull(getInfoFactory());
            try {
                getInfoFactory().showInfoDialog(object,htmlView);
            } catch (RaplaException ex) {
                showException(ex,getComponent());
            } // end of try-catch
        }
    }
}

⌨️ 快捷键说明

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