package cc.glsn.v15.housefund; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.TreeMap; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import cc.glsn.v15.SQLConnection; public class EditTab extends JPanel { /** * */ private static final long serialVersionUID = -7056204437190099160L; int TransID; Client Clint; EditTab SelfRef; JLabel SaveResponce; Map Fields; /** * @param arg0 * @param arg1 * @param transID */ public EditTab(Client cccc,int transID) { super(new GridBagLayout()); TransID = transID; Clint = cccc; SelfRef=this; JPanel S=new JPanel(new java.awt.GridBagLayout()); JScrollPane Scr=new JScrollPane(S); { java.awt.GridBagConstraints c=new java.awt.GridBagConstraints(); c.gridheight=1; c.gridwidth=1; c.weightx=100.0; c.weighty=100.0; c.fill=GridBagConstraints.BOTH; c.insets=new java.awt.Insets(1,1,1,1); c.anchor=GridBagConstraints.WEST; this.add(Scr,c); } SimpleDateFormat sdf=Globals.getDateFormat(); DecimalFormat mf=Globals.getMoneyFormat(); DecimalFormat pf=Globals.getPercentFormat(); Globals.addItem("TransID",S,0); Globals.addItem("Date",S,0); Globals.addItem("Desc",S,0); Globals.addItem("Catagory",S,0); Globals.addItem("Note",S,0); Globals.addItem("Status",S,0); Globals.addItem("Hidden",S,0); Globals.addItem("Total",S,Globals.FlagEndRow); Fields=new TreeMap(); Globals.addItem(TransID,S,0); newField("date",S,10,0); newField("desc",S,15,0); newField("catagory",S,15,0); newField("note",S,20,0); newField("status",S,4,0); newField("hidden",S,4,0); newField("total",S,15,Globals.FlagEndRow); for(String n : Globals.getAllUsers()) { Globals.addItem(n,S,0); newField("amt-" + n,S,10,Globals.FlagEndRow); } JButton UpdateButton=new JButton("Save"); UpdateButton.addActionListener(new UpdatePress()); Globals.addComponent(UpdateButton,S,0); JButton CloseButton=new JButton("Close"); CloseButton.addActionListener(new ClosePress()); Globals.addComponent(CloseButton,S,0); SaveResponce=new JLabel(); SaveResponce.setForeground(java.awt.Color.RED); Globals.addComponent(SaveResponce,S,Globals.FlagEndRow); if (TransID>=0) { populate(); } else { Fields.get("date").setText(sdf.format(new Date())); Fields.get("status").setText("1"); Fields.get("hidden").setText("0"); Fields.get("amt-joe").setText("%0.25"); Fields.get("amt-alex").setText("%0.25"); Fields.get("amt-andrew").setText("%0.25"); Fields.get("amt-paul").setText("%0.25"); } } private void populate() { try { SQLConnection C=Globals.getSQL(); SimpleDateFormat sdf=Globals.getDateFormat(); DecimalFormat mf=Globals.getMoneyFormat(); DecimalFormat pf=Globals.getPercentFormat(); ResultSet R=C.doSingleQuery("select * from transaction where id="+ TransID); R.first(); Fields.get("date").setText(sdf.format(R.getTimestamp("date"))); Fields.get("desc").setText(R.getString("title")); Fields.get("catagory").setText(R.getString("catagory")); Fields.get("note").setText(R.getString("note")); Fields.get("status").setText(new Integer(R.getInt("status")).toString()); Fields.get("hidden").setText(new Integer(R.getInt("totalhidden")).toString()); Fields.get("total").setText(mf.format(R.getDouble("amount"))); ResultSet R2=C.doSingleQuery("select * from distro where transid=" + R.getInt("id")); while(R2.next()) { String n=R2.getString("person"); double p=R2.getDouble("value"); Fields.get("amt-" + n).setText(mf.format(p)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } public String getTitle() { if (TransID < 0) return "Edit - New"; else return "Edit - " + TransID; } private void newField(String S, JPanel P, int Sz, int Flags) { JTextField F=new JTextField(Sz); Fields.put(S,F); Globals.addComponent(F,P,Flags); } class UpdatePress implements ActionListener { public void actionPerformed(ActionEvent evt) { int FoundDollar=0; int FoundPercent=0; double DollarSum=0; double PercentSum=0; TreeMap Peeps=new TreeMap(); int Status=0; int TotalHidden=0; long time; double Total=0.0; try { Status=new Integer(Fields.get("status").getText()); TotalHidden=new Integer(Fields.get("hidden").getText()); Total=parseDollarStr(Fields.get("total").getText()); time=Globals.getDateFormat().parse(Fields.get("date").getText()).getTime(); SaveResponce.setText("Wanker"); for(String n : Globals.getAllUsers()) { String str=Fields.get("amt-" + n).getText(); if (str.contains("%")) { double p=parseDollarStr(str); PercentSum+=p; FoundPercent++; Peeps.put(n,p*Total); } else if (str.contains("$")) { FoundDollar++; double p=parseDollarStr(str); DollarSum+=p; Peeps.put(n,p); } else if (str.length()>0) { SaveResponce.setText("amount for " + n + " is not dollar or percent. take off, hoser."); return; } } if ((FoundDollar>0) && (FoundPercent>0)) { SaveResponce.setText("a mix of dollar values and percents where found. take off, hoser."); return; } if (FoundDollar + FoundPercent==0) { SaveResponce.setText("no dollar values or percents found."); return; } if (FoundDollar>0) { if (Total!=DollarSum) { SaveResponce.setText("The dollar amounts do not add up. take off, hoser." + " " + Total + " " + DollarSum ); return; } } if (FoundPercent>0) { if (PercentSum!=1.0) { SaveResponce.setText("The percentages do not add up to 1.0. Nice work."); return; } } } catch(Exception e) { SaveResponce.setText("Some sort of magical exception. Nice work. " + e); e.printStackTrace(); return; } try { SQLConnection C=Globals.getSQL(); ResultSet R=null; if (TransID>=0) { R=C.doSingleUpdatableQuery("select * from transaction where id=" + TransID); R.first(); } else { R=C.doSingleUpdatableQuery("select * from transaction"); R.moveToInsertRow(); } R.updateTimestamp("date",new Timestamp(time)); R.updateDouble("amount",Total); R.updateString("title",Fields.get("desc").getText()); R.updateString("catagory",Fields.get("catagory").getText()); R.updateString("note",Fields.get("note").getText()); R.updateInt("status",Status); R.updateInt("totalhidden",TotalHidden); if (TransID>=0) { R.updateRow(); } else { R.insertRow(); ResultSet R2=C.doSingleQuery("select * from transaction order by id desc limit 1"); R2.first(); TransID=R2.getInt("id"); } R.close(); R=null; C.doStatement("delete from distro where transid="+ TransID); ResultSet R2=C.doSingleUpdatableQuery("select * from distro"); for(String n : Peeps.keySet()) { R2.moveToInsertRow(); double p=Peeps.get(n); R2.updateInt("transid",TransID); R2.updateString("person",n); R2.updateInt("type",2); R2.updateDouble("value",p); R2.insertRow(); } R2.close(); C.close(); SaveResponce.setText("Saved"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } } static double parseDollarStr(String str) { str=str.replace("$",""); str=str.replace("%",""); double mult=1.0; str=str.replace(" ",""); str=str.replace(",",""); if (str.contains("(")) mult=-1.0; str=str.replace("(",""); str=str.replace(")",""); if (str.equals("-")) return 0.0; if (str.equals("")) return 0.0; return new Double(str).doubleValue()*mult; } class ClosePress implements ActionListener { public void actionPerformed(ActionEvent evt) { Clint.rmEditTab(SelfRef); } } }