forked from LupusNobilis/java-org.hwo
Repair
commit
7625c200d9
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||||
|
<classpathentry kind="lib" path="contrib/javassist-3.15.0-GA.jar"/>
|
||||||
|
<classpathentry kind="lib" path="contrib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
|
||||||
|
<classpathentry kind="lib" path="contrib/cglib-nodep-3.1.jar"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,4 @@
|
||||||
|
.DS_Store
|
||||||
|
bin/*
|
||||||
|
|
||||||
|
/bin
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>org.hwo</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,11 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.6
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,61 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
public class BitField {
|
||||||
|
|
||||||
|
private String[] labels;
|
||||||
|
|
||||||
|
public BitField()
|
||||||
|
{
|
||||||
|
labels = new String[32];
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getLabels()
|
||||||
|
{
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BitField(Element element)
|
||||||
|
{
|
||||||
|
labels = new String[32];
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
NodeList bits = element.getElementsByTagName("Bit");
|
||||||
|
for (int i=0;i<bits.getLength();i++)
|
||||||
|
{
|
||||||
|
Element bit = (Element)bits.item(i);
|
||||||
|
|
||||||
|
labels[ Integer.decode(bit.getAttribute("no")) ] = bit.getTextContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
for (int i=0;i<32;i++)
|
||||||
|
labels[i] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toText(Integer value)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
if (value != null)
|
||||||
|
for (int i=0;i<32;i++)
|
||||||
|
{
|
||||||
|
if (labels[i]!=null)
|
||||||
|
if ((value & 1<<i) != 0)
|
||||||
|
{
|
||||||
|
if (sb.toString().length()>0)
|
||||||
|
sb.append(", ");
|
||||||
|
sb.append(labels[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
|
public class ChkSum {
|
||||||
|
|
||||||
|
public static short chksum(byte[] buffer)
|
||||||
|
{
|
||||||
|
return chksum(buffer, 0, buffer.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short chksum(byte[] buffer,int offset,int len)
|
||||||
|
{
|
||||||
|
int sum = 0,
|
||||||
|
xor = 0;
|
||||||
|
|
||||||
|
for (int n = offset; n < (offset + len); n++)
|
||||||
|
{
|
||||||
|
sum += buffer[n];
|
||||||
|
xor ^= buffer[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] t = new byte[12];
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(t).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
bb.putInt(0, sum);
|
||||||
|
bb.putInt(4, xor);
|
||||||
|
bb.put(8,bb.get(0));
|
||||||
|
bb.put(9,bb.get(4));
|
||||||
|
|
||||||
|
return bb.getShort(8);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,118 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
import java.util.prefs.BackingStoreException;
|
||||||
|
import java.util.prefs.Preferences;
|
||||||
|
|
||||||
|
|
||||||
|
public class DBProfile
|
||||||
|
{
|
||||||
|
private Integer profileno;
|
||||||
|
private String bezeichnung;
|
||||||
|
private String hostname;
|
||||||
|
private String database;
|
||||||
|
private Integer port;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public DBProfile(Preferences pref,int profileno)
|
||||||
|
{
|
||||||
|
this.setProfileno(profileno);
|
||||||
|
load(pref);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getProfileno() {
|
||||||
|
return profileno;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(Preferences pref)
|
||||||
|
{
|
||||||
|
Preferences prefs = pref.node("database").node(profileno.toString());
|
||||||
|
|
||||||
|
hostname = prefs.get("hostname", "localhost");
|
||||||
|
database = prefs.get("database", "hworganizer");
|
||||||
|
username = prefs.get("username", "");
|
||||||
|
password = prefs.get("password", "");
|
||||||
|
port = prefs.getInt("port", 5432);
|
||||||
|
bezeichnung = prefs.get("bezeichnung", "Profil " + profileno.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(Preferences pref)
|
||||||
|
{
|
||||||
|
Preferences prefs = pref.node("database").node(profileno.toString());
|
||||||
|
|
||||||
|
prefs.put("hostname", hostname);
|
||||||
|
prefs.put("database", database);
|
||||||
|
prefs.put("username", username);
|
||||||
|
prefs.put("password", password);
|
||||||
|
prefs.putInt("port", port);
|
||||||
|
prefs.put("bezeichnung", bezeichnung);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
prefs.flush();
|
||||||
|
} catch (BackingStoreException bse)
|
||||||
|
{
|
||||||
|
System.err.println("Einstellungen konnten nicht gespeichert werden!");
|
||||||
|
System.err.println(bse.toString());
|
||||||
|
bse.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProfileno(int profileno) {
|
||||||
|
this.profileno = profileno;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHostname() {
|
||||||
|
return hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHostname(String hostname) {
|
||||||
|
this.hostname = hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDatabase() {
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatabase(String database) {
|
||||||
|
this.database = database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(Integer port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBezeichnung() {
|
||||||
|
return bezeichnung;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBezeichnung(String bezeichnung) {
|
||||||
|
this.bezeichnung = bezeichnung;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getBezeichnung();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
public class Reverse {
|
||||||
|
|
||||||
|
byte[] reverseTable;
|
||||||
|
|
||||||
|
public Reverse()
|
||||||
|
{
|
||||||
|
reverseTable = new byte[256];
|
||||||
|
buildReverseTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildReverseTable()
|
||||||
|
{
|
||||||
|
for (short i=0;i<256;i++)
|
||||||
|
{
|
||||||
|
short in = i;
|
||||||
|
byte out = 0;
|
||||||
|
for (int n=0;n<8;n++)
|
||||||
|
{
|
||||||
|
out <<= 1;
|
||||||
|
if ((in & 0x01)==0x01)
|
||||||
|
out |= 0x01;
|
||||||
|
in >>= 1;
|
||||||
|
}
|
||||||
|
reverseTable[in] = out;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte reverse(byte in)
|
||||||
|
{
|
||||||
|
short s = Unsigned.byte2short(in);
|
||||||
|
return reverseTable[s];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package org.hwo;
|
||||||
|
|
||||||
|
public class Unsigned {
|
||||||
|
|
||||||
|
|
||||||
|
static public short byte2short(byte by)
|
||||||
|
{
|
||||||
|
short s;
|
||||||
|
|
||||||
|
if (by < 0)
|
||||||
|
{
|
||||||
|
s = (short)(128 | (by & 0x7f));
|
||||||
|
} else
|
||||||
|
s = by;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,243 @@
|
||||||
|
package org.hwo.beacon;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
|
public class Beacon extends Thread{
|
||||||
|
|
||||||
|
public class BeaconSender
|
||||||
|
{
|
||||||
|
private Properties properties;
|
||||||
|
private InetAddress inetAddress;
|
||||||
|
|
||||||
|
private Long lastBeacon;
|
||||||
|
|
||||||
|
public BeaconSender()
|
||||||
|
{
|
||||||
|
properties = new Properties();
|
||||||
|
lastBeacon = 0l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InetAddress getInetAddress() {
|
||||||
|
return inetAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInetAddress(InetAddress inetAddress) {
|
||||||
|
this.inetAddress = inetAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties getProperties() {
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProperties(Properties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getLastBeacon() {
|
||||||
|
return lastBeacon;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastBeacon(Long lastBeacon) {
|
||||||
|
this.lastBeacon = lastBeacon;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int mode = 0;
|
||||||
|
|
||||||
|
for (String arg:args)
|
||||||
|
{
|
||||||
|
if (arg.equals("-s"))
|
||||||
|
mode = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Beacon beacon = new Beacon(44999);
|
||||||
|
beacon.setBeaconName("Test beacon");
|
||||||
|
|
||||||
|
if (mode == 1)
|
||||||
|
{
|
||||||
|
beacon.start();
|
||||||
|
|
||||||
|
System.console().printf("Press Enter to exit!");
|
||||||
|
System.console().readLine();
|
||||||
|
System.console().printf("exiting...");
|
||||||
|
beacon.exit();
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
beacon.ping();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int port;
|
||||||
|
private boolean exit;
|
||||||
|
private DatagramSocket socket;
|
||||||
|
private Properties properties;
|
||||||
|
private Integer intervall;
|
||||||
|
private String beaconName;
|
||||||
|
private UUID uuid;
|
||||||
|
|
||||||
|
private Hashtable<String, BeaconSender> beaconSenders;
|
||||||
|
|
||||||
|
public Beacon(int port)
|
||||||
|
{
|
||||||
|
this.port = port;
|
||||||
|
this.socket = null;
|
||||||
|
this.properties = new Properties();
|
||||||
|
this.intervall = 5000;
|
||||||
|
this.beaconSenders = new Hashtable<String, Beacon.BeaconSender>();
|
||||||
|
this.beaconName = "DefaultBeacon";
|
||||||
|
this.uuid = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void exit()
|
||||||
|
{
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean getExit()
|
||||||
|
{
|
||||||
|
return exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
Timer pingTimer = new Timer();
|
||||||
|
pingTimer.schedule(new TimerTask() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ping();
|
||||||
|
}
|
||||||
|
}, intervall, intervall);
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket = new DatagramSocket(this.port);
|
||||||
|
DatagramPacket packet = new DatagramPacket(new byte[1500], 1500);
|
||||||
|
|
||||||
|
while (!getExit())
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
socket.receive(packet);
|
||||||
|
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData(),0,packet.getLength());
|
||||||
|
|
||||||
|
BeaconSender bs = beaconSenders.get(packet.getAddress().getHostAddress());
|
||||||
|
if (bs == null)
|
||||||
|
{
|
||||||
|
bs = new BeaconSender();
|
||||||
|
bs.setInetAddress(packet.getAddress());
|
||||||
|
}
|
||||||
|
bs.setLastBeacon((new Date()).getTime());
|
||||||
|
bs.getProperties().load(bis);
|
||||||
|
|
||||||
|
if (bs.getProperties().getProperty("beacon.uuid").equals(uuid.toString()))
|
||||||
|
{
|
||||||
|
System.err.println("Received Local Echo.");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
System.err.println(String.format("Beacon from %s",packet.getAddress().getHostAddress()));
|
||||||
|
System.err.println(String.format("Beacon: %s", bs.getProperties().getProperty("beacon.name")));
|
||||||
|
beaconSenders.put(packet.getAddress().getHostAddress(), bs);
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SocketException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
pingTimer.cancel();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ping()
|
||||||
|
{
|
||||||
|
DatagramSocket sock = socket;
|
||||||
|
try {
|
||||||
|
if (sock == null)
|
||||||
|
sock = new DatagramSocket();
|
||||||
|
} catch (SocketException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
properties.setProperty("beacon.name", beaconName);
|
||||||
|
properties.setProperty("beacon.uuid", uuid.toString());
|
||||||
|
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
properties.store(bos, null);
|
||||||
|
|
||||||
|
byte[] bytes = bos.toByteArray();
|
||||||
|
|
||||||
|
DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
|
||||||
|
|
||||||
|
packet.setPort(this.port);
|
||||||
|
packet.setAddress(InetAddress.getByName("255.255.255.255"));
|
||||||
|
sock.send(packet);
|
||||||
|
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties getProperties() {
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProperties(Properties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIntervall() {
|
||||||
|
return intervall;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntervall(Integer intervall) {
|
||||||
|
this.intervall = intervall;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Hashtable<String, BeaconSender> getBeaconSenders() {
|
||||||
|
return beaconSenders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBeaconSenders(Hashtable<String, BeaconSender> beaconSenders) {
|
||||||
|
this.beaconSenders = beaconSenders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBeaconName() {
|
||||||
|
return beaconName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBeaconName(String beaconName) {
|
||||||
|
this.beaconName = beaconName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package org.hwo.buffer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BitStream {
|
||||||
|
|
||||||
|
private boolean byteOrder;
|
||||||
|
|
||||||
|
private int pByte;
|
||||||
|
private byte pBit;
|
||||||
|
|
||||||
|
private List<Byte> buffer;
|
||||||
|
|
||||||
|
public BitStream()
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
buffer = new ArrayList<Byte>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BitStream(byte[] source)
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
buffer = new ArrayList<Byte>();
|
||||||
|
for (byte b:source)
|
||||||
|
buffer.add(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BitStream(Byte[] source)
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
buffer = new ArrayList<Byte>(Arrays.asList(source));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
pByte = 0;
|
||||||
|
pBit = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private short unsigned2short(byte by)
|
||||||
|
{
|
||||||
|
short s;
|
||||||
|
|
||||||
|
if (by < 0)
|
||||||
|
{
|
||||||
|
s = (short)(128 | (by & 0x7f));
|
||||||
|
} else
|
||||||
|
s = by;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public short read(byte bits)
|
||||||
|
{
|
||||||
|
short symbol = 0;
|
||||||
|
byte position = 0; // Bit-Position im symbol
|
||||||
|
|
||||||
|
while (bits > 0)
|
||||||
|
{
|
||||||
|
symbol <<= 1;
|
||||||
|
|
||||||
|
short s = (short)( unsigned2short(buffer.get(pByte)));
|
||||||
|
|
||||||
|
if (byteOrder)
|
||||||
|
{
|
||||||
|
s >>= pBit;
|
||||||
|
if ((s & 0x01) == 0x01)
|
||||||
|
symbol |= 0x01;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
s <<= pBit;
|
||||||
|
if ((s & 0x80) == 0x80)
|
||||||
|
symbol |= 0x01;
|
||||||
|
};
|
||||||
|
|
||||||
|
pBit++;
|
||||||
|
if (pBit == 8)
|
||||||
|
{
|
||||||
|
pBit = 0;
|
||||||
|
pByte++;
|
||||||
|
}
|
||||||
|
bits--;
|
||||||
|
}
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public byte[] getBuffer()
|
||||||
|
{
|
||||||
|
int len = buffer.size();
|
||||||
|
byte[] out = new byte[ len ];
|
||||||
|
for (int i=0;i<len;i++)
|
||||||
|
out[i] = buffer.get(i);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isByteOrder() {
|
||||||
|
return byteOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setByteOrder(boolean byteOrder) {
|
||||||
|
this.byteOrder = byteOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,167 @@
|
||||||
|
package org.hwo.compressor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import org.hwo.Reverse;
|
||||||
|
import org.hwo.Unsigned;
|
||||||
|
import org.hwo.buffer.BitStream;
|
||||||
|
|
||||||
|
public class LZW {
|
||||||
|
|
||||||
|
static short CLEARCODE = (short)256;
|
||||||
|
static short ENDOFINFORMATION = (short)257;
|
||||||
|
|
||||||
|
ArrayList<Byte[]> stringTable;
|
||||||
|
ArrayList<Byte> omega;
|
||||||
|
|
||||||
|
LinkedList<Byte> uncompressed;
|
||||||
|
BitStream compressed;
|
||||||
|
|
||||||
|
private boolean byteOrder;
|
||||||
|
|
||||||
|
public LZW()
|
||||||
|
{
|
||||||
|
stringTable = new ArrayList<Byte[]>();
|
||||||
|
omega = new ArrayList<Byte>();
|
||||||
|
uncompressed = new LinkedList<Byte>();
|
||||||
|
compressed = new BitStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeStringTable()
|
||||||
|
{
|
||||||
|
stringTable.clear();
|
||||||
|
for (short n=0;n<256;n++)
|
||||||
|
stringTable.add(new Byte[]{ (byte)n });
|
||||||
|
|
||||||
|
stringTable.add(new Byte[0]);
|
||||||
|
stringTable.add(new Byte[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte getSymbolLength()
|
||||||
|
{
|
||||||
|
if (stringTable.size() < 511)
|
||||||
|
return 9;
|
||||||
|
if (stringTable.size() < 1023)
|
||||||
|
return 10;
|
||||||
|
if (stringTable.size() < 2047)
|
||||||
|
return 11;
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void compress()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void decompress()
|
||||||
|
{
|
||||||
|
short symbol,
|
||||||
|
lastsymbol = 0;
|
||||||
|
|
||||||
|
uncompressed.clear();
|
||||||
|
initializeStringTable();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
while ( (symbol = compressed.read(getSymbolLength())) != ENDOFINFORMATION)
|
||||||
|
{
|
||||||
|
if (symbol == CLEARCODE)
|
||||||
|
{
|
||||||
|
initializeStringTable();
|
||||||
|
symbol = compressed.read(getSymbolLength());
|
||||||
|
if (symbol == ENDOFINFORMATION)
|
||||||
|
break;
|
||||||
|
|
||||||
|
uncompressed.addAll(Arrays.asList(stringTable.get(symbol)));
|
||||||
|
lastsymbol = symbol;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if (symbol < stringTable.size())
|
||||||
|
{
|
||||||
|
Byte[] codeString = stringTable.get(symbol);
|
||||||
|
uncompressed.addAll(Arrays.asList(codeString));
|
||||||
|
|
||||||
|
Byte[] b = stringTable.get(lastsymbol);
|
||||||
|
Byte[] nb = Arrays.copyOf(b, b.length + 1 );
|
||||||
|
nb[ b.length ] = codeString[0];
|
||||||
|
stringTable.add(nb);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Byte[] b = stringTable.get(lastsymbol);
|
||||||
|
Byte[] nb = Arrays.copyOf(b, b.length + 1 );
|
||||||
|
nb[ b.length ] = b[0];
|
||||||
|
stringTable.add(nb);
|
||||||
|
uncompressed.addAll(Arrays.asList(nb));
|
||||||
|
}
|
||||||
|
lastsymbol = symbol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Byte[] getBoxedUncompressedData()
|
||||||
|
{
|
||||||
|
return uncompressed.toArray(new Byte[0]);
|
||||||
|
}
|
||||||
|
public byte[] getUncompressedData()
|
||||||
|
{
|
||||||
|
Byte[] boxed = getBoxedUncompressedData();
|
||||||
|
byte[] unboxed = new byte[ boxed.length ];
|
||||||
|
|
||||||
|
for (int i=0;i<boxed.length;i++)
|
||||||
|
unboxed[i] = boxed[i];
|
||||||
|
|
||||||
|
return unboxed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setUncompressedData(Byte[] data)
|
||||||
|
{
|
||||||
|
uncompressed.clear();
|
||||||
|
uncompressed.addAll(Arrays.asList(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte[] getBoxedCompressedData()
|
||||||
|
{
|
||||||
|
byte[] unboxed = compressed.getBuffer();
|
||||||
|
Byte[] boxed = new Byte[ unboxed.length ];
|
||||||
|
for (int i=0;i<unboxed.length;i++)
|
||||||
|
boxed[i] = unboxed[i];
|
||||||
|
return boxed;
|
||||||
|
}
|
||||||
|
public byte[] getCompressedData()
|
||||||
|
{
|
||||||
|
return compressed.getBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompressedData(Byte[] data)
|
||||||
|
{
|
||||||
|
compressed = new BitStream(data);
|
||||||
|
}
|
||||||
|
public void setCompressedData(byte[] data)
|
||||||
|
{
|
||||||
|
compressed = new BitStream(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isByteOrder() {
|
||||||
|
return byteOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setByteOrder(boolean byteOrder) {
|
||||||
|
this.byteOrder = byteOrder;
|
||||||
|
this.compressed.setByteOrder(byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,235 @@
|
||||||
|
package org.hwo.csv;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import javassist.bytecode.EnclosingMethodAttribute;
|
||||||
|
|
||||||
|
public class CSV {
|
||||||
|
|
||||||
|
List<String[]> records;
|
||||||
|
|
||||||
|
private char separator;
|
||||||
|
private char enclosedBy;
|
||||||
|
|
||||||
|
boolean isEnclosed;
|
||||||
|
List<String> parserRecord;
|
||||||
|
List<Character> parserField;
|
||||||
|
char lastChar;
|
||||||
|
|
||||||
|
public CSV()
|
||||||
|
{
|
||||||
|
this.records = new LinkedList<String[]>();
|
||||||
|
this.setSeparator(';');
|
||||||
|
this.setEnclosedBy('\"');
|
||||||
|
|
||||||
|
this.parserRecord = new ArrayList<String>();
|
||||||
|
this.parserField = new ArrayList<Character>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void nextRecord()
|
||||||
|
{
|
||||||
|
nextField();
|
||||||
|
|
||||||
|
this.records.add(parserRecord.toArray(new String[]{}));
|
||||||
|
parserRecord.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void nextField()
|
||||||
|
{
|
||||||
|
char fieldChars[] = new char[ parserField.size() ];
|
||||||
|
|
||||||
|
for (int i=0;i<parserField.size(); i++)
|
||||||
|
fieldChars[i] = parserField.get(i);
|
||||||
|
|
||||||
|
parserRecord.add(String.valueOf(fieldChars));
|
||||||
|
parserField.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void feeder(char ch)
|
||||||
|
{
|
||||||
|
if (ch == getEnclosedBy())
|
||||||
|
{
|
||||||
|
isEnclosed = !isEnclosed;
|
||||||
|
|
||||||
|
if (isEnclosed && lastChar == getEnclosedBy())
|
||||||
|
parserField.add(getEnclosedBy());
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if (isEnclosed)
|
||||||
|
parserField.add(ch);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ch == '\r')
|
||||||
|
{
|
||||||
|
// Ignore \r
|
||||||
|
} else if (ch == '\n')
|
||||||
|
nextRecord();
|
||||||
|
else if (ch == getSeparator())
|
||||||
|
nextField();
|
||||||
|
else
|
||||||
|
parserField.add(ch);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
lastChar = ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readFromFile(String filename)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
readFromStream(new FileInputStream(filename));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readFromStream(InputStream is) throws IOException
|
||||||
|
{
|
||||||
|
this.readFromStream(new InputStreamReader(is,"ISO-8859-1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readFromStream(InputStreamReader isr) throws IOException
|
||||||
|
{
|
||||||
|
BufferedReader br = new BufferedReader(isr);
|
||||||
|
|
||||||
|
isEnclosed = false;
|
||||||
|
records.clear();
|
||||||
|
parserField.clear();
|
||||||
|
parserRecord.clear();
|
||||||
|
|
||||||
|
while (br.ready())
|
||||||
|
feeder((char)br.read());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void saveToFile(String filename)
|
||||||
|
{
|
||||||
|
saveToFile(new File(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveToFile(File output)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream(output,false);
|
||||||
|
saveToStream(fos);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveToStream(OutputStream output)
|
||||||
|
{
|
||||||
|
OutputStreamWriter osw;
|
||||||
|
try {
|
||||||
|
osw = new OutputStreamWriter(output,"UTF8");
|
||||||
|
saveToStream(osw);
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveToStream(OutputStreamWriter output)
|
||||||
|
{
|
||||||
|
BufferedWriter writer = new BufferedWriter(output);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
for (String[] record:this.records)
|
||||||
|
{
|
||||||
|
for (int i=0;i<record.length;i++)
|
||||||
|
{
|
||||||
|
if (i>0)
|
||||||
|
writer.write(separator);
|
||||||
|
|
||||||
|
if (record[i] != null)
|
||||||
|
{
|
||||||
|
if (String.class.isInstance(record[i]))
|
||||||
|
writer.write(enclosedBy);
|
||||||
|
|
||||||
|
writer.write(record[i]);
|
||||||
|
|
||||||
|
if (String.class.isInstance(record[i]))
|
||||||
|
writer.write(enclosedBy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.write("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue(int row,int column)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return this.records.get(row)[column];
|
||||||
|
} catch (ArrayIndexOutOfBoundsException ex)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String[]> getCells()
|
||||||
|
{
|
||||||
|
return this.records;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Object[]> getCellsAsObjects()
|
||||||
|
{
|
||||||
|
List<Object[]> ol = new ArrayList<Object[]>();
|
||||||
|
|
||||||
|
for (String[] record:records)
|
||||||
|
{
|
||||||
|
Object[] ro = new Object[record.length];
|
||||||
|
for (int i=0;i<ro.length;i++)
|
||||||
|
ro[i] = record[i];
|
||||||
|
ol.add(ro);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public char getSeparator() {
|
||||||
|
return separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSeparator(char separator) {
|
||||||
|
this.separator = separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public char getEnclosedBy() {
|
||||||
|
return enclosedBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnclosedBy(char enclosedBy) {
|
||||||
|
this.enclosedBy = enclosedBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package org.hwo.datetime;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class DateTime {
|
||||||
|
|
||||||
|
|
||||||
|
public static String getDateCode()
|
||||||
|
{
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
return sdf.format(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,162 @@
|
||||||
|
package org.hwo.datetime;
|
||||||
|
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
|
||||||
|
import javax.swing.JSpinner;
|
||||||
|
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.SpinnerNumberModel;
|
||||||
|
import javax.swing.event.ChangeEvent;
|
||||||
|
import javax.swing.event.ChangeListener;
|
||||||
|
|
||||||
|
public class JTimeOfDay extends JPanel {
|
||||||
|
|
||||||
|
private TimeOfDay timeOfDay;
|
||||||
|
private LinkedList<ChangeListener> changeListeners;
|
||||||
|
private JSpinner spHours;
|
||||||
|
private JSpinner spMinutes;
|
||||||
|
private JSpinner spSeconds;
|
||||||
|
private JLabel lColon2;
|
||||||
|
|
||||||
|
private boolean hideSeconds;
|
||||||
|
|
||||||
|
|
||||||
|
public JTimeOfDay() {
|
||||||
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||||
|
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
|
||||||
|
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||||
|
gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
|
||||||
|
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||||
|
setLayout(gridBagLayout);
|
||||||
|
|
||||||
|
spHours = new JSpinner();
|
||||||
|
spHours.addChangeListener(new ChangeListener() {
|
||||||
|
public void stateChanged(ChangeEvent e) {
|
||||||
|
timeOfDay.setHours((Integer)spHours.getValue());
|
||||||
|
fireChange();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
spHours.setModel(new SpinnerNumberModel(0, 0, 23, 1));
|
||||||
|
GridBagConstraints gbc_spHours = new GridBagConstraints();
|
||||||
|
gbc_spHours.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_spHours.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_spHours.gridx = 0;
|
||||||
|
gbc_spHours.gridy = 0;
|
||||||
|
add(spHours, gbc_spHours);
|
||||||
|
|
||||||
|
JLabel lblNewLabel = new JLabel(":");
|
||||||
|
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_lblNewLabel.gridx = 1;
|
||||||
|
gbc_lblNewLabel.gridy = 0;
|
||||||
|
add(lblNewLabel, gbc_lblNewLabel);
|
||||||
|
|
||||||
|
spMinutes = new JSpinner();
|
||||||
|
spMinutes.addChangeListener(new ChangeListener() {
|
||||||
|
public void stateChanged(ChangeEvent e) {
|
||||||
|
timeOfDay.setMinutes((Integer)spMinutes.getValue());
|
||||||
|
fireChange();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
spMinutes.setModel(new SpinnerNumberModel(0, 0, 59, 1));
|
||||||
|
GridBagConstraints gbc_spMinutes = new GridBagConstraints();
|
||||||
|
gbc_spMinutes.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_spMinutes.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_spMinutes.gridx = 2;
|
||||||
|
gbc_spMinutes.gridy = 0;
|
||||||
|
add(spMinutes, gbc_spMinutes);
|
||||||
|
|
||||||
|
lColon2 = new JLabel(":");
|
||||||
|
GridBagConstraints gbc_lColon2 = new GridBagConstraints();
|
||||||
|
gbc_lColon2.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_lColon2.gridx = 3;
|
||||||
|
gbc_lColon2.gridy = 0;
|
||||||
|
add(lColon2, gbc_lColon2);
|
||||||
|
|
||||||
|
spSeconds = new JSpinner();
|
||||||
|
spSeconds.addChangeListener(new ChangeListener() {
|
||||||
|
public void stateChanged(ChangeEvent e) {
|
||||||
|
timeOfDay.setSeconds((Integer)spSeconds.getValue());
|
||||||
|
fireChange();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
spSeconds.setModel(new SpinnerNumberModel(0, 0, 59, 1));
|
||||||
|
GridBagConstraints gbc_spSeconds = new GridBagConstraints();
|
||||||
|
gbc_spSeconds.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_spSeconds.gridx = 4;
|
||||||
|
gbc_spSeconds.gridy = 0;
|
||||||
|
add(spSeconds, gbc_spSeconds);
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void initialize()
|
||||||
|
{
|
||||||
|
setSecondsHidden(false);
|
||||||
|
|
||||||
|
changeListeners = new LinkedList<ChangeListener>();
|
||||||
|
timeOfDay = new TimeOfDay();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addChangeListener(ChangeListener changeListener)
|
||||||
|
{
|
||||||
|
changeListeners.add(changeListener);
|
||||||
|
}
|
||||||
|
public void removeChangeListener(ChangeListener changeListener)
|
||||||
|
{
|
||||||
|
changeListeners.remove(changeListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getSecondsHidden()
|
||||||
|
{
|
||||||
|
return this.hideSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecondsHidden(Boolean hideSeconds)
|
||||||
|
{
|
||||||
|
this.hideSeconds = hideSeconds;
|
||||||
|
if (hideSeconds)
|
||||||
|
{
|
||||||
|
lColon2.setVisible(false);
|
||||||
|
spSeconds.setVisible(false);
|
||||||
|
spSeconds.setValue(0);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
lColon2.setVisible(true);
|
||||||
|
spSeconds.setVisible(true);
|
||||||
|
}
|
||||||
|
this.validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void fireChange()
|
||||||
|
{
|
||||||
|
for (ChangeListener l:changeListeners)
|
||||||
|
l.stateChanged(new ChangeEvent(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateView()
|
||||||
|
{
|
||||||
|
spHours.setValue(timeOfDay.getHours());
|
||||||
|
spMinutes.setValue(timeOfDay.getMinutes());
|
||||||
|
spSeconds.setValue(timeOfDay.getSeonds());
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeOfDay getTimeOfDay()
|
||||||
|
{
|
||||||
|
return timeOfDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeOfDay(TimeOfDay timeOfDay)
|
||||||
|
{
|
||||||
|
this.timeOfDay = timeOfDay;
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
package org.hwo.datetime;
|
||||||
|
|
||||||
|
public class TimeOfDay {
|
||||||
|
|
||||||
|
public static int getHours(int secondsOfDay)
|
||||||
|
{
|
||||||
|
return secondsOfDay / 3600;
|
||||||
|
}
|
||||||
|
public static int getMinutes(int secondsOfDay)
|
||||||
|
{
|
||||||
|
return (secondsOfDay % 3600) / 60;
|
||||||
|
}
|
||||||
|
public static int getSeconds(int secondsOfDay)
|
||||||
|
{
|
||||||
|
return (secondsOfDay % 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int secondsOfDay;
|
||||||
|
|
||||||
|
public TimeOfDay()
|
||||||
|
{
|
||||||
|
secondsOfDay = 0;
|
||||||
|
}
|
||||||
|
public TimeOfDay(int sod)
|
||||||
|
{
|
||||||
|
secondsOfDay = sod;
|
||||||
|
}
|
||||||
|
public TimeOfDay(int hour,int minute,int second)
|
||||||
|
{
|
||||||
|
initialize(hour, minute, second);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize(int hour,int minute,int second)
|
||||||
|
{
|
||||||
|
secondsOfDay = (hour * 3600) + (minute * 60) + second;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSeondsOfDay()
|
||||||
|
{
|
||||||
|
return secondsOfDay;
|
||||||
|
}
|
||||||
|
public void setSecondsOfDay(int secondsOfDay)
|
||||||
|
{
|
||||||
|
this.secondsOfDay = secondsOfDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHours()
|
||||||
|
{
|
||||||
|
return getHours(secondsOfDay);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMinutes()
|
||||||
|
{
|
||||||
|
return getMinutes(secondsOfDay);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSeonds()
|
||||||
|
{
|
||||||
|
return getSeconds(secondsOfDay);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHours(int hours)
|
||||||
|
{
|
||||||
|
initialize(hours, getMinutes(), getSeonds());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinutes(int minutes)
|
||||||
|
{
|
||||||
|
initialize(getHours(), minutes, getSeonds());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSeconds(int seconds)
|
||||||
|
{
|
||||||
|
initialize(getHours(), getMinutes(), seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package org.hwo.fifo;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FiFo<T> {
|
||||||
|
|
||||||
|
private LinkedList<T> elements;
|
||||||
|
|
||||||
|
public FiFo()
|
||||||
|
{
|
||||||
|
elements = new LinkedList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(T element)
|
||||||
|
{
|
||||||
|
elements.add(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T pull()
|
||||||
|
{
|
||||||
|
return elements.poll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T peek()
|
||||||
|
{
|
||||||
|
return elements.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> getElements()
|
||||||
|
{
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty()
|
||||||
|
{
|
||||||
|
return elements.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasElements()
|
||||||
|
{
|
||||||
|
return !elements.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
package org.hwo.i18n;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.MissingResourceException;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class Messages {
|
||||||
|
|
||||||
|
protected static Messages pInstance;
|
||||||
|
protected static List<Messages> instanceList;
|
||||||
|
protected static Locale activeLocale;
|
||||||
|
|
||||||
|
protected String BUNDLE_NAME = "org.hwo.i18n.messages";
|
||||||
|
|
||||||
|
|
||||||
|
private ResourceBundle defaultResourceBundle;
|
||||||
|
private ResourceBundle localeResourceBundle;
|
||||||
|
|
||||||
|
protected Messages() {
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Messages(String bundleName)
|
||||||
|
{
|
||||||
|
BUNDLE_NAME = bundleName;
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
System.err.println(this.getClass().getName() + ": Using Locale:" + activeLocale.getCountry() + " / " + activeLocale.getLanguage());
|
||||||
|
defaultResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
|
||||||
|
localeResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME,activeLocale);
|
||||||
|
|
||||||
|
instanceList.add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getString(String key) {
|
||||||
|
for (Messages messages: instanceList)
|
||||||
|
{
|
||||||
|
if (messages.hasKey(key))
|
||||||
|
return messages.getInstanceString(key);
|
||||||
|
}
|
||||||
|
return '!' + key + '!';
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasKey(String key)
|
||||||
|
{
|
||||||
|
return localeResourceBundle.containsKey(key) | defaultResourceBundle.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInstanceString(String key)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (localeResourceBundle.containsKey(key))
|
||||||
|
return localeResourceBundle.getString(key);
|
||||||
|
if (defaultResourceBundle.containsKey(key))
|
||||||
|
return defaultResourceBundle.getString(key);
|
||||||
|
return '!' + key + '!';
|
||||||
|
} catch (MissingResourceException e) {
|
||||||
|
return '!' + key + '!';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Messages getInstance()
|
||||||
|
{
|
||||||
|
return pInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
if (activeLocale == null)
|
||||||
|
activeLocale = Locale.getDefault();
|
||||||
|
|
||||||
|
if (instanceList == null)
|
||||||
|
instanceList = new LinkedList<Messages>();
|
||||||
|
|
||||||
|
if (pInstance == null)
|
||||||
|
pInstance = new Messages();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
org.hwo=HWOs Java Framework
|
||||||
|
interface=Schnittstelle
|
||||||
|
ok=OK
|
||||||
|
cancel=abbrechen
|
||||||
|
SerialPortChooser.0=Schnittstelle w\u00E4hlen
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
org.hwo=HWOs Java Framework
|
||||||
|
interface=Interface
|
||||||
|
ok=OK
|
||||||
|
cancel=Cancel
|
||||||
|
SerialPortChooser.0=Select Interface
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.hwo.image;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.RenderedImage;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public interface ImageFile {
|
||||||
|
|
||||||
|
public ImageFile newInstance();
|
||||||
|
|
||||||
|
public Boolean identify(File imageFile);
|
||||||
|
|
||||||
|
public void reset();
|
||||||
|
public void load(File imageFile);
|
||||||
|
public void save(File imageFile);
|
||||||
|
|
||||||
|
public Integer getNumPages();
|
||||||
|
public BufferedImage getPage(Integer pageNo);
|
||||||
|
|
||||||
|
public void addPage(BufferedImage image);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
package org.hwo.image;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.hwo.image.imageio.ImageIOFile;
|
||||||
|
import org.hwo.image.tiff.TiffFile;
|
||||||
|
|
||||||
|
public class ImageProvider {
|
||||||
|
|
||||||
|
private static ImageProvider imageProvider;
|
||||||
|
|
||||||
|
public static ImageProvider instance()
|
||||||
|
{
|
||||||
|
if (imageProvider == null)
|
||||||
|
imageProvider = new ImageProvider();
|
||||||
|
return new ImageProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageFile[] imageFiles = {
|
||||||
|
new TiffFile(),
|
||||||
|
new ImageIOFile("png"),
|
||||||
|
new ImageIOFile("jpg")
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
protected ImageProvider()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageFile load(File file)
|
||||||
|
{
|
||||||
|
for (ImageFile imageFile:imageFiles)
|
||||||
|
{
|
||||||
|
if (imageFile.identify(file))
|
||||||
|
{
|
||||||
|
ImageFile ifile = imageFile.newInstance();
|
||||||
|
ifile.load(file);
|
||||||
|
return ifile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageFile load(String path)
|
||||||
|
{
|
||||||
|
return load(new File(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(BufferedImage image,File imageFile)
|
||||||
|
{
|
||||||
|
for (ImageFile ifile:imageFiles)
|
||||||
|
{
|
||||||
|
if (ifile.identify(imageFile))
|
||||||
|
{
|
||||||
|
ImageFile writer = ifile.newInstance();
|
||||||
|
writer.addPage(image);
|
||||||
|
writer.save(imageFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package org.hwo.image;
|
||||||
|
|
||||||
|
public class UnsupportedFormatException extends RuntimeException {
|
||||||
|
|
||||||
|
public UnsupportedFormatException()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnsupportedFormatException(String message)
|
||||||
|
{
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
package org.hwo.image.imageio;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.imageio.ImageReader;
|
||||||
|
import javax.imageio.ImageWriter;
|
||||||
|
import javax.imageio.stream.ImageInputStream;
|
||||||
|
import javax.imageio.stream.ImageOutputStream;
|
||||||
|
|
||||||
|
import org.hwo.image.ImageFile;
|
||||||
|
|
||||||
|
public class ImageIOFile implements ImageFile {
|
||||||
|
|
||||||
|
private String imageType;
|
||||||
|
private ImageReader imageReader;
|
||||||
|
private ImageWriter imageWriter;
|
||||||
|
|
||||||
|
private LinkedList<BufferedImage> images;
|
||||||
|
|
||||||
|
public ImageIOFile(String imageType)
|
||||||
|
{
|
||||||
|
this.imageType = imageType;
|
||||||
|
imageReader = ImageIO.getImageReadersByFormatName(imageType).next();
|
||||||
|
imageWriter = ImageIO.getImageWritersByFormatName(imageType).next();
|
||||||
|
images = new LinkedList<BufferedImage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ImageFile newInstance() {
|
||||||
|
return new ImageIOFile(imageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean identify(File imageFile) {
|
||||||
|
return imageFile.getName().endsWith("." + imageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
imageReader.reset();
|
||||||
|
imageWriter.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(File imageFile) {
|
||||||
|
reset();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
|
||||||
|
imageReader.setInput(iis);
|
||||||
|
|
||||||
|
images.clear();
|
||||||
|
|
||||||
|
int nImages = imageReader.getNumImages(true);
|
||||||
|
|
||||||
|
for (int n=0;n<nImages;n++)
|
||||||
|
images.add(imageReader.read(n));
|
||||||
|
|
||||||
|
iis.close();
|
||||||
|
|
||||||
|
imageReader.reset();
|
||||||
|
} catch (IOException ioe)
|
||||||
|
{
|
||||||
|
ioe.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(File imageFile) {
|
||||||
|
reset();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ImageOutputStream ios = ImageIO.createImageOutputStream(imageFile);
|
||||||
|
imageWriter.setOutput(ios);
|
||||||
|
|
||||||
|
for (int n=0;n<images.size();n++)
|
||||||
|
imageWriter.write(images.get(n));
|
||||||
|
|
||||||
|
ios.close();
|
||||||
|
|
||||||
|
imageWriter.reset();
|
||||||
|
} catch (IOException ioe)
|
||||||
|
{
|
||||||
|
ioe.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getNumPages() {
|
||||||
|
return images.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage getPage(Integer pageNo) {
|
||||||
|
return images.get(pageNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addPage(BufferedImage image) {
|
||||||
|
images.add(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,358 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.hwo.Unsigned;
|
||||||
|
import org.hwo.image.UnsupportedFormatException;
|
||||||
|
|
||||||
|
class IFD
|
||||||
|
{
|
||||||
|
final TiffFile tiffFile;
|
||||||
|
|
||||||
|
boolean decoded;
|
||||||
|
|
||||||
|
Short numEntries;
|
||||||
|
HashMap<Short, IFDEntry> entries;
|
||||||
|
|
||||||
|
BufferedImage bufferedImage;
|
||||||
|
|
||||||
|
protected IFD(TiffFile tiffFile)
|
||||||
|
{
|
||||||
|
this.tiffFile = tiffFile;
|
||||||
|
decoded = false;
|
||||||
|
entries = new HashMap<Short, IFDEntry>();
|
||||||
|
bufferedImage = null;
|
||||||
|
|
||||||
|
numEntries = this.tiffFile.sourceBuffer.getShort();
|
||||||
|
System.err.println(String.format("readIFD(): reading %d entries",numEntries));
|
||||||
|
|
||||||
|
for (int i=0;i<numEntries;i++)
|
||||||
|
{
|
||||||
|
IFDEntry entry = IFDEntry.read(this);
|
||||||
|
if (entry != null)
|
||||||
|
entries.put(entry.tag, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer nextIFD = this.tiffFile.sourceBuffer.getInt();
|
||||||
|
if (nextIFD != 0)
|
||||||
|
{
|
||||||
|
this.tiffFile.sourceBuffer.position(nextIFD);
|
||||||
|
this.tiffFile.ifds.add(0,new IFD(this.tiffFile));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected IFD(TiffFile tiffFile, BufferedImage image)
|
||||||
|
{
|
||||||
|
this.tiffFile = tiffFile;
|
||||||
|
decoded = true;
|
||||||
|
bufferedImage = image;
|
||||||
|
numEntries = 0;
|
||||||
|
entries = new HashMap<Short, IFDEntry>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDecoded()
|
||||||
|
{
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BufferedImage decode()
|
||||||
|
{
|
||||||
|
System.err.println("TiffFile: Decoding...");
|
||||||
|
if (isDecoded())
|
||||||
|
return bufferedImage;
|
||||||
|
|
||||||
|
int width = getWidth(),
|
||||||
|
height = getHeight();
|
||||||
|
|
||||||
|
Short compression = getCompression();
|
||||||
|
|
||||||
|
short[] bitsPerSample = getBitsPerSample();
|
||||||
|
PHOTOMETRIC photometric = getPhotometric();
|
||||||
|
short samplesPerPixel = getSamplesPerPixel();
|
||||||
|
|
||||||
|
System.err.println(String.format("Size: %dx%d TYPE: %s COMP: %d BPS: %s",width,height,photometric.toString(),compression,Arrays.toString(bitsPerSample)));
|
||||||
|
|
||||||
|
BufferedImage image = new BufferedImage(width, height, getJImageType(photometric, bitsPerSample));
|
||||||
|
|
||||||
|
int rowsPerStrip = getRowsPerStrip();
|
||||||
|
int[] stripOffsets = getStripOffsets();
|
||||||
|
int[] stripByteCounts = getStripByteCounts();
|
||||||
|
|
||||||
|
if (stripOffsets.length != stripByteCounts.length)
|
||||||
|
throw new UnsupportedFormatException("StripOffset.length != StripbyteCount.length");
|
||||||
|
|
||||||
|
if ((stripOffsets.length * rowsPerStrip) < height)
|
||||||
|
throw new UnsupportedFormatException("stripped lines < scanlines");
|
||||||
|
|
||||||
|
ArrayList<Strip> strips = new ArrayList<Strip>();
|
||||||
|
|
||||||
|
for (int i=0;i<stripOffsets.length;i++)
|
||||||
|
strips.add(new Strip(this, stripOffsets[i], stripByteCounts[i], rowsPerStrip));
|
||||||
|
|
||||||
|
switch (photometric)
|
||||||
|
{
|
||||||
|
case BLACK:
|
||||||
|
case WHITE:
|
||||||
|
decodeGray(image,strips);
|
||||||
|
break;
|
||||||
|
case RGB:
|
||||||
|
decodeRGB(image,strips);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new UnsupportedFormatException("unsupported pixel format");
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decodeGray(BufferedImage image,List<Strip> strips)
|
||||||
|
{
|
||||||
|
PHOTOMETRIC photometric = getPhotometric();
|
||||||
|
|
||||||
|
int width = getWidth(),
|
||||||
|
height = getHeight();
|
||||||
|
|
||||||
|
short[] bitsPerSample = getBitsPerSample();
|
||||||
|
short samplesPerPixel = getSamplesPerPixel();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
switch (bitsPerSample[0])
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
case 4:
|
||||||
|
case 8:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new UnsupportedFormatException();
|
||||||
|
}
|
||||||
|
|
||||||
|
int y = 0;
|
||||||
|
short predictor = getPredictor();
|
||||||
|
|
||||||
|
for (Strip strip:strips)
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = strip.getBuffer();
|
||||||
|
int p = 0;
|
||||||
|
|
||||||
|
for (int row=0; (row < strip.rowcount) && (y + row < height); row++)
|
||||||
|
{
|
||||||
|
int[] lastPixel = new int[0];
|
||||||
|
|
||||||
|
for (int column = 0; column < width ; column++)
|
||||||
|
{
|
||||||
|
int[] pixel = new int[1];
|
||||||
|
|
||||||
|
pixel[0] = Unsigned.byte2short(buffer.get(p));
|
||||||
|
if (predictor == 2 && column > 0)
|
||||||
|
pixel[0] += lastPixel[0];
|
||||||
|
|
||||||
|
image.getRaster().setPixel(column, y + row, pixel);
|
||||||
|
|
||||||
|
p += samplesPerPixel;
|
||||||
|
lastPixel = pixel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
y += strip.rowcount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decodeRGB(BufferedImage image,List<Strip> strips)
|
||||||
|
{
|
||||||
|
int width = getWidth(),
|
||||||
|
height = getHeight();
|
||||||
|
|
||||||
|
short[] bitsPerSample = getBitsPerSample();
|
||||||
|
short samplesPerPixel = getSamplesPerPixel();
|
||||||
|
|
||||||
|
switch (samplesPerPixel)
|
||||||
|
{
|
||||||
|
case 3:
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new UnsupportedFormatException();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0;i<samplesPerPixel;i++)
|
||||||
|
if (bitsPerSample[i] != 8)
|
||||||
|
throw new UnsupportedFormatException();
|
||||||
|
|
||||||
|
int y = 0;
|
||||||
|
|
||||||
|
short predictor = getPredictor();
|
||||||
|
|
||||||
|
for (Strip strip:strips)
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = strip.getBuffer();
|
||||||
|
int p = 0;
|
||||||
|
|
||||||
|
for (int row=0; (row < strip.rowcount) && (y + row < height); row++)
|
||||||
|
{
|
||||||
|
int[] lastPixel = new int[0];
|
||||||
|
|
||||||
|
for (int column = 0; column < width ; column++)
|
||||||
|
{
|
||||||
|
byte r,g,b,a;
|
||||||
|
|
||||||
|
int[] pixel = new int[samplesPerPixel];
|
||||||
|
|
||||||
|
for (int n=0;n<samplesPerPixel;n++)
|
||||||
|
{
|
||||||
|
pixel[n] = Unsigned.byte2short(buffer.get(p + n));
|
||||||
|
if (predictor == 2 && column > 0)
|
||||||
|
{
|
||||||
|
pixel[n] += lastPixel[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
image.getRaster().setPixel(column, y + row, pixel);
|
||||||
|
|
||||||
|
p += samplesPerPixel;
|
||||||
|
lastPixel = pixel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
y += strip.rowcount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedImage getBufferedImage()
|
||||||
|
{
|
||||||
|
if (bufferedImage == null)
|
||||||
|
bufferedImage = decode();
|
||||||
|
return bufferedImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getJImageType(PHOTOMETRIC p,short[] bitsPerSample)
|
||||||
|
{
|
||||||
|
switch (p)
|
||||||
|
{
|
||||||
|
case WHITE:
|
||||||
|
case BLACK:
|
||||||
|
return BufferedImage.TYPE_BYTE_GRAY;
|
||||||
|
case RGB:
|
||||||
|
switch (bitsPerSample.length)
|
||||||
|
{
|
||||||
|
case 3:
|
||||||
|
return BufferedImage.TYPE_3BYTE_BGR;
|
||||||
|
case 4:
|
||||||
|
return BufferedImage.TYPE_4BYTE_ABGR;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PALETTE:
|
||||||
|
return BufferedImage.TYPE_3BYTE_BGR;
|
||||||
|
}
|
||||||
|
throw new UnsupportedFormatException();
|
||||||
|
}
|
||||||
|
|
||||||
|
IFDEntry getEntry(short tag)
|
||||||
|
{
|
||||||
|
if (entries.containsKey(tag))
|
||||||
|
return entries.get(tag);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getWidth()
|
||||||
|
{
|
||||||
|
IFDEntry e = getEntry((short)256);
|
||||||
|
if (e == null)
|
||||||
|
throw new UnsupportedFormatException("missing width tag");
|
||||||
|
return ((NumericEntry)e).getInteger();
|
||||||
|
}
|
||||||
|
|
||||||
|
int getHeight()
|
||||||
|
{
|
||||||
|
IFDEntry e = getEntry((short)257);
|
||||||
|
if (e == null)
|
||||||
|
throw new UnsupportedFormatException("missing width tag");
|
||||||
|
return ((NumericEntry)e).getInteger();
|
||||||
|
}
|
||||||
|
|
||||||
|
short getPageNumber()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)297);
|
||||||
|
if (e == null)
|
||||||
|
return -1;
|
||||||
|
return e.getShort();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PHOTOMETRIC getPhotometric()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)262);
|
||||||
|
if (e == null)
|
||||||
|
return PHOTOMETRIC.WHITE;
|
||||||
|
return PHOTOMETRIC.fromCode(e.getShort());
|
||||||
|
}
|
||||||
|
|
||||||
|
short[] getBitsPerSample()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)258);
|
||||||
|
if (e == null)
|
||||||
|
return new short[]{1};
|
||||||
|
return e.getShortArray();
|
||||||
|
}
|
||||||
|
Integer getRowsPerStrip()
|
||||||
|
{
|
||||||
|
NumericEntry e = (NumericEntry)entries.get((short)278);
|
||||||
|
if (e == null)
|
||||||
|
return 0;
|
||||||
|
return e.getInteger();
|
||||||
|
}
|
||||||
|
short getSamplesPerPixel()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)277);
|
||||||
|
if (e == null)
|
||||||
|
return 1;
|
||||||
|
return e.getShort();
|
||||||
|
}
|
||||||
|
int[] getStripOffsets()
|
||||||
|
{
|
||||||
|
NumericEntry e = (NumericEntry)entries.get((short)273);
|
||||||
|
if (e == null)
|
||||||
|
return new int[0];
|
||||||
|
return e.getIntArray();
|
||||||
|
}
|
||||||
|
int[] getStripByteCounts()
|
||||||
|
{
|
||||||
|
NumericEntry e = (NumericEntry)entries.get((short)279);
|
||||||
|
if (e == null)
|
||||||
|
return new int[0];
|
||||||
|
return e.getIntArray();
|
||||||
|
}
|
||||||
|
short getCompression()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)259);
|
||||||
|
if (e == null)
|
||||||
|
return 1;
|
||||||
|
return e.getShort();
|
||||||
|
}
|
||||||
|
short getPredictor()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)317);
|
||||||
|
if (e == null)
|
||||||
|
return 1;
|
||||||
|
return e.getShort();
|
||||||
|
}
|
||||||
|
short getFillOrder()
|
||||||
|
{
|
||||||
|
ShortEntry e = (ShortEntry)entries.get((short)266);
|
||||||
|
if (e == null)
|
||||||
|
return 1;
|
||||||
|
return e.getShort();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
|
import org.hwo.image.UnsupportedFormatException;
|
||||||
|
|
||||||
|
abstract class IFDEntry
|
||||||
|
{
|
||||||
|
public static IFDEntry read(IFD ifd)
|
||||||
|
{
|
||||||
|
int save = ifd.tiffFile.sourceBuffer.position();
|
||||||
|
|
||||||
|
ifd.tiffFile.sourceBuffer.getShort();
|
||||||
|
TAGTYPE tagType = TAGTYPE.fromCode(ifd.tiffFile.sourceBuffer.getShort());
|
||||||
|
|
||||||
|
ifd.tiffFile.sourceBuffer.position(save);
|
||||||
|
|
||||||
|
switch (tagType)
|
||||||
|
{
|
||||||
|
case ASCII:
|
||||||
|
return new StringEntry(ifd);
|
||||||
|
case LONG:
|
||||||
|
return new IntegerEntry(ifd);
|
||||||
|
case SHORT:
|
||||||
|
return new ShortEntry(ifd);
|
||||||
|
default: // Skip Entry
|
||||||
|
ifd.tiffFile.sourceBuffer.getInt();
|
||||||
|
ifd.tiffFile.sourceBuffer.getInt();
|
||||||
|
ifd.tiffFile.sourceBuffer.getInt();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final IFD ifd;
|
||||||
|
|
||||||
|
Short tag;
|
||||||
|
TAGTYPE tagType;
|
||||||
|
|
||||||
|
ByteBuffer buffer;
|
||||||
|
|
||||||
|
|
||||||
|
public IFDEntry(IFD ifd)
|
||||||
|
{
|
||||||
|
this.ifd = ifd;
|
||||||
|
System.err.println(String.format("readIFD(): reading entry at 0x%08x",this.ifd.tiffFile.sourceBuffer.position()));
|
||||||
|
|
||||||
|
Integer count,
|
||||||
|
offset;
|
||||||
|
|
||||||
|
tag = this.ifd.tiffFile.sourceBuffer.getShort();
|
||||||
|
tagType = TAGTYPE.fromCode(this.ifd.tiffFile.sourceBuffer.getShort());
|
||||||
|
|
||||||
|
System.err.println(String.format("TAG: %d TYPE: %s", tag, tagType));
|
||||||
|
|
||||||
|
count = this.ifd.tiffFile.sourceBuffer.getInt();
|
||||||
|
offset = this.ifd.tiffFile.sourceBuffer.getInt();
|
||||||
|
|
||||||
|
int fieldSize = tagType.getSize() * count;
|
||||||
|
|
||||||
|
byte[] ba = new byte[ fieldSize ];
|
||||||
|
|
||||||
|
if (fieldSize <= 4)
|
||||||
|
offset = this.ifd.tiffFile.sourceBuffer.position() - 4;
|
||||||
|
|
||||||
|
int save = this.ifd.tiffFile.sourceBuffer.position();
|
||||||
|
|
||||||
|
this.ifd.tiffFile.sourceBuffer.position(offset);
|
||||||
|
this.ifd.tiffFile.sourceBuffer.get(ba);
|
||||||
|
|
||||||
|
this.ifd.tiffFile.sourceBuffer.position(save);
|
||||||
|
|
||||||
|
buffer = ByteBuffer.wrap(ba);
|
||||||
|
buffer.order( this.ifd.tiffFile.bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN );
|
||||||
|
|
||||||
|
decodeBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IFDEntry(IFD ifd,Short tag,TAGTYPE tagType)
|
||||||
|
{
|
||||||
|
this.ifd = ifd;
|
||||||
|
this.tag = tag;
|
||||||
|
this.tagType = tagType;
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteBuffer getBuffer()
|
||||||
|
{
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void releaseBuffer()
|
||||||
|
{
|
||||||
|
buffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void decodeBuffer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void encodeBuffer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public Object getValues();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("TAG: %d TYPE: %s",tag,tagType.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
public class IntegerEntry extends NumericEntry{
|
||||||
|
|
||||||
|
int[] values;
|
||||||
|
|
||||||
|
public IntegerEntry(IFD ifd)
|
||||||
|
{
|
||||||
|
super(ifd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntegerEntry(IFD ifd,Short tag)
|
||||||
|
{
|
||||||
|
super(ifd,tag,TAGTYPE.LONG);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void decodeBuffer()
|
||||||
|
{
|
||||||
|
values = new int[ getBuffer().asIntBuffer().capacity() ];
|
||||||
|
getBuffer().asIntBuffer().get(values);
|
||||||
|
releaseBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encodeBuffer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] getIntArray()
|
||||||
|
{
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short[] getShortArray()
|
||||||
|
{
|
||||||
|
short[] svalues = new short[values.length];
|
||||||
|
for (int i=0;i<values.length;i++)
|
||||||
|
svalues[i] = (short)values[i];
|
||||||
|
return svalues;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInteger()
|
||||||
|
{
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShort()
|
||||||
|
{
|
||||||
|
return (short)values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
public abstract class NumericEntry extends IFDEntry {
|
||||||
|
|
||||||
|
protected NumericEntry(IFD ifd)
|
||||||
|
{
|
||||||
|
super(ifd);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected NumericEntry(IFD ifd,short tag,TAGTYPE tagType)
|
||||||
|
{
|
||||||
|
super(ifd,tag,tagType);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
abstract int getInteger();
|
||||||
|
abstract short getShort();
|
||||||
|
|
||||||
|
abstract int[] getIntArray();
|
||||||
|
abstract short[] getShortArray();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
public enum PHOTOMETRIC { UNKNOWN, WHITE, BLACK, RGB, PALETTE, TRANSPARENCY;
|
||||||
|
|
||||||
|
public static PHOTOMETRIC fromCode(Short photometric)
|
||||||
|
{
|
||||||
|
switch (photometric)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
return WHITE;
|
||||||
|
case 1:
|
||||||
|
return BLACK;
|
||||||
|
case 2:
|
||||||
|
return RGB;
|
||||||
|
case 3:
|
||||||
|
return PALETTE;
|
||||||
|
case 4:
|
||||||
|
return TRANSPARENCY;
|
||||||
|
default:
|
||||||
|
return UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
public class ShortEntry extends NumericEntry {
|
||||||
|
|
||||||
|
short[] values;
|
||||||
|
|
||||||
|
public ShortEntry(IFD ifd)
|
||||||
|
{
|
||||||
|
super(ifd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShortEntry(IFD ifd,Short tag)
|
||||||
|
{
|
||||||
|
super(ifd,tag,TAGTYPE.LONG);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void decodeBuffer()
|
||||||
|
{
|
||||||
|
values = new short[ getBuffer().asShortBuffer().capacity() ];
|
||||||
|
getBuffer().asShortBuffer().get(values);
|
||||||
|
releaseBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encodeBuffer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short[] getShortArray()
|
||||||
|
{
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] getIntArray()
|
||||||
|
{
|
||||||
|
int[] svalues = new int[values.length];
|
||||||
|
for (int i=0;i<values.length;i++)
|
||||||
|
svalues[i] = values[i];
|
||||||
|
return svalues;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShort()
|
||||||
|
{
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInteger()
|
||||||
|
{
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
public class StringEntry extends IFDEntry {
|
||||||
|
|
||||||
|
String value;
|
||||||
|
|
||||||
|
public StringEntry(IFD ifd) {
|
||||||
|
super(ifd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void decodeBuffer() {
|
||||||
|
value = new String(buffer.array(),Charset.forName("ASCII"));
|
||||||
|
releaseBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encodeBuffer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValues() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import org.hwo.compressor.LZW;
|
||||||
|
import org.hwo.image.UnsupportedFormatException;
|
||||||
|
|
||||||
|
class Strip
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private final IFD ifd;
|
||||||
|
int offset,
|
||||||
|
length,
|
||||||
|
rowcount;
|
||||||
|
|
||||||
|
public Strip(IFD ifd, int offset,int length,int rowcount)
|
||||||
|
{
|
||||||
|
this.ifd = ifd;
|
||||||
|
this.offset = offset;
|
||||||
|
this.length = length;
|
||||||
|
this.rowcount = rowcount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOffset()
|
||||||
|
{
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
public int getLength()
|
||||||
|
{
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
public int getRowCount()
|
||||||
|
{
|
||||||
|
return rowcount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ByteBuffer getBuffer()
|
||||||
|
{
|
||||||
|
ByteBuffer result;
|
||||||
|
|
||||||
|
switch (this.ifd.getCompression())
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
this.ifd.tiffFile.sourceBuffer.position(offset);
|
||||||
|
result = this.ifd.tiffFile.sourceBuffer.slice();
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
byte[] source = new byte[ length ];
|
||||||
|
this.ifd.tiffFile.sourceBuffer.position(offset);
|
||||||
|
this.ifd.tiffFile.sourceBuffer.get(source);
|
||||||
|
|
||||||
|
LZW lzw = new LZW();
|
||||||
|
lzw.setCompressedData(source);
|
||||||
|
|
||||||
|
if (this.ifd.getFillOrder()==2)
|
||||||
|
lzw.setByteOrder(true);
|
||||||
|
|
||||||
|
lzw.decompress();
|
||||||
|
|
||||||
|
result = ByteBuffer.wrap(lzw.getUncompressedData());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new UnsupportedFormatException("Unsupported compression scheme");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
public enum TAGTYPE { UNKNOWN(0), BYTE(1), ASCII(1), SHORT(2), LONG(4), RATIONAL(8), SBYTE(1), UNDEFINED(1), SSHORT(2), SLONG(4), SRATIONAL(8), FLOAT(4), DOUBLE(4);
|
||||||
|
|
||||||
|
static public TAGTYPE fromCode(Short code)
|
||||||
|
{
|
||||||
|
switch (code)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
return BYTE;
|
||||||
|
case 2:
|
||||||
|
return ASCII;
|
||||||
|
case 3:
|
||||||
|
return SHORT;
|
||||||
|
case 4:
|
||||||
|
return LONG;
|
||||||
|
case 5:
|
||||||
|
return RATIONAL;
|
||||||
|
case 6:
|
||||||
|
return SBYTE;
|
||||||
|
case 7:
|
||||||
|
return UNDEFINED;
|
||||||
|
case 8:
|
||||||
|
return SSHORT;
|
||||||
|
case 9:
|
||||||
|
return SLONG;
|
||||||
|
case 10:
|
||||||
|
return SRATIONAL;
|
||||||
|
case 11:
|
||||||
|
return FLOAT;
|
||||||
|
case 12:
|
||||||
|
return DOUBLE;
|
||||||
|
default:
|
||||||
|
return UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int size;
|
||||||
|
|
||||||
|
private TAGTYPE(int size)
|
||||||
|
{
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize()
|
||||||
|
{
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,152 @@
|
||||||
|
package org.hwo.image.tiff;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.nio.ShortBuffer;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hwo.image.ImageFile;
|
||||||
|
import org.hwo.image.UnsupportedFormatException;
|
||||||
|
|
||||||
|
public class TiffFile implements ImageFile {
|
||||||
|
|
||||||
|
ByteBuffer sourceBuffer;
|
||||||
|
|
||||||
|
boolean bigEndian;
|
||||||
|
LinkedList<IFD> ifds;
|
||||||
|
List<BufferedImage> pages;
|
||||||
|
|
||||||
|
|
||||||
|
public TiffFile()
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ImageFile newInstance() {
|
||||||
|
return new TiffFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
bigEndian = false;
|
||||||
|
ifds = new LinkedList<IFD>();
|
||||||
|
|
||||||
|
pages = new LinkedList<BufferedImage>();
|
||||||
|
sourceBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(File imageFile) {
|
||||||
|
|
||||||
|
if (!identify(imageFile))
|
||||||
|
throw new UnsupportedFormatException();
|
||||||
|
|
||||||
|
Integer firstIFD = sourceBuffer.getInt(0x0004);
|
||||||
|
sourceBuffer.position(firstIFD);
|
||||||
|
|
||||||
|
ifds.add(0, new IFD(this));
|
||||||
|
|
||||||
|
reorderPages();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reorderPages()
|
||||||
|
{
|
||||||
|
System.err.println("Testing Page Order");
|
||||||
|
|
||||||
|
if (ifds.size() == 0) // Keine Seiten vorhanden
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ifds.get(0).getPageNumber() == -1) // Keine Seitenzahlen angegeben
|
||||||
|
return;
|
||||||
|
|
||||||
|
System.err.println("Reorder...");
|
||||||
|
|
||||||
|
IFD[] ifdsOrdered = new IFD[ ifds.size() ];
|
||||||
|
|
||||||
|
for (int n=0; n<ifdsOrdered.length; n++)
|
||||||
|
ifdsOrdered[ ifds.get(n).getPageNumber() ] = ifds.get(n);
|
||||||
|
|
||||||
|
ifds.clear();
|
||||||
|
ifds.addAll(Arrays.asList(ifdsOrdered));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(File imageFile) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getNumPages() {
|
||||||
|
return ifds.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage getPage(Integer pageNo) {
|
||||||
|
if (pageNo < ifds.size())
|
||||||
|
{
|
||||||
|
return ifds.get(pageNo).getBufferedImage();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addPage(BufferedImage image) {
|
||||||
|
ifds.add(new IFD(this, image));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean identify(File imageFile) {
|
||||||
|
|
||||||
|
reset();
|
||||||
|
|
||||||
|
if (!imageFile.exists())
|
||||||
|
if (imageFile.getName().endsWith(".tiff"))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
byte[] buffer = new byte[ (int)imageFile.length() ];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileInputStream input = new FileInputStream(imageFile);
|
||||||
|
input.read(buffer);
|
||||||
|
input.close();
|
||||||
|
|
||||||
|
sourceBuffer = ByteBuffer.wrap(buffer);
|
||||||
|
|
||||||
|
if (sourceBuffer.getShort(0) == 0x4D4D)
|
||||||
|
bigEndian = true;
|
||||||
|
else if (sourceBuffer.getShort(0) == 0x4949)
|
||||||
|
bigEndian = false;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (bigEndian)
|
||||||
|
sourceBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||||
|
else
|
||||||
|
sourceBuffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
|
||||||
|
if (sourceBuffer.getShort(0x0002) != 42)
|
||||||
|
return false;
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package org.hwo.interactiveobjects;
|
||||||
|
|
||||||
|
public interface IInteractiveObjectEditor {
|
||||||
|
|
||||||
|
public void setInteractiveObject(Object o);
|
||||||
|
public Object getInteractiveObject();
|
||||||
|
|
||||||
|
public void setVisible(boolean visible);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.hwo.interactiveobjects;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface InteractiveObject {
|
||||||
|
|
||||||
|
public boolean keywordSearch() default false;
|
||||||
|
|
||||||
|
public Class<? extends IInteractiveObjectEditor> editor();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package org.hwo.interactiveobjects;
|
||||||
|
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
public class InteractiveObjectHelper {
|
||||||
|
|
||||||
|
|
||||||
|
private static Hashtable<Object, IInteractiveObjectEditor> p_editors = new Hashtable<Object, IInteractiveObjectEditor>();
|
||||||
|
|
||||||
|
private static InteractiveObject getInteractiveObject(Class<?> clazz)
|
||||||
|
{
|
||||||
|
InteractiveObject io = clazz.getAnnotation(InteractiveObject.class);
|
||||||
|
if (io != null)
|
||||||
|
return io;
|
||||||
|
if (clazz.getSuperclass() != null)
|
||||||
|
return getInteractiveObject(clazz.getSuperclass());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static InteractiveObject getInteractiveObject(Object item)
|
||||||
|
{
|
||||||
|
Class<?> clazz = item.getClass();
|
||||||
|
return getInteractiveObject(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInteractiveObject(Class<?> clazz)
|
||||||
|
{
|
||||||
|
return (getInteractiveObject(clazz)!=null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IInteractiveObjectEditor getEditor(Object item)
|
||||||
|
{
|
||||||
|
InteractiveObject io = getInteractiveObject(item);
|
||||||
|
|
||||||
|
if (io != null)
|
||||||
|
{
|
||||||
|
if (!p_editors.containsKey(item))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IInteractiveObjectEditor editor = io.editor().newInstance();
|
||||||
|
p_editors.put(item, editor);
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.err.println(ex.toString());
|
||||||
|
ex.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return p_editors.get(item);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showEditor(Object o)
|
||||||
|
{
|
||||||
|
IInteractiveObjectEditor editor = getEditor(o);
|
||||||
|
editor.setInteractiveObject(o);
|
||||||
|
editor.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
import org.hwo.fifo.FiFo;
|
||||||
|
import org.hwo.nativeloader.NativeLoader;
|
||||||
|
|
||||||
|
public class NativeSerialPort extends SerialPort {
|
||||||
|
|
||||||
|
class SerialPortInputStream extends InputStream
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
if (!isOpen())
|
||||||
|
throw new IOException("Port nicht gešffnet!");
|
||||||
|
|
||||||
|
int ch = native_getch(nativeHandle,getTimeout());
|
||||||
|
//System.err.println(String.format("RX: 0x%08x", ch));
|
||||||
|
if (ch < 0)
|
||||||
|
throw new IOException(String.format("native_getch: returned: %d",ch));
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class SerialPortOutputStream extends OutputStream
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void write(int arg0) throws IOException {
|
||||||
|
if (!isOpen())
|
||||||
|
throw new IOException("Port nicht gešffnet!");
|
||||||
|
//System.err.println(String.format("TX: 0x%08x", arg0));
|
||||||
|
native_putch(nativeHandle, getTimeout(), arg0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static native int native_open(String portName);
|
||||||
|
private static native int native_close(int handle);
|
||||||
|
private static native int native_send(int handle,byte[] data);
|
||||||
|
private static native int native_recv(int handle,byte[] data);
|
||||||
|
|
||||||
|
private static native int native_getch(int handle,int timeout);
|
||||||
|
private static native int native_putch(int handle,int timeout,int ch);
|
||||||
|
|
||||||
|
private int nativeHandle;
|
||||||
|
|
||||||
|
private SerialPortInputStream inputStream;
|
||||||
|
private SerialPortOutputStream outputStream;
|
||||||
|
|
||||||
|
public NativeSerialPort()
|
||||||
|
{
|
||||||
|
nativeHandle = -1;
|
||||||
|
inputStream = new SerialPortInputStream();
|
||||||
|
outputStream = new SerialPortOutputStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isOpen() {
|
||||||
|
return (nativeHandle >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean open() {
|
||||||
|
if (isOpen())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
nativeHandle = native_open(getPortName());
|
||||||
|
System.err.println(String.format("NativeSerialPort: nativeHandle: %d",nativeHandle));
|
||||||
|
if (nativeHandle < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
if (nativeHandle >= 0)
|
||||||
|
{
|
||||||
|
System.err.println(String.format("NativeSerialPort: Closing nativeHandle %d",nativeHandle));
|
||||||
|
native_close(nativeHandle);
|
||||||
|
}
|
||||||
|
nativeHandle = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getInputStream() {
|
||||||
|
return inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OutputStream getOutputStream() {
|
||||||
|
return outputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
NativeLoader.loadLibrary("hwoio");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import org.hwo.os.OsDetect;
|
||||||
|
|
||||||
|
public abstract class SerialPort {
|
||||||
|
|
||||||
|
static public String[] getPortNames()
|
||||||
|
{
|
||||||
|
switch (OsDetect.getOperatingSystem())
|
||||||
|
{
|
||||||
|
case OSX:
|
||||||
|
return SerialPortOSX.getPortNames();
|
||||||
|
case WINDOWS:
|
||||||
|
return SerialPortWINDOWS.getPortNames();
|
||||||
|
case LINUX:
|
||||||
|
return SerialPortLINUX.getPortNames();
|
||||||
|
default:
|
||||||
|
return new String[]{};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public SerialPort newInstance()
|
||||||
|
{
|
||||||
|
switch (OsDetect.getOperatingSystem())
|
||||||
|
{
|
||||||
|
case OSX:
|
||||||
|
return new SerialPortOSX();
|
||||||
|
case WINDOWS:
|
||||||
|
return new SerialPortWINDOWS();
|
||||||
|
case LINUX:
|
||||||
|
return new SerialPortLINUX();
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String portName;
|
||||||
|
private int timeout; // 0 = Non-Blocking, -1 = Block forever
|
||||||
|
|
||||||
|
protected SerialPort()
|
||||||
|
{
|
||||||
|
this.portName = "";
|
||||||
|
this.timeout = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public boolean isOpen();
|
||||||
|
abstract public boolean open();
|
||||||
|
abstract public void close();
|
||||||
|
|
||||||
|
abstract public InputStream getInputStream();
|
||||||
|
abstract public OutputStream getOutputStream();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getPortName() {
|
||||||
|
return portName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPortName(String portName) {
|
||||||
|
this.portName = portName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,153 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JDialog;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import java.awt.Dialog.ModalExclusionType;
|
||||||
|
import java.awt.Dialog.ModalityType;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
|
||||||
|
import org.hwo.i18n.Messages;
|
||||||
|
import org.hwo.ui.JComboBoxEx;
|
||||||
|
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
|
public class SerialPortChooser extends JDialog {
|
||||||
|
|
||||||
|
|
||||||
|
public static SerialPort execute()
|
||||||
|
{
|
||||||
|
return execute(null);
|
||||||
|
}
|
||||||
|
public static SerialPort execute(String selectedPortName)
|
||||||
|
{
|
||||||
|
SerialPortChooser spc = new SerialPortChooser();
|
||||||
|
|
||||||
|
if (selectedPortName != null)
|
||||||
|
spc.setSelectedSerialPort(selectedPortName);
|
||||||
|
|
||||||
|
spc.setVisible(true);
|
||||||
|
|
||||||
|
return spc.getSelectedSerialPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private final JPanel contentPanel = new JPanel();
|
||||||
|
|
||||||
|
private SerialPort selectedSerialPort;
|
||||||
|
private JComboBoxEx cbPortList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the dialog.
|
||||||
|
*/
|
||||||
|
public SerialPortChooser() {
|
||||||
|
setTitle(Messages.getString("SerialPortChooser.0"));
|
||||||
|
setModalityType(ModalityType.APPLICATION_MODAL);
|
||||||
|
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
|
||||||
|
setModal(true);
|
||||||
|
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||||
|
setBounds(100, 100, 491, 169);
|
||||||
|
getContentPane().setLayout(new BorderLayout());
|
||||||
|
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
||||||
|
GridBagLayout gbl_contentPanel = new GridBagLayout();
|
||||||
|
gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
|
||||||
|
gbl_contentPanel.rowHeights = new int[]{0, 0};
|
||||||
|
gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
||||||
|
gbl_contentPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||||
|
contentPanel.setLayout(gbl_contentPanel);
|
||||||
|
{
|
||||||
|
JLabel lblSchnittstelle = new JLabel(Messages.getString("interface"));
|
||||||
|
GridBagConstraints gbc_lblSchnittstelle = new GridBagConstraints();
|
||||||
|
gbc_lblSchnittstelle.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_lblSchnittstelle.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblSchnittstelle.gridx = 0;
|
||||||
|
gbc_lblSchnittstelle.gridy = 0;
|
||||||
|
contentPanel.add(lblSchnittstelle, gbc_lblSchnittstelle);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
cbPortList = new JComboBoxEx();
|
||||||
|
GridBagConstraints gbc_cbPortList = new GridBagConstraints();
|
||||||
|
gbc_cbPortList.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_cbPortList.gridx = 1;
|
||||||
|
gbc_cbPortList.gridy = 0;
|
||||||
|
contentPanel.add(cbPortList, gbc_cbPortList);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
JPanel buttonPane = new JPanel();
|
||||||
|
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
||||||
|
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
||||||
|
{
|
||||||
|
JButton okButton = new JButton(Messages.getString("ok"));
|
||||||
|
okButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
accept();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
okButton.setActionCommand("OK");
|
||||||
|
buttonPane.add(okButton);
|
||||||
|
getRootPane().setDefaultButton(okButton);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
JButton cancelButton = new JButton(Messages.getString("cancel"));
|
||||||
|
cancelButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cancelButton.setActionCommand("Cancel");
|
||||||
|
buttonPane.add(cancelButton);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
selectedSerialPort = null;
|
||||||
|
|
||||||
|
cbPortList.removeAllItems();
|
||||||
|
|
||||||
|
for (String portName: SerialPort.getPortNames())
|
||||||
|
cbPortList.addItem(portName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void accept()
|
||||||
|
{
|
||||||
|
selectedSerialPort = SerialPort.newInstance();
|
||||||
|
selectedSerialPort.setPortName(cbPortList.getSelectedItem().toString());
|
||||||
|
|
||||||
|
setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancel()
|
||||||
|
{
|
||||||
|
selectedSerialPort = null;
|
||||||
|
setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SerialPort getSelectedSerialPort() {
|
||||||
|
return selectedSerialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectedSerialPort(SerialPort selectedSerialPort) {
|
||||||
|
cbPortList.selectEqualItem(selectedSerialPort.getPortName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectedSerialPort(String portName) {
|
||||||
|
cbPortList.selectEqualItem(portName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SerialPortLINUX extends NativeSerialPort{
|
||||||
|
|
||||||
|
static public String[] getPortNames()
|
||||||
|
{
|
||||||
|
ArrayList<String> portNames = new ArrayList<String>();
|
||||||
|
|
||||||
|
File devDir = new File("/dev");
|
||||||
|
File[] list = devDir.listFiles(new FilenameFilter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(File arg0, String arg1) {
|
||||||
|
if (arg1.startsWith("ttyS") || arg1.startsWith("ttyACM"))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (File file:list)
|
||||||
|
portNames.add("/dev/" + file.getName());
|
||||||
|
|
||||||
|
return portNames.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SerialPortOSX extends NativeSerialPort {
|
||||||
|
|
||||||
|
static public String[] getPortNames()
|
||||||
|
{
|
||||||
|
ArrayList<String> portNames = new ArrayList<String>();
|
||||||
|
|
||||||
|
File devDir = new File("/dev");
|
||||||
|
File[] list = devDir.listFiles(new FilenameFilter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(File arg0, String arg1) {
|
||||||
|
if (arg1.startsWith("tty."))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (File file:list)
|
||||||
|
portNames.add("/dev/" + file.getName());
|
||||||
|
|
||||||
|
return portNames.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.hwo.io;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SerialPortWINDOWS extends NativeSerialPort {
|
||||||
|
|
||||||
|
|
||||||
|
static public String[] getPortNames()
|
||||||
|
{
|
||||||
|
ArrayList<String> portNames = new ArrayList<String>();
|
||||||
|
|
||||||
|
SerialPortWINDOWS sp = new SerialPortWINDOWS();
|
||||||
|
|
||||||
|
for (int i = 1; i < 32; i++)
|
||||||
|
{
|
||||||
|
sp.setPortName(String.format("COM%d:",i));
|
||||||
|
if (sp.open())
|
||||||
|
{
|
||||||
|
portNames.add(String.format("COM%d:",i));
|
||||||
|
sp.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return portNames.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import javax.swing.JCheckBox;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode;
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import javax.swing.BoxLayout;
|
||||||
|
|
||||||
|
public class BitFieldEditor extends JPanel implements RegisterEditorControl {
|
||||||
|
|
||||||
|
private ServiceNodeRegister serviceNodeRegister;
|
||||||
|
|
||||||
|
private JCheckBox[] checkboxes;
|
||||||
|
private JScrollPane scrollPane;
|
||||||
|
private JPanel pBits;
|
||||||
|
|
||||||
|
public BitFieldEditor() {
|
||||||
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||||
|
gridBagLayout.columnWidths = new int[]{0, 0};
|
||||||
|
gridBagLayout.rowHeights = new int[]{0, 0};
|
||||||
|
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||||
|
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||||
|
setLayout(gridBagLayout);
|
||||||
|
|
||||||
|
scrollPane = new JScrollPane();
|
||||||
|
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
||||||
|
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_scrollPane.gridx = 0;
|
||||||
|
gbc_scrollPane.gridy = 0;
|
||||||
|
add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
|
pBits = new JPanel();
|
||||||
|
scrollPane.setViewportView(pBits);
|
||||||
|
pBits.setLayout(new BoxLayout(pBits, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
checkboxes = new JCheckBox[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
for (JCheckBox cb:checkboxes)
|
||||||
|
{
|
||||||
|
remove(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serviceNodeRegister != null)
|
||||||
|
{
|
||||||
|
checkboxes = new JCheckBox[32];
|
||||||
|
for (int i=0;i<32;i++)
|
||||||
|
{
|
||||||
|
if (serviceNodeRegister.getBitField().getLabels()[i]!=null)
|
||||||
|
{
|
||||||
|
checkboxes[i] = new JCheckBox();
|
||||||
|
checkboxes[i].setText(serviceNodeRegister.getBitField().getLabels()[i]);
|
||||||
|
|
||||||
|
pBits.add(checkboxes[i]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
synchronize();
|
||||||
|
}
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void synchronize()
|
||||||
|
{
|
||||||
|
for (int i=0;i<32;i++)
|
||||||
|
{
|
||||||
|
if (checkboxes[i]!=null)
|
||||||
|
checkboxes[i].setSelected(
|
||||||
|
(serviceNodeRegister.getIntValue() & (1<<i))!=0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
setServiceNodeRegister(serviceNodeRegister);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRegister() {
|
||||||
|
Integer v = serviceNodeRegister.getIntValue();
|
||||||
|
|
||||||
|
for (int i=0;i<32;i++)
|
||||||
|
{
|
||||||
|
if (checkboxes[i]!=null)
|
||||||
|
{
|
||||||
|
if (checkboxes[i].isSelected())
|
||||||
|
v |= 1<<i;
|
||||||
|
else
|
||||||
|
v &= ~(1<<i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
serviceNodeRegister.setIntValue(v);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getComponent() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceNodeRegister getServiceNodeRegister() {
|
||||||
|
return serviceNodeRegister;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServiceNodeRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
this.serviceNodeRegister = serviceNodeRegister;
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.event.FocusEvent;
|
||||||
|
import java.awt.event.FocusListener;
|
||||||
|
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
|
||||||
|
|
||||||
|
public class FloatEditor extends JTextField implements RegisterEditorControl {
|
||||||
|
|
||||||
|
private ServiceNodeRegister serviceNodeRegister;
|
||||||
|
|
||||||
|
public FloatEditor()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
|
||||||
|
addFocusListener(new FocusListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void focusLost(FocusEvent arg0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void focusGained(FocusEvent arg0) {
|
||||||
|
selectAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
setServiceNodeRegister(serviceNodeRegister);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceNodeRegister getServiceNodeRegister() {
|
||||||
|
return serviceNodeRegister;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServiceNodeRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
this.serviceNodeRegister = serviceNodeRegister;
|
||||||
|
setText(serviceNodeRegister.getFloatValue().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRegister() {
|
||||||
|
Float f = Float.parseFloat(getText());
|
||||||
|
if (f!=null)
|
||||||
|
serviceNodeRegister.setFloatValue(f);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getComponent() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.event.FocusEvent;
|
||||||
|
import java.awt.event.FocusListener;
|
||||||
|
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
|
||||||
|
|
||||||
|
public class IntegerEditor extends JTextField implements RegisterEditorControl {
|
||||||
|
|
||||||
|
private ServiceNodeRegister serviceNodeRegister;
|
||||||
|
|
||||||
|
public IntegerEditor()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
|
||||||
|
addFocusListener(new FocusListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void focusLost(FocusEvent arg0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void focusGained(FocusEvent arg0) {
|
||||||
|
selectAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
setServiceNodeRegister(serviceNodeRegister);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceNodeRegister getServiceNodeRegister() {
|
||||||
|
return serviceNodeRegister;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServiceNodeRegister(ServiceNodeRegister serviceNodeRegister) {
|
||||||
|
this.serviceNodeRegister = serviceNodeRegister;
|
||||||
|
setText(serviceNodeRegister.getIntValue().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRegister() {
|
||||||
|
Integer i = Integer.decode(getText());
|
||||||
|
if (i!=null)
|
||||||
|
serviceNodeRegister.setIntValue(i);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getComponent() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,209 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.EventQueue;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import org.hwo.interactiveobjects.IInteractiveObjectEditor;
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
|
||||||
|
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.Insets;
|
||||||
|
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.xml.ws.ServiceMode;
|
||||||
|
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.WindowFocusListener;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
|
public class RegisterEditor extends JFrame implements IInteractiveObjectEditor{
|
||||||
|
|
||||||
|
private JPanel contentPane;
|
||||||
|
private JTextField tfRegister;
|
||||||
|
private JLabel lblLabel;
|
||||||
|
private JTextField tfValueAct;
|
||||||
|
|
||||||
|
ServiceNodeRegister serviceNodeRegister;
|
||||||
|
private JPanel pEditor;
|
||||||
|
|
||||||
|
private RegisterEditorControl editorControl;
|
||||||
|
|
||||||
|
public RegisterEditor() {
|
||||||
|
addWindowFocusListener(new WindowFocusListener() {
|
||||||
|
public void windowGainedFocus(WindowEvent arg0) {
|
||||||
|
if (editorControl != null)
|
||||||
|
editorControl.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
public void windowLostFocus(WindowEvent arg0) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
||||||
|
setBounds(100, 100, 628, 304);
|
||||||
|
contentPane = new JPanel();
|
||||||
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
setContentPane(contentPane);
|
||||||
|
GridBagLayout gbl_contentPane = new GridBagLayout();
|
||||||
|
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
|
||||||
|
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
|
||||||
|
gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
|
||||||
|
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
|
||||||
|
contentPane.setLayout(gbl_contentPane);
|
||||||
|
|
||||||
|
JLabel lblRegister = new JLabel("Register:");
|
||||||
|
GridBagConstraints gbc_lblRegister = new GridBagConstraints();
|
||||||
|
gbc_lblRegister.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblRegister.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblRegister.gridx = 0;
|
||||||
|
gbc_lblRegister.gridy = 0;
|
||||||
|
contentPane.add(lblRegister, gbc_lblRegister);
|
||||||
|
|
||||||
|
tfRegister = new JTextField();
|
||||||
|
tfRegister.setEditable(false);
|
||||||
|
GridBagConstraints gbc_tfRegister = new GridBagConstraints();
|
||||||
|
gbc_tfRegister.insets = new Insets(0, 0, 5, 0);
|
||||||
|
gbc_tfRegister.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_tfRegister.gridx = 1;
|
||||||
|
gbc_tfRegister.gridy = 0;
|
||||||
|
contentPane.add(tfRegister, gbc_tfRegister);
|
||||||
|
tfRegister.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblBezeichnung = new JLabel("Bezeichnung:");
|
||||||
|
GridBagConstraints gbc_lblBezeichnung = new GridBagConstraints();
|
||||||
|
gbc_lblBezeichnung.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblBezeichnung.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblBezeichnung.gridx = 0;
|
||||||
|
gbc_lblBezeichnung.gridy = 1;
|
||||||
|
contentPane.add(lblBezeichnung, gbc_lblBezeichnung);
|
||||||
|
|
||||||
|
lblLabel = new JLabel("N/A");
|
||||||
|
GridBagConstraints gbc_lblLabel = new GridBagConstraints();
|
||||||
|
gbc_lblLabel.insets = new Insets(0, 0, 5, 0);
|
||||||
|
gbc_lblLabel.gridx = 1;
|
||||||
|
gbc_lblLabel.gridy = 1;
|
||||||
|
contentPane.add(lblLabel, gbc_lblLabel);
|
||||||
|
|
||||||
|
JLabel lblAlterWert = new JLabel("Alter Wert:");
|
||||||
|
GridBagConstraints gbc_lblAlterWert = new GridBagConstraints();
|
||||||
|
gbc_lblAlterWert.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblAlterWert.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblAlterWert.gridx = 0;
|
||||||
|
gbc_lblAlterWert.gridy = 2;
|
||||||
|
contentPane.add(lblAlterWert, gbc_lblAlterWert);
|
||||||
|
|
||||||
|
tfValueAct = new JTextField();
|
||||||
|
tfValueAct.setEditable(false);
|
||||||
|
GridBagConstraints gbc_tfValueAct = new GridBagConstraints();
|
||||||
|
gbc_tfValueAct.anchor = GridBagConstraints.NORTH;
|
||||||
|
gbc_tfValueAct.insets = new Insets(0, 0, 5, 0);
|
||||||
|
gbc_tfValueAct.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_tfValueAct.gridx = 1;
|
||||||
|
gbc_tfValueAct.gridy = 2;
|
||||||
|
contentPane.add(tfValueAct, gbc_tfValueAct);
|
||||||
|
tfValueAct.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNeuerWert = new JLabel("Neuer Wert:");
|
||||||
|
GridBagConstraints gbc_lblNeuerWert = new GridBagConstraints();
|
||||||
|
gbc_lblNeuerWert.anchor = GridBagConstraints.NORTHEAST;
|
||||||
|
gbc_lblNeuerWert.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNeuerWert.gridx = 0;
|
||||||
|
gbc_lblNeuerWert.gridy = 3;
|
||||||
|
contentPane.add(lblNeuerWert, gbc_lblNeuerWert);
|
||||||
|
|
||||||
|
pEditor = new JPanel();
|
||||||
|
GridBagConstraints gbc_pEditor = new GridBagConstraints();
|
||||||
|
gbc_pEditor.insets = new Insets(0, 0, 5, 0);
|
||||||
|
gbc_pEditor.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_pEditor.gridx = 1;
|
||||||
|
gbc_pEditor.gridy = 3;
|
||||||
|
contentPane.add(pEditor, gbc_pEditor);
|
||||||
|
pEditor.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
GridBagConstraints gbc_panel = new GridBagConstraints();
|
||||||
|
gbc_panel.gridwidth = 2;
|
||||||
|
gbc_panel.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_panel.gridx = 0;
|
||||||
|
gbc_panel.gridy = 4;
|
||||||
|
contentPane.add(panel, gbc_panel);
|
||||||
|
GridBagLayout gbl_panel = new GridBagLayout();
|
||||||
|
gbl_panel.columnWidths = new int[]{0, 0, 0};
|
||||||
|
gbl_panel.rowHeights = new int[]{0, 0};
|
||||||
|
gbl_panel.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
|
||||||
|
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||||
|
panel.setLayout(gbl_panel);
|
||||||
|
|
||||||
|
JButton btnNewButton = new JButton("schreiben...");
|
||||||
|
btnNewButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
editorControl.updateRegister();
|
||||||
|
serviceNodeRegister.write();
|
||||||
|
setVisible(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
|
||||||
|
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_btnNewButton.gridx = 0;
|
||||||
|
gbc_btnNewButton.gridy = 0;
|
||||||
|
panel.add(btnNewButton, gbc_btnNewButton);
|
||||||
|
|
||||||
|
JButton btnNewButton_1 = new JButton("abbrechen");
|
||||||
|
btnNewButton_1.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
setVisible(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
|
||||||
|
gbc_btnNewButton_1.gridx = 1;
|
||||||
|
gbc_btnNewButton_1.gridy = 0;
|
||||||
|
panel.add(btnNewButton_1, gbc_btnNewButton_1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInteractiveObject(Object o) {
|
||||||
|
serviceNodeRegister = (ServiceNodeRegister)o;
|
||||||
|
|
||||||
|
serviceNodeRegister.read();
|
||||||
|
|
||||||
|
tfRegister.setText(serviceNodeRegister.register.toString());
|
||||||
|
lblLabel.setText(serviceNodeRegister.label);
|
||||||
|
tfValueAct.setText(serviceNodeRegister.value());
|
||||||
|
|
||||||
|
if (editorControl != null)
|
||||||
|
pEditor.remove(editorControl.getComponent());
|
||||||
|
|
||||||
|
if (serviceNodeRegister.isInteger())
|
||||||
|
{
|
||||||
|
editorControl = new IntegerEditor();
|
||||||
|
} else if (serviceNodeRegister.isFloat())
|
||||||
|
{
|
||||||
|
editorControl = new FloatEditor();
|
||||||
|
} else if (serviceNodeRegister.isBitField())
|
||||||
|
{
|
||||||
|
editorControl = new BitFieldEditor();
|
||||||
|
}
|
||||||
|
editorControl.setRegister(serviceNodeRegister);
|
||||||
|
pEditor.add(editorControl.getComponent());
|
||||||
|
pEditor.invalidate();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getInteractiveObject() {
|
||||||
|
return serviceNodeRegister;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
|
||||||
|
import javax.tools.JavaCompiler;
|
||||||
|
|
||||||
|
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
|
||||||
|
|
||||||
|
public interface RegisterEditorControl {
|
||||||
|
|
||||||
|
public void setRegister(ServiceNodeRegister serviceNodeRegister);
|
||||||
|
public boolean requestFocusInWindow();
|
||||||
|
public void updateRegister();
|
||||||
|
|
||||||
|
public Component getComponent();
|
||||||
|
}
|
|
@ -0,0 +1,491 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hwo.BitField;
|
||||||
|
import org.hwo.ChkSum;
|
||||||
|
import org.hwo.interactiveobjects.InteractiveObject;
|
||||||
|
import org.hwo.io.SerialPort;
|
||||||
|
import org.hwo.models.TableMapper.TableColumn;
|
||||||
|
|
||||||
|
/* ServiceLink
|
||||||
|
*
|
||||||
|
* Kommunikation via USB mit RegBus fŠhigem System
|
||||||
|
*
|
||||||
|
* †bertragungssicherheit wird durch USB sichergestellt
|
||||||
|
*
|
||||||
|
* †bertragen werden Telegramme mit folgendem Aufbau:
|
||||||
|
*
|
||||||
|
* Offset Type Inhalt
|
||||||
|
* 0 Byte RequestTyp ( 0 = NoOP, 1 = Wert lesen, 2 = Wert schreiben, 3 = Ereignis senden )
|
||||||
|
* 1 Byte Achse
|
||||||
|
* 2 Byte Knoten
|
||||||
|
* 3 Short Register Nummer
|
||||||
|
* 5 Int32/Float Wert (nur bei schreiben oder lesen antwort)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public class ServiceLink {
|
||||||
|
|
||||||
|
static int REQ_READ = 0x01;
|
||||||
|
static int REQ_WRITE = 0x02;
|
||||||
|
static int REQ_EVENT = 0x04;
|
||||||
|
static int REQ_FAIL = 0x08;
|
||||||
|
|
||||||
|
static int REQ_INT = 0x10;
|
||||||
|
static int REQ_FLOAT = 0x20;
|
||||||
|
static int REQ_ACK = 0x40;
|
||||||
|
|
||||||
|
static int SL_MAGIC = 0x66;
|
||||||
|
|
||||||
|
|
||||||
|
class ServiceTelegram
|
||||||
|
{
|
||||||
|
byte request;
|
||||||
|
byte achse;
|
||||||
|
byte knoten;
|
||||||
|
int register;
|
||||||
|
byte[] value;
|
||||||
|
|
||||||
|
public ServiceTelegram()
|
||||||
|
{
|
||||||
|
request = 0;
|
||||||
|
achse = 0;
|
||||||
|
knoten = 0;
|
||||||
|
register = 0;
|
||||||
|
value = new byte[4];
|
||||||
|
ByteBuffer.wrap(value).asIntBuffer().put(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send()
|
||||||
|
{
|
||||||
|
byte[] txbuffer = new byte[12];
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(txbuffer).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
bb.put( (byte)SL_MAGIC );
|
||||||
|
bb.put(request);
|
||||||
|
bb.put(achse);
|
||||||
|
bb.put(knoten);
|
||||||
|
bb.putShort((short)register);
|
||||||
|
if ((request & REQ_WRITE) == REQ_WRITE)
|
||||||
|
bb.put(value);
|
||||||
|
|
||||||
|
short chk = ChkSum.chksum(txbuffer, 1, bb.position()-1);
|
||||||
|
bb.putShort(chk);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
getSerialPort().getOutputStream().write(txbuffer,0,bb.position());
|
||||||
|
} catch (IOException ex)
|
||||||
|
{
|
||||||
|
getSerialPort().close();
|
||||||
|
System.err.println(ex.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recv() throws ServiceLinkException
|
||||||
|
{
|
||||||
|
byte[] rxbuffer = new byte[12];
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(rxbuffer).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
try {
|
||||||
|
int ch = getSerialPort().getInputStream().read();
|
||||||
|
|
||||||
|
while ((ch != SL_MAGIC) && (ch >= 0))
|
||||||
|
ch = getSerialPort().getInputStream().read();
|
||||||
|
|
||||||
|
getSerialPort().getInputStream().read(rxbuffer,0,5);
|
||||||
|
|
||||||
|
request = bb.get();
|
||||||
|
achse = bb.get();
|
||||||
|
knoten = bb.get();
|
||||||
|
register = bb.getShort();
|
||||||
|
|
||||||
|
if ((request & (REQ_READ | REQ_ACK)) == (REQ_READ | REQ_ACK))
|
||||||
|
{
|
||||||
|
getSerialPort().getInputStream().read(rxbuffer,bb.position(),4);
|
||||||
|
bb.get(value);
|
||||||
|
};
|
||||||
|
getSerialPort().getInputStream().read(rxbuffer,bb.position(),2);
|
||||||
|
short chksum = bb.getShort();
|
||||||
|
|
||||||
|
if (chksum != ChkSum.chksum(rxbuffer, 0, bb.position() - 2))
|
||||||
|
throw new ServiceLinkException();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
getSerialPort().close();
|
||||||
|
|
||||||
|
System.err.println(e.toString());
|
||||||
|
throw new ServiceLinkException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ServiceNode
|
||||||
|
{
|
||||||
|
@InteractiveObject(editor=RegisterEditor.class)
|
||||||
|
public class ServiceNodeRegister
|
||||||
|
{
|
||||||
|
@TableColumn(label="Register",readonly=true,width=60)
|
||||||
|
Integer register;
|
||||||
|
@TableColumn(label="Bezeichnung",readonly=true,width=150)
|
||||||
|
String label;
|
||||||
|
|
||||||
|
private Float floatValue;
|
||||||
|
private Integer intValue;
|
||||||
|
|
||||||
|
boolean floatType;
|
||||||
|
|
||||||
|
private BitField bitField;
|
||||||
|
|
||||||
|
public ServiceNodeRegister(int register,boolean floatType)
|
||||||
|
{
|
||||||
|
this.label = String.format("%d", register);
|
||||||
|
this.register = register;
|
||||||
|
this.floatType = floatType;
|
||||||
|
}
|
||||||
|
public ServiceNodeRegister(int register,String label,boolean floatType)
|
||||||
|
{
|
||||||
|
this.label = label;
|
||||||
|
this.register = register;
|
||||||
|
this.floatType = floatType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBitField()
|
||||||
|
{
|
||||||
|
return (bitField != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFloat()
|
||||||
|
{
|
||||||
|
return floatType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInteger()
|
||||||
|
{
|
||||||
|
return (!isBitField() && !isFloat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@TableColumn(label="Wert",width=500)
|
||||||
|
public String value()
|
||||||
|
{
|
||||||
|
if (bitField != null)
|
||||||
|
return bitField.toText(intValue);
|
||||||
|
if (intValue != null)
|
||||||
|
return String.format("%d", intValue);
|
||||||
|
if (floatValue != null)
|
||||||
|
return String.format("%f", floatValue);
|
||||||
|
return "N/A";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value)
|
||||||
|
{
|
||||||
|
if (floatType)
|
||||||
|
floatValue = new Float(value);
|
||||||
|
else
|
||||||
|
intValue = new Integer(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (floatType)
|
||||||
|
floatValue = ServiceNode.this.readFloat(register);
|
||||||
|
else
|
||||||
|
intValue = ServiceNode.this.readInt(register);
|
||||||
|
} catch (Exception e) {
|
||||||
|
floatValue = null;
|
||||||
|
intValue = 0;
|
||||||
|
e.printStackTrace();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (floatType)
|
||||||
|
ServiceNode.this.writeFloat(register, floatValue);
|
||||||
|
else
|
||||||
|
ServiceNode.this.writeInt(register, intValue);
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public BitField getBitField() {
|
||||||
|
return bitField;
|
||||||
|
}
|
||||||
|
public void setBitField(BitField bitField) {
|
||||||
|
this.bitField = bitField;
|
||||||
|
}
|
||||||
|
public Integer getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
public void setIntValue(Integer intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
public Float getFloatValue() {
|
||||||
|
return floatValue;
|
||||||
|
}
|
||||||
|
public void setFloatValue(Float floatValue) {
|
||||||
|
this.floatValue = floatValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte achse;
|
||||||
|
private byte knoten;
|
||||||
|
|
||||||
|
List<ServiceNodeRegister> registers;
|
||||||
|
|
||||||
|
public ServiceNode(byte achse,byte knoten)
|
||||||
|
{
|
||||||
|
this.achse = achse;
|
||||||
|
this.knoten = knoten;
|
||||||
|
this.registers = new ArrayList<ServiceLink.ServiceNode.ServiceNodeRegister>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceNodeRegister createRegister(int register,String label,boolean floatType)
|
||||||
|
{
|
||||||
|
return new ServiceNodeRegister(register, label, floatType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ServiceNodeRegister> getRegisters()
|
||||||
|
{
|
||||||
|
return registers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read()
|
||||||
|
{
|
||||||
|
for (ServiceNodeRegister r:registers)
|
||||||
|
r.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceLink getServiceLink()
|
||||||
|
{
|
||||||
|
return ServiceLink.this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int readInt(int register) throws IOException, ServiceLinkException
|
||||||
|
{
|
||||||
|
for (int n=0;n<retries;n++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ServiceLink.this.readInt(achse, knoten, register);
|
||||||
|
} catch (ServiceLinkException e)
|
||||||
|
{
|
||||||
|
if (n == (retries -1))
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ServiceLinkException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float readFloat(int register) throws IOException, ServiceLinkException
|
||||||
|
{
|
||||||
|
for (int n=0;n<retries;n++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ServiceLink.this.readFloat(achse, knoten, register);
|
||||||
|
} catch (ServiceLinkException e)
|
||||||
|
{
|
||||||
|
if (n == (retries -1))
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ServiceLinkException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeInt(int register,int value) throws IOException, ServiceLinkException
|
||||||
|
{
|
||||||
|
for (int n=0;n<retries;n++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ServiceLink.this.writeInt(achse, knoten, register, value);
|
||||||
|
return;
|
||||||
|
} catch (ServiceLinkException e)
|
||||||
|
{
|
||||||
|
if (n == (retries -1))
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ServiceLinkException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeFloat(int register,float value) throws IOException, ServiceLinkException
|
||||||
|
{
|
||||||
|
for (int n=0;n<retries;n++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ServiceLink.this.writeFloat(achse, knoten, register,value);
|
||||||
|
return;
|
||||||
|
} catch (ServiceLinkException e)
|
||||||
|
{
|
||||||
|
if (n == (retries -1))
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ServiceLinkException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getAchse() {
|
||||||
|
return achse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAchse(byte achse) {
|
||||||
|
this.achse = achse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getKnoten() {
|
||||||
|
return knoten;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKnoten(byte knoten) {
|
||||||
|
this.knoten = knoten;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private SerialPort serialPort;
|
||||||
|
int retries;
|
||||||
|
|
||||||
|
public ServiceLink(SerialPort serialPort)
|
||||||
|
{
|
||||||
|
this.retries = 3;
|
||||||
|
this.serialPort = serialPort;
|
||||||
|
this.serialPort.setTimeout(500);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.err.println(String.format("BRKVAL : 0x%04x",readInt((byte)0,(byte)0,(short)0x200)));
|
||||||
|
System.err.println(String.format("SPLIMIT: 0x%04x",readInt((byte)0,(byte)0,(short)0x201)));
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void open() throws ServiceLinkException
|
||||||
|
{
|
||||||
|
if (serialPort != null)
|
||||||
|
serialPort.open();
|
||||||
|
throwNotOpen();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
if (serialPort != null)
|
||||||
|
serialPort.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
if (serialPort.isOpen())
|
||||||
|
serialPort.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceNode getServiceNode(byte achse,byte knoten)
|
||||||
|
{
|
||||||
|
return new ServiceNode(achse, knoten);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer readInt(byte achse,byte knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
throwNotOpen();
|
||||||
|
return ByteBuffer.wrap( request((byte)(REQ_READ | REQ_INT), achse, knoten, register).value ).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Float readFloat(byte achse,byte knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
throwNotOpen();
|
||||||
|
return ByteBuffer.wrap( request((byte)(REQ_READ | REQ_FLOAT), achse, knoten, register).value ).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeInt(byte achse,byte knoten,int register,int value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
throwNotOpen();
|
||||||
|
|
||||||
|
byte[] v = new byte[4];
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(v).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
bb.putInt(value);
|
||||||
|
|
||||||
|
ServiceTelegram telegram = request((byte)(REQ_WRITE | REQ_INT), achse, knoten, register, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeFloat(byte achse,byte knoten,int register,float value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
throwNotOpen();
|
||||||
|
|
||||||
|
byte[] v = new byte[4];
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(v).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
bb.putFloat(value);
|
||||||
|
|
||||||
|
ServiceTelegram telegram = request((byte)(REQ_WRITE | REQ_FLOAT), achse, knoten, register, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized ServiceTelegram request(byte request,byte achse,byte knoten,int register) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
return request(request,achse,knoten,register,new byte[0]);
|
||||||
|
}
|
||||||
|
private synchronized ServiceTelegram request(byte request,byte achse,byte knoten,int register,byte[] value) throws IOException, ServiceLinkException, ServiceLinkRequestFailedException
|
||||||
|
{
|
||||||
|
ServiceTelegram telegram = new ServiceTelegram();
|
||||||
|
|
||||||
|
telegram.request = request;
|
||||||
|
telegram.achse = achse;
|
||||||
|
telegram.knoten = knoten;
|
||||||
|
telegram.register = register;
|
||||||
|
|
||||||
|
if (value.length == 4)
|
||||||
|
telegram.value = value;
|
||||||
|
|
||||||
|
|
||||||
|
telegram.send();
|
||||||
|
telegram.recv();
|
||||||
|
|
||||||
|
if ((telegram.request & REQ_ACK)==0)
|
||||||
|
throw new ServiceLinkRequestFailedException();
|
||||||
|
|
||||||
|
return telegram;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SerialPort getSerialPort() {
|
||||||
|
return serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSerialPort(SerialPort serialPort) {
|
||||||
|
if (isOpen())
|
||||||
|
this.serialPort.close();
|
||||||
|
|
||||||
|
this.serialPort = serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void throwNotOpen() throws ServiceLinkException
|
||||||
|
{
|
||||||
|
if (!isOpen())
|
||||||
|
throw new ServiceLinkException("Port not Opened!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen()
|
||||||
|
{
|
||||||
|
return this.serialPort.isOpen();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ServiceLinkException extends Exception {
|
||||||
|
|
||||||
|
public ServiceLinkException()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceLinkException(String message)
|
||||||
|
{
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceLinkException(Throwable e) {
|
||||||
|
super(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package org.hwo.io.servicelink;
|
||||||
|
|
||||||
|
public class ServiceLinkRequestFailedException extends ServiceLinkException {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
package org.hwo.layout;
|
||||||
|
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.FontMetrics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.font.GlyphVector;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class TextLayouter {
|
||||||
|
|
||||||
|
private Font font;
|
||||||
|
private FontMetrics metrics;
|
||||||
|
|
||||||
|
private char[] characters;
|
||||||
|
|
||||||
|
private GlyphVector[] glyphs;
|
||||||
|
|
||||||
|
private int position;
|
||||||
|
|
||||||
|
public TextLayouter(String text)
|
||||||
|
{
|
||||||
|
this.setFont(new Font("Arial", 0, 50));
|
||||||
|
this.characters = text.toCharArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextLayouter(String text,Font font)
|
||||||
|
{
|
||||||
|
this.characters = text.toCharArray();
|
||||||
|
this.setFont(font);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(int position) {
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine(float width)
|
||||||
|
{
|
||||||
|
LinkedList<Character> lineCharacters = new LinkedList<Character>();
|
||||||
|
float x = 0;
|
||||||
|
|
||||||
|
while ( position < characters.length )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//return String.valueOf(lineCharacters.toArray(new char[0]));
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Font getFont() {
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFont(Font font) {
|
||||||
|
this.font = font;
|
||||||
|
|
||||||
|
BufferedImage img = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g2d = img.createGraphics();
|
||||||
|
g2d.setFont(font);
|
||||||
|
this.metrics = g2d.getFontMetrics();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,132 @@
|
||||||
|
package org.hwo.models;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class Column {
|
||||||
|
private int MODE_FIELD = 1;
|
||||||
|
private int MODE_GETSET = 2;
|
||||||
|
|
||||||
|
private int mode;
|
||||||
|
private String fieldName;
|
||||||
|
private boolean readonly;
|
||||||
|
private Class p_class;
|
||||||
|
private Field field;
|
||||||
|
private Method getter;
|
||||||
|
private Method setter;
|
||||||
|
|
||||||
|
public Column(Class classInfo,String fieldName,boolean readonly) throws NoSuchFieldException
|
||||||
|
{
|
||||||
|
this.fieldName = fieldName;
|
||||||
|
this.readonly = readonly;
|
||||||
|
this.p_class = classInfo;
|
||||||
|
this.getter = null;
|
||||||
|
this.setter = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
mode = MODE_FIELD;
|
||||||
|
this.field = this.p_class.getDeclaredField(fieldName);
|
||||||
|
this.field.setAccessible(true);
|
||||||
|
} catch (NoSuchFieldException nsfex)
|
||||||
|
{
|
||||||
|
mode = MODE_GETSET;
|
||||||
|
// Kein deklariertes Feld, also suchen wir eine get... und set... Methode...
|
||||||
|
Method[] methods = this.p_class.getDeclaredMethods();
|
||||||
|
for (Method method : methods) {
|
||||||
|
// passender getter?
|
||||||
|
if (("get"+fieldName.toLowerCase()).equals(method.getName().toLowerCase()))
|
||||||
|
getter = method;
|
||||||
|
// passender setter?
|
||||||
|
if (("set"+fieldName.toLowerCase()).equals(method.getName().toLowerCase()))
|
||||||
|
setter = method;
|
||||||
|
}
|
||||||
|
if (getter == null)
|
||||||
|
throw nsfex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getFieldName()
|
||||||
|
{
|
||||||
|
return this.fieldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getReadOnly()
|
||||||
|
{
|
||||||
|
if ((mode == MODE_GETSET) && (setter == null))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return this.readonly;
|
||||||
|
}
|
||||||
|
public void setReadOnly(boolean readOnly)
|
||||||
|
{
|
||||||
|
this.readonly = readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class getColumnClass()
|
||||||
|
{
|
||||||
|
if (mode == MODE_GETSET)
|
||||||
|
return this.getter.getReturnType();
|
||||||
|
|
||||||
|
return this.field.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
Object getValue(Object o)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (mode == MODE_GETSET)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Object ro = this.getter.invoke(o, null);
|
||||||
|
return ro;
|
||||||
|
} catch (InvocationTargetException itex)
|
||||||
|
{
|
||||||
|
System.err.println(itex.toString());
|
||||||
|
itex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.field.get(o);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
System.err.println("IllegalArgument! " + e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
System.err.println("IllegalAccess! " + e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
void setValue(Object o,Object value)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (mode == MODE_GETSET)
|
||||||
|
{
|
||||||
|
if (this.setter != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.setter.invoke(o,new Object[]{ value });
|
||||||
|
} catch (InvocationTargetException itex)
|
||||||
|
{
|
||||||
|
System.err.println(itex.toString());
|
||||||
|
itex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.field.set(o,value);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
System.err.println("IllegalArgument! " + e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
System.err.println("IllegalAccess! " + e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package org.hwo.models;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.swing.AbstractListModel;
|
||||||
|
|
||||||
|
public class FlexibleObjectListModel<T> extends AbstractListModel {
|
||||||
|
|
||||||
|
private List<T> items;
|
||||||
|
|
||||||
|
public FlexibleObjectListModel() {
|
||||||
|
this.items = new ArrayList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getElementAt(int index) {
|
||||||
|
return this.items.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSize() {
|
||||||
|
return this.items.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
this.items.clear();
|
||||||
|
fireContentsChanged(this, 0, getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(List<T> items) {
|
||||||
|
this.items = items;
|
||||||
|
fireContentsChanged(this, 0, getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(T o)
|
||||||
|
{
|
||||||
|
this.items.add(o);
|
||||||
|
fireContentsChanged(this, 0, getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeItem(T o)
|
||||||
|
{
|
||||||
|
int index = this.items.indexOf(o);
|
||||||
|
this.items.remove(o);
|
||||||
|
fireContentsChanged(this, 0, getSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,115 @@
|
||||||
|
package org.hwo.models;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.swing.table.AbstractTableModel;
|
||||||
|
|
||||||
|
public class FlexibleObjectTableModel<T> extends AbstractTableModel{
|
||||||
|
|
||||||
|
List<Column> columns;
|
||||||
|
List<T> rows;
|
||||||
|
|
||||||
|
Class p_classinfo;
|
||||||
|
|
||||||
|
public FlexibleObjectTableModel(Class tClass)
|
||||||
|
{
|
||||||
|
this.columns = new ArrayList<Column>();
|
||||||
|
this.rows = new ArrayList<T>();
|
||||||
|
|
||||||
|
this.p_classinfo = tClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addColumn(String fieldName)
|
||||||
|
{
|
||||||
|
this.addColumn(fieldName,true);
|
||||||
|
}
|
||||||
|
public void addColumn(String fieldName,boolean readOnly)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.columns.add(new Column(this.p_classinfo,fieldName,readOnly));
|
||||||
|
fireTableStructureChanged();
|
||||||
|
} catch (NoSuchFieldException nsfex)
|
||||||
|
{
|
||||||
|
System.err.println(nsfex.toString());
|
||||||
|
nsfex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<T> getRows()
|
||||||
|
{
|
||||||
|
return this.rows;
|
||||||
|
}
|
||||||
|
public void setRows(List<T> rows)
|
||||||
|
{
|
||||||
|
this.rows = rows;
|
||||||
|
this.fireTableDataChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int indexOf(Object o)
|
||||||
|
{
|
||||||
|
return this.rows.indexOf(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRow(T o)
|
||||||
|
{
|
||||||
|
this.rows.add(o);
|
||||||
|
this.fireTableDataChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeRow(T o)
|
||||||
|
{
|
||||||
|
this.rows.remove(o);
|
||||||
|
this.fireTableDataChanged();
|
||||||
|
}
|
||||||
|
public void removeRow(int row)
|
||||||
|
{
|
||||||
|
this.rows.remove(row);
|
||||||
|
this.fireTableDataChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnCount() {
|
||||||
|
return this.columns.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getRowCount() {
|
||||||
|
return this.rows.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValueAt(int arg0, int arg1) {
|
||||||
|
return this.columns.get(arg1).getValue(this.rows.get(arg0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getColumnName(int columnIndex)
|
||||||
|
{
|
||||||
|
return this.columns.get(columnIndex).getFieldName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setValueAt(Object aValue,
|
||||||
|
int rowIndex,
|
||||||
|
int columnIndex)
|
||||||
|
{
|
||||||
|
this.columns.get(columnIndex).setValue(this.rows.get(rowIndex), aValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCellEditable(int rowIndex,int columnIndex)
|
||||||
|
{
|
||||||
|
return !this.columns.get(columnIndex).getReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class getColumnClass(int columnIndex)
|
||||||
|
{
|
||||||
|
return this.columns.get(columnIndex).getColumnClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package org.hwo.models;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.table.AbstractTableModel;
|
||||||
|
|
||||||
|
public class ListTableModel extends AbstractTableModel {
|
||||||
|
|
||||||
|
private List<Object[]> rows;
|
||||||
|
|
||||||
|
public ListTableModel()
|
||||||
|
{
|
||||||
|
rows = new ArrayList<Object[]>();
|
||||||
|
rows.add(new Object[]{"Eins","Zwei","Drei"});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Integer calcColumnCount()
|
||||||
|
{
|
||||||
|
Integer max = 0;
|
||||||
|
|
||||||
|
for (Object[] record:rows)
|
||||||
|
if (record.length > max)
|
||||||
|
max = record.length;
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnCount() {
|
||||||
|
return calcColumnCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getRowCount() {
|
||||||
|
return rows.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
||||||
|
return rows.get(rowIndex)[columnIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Object[]> getRows() {
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setRows(List<Object[]> rows) {
|
||||||
|
this.rows = rows;
|
||||||
|
fireTableStructureChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
public abstract class AbstractTableMapperListener implements
|
||||||
|
TableMapperListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void ValueChanged(int row, int column) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface TableColumn {
|
||||||
|
|
||||||
|
String label() default "";
|
||||||
|
boolean readonly() default false;
|
||||||
|
boolean visible() default true;
|
||||||
|
int width() default 100;
|
||||||
|
String after() default "";
|
||||||
|
boolean firstColumn() default false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,637 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.event.ListSelectionListener;
|
||||||
|
import javax.swing.table.AbstractTableModel;
|
||||||
|
|
||||||
|
import org.hwo.csv.CSV;
|
||||||
|
import org.hwo.interactiveobjects.InteractiveObjectHelper;
|
||||||
|
|
||||||
|
public class TableMapper extends AbstractTableModel
|
||||||
|
{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -6556060772230310664L;
|
||||||
|
|
||||||
|
public class ColumnInfo
|
||||||
|
{
|
||||||
|
private TableColumn p_annotation;
|
||||||
|
|
||||||
|
private Field p_field;
|
||||||
|
private Method p_method;
|
||||||
|
private String p_label;
|
||||||
|
private boolean p_readonly;
|
||||||
|
private Class<?> p_columnclass;
|
||||||
|
private int width;
|
||||||
|
private TableColumn tableColumn;
|
||||||
|
|
||||||
|
private ColumnInfo nextColumn;
|
||||||
|
|
||||||
|
protected ColumnInfo(TableColumn tc,Field field)
|
||||||
|
{
|
||||||
|
tableColumn = tc;
|
||||||
|
p_annotation = field.getAnnotation(TableColumn.class);
|
||||||
|
|
||||||
|
p_method = null;
|
||||||
|
p_field = field;
|
||||||
|
p_columnclass = field.getType();
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
if (p_label.equals(""))
|
||||||
|
p_label = field.getName();
|
||||||
|
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
protected ColumnInfo(TableColumn tc,Method method)
|
||||||
|
{
|
||||||
|
tableColumn = tc;
|
||||||
|
p_annotation = method.getAnnotation(TableColumn.class);
|
||||||
|
|
||||||
|
p_method = method;
|
||||||
|
p_field = null;
|
||||||
|
p_columnclass = method.getReturnType();
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
if (p_label.equals(""))
|
||||||
|
p_label = method.getName();
|
||||||
|
|
||||||
|
method.setAccessible(true);
|
||||||
|
|
||||||
|
p_readonly = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
p_label = p_annotation.label();
|
||||||
|
p_readonly = p_annotation.readonly();
|
||||||
|
setWidth(p_annotation.width());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void appendAtEnd(ColumnInfo column)
|
||||||
|
{
|
||||||
|
if (nextColumn == null)
|
||||||
|
nextColumn = column;
|
||||||
|
else
|
||||||
|
nextColumn.appendAtEnd(column);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ColumnInfo> toList()
|
||||||
|
{
|
||||||
|
List<ColumnInfo> list = new LinkedList<TableMapper.ColumnInfo>();
|
||||||
|
appendToList(list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void appendToList(List<ColumnInfo> list)
|
||||||
|
{
|
||||||
|
list.add(this);
|
||||||
|
if (nextColumn != null)
|
||||||
|
nextColumn.appendToList(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeColumn(ColumnInfo ci)
|
||||||
|
{
|
||||||
|
if (nextColumn == ci)
|
||||||
|
{
|
||||||
|
nextColumn = ci.nextColumn;
|
||||||
|
ci.nextColumn = null;
|
||||||
|
} else if (nextColumn != null)
|
||||||
|
nextColumn.removeColumn(ci);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColumnInfo findColumnByName(String name)
|
||||||
|
{
|
||||||
|
if (this.p_label.equals(name))
|
||||||
|
return this;
|
||||||
|
if (nextColumn!=null)
|
||||||
|
return nextColumn.findColumnByName(name);
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void append(ColumnInfo ci)
|
||||||
|
{
|
||||||
|
ci.nextColumn = nextColumn;
|
||||||
|
nextColumn = ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getLabel()
|
||||||
|
{
|
||||||
|
return this.p_label;
|
||||||
|
}
|
||||||
|
public void setLabel(String label)
|
||||||
|
{
|
||||||
|
this.p_label = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getReadOnly()
|
||||||
|
{
|
||||||
|
return this.p_readonly;
|
||||||
|
}
|
||||||
|
public void setReadOnly(boolean readOnly)
|
||||||
|
{
|
||||||
|
this.p_readonly = readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnClass(Class<?> cl)
|
||||||
|
{
|
||||||
|
this.p_columnclass = cl;
|
||||||
|
}
|
||||||
|
public Class<?> getColumnClass()
|
||||||
|
{
|
||||||
|
return this.p_columnclass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getValue(Object instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
|
||||||
|
{
|
||||||
|
if (p_field != null)
|
||||||
|
return p_field.get(instance);
|
||||||
|
if (p_method != null)
|
||||||
|
return p_method.invoke(instance, null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(Object instance,Object value) throws IllegalAccessException
|
||||||
|
{
|
||||||
|
if (p_field != null)
|
||||||
|
p_field.set(instance, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWidth(int width) {
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColumnInfo getNextColumn() {
|
||||||
|
return nextColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextColumn(ColumnInfo nextColumn) {
|
||||||
|
this.nextColumn = nextColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TableColumn getTableColumn() {
|
||||||
|
return tableColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTableColumn(TableColumn tableColumn) {
|
||||||
|
this.tableColumn = tableColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Class<?> p_class;
|
||||||
|
|
||||||
|
private ColumnInfo firstColumn;
|
||||||
|
|
||||||
|
private List<Object> p_rows;
|
||||||
|
private List<ColumnInfo> p_columns;
|
||||||
|
private boolean p_readonly; // Table readonly...
|
||||||
|
|
||||||
|
private TableMapperColumnModel
|
||||||
|
columnModel;
|
||||||
|
|
||||||
|
private JTable jTable;
|
||||||
|
|
||||||
|
private TableMapperObject
|
||||||
|
tableMapperObject;
|
||||||
|
private Field editorObjectField;
|
||||||
|
private Class<?> editorObjectClass;
|
||||||
|
|
||||||
|
private MouseAdapter mouseAdapter;
|
||||||
|
|
||||||
|
private boolean editorEnabled;
|
||||||
|
|
||||||
|
private LinkedList<TableMapperListener> tableMapperListeners;
|
||||||
|
|
||||||
|
public TableMapper(Class<?> clazz,JTable table)
|
||||||
|
{
|
||||||
|
this.tableMapperListeners = new LinkedList<TableMapperListener>();
|
||||||
|
this.p_class = clazz;
|
||||||
|
this.jTable = table;
|
||||||
|
|
||||||
|
this.firstColumn = null;
|
||||||
|
|
||||||
|
this.p_rows = new LinkedList<Object>();
|
||||||
|
this.p_columns = new ArrayList<TableMapper.ColumnInfo>();
|
||||||
|
this.p_readonly = false;
|
||||||
|
|
||||||
|
initializeMapper();
|
||||||
|
initializeColumnInfo();
|
||||||
|
|
||||||
|
table.setModel(this);
|
||||||
|
table.setColumnModel(new TableMapperColumnModel(this));
|
||||||
|
|
||||||
|
table.setAutoCreateRowSorter(true);
|
||||||
|
|
||||||
|
mouseAdapter = new MouseAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
switch (e.getButton())
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
if (e.getClickCount()==2)
|
||||||
|
{
|
||||||
|
openEditor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (InteractiveObjectHelper.isInteractiveObject(editorObjectClass))
|
||||||
|
setEditorEnabled(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeMapper()
|
||||||
|
{
|
||||||
|
tableMapperObject = p_class.getAnnotation(TableMapperObject.class);
|
||||||
|
if (tableMapperObject != null)
|
||||||
|
{
|
||||||
|
if (tableMapperObject.editorField() != null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
editorObjectField = p_class.getDeclaredField(tableMapperObject.editorField());
|
||||||
|
editorObjectField.setAccessible(true);
|
||||||
|
|
||||||
|
editorObjectClass = editorObjectField.getType();
|
||||||
|
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
editorObjectClass = p_class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListSelectionListener(ListSelectionListener listener)
|
||||||
|
{
|
||||||
|
jTable.getSelectionModel().addListSelectionListener(listener);
|
||||||
|
}
|
||||||
|
public void removeListSelectionListener(ListSelectionListener listener)
|
||||||
|
{
|
||||||
|
jTable.getSelectionModel().removeListSelectionListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTableMapperListener(TableMapperListener listener)
|
||||||
|
{
|
||||||
|
tableMapperListeners.add(listener);
|
||||||
|
}
|
||||||
|
public void removeTableMapperListener(TableMapperListener listener)
|
||||||
|
{
|
||||||
|
tableMapperListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireValueChanged(int row,int column)
|
||||||
|
{
|
||||||
|
for (TableMapperListener listener: tableMapperListeners)
|
||||||
|
listener.ValueChanged(row, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getEditorObject()
|
||||||
|
{
|
||||||
|
if (jTable.getSelectedRow()!=-1)
|
||||||
|
{
|
||||||
|
int row = jTable.convertRowIndexToModel(jTable.getSelectedRow());
|
||||||
|
Object rowObject = p_rows.get(row);
|
||||||
|
if (editorObjectField == null)
|
||||||
|
return rowObject;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Object editorObject = editorObjectField.get(rowObject);
|
||||||
|
return editorObject;
|
||||||
|
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openEditor()
|
||||||
|
{
|
||||||
|
Object editorObject = getEditorObject();
|
||||||
|
if (editorObject!=null)
|
||||||
|
InteractiveObjectHelper.showEditor(editorObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ColumnInfo> getColumnInfo()
|
||||||
|
{
|
||||||
|
return p_columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(List rows)
|
||||||
|
{
|
||||||
|
this.p_rows = rows;
|
||||||
|
fireTableDataChanged();
|
||||||
|
}
|
||||||
|
public List getRows()
|
||||||
|
{
|
||||||
|
return this.p_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<T> getRows(Class<T> clazz)
|
||||||
|
{
|
||||||
|
return (List<T>) p_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
this.p_rows.clear();
|
||||||
|
fireTableDataChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEditorEnabled()
|
||||||
|
{
|
||||||
|
return editorEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEditorEnabled(boolean enabled)
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
jTable.addMouseListener(mouseAdapter);
|
||||||
|
else
|
||||||
|
jTable.removeMouseListener(mouseAdapter);
|
||||||
|
|
||||||
|
editorEnabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addColumnInfo(ColumnInfo ci)
|
||||||
|
{
|
||||||
|
if (ci.getTableColumn().firstColumn())
|
||||||
|
p_columns.add(0,ci);
|
||||||
|
else
|
||||||
|
p_columns.add(ci);
|
||||||
|
/*
|
||||||
|
if (firstColumn == null)
|
||||||
|
firstColumn = ci;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ci.getTableColumn().firstColumn())
|
||||||
|
{
|
||||||
|
ci.setNextColumn(firstColumn);
|
||||||
|
firstColumn = ci;
|
||||||
|
} else
|
||||||
|
firstColumn.appendAtEnd(ci);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addFieldsOfClass(Class<?> clazz)
|
||||||
|
{
|
||||||
|
if (clazz.getSuperclass() != null)
|
||||||
|
addFieldsOfClass(clazz.getSuperclass());
|
||||||
|
|
||||||
|
for (Field field: clazz.getDeclaredFields())
|
||||||
|
{
|
||||||
|
if (field.getAnnotation(TableColumn.class)!=null)
|
||||||
|
{
|
||||||
|
ColumnInfo ci = new ColumnInfo(field.getAnnotation(TableColumn.class),field);
|
||||||
|
addColumnInfo(ci);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Method method: clazz.getDeclaredMethods())
|
||||||
|
{
|
||||||
|
if (method.getAnnotation(TableColumn.class)!=null)
|
||||||
|
{
|
||||||
|
ColumnInfo ci = new ColumnInfo(method.getAnnotation(TableColumn.class),method);
|
||||||
|
addColumnInfo(ci);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int findColumnIndexByName(String name)
|
||||||
|
{
|
||||||
|
for (int i=0;i<p_columns.size();i++)
|
||||||
|
{
|
||||||
|
if (p_columns.get(i).p_label.equals(name))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reorderColumns()
|
||||||
|
{
|
||||||
|
List<ColumnInfo> lc = new ArrayList<TableMapper.ColumnInfo>(p_columns);
|
||||||
|
|
||||||
|
for (ColumnInfo ci:lc)
|
||||||
|
{
|
||||||
|
if (!ci.getTableColumn().after().equals(""))
|
||||||
|
{
|
||||||
|
int indColumn = p_columns.indexOf(ci);
|
||||||
|
int indBefore = findColumnIndexByName(ci.getTableColumn().after());
|
||||||
|
if (indBefore != -1)
|
||||||
|
{
|
||||||
|
if (indBefore < indColumn)
|
||||||
|
{
|
||||||
|
List<ColumnInfo> subset = new ArrayList<TableMapper.ColumnInfo>(p_columns.subList(indColumn, p_columns.size()));
|
||||||
|
p_columns.removeAll(subset);
|
||||||
|
p_columns.addAll(indBefore+1, subset);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
List<ColumnInfo> subset = new ArrayList<TableMapper.ColumnInfo>(p_columns.subList(indColumn, indBefore));
|
||||||
|
p_columns.removeAll(subset);
|
||||||
|
p_columns.addAll(indColumn+1, subset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeColumnInfo()
|
||||||
|
{
|
||||||
|
addFieldsOfClass(p_class);
|
||||||
|
reorderColumns();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReadOnly(boolean readOnly)
|
||||||
|
{
|
||||||
|
this.p_readonly = readOnly;
|
||||||
|
}
|
||||||
|
public boolean getReadOnly()
|
||||||
|
{
|
||||||
|
return this.p_readonly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRow(Object row)
|
||||||
|
{
|
||||||
|
this.p_rows.add(row);
|
||||||
|
//fireTableRowsInserted(getRowCount()-1, getRowCount()-1);
|
||||||
|
fireTableDataChanged();
|
||||||
|
}
|
||||||
|
public void removeRow(Object row)
|
||||||
|
{
|
||||||
|
int index = this.p_rows.indexOf(row);
|
||||||
|
if (index != -1)
|
||||||
|
{
|
||||||
|
this.p_rows.remove(index);
|
||||||
|
fireTableRowsDeleted(index, index);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getRow(int index)
|
||||||
|
{
|
||||||
|
return this.p_rows.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getRow(int index,Class<T> clazz)
|
||||||
|
{
|
||||||
|
return (T)this.p_rows.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object appendNewInstance()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Object instance = this.p_class.newInstance();
|
||||||
|
if (instance != null)
|
||||||
|
addRow(instance);
|
||||||
|
return instance;
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnCount() {
|
||||||
|
return this.p_columns.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getRowCount() {
|
||||||
|
return this.p_rows.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
||||||
|
if ( (rowIndex < this.p_rows.size()) && (columnIndex < this.p_columns.size()))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return this.p_columns.get(columnIndex).getValue(this.p_rows.get(rowIndex));
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.err.println("Exception: " + ex);
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setValueAt(Object aValue,
|
||||||
|
int rowIndex,
|
||||||
|
int columnIndex)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.p_columns.get(columnIndex).setValue(this.p_rows.get(rowIndex), aValue);
|
||||||
|
fireValueChanged(rowIndex, columnIndex);
|
||||||
|
} catch (IllegalAccessException ex)
|
||||||
|
{
|
||||||
|
System.err.println("IllegalAccessException: " + ex);
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCellEditable(int rowIndex,int columnIndex)
|
||||||
|
{
|
||||||
|
if (this.p_readonly)
|
||||||
|
return false;
|
||||||
|
return !this.p_columns.get(columnIndex).getReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getColumnClass(int columnIndex)
|
||||||
|
{
|
||||||
|
Class<?> c = this.p_columns.get(columnIndex).getColumnClass();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getColumnName(int columnIndex)
|
||||||
|
{
|
||||||
|
return this.p_columns.get(columnIndex).getLabel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getSelectedRow()
|
||||||
|
{
|
||||||
|
if (jTable.getSelectedRow()!=-1)
|
||||||
|
{
|
||||||
|
return this.p_rows.get(jTable.convertRowIndexToModel(jTable.getSelectedRow()));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exportToFile(File exportFile)
|
||||||
|
{
|
||||||
|
CSV csv = new CSV();
|
||||||
|
|
||||||
|
List<String[]> cells = csv.getCells();
|
||||||
|
|
||||||
|
String[] header = new String[ getColumnCount() ];
|
||||||
|
for (int i=0;i<getColumnCount();i++)
|
||||||
|
header[i] = p_columns.get(i).getLabel();
|
||||||
|
|
||||||
|
cells.add( header );
|
||||||
|
|
||||||
|
for (int row=0;row<getRowCount();row++)
|
||||||
|
{
|
||||||
|
String[] record = new String[ getColumnCount() ];
|
||||||
|
|
||||||
|
for (int column=0;column<getColumnCount();column++)
|
||||||
|
if (getValueAt(row, column)!=null)
|
||||||
|
record[column] = getValueAt(row, column).toString();
|
||||||
|
else
|
||||||
|
record[column] = "";
|
||||||
|
|
||||||
|
cells.add(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
csv.saveToFile(exportFile);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
|
@ -0,0 +1,170 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import javax.swing.DefaultListSelectionModel;
|
||||||
|
import javax.swing.ListSelectionModel;
|
||||||
|
import javax.swing.event.ChangeEvent;
|
||||||
|
import javax.swing.event.TableColumnModelListener;
|
||||||
|
import javax.swing.table.TableColumn;
|
||||||
|
import javax.swing.table.TableColumnModel;
|
||||||
|
|
||||||
|
import org.hwo.models.TableMapper.TableMapper.ColumnInfo;
|
||||||
|
|
||||||
|
class TableMapperColumnModel implements TableColumnModel{
|
||||||
|
|
||||||
|
private TableMapper tableMapper;
|
||||||
|
private LinkedList<TableColumnModelListener> listeners;
|
||||||
|
|
||||||
|
private ArrayList<TableColumn> tableColumns;
|
||||||
|
|
||||||
|
private int margin;
|
||||||
|
|
||||||
|
private ListSelectionModel listSelectionModel;
|
||||||
|
|
||||||
|
public TableMapperColumnModel(TableMapper tableMapper)
|
||||||
|
{
|
||||||
|
this.tableMapper = tableMapper;
|
||||||
|
this.listeners = new LinkedList<TableColumnModelListener>();
|
||||||
|
this.tableColumns = new ArrayList<TableColumn>();
|
||||||
|
this.listSelectionModel = new DefaultListSelectionModel();
|
||||||
|
|
||||||
|
for (int i=0;i<tableMapper.getColumnCount();i++)
|
||||||
|
{
|
||||||
|
ColumnInfo ci = tableMapper.getColumnInfo().get(i);
|
||||||
|
TableColumn tc = new TableColumn(i, ci.getWidth());
|
||||||
|
tc.setPreferredWidth(ci.getWidth());
|
||||||
|
tc.setHeaderValue(ci.getLabel());
|
||||||
|
tc.addPropertyChangeListener(new PropertyChangeListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void propertyChange(PropertyChangeEvent arg0) {
|
||||||
|
fireColumnMarginChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
tableColumns.add(tc);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireColumnMarginChanged()
|
||||||
|
{
|
||||||
|
for (TableColumnModelListener listener:listeners)
|
||||||
|
listener.columnMarginChanged(new ChangeEvent(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addColumn(TableColumn aColumn) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addColumnModelListener(TableColumnModelListener x) {
|
||||||
|
listeners.add(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeColumnModelListener(TableColumnModelListener x) {
|
||||||
|
listeners.remove(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TableColumn getColumn(int columnIndex) {
|
||||||
|
return tableColumns.get(columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnCount() {
|
||||||
|
return tableColumns.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnIndex(Object columnIdentifier) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnIndexAtX(int xPosition) {
|
||||||
|
int pos = 0;
|
||||||
|
for (int i=0;i<tableColumns.size();i++)
|
||||||
|
{
|
||||||
|
pos += tableColumns.get(i).getWidth();
|
||||||
|
|
||||||
|
if (xPosition < pos)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnMargin() {
|
||||||
|
return margin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getColumnSelectionAllowed() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<TableColumn> getColumns() {
|
||||||
|
return new Vector<TableColumn>(tableColumns).elements();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSelectedColumnCount() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] getSelectedColumns() {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ListSelectionModel getSelectionModel() {
|
||||||
|
return listSelectionModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTotalColumnWidth() {
|
||||||
|
int width = 0;
|
||||||
|
for (TableColumn tc:tableColumns)
|
||||||
|
width += tc.getWidth();
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void moveColumn(int columnIndex, int newIndex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeColumn(TableColumn column) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setColumnMargin(int newMargin) {
|
||||||
|
margin = newMargin;
|
||||||
|
fireColumnMarginChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setColumnSelectionAllowed(boolean flag) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSelectionModel(ListSelectionModel newModel) {
|
||||||
|
listSelectionModel = newModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
public interface TableMapperListener {
|
||||||
|
|
||||||
|
public void ValueChanged(int row,int column);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package org.hwo.models.TableMapper;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface TableMapperObject {
|
||||||
|
|
||||||
|
String editorField();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
package org.hwo.nativeloader;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import javax.management.RuntimeErrorException;
|
||||||
|
|
||||||
|
import org.hwo.os.OsDetect;
|
||||||
|
|
||||||
|
public class NativeLoader {
|
||||||
|
|
||||||
|
public static void loadLibrary(String libName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.loadLibrary(libName);
|
||||||
|
return;
|
||||||
|
} catch (UnsatisfiedLinkError e)
|
||||||
|
{
|
||||||
|
System.err.println("Native Library not found, trying to load from JAR-File");
|
||||||
|
System.err.println(e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
String platformLibName;
|
||||||
|
String resourcePath;
|
||||||
|
|
||||||
|
switch (OsDetect.getOperatingSystem())
|
||||||
|
{
|
||||||
|
case LINUX:
|
||||||
|
platformLibName = String.format("lib%s.so",libName);
|
||||||
|
break;
|
||||||
|
case OSX:
|
||||||
|
platformLibName = String.format("lib%s.dylib",libName);
|
||||||
|
break;
|
||||||
|
case WINDOWS:
|
||||||
|
platformLibName = String.format("%s.dll",libName);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Betriebssystem nicht unterstŸtzt!");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.err.println(String.format("NativeLoader: Platform dependent Name: %s",platformLibName));
|
||||||
|
|
||||||
|
resourcePath = "/native/" + platformLibName;
|
||||||
|
System.err.println(String.format("NativeLoader: Resource name: %s",resourcePath));
|
||||||
|
|
||||||
|
InputStream libInputStream = NativeLoader.class.getResourceAsStream(resourcePath);
|
||||||
|
if (libInputStream == null)
|
||||||
|
{
|
||||||
|
System.err.println("Resource wurde nicht gefunden!");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
File tempLibFile = File.createTempFile("native-", platformLibName);
|
||||||
|
FileOutputStream fos = new FileOutputStream(tempLibFile);
|
||||||
|
|
||||||
|
System.err.println(String.format("Using Temp File: %s",tempLibFile.getAbsoluteFile()));
|
||||||
|
System.err.println(String.format("Size: %d",libInputStream.available()));
|
||||||
|
|
||||||
|
byte[] buffer = new byte[libInputStream.available()];
|
||||||
|
libInputStream.read(buffer);
|
||||||
|
fos.write(buffer);
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
System.err.println(String.format("Loading: %s", tempLibFile.getAbsolutePath()));
|
||||||
|
System.load(tempLibFile.getAbsolutePath());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package org.hwo.net;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.hwo.net.serverobjects.ServerObjectRequest;
|
||||||
|
|
||||||
|
public interface ServerObject {
|
||||||
|
|
||||||
|
public ServerObject getRootObject();
|
||||||
|
public ServerObject getParent();
|
||||||
|
public ServerObject[] getChildren();
|
||||||
|
public ServerObject getNamedChild(String childName);
|
||||||
|
|
||||||
|
public void addNamedChild(String childName,ServerObject serverObject);
|
||||||
|
|
||||||
|
public void climb(ServerObjectRequest request) throws IOException;
|
||||||
|
public void request(ServerObjectRequest request) throws IOException;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.hwo.net;
|
||||||
|
|
||||||
|
public interface ServerObjectProvider {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class HttpRequestURI {
|
||||||
|
|
||||||
|
private String requestPath;
|
||||||
|
private HttpServerRequest request;
|
||||||
|
|
||||||
|
private String path;
|
||||||
|
private String[] elements;
|
||||||
|
private String queryString;
|
||||||
|
private String hostname;
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
public HttpRequestURI(HttpServerRequest request,String path)
|
||||||
|
{
|
||||||
|
this.requestPath = path;
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void decode()
|
||||||
|
{
|
||||||
|
if (requestPath.indexOf('?')>0)
|
||||||
|
{
|
||||||
|
queryString = requestPath.substring(requestPath.indexOf('?')+1);
|
||||||
|
this.path = requestPath.substring(0,requestPath.indexOf('?'));
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
queryString = "";
|
||||||
|
this.path = requestPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
String ptokens[] = this.path.split("/");
|
||||||
|
if (ptokens.length>0)
|
||||||
|
elements = Arrays.copyOfRange(ptokens, 1, ptokens.length);
|
||||||
|
else
|
||||||
|
elements = new String[0];
|
||||||
|
|
||||||
|
hostname = request.getRequestHeader("Host");
|
||||||
|
if (hostname.indexOf(':')>0)
|
||||||
|
{
|
||||||
|
String[] tokens = hostname.split(":");
|
||||||
|
hostname = tokens[0];
|
||||||
|
port = Integer.parseInt(tokens[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath()
|
||||||
|
{
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getQueryString()
|
||||||
|
{
|
||||||
|
return queryString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getPathElements()
|
||||||
|
{
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpRetryException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
import org.hwo.net.requesthandler.ServerObjectHandler;
|
||||||
|
import org.hwo.net.serverobjects.VirtualRootObject;
|
||||||
|
|
||||||
|
public class HttpServer extends Thread {
|
||||||
|
|
||||||
|
private int port;
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
|
private HttpServerConnectionFactory connectionFactory;
|
||||||
|
private HttpServerRequestFactory requestFactory;
|
||||||
|
private HttpServerRequestHandler requestHandler;
|
||||||
|
|
||||||
|
public HttpServer(int port)
|
||||||
|
{
|
||||||
|
this.port = port;
|
||||||
|
this.connectionFactory = new HttpServerConnection.Factory();
|
||||||
|
this.requestFactory = new HttpServerRequest.Factory();
|
||||||
|
|
||||||
|
this.requestHandler = new HttpServerRequestHandler() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doRequest(HttpServerRequest httpRequest) throws IOException{
|
||||||
|
httpRequest.setResponseHeader("Content-Type", "text/plain");
|
||||||
|
httpRequest.getResponseWriter().write("This Page has no other content than this text.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpServerConnectionFactory getConnectionFactory()
|
||||||
|
{
|
||||||
|
return connectionFactory;
|
||||||
|
}
|
||||||
|
public void setConnectionFactory(HttpServerConnectionFactory factory)
|
||||||
|
{
|
||||||
|
connectionFactory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public HttpServerRequestFactory getRequestFactory()
|
||||||
|
{
|
||||||
|
return requestFactory;
|
||||||
|
}
|
||||||
|
public void setRequestFactory(HttpServerRequestFactory factory)
|
||||||
|
{
|
||||||
|
requestFactory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpServerRequestHandler getRequestHandler() {
|
||||||
|
return requestHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequestHandler(HttpServerRequestHandler requestHandler) {
|
||||||
|
this.requestHandler = requestHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
serverSocket = new ServerSocket(port);
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
|
||||||
|
HttpServerConnection connection = getConnectionFactory().createHttpServerConnection(this, clientSocket);
|
||||||
|
connection.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
HttpServer httpServer = new HttpServer(8080);
|
||||||
|
ServerObjectHandler soh = new ServerObjectHandler();
|
||||||
|
|
||||||
|
httpServer.setRequestHandler(soh);
|
||||||
|
|
||||||
|
httpServer.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class HttpServerConnection extends Thread {
|
||||||
|
|
||||||
|
public static class Factory implements HttpServerConnectionFactory
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public HttpServerConnection createHttpServerConnection(
|
||||||
|
HttpServer httpServer, Socket clientSocket) throws IOException {
|
||||||
|
return new HttpServerConnection(httpServer, clientSocket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpServer httpServer;
|
||||||
|
private Socket clientSocket;
|
||||||
|
|
||||||
|
private BufferedReader bufferedReader;
|
||||||
|
private BufferedWriter bufferedWriter;
|
||||||
|
|
||||||
|
public HttpServerConnection(HttpServer httpServer,Socket clientSocket) throws IOException
|
||||||
|
{
|
||||||
|
this.httpServer = httpServer;
|
||||||
|
this.clientSocket = clientSocket;
|
||||||
|
this.bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpServer getHttpServer()
|
||||||
|
{
|
||||||
|
return httpServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Socket getClientSocket()
|
||||||
|
{
|
||||||
|
return clientSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedReader getBufferedReader()
|
||||||
|
{
|
||||||
|
return bufferedReader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedWriter getBufferedWriter()
|
||||||
|
{
|
||||||
|
return bufferedWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void close() throws IOException
|
||||||
|
{
|
||||||
|
clientSocket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (!clientSocket.isClosed())
|
||||||
|
{
|
||||||
|
HttpServerRequest request = httpServer.getRequestFactory().createRequest(this);
|
||||||
|
|
||||||
|
doRequest(request);
|
||||||
|
|
||||||
|
request.sendResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
close();
|
||||||
|
} catch (IOException exception)
|
||||||
|
{
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doRequest(HttpServerRequest httpRequest) throws IOException
|
||||||
|
{
|
||||||
|
httpServer.getRequestHandler().doRequest(httpRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public interface HttpServerConnectionFactory {
|
||||||
|
|
||||||
|
public HttpServerConnection createHttpServerConnection(HttpServer httpServer,Socket clientSocket) throws IOException;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,188 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class HttpServerRequest {
|
||||||
|
|
||||||
|
public static class Factory implements HttpServerRequestFactory
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public HttpServerRequest createRequest(HttpServerConnection connection) throws IOException {
|
||||||
|
return new HttpServerRequest(connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpServerConnection connection;
|
||||||
|
|
||||||
|
String requestLine;
|
||||||
|
String requestMethod;
|
||||||
|
String requestProtocol;
|
||||||
|
HttpRequestURI
|
||||||
|
requestURI;
|
||||||
|
|
||||||
|
Properties header;
|
||||||
|
|
||||||
|
Integer resultCode;
|
||||||
|
Properties responseHeader;
|
||||||
|
byte[] responseBody;
|
||||||
|
|
||||||
|
ByteArrayOutputStream
|
||||||
|
responseOutputStream;
|
||||||
|
BufferedWriter
|
||||||
|
responseBufferedWriter;
|
||||||
|
|
||||||
|
boolean responseSent;
|
||||||
|
|
||||||
|
|
||||||
|
public HttpServerRequest(HttpServerConnection httpServerConnection) throws IOException
|
||||||
|
{
|
||||||
|
this.connection = httpServerConnection;
|
||||||
|
this.header = new Properties();
|
||||||
|
|
||||||
|
readRequest();
|
||||||
|
readHeader();
|
||||||
|
|
||||||
|
requestURI.decode();
|
||||||
|
|
||||||
|
resultCode = 400;
|
||||||
|
responseHeader = new Properties();
|
||||||
|
responseOutputStream = new ByteArrayOutputStream();
|
||||||
|
responseBufferedWriter = new BufferedWriter(new OutputStreamWriter(responseOutputStream));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isResponseSent()
|
||||||
|
{
|
||||||
|
return responseSent;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readRequest() throws IOException
|
||||||
|
{
|
||||||
|
this.requestLine = connection.getBufferedReader().readLine();
|
||||||
|
String[] tokens = this.requestLine.split(" ",3);
|
||||||
|
requestMethod = tokens[0];
|
||||||
|
if (tokens.length > 1)
|
||||||
|
requestURI = new HttpRequestURI(this,tokens[1]);
|
||||||
|
if (tokens.length > 2)
|
||||||
|
requestProtocol = tokens[2];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readHeader() throws IOException
|
||||||
|
{
|
||||||
|
String line;
|
||||||
|
String headerName = null;
|
||||||
|
StringBuilder headerValue = new StringBuilder();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
line = connection.getBufferedReader().readLine();
|
||||||
|
if ((line.length() == 0) || (line.charAt(0) > 32))
|
||||||
|
{
|
||||||
|
if (headerName != null)
|
||||||
|
{
|
||||||
|
header.setProperty(headerName, headerValue.toString());
|
||||||
|
}
|
||||||
|
if (line.length() > 0)
|
||||||
|
{
|
||||||
|
headerName = line.substring(0,line.indexOf(':'));
|
||||||
|
headerValue = new StringBuilder();
|
||||||
|
headerValue.append(line.substring(line.indexOf(':')+1).trim());
|
||||||
|
} else
|
||||||
|
return;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if (headerName != null)
|
||||||
|
headerValue.append(line.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestURI getRequestURI()
|
||||||
|
{
|
||||||
|
return requestURI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRequestHeader(String headerName)
|
||||||
|
{
|
||||||
|
return header.getProperty(headerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public OutputStream getResponseOutputStream()
|
||||||
|
{
|
||||||
|
return responseOutputStream;
|
||||||
|
}
|
||||||
|
public BufferedWriter getResponseWriter()
|
||||||
|
{
|
||||||
|
return responseBufferedWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponseHeader(String headerName,String headerValue)
|
||||||
|
{
|
||||||
|
responseHeader.setProperty(headerName, headerValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void sendResponse(int code,byte[] responseBody) throws IOException
|
||||||
|
{
|
||||||
|
this.responseBody = responseBody;
|
||||||
|
this.resultCode = code;
|
||||||
|
sendResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendResponse(int code) throws IOException
|
||||||
|
{
|
||||||
|
this.resultCode = code;
|
||||||
|
sendResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void sendResponse() throws IOException
|
||||||
|
{
|
||||||
|
if (responseSent)
|
||||||
|
throw new IOException("The Response has already been sent.");
|
||||||
|
|
||||||
|
responseSent = true;
|
||||||
|
|
||||||
|
if (responseBody == null)
|
||||||
|
{
|
||||||
|
responseBufferedWriter.flush();
|
||||||
|
if (responseOutputStream.size() > 0)
|
||||||
|
{
|
||||||
|
responseBody = responseOutputStream.toByteArray();
|
||||||
|
} else
|
||||||
|
responseBody = new byte[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
responseHeader.setProperty("Content-Length", String.format("%d", responseBody.length));
|
||||||
|
|
||||||
|
String protocol = "HTTP/1.0";
|
||||||
|
if (requestProtocol != null)
|
||||||
|
protocol = requestProtocol;
|
||||||
|
|
||||||
|
connection.getBufferedWriter().write(String.format("%s %d\r\n", protocol, resultCode));
|
||||||
|
|
||||||
|
Enumeration<?> headerEnum = responseHeader.keys();
|
||||||
|
|
||||||
|
while (headerEnum.hasMoreElements())
|
||||||
|
{
|
||||||
|
String headerName = (String)headerEnum.nextElement();
|
||||||
|
connection.getBufferedWriter().write(String.format("%s: %s\r\n", headerName, responseHeader.getProperty(headerName)));
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.getBufferedWriter().write("\r\n");
|
||||||
|
connection.getBufferedWriter().flush();
|
||||||
|
connection.getClientSocket().getOutputStream().write(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface HttpServerRequestFactory {
|
||||||
|
|
||||||
|
public HttpServerRequest createRequest(HttpServerConnection connection) throws IOException;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package org.hwo.net.http;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface HttpServerRequestHandler {
|
||||||
|
|
||||||
|
public void doRequest(HttpServerRequest httpRequest) throws IOException;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package org.hwo.net.requesthandler;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.hwo.net.ServerObject;
|
||||||
|
import org.hwo.net.http.HttpServerRequest;
|
||||||
|
import org.hwo.net.http.HttpServerRequestHandler;
|
||||||
|
import org.hwo.net.serverobjects.ServerObjectRequest;
|
||||||
|
import org.hwo.net.serverobjects.VirtualRootObject;
|
||||||
|
|
||||||
|
public class ServerObjectHandler implements HttpServerRequestHandler{
|
||||||
|
|
||||||
|
private ServerObject rootObject;
|
||||||
|
private HashMap<Integer, ServerObject> errorObjects;
|
||||||
|
private ServerObject defaultErrorObject;
|
||||||
|
|
||||||
|
|
||||||
|
public ServerObjectHandler()
|
||||||
|
{
|
||||||
|
rootObject = new VirtualRootObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerObject getRootObject()
|
||||||
|
{
|
||||||
|
return rootObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerObject getErrorObject(Integer code)
|
||||||
|
{
|
||||||
|
if (errorObjects.containsKey(code))
|
||||||
|
return errorObjects.get(code);
|
||||||
|
return defaultErrorObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doRequest(HttpServerRequest httpRequest) throws IOException {
|
||||||
|
ServerObjectRequest sor = new ServerObjectRequest(httpRequest);
|
||||||
|
|
||||||
|
rootObject.climb(sor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package org.hwo.net.serverobjects;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.hwo.net.ServerObject;
|
||||||
|
import org.hwo.net.http.HttpServerRequest;
|
||||||
|
|
||||||
|
public abstract class AbstractServerObject implements ServerObject {
|
||||||
|
|
||||||
|
private ServerObject parentObject;
|
||||||
|
HashMap<String, ServerObject> mappedChildren;
|
||||||
|
|
||||||
|
public AbstractServerObject()
|
||||||
|
{
|
||||||
|
mappedChildren = new HashMap<String, ServerObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerObject getRootObject() {
|
||||||
|
if (getParent() == null)
|
||||||
|
return this;
|
||||||
|
return getParent().getRootObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerObject getParent() {
|
||||||
|
return parentObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerObject[] getChildren() {
|
||||||
|
return (new ArrayList<ServerObject>(mappedChildren.values())).toArray(new ServerObject[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addNamedChild(String childName, ServerObject serverObject) {
|
||||||
|
mappedChildren.put(childName, serverObject);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerObject getNamedChild(String childName) {
|
||||||
|
if (!mappedChildren.containsKey(childName))
|
||||||
|
return null;
|
||||||
|
return mappedChildren.get(childName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void climb(ServerObjectRequest request) throws IOException {
|
||||||
|
String nextChildName = request.popNextElement();
|
||||||
|
|
||||||
|
if (nextChildName == null)
|
||||||
|
{
|
||||||
|
request(request);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
ServerObject nextChild = getNamedChild(nextChildName);
|
||||||
|
if (nextChild == null)
|
||||||
|
throw new ServerObjectNotFoundException(request,nextChildName);
|
||||||
|
nextChild.climb(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package org.hwo.net.serverobjects;
|
||||||
|
|
||||||
|
public class ServerObjectNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
private ServerObjectRequest objectRequest;
|
||||||
|
private String childName;
|
||||||
|
|
||||||
|
public ServerObjectNotFoundException(ServerObjectRequest request)
|
||||||
|
{
|
||||||
|
objectRequest = request;
|
||||||
|
childName = null;
|
||||||
|
}
|
||||||
|
public ServerObjectNotFoundException(ServerObjectRequest request,String childName)
|
||||||
|
{
|
||||||
|
objectRequest = request;
|
||||||
|
this.childName = childName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerObjectRequest getObjectRequest()
|
||||||
|
{
|
||||||
|
return objectRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package org.hwo.net.serverobjects;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import org.hwo.net.http.HttpServerRequest;
|
||||||
|
|
||||||
|
public class ServerObjectRequest {
|
||||||
|
|
||||||
|
private HttpServerRequest httpRequest;
|
||||||
|
|
||||||
|
private Stack<String> pathToClimb;
|
||||||
|
private Stack<String> pathClimbed;
|
||||||
|
|
||||||
|
public ServerObjectRequest(HttpServerRequest httpRequest)
|
||||||
|
{
|
||||||
|
this.httpRequest = httpRequest;
|
||||||
|
pathToClimb = new Stack<String>();
|
||||||
|
pathClimbed = new Stack<String>();
|
||||||
|
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize()
|
||||||
|
{
|
||||||
|
pathToClimb = new Stack<String>();
|
||||||
|
pathToClimb.addAll(Arrays.asList(httpRequest.getRequestURI().getPathElements()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpServerRequest getHttpRequest()
|
||||||
|
{
|
||||||
|
return httpRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String popNextElement()
|
||||||
|
{
|
||||||
|
if (pathToClimb.empty())
|
||||||
|
return null;
|
||||||
|
String n = pathToClimb.pop();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushLastElement(String element)
|
||||||
|
{
|
||||||
|
pathClimbed.push(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.hwo.net.serverobjects;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.hwo.net.ServerObject;
|
||||||
|
import org.hwo.net.http.HttpServerRequest;
|
||||||
|
|
||||||
|
public class VirtualRootObject extends AbstractServerObject {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void request(ServerObjectRequest request) throws IOException{
|
||||||
|
request.getHttpRequest().setResponseHeader("Content-Type", "text/plain");
|
||||||
|
request.getHttpRequest().getResponseWriter().write("This is the virtual root of this application server.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package org.hwo.os;
|
||||||
|
|
||||||
|
public class OsDetect {
|
||||||
|
|
||||||
|
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
||||||
|
|
||||||
|
public static OsType getOperatingSystem()
|
||||||
|
{
|
||||||
|
if (System.getProperty("os.name").equals("Mac OS X"))
|
||||||
|
return OsType.OSX;
|
||||||
|
if (System.getProperty("os.name").equals("Linux"))
|
||||||
|
return OsType.LINUX;
|
||||||
|
if (System.getProperty("os.name").startsWith("Windows"))
|
||||||
|
return OsType.WINDOWS;
|
||||||
|
|
||||||
|
return OsType.UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package org.hwo.proxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public abstract class AbstractInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract Object invoke(Method method, Object target, Object[] params);
|
||||||
|
|
||||||
|
public Object invokeTarget(Method method, Object target, Object[] params)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return method.invoke(target, params);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package org.hwo.proxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
|
||||||
|
public interface Interceptor {
|
||||||
|
|
||||||
|
public Object invoke(Method method,Object target,Object[] params);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.hwo.proxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.hwo.buffer.BitStream;
|
||||||
|
import org.hwo.fifo.FiFo;
|
||||||
|
|
||||||
|
public class ProxyTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Interceptor mi = new AbstractInterceptor() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(Method method, Object target, Object[] params) {
|
||||||
|
System.err.println("INVOKE: " + method.getName());
|
||||||
|
return invokeTarget(method, target, params);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.hwo.proxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import net.sf.cglib.proxy.Enhancer;
|
||||||
|
import net.sf.cglib.proxy.MethodInterceptor;
|
||||||
|
import net.sf.cglib.proxy.MethodProxy;
|
||||||
|
|
||||||
|
public class ProxyWrapper {
|
||||||
|
|
||||||
|
public ProxyWrapper()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public <U> U proxyForObject(Class<U> clazz,U object)
|
||||||
|
{
|
||||||
|
Enhancer e = new Enhancer();
|
||||||
|
e.setSuperclass(clazz);
|
||||||
|
e.setCallback(new MethodInterceptor() {
|
||||||
|
@Override
|
||||||
|
public Object intercept(Object proxy, Method method, Object[] args,
|
||||||
|
MethodProxy methodProxy) throws Throwable {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return clazz.cast(e.create());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.hwo.tasklet;
|
||||||
|
|
||||||
|
public interface Tasklet {
|
||||||
|
|
||||||
|
public void run(Object arg);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
package org.hwo.tasklet;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import org.hwo.fifo.FiFo;
|
||||||
|
|
||||||
|
public class TaskletThread extends Thread {
|
||||||
|
|
||||||
|
private class QueuedTasklet
|
||||||
|
{
|
||||||
|
private Tasklet tasklet;
|
||||||
|
private Object argument;
|
||||||
|
|
||||||
|
public QueuedTasklet(Tasklet tasklet,Object argument)
|
||||||
|
{
|
||||||
|
this.tasklet = tasklet;
|
||||||
|
this.argument = argument;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private FiFo<QueuedTasklet> queuedTasklets = new FiFo<QueuedTasklet>();
|
||||||
|
|
||||||
|
public TaskletThread() {
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void queue(Tasklet tasklet,Object arg)
|
||||||
|
{
|
||||||
|
synchronized (queuedTasklets) {
|
||||||
|
queuedTasklets.push(new QueuedTasklet(tasklet,arg));
|
||||||
|
}
|
||||||
|
queuedTasklets.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
QueuedTasklet queuedTasklet = null;
|
||||||
|
|
||||||
|
synchronized (queuedTasklets) {
|
||||||
|
try {
|
||||||
|
queuedTasklets.wait(100);
|
||||||
|
queuedTasklet = queuedTasklets.pull();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queuedTasklet != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
queuedTasklet.tasklet.run(queuedTasklet.argument);
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.err.println("Tasklet throwed Exception: " + ex.toString());
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInterrupted())
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.hwo.test;
|
||||||
|
|
||||||
|
import org.hwo.buffer.BitStream;
|
||||||
|
|
||||||
|
public class BitStreamTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
BitStream bs = new BitStream(new byte[]{ 0x55,0x55,0x55,0x55 });
|
||||||
|
|
||||||
|
for (int i=0;i<6;i++)
|
||||||
|
System.err.println(String.format("Pattern: 0x%x",bs.read((byte)i)));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
package org.hwo.thread;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.EventListener;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class ThreadedOperation extends Thread{
|
||||||
|
|
||||||
|
List<ThreadedOperationUpdateListener> updateListeners;
|
||||||
|
private String operationName;
|
||||||
|
|
||||||
|
|
||||||
|
public ThreadedOperation()
|
||||||
|
{
|
||||||
|
updateListeners = new ArrayList<ThreadedOperationUpdateListener>();
|
||||||
|
operationName = "AktivitŠt...";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addThreadedOperationUpdateListener(ThreadedOperationUpdateListener listener)
|
||||||
|
{
|
||||||
|
updateListeners.add(listener);
|
||||||
|
}
|
||||||
|
public void removeThreadedOperationUpdateListener(ThreadedOperationUpdateListener listener)
|
||||||
|
{
|
||||||
|
updateListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireUpdate(ThreadedOperationUpdateArgs updateArgs)
|
||||||
|
{
|
||||||
|
for (ThreadedOperationUpdateListener listener:updateListeners)
|
||||||
|
listener.threadedOperationUpdate(updateArgs);
|
||||||
|
}
|
||||||
|
protected void fireUpdate(Integer progressOperation,Integer progressStep)
|
||||||
|
{
|
||||||
|
fireUpdate(new ThreadedOperationUpdateArgs(progressOperation,progressStep));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireUpdate(Integer progressOperation,Integer progressStep,String statusText)
|
||||||
|
{
|
||||||
|
fireUpdate(new ThreadedOperationUpdateArgs(progressOperation,progressStep,statusText));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireUpdate(Integer progressOperation,Integer progressStep,String statusText,String stepLabel)
|
||||||
|
{
|
||||||
|
fireUpdate(new ThreadedOperationUpdateArgs(progressOperation,progressStep,statusText,stepLabel));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fireFinished(int code)
|
||||||
|
{
|
||||||
|
for (ThreadedOperationUpdateListener listener:updateListeners)
|
||||||
|
listener.threadedOperationFinished(code);
|
||||||
|
}
|
||||||
|
protected void fireFinished()
|
||||||
|
{
|
||||||
|
fireFinished(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void run();
|
||||||
|
|
||||||
|
public String getOperationName() {
|
||||||
|
return operationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperationName(String operationName) {
|
||||||
|
this.operationName = operationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package org.hwo.thread;
|
||||||
|
|
||||||
|
public class ThreadedOperationUpdateArgs {
|
||||||
|
|
||||||
|
private Integer progressOperation;
|
||||||
|
private Integer progressStep;
|
||||||
|
private String stepLabel;
|
||||||
|
private String statusText;
|
||||||
|
private String titleText;
|
||||||
|
|
||||||
|
public ThreadedOperationUpdateArgs()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public ThreadedOperationUpdateArgs(Integer progressStep)
|
||||||
|
{
|
||||||
|
this.progressStep = progressStep;
|
||||||
|
}
|
||||||
|
public ThreadedOperationUpdateArgs(Integer progressOperation,Integer progressStep)
|
||||||
|
{
|
||||||
|
this.progressOperation = progressOperation;
|
||||||
|
this.progressStep = progressStep;
|
||||||
|
}
|
||||||
|
public ThreadedOperationUpdateArgs(Integer progressOperation,Integer progressStep,String statusText)
|
||||||
|
{
|
||||||
|
this.progressOperation = progressOperation;
|
||||||
|
this.progressStep = progressStep;
|
||||||
|
this.statusText = statusText;
|
||||||
|
}
|
||||||
|
public ThreadedOperationUpdateArgs(Integer progressOperation,Integer progressStep,String statusText,String stepLabel)
|
||||||
|
{
|
||||||
|
this.progressOperation = progressOperation;
|
||||||
|
this.progressStep = progressStep;
|
||||||
|
this.statusText = statusText;
|
||||||
|
this.stepLabel = stepLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStepLabel() {
|
||||||
|
return stepLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStepLabel(String stepLabel) {
|
||||||
|
this.stepLabel = stepLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getProgressStep() {
|
||||||
|
return progressStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProgressStep(Integer progressStep) {
|
||||||
|
this.progressStep = progressStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getProgressOperation() {
|
||||||
|
return progressOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProgressOperation(Integer progressOperation) {
|
||||||
|
this.progressOperation = progressOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatusText() {
|
||||||
|
return statusText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatusText(String statusText) {
|
||||||
|
this.statusText = statusText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitleText() {
|
||||||
|
return titleText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitleText(String titleText) {
|
||||||
|
this.titleText = titleText;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package org.hwo.thread;
|
||||||
|
|
||||||
|
public interface ThreadedOperationUpdateListener {
|
||||||
|
public void threadedOperationUpdate(ThreadedOperationUpdateArgs args);
|
||||||
|
public void threadedOperationFinished(int result);
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
|
import org.hwo.ui.treetable.TreeTable;
|
||||||
|
|
||||||
|
public interface CellEditor {
|
||||||
|
|
||||||
|
public void addCellEditorListener(CellEditorListener listener);
|
||||||
|
public void removeCellEditorListener(CellEditorListener listener);
|
||||||
|
|
||||||
|
public boolean editBegin(TreeTable treeTable,Rectangle rect,Object value);
|
||||||
|
public void editCancel();
|
||||||
|
public void editFinish();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import org.hwo.ui.treetable.TreeTable;
|
||||||
|
|
||||||
|
public class CellEditorArgs {
|
||||||
|
|
||||||
|
private CellEditor cellEditor;
|
||||||
|
private Object value;
|
||||||
|
private TreeTable treeTable;
|
||||||
|
|
||||||
|
public CellEditorArgs(CellEditor cellEditor,Object value,TreeTable treeTable)
|
||||||
|
{
|
||||||
|
this.cellEditor = cellEditor;
|
||||||
|
this.value = value;
|
||||||
|
this.treeTable = treeTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CellEditor getCellEditor() {
|
||||||
|
return cellEditor;
|
||||||
|
}
|
||||||
|
public void setCellEditor(CellEditor cellEditor) {
|
||||||
|
this.cellEditor = cellEditor;
|
||||||
|
}
|
||||||
|
public Object getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
public void setValue(Object value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
public TreeTable getTreeTable() {
|
||||||
|
return treeTable;
|
||||||
|
}
|
||||||
|
public void setTreeTable(TreeTable treeTable) {
|
||||||
|
this.treeTable = treeTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
public interface CellEditorListener {
|
||||||
|
|
||||||
|
public void editBegin(CellEditorArgs args);
|
||||||
|
public void editCanceled(CellEditorArgs args);
|
||||||
|
public void editFinished(CellEditorArgs args);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
|
public interface CellRenderer {
|
||||||
|
|
||||||
|
public void renderCell(Graphics g,int row,int column,Rectangle cellRect,Object value,Object rowObject);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import java.awt.Container;
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
import org.hwo.ui.treetable.TreeTable;
|
||||||
|
|
||||||
|
public class DefaultCellEditor implements CellEditor {
|
||||||
|
|
||||||
|
private List<CellEditorListener> editorListeners;
|
||||||
|
|
||||||
|
private JTextField textField;
|
||||||
|
private TreeTable treeTable;
|
||||||
|
private Object value;
|
||||||
|
|
||||||
|
public DefaultCellEditor()
|
||||||
|
{
|
||||||
|
editorListeners = new LinkedList<CellEditorListener>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCellEditorListener(CellEditorListener listener) {
|
||||||
|
editorListeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeCellEditorListener(CellEditorListener listener) {
|
||||||
|
editorListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fireEditBegin()
|
||||||
|
{
|
||||||
|
CellEditorArgs args = new CellEditorArgs(this, value, treeTable);
|
||||||
|
for (CellEditorListener listener: editorListeners)
|
||||||
|
listener.editBegin(args);
|
||||||
|
}
|
||||||
|
private void fireEditFinish()
|
||||||
|
{
|
||||||
|
CellEditorArgs args = new CellEditorArgs(this, value, treeTable);
|
||||||
|
for (CellEditorListener listener: editorListeners)
|
||||||
|
listener.editFinished(args);
|
||||||
|
}
|
||||||
|
private void fireEditCancel()
|
||||||
|
{
|
||||||
|
CellEditorArgs args = new CellEditorArgs(this, value, treeTable);
|
||||||
|
for (CellEditorListener listener: editorListeners)
|
||||||
|
listener.editCanceled(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void destroyTextField()
|
||||||
|
{
|
||||||
|
textField.setVisible(false);
|
||||||
|
Container parent = textField.getParent();
|
||||||
|
|
||||||
|
parent.remove(textField);
|
||||||
|
|
||||||
|
textField = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean editBegin(TreeTable treeTable, Rectangle rect, Object value) {
|
||||||
|
if (textField != null)
|
||||||
|
editCancel();
|
||||||
|
|
||||||
|
this.treeTable = treeTable;
|
||||||
|
this.value = value;
|
||||||
|
|
||||||
|
this.textField = new JTextField();
|
||||||
|
treeTable.add(textField);
|
||||||
|
|
||||||
|
textField.setBounds(rect);
|
||||||
|
textField.requestFocusInWindow();
|
||||||
|
textField.repaint();
|
||||||
|
|
||||||
|
fireEditBegin();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void editCancel() {
|
||||||
|
if (textField != null)
|
||||||
|
{
|
||||||
|
destroyTextField();
|
||||||
|
|
||||||
|
fireEditCancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void editFinish() {
|
||||||
|
if (this.textField != null)
|
||||||
|
{
|
||||||
|
this.value = this.textField.getText();
|
||||||
|
destroyTextField();
|
||||||
|
|
||||||
|
fireEditFinish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
|
public class DefaultCellRenderer implements CellRenderer{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void renderCell(Graphics g,int row,int column, Rectangle cellRect, Object value,Object rowObject)
|
||||||
|
{
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
g.drawString( value.toString(),
|
||||||
|
cellRect.x + 2,
|
||||||
|
cellRect.y + cellRect.height - 4
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.hwo.ui;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.event.ListSelectionListener;
|
||||||
|
|
||||||
|
import org.hwo.models.FlexibleObjectTableModel;
|
||||||
|
|
||||||
|
public class FlexibleJTable<T> extends JTable {
|
||||||
|
private FlexibleObjectTableModel<T> model;
|
||||||
|
|
||||||
|
public FlexibleJTable(Class class_t)
|
||||||
|
{
|
||||||
|
this.model = new FlexibleObjectTableModel<T>(class_t);
|
||||||
|
this.setModel(this.model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FlexibleObjectTableModel<T> getModel()
|
||||||
|
{
|
||||||
|
return this.model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getSelectedObject()
|
||||||
|
{
|
||||||
|
if (this.getSelectedRow()==-1)
|
||||||
|
return null;
|
||||||
|
return this.model.getRows().get(this.getSelectedRow());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListSelectionListener(ListSelectionListener listener)
|
||||||
|
{
|
||||||
|
this.getSelectionModel().addListSelectionListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeListSelectionListener(ListSelectionListener listener)
|
||||||
|
{
|
||||||
|
this.getSelectionModel().removeListSelectionListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addColumn(String column)
|
||||||
|
{
|
||||||
|
this.addColumn(column, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addColumn(String column,boolean readonly)
|
||||||
|
{
|
||||||
|
this.model.addColumn(column,readonly);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(List<T> rows)
|
||||||
|
{
|
||||||
|
this.model.setRows(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRow(T o)
|
||||||
|
{
|
||||||
|
this.model.addRow(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRowCount()
|
||||||
|
{
|
||||||
|
return this.model.getRowCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue