/* * Copyright (c) 2014 Trade Ideas LLC - All Right Reserved. * This source code is proprietary and confidential. * Unauthorized copying of this file, via any medium is strictly prohibited. * * $RCSfile: TradeIdeasForm.java,v $ * $Date: 2014/02/19 07:15:20 $ * $Revision: 1.4 $ */ package com.tradeideas.sample; import com.tradeideas.external.PanelPair; import com.tradeideas.external.TIMain; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JSeparator; /** * * @author phil */ public class TradeIdeasForm extends javax.swing.JFrame { /** * This is the way to access the TI library. There is a new one of these * objects for each window. As far as the form is concerned, this object * never changes, even though the user can completely reconfigure the * object. */ private final PanelPair panelPair; /** * Creates new form TradeIdeasForm */ public TradeIdeasForm() { initComponents(); // Create the TI panels and install them. A border layout is an easy // way to say the the panels from the library take the entire space of // the panels in this form. panelPair = TIMain.getTIPanels(); leftHeaderPanel.setLayout(new BorderLayout()); leftHeaderPanel.add(panelPair.getTopSmall(), BorderLayout.CENTER); clientPanel.setLayout(new BorderLayout()); clientPanel.add(panelPair.getClient(), BorderLayout.CENTER); // The window title uses normal property change listener. panelPair.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { updateWindowTitle(); } }); updateWindowTitle(); // This is a very simple custom interface. panelPair.setPopupListener(new PanelPair.PopupListener() { @Override public void onPopup(JPopupMenu menu, String symbol) { updateMenu(menu, symbol); } }); // This is a very simple custom interface. // We don't use the a normal property change listener because a normal // property change listener won't let you send the same message twice // in a row. If the user double-clicks on MSFT, and then does it again, // the TI library will call this twice in a row. It's possible that // the linked window changed to a different symbol and the TI library // doesn't know it. panelPair.setSymbolLinkingListener(new PanelPair.SymbolLinkingListener() { @Override public void onSymbolUpdate(String symbol) { JOptionPane.showMessageDialog(null, "The symbol is " + symbol, "Symbol Linking Test", JOptionPane.PLAIN_MESSAGE); } }); } /** * This is the last menu we have adorned with our menu items. This will be * null if we haven't processed any menu, or if the last menu was * invalidated. */ private JPopupMenu processedMenu; /** * This is a menu item that can be customized with the current symbol. */ private JMenuItem buyMenuItem; /** * This is a menu item that can be customized with the current symbol. */ private JMenuItem sellMenuItem; /** * This is called right before the popup is displayed. It is called * regardless of who initiated the popup. * * @param menu Never null. * @param symbol This will be null if the user didn't click on a symbol. */ private void updateMenu(JPopupMenu menu, String symbol) { assert null != menu; if (processedMenu != menu) { // The library has created a new menu. Add our items. This can // happen a lot because the user can switch to a diffent window // type at any time. processedMenu = menu; buyMenuItem = new JMenuItem(); sellMenuItem = new JMenuItem(); final JMenuItem duplicateMenuItem = new JMenuItem("Duplicate"); duplicateMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { duplicateWindow(); } }); menu.insert(buyMenuItem, 0); menu.insert(sellMenuItem, 1); menu.insert(duplicateMenuItem, 2); menu.insert(new JSeparator(), 3); } final boolean enabled = null != symbol; if (!enabled) { symbol = "[symbol]"; } buyMenuItem.setText("Buy " + symbol); sellMenuItem.setText("Sell " + symbol); buyMenuItem.setEnabled(enabled); sellMenuItem.setEnabled(enabled); } /** * This is a standard function in E*TRADE windows. */ private void duplicateWindow() { TradeIdeasForm other = new TradeIdeasForm(); // Note the user of loadFromLayout() and saveToLayout(). These // functions work for layouts and for duplicating windows. other.panelPair.loadFromLayout(panelPair.saveToLayout()); other.setVisible(true); } /** * This is displayed in the E*TRADE part of the window. */ private void updateWindowTitle() { setTitle(panelPair.getWindowName()); } /** * 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") // //GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); popupMenuButton = new javax.swing.JButton(); leftHeaderPanel = new javax.swing.JPanel(); clientPanel = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } }); jLabel1.setText("linking, help, etc."); popupMenuButton.setText("☰"); popupMenuButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { popupMenuButtonActionPerformed(evt); } }); javax.swing.GroupLayout leftHeaderPanelLayout = new javax.swing.GroupLayout(leftHeaderPanel); leftHeaderPanel.setLayout(leftHeaderPanelLayout); leftHeaderPanelLayout.setHorizontalGroup( leftHeaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 232, Short.MAX_VALUE) ); leftHeaderPanelLayout.setVerticalGroup( leftHeaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(leftHeaderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(popupMenuButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(leftHeaderPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(popupMenuButton))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout clientPanelLayout = new javax.swing.GroupLayout(clientPanel); clientPanel.setLayout(clientPanelLayout); clientPanelLayout.setHorizontalGroup( clientPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 388, Short.MAX_VALUE) ); clientPanelLayout.setVerticalGroup( clientPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 243, 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(clientPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .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) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(clientPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// //GEN-END:initComponents /** * The components will continue to use resources until you dispose of them. * * @param evt */ private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed panelPair.dispose(); }//GEN-LAST:event_formWindowClosed /** * This is a standard button on the E*TRADE header panel. * * @param evt */ private void popupMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popupMenuButtonActionPerformed JPopupMenu popupMenu = panelPair.getPopup(); if (null != popupMenu) { popupMenu.show(popupMenuButton, 0, popupMenuButton.getHeight()); } }//GEN-LAST:event_popupMenuButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel clientPanel; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel leftHeaderPanel; private javax.swing.JButton popupMenuButton; // End of variables declaration//GEN-END:variables }