Div. Updates
parent
c6f2f57919
commit
8dbb668a26
Binary file not shown.
Binary file not shown.
|
@ -25,5 +25,14 @@ public class StringHelper {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String fromCharacters(Character[] chars){
|
||||
char[] chs = new char[chars.length];
|
||||
|
||||
for (int i=0;i<chars.length;i++)
|
||||
chs[i] = chars[i].charValue();
|
||||
|
||||
return new String(chs);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import java.util.UUID;
|
|||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
public class Beacon extends Thread{
|
||||
public class Beacon {
|
||||
|
||||
public class BeaconSender
|
||||
{
|
||||
|
@ -28,6 +28,7 @@ public class Beacon extends Thread{
|
|||
|
||||
private Long lastBeacon;
|
||||
|
||||
|
||||
public BeaconSender()
|
||||
{
|
||||
properties = new Properties();
|
||||
|
@ -58,6 +59,10 @@ public class Beacon extends Thread{
|
|||
this.lastBeacon = lastBeacon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s", getProperties().getProperty("beacon.name"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -96,7 +101,11 @@ public class Beacon extends Thread{
|
|||
private Integer intervall;
|
||||
private String beaconName;
|
||||
private UUID uuid;
|
||||
private boolean serverOnly;
|
||||
private boolean clientOnly;
|
||||
|
||||
private Thread thread;
|
||||
|
||||
private Hashtable<String, BeaconSender> beaconSenders;
|
||||
|
||||
public Beacon(int port)
|
||||
|
@ -106,34 +115,87 @@ public class Beacon extends Thread{
|
|||
this.properties = new Properties();
|
||||
this.intervall = 5000;
|
||||
this.beaconSenders = new Hashtable<String, Beacon.BeaconSender>();
|
||||
this.beaconName = "DefaultBeacon";
|
||||
this.uuid = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
this.beaconName = InetAddress.getLocalHost().getHostName();
|
||||
} catch (UnknownHostException e) {
|
||||
this.beaconName = "Unkown Host";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setServerOnly(boolean serverOnly) {
|
||||
this.serverOnly = serverOnly;
|
||||
}
|
||||
public boolean isServerOnly() {
|
||||
return serverOnly;
|
||||
}
|
||||
public void setClientOnly(boolean clientOnly) {
|
||||
this.clientOnly = clientOnly;
|
||||
}
|
||||
public boolean isClientOnly() {
|
||||
return clientOnly;
|
||||
}
|
||||
|
||||
public boolean isAlive(){
|
||||
return this.thread != null;
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
if (thread == null) {
|
||||
thread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Beacon.this.run();
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void exit()
|
||||
{
|
||||
exit = true;
|
||||
if (thread != null){
|
||||
exit = true;
|
||||
socket.close();
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
thread = null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean getExit()
|
||||
public boolean getExit()
|
||||
{
|
||||
return exit;
|
||||
}
|
||||
public void setExit(Boolean exit){
|
||||
this.exit = exit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Timer pingTimer = new Timer();
|
||||
pingTimer.schedule(new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ping();
|
||||
}
|
||||
}, intervall, intervall);
|
||||
if (!clientOnly){
|
||||
pingTimer.schedule(new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ping();
|
||||
}
|
||||
}, intervall, intervall);
|
||||
}
|
||||
|
||||
try {
|
||||
socket = new DatagramSocket(this.port);
|
||||
if (serverOnly)
|
||||
socket = new DatagramSocket();
|
||||
else
|
||||
socket = new DatagramSocket(this.port);
|
||||
|
||||
DatagramPacket packet = new DatagramPacket(new byte[1500], 1500);
|
||||
|
||||
while (!getExit())
|
||||
|
@ -163,7 +225,6 @@ public class Beacon extends Thread{
|
|||
};
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,6 +236,24 @@ public class Beacon extends Thread{
|
|||
|
||||
}
|
||||
|
||||
public BeaconSender getSenderByUUID(UUID uuid){
|
||||
|
||||
for (BeaconSender sender: this.beaconSenders.values()){
|
||||
if (UUID.fromString(sender.getProperties().getProperty("beacon.uuid")).equals(uuid))
|
||||
return sender;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public BeaconSender getSenderByName(String beaconName){
|
||||
|
||||
for (BeaconSender sender: this.beaconSenders.values()){
|
||||
if (sender.getProperties().getProperty("beacon.name").equals(beaconName))
|
||||
return sender;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ping()
|
||||
{
|
||||
DatagramSocket sock = socket;
|
||||
|
|
|
@ -17,50 +17,117 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.hwo.StringHelper;
|
||||
import org.hwo.text.LineReader;
|
||||
|
||||
import javassist.bytecode.EnclosingMethodAttribute;
|
||||
|
||||
public class CSV {
|
||||
|
||||
List<String[]> records;
|
||||
List<CSVRecord> 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.records = new LinkedList<CSVRecord>();
|
||||
this.setSeparator(';');
|
||||
this.setEnclosedBy('\"');
|
||||
|
||||
this.parserRecord = new ArrayList<String>();
|
||||
this.parserField = new ArrayList<Character>();
|
||||
this.setEnclosedBy('\"');
|
||||
}
|
||||
|
||||
private void nextRecord()
|
||||
{
|
||||
nextField();
|
||||
|
||||
this.records.add(parserRecord.toArray(new String[]{}));
|
||||
parserRecord.clear();
|
||||
public void readFromFile(String filename)
|
||||
{
|
||||
try {
|
||||
readFromStream(new FileInputStream(filename));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void nextField()
|
||||
public void readFromStream(InputStream is) throws IOException
|
||||
{
|
||||
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();
|
||||
this.readFromStream(new InputStreamReader(is,"ISO-8859-1"));
|
||||
}
|
||||
|
||||
private void feeder(char ch)
|
||||
public void readFromStream(InputStreamReader isr) throws IOException
|
||||
{
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
|
||||
records.clear();
|
||||
|
||||
while (br.ready()) {
|
||||
CSVRecord row = textToRow(br.readLine());
|
||||
records.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
private Object textToObject(String text){
|
||||
|
||||
if (text.indexOf('.')>=0){
|
||||
try {
|
||||
Double d = Double.parseDouble(text);
|
||||
return d;
|
||||
} catch (Exception ex2){
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Integer i = Integer.parseInt(text);
|
||||
return i;
|
||||
} catch (Exception ex){
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
private CSVRecord textToRow(String line){
|
||||
CSVRecord record = new CSVRecord();
|
||||
LineReader reader = new LineReader(line);
|
||||
|
||||
ArrayList<Character> cell = null;
|
||||
boolean enclosed = false;
|
||||
|
||||
while (!reader.endOfLine()){
|
||||
char ch = reader.read();
|
||||
|
||||
if ((this.separator == ch) && (!enclosed) ){
|
||||
if (cell != null){
|
||||
String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0]));
|
||||
record.appendValue(textToObject(celltext));
|
||||
}
|
||||
cell = new ArrayList<Character>();
|
||||
|
||||
} else if (this.enclosedBy == ch){
|
||||
if (enclosed){
|
||||
if (reader.peek() == ch){
|
||||
cell.add(reader.read());
|
||||
} else {
|
||||
enclosed = false;
|
||||
String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0]));
|
||||
record.appendValue(celltext);
|
||||
cell = null;
|
||||
}
|
||||
} else {
|
||||
if (cell.size() == 0){
|
||||
enclosed = true;
|
||||
} else {
|
||||
cell.add(ch);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (cell == null){
|
||||
cell = new ArrayList<Character>();
|
||||
}
|
||||
cell.add(ch);
|
||||
}
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
/* private void feeder(char ch)
|
||||
{
|
||||
if (ch == getEnclosedBy())
|
||||
{
|
||||
|
@ -89,37 +156,8 @@ public class CSV {
|
|||
|
||||
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));
|
||||
|
@ -154,21 +192,21 @@ public class CSV {
|
|||
|
||||
try {
|
||||
|
||||
for (String[] record:this.records)
|
||||
for (CSVRecord record:this.records)
|
||||
{
|
||||
for (int i=0;i<record.length;i++)
|
||||
for (int i=0;i<record.size();i++)
|
||||
{
|
||||
if (i>0)
|
||||
writer.write(separator);
|
||||
|
||||
if (record[i] != null)
|
||||
if (record.getValue(i) != null)
|
||||
{
|
||||
if (String.class.isInstance(record[i]))
|
||||
if (String.class.isInstance(record.getValue(i)))
|
||||
writer.write(enclosedBy);
|
||||
|
||||
writer.write(record[i]);
|
||||
writer.write(record.getValue(i).toString());
|
||||
|
||||
if (String.class.isInstance(record[i]))
|
||||
if (String.class.isInstance(record.getValue(i)))
|
||||
writer.write(enclosedBy);
|
||||
}
|
||||
}
|
||||
|
@ -184,37 +222,30 @@ public class CSV {
|
|||
|
||||
}
|
||||
|
||||
public String getValue(int row,int column)
|
||||
public Object getValue(int row,int column)
|
||||
{
|
||||
try
|
||||
{
|
||||
return this.records.get(row)[column];
|
||||
return this.records.get(row).getValue(column).toString();
|
||||
} catch (ArrayIndexOutOfBoundsException ex)
|
||||
{
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<String[]> getCells()
|
||||
|
||||
|
||||
|
||||
public List<CSVRecord> getRecords()
|
||||
{
|
||||
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 CSVRecord getRecord(int row){
|
||||
return records.get(row);
|
||||
}
|
||||
|
||||
|
||||
public char getSeparator() {
|
||||
return separator;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package org.hwo.csv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CSVRecord {
|
||||
|
||||
ArrayList<Object> columns;
|
||||
|
||||
public CSVRecord(){
|
||||
columns = new ArrayList<Object>();
|
||||
}
|
||||
|
||||
public Object getValue(int column){
|
||||
return columns.get(column);
|
||||
}
|
||||
|
||||
public void setValue(int column,Object value){
|
||||
columns.set(column, value);
|
||||
}
|
||||
|
||||
public void appendValue(Object value){
|
||||
columns.add(value);
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return columns.size();
|
||||
}
|
||||
|
||||
public String getStringValue(int column){
|
||||
return getValue(column).toString();
|
||||
}
|
||||
|
||||
public Integer getIntegerValue(int column){
|
||||
Object v = getValue(column);
|
||||
if (Integer.class.isInstance(v)){
|
||||
return (Integer)v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Double getDoubleValue(int column){
|
||||
Object v = getValue(column);
|
||||
if (Integer.class.isInstance(v)){
|
||||
return (Double)v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.hwo.diagram;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Diagram {
|
||||
|
||||
private Double scale_x_min,
|
||||
scale_x_max,
|
||||
scale_y_min,
|
||||
scale_y_max;
|
||||
|
||||
|
||||
private LinkedList<Plot> plots;
|
||||
|
||||
|
||||
public Diagram(){
|
||||
plots = new LinkedList<Plot>();
|
||||
}
|
||||
|
||||
public Plot newPlot(){
|
||||
Plot p = new Plot(this);
|
||||
this.plots.add(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public BufferedImage plot(){
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.hwo.diagram;
|
||||
|
||||
public class Plot {
|
||||
|
||||
private Diagram diagram;
|
||||
|
||||
private String label;
|
||||
|
||||
protected Plot(Diagram diagram){
|
||||
this.diagram = diagram;
|
||||
}
|
||||
|
||||
public Diagram getDiagram() {
|
||||
return diagram;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -11,19 +11,32 @@ import java.util.concurrent.TimeoutException;
|
|||
import org.hwo.fifo.FiFo;
|
||||
import org.hwo.nativeloader.NativeLoader;
|
||||
|
||||
|
||||
/* NATIVE RETURN VALUES:
|
||||
*
|
||||
* 0 No error
|
||||
* -1 Unknown IO Error
|
||||
* -2 Timeout
|
||||
* -3 Interface could not be opened
|
||||
* -4 Parameter Error
|
||||
* -5 More than one character returned
|
||||
* -6 Port not opened
|
||||
* -99 Java <-> C Interface Error
|
||||
*
|
||||
*/
|
||||
|
||||
public class NativeSerialPort extends SerialPort {
|
||||
|
||||
class SerialPortInputStream extends InputStream
|
||||
{
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
public int read() throws SerialPortExeption {
|
||||
if (!isOpen())
|
||||
throw new IOException("Port nicht gešffnet!");
|
||||
throw new SerialPortExeption(-6);
|
||||
|
||||
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));
|
||||
throw new SerialPortExeption(ch);
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
@ -32,10 +45,9 @@ public class NativeSerialPort extends SerialPort {
|
|||
class SerialPortOutputStream extends OutputStream
|
||||
{
|
||||
@Override
|
||||
public void write(int arg0) throws IOException {
|
||||
public void write(int arg0) throws SerialPortExeption {
|
||||
if (!isOpen())
|
||||
throw new IOException("Port nicht gešffnet!");
|
||||
//System.err.println(String.format("TX: 0x%08x", arg0));
|
||||
throw new SerialPortExeption(-6);
|
||||
native_putch(nativeHandle, getTimeout(), arg0);
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +84,7 @@ public class NativeSerialPort extends SerialPort {
|
|||
return false;
|
||||
|
||||
nativeHandle = native_open(getPortName());
|
||||
System.err.println(String.format("NativeSerialPort: nativeHandle: %d",nativeHandle));
|
||||
System.err.println(String.format("NativeSerialPort: nativeHandle: %d for device %s",nativeHandle,getPortName()));
|
||||
if (nativeHandle < 0)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ public abstract class SerialPort {
|
|||
protected SerialPort()
|
||||
{
|
||||
this.portName = "";
|
||||
this.timeout = -1;
|
||||
this.timeout = 2500;
|
||||
}
|
||||
|
||||
abstract public boolean isOpen();
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package org.hwo.io;
|
||||
|
||||
import java.io.IOException;
|
||||
/*
|
||||
* -1 Unknown IO Error
|
||||
* -2 Timeout
|
||||
* -3 Interface could not be opened
|
||||
* -4 Parameter Error
|
||||
* -5 More than one character returned
|
||||
* -6 Port not opened
|
||||
*
|
||||
*/
|
||||
|
||||
public class SerialPortExeption extends IOException {
|
||||
|
||||
private int code;
|
||||
|
||||
public SerialPortExeption(){
|
||||
this.code = -99;
|
||||
}
|
||||
|
||||
public SerialPortExeption(int code){
|
||||
super(getMessage(code));
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
static protected String getMessage(int code){
|
||||
switch (code){
|
||||
case -1:
|
||||
return "Unknown IO Error";
|
||||
case -2:
|
||||
return "Timeout occured";
|
||||
case -3:
|
||||
return "Interface could not be opened";
|
||||
case -4:
|
||||
return "Parameter error";
|
||||
default:
|
||||
return "unknown";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("SerialPortException: [%d] %s",code,getMessage(code));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -19,6 +19,7 @@ import javax.swing.event.ListSelectionListener;
|
|||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import org.hwo.csv.CSV;
|
||||
import org.hwo.csv.CSVRecord;
|
||||
import org.hwo.interactiveobjects.InteractiveObjectHelper;
|
||||
import org.hwo.ui.KeyStrokeHelper;
|
||||
import org.hwo.ui.KeyStrokeListener;
|
||||
|
@ -637,23 +638,23 @@ public class TableMapper extends AbstractTableModel
|
|||
{
|
||||
CSV csv = new CSV();
|
||||
|
||||
List<String[]> cells = csv.getCells();
|
||||
List<CSVRecord> cells = csv.getRecords();
|
||||
|
||||
String[] header = new String[ getColumnCount() ];
|
||||
CSVRecord header = new CSVRecord();
|
||||
for (int i=0;i<getColumnCount();i++)
|
||||
header[i] = p_columns.get(i).getLabel();
|
||||
header.appendValue(p_columns.get(i).getLabel());
|
||||
|
||||
cells.add( header );
|
||||
cells.add(header);
|
||||
|
||||
for (int row=0;row<getRowCount();row++)
|
||||
{
|
||||
String[] record = new String[ getColumnCount() ];
|
||||
CSVRecord record = new CSVRecord();
|
||||
|
||||
for (int column=0;column<getColumnCount();column++)
|
||||
if (getValueAt(row, column)!=null)
|
||||
record[column] = getValueAt(row, column).toString();
|
||||
record.appendValue(getValueAt(row, column).toString());
|
||||
else
|
||||
record[column] = "";
|
||||
record.appendValue("");
|
||||
|
||||
cells.add(record);
|
||||
}
|
||||
|
|
|
@ -4,29 +4,26 @@ import java.io.File;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream.GetField;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.management.RuntimeErrorException;
|
||||
|
||||
import org.hwo.os.OsDetect;
|
||||
import org.hwo.os.OsDetect.Bitness;
|
||||
import org.hwo.os.OsDetect.OsType;
|
||||
|
||||
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();
|
||||
}
|
||||
private static boolean patchedLinux = false;
|
||||
|
||||
private static String calculateLibraryName(String libName){
|
||||
|
||||
String platformLibName;
|
||||
String resourcePath;
|
||||
|
||||
if (OsDetect.getBitness()==Bitness.B32){
|
||||
libName = libName + "32";
|
||||
}
|
||||
|
||||
switch (OsDetect.getOperatingSystem())
|
||||
{
|
||||
|
@ -40,8 +37,42 @@ public class NativeLoader {
|
|||
platformLibName = String.format("%s.dll",libName);
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Betriebssystem nicht unterstŸtzt!");
|
||||
throw new RuntimeException("Betriebssystem nicht unterst<EFBFBD>tzt!");
|
||||
}
|
||||
|
||||
return platformLibName;
|
||||
|
||||
}
|
||||
|
||||
public static void loadLibrary(String libName)
|
||||
{
|
||||
if (OsDetect.getOperatingSystem()==OsType.LINUX){
|
||||
System.err.println("Linux erkannt. Patche java.library.path");
|
||||
System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path")));
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
System.loadLibrary(libName);
|
||||
return;
|
||||
} catch (UnsatisfiedLinkError e)
|
||||
{
|
||||
System.err.println("Native Library " + libName + " failed to load.");
|
||||
}
|
||||
|
||||
String platformLibName = calculateLibraryName(libName);
|
||||
|
||||
try
|
||||
{
|
||||
System.loadLibrary("./"+platformLibName);
|
||||
return;
|
||||
} catch (UnsatisfiedLinkError e)
|
||||
{
|
||||
System.err.println("Native Library " + platformLibName + " failed to load.");
|
||||
}
|
||||
|
||||
String resourcePath;
|
||||
|
||||
System.err.println(String.format("NativeLoader: Platform dependent Name: %s",platformLibName));
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.hwo.os;
|
|||
public class OsDetect {
|
||||
|
||||
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
||||
public enum Bitness { B32, B64 };
|
||||
|
||||
public static OsType getOperatingSystem()
|
||||
{
|
||||
|
@ -16,4 +17,21 @@ public class OsDetect {
|
|||
return OsType.UNKNOWN;
|
||||
}
|
||||
|
||||
public static Bitness getBitness(){
|
||||
String arch = System.getProperty("os.arch");
|
||||
System.err.println("Arch: " + arch);
|
||||
if (arch == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (arch.equals("x86")){
|
||||
return Bitness.B32;
|
||||
} else if (arch.equals("i386")){
|
||||
return Bitness.B32;
|
||||
} else if (arch.equals("xmd64")){
|
||||
return Bitness.B64;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package org.hwo.rpc.json;
|
||||
|
||||
|
||||
public class JSONRpcServer {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
package org.hwo.rpc.json;
|
||||
|
||||
import java.awt.peer.TextComponentPeer;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
|
@ -9,6 +13,8 @@ import java.lang.reflect.InvocationHandler;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
|
@ -21,6 +27,9 @@ import org.json.simple.JSONValue;
|
|||
public class JSONRpcService implements InvocationHandler{
|
||||
|
||||
private String url;
|
||||
private InetAddress inetAddress;
|
||||
private int port;
|
||||
|
||||
private Integer nextid;
|
||||
|
||||
public JSONRpcService(String url){
|
||||
|
@ -28,10 +37,68 @@ public class JSONRpcService implements InvocationHandler{
|
|||
this.nextid = 0;
|
||||
}
|
||||
|
||||
public JSONRpcService(InetAddress inetAddress,int port){
|
||||
this.inetAddress = inetAddress;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public <T> T createProxy(Class<T> iface){
|
||||
T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, this);
|
||||
return proxy;
|
||||
}
|
||||
|
||||
private byte[] request(byte[] request) throws Throwable{
|
||||
if (url != null)
|
||||
return requestURL(request);
|
||||
if (inetAddress != null)
|
||||
return requestTCP(request);
|
||||
return new byte[0];
|
||||
}
|
||||
private byte[] requestURL(byte[] request) throws Throwable{
|
||||
URL url = new URL(this.url);
|
||||
URLConnection conn = url.openConnection();
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
OutputStream out = conn.getOutputStream();
|
||||
out.write(request);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine())!=null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
return sb.toString().getBytes();
|
||||
}
|
||||
private byte[] requestTCP(byte[] request) {
|
||||
|
||||
try {
|
||||
Socket socket = new Socket(inetAddress,port);
|
||||
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
dos.writeInt(request.length);
|
||||
dos.write(request);
|
||||
|
||||
int len = dis.readInt();
|
||||
byte[] response = new byte[len];
|
||||
dis.read(response,0,len);
|
||||
|
||||
dis.close();
|
||||
dos.close();
|
||||
socket.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
|
@ -43,29 +110,13 @@ public class JSONRpcService implements InvocationHandler{
|
|||
req.put("params", Arrays.asList(args));
|
||||
|
||||
String reqString = JSONValue.toJSONString(req);
|
||||
|
||||
System.err.println(reqString);
|
||||
|
||||
URL url = new URL(this.url);
|
||||
URLConnection conn = url.openConnection();
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
OutputStream out = conn.getOutputStream();
|
||||
out.write(reqString.getBytes());
|
||||
out.flush();
|
||||
out.close();
|
||||
byte[] responseBytes = request( reqString.getBytes() );
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
System.err.println("Response: " + new String(responseBytes));
|
||||
|
||||
while ((line = reader.readLine())!=null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
System.err.println("Response: " + sb.toString());
|
||||
|
||||
Map<String, Object> response = (Map<String, Object>)JSONValue.parse(sb.toString());
|
||||
Map<String, Object> response = (Map<String, Object>)JSONValue.parse(new String(responseBytes));
|
||||
|
||||
if (response.containsKey("error") && (response.get("error") != null)) {
|
||||
throw new RuntimeException("JSON-RPC: " + response.get("error"));
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package org.hwo.rpc.json;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public abstract class RPCAdapter {
|
||||
/*
|
||||
* InetAddress listenAddress;
|
||||
int listenPort;
|
||||
|
||||
boolean exit;
|
||||
|
||||
public JSONRpcServer(InetAddress listen,int port){
|
||||
this.listenAddress = listen;
|
||||
this.listenPort = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
ServerSocket serverSocket = new ServerSocket(listenPort, 5, listenAddress);
|
||||
|
||||
while (!exit){
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
int len = dis.readInt();
|
||||
if (len < 10240){
|
||||
byte[] requestBytes = new byte[ len ];
|
||||
dis.read(requestBytes,0,len);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void exit()
|
||||
{
|
||||
this.exit = true;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package org.hwo.rpc.simple;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SimpleRPCRequest implements Serializable{
|
||||
|
||||
private String interfaceName;
|
||||
private String methodName;
|
||||
private Object[] params;
|
||||
|
||||
public SimpleRPCRequest(String interfaceName,String methodName,Object[] params){
|
||||
this.interfaceName = interfaceName;
|
||||
this.methodName = methodName;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public String getInterfaceName() {
|
||||
return interfaceName;
|
||||
}
|
||||
public String getMethodName() {
|
||||
return methodName;
|
||||
}
|
||||
public Object[] getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package org.hwo.rpc.simple;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SimpleRPCResponse implements Serializable{
|
||||
|
||||
|
||||
private Exception exception;
|
||||
private Object result;
|
||||
|
||||
public SimpleRPCResponse(Object result,Exception exception){
|
||||
this.exception = exception;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
}
|
||||
public Object getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
package org.hwo.rpc.simple;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class SimpleRPCServer extends Thread{
|
||||
|
||||
private Hashtable<String, Object> registeredObjects;
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
private boolean exit;
|
||||
|
||||
public SimpleRPCServer(InetAddress listenAddress,int port) throws IOException
|
||||
{
|
||||
this.registeredObjects = new Hashtable<String, Object>();
|
||||
this.serverSocket = new ServerSocket(port, 5, listenAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!exit){
|
||||
try {
|
||||
handleOneRequest();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
exit = false;
|
||||
}
|
||||
|
||||
public void exit(){
|
||||
this.exit = true;
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void handleOneRequest() throws IOException{
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
int len = dis.readInt();
|
||||
byte[] request = new byte[ len ];
|
||||
dis.read(request);
|
||||
|
||||
byte[] response = handleRequest(request);
|
||||
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
dos.writeInt(response.length);
|
||||
dos.write(response);
|
||||
|
||||
dos.flush();
|
||||
dos.close();
|
||||
dis.close();
|
||||
}
|
||||
|
||||
|
||||
public byte[] handleRequest(byte[] request){
|
||||
try {
|
||||
ByteArrayInputStream i = new ByteArrayInputStream(request);
|
||||
ObjectInputStream ois = new ObjectInputStream(i);
|
||||
|
||||
SimpleRPCRequest simpleRequest = (SimpleRPCRequest)ois.readObject();
|
||||
Exception exception = null;
|
||||
Object result = null;
|
||||
|
||||
if (registeredObjects.containsKey(simpleRequest.getInterfaceName())){
|
||||
try {
|
||||
result = this.invoke(registeredObjects.get(simpleRequest.getInterfaceName()), simpleRequest.getMethodName(), simpleRequest.getParams());
|
||||
} catch (Exception e){
|
||||
exception = e;
|
||||
result = null;
|
||||
}
|
||||
} else {
|
||||
exception = new ClassNotFoundException(String.format("RPC-Interface %s is unknown.",simpleRequest.getInterfaceName()));
|
||||
}
|
||||
|
||||
SimpleRPCResponse simpleResponse = new SimpleRPCResponse(result, exception);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
|
||||
oos.writeObject(simpleResponse);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
bos.flush();
|
||||
bos.close();
|
||||
|
||||
return bos.toByteArray();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
private Object invoke(Object obj,String methodName,Object[] params) throws Exception {
|
||||
|
||||
Class<?> clazz = obj.getClass();
|
||||
Class<?>[] paramTypes;
|
||||
|
||||
if (params != null) {
|
||||
paramTypes = new Class<?>[ params.length ];
|
||||
|
||||
for (int i=0;i<params.length; i++){
|
||||
paramTypes[i] = params[i].getClass();
|
||||
}
|
||||
|
||||
} else {
|
||||
paramTypes = new Class<?>[0];
|
||||
}
|
||||
|
||||
Method method = clazz.getDeclaredMethod(methodName, paramTypes);
|
||||
|
||||
Object result = method.invoke(obj, params);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void registerObject(Class<?> interfaceClass,Object obj){
|
||||
this.registerObject(interfaceClass.getCanonicalName(), obj);
|
||||
}
|
||||
public void registerObject(String name,Object obj){
|
||||
this.registeredObjects.put(name, obj);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package org.hwo.rpc.simple;
|
||||
|
||||
import java.awt.peer.TextComponentPeer;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
public class SimpleRPCService {
|
||||
|
||||
private class InvocationHelper implements InvocationHandler {
|
||||
|
||||
private String interfaceName;
|
||||
|
||||
public InvocationHelper(String interfaceName){
|
||||
this.interfaceName = interfaceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
SimpleRPCRequest simpleRequest = new SimpleRPCRequest(interfaceName, method.getName(), args);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
|
||||
oos.writeObject(simpleRequest);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
bos.close();
|
||||
|
||||
byte[] request = bos.toByteArray();
|
||||
|
||||
Socket socket = new Socket(inetAddress,port);
|
||||
socket.setSoTimeout(soTimeout);
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
dos.writeInt(request.length);
|
||||
dos.write(request);
|
||||
dos.flush();
|
||||
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
int len = dis.readInt();
|
||||
byte[] response = new byte[ len ];
|
||||
dis.read(response);
|
||||
dis.close();
|
||||
dos.close();
|
||||
socket.close();
|
||||
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(response);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||
|
||||
SimpleRPCResponse simpleResponse = (SimpleRPCResponse)ois.readObject();
|
||||
|
||||
if (simpleResponse.getException() != null)
|
||||
throw simpleResponse.getException();
|
||||
|
||||
return simpleResponse.getResult();
|
||||
}
|
||||
}
|
||||
|
||||
private InetAddress inetAddress;
|
||||
private int port;
|
||||
private int soTimeout;
|
||||
|
||||
public SimpleRPCService(InetAddress inetAddress,int port){
|
||||
this.inetAddress = inetAddress;
|
||||
this.port = port;
|
||||
this.soTimeout = 30000;
|
||||
}
|
||||
|
||||
public int getSoTimeout() {
|
||||
return soTimeout;
|
||||
}
|
||||
public void setSoTimeout(int soTimeout) {
|
||||
this.soTimeout = soTimeout;
|
||||
}
|
||||
|
||||
public <T> T createProxy(Class<T> iface){
|
||||
T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, new InvocationHelper(iface.getCanonicalName()));
|
||||
System.err.println("SimpleRPCService: Created Proxy for Interface: " + iface.getCanonicalName() + " at IP: " + inetAddress.toString());
|
||||
return proxy;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.hwo.rpc.simple;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class SimpleRPCTest {
|
||||
|
||||
interface TestInterface {
|
||||
|
||||
public int add(Integer a,Integer b);
|
||||
|
||||
}
|
||||
|
||||
static class TestClass implements TestInterface{
|
||||
|
||||
public TestClass(){
|
||||
}
|
||||
|
||||
@Override
|
||||
public int add(Integer a, Integer b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
SimpleRPCServer server = new SimpleRPCServer(InetAddress.getLocalHost(),44352);
|
||||
SimpleRPCService service = new SimpleRPCService(InetAddress.getLocalHost(), 44352);
|
||||
|
||||
TestClass tc = new TestClass();
|
||||
server.registerObject(TestInterface.class, tc);
|
||||
|
||||
server.start();
|
||||
|
||||
TestInterface ti = service.createProxy(TestInterface.class);
|
||||
|
||||
System.err.println("ti.add(): " + ti.add(4, 5));
|
||||
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package org.hwo.text;
|
||||
|
||||
|
||||
public class LineReader {
|
||||
|
||||
private char[] buffer;
|
||||
private int position;
|
||||
|
||||
|
||||
public LineReader()
|
||||
{
|
||||
position = 0;
|
||||
}
|
||||
|
||||
public LineReader(String line)
|
||||
{
|
||||
position = 0;
|
||||
setText(line);
|
||||
}
|
||||
|
||||
public void setText(String text){
|
||||
position = 0;
|
||||
buffer = (text == null) ? new char[0] : text.toCharArray();
|
||||
}
|
||||
public String getText(){
|
||||
return new String(buffer);
|
||||
}
|
||||
|
||||
public char peek(){
|
||||
return buffer[position];
|
||||
}
|
||||
public char peek(int offset){
|
||||
return buffer[position + offset];
|
||||
}
|
||||
|
||||
public char read(){
|
||||
return buffer[position++];
|
||||
}
|
||||
|
||||
public void seek(int pos){
|
||||
position = pos;
|
||||
}
|
||||
|
||||
public boolean endOfLine(){
|
||||
return (position >= buffer.length);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
package org.hwo.ui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.FlowLayout;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
|
||||
import javax.swing.JList;
|
||||
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.Dialog.ModalExclusionType;
|
||||
import java.awt.Dialog.ModalityType;
|
||||
|
||||
public class JObjectSelector extends JDialog {
|
||||
|
||||
public static Object execute(Object[] items){
|
||||
|
||||
JObjectSelector os = new JObjectSelector();
|
||||
os.setItems(items);
|
||||
os.setVisible(true);
|
||||
return os.selectedItem;
|
||||
}
|
||||
|
||||
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
private JList lItems;
|
||||
|
||||
private Object[] items;
|
||||
private Object selectedItem;
|
||||
private JLabel lText;
|
||||
|
||||
/**
|
||||
* Create the dialog.
|
||||
*/
|
||||
public JObjectSelector() {
|
||||
setModalityType(ModalityType.APPLICATION_MODAL);
|
||||
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
|
||||
setTitle("Auswahl\u2026");
|
||||
setBounds(100, 100, 450, 300);
|
||||
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};
|
||||
gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
|
||||
gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
||||
gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
||||
contentPanel.setLayout(gbl_contentPanel);
|
||||
{
|
||||
lText = new JLabel("-");
|
||||
GridBagConstraints gbc_lText = new GridBagConstraints();
|
||||
gbc_lText.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_lText.gridx = 0;
|
||||
gbc_lText.gridy = 0;
|
||||
contentPanel.add(lText, gbc_lText);
|
||||
}
|
||||
{
|
||||
lItems = new JList();
|
||||
GridBagConstraints gbc_lItems = new GridBagConstraints();
|
||||
gbc_lItems.fill = GridBagConstraints.BOTH;
|
||||
gbc_lItems.gridx = 0;
|
||||
gbc_lItems.gridy = 1;
|
||||
contentPanel.add(lItems, gbc_lItems);
|
||||
}
|
||||
{
|
||||
JPanel buttonPane = new JPanel();
|
||||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
||||
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
||||
{
|
||||
JButton okButton = new JButton("OK");
|
||||
okButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
accept();
|
||||
}
|
||||
});
|
||||
okButton.setActionCommand("OK");
|
||||
buttonPane.add(okButton);
|
||||
getRootPane().setDefaultButton(okButton);
|
||||
}
|
||||
{
|
||||
JButton cancelButton = new JButton("Cancel");
|
||||
cancelButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
cancel();
|
||||
}
|
||||
});
|
||||
cancelButton.setActionCommand("Cancel");
|
||||
buttonPane.add(cancelButton);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void accept()
|
||||
{
|
||||
selectedItem = lItems.getSelectedValue();
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
public void cancel(){
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
public void setItems(Object[] items) {
|
||||
this.items = items;
|
||||
DefaultListModel dlm = new DefaultListModel();
|
||||
for (Object o:items){
|
||||
dlm.addElement(o);
|
||||
}
|
||||
lItems.setModel(dlm);
|
||||
}
|
||||
public Object[] getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setText(String text){
|
||||
this.lText.setText(text);
|
||||
}
|
||||
public String getText(){
|
||||
return this.lText.getText();
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in New Issue