SWT is a great gui toolkit for the java platform which is easy to use, easy to change and what’s more important: it’s fast and native.
I use the “Text” input widget a lot because it’s common in all kinds of applications and whenever you see one of them there is probably a “label”, too…a label that describes what to enter in the input field.
However, there is a nice way to leave the “label” completely out of your gui.
Have a look: 
When you click into the Text-widget, the gray text will vanish. That’s it :D!
Here is the code:
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
/**
*
* @author Andreas Schipplock
*/
public class ASText extends Text {
private String hiddenText = "";
private final Color black;
private final Color gray;
public ASText(Composite parent, int style) {
super(parent, style);
this.black = new Color(super.getDisplay(), 0,0,0);
this.gray = new Color(super.getDisplay(), 119,136,153);
setForeground(gray);
parent.setFocus();
this.addListener(SWT.MouseUp, new Listener () {
public void handleEvent (Event e) {
if (getText().equals(getHiddenText())) {
setText("");
setForeground(black);
}
}
});
}
public void setHiddenText(String text) {
this.hiddenText = text;
}
public String getHiddenText() {
return this.hiddenText;
}
@Override
protected void checkSubclass() {}
}
And here the main to test the Text widget:
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
*
* @author Andreas Schipplock
*/
public class Main {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Test");
shell.setSize(400,250);
// text input single
ASText text1 = new ASText(shell, SWT.BORDER | SWT.SINGLE);
text1.setBounds(2, 2, 230, 20);
text1.setHiddenText("first name");
text1.setText(text1.getHiddenText());
shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Andreas Schipplock.
Hi,
Ihr habt leider einen dicken Fehler in der Webapplikation, mit der man sich seinen Account und somit seine epost Adresse sichern kann…dieser Bug sorgt dafuer, dass ich und jeder andere auch alle moeglichen Adressen sperren kann, so dass sie niemand mehr benutzen kann.
Wenn man das Formular ausfuellt, dann wird einem ja die Adresse vorname.nachname@epost.de angeboten…wenn man in diesem Schritt den Webbrowser schliesst, gilt diese Adresse als vergeben, obwohl man weder eine Bestaetigung noch sonst irgendwas bestaetigt hat.
Ich habe das gerade durch Zufall gemerkt und ich bin entsetzt von solch amateurhafter Programmierung. Ich habe es gerade live erlebt und nun denkt euer System, dass andreas.schipplock@epost.de schon vergeben ist.
Ich kann ja mal bei Gelegenheit darueber bloggen. Es gibt bestimmt genug Kiddies, die sich als Angela Merkel registrieren wollen…Frau Merkel wird sich dann freuen, wenn ihre ID dann angela.merkel.10@epost.de ist…
Ich wuerde vorschlagen, dass ihr das an die Softwareentwicklung weiterleitet, denn solch ein Formular ist fenlerhaft…sowas laesst natuerlich andere Schluesse zu…Sicherheit…wer weiss?
Ich geh davon aus, dass ihr mein Problem nicht loesen koennt, weil meine ID ja nun als verwendet gilt, obwohl ich nichts bestaetigt habe…solltet ihr mein Problem loesen koennen, werde ich mir epost vllt. noch mal angucken…wenn nicht, dann war es das fuer mich und meine Erfahrungen werde ich dann ausfuehrlich dokumentieren. Ich hoffe, dass dieser Bug noch unentdeckt ist, da sonst andere Unfug damit machen und bald niemand mehr seine Id im Format vorname.nachname bekommt.
Ich weiss jetzt nicht mal, wie ich diesen Konflikt loesen kann, denn ich habe nicht mal eine Email von euch bekommen…
M.f.G.
If you work with Postgresql or even Oracle something like foreign keys are used on a daily basis…such constraints are very welcome and used all over the world except in mysql with myisam.
You have to turn on innodb for that.
create table car (
id integer primary key not null auto_increment,
name varchar not null
) ENGINE=INNODB;
create table car_owner (
id integer primary key not null auto_increment,
first_name varchar(100) not null,
last_name varchar(100) not null,
car integer not null,
foreign key (car) references car(id)
) ENGINE=INNODB;
insert into car (name) values ('Audi 80');
insert into car (name) values ('Mercedes 190D');
insert into car_owner (first_name, last_name, car)
values ('Andreas','Schipplock',1); # works!
insert into car_owner (first_name, last_name, car)
values ('Andreas','Schipplock',2); # works!
insert into car_owner (first_name, last_name, car)
values ('Andreas','Schipplock',3); # fails! intended behavior :)
That’s it…simple.
I just had trouble telling cakephp to connect to my oracle 11g R2 but I finally found a solution:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'oracle',
'persistent' => false,
'connect' => 'oci_connect',
'login' => 'OE',
'password' => 'yourpassword',
'database' => 'localhost:1521/orcl',
'prefix' => '',
);
}
Works! Now I can go on playing with cakephp.
Bastian and me; we just talked about how you would insert an item into a simple array at a specified position.
The solution was easy:
function array_insert_item_at_position($array, $position, $item) {
$new_array_before = array_slice($array, 0, $position, true);
$new_array_after = array_slice($array, $position, count($array), true);
$final_array = array_merge($new_array_before, array($item), $new_array_after);
return $final_array;
}
$foobar = array("foo","bar");
print_r($foobar);
$bar = array_insert_item_at_position($foobar,1, "is not");
print_r($bar);
Array
(
[0] => foo
[1] => bar
)
Array
(
[0] => foo
[1] => is not
[2] => bar
)
Works for simple arrays :).
Moin!
Yesterday I was telling you how to write better get and set methods. However, there is another way of doing it even better but I first thought it would limit you in your ability to write useful get and set methods but it’s not.
Just keep in mind that whenever you set a property php5 calls __get or __set and you can use them to make accessor methods like in ruby and I first thought this would limit you in the ability to create unique get and get methods but it’s not because php is able to verify if a named method is available inside the class or not.
That feature got me the following code:
class Car {
private $mileage = 123000;
function __get($f) {
$m = "get_".$f;
return method_exists($this,$m) ? $this->$m() : $this->$f;
}
function __set($f,$v) {
$m = "set_".$f;
return method_exists($this,$m) ? $this->$m($v) : $this->$f = $v;
}
}
$mycar = new Car;
print $mycar->mileage; // 123000
$mycar->mileage = 124000;
print $mycar->mileage; // 124000
Executing this script will print out “123000124000” on your terminal which is perfectly fine though you might think you just set the private property “mileage” to some value but in fact the method __set did set the value for that private property “mileage”.
But now in the near future you decide that mileage can’t just be a simple number but needs some formatting attached to it (which would be bad practice here, but just to be clear), so you want to create your unique get-method, so you simply add it to your implementation:
class Car {
private $mileage = 123000;
function __get($f) {
$m = "get_".$f;
return method_exists($this,$m) ? $this->$m() : $this->$f;
}
function __set($f,$v) {
$m = "set_".$f;
return method_exists($this,$m) ? $this->$m($v) : $this->$f = $v;
}
function get_mileage() {
return $this->mileage." km\n";
}
}
$mycar = new Car;
print $mycar->mileage; // 123000 km\n
$mycar->mileage = 124000;
print $mycar->mileage; // 124000 km\n
Executing this script will append ” km\n” to each “getted” value because “__get” tried to check if there is a “get_mileage” method available which evaluated to true so it simply called it.
Basically that’s it…you can do the same with “set” and just add your unique set-method:
class Car {
private $mileage = 123000;
function __get($f) {
$m = "get_".$f;
return method_exists($this,$m) ? $this->$m() : $this->$f;
}
function __set($f,$v) {
$m = "set_".$f;
return method_exists($this,$m) ? $this->$m($v) : $this->$f = $v;
}
function get_mileage() {
return $this->mileage." km\n";
}
function set_mileage($newMileage) {
if ($newMileage > 100000) {
$this->mileage = 200000;
} else {
$this->mileage = $newMileage;
}
}
}
$mycar = new Car;
print $mycar->mileage; // 123000 km\n
$mycar->mileage = 124000;
print $mycar->mileage; // 200000 km\n
Now if you have a bunch of private properties you don’t need to write accessor methods for all of them. Just when you need them you’ll implement them and you will be fine :).
Andreas Schipplock.



