Java log class

Log is a simple java class that provides methods to log messages to a text/log file. To log other types of data simple add the needed methods using the method ‘public static void write(boolean msg)’ as a template.

Usage:
To use this class simply import it in to any class that you wish to use its methods. to add to the log file add the following line to your code.

1
Log.write("<PUT YOUR MESSAGE HERE>");

And here is the class code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.BufferedWriter;
import java.io.FileWriter;
 
public class Log {
 
	private logFilePath = "<path to your log file goes here>"; // set this to point to the log file you wish to use
 
	// default constructor 
	public static void write(String msg) {
		try {
			// Create file
			FileWriter fstream = new FileWriter(logFilePath, true);
			BufferedWriter out = new BufferedWriter(fstream);
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			msg = dateFormat.format(date) + " ---> " + msg;
			out.write(msg);
			out.newLine();
			out.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
	// method to log boolean values
	public static void write(boolean msg) {
		write("" + msg);
	}
	// method to log long values
	public static void write(long msg) {
		write("" + msg);
	}
 
}
Posted in Java, JAVA Classes, JAVA Snippits | Leave a comment

Splash Box

Splash box is a very simple java class that provides the needed methods to add an animated splash screen to you application.

This class is released as is and is free to use in any application. The attached zip contains the source for the splash box class and a demo class to show you how to use it. I have used this on a number of applications. its simple and easy to modify Enjoy!

SplashBox.v.1.2.zip

Posted in Java, JAVA Classes | Leave a comment

New Forums

Hi all. At last I have a working forum plugin set up. You can access the new forums via the forum tab at the top of all pages on this site. Enjoy!

Posted in Uncategorized | Leave a comment

OSD 3 Pax Screen

Work is progressing on the new version of OSD. The pax (casualty) details screen is currently being worked on and is almost ready for full integration with the accident and avalanche reports. Below is a small preview of what the final screen will look like. There is still much to be done before its complete, but the code base for the image mapping and injury location selection system is complete. More screens and info will be posted as I progress.

osd3paxReport

I have also started working on the port to Android and will hopefully have a good deal to show for my efforts some time early in the new year.

Posted in Java, OSD | Leave a comment

Adding a mouse listner to a JComboBox

As is often the case its always best to add tooltips and status messages to your application to let the user know whats going on. So I set about today to add in some nice messages to help users understand what all the comboBoxes in OSD application do.

OSD is my resort dispatch application and it uses a status/noticifation bar as well as tool tips to pass messages to the user which are displayed as soon as the mouse enters a component. A JComboBox however is not a single component so adding a mouseListener to one has no effect whats so ever and the methods within it are never called. To get around this an identical mouseListener needs to be added to each of the components within the JComboBox.

Rather than diggin in, adding the mouseListener to each component one at a time and ending the day with loads more code than needed, a better solution I have found is to pull the components out of the JComboBox in to an array then loop through that array and apply the same mouseListener to all the components in one go.

So here is the snippet.

1
2
3
4
5
6
7
8
9
10
11
Component[] components = comboBox.getComponents();
for (int i = 0; i < components.length; i++) {
	components[i].addMouseListener(new MouseAdapter() {
		public void mouseEntered(MouseEvent arg0) {
			doSomthing();
		}
		public void mouseExited(MouseEvent arg0) {
			doSomthing();
		}
	});
}
Posted in Code Tips, Java, JAVA Snippits | Leave a comment

Trigger WindowListner methods manualy

For the last two days I have been banging my had on a problem to get JComboBox to reload its list of selections from a database when the user closed a window by clicking a button. I did not know that the WindowAdapter events where only triggered automatically if the user clicked on the X in the title bar of the JFrame. Well much searching and scratching of my now noticeably balder head I stumbled across a snippet that put me on the right path to the solution.

And here after a few edits is the final bit of code. What this does I’m still not 100% about, but I do know that if you call this line it will trigger WindowAdapter to execute its windowClosing method in the same manner as if the user had clicked the close cross on the title bar

1
getToolkit().getSystemEventQueue().postEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
Posted in Java, JAVA Snippits | Leave a comment

First Look OSD 3.0

Just to let the world know that work is progressing on version 3.0 of OSD.

This all new version is moving OSD away from the browser and on to the desktop. Writen in Java version 3.0 runs equaly well on Windows, Mac, Linux and Android ( IPad version may be available at a much later date) with inmost cases littel change to the layout or look.

Although I’m posting this single scrren as a teaser I have to admit version 3 is at least 3 months from beta release and may be 8 to a stable release but it should be available to users befor the start of next winter with time to train and adapt to the new system

OSD 3.0 has also made the move over to MS SQL Server database for its back end enabling better intergration with more common busness systems than was provided by MySQL. For users wishing to continue with MySQL backends options will be provided to enable this, but backward compatability may not happen as the version 3.0 has changed many of the underlying table structures and datatypes. If there is enough call for code to port data over from MySQL I will work on that after 3.0′s DB structure is finalised.

A full feature list will be provided nearer release date, but if you just can not wait and wish to know more please drop me a comment or email

OSD3.0-firstLook

Posted in Uncategorized | Leave a comment

Java icon / image JButton class

This is a very simple Java class to display a JButton with just an image instead of the normal square button. This class requires a path to a resource that is the image. It only has the constructor method, but inherits all the methods of the JButton class.

Updated to accept a path to a resource or an object of the ImageIcon class. You can also set the tool tip text at the same time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class IconButton extends JButton {
 
	/**
	 * @param icon String Path to image file
	 */
	public IconButton(String icon) {
		setIcon(new ImageIcon(IconButton.class.getResource(icon)));
		init();
	}
 
	/**
	 * @param icon ImageIcon ImageIcon with image to display set in it
	 */
	public IconButton(ImageIcon icon) {
		setIcon(icon);
		init();
	}
 
	/**
	 * @param icon String path to image file
	 * @param toolTip String tool tip message to display on the button
	 */
	public IconButton(String icon, String toolTip) {
		setToolTipText(toolTip);
		setIcon(new ImageIcon(IconButton.class.getResource(icon)));
		init();
	}
 
	/**
	 * @param icon ImageIcon ImageIcon with image to display set in it
	 * @param toolTip String tool tip message to display on the button
	 */ 
	public IconButton(ImageIcon icon, String toolTip) {
		setToolTipText(toolTip);
		setIcon(icon);
		init();
	}
 
	private void init() {
		setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		setBackground(new Color(0, 0, 0, 0));
	}
}

This image shows a button created with this class on the left, and a normal JButton on the right.

Posted in Java, JAVA Classes | Leave a comment

Simple Ajax funciton

Understanding the basics of Ajax can be a trick in its self, getting it to work right is a whole other challenge.

This very basic JavaScript method will take away some of your pain.

add a ‘target’ div to your page where you want the output from your Ajax script to be displayed, give it an ID, create a PHP script to do something with the data your going to the ajax function then send the url to your PHP script, a data string and the ID of the target dive to this method and Bobs your uncle.

A simple example of the data string would be ‘a=123′
and the most basic php ajax script I can up with for that would be …

<?php
echo $_REQUEST['a'];
?>

the above php will return 123 to the ajax function below, which in turn will put that output in to the target div

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * A basic ajax script/function
 * 
 * @param url string The path to the ajax script to execute
 * @param data string a request/POST style string
 * @param eid string The id of the element to send the ajax output to.
 */
function ajax(url, data, eid) {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest;
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (req) {
		dataString = data;
		req.open("POST", url, true);
		req.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", dataString.length);
		req.setRequestHeader("Connection", "close");
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {
					var returnData = req.responseText;
					document.getElementById(eid).innerHTML = returnData;
				}
			}
		};
		req.send(dataString);
	} else {
		alert("Your browser does not support XMLHttpRequest technology (AJAX)!");
	}
}
Posted in JavaScript | Leave a comment

