Java tutorial
package view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.geom.Ellipse2D; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.StringTokenizer; import javax.swing.*; import javax.swing.border.Border; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.text.DefaultCaret; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.time.Day; import org.jfree.data.time.RegularTimePeriod; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.util.ShapeUtilities; import model.DailyData; import model.SpreadData; import model.SpreadObject; import controller.Controller; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class App extends javax.swing.JFrame { { //Set Look & Feel try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } } private JMenuItem helpMenuItem; private JMenu jMenu5; private JPanel jPanel3; private JPanel jPanel2; private JPanel jPanel1; private JMenuItem pasteMenuItem; private JMenuItem copyMenuItem; private JMenuItem cutMenuItem; private JMenu jMenu4; private JMenuItem exitMenuItem; private JSeparator jSeparator2; private JTextArea jTextArea1; private JPanel jPanel5; private JMenuItem newFileMenuItem; private JMenu jMenu3; private JMenuBar jMenuBar1; private JScrollPane jScrollPane1; private JTable jTable1; private JPanel jPanel6; private JButton jButton1; private JPanel jPanel4; private JFreeChart chart = null; private DateAxis axis; private JScrollPane jScrollPane2; // /** // * Auto-generated main method to display this JFrame // */ // public static void main(String[] args) { // SwingUtilities.invokeLater(new Runnable() { // public void run() { // App inst = new App(); // inst.setLocationRelativeTo(null); // inst.setVisible(true); // } // }); // } public App() { super("Pair Trading Screener"); initGUI(); } private void initGUI() { try { { jPanel1 = new JPanel(); BorderLayout jPanel1Layout = new BorderLayout(); jPanel1.setLayout(jPanel1Layout); getContentPane().add(jPanel1, BorderLayout.CENTER); jPanel1.setPreferredSize(new java.awt.Dimension(901, 398)); { jPanel2 = new JPanel(); BoxLayout jPanel2Layout = new BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS); jPanel2.setLayout(jPanel2Layout); jPanel1.add(jPanel2, BorderLayout.WEST); jPanel2.setPreferredSize(new java.awt.Dimension(292, 446)); { jPanel5 = new JPanel(); jPanel2.add(jPanel5); jPanel5.setPreferredSize(new java.awt.Dimension(292, 109)); { { jTextArea1 = new JTextArea(); jTextArea1.setWrapStyleWord(true); jTextArea1.setLineWrap(true); DefaultCaret caret = (DefaultCaret) jTextArea1.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); jTextArea1.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent evt) { if (jTable1.getModel().getRowCount() == 0 && !jButton1.isEnabled()) { jButton1.setEnabled(true); jTextArea1.setText(""); } } }); JScrollPane sp = new JScrollPane(); sp.setPreferredSize(new java.awt.Dimension(281, 97)); sp.setViewportView(jTextArea1); jPanel5.add(sp, BorderLayout.CENTER); } } } { jPanel4 = new JPanel(); jPanel2.add(jPanel4); FlowLayout jPanel4Layout = new FlowLayout(); jPanel4Layout.setAlignment(FlowLayout.RIGHT); jPanel4.setPreferredSize(new java.awt.Dimension(292, 45)); jPanel4.setSize(102, 51); jPanel4.setLayout(jPanel4Layout); { jButton1 = new JButton(); jPanel4.add(jButton1); jButton1.setText("Get Quotes"); jButton1.setSize(100, 50); jButton1.setPreferredSize(new java.awt.Dimension(100, 26)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // String tickerStr = jTextArea1.getText(); if (tickerStr.equals("") || tickerStr.equals(null) || tickerStr.equals(" ")) { jTextArea1.setText(" "); return; } StringTokenizer tokenizer = new StringTokenizer(tickerStr, " "); String[] tickers = new String[tokenizer.countTokens()]; int i = 0; while (tokenizer.hasMoreTokens()) { tickers[i] = tokenizer.nextToken(); i++; } try { Controller.getQuotes(tickers); } catch (CloneNotSupportedException e) { JOptionPane.showMessageDialog(jPanel1, " "); } jButton1.setEnabled(false); } }); } } { jPanel6 = new JPanel(); BorderLayout jPanel6Layout = new BorderLayout(); jPanel6.setLayout(jPanel6Layout); jPanel2.add(jPanel6); { jScrollPane1 = new JScrollPane(); jPanel6.add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.setPreferredSize(new java.awt.Dimension(292, 341)); { TableModel jTable1Model = new DefaultTableModel(null, new String[] { "", "MA Value", "", "MA Value" }); jTable1 = new JTable(); jScrollPane1.setViewportView(jTable1); jTable1.setModel(jTable1Model); jTable1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } } } } { jPanel3 = new JPanel(); BorderLayout jPanel3Layout = new BorderLayout(); jPanel3.setLayout(jPanel3Layout); jPanel1.add(jPanel3, BorderLayout.CENTER); { // chart = ChartFactory.createLineChart(" ", "dates", "correlation ratio", null, // PlotOrientation.VERTICAL, true, true, false); // ChartPanel chartPanel = new ChartPanel(chart); // chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) ); // jPanel3.add(chartPanel); } { } } } this.setSize(966, 531); { jMenuBar1 = new JMenuBar(); setJMenuBar(jMenuBar1); { jMenu3 = new JMenu(); jMenuBar1.add(jMenu3); jMenu3.setText("File"); { // newFileMenuItem = new JMenuItem(); // jMenu3.add(newFileMenuItem); // newFileMenuItem.setText("New"); // newFileMenuItem.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent evt) { //// jTextArea1.setText(""); //// DefaultTableModel model = (DefaultTableModel)jTable1.getModel(); //// model.setRowCount(0); //// Controller.clearPortfolio(); // } // }); } { jSeparator2 = new JSeparator(); jMenu3.add(jSeparator2); } { exitMenuItem = new JMenuItem(); jMenu3.add(exitMenuItem); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int action = JOptionPane.showConfirmDialog(jPanel1, " ?", "Confirm Exit", JOptionPane.OK_CANCEL_OPTION); if (action == JOptionPane.OK_OPTION) System.exit(0); } }); } } { jMenu4 = new JMenu(); jMenuBar1.add(jMenu4); jMenu4.setText("Edit"); { cutMenuItem = new JMenuItem(); jMenu4.add(cutMenuItem); cutMenuItem.setText("Cut"); cutMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String txt = jTextArea1.getText(); StringSelection selection = new StringSelection(txt); Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard(); clp.setContents(selection, null); jTextArea1.setText(""); } }); } { copyMenuItem = new JMenuItem(); jMenu4.add(copyMenuItem); copyMenuItem.setText("Copy"); copyMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String txt = jTextArea1.getText(); StringSelection selection = new StringSelection(txt); Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard(); clp.setContents(selection, null); } }); } { pasteMenuItem = new JMenuItem(); jMenu4.add(pasteMenuItem); pasteMenuItem.setText("Paste"); pasteMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard(); try { String data = (String) clp.getData(DataFlavor.stringFlavor); jTextArea1.setText(data); } catch (UnsupportedFlavorException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } } { jMenu5 = new JMenu(); jMenuBar1.add(jMenu5); jMenu5.setText("Help"); { helpMenuItem = new JMenuItem(); jMenu5.add(helpMenuItem); helpMenuItem.setText("About"); helpMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(jPanel1, " . r.zhumagulov@gmail.com", "About", JOptionPane.PLAIN_MESSAGE); } }); } } } } catch (Exception e) { e.printStackTrace(); } } public void addData(final List<SpreadObject> spreadList) { Object rows[][] = new Object[spreadList.size()][4]; for (int j = 0; j < spreadList.size(); j++) { SpreadObject currPair = spreadList.get(j); int lastIndexBase = currPair.getBaseStock().getData().size(); int lastIndexSec = currPair.getSecondStock().getData().size(); DailyData firstVal = currPair.getBaseStock().getData().get(lastIndexBase - 1); DailyData secVal = currPair.getSecondStock().getData().get(lastIndexSec - 1); // int size = currPair.getData().size(); rows[j][0] = currPair.getBaseStock().getTicker(); rows[j][1] = firstVal.getAverVolatile().toPlainString(); rows[j][2] = currPair.getSecondStock().getTicker(); rows[j][3] = secVal.getAverVolatile().toPlainString(); } DefaultTableModel model = new DefaultTableModel(rows, new String[] { "", "MA Value", "", "MA Value" }) { public boolean isCellEditable(int row, int column) { return false; }; void reset() { this.setColumnCount(0); } }; jTable1.setModel(model); jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent arg0) { int selectedRow = jTable1.getSelectedRow(); SpreadObject obj = spreadList.get(selectedRow); renderChart(obj); } }); } private void renderChart(SpreadObject obj) { try { jPanel3.removeAll(); jPanel3.revalidate(); chart = ChartFactory.createTimeSeriesChart( obj.getBaseStock().getTicker() + " : " + obj.getSecondStock().getTicker(), "date", "spread ratio", createDataset(obj), true, true, false); } catch (ParseException e) { System.out.println(" " + e.toString()); } final XYPlot plot = chart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesShape(0, new Ellipse2D.Double(-3, -3, 0, 0)); renderer.setSeriesPaint(0, Color.RED); plot.setRenderer(renderer); axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy")); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setDomainZoomable(true); chartPanel.setRangeZoomable(false); Border border = BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createEtchedBorder()); chartPanel.setBorder(border); jPanel3.add(chartPanel); jPanel3.revalidate(); } private XYDataset createDataset(SpreadObject obj) throws ParseException { TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeries series = new TimeSeries( obj.getBaseStock().getTicker() + " : " + obj.getSecondStock().getTicker()); List<SpreadData> data = obj.getData(); for (SpreadData item : data) { String date = item.getDate(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dateRes = df.parse(date); Day d = new Day(dateRes); RegularTimePeriod regTime = d.next(); series.add(regTime, item.getSpreadValue().doubleValue()); } dataset.addSeries(series); return dataset; } // private JScrollPane getJScrollPane2() { // if(jScrollPane2 == null) { // jScrollPane2 = new JScrollPane(); // { // jTextArea1 = new JTextArea(); // jScrollPane2.setViewportView(jTextArea1); // jTextArea1.setWrapStyleWord(true); // jTextArea1.setLineWrap(true); // DefaultCaret caret = (DefaultCaret)jTextArea1.getCaret(); // caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // // jTextArea1.setSize(292, 86); // jTextArea1.setPreferredSize(new java.awt.Dimension(281, 97)); // jPanel5.add(getJScrollPane2()); // jScrollPane2.setPreferredSize(new java.awt.Dimension(281, 97)); // // jTextArea1.addFocusListener(new FocusAdapter() { // public void focusGained(FocusEvent evt) { // // if(jTable1.getModel().getRowCount() == 0 && !jButton1.isEnabled()) { // jButton1.setEnabled(true); // jTextArea1.setText(""); // } // // } // }); // // } // } // return jScrollPane2; // } }