License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package rssviewer.view;

import javafx.scene.input.MouseEvent;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

import rssviewer.model.Item;
import rssviewer.Main;

/**
 * View to display channel items
 */

public def height = 72;
public def width = 235;

public class ListItem extends CustomNode { 
    override var translateX = 4;
    public var item:Item on replace {
        if(item != null) {
            title.content = trimString(item.title, 80);
            date.content = "{ item.pubDate }";
            visible = true;
        } else {
            visible = false;
        }
    };
    
    var title = Text {
        x: 10
        y: 20
        font:Font {
            name: "Bitstream Vera Sans Bold"
            size: 11
        }
        fill: Color.LIGHTGRAY
        wrappingWidth: 210
        content: "TITLE"
    };
    
    var date = Text {
        x: 10
        y: height - 8
        font:Font {
            name: "Bitstream Vera Sans Bold"
            size: 9
        }
        fill: Color.GRAY
        content: "DATE"
    };
    
    var bgRect = Rectangle {
        x: 0
        y: 0
        width: width
        height: height
        fill:LinearGradient {
            startX: 0.0,
            startY: 0.0,
            endX: 0.0,
            endY: height
            proportional: false
            stops: [ 
                Stop {
                    offset: 0.0
                    color: Color.BLACK
                },
                Stop {
                    offset: 0.25
                    color: Color.rgb(50, 50, 50)
                },
                Stop {
                    offset: 1.0
                    color: Color.BLACK
                }
            ]
        }
        opacity: 0.2
    }
    
    var hrLine = Line {
        startX: 5
        startY: height
        endX: width - 5
        endY: height
        strokeWidth: 2.0
        stroke:LinearGradient {
            startX: 0.0,
            startY: 0.0,
            endX: width,
            endY: 0.0
            proportional: false
            stops: [ 
                Stop {
                    offset: 0.0
                    color: Color.BLACK
                },
                Stop {
                    offset: 0.5
                    color: Color.GRAY
                },
                Stop {
                    offset: 1.0
                    color: Color.BLACK
                }
            ]
        };
    }
    
    override function create():Node {
       Group { 
            content: [ bgRect, title, date, hrLine ] 
            visible: true
        };
    }
    
    override var onMouseEntered = function(e:MouseEvent) {
        bgRect.opacity = 0.9;
    }
    
    override var onMouseExited = function(e:MouseEvent) {
        bgRect.opacity = 0.2;
    }
    
    override var onMousePressed = function(e:MouseEvent) {
       if(item.link == null) { return; }
           if(Main.inBrowser) {
               var ase = javafx.stage.AppletStageExtension {};
                 ase.showDocument(item.link, "_blank");
            } else { // Use JDK 6 API
            /**
             * Invoke java.awt.Desktop.browse(< URI >) using Reflection
             */
            try {
                var uri = new java.net.URI("{item.link}");
                var desktopClazz = java.lang.Class.forName("java.awt.Desktop");
                var getDesktopMethod = desktopClazz.getMethod("getDesktop");
                var desktop = getDesktopMethod.invoke(null);
                var browseMethod = desktopClazz.getMethod("browse", [uri.getClass()] as java.lang.Class[]);
                browseMethod.invoke(desktop, uri);
            } catch (exp : java.lang.Exception) {
                // JDK 6 API not available! - Do nothing.
            }
        }
    }
    
}

// Trim the string if length is greater than specified length
public function trimString(string:String, length:Integer):String {
    if(string == null) return "";
        
    var plainString = extractPlainText(string);
    if(plainString.length() > length) { 
        return "{plainString.substring(0, length).trim()}..."; 
    } else {
        return plainString;
    }
}

/**
 * Very basic implementation to remove HTML tags
 * and return just the plain text contents
 */
function extractPlainText(string:String):String {
    var startIndex = 0; 
    var endIndex = 0;
    var index = -1;
    var returnString = string;
    var buffer:java.lang.StringBuffer; 
    
    while(true) {
        index = returnString.indexOf("&#");
        if(index != -1) {
            endIndex = returnString.indexOf(";", index);
            var ascii = returnString.substring(index + 2, endIndex);
            var unicode = "";
            try {
                println("ASCII {ascii}");
                var asciiInt = java.lang.Integer.parseInt(ascii);
                unicode = new java.lang.String(java.lang.Character.toChars(asciiInt));
            } catch (e:java.lang.Exception) { }
            buffer = new java.lang.StringBuffer();
            buffer.append(returnString.substring(0, index));
            buffer.append(unicode);
            buffer.append(returnString.substring((endIndex + 1), returnString.length()));
            returnString = buffer.toString();
            startIndex = endIndex + 1;
        } else {
            break;
        }
    }
    
    return returnString.replaceAll("&quot;", "\"");
}