/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package getfacts.full;
import getfacts.Article;
import getfacts.ColorSet;
import getfacts.ConfigurationSafe;
import getfacts.IDisposable;
import getfacts.toolkit.ImagePanel;
import getfacts.toolkit.MiscToolkit;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
/**
*
* @author jmc15
*/
public class DefaultArticlePane
extends javax.swing.JPanel
implements ComponentListener, MouseListener, IDisposable
{
private final Article article;
private final JLabel articleTitleLabel;
private final OneTwoThirdsLayoutManager globalLayout;
private final JEditorPane articleContentEditorPane;
private Graphics2D graphics;
//private Border BorderFactory;
private Paint backgroundGradient;
private ImagePanel imagePanel;
private ColorSet colorSet = ConfigurationSafe.getColorSet();
private int orientation;
/**
* Creates new form DefaultArticlePane
*/
public DefaultArticlePane(Article a, int o)
{
orientation = o;
article = a;
backgroundGradient = colorSet.get(ColorSet.MAIN_BACKGROUND_COLOR_KEY);
initComponents();
addComponentListener(this);
globalLayout = new OneTwoThirdsLayoutManager(OneTwoThirdsLayoutManager.TOP);
this.setLayout(globalLayout);
articleTitleLabel = new JLabel();
articleTitleLabel.addComponentListener(this);
articleTitleLabel.addMouseListener(this);
articleTitleLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
this.add(articleTitleLabel, OneTwoThirdsLayoutManager.ONE_THIRD);
JPanel innerArticlePanel = new JPanel();
OneTwoThirdsLayoutManager innerArticleLayout = new OneTwoThirdsLayoutManager(OneTwoThirdsLayoutManager.LEFT);
innerArticlePanel.setLayout(innerArticleLayout);
this.add(innerArticlePanel, OneTwoThirdsLayoutManager.TWO_THIRDS);
articleContentEditorPane = new JEditorPane();
articleContentEditorPane.setEditable(false);
innerArticlePanel.add(articleContentEditorPane, OneTwoThirdsLayoutManager.TWO_THIRDS);
articleTitleLabel.setText( article.getTitle() );
//jScrollPane2.setBackground(this.getBackground() );
articleContentEditorPane.setContentType("text/html");
articleContentEditorPane.setText(article.getContent());
if( article.getImageFile() != null )
{
imagePanel = new ImagePanel(article.getImageFile());
imagePanel.setBackground(Color.WHITE);
imagePanel.setAlignmentX(1);
innerArticlePanel.add(imagePanel, OneTwoThirdsLayoutManager.ONE_THIRD);
}
this.validate();
this.repaint();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setBackground(new java.awt.Color(255, 153, 255));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 420, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 311, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
/*private void ResizeTitleLabel(JLabel l)
{
if (graphics==null) {
return;
}
Rectangle r=new Rectangle(l.getSize());
int fontSize=MIN_FONT_SIZE;
Font f=l.getFont();
//Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
while (fontSize<MAX_FONT_SIZE)
{
//r1.setSize(getTextSize(l, f.deriveFont(f.getStyle(), fontSize)));
r2.setSize(getTextSize(l, f.deriveFont(f.getStyle(),fontSize+1)));
if ( ! r.contains(r2)) {
break;
}
fontSize++;
}
l.setFont(f.deriveFont(f.getStyle(),fontSize));
repaint();
}
private Dimension getTextSize(JLabel l, Font f) {
Dimension size=new Dimension();
//graphics.setFont(f);
FontMetrics fm=graphics.getFontMetrics(f);
//size.width=fm.stringWidth(l.getText());
//size.height=fm.getHeight();
Rectangle2D bounds = fm.getStringBounds(l.getText(), graphics);
size.width = (int)bounds.getWidth();
size.height = (int)bounds.getHeight();
return size;
}*/
@Override
protected void paintComponent(Graphics g)
{
//super.paintComponent(g);
graphics=(Graphics2D)g;
if( isOpaque() )
{
//GradientPaint backgroundGradient = new GradientPaint(0,0,Color.WHITE, 0, getHeight(), Color.GRAY);
graphics.setPaint(backgroundGradient);
graphics.fillRect(0, 0, getWidth(), getHeight());
}
}
@Override
public void componentResized(ComponentEvent ce)
{
if( ce.getComponent() == articleTitleLabel )
{
MiscToolkit.ResizeLabel(graphics, articleTitleLabel, FullScreenFrame.MIN_FONT_SIZE, FullScreenFrame.MAX_FONT_SIZE);
}
else if(ce.getComponent() == this )
{
Color c1;
Color c2;
if( article.isNew() )
{
c1 = colorSet.get(ColorSet.NEW_GRADIENT_COLOR_KEY);
c2 = colorSet.get(ColorSet.MAIN_BACKGROUND_COLOR_KEY);
}
else
{
c1 = colorSet.get(ColorSet.OLD_GRADIENT_COLOR_KEY);
c2 = colorSet.get(ColorSet.MAIN_BACKGROUND_COLOR_KEY);
}
switch(orientation)
{
default:
case OneTwoThirdsLayoutManager.LEFT:
backgroundGradient = new GradientPaint(0, 0, c1, getWidth(), 0/*getHeight()/3*/, c2);
break;
case OneTwoThirdsLayoutManager.RIGHT:
backgroundGradient = new GradientPaint(0, 0, c2, getWidth(), 0/*getHeight()/3*/, c1);
break;
}
}
}
@Override
public void componentMoved(ComponentEvent ce) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentShown(ComponentEvent ce) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentHidden(ComponentEvent ce) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseClicked(MouseEvent me)
{
if( me.getComponent()==articleTitleLabel)
{
String url = article.getUrl();
if( url!= null )
{
try
{
Desktop.getDesktop().browse( new URI(url) );
}
catch (IOException ex)
{
Logger.getLogger(DefaultArticlePane.class.getName()).log(Level.SEVERE, null, ex);
}
catch (URISyntaxException ex)
{
Logger.getLogger(DefaultArticlePane.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
@Override
public void mousePressed(MouseEvent me)
{
if( me.getComponent()==articleTitleLabel)
{
articleTitleLabel.setBorder( new BevelBorder(BevelBorder.LOWERED) );
}
}
@Override
public void mouseReleased(MouseEvent me)
{
if( me.getComponent()==articleTitleLabel)
{
articleTitleLabel.setBorder( null );
}
}
@Override
public void mouseEntered(MouseEvent me)
{
if( me.getComponent()==articleTitleLabel)
{
articleTitleLabel.setOpaque(true);
repaint();
}
}
@Override
public void mouseExited(MouseEvent me)
{
if( me.getComponent()==articleTitleLabel)
{
articleTitleLabel.setOpaque(false);
repaint();
}
}
public void dispose()
{
if(imagePanel != null)
{
imagePanel.dispose();
imagePanel = null;
}
graphics = null;
}
}