Commit 651abe56 by Jessica Hawkwell

initial import

0 parents
Showing with 1382 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath com.starphoenixmedia.candle_pos.Starter /dev/tty.wchusbserial1410 c</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath com.starphoenixmedia.candle_pos.Starter /dev/tty.wchusbserial1410 c</exec.args>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath com.starphoenixmedia.candle_pos.Starter /dev/tty.wchusbserial1410 c</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.starphoenixmedia</groupId>
<artifactId>candle_pos</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Candle POS</name>
<organization>
<name>StarPhoenix Media</name>
<url>http://starphoenixmedia.com/</url>
</organization>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<classpathMavenRepositoryLayout>true</classpathMavenRepositoryLayout>
<packageName>CandlePOS</packageName>
<mainClass>com.starphoenixmedia.candle_pos.Starter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.neuronrobotics</groupId>
<artifactId>nrjavaserial</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
<version>0.8.90</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyoptionaltools</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_cs</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_de_DE</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_es</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_fr</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_hu</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_it</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_ja_JP</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_ko_KR</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_pl</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_pt_BR</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_ru</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_zh_CN</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyLocale_zh_TW</artifactId>
<version>10.12.1.1</version>
</dependency>
</dependencies>
</project>
package com.starphoenixmedia.candle_pos;
import com.starphoenixmedia.candle_pos.modeforms.BGeneric;
import com.starphoenixmedia.candle_pos.modeforms.IModeWindow;
import com.starphoenixmedia.candle_pos.modeforms.MConfigure;
import com.starphoenixmedia.candle_pos.modeforms.MInventory;
import com.starphoenixmedia.candle_pos.modeforms.MSales;
import com.starphoenixmedia.candle_pos.modeforms.MTest;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
import gnu.io.NRSerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.util.Iterator;
import java.util.Set;
import java.util.TooManyListenersException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author jlhawkwell
*/
public class CandleStart
{
public static ExecutorService es;
public static void init(String[] args)
{
String dev;
char mode = 's';
int parity = 0;
int baud = 2400;
byte data = 8;
int stop = 2;
if ( args.length == 0 )
{
StringBuilder sb = new StringBuilder();
so(sb, "Usage: dev [mode] [baud] [port]");
so(sb, "\tdev\tThe serial port device to open");
so(sb, "\tmode\tOperational mode of this program: configure, inventory, sales, test (Default: sales)");
so(sb, "\tbaud\tDesired baud rate for scale operations (Default: 2400)");
so(sb, "\tport\tPort configuration string for data bits, parity, and stop bits (Default: 8n2)");
Set<String> ports = NRSerialPort.getAvailableSerialPorts();
so(sb, "\nAvailable Ports:");
Iterator<String> it = ports.iterator();
while ( it.hasNext() )
{
so(sb, it.next(), true);
}
System.out.println(sb);
return;
}
switch ( args.length )
{
default:
case 4: // config string (data, parity, stop)
Pattern p = Pattern.compile("([5-8])-?(e|n|o)-?(1|1.5|2)");
Matcher m = p.matcher(args[3]);
data = Byte.parseByte(m.group(1));
switch ( m.group(2) )
{
case "e": parity = 2; break;
case "n": parity = 0; break;
case "o": parity = 1; break;
default:
System.out.println("\nERROR\n\tParity must be one of the following: e, n, o");
return;
}
switch ( m.group(3) )
{
case "1": stop = 1; break;
case "1.5": stop = 3; break;
case "2": stop = 2; break;
default:
System.out.println("\nERROR\n\tStop bits must be one of the following: 1, 1.5, 2");
return;
}
case 3: // baud rate
baud = Integer.parseInt(args[2]);
case 2: // program mode
switch ( args[1].toLowerCase().charAt(0) )
{
case 'c':
case 'i':
case 's':
case 't':
mode = args[1].toLowerCase().charAt(0);
break;
default:
mode = 's';
}
case 1: // device
dev = args[0];
}
es = new ThreadPoolExecutor(5, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5));
try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
catch ( UnsupportedLookAndFeelException | ClassNotFoundException |
InstantiationException | IllegalAccessException e)
{}
NRSerialPort sp = new NRSerialPort(dev, baud);
sp.connect();
try { sp.getSerialPortInstance().setSerialPortParams(baud, data, stop, parity); }
catch (UnsupportedCommOperationException ex)
{ Logger.getLogger(Starter.class.getName()).log(Level.SEVERE, null, ex); }
try { sp.addEventListener(new BGeneric()); }
catch ( TooManyListenersException tml ) { System.out.println("Too Many Listeners!"); }
ScaleReader sr = new ScaleReader(sp);
IModeWindow mw = null;
switch ( mode )
{
case 'c': mw = new MConfigure(); break;
case 'i': mw = new MInventory(); break;
case 's': mw = new MSales(); break;
case 't': mw = new MTest(); break;
}
if ( mw == null ) { System.out.println("\nERROR\n\tCould not determine operational mode."); return; }
mw.setup(sr);
mw.run();
}
private static void so (StringBuilder sb, String s) { so(sb, s, false); }
private static void so (StringBuilder sb, String s, boolean tab)
{
if ( tab ) { sb.append("\t"); }
sb.append(s);
sb.append("\n");
}
}
package com.starphoenixmedia.candle_pos;
/**
*
* @author jlhawkwell
*/
public class ScaleForm
{
}
package com.starphoenixmedia.candle_pos;
import com.sun.glass.ui.Application;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author jlhawkwell
*/
public class Starter
{
public static void main(final String[] args)
{
for ( String s : args )
{
if ( "dumpvars".equals(s.toLowerCase()) )
{
Iterator<Entry<Object, Object>> it = System.getProperties().entrySet().iterator();
Entry<Object, Object> e;
while ( it.hasNext() )
{
e = it.next();
System.out.print(e.getKey().toString());
System.out.print("\t=\t");
System.out.println(e.getValue().toString());
}
Iterator<Entry<String, String>>its = System.getenv().entrySet().iterator();
Entry<String, String> es;
while ( its.hasNext() )
{
es = its.next();
System.out.print(es.getKey());
System.out.print("\t=\t");
System.out.println(es.getValue());
}
return;
}
}
final FireCodeClassLoader fccl = new FireCodeClassLoader(CandleStart.class.getClassLoader());
fccl.init();
ArrayList<File> locs = new ArrayList<>();
Thread t = new Thread()
{
@Override public void run()
{
System.out.println("Booting...");
Class cls = null;
try { cls = fccl.loadClass("com.starphoenixmedia.candle_pos.CandleStart"); }
catch (ClassNotFoundException ex)
{ Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex); return; }
/**if ( cls.isAssignableFrom(CandleStart.class) )
{
CandleStart fcl;
try { fcl = (CandleStart) cls.newInstance(); }
catch (InstantiationException | IllegalAccessException ex)
{ Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex); return; }
fcl.init(args);
}
else
{ // */
Method m = null;
try { m = cls.getMethod("init", String[].class); }
catch (NoSuchMethodException | SecurityException ex)
{ Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex); return; }
Object o = null;
try { o = cls.newInstance(); }
catch ( InstantiationException | IllegalAccessException ex)
{ Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex); }
if ( (m != null) && (o != null) )
{
try { m.invoke(o, (Object) args); }
catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
{
if ( (ex instanceof InvocationTargetException) && (ex.getCause() != null) )
{ Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex.getCause()); }
else { Logger.getLogger(CandleStart.class.getName()).log(Level.SEVERE, null, ex); }
}
}
//}
}
};
t.setName("Candle-Main");
t.setContextClassLoader(fccl);
t.setDaemon(false);
t.start();
}
/*public static void filerDeeper(ArrayList<File> list, File loc)
{
if ( !loc.exists() ) { return; }
File[] fs = loc.listFiles();
if ( fs == null ) { return; }
if ( fs.length == 0 ) { return; }
String pt;
for ( File f : fs )
{
if ( f.getName().isEmpty() ) { }
else if ( f.getName().startsWith(".") ) { }
else
{
if ( f.isFile() && f.getName().endsWith(".jar") )
{
try
{
pt = f.getCanonicalPath();
list.add(new File(pt));
System.out.println("Added ".concat(pt));
}
catch ( IOException x ) { }
}
else if ( f.isDirectory() ) { filerDeeper(list, f); }
}
}
} // */
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.CandleStart;
import com.starphoenixmedia.candle_pos.scale.ScaleAction;
import com.starphoenixmedia.candle_pos.util.ScaleActionListener;
import com.starphoenixmedia.candle_pos.scale.ScaleData;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
import com.starphoenixmedia.candle_pos.util.SuperListener;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.net.URL;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author jlhawkwell
*/
public class BGeneric implements ScaleActionListener, SerialPortEventListener, WindowListener, WindowStateListener
{
protected JFrame window;
protected JTabbedPane menu;
protected ScaleReader sr;
protected ScaleData sd;
protected JLabel outputLabel;
protected HashMap<String, JToolBar> menus;
public void setup(ScaleReader reader)
{
sr = reader;
if ( sr.getScaleData() != null) { sd = sr.getScaleData(); }
}
public void run()
{
menus = new HashMap();
window = new JFrame();
window.setTitle("Candle POS");
BorderLayout bl = new BorderLayout();
bl.setHgap(1);
bl.setVgap(1);
window.setLayout(bl);
// menu setup
menu = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
// system menu
menus.put("system", addMenuBar("System"));
menu.addTab("System", createIcon("categories/applications-system"), menus.get("system"));
JButton jb = new JButton("Exit");
jb.setIcon(createIcon("actions/system-log-out"));
jb.setText("Exit");
jb.addActionListener((ActionEvent e) ->
{ window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); });
menus.get("system").add(jb);
LookAndFeelInfo[] lafis = UIManager.getInstalledLookAndFeels();
int i;
menus.get("system").addSeparator();
SuperListener al;
ImageIcon ct = createIcon("apps/preferences-desktop-theme");
for ( i = 0; i < lafis.length; i++ )
{
jb = new JButton(lafis[i].getName());
jb.setName(lafis[i].getName());
jb.setIcon(ct);
al = new SuperListener()
{
private String l;
@Override public void actionPerformed(ActionEvent e)
{
try
{
UIManager.setLookAndFeel(l);
SwingUtilities.updateComponentTreeUI(window);
}
catch ( ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException x ) {}
}
@Override public void setup(String laf) { l = laf; }
};
al.setup(lafis[i].getClassName());
jb.addActionListener(al);
menus.get("system").add(jb);
}
window.add(menu, BorderLayout.PAGE_START);
// status bar
JToolBar statusBar = addMenuBar("Status");
statusBar.add(new JLabel("Scale Reading:"));
statusBar.addSeparator();
outputLabel = new JLabel("---");
statusBar.add(outputLabel);
window.add(statusBar, BorderLayout.PAGE_END);
// final steps
Dimension dim = new Dimension();
Rectangle rect = window.getGraphicsConfiguration().getBounds();
dim.setSize(rect.width / 2d, rect.height / 2d);
window.setMinimumSize(dim);
window.setBounds(dim.width / 2, dim.height / 2, dim.width, dim.height);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.addWindowStateListener(this);
window.addWindowListener(this);
sr.setActionListener(this);
}
@Override public void serialEvent(SerialPortEvent ev) { grabSerialEvent(ev); }
public void grabSerialEvent(SerialPortEvent ev) {}
// helper functions
protected JToolBar addMenuBar(String name)
{
JToolBar jtb = new JToolBar();
jtb.setBorderPainted(true);
jtb.setFloatable(false);
jtb.setRollover(true);
jtb.setName(name);
return jtb;
}
private int getTabIndex(String name)
{
int a;
for ( a = 0; a < menu.getTabCount(); a++ )
{
if ( name.equals(menu.getTitleAt(a)) ) { return a; }
}
return -1;
}
public ImageIcon createIcon(String icon)
{
StringBuilder sb = new StringBuilder("org/freedesktop/tango/22x22/");
sb.append(icon);
sb.append(".png");
URL f = getClass().getClassLoader().getResource(sb.toString());
if ( f != null ) { return new ImageIcon(f); }
else { return null; }
}
@Override public void windowStateChanged(WindowEvent e) {}
@Override public void windowOpened(WindowEvent e) {}
@Override public void windowClosing(WindowEvent e)
{
if ( sr != null ) { sr.halt(); }
CandleStart.es.shutdown();
window.setVisible(false);
}
@Override public void windowClosed(WindowEvent e)
{
window.dispose();
System.exit(0);
}
@Override public void windowIconified(WindowEvent e) {}
@Override public void windowDeiconified(WindowEvent e) {}
@Override public void windowActivated(WindowEvent e) {}
@Override public void windowDeactivated(WindowEvent e) {}
@Override public void actionPerformed(ScaleAction action) { outputLabel.setText(action.getReading()); }
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
/**
*
* @author jlhawkwell
*/
public interface IModeWindow
{
public void setup(ScaleReader scale);
public void run();
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.CandleStart;
import com.starphoenixmedia.candle_pos.scale.ScaleAction;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
import com.starphoenixmedia.candle_pos.util.IconTreeNode;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
/**
*
* @author jlhawkwell
*/
public class MConfigure extends BGeneric implements IModeWindow, TreeSelectionListener
{
private JTree tree;
private JTable table;
private DefaultTreeModel treeModel;
private DefaultTableModel tableModel;
@Override public void setup(ScaleReader scale)
{
super.setup(scale);
//org.apache.derby.jdbc.EmbeddedDriver
}
@Override public void run()
{
super.run();
menus.put("table", addMenuBar("Tables"));
menu.addTab("Tables", createIcon("categories/applications-office"), menus.get("table"));
JScrollPane jspa = new JScrollPane();
window.add(jspa, BorderLayout.WEST);
DefaultMutableTreeNode rootnode =
new IconTreeNode(createIcon("mimetypes/package-x-generic"), null, "Database", true);
rootnode.add(new IconTreeNode(createIcon("mimetypes/x-office-address-book"), "cat_catalogs", "Catalog"));
IconTreeNode icn = (IconTreeNode)rootnode.getChildAt(0);
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), "cat_scents", "Scents"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), "cat_dyes", "Dyes"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), "cat_wax", "Wax"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), null, "Wicks"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), null, "Containers"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), null, "Additives"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), null, "Acc/Misc"));
//icn.add(new IconTreeNode(createIcon("mimetypes/x-office-calendar"), null, "Lookup Table"));
rootnode.add(new IconTreeNode(createIcon("mimetypes/x-office-drawing-template"), null, "Batch"));
rootnode.add(new IconTreeNode(createIcon("mimetypes/x-office-document-template"), null, "Inventory"));
rootnode.add(new IconTreeNode(createIcon("mimetypes/x-office-spreadsheet-template"), null, "Sales"));
icn = (IconTreeNode)rootnode.getChildAt(3);
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-spreadsheet"), null, "Receipts"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-spreadsheet"), null, "Expenses"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-spreadsheet"), null, "Tax"));
icn.add(new IconTreeNode(createIcon("mimetypes/x-office-spreadsheet"), null, "Earnings"));
treeModel = new DefaultTreeModel(rootnode);
tree = new JTree(treeModel);
tree.setSelectionModel(new DefaultTreeSelectionModel());
tree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setExpandsSelectedPaths(true);
tree.expandRow(1);
tree.expandRow(12);
tree.setCellRenderer(new DefaultTreeCellRenderer()
{
private static final long serialVersionUID = 1L;
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
boolean expanded, boolean isLeaf, int row, boolean focused)
{
Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
if ( value instanceof IconTreeNode )
{
Icon i = ((IconTreeNode)value).getIcon();
if ( i != null ) { setIcon(((IconTreeNode)value).getIcon()); }
}
return c;
}
});
tree.addTreeSelectionListener(this);
jspa.setViewportView(tree);
JScrollPane jspb = new JScrollPane();
window.add(jspb, BorderLayout.CENTER);
table = new JTable();
jspb.setViewportView(table);
jspb.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jspb.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
CandleStart.es.submit(sr);
window.pack();
window.setVisible(true);
}
@Override public void actionPerformed(ScaleAction action) { super.actionPerformed(action); }
@Override public void valueChanged(TreeSelectionEvent e)
{
if ( tree.getLastSelectedPathComponent() instanceof IconTreeNode )
{
IconTreeNode itn = (IconTreeNode)tree.getLastSelectedPathComponent();
String pt = itn.getTableName();
}
}
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
/**
*
* @author jlhawkwell
*/
public class MInventory implements IModeWindow
{
@Override public void setup(ScaleReader scale)
{ }
@Override public void run()
{ }
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
/**
*
* @author jlhawkwell
*/
public class MSales implements IModeWindow
{
@Override public void setup(ScaleReader scale)
{ }
@Override public void run()
{ }
}
package com.starphoenixmedia.candle_pos.modeforms;
import com.starphoenixmedia.candle_pos.CandleStart;
import com.starphoenixmedia.candle_pos.scale.ScaleAction;
import com.starphoenixmedia.candle_pos.scale.ScaleData;
import com.starphoenixmedia.candle_pos.scale.ScaleReader;
import com.starphoenixmedia.candle_pos.scale.ScaleUnits;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* @author jlhawkwell
*/
public class MTest extends BGeneric implements IModeWindow
{
//JLabel scaleText;
DefaultTableModel dtm;
JTable jt;
@Override public void setup(ScaleReader scale)
{
super.setup(scale);
}
@Override public void run()
{
super.run();
JScrollPane jsp = new JScrollPane();
window.add(jsp, BorderLayout.CENTER);
jt = new JTable()
{
private static final long serialVersionUID = 1L;
@Override public boolean isCellEditable(int x, int y) { return false; }
};
jsp.setViewportView(jt);
dtm = new DefaultTableModel();
jt.setModel(dtm);
dtm.addColumn("Units");
dtm.addColumn("Data");
jt.getColumn("Units").setHeaderValue("Units");
jt.getColumn("Data").setHeaderValue("Data");
dtm.addRow(new String[]{ScaleUnits.UNIT_g.getString(), "0"});
dtm.addRow(new String[]{ScaleUnits.UNIT_kg.getString(), "0"});
dtm.addRow(new String[]{ScaleUnits.UNIT_lbozd.getString(), "0"});
dtm.addRow(new String[]{ScaleUnits.UNIT_lbozf.getString(), "0"});
CandleStart.es.submit(sr);
window.pack();
window.setVisible(true);
}
@Override public void actionPerformed(ScaleAction action)
{
super.actionPerformed(action);
byte[] raw = action.getRaw();
ScaleUnits[] su = new ScaleUnits[]
{
ScaleUnits.UNIT_g,
ScaleUnits.UNIT_kg,
ScaleUnits.UNIT_lbozd,
ScaleUnits.UNIT_lbozf
};
int a;
double[] data;
String reading;
for ( a = 0; a <= 3; a++ )
{
data = action.convertData(su[a], raw);
reading = ScaleData.readingString(su[a], data);
dtm.setValueAt(reading, a, 1);
}
}
}
package com.starphoenixmedia.candle_pos.modeforms;
package com.starphoenixmedia.candle_pos.scale;
/**
*
* @author jlhawkwell
*/
public class ScaleAction extends ScaleData
{
private final ScaleUnits su;
private final ScaleMode sm;
private final double[] readingD;
ScaleAction(ScaleData data)
{
su = data.getUnit();
sm = data.getMode();
raw = data.getRaw();
reading = data.getReading();
readingD = ScaleData.parseValues(su, raw);
}
public double[] convertData(ScaleUnits newUnit, byte[] data)
{
// FAST FAIL
if ( newUnit == su ) { return readingD; }
// convert to grams
double grams = readingD[0];
if ( su == ScaleUnits.UNIT_g ) { grams = readingD[0]; }
else if ( su == ScaleUnits.UNIT_kg ) { grams = readingD[0] * 1000; }
else if ( (su == ScaleUnits.UNIT_lbozd) || (su == ScaleUnits.UNIT_lbozf) )
{
grams = readingD[0] * 16;
grams += readingD[1];
if ( su == ScaleUnits.UNIT_lbozf )
{
switch ( data[7] )
{
case 1: grams += 0.25d; break;
case 2: grams += 0.5d; break;
case 3: grams += 0.75d; break;
}
}
grams = grams * 28.35;
}
// convert out
if ( newUnit == ScaleUnits.UNIT_g ) { return new double[]{grams}; }
else if ( newUnit == ScaleUnits.UNIT_kg ) { return new double[]{grams / 1000}; }
else if ( (newUnit == ScaleUnits.UNIT_lbozd) || (newUnit == ScaleUnits.UNIT_lbozf) )
{
double[] ret = {0,0,0};
grams = grams / 28.35;
ret[0] = Math.floor(grams / 16);
grams -= ret[0];
if ( newUnit == ScaleUnits.UNIT_lbozf )
{
ret[1] = Math.floor(grams);
grams -= ret[1];
if ( grams < 0.25) { ret[2] = 0; }
else if ( grams < 0.5 ) { ret[2] = 1; }
else if ( grams < 0.75 ) { ret[2] = 2; }
else if ( grams < 1 ) { ret[2] = 3; }
else { ret[2] = 0; }
return ret;
}
else
{
ret[1] = grams;
return new double[]{ret[0], ret[1]};
}
}
return new double[]{};
}
}
package com.starphoenixmedia.candle_pos.scale;
import java.text.DecimalFormat;
/**
*
* @author jlhawkwell
*/
public class ScaleData
{
protected byte[] raw;
protected ScaleUnits sc_unit = ScaleUnits.UNIT_kg;
protected ScaleMode sc_mode = ScaleMode.MODE_NORMAL;
protected String reading;
public static ScaleUnits parseScaleUnits(byte[] bt)
{
int t = (0x70 & bt[0]) >> 4;
for ( ScaleUnits s : ScaleUnits.values() )
{
if ( s.getValue() == t ) { return s; }
}
return null;
}
public static ScaleMode parseScaleMode(byte[] bt)
{
int t = (0x0f & bt[0]);
for ( ScaleMode s : ScaleMode.values() )
{
if ( s.getValue() == t ) { return s; }
}
return null;
}
public static double[] parseValues(ScaleUnits unit, byte[] bt)
{
switch ( unit )
{
case UNIT_g:
return new double[]{ doublize(bt[3], bt[4], bt[5], bt[6], bt[7]) };
case UNIT_kg:
return new double[]{ doublize(bt[3], bt[4], bt[5], bt[6], bt[7]) / 100d };
case UNIT_lbozd:
return new double[]{ doublize(bt[3], bt[4]), doublize(bt[5], bt[6], bt[7]) / 10d };
case UNIT_lbozf:
return new double[]{ doublize(bt[3], bt[4]), doublize(bt[5], bt[6]), doublize(bt[7]) };
default:
break;
}
return new double[]{};
}
public static String readingString(ScaleUnits unit, double[] data)
{
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
StringBuilder sb = new StringBuilder();
switch ( unit )
{
case UNIT_g:
df.applyPattern("##,##0");
sb.append(df.format(data[0]));
sb.append(" g");
break;
case UNIT_kg:
df.applyPattern("##0.00");
sb.append(df.format(data[0]));
sb.append(" kg");
break;
case UNIT_lbozd:
df.applyPattern("#0");
sb.append(df.format(data[0]));
sb.append(" lb ");
df.applyPattern("#0.0");
sb.append(df.format(data[1]));
sb.append(" oz");
break;
case UNIT_lbozf:
df.applyPattern("#0");
sb.append(df.format(data[0]));
sb.append(" lb ");
sb.append(df.format(data[1]));
sb.append(" ");
switch ( (int)data[2] )
{
case 0: sb.append("0"); break;
case 1: sb.append("\u00bc"); break;
case 2: sb.append("\u00bd"); break;
case 3: sb.append("\u00be"); break;
default:
break;
}
sb.append(" oz");
break;
default:
break;
}
return sb.toString();
}
public synchronized void setRaw(byte[] bt)
{
raw = bt;
// scale status
sc_unit = parseScaleUnits(bt);
sc_mode = parseScaleMode(bt);
// skip 1 and 2
// measure
reading = readingString(sc_unit, parseValues(sc_unit, bt));
}
public synchronized String getReading() { return reading; }
public synchronized ScaleMode getMode() { return sc_mode; }
public synchronized ScaleUnits getUnit() { return sc_unit; }
public synchronized byte[] getRaw() { return raw; }
private static double doublize(byte... bs) { return Double.valueOf(numberize(bs)); }
private static String numberize(byte... bs) { return numberize(false, bs); }
private static String numberize(boolean force, byte... bs)
{
StringBuilder sb = new StringBuilder();
for ( byte b : bs )
{
if ( (sb.length() == 0) || force )
{
if ( (b != 0) || force ) { sb.append(b); }
}
else { sb.append(b); }
}
if ( sb.length() == 0 ) { sb.append("0"); }
return sb.toString();
}
private static String numberize(int... bs) { return numberize(false, bs); }
private static String numberize(boolean force, int... bs)
{
StringBuilder sb = new StringBuilder();
for ( int b : bs )
{
if ( (sb.length() == 0) || force )
{
if ( (b != 0) || force ) { sb.append(b); }
else { sb.append(" "); }
}
}
if ( sb.length() == 0 ) { sb.append("0"); }
return sb.toString();
}
}
package com.starphoenixmedia.candle_pos.scale;
/**
*
* @author jlhawkwell
*/
public enum ScaleMode
{
MODE_NORMAL(0, "Normal mode: positive weight"),
MODE_TEST(1, "Test mode: adjust zero counts"),
MODE_SPAN(2, "Calibration mode: adjust SPAN"),
MODE_TARE(3, "TARE"),
MODE_LO(4, "Low Battery"),
MODE_ERRO(5, "Overload"),
MODE_ERRL(6, "Zero counts too low"),
MODE_IIII(7, "Negative weight"),
MODE_8888(12, "Displaying 8888"),
MODE_ERRT(13, "Tare Error"),
MODE_CALT(14, "Calibration mode: tare"),
MODE_CAL(15, "Calibration mode");
private int m;
private String desc;
ScaleMode(int i, String s) { m = i; desc = s; }
public int getValue() { return m; }
public String getString() { return desc; }
}
package com.starphoenixmedia.candle_pos.scale;
import com.starphoenixmedia.candle_pos.scale.ScaleAction;
import com.starphoenixmedia.candle_pos.util.ScaleActionListener;
import gnu.io.NRSerialPort;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author jlhawkwell
*/
public class ScaleReader extends Thread implements Runnable
{
private final NRSerialPort sp;
private boolean exit = true;
private ScaleActionListener sal;
private ScaleData sd;
public ScaleReader(NRSerialPort serial) { sp = serial; }
public void setActionListener(ScaleActionListener listen)
{
sal = listen;
}
public void halt() { synchronized(this) { exit = false; } }
@Override public void run() { run(false); }
public void run(boolean display)
{
sd = new ScaleData();
InputStream is = sp.getInputStream();
byte[] bt = new byte[]{0,0,0, 0,0,0,0,0, 0};
int a;
try
{
while ( exit )
{
if ( is.available() >= 6 )
{
a = is.read();
if ( a == 0x02 )
{
is.read(bt);
sd.setRaw(bt);
if ( sal != null ) { sal.actionPerformed(new ScaleAction(sd)); }
if ( display )
{
System.out.print("\nData: ");
System.out.print(sd.getReading());
System.out.print("\t");
System.out.print(sd.getUnit().getString());
System.out.print("\t");
System.out.println(sd.getMode().getString());
}
}
}
}
sp.disconnect();
sp.removeEventListener();
}
catch ( IOException ex ) { } // */
}
public ScaleData getScaleData() { return sd; }
}
package com.starphoenixmedia.candle_pos.scale;
/**
*
* @author jlhawkwell
*/
public enum ScaleUnits
{
UNIT_kg(2, "kG"),
UNIT_lbozd(3, "lb-oz (decimal)"),
UNIT_g(4, "g"),
UNIT_lbozf(5, "lb-oz (fractional)");
private int u;
private String desc;
ScaleUnits(int v, String s) { u = v; desc = s; }
public int getValue() { return u; }
public String getString() { return desc; }
}
package com.starphoenixmedia.candle_pos.util;
import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;
/**
*
* @author jlhawkwell
*/
public class IconTreeNode extends DefaultMutableTreeNode
{
private static final long serialVersionUID = 1L;
private Icon icon;
private String tableName;
public IconTreeNode() { super(); }
public IconTreeNode(Icon icon, String tableName)
{
super();
this.icon = icon;
this.tableName = tableName;
}
public IconTreeNode(Icon icon, String tableName, Object userObject)
{
super(userObject);
this.icon = icon;
this.tableName = tableName;
}
public IconTreeNode(Icon icon, String tableName, Object userObject, boolean allowsChildren)
{
super(userObject, allowsChildren);
this.icon = icon;
this.tableName = tableName;
}
public Icon getIcon() { return icon; }
public String getTableName() { return tableName; }
public void setIcon(Icon icon) { this.icon = icon; }
public void setTableName(String tableName) { this.tableName = tableName; }
@Override public String toString()
{
if ( userObject != null ) { return userObject.toString(); }
else if ( tableName != null ) { return tableName; }
else if ( icon != null ) { return icon.toString(); }
else { return super.toString(); }
}
}
package com.starphoenixmedia.candle_pos.util;
import com.starphoenixmedia.candle_pos.scale.ScaleAction;
/**
*
* @author jlhawkwell
*/
public interface ScaleActionListener
{
public void actionPerformed(ScaleAction action);
}
package com.starphoenixmedia.candle_pos.util;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author jlhawkwell
*/
public class SuperListener implements ActionListener
{
@Override public void actionPerformed(ActionEvent e) { }
public void setup(String data) {}
}
# *************************************************************************
# *** DO NOT TOUCH FILES IN THIS DIRECTORY! ***
# *** FILES IN THIS DIRECTORY AND SUBDIRECTORIES CONSTITUTE A DERBY ***
# *** DATABASE, WHICH INCLUDES THE DATA (USER AND SYSTEM) AND THE ***
# *** FILES NECESSARY FOR DATABASE RECOVERY. ***
# *** EDITING, ADDING, OR DELETING ANY OF THESE FILES MAY CAUSE DATA ***
# *** CORRUPTION AND LEAVE THE DATABASE IN A NON-RECOVERABLE STATE. ***
# *************************************************************************
\ No newline at end of file
# *************************************************************************
# *** DO NOT TOUCH FILES IN THIS DIRECTORY! ***
# *** FILES IN THIS DIRECTORY ARE USED BY THE DERBY DATABASE RECOVERY ***
# *** SYSTEM. EDITING, ADDING, OR DELETING FILES IN THIS DIRECTORY ***
# *** WILL CAUSE THE DERBY RECOVERY SYSTEM TO FAIL, LEADING TO ***
# *** NON-RECOVERABLE CORRUPT DATABASES. ***
# *************************************************************************
\ No newline at end of file
# *************************************************************************
# *** DO NOT TOUCH FILES IN THIS DIRECTORY! ***
# *** FILES IN THIS DIRECTORY ARE USED BY THE DERBY DATABASE TO STORE ***
# *** USER AND SYSTEM DATA. EDITING, ADDING, OR DELETING FILES IN THIS ***
# *** DIRECTORY WILL CORRUPT THE ASSOCIATED DERBY DATABASE AND MAKE ***
# *** IT NON-RECOVERABLE. ***
# *************************************************************************
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!