0

get_middle()

So I’m guessing anyone who deals with API’s or data from other applications will need this at one point or another. What this does is extract data from a string between the given start and end tags with the option to start the search at any point in the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 	/**
	 * function to return a string from within another string
	 * found between $beginning and $ending
	 *
	 * @param string $source
	 *        	the string to search
	 * @param string $beginning
	 *        	the string that marks the start of the text to extract
	 * @param string $ending
	 *        	the string that marks the end of the text to extract. Default = 0
	 * @param string $init_pos
	 *        	the int possition of start searching from
	 *        	
	 */
	public static function get_middle ($source, $beginning, $ending, $init_pos = 0) {
 
		$beginning_pos = strpos ( $source, $beginning, $init_pos );
		$middle_pos = $beginning_pos + strlen ( $beginning );
		$ending_pos = strpos ( $source, $ending, $beginning_pos + 1 );
		return substr ( $source, $middle_pos, $ending_pos - $middle_pos );
 
	}
0

PanelButton Class

If your looking for a way to get that Windows 8 look and feel for you buttons well try this simple class. Note this class requires the Mig layout which you can find at

    http://www.miglayout.com/

This is simply a JPanel with a text label embedded in it. You need to add in mouse listener event handlers, but I left them out because of the way I sue this class in my project.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
public class PanelButton extends JPanel {
 
	private JLabel textbox = new JLabel("SET LABLE TEXT!");
 
	/**
	 * basic constructor, use the get and set methods to set up the button
	 */
	public PanelButton() {
		setBorder(null);
		setLayout(new MigLayout("", "[grow]", "[grow]"));
		add(textbox, "cell 0 0,alignx center,aligny center");
	}
 
	/**
	 * @param panelColor color for the button
	 * @param textfont font for the text
	 * @param fontColor color for the font 
	 * @param dimension the dimentions of the button
	 */
	public PanelButton(Color panelColor, Font textfont, Color fontColor, Dimension dimension) {
		setBorder(null);
 
		setLayout(new MigLayout("", "[grow]", "[grow]"));
 
		add(textbox, "cell 0 0,alignx center,aligny center");
 
		this.setTextFont(textfont);
		this.setPanelColor(panelColor);
		this.setTextColor(fontColor);
		this.setDimensions(dimension);
	}
 
	public void setText(String text){
		textbox.setText(text);
	}
 
	public void setTextFont(Font textfont) {
		textbox.setFont(textfont);
	}
 
	public void setTextColor(Color color) {
		textbox.setForeground(color);
	}
 
	public void setPanelColor(Color color) {
		setBackground(color);
	}
 
	public void setDimensions(Dimension d) {
		setPreferredSize(d);
		setMinimumSize(d);
		setMaximumSize(d);
		setSize(d);
	}
 
}
0

ImagePanel

This is a simple class that extends the stock JPanel and adds an image to the background.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ImagePanel extends JPanel {
	Image image;
 
	public ImagePanel() {
		try {
			image = Toolkit.getDefaultToolkit().createImage("cache\\img\\logo_bg.png");
		} catch (Exception e) { 
			e.printStackTrace();
		}
		this.repaint();
	}
 
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (image != null)
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
	}
}
0

Operator in PHP varible names!

Hi all. More geeky goodness for ya hehe, So today I came across an interesting issue in PHP. Using the code below to populate an object in PHP from the results of a LDAP search, I found that one of the variables produced in the object had a hyphen( – )in it!

1
foreach ( $details as $key => $d ) { $this->$key = $d; }

When I print_r the object I see this
[userObj] => LDAPUser Object
(
[msds-supportedencryptiontypes] => Array
(
[count] => 1
[0] => 0
)
)

Sorry that’s hard to read, but it shows that the object userOBj of type LDAPUser contains an array named msds-supportedencryptiontypes. This should not be allowed as far as I can tell.

Now as any noob PHP coder should know, there are some rules as to what constitutes a valid variable name in PHP. This code however allows the invalid variable name msds-supportedencryptiontypes to be placed in to the object. This produces no error or warning in any php logs and script executes to completion.

I only found this as I was writing the code to exclude all the unneeded values from the $details array before building the object from it and found that if I reference the variable msds-supportedencryptiontypes I get a property not found error. I’m not sure if this was intentional or is just an over site, but it looks like PHP does not check the value of $key before assigning a value to it. The use of the hyphen in the name of array key is allowed ( though why it would be I don’t know)

Work around? well maybe this is allowed and I stumbled on the correct answer, but after trying for a while to work out how to access this variable I tried this…(just an example)

1
2
$key = 'msds-supportedencryptiontypes' ;
$this->$key = "TEST STRING";

This allowed me to access the invalid array element and change its value.

I do not know if this is a bug or intentional, but it works. I will be trying to avoid this, but I thought others may like to know there is a way around the limit on using at least this operator in a variable name in PHP

0

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);
	}
 
}
0

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

0

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.

0

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();
		}
	});
}
0

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));
0

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