From 4256d6e7b211967fc152ad33587d99cfdfb3fc01 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 7 Sep 2013 01:14:18 +0000 Subject: [PATCH] --- .classpath | 12 +- src/zutil/chart/AbstractChart.java | 156 ++++++++++++++++++ src/zutil/chart/ChartData.java | 103 ++++++++++++ src/zutil/chart/LineChart.java | 62 +++++++ src/zutil/db/DBConnection.java | 7 +- src/zutil/db/bean/DBBean.java | 2 +- .../log/net/NetLogGuiClientInstance.java | 1 - src/zutil/math/Tick.java | 12 +- .../net/http/soap/SOAPClientFactory.java | 4 +- src/zutil/test/ChartTest.java | 60 +++++++ src/zutil/test/SOAPClientTest.java | 4 +- src/zutil/test/UPnPServerTest.java | 4 +- 12 files changed, 401 insertions(+), 26 deletions(-) create mode 100644 src/zutil/chart/AbstractChart.java create mode 100644 src/zutil/chart/ChartData.java create mode 100644 src/zutil/chart/LineChart.java create mode 100644 src/zutil/test/ChartTest.java diff --git a/.classpath b/.classpath index cb89fff..e5bfbb1 100644 --- a/.classpath +++ b/.classpath @@ -2,13 +2,13 @@ - - - + + + - - - + + + diff --git a/src/zutil/chart/AbstractChart.java b/src/zutil/chart/AbstractChart.java new file mode 100644 index 0000000..05a0f50 --- /dev/null +++ b/src/zutil/chart/AbstractChart.java @@ -0,0 +1,156 @@ +/******************************************************************************* + * Copyright (c) 2013 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ +package zutil.chart; + +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.geom.Line2D; +import java.util.logging.Logger; + +import javax.swing.JPanel; + +import zutil.log.LogUtil; + +public abstract class AbstractChart extends JPanel{ + private static final Logger logger = LogUtil.getLogger(); + private static final long serialVersionUID = 1L; + + /** The offset from the borders of the panel in pixels */ + public static final int PADDING = 20; + + protected ChartData data; + protected int width; + protected int height; + protected Rectangle chartBound; + + + + protected void paintComponent(Graphics g){ + Graphics2D g2 = (Graphics2D)g; + + Rectangle bound = drawScale( g2 ); + drawChart( g2, bound ); + } + + protected Rectangle drawScale(Graphics2D g2){ + if( data == null ) + return null; + + // update values + width = this.getWidth(); + height = this.getHeight(); + Rectangle bound = new Rectangle(); + + // Values + int stepLength = 7; + + /////// Temp values + // Calculate Font sizes + FontMetrics metric = g2.getFontMetrics(); + int fontHeight = metric.getHeight(); + int fontXWidth = 0; + int fontYWidth = 0; + for( Point p : data.getPoints() ){ + int length = 0; + String tmp = data.getXString( p.x ); + if( tmp != null ) length = metric.stringWidth( tmp ); + else length = metric.stringWidth( ""+p.x ); + fontXWidth = Math.max(length, fontXWidth); + + tmp = data.getXString( p.y ); + if( tmp != null ) length = metric.stringWidth( tmp ); + else length = metric.stringWidth( ""+p.y ); + fontYWidth = Math.max(length, fontYWidth); + } + // Calculate origo + Point origo = new Point( PADDING+fontYWidth+stepLength, height-PADDING-fontHeight-stepLength ); + bound.x = (int) (origo.getX()+1); + bound.y = PADDING; + bound.width = width-bound.x-PADDING; + bound.height = (int) (origo.getY()-PADDING-1); + // Calculate Axis scales + double xScale = (double)(Math.abs(data.getMaxX())+Math.abs(data.getMinX()))/bound.width; + double yScale = (double)(Math.abs(data.getMaxY())+Math.abs(data.getMinY()))/bound.height; + + + /////// Draw + // Y Axis + g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX(), origo.getY() )); + // X Axis + g2.draw( new Line2D.Double( origo.getX(), origo.getY(), width-PADDING, origo.getY() )); + // Y Axis steps and labels + g2.draw( new Line2D.Double( origo.getX(), origo.getY(), origo.getX()-stepLength, origo.getY() )); + g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX()-stepLength, PADDING )); + + // X Axis steps and labels + g2.draw( new Line2D.Double( width-PADDING, origo.getY(), width-PADDING, origo.getY()+stepLength )); + + // DEBUG + /* + g2.setColor(Color.red); + g2.drawRect(bound.x, bound.y, bound.width, bound.height); + */ + return bound; + } + + /** + * This method is called after the chart scale has been drawn. + * This method will draw the actual chart + * + * @param g is the Graphics object that will paint the chart + * @param bound is the bounds of the chart, the drawing should not exceed this bound + */ + protected abstract void drawChart(Graphics2D g2, Rectangle bound); + + + /** + * Sets the data that will be drawn. + * + * @param data is the data to draw + */ + public void setChartData(ChartData data){ + this.data = data; + } + + /** + * Converts a x value to ax pixel coordinate + * + * @param x is the x data value + * @return pixel coordinate, or 0 if the chart have not been drawn yet. + */ + protected int getXCoordinate(int x){ + return 0; + } + + /** + * Converts a y value to a y pixel coordinate + * + * @param y is the y data value + * @return pixel coordinate, or 0 if the chart have not been drawn yet. + */ + protected int getYCoordinate(int y){ + return 0; + } +} diff --git a/src/zutil/chart/ChartData.java b/src/zutil/chart/ChartData.java new file mode 100644 index 0000000..6cc819a --- /dev/null +++ b/src/zutil/chart/ChartData.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2013 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ +package zutil.chart; + +import java.awt.Point; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class ChartData { + + private HashMap xStrings; + private HashMap yStrings; + private int maxx; + private int minx; + private int maxy; + private int miny; + + private ArrayList points; + + + public ChartData(){ + xStrings = new HashMap(); + yStrings = new HashMap(); + + points = new ArrayList(); + } + + public void setXValueString(int x, String name){ + xStrings.put(x, name); + } + public void setYValueString(int y, String name){ + yStrings.put(y, name); + } + + + public void addPoint(int x, int y){ + points.add( new Point( x, y)); + setMaxMin(x, y); + } + public void addPoint(int y){ + points.add( new Point( maxx, y)); + maxx++; + setMaxMin(maxx, y); + } + public void addPoint(String x, int y){ + xStrings.put(maxx, x); + points.add( new Point( maxx, y)); + maxx++; + setMaxMin(maxx, y); + } + + + private void setMaxMin(int x, int y){ + if( x > maxx ) maxx = x; + else if( x < minx ) minx = x; + + if( y > maxy ) maxx = y; + else if( y < miny ) minx = y; + } + + public int getMaxX(){ + return maxx; + } + public int getMinX(){ + return minx; + } + public int getMaxY(){ + return maxy; + } + public int getMinY(){ + return miny; + } + public String getXString(int x){ + return xStrings.get(x); + } + public String getYString(int y){ + return yStrings.get(y); + } + + protected List getPoints(){ + return points; + } +} diff --git a/src/zutil/chart/LineChart.java b/src/zutil/chart/LineChart.java new file mode 100644 index 0000000..02b42fb --- /dev/null +++ b/src/zutil/chart/LineChart.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2013 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ +package zutil.chart; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.geom.Line2D; + +public class LineChart extends AbstractChart{ + private static final long serialVersionUID = 1L; + + @Override + protected void drawChart(Graphics2D g2, Rectangle bound) { + // TODO Auto-generated method stub + drawLine(g2, 50, 50,100,150); + drawLine(g2, 100,150,150,100); + drawLine(g2, 150,100,200,300); + drawLine(g2, 200,300,250,40); + } + + private void drawLine(Graphics2D g2, float x1, float y1, float x2, float y2){ + // Shadow + g2.setPaint(new Color(220,220,220)); + g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); + g2.draw( new Line2D.Float(x1, y1+2, x2, y2+2)); + // Smoth shadow + g2.setPaint(new Color(230,230,230)); + g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); + g2.draw( new Line2D.Float(x1+1, y1+3, x2-1, y2+3)); + + // Line border + g2.setPaint(new Color(255,187,187)); + g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); + g2.draw( new Line2D.Float(x1, y1, x2, y2)); + + // Line + g2.setPaint(new Color(237,28,36)); + g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); + g2.draw( new Line2D.Float(x1, y1, x2, y2)); + } +} diff --git a/src/zutil/db/DBConnection.java b/src/zutil/db/DBConnection.java index ad42554..0f2ea69 100644 --- a/src/zutil/db/DBConnection.java +++ b/src/zutil/db/DBConnection.java @@ -23,6 +23,7 @@ package zutil.db; import java.io.Closeable; +import java.math.BigInteger; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -114,13 +115,13 @@ public class DBConnection implements Closeable{ /** * @return the last inserted id or -1 if there was an error */ - public Object getLastInsertID(){ + public long getLastInsertID(){ try{ - return exec("SELECT LAST_INSERT_ID()", new SimpleSQLHandler()); + return exec("SELECT LAST_INSERT_ID()", new SimpleSQLHandler()).longValue(); }catch(SQLException e){ logger.log(Level.WARNING, null, e); } - return null; + return -1; } /** diff --git a/src/zutil/db/bean/DBBean.java b/src/zutil/db/bean/DBBean.java index b27e9f5..8830362 100644 --- a/src/zutil/db/bean/DBBean.java +++ b/src/zutil/db/bean/DBBean.java @@ -266,7 +266,7 @@ public abstract class DBBean { // Execute the SQL DBConnection.exec(stmt); if( id == null ) - this.id = (Long) db.getLastInsertID(); + this.id = db.getLastInsertID(); // Save the list, after we get the object id for(Field field : config.fields){ diff --git a/src/zutil/log/net/NetLogGuiClientInstance.java b/src/zutil/log/net/NetLogGuiClientInstance.java index 59187c8..ff0e943 100644 --- a/src/zutil/log/net/NetLogGuiClientInstance.java +++ b/src/zutil/log/net/NetLogGuiClientInstance.java @@ -103,7 +103,6 @@ public class NetLogGuiClientInstance implements Initializable, NetLogListener { logTable.getItems().add(msg); Platform.runLater(new Runnable() { - @Override public void run() { logCountLabel.setText("" + (Long.parseLong(logCountLabel.getText()) + 1)); } diff --git a/src/zutil/math/Tick.java b/src/zutil/math/Tick.java index fd0fea5..87c76ac 100644 --- a/src/zutil/math/Tick.java +++ b/src/zutil/math/Tick.java @@ -77,9 +77,9 @@ public class Tick { */ public static char increment(char c){ switch(Character.toLowerCase(c)){ - case 'z': return 'Ã¥'; - case 'Ã¥': return 'ä'; - case 'ä': return 'ö'; + case 'z': return 'å'; + case 'å': return 'ä'; + case 'ä': return 'ö'; } c = (char)(Character.toLowerCase(c) + 1); if(isAlfa(c)){ @@ -123,9 +123,9 @@ public class Tick { case 'x': case 'y': case 'z': - case 'Ã¥': - case 'ä': - case 'ö': return true; + case 'å': + case 'ä': + case 'ö': return true; default: return false; } } diff --git a/src/zutil/net/http/soap/SOAPClientFactory.java b/src/zutil/net/http/soap/SOAPClientFactory.java index dac2baa..66dae64 100644 --- a/src/zutil/net/http/soap/SOAPClientFactory.java +++ b/src/zutil/net/http/soap/SOAPClientFactory.java @@ -26,8 +26,6 @@ import java.util.HashMap; import java.util.List; import java.util.logging.Logger; -import javax.wsdl.WSDLException; - import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; @@ -57,7 +55,7 @@ public class SOAPClientFactory { * @return a client Object */ @SuppressWarnings("unchecked") - public static T getClient(Class intf) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{ + public static T getClient(Class intf) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{ if( !WSInterface.class.isAssignableFrom( intf )){ throw new ClassCastException("The Web Service class is not a subclass of WSInterface!"); } diff --git a/src/zutil/test/ChartTest.java b/src/zutil/test/ChartTest.java new file mode 100644 index 0000000..d5eb16f --- /dev/null +++ b/src/zutil/test/ChartTest.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2013 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ +package zutil.test; + +import java.util.logging.Level; + +import javax.swing.JFrame; + +import zutil.chart.ChartData; +import zutil.chart.LineChart; +import zutil.log.LogUtil; + +public class ChartTest extends JFrame{ + private static final long serialVersionUID = 1L; + + public static void main(String[] args) { + LogUtil.setLevel("zutil", Level.FINEST); + ChartTest frame = new ChartTest(); + frame.setVisible(true); + } + + public ChartTest(){ + ChartData data = new ChartData(); + data.addPoint(1,1); + data.addPoint(2,1); + data.addPoint(3,1); + data.addPoint(4,1); + data.addPoint(5,1); + data.addPoint(6,1); + data.addPoint(7,1); + data.addPoint(8,1); + + LineChart chart = new LineChart(); + chart.setChartData( data ); + this.add( chart ); + + this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); + this.setSize(600, 400); + } + +} diff --git a/src/zutil/test/SOAPClientTest.java b/src/zutil/test/SOAPClientTest.java index 6bb9271..c6b9e3d 100644 --- a/src/zutil/test/SOAPClientTest.java +++ b/src/zutil/test/SOAPClientTest.java @@ -27,8 +27,6 @@ import java.util.logging.Level; import javassist.CannotCompileException; import javassist.NotFoundException; -import javax.wsdl.WSDLException; - import zutil.log.CompactLogFormatter; import zutil.log.LogUtil; import zutil.net.http.soap.SOAPClientFactory; @@ -36,7 +34,7 @@ import zutil.net.ws.WSInterface; public class SOAPClientTest { - public static void main(String[] args) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{ + public static void main(String[] args) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{ LogUtil.setGlobalLevel(Level.ALL); LogUtil.setFormatter("", new CompactLogFormatter()); diff --git a/src/zutil/test/UPnPServerTest.java b/src/zutil/test/UPnPServerTest.java index ed092ee..e5d2ab6 100644 --- a/src/zutil/test/UPnPServerTest.java +++ b/src/zutil/test/UPnPServerTest.java @@ -25,8 +25,6 @@ package zutil.test; import java.io.File; import java.io.IOException; -import javax.wsdl.WSDLException; - import zutil.io.MultiPrintStream; import zutil.net.http.HttpServer; import zutil.net.http.soap.SOAPHttpPage; @@ -37,7 +35,7 @@ import zutil.net.ws.WebServiceDef; public class UPnPServerTest { - public static void main(String[] args) throws IOException, WSDLException{ + public static void main(String[] args) throws IOException{ UPnPMediaServer upnp = new UPnPMediaServer("http://192.168.0.60:8080/"); MultiPrintStream.out.println("UPNP Server running");