/*
* 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: ToolBar.java,v $
* $Date: 2014/02/19 07:17:57 $
* $Revision: 1.6 $
*/
package com.tradeideas.sample;
import com.tradeideas.external.TIMain;
import java.awt.Color;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.UIManager;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
/**
* This represents the E*TRADE main program.
*
* @author phil
*/
public class ToolBar extends javax.swing.JFrame {
private final Logger logger = Logger.getLogger(ToolBar.class.getName());
/**
* This is intended to copy what E*TRADE already does.
*/
private void initLogger() {
// http://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatically
ConsoleAppender console = new ConsoleAppender(); //create appender
//configure the appender
String PATTERN = "%d [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.INFO);
console.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);
}
/**
* The main program must provide a user name to TI. This should be unique,
* so no two users share anything or kick each other off. And this should be
* the same every time the same user logs in, so he can see his old
* settings. Normally E*TRADE hashes/encrypts/obfuscates the user's E*TRADE
* account name to get the TI user name. This test program can't do that. So
* instead it uses the user's network card to make a unique username. This
* should be a pretty reasonable approximation of what we really need.
*/
private class FakeUserId {
private final String value;
private int accumulator;
private int used;
private void addInterface(NetworkInterface ni) {
byte[] hardwareAddress;
try {
hardwareAddress = ni.getHardwareAddress();
} catch (SocketException exception) {
logger.info("Skipping network adapter: " + ni.getDisplayName(), exception);
return;
}
if (null == hardwareAddress) {
logger.info("Skipping network adapter with no hardware address: " + ni.getDisplayName());
return;
}
used++;
int position = 0;
for (byte b : hardwareAddress) {
accumulator += ((int) b) << position;
position += 3;
position %= 20;
}
}
public FakeUserId() {
try {
for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
addInterface(ni);
}
} catch (SocketException exception) {
logger.info("Unable to iterate over network interfaces.", exception);
}
value = "auto" + accumulator;
logger.info("Username = “" + value + "” based on " + used + " network interfaces.");
}
/**
* Give this value to TI.
* @return The username.
*/
@Override
public String toString() {
return value;
}
}
/**
* Creates new form ToolBar
*/
public ToolBar() {
initLogger();
initComponents();
TIMain.setObfuscatedUserName(new FakeUserId().toString());
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("E*TRADE Toolbar");
jButton1.setText("Chart");
jButton2.setText("Execution
Panel");
jButton3.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
jButton3.setText("Trade-Ideas");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("News");
jLabel1.setText("...");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// //GEN-END:initComponents
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
new TradeIdeasForm().setVisible(true);
}//GEN-LAST:event_jButton3ActionPerformed
/**
* Initialize the main program. The look and feel code came from Ibo. This
* is an approximation of what the E*TRADE main program will do.
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
Map lfColors = new LinkedHashMap(32);
Color background = new Color(37, 37, 37);
UIManager.put("control", background);
UIManager.put("Label.background", Color.WHITE);
UIManager.put("nimbusLightBackground", background);
UIManager.put("ToolTip.foreground", Color.BLACK);
UIManager.put("nimbusBase", Color.BLACK);
UIManager.put("nimbusBrightBase", new Color(30, 90, 120));
UIManager.put("nimbusBlueGrey", new Color(26, 26, 26));
UIManager.put("nimbusFocus", new Color(70, 151, 236));
UIManager.put("text", new Color(255, 255, 255));
UIManager.put("textInactiveText", new Color(150, 150, 150));
UIManager.put("Menu.background", new Color(68, 68, 68));
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ToolBar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ToolBar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ToolBar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ToolBar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ToolBar().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}