org.yccheok.jstock.gui.CompanyNews.java Source code

Java tutorial

Introduction

Here is the source code for org.yccheok.jstock.gui.CompanyNews.java

Source

/*
 * JStock - Free Stock Market Software
 * Copyright (C) 2014 Yan Cheng Cheok <yccheok@yahoo.com>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.yccheok.jstock.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.BorderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author schern
 */
public class CompanyNews extends javax.swing.JFrame {

    /**
     * 
     * @param ticker
     * @param co 
     */

    public CompanyNews(String ticker, String co) {
        symbol = ticker;
        title = co + " (" + ticker + ")";
        initComponents();
    }

    /**
     * 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() {

        setTitle(title);
        setIconImage(getMyIconImage());
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new BorderLayout());
        getContentPane().add(jPanel1);

        jTable1 = new JTable();
        jTable1.setModel(NTModel);
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setBorder(BorderFactory.createEmptyBorder());

        jTable1.getTableHeader().setFont(jTable1.getFont()
                .deriveFont(jTable1.getFont().getStyle() | java.awt.Font.BOLD, jTable1.getFont().getSize() + 1));
        jTable1.getTableHeader().setBackground(headerBG);
        jTable1.getTableHeader().setForeground(Color.GRAY);
        jTable1.addMouseListener(new gotoNews());

        jTable1.setPreferredScrollableViewportSize(new Dimension(500, 250));
        jScrollPane1 = new JScrollPane();

        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, BorderLayout.CENTER);

        this.pack();
        //this.setVisible(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 505, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 295, Short.MAX_VALUE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void getCoNews() {
        //hide the url column
        if (jTable1.getColumnCount() > 0) {
            TableColumn colToDel = jTable1.getColumnModel().getColumn(jTable1.getColumnCount() - 1);
            jTable1.removeColumn(colToDel);
            jTable1.validate();
        }

        NewsTModel ntm = (NewsTModel) jTable1.getModel();
        ntm.getCoNews(symbol);

    }

    private class gotoNews extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                int rowSelected = jTable1.getSelectedRow();
                //int modelIndex = jTable1.convertRowIndexToModel(rowSelected);
                NewsTModel ntm = (NewsTModel) jTable1.getModel();
                Object urlObject = ntm.getValueAt(rowSelected, linkColumn);
                URL link = new URL(urlObject.toString());
                openWebpage(link);
            } catch (MalformedURLException malExp) {
                log.error(null, malExp);
            }
        }

    }

    static void openWebpage(URI uri) {
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(uri);
            } catch (Exception e) {
                log.error(null, e);
            }
        }
    }

    public static void openWebpage(URL url) {
        try {
            openWebpage(url.toURI());
        } catch (URISyntaxException e) {
            log.error(null, e);
        }
    }

    private Image getMyIconImage() {
        if (Utils.isWindows7() || Utils.isWindows8()) {
            return new javax.swing.ImageIcon(getClass().getResource("/images/128x128/chart.png")).getImage();
        }
        return new javax.swing.ImageIcon(getClass().getResource("/images/16x16/chart.png")).getImage();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable jTable1;
    private javax.swing.JScrollPane jScrollPane1;
    private NewsTModel NTModel = new NewsTModel();
    private Color headerBG = new Color(204, 229, 255);
    // End of variables declaration//GEN-END:variables

    private final String title;
    private final String symbol;
    private final int linkColumn = 2;

    private static final Log log = LogFactory.getLog(IndicatorPanel.class);
}