PHP function ratioResize($imageWidth, $imageHeight, $maxThumbX, $maxThumbY)

At some point if your following the path of most any noob PHP developer you will find yourself writing a gallery application of some form. I must have a half dozen under my belt by now.

A common function of such an application is to display some nicely formatted and evenly sized thumb nail images. So how do you get all these randomly sized images to display evenly on your page? maths to the rescue.

This simple function will take the original x and y dimentions of an image and work out the best size to display that image within the given maximum sizes without distorting or trimming the ratios to fit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/** 
* method to return array with new image size fitting 
* within the given $maxThumbX/$maxThumbY ratio
*
* @param int $imageWidth : original width of the image
* @param int $imageHeight : original height of the image
* @param int $maxThumbX : the maximum width allowed
* @param int $maxThumbY : the maximum height allowed
**/
 
	function  ratioResize($imageWidth, $imageHeight, $maxThumbX, $maxThumbY) {
		$width_ratio = ($imageWidth / $maxThumbX);
		$height_ratio = ($imageHeight / $maxThumbY);
		if ($width_ratio >= $height_ratio) {
			$ratio = $width_ratio;
		} else {
			$ratio = $height_ratio;
		}
		$imageXY = array();
		$imageXY ['width'] = ($imageWidth / $ratio);
		$imageXY ['height'] = ($imageHeight / $ratio);
		return $imageXY;
	}
 
//END
Posted in PHP, PHP Snippits | Leave a comment
« Older