Removing lua dependency from java code

git-svn-id: https://tesseract-ocr.googlecode.com/svn/trunk@243 d0cd1f9f-072b-0410-8dd7-cf729c803f20
This commit is contained in:
theraysmith 2009-06-02 23:37:06 +00:00
parent 254b936aef
commit c82051a461
8 changed files with 458 additions and 127 deletions

View File

@ -1,7 +1,9 @@
SUBDIRS =
EXTRA_DIST = \
test.lua makefile SVEvent.java SVEventHandler.java \
test.lua makefile SVAbstractMenuItem.java \
SVCheckboxMenuItem.java SVEmptyMenuItem.java \
SVEvent.java SVEventHandler.java \
SVEventType.java SVImageHandler.java SVMenuBar.java \
SVMenuItem.java SVPopupMenu.java SVWindow.java \
SVMenuItem.java SVPopupMenu.java SVSubMenuItem.java SVWindow.java \
ScrollView.java

View File

@ -0,0 +1,58 @@
// Copyright 2007 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
package com.google.scrollview.ui;
/**
* A MenuListItem is any sort of menu entry. This can either be within a popup
* menu or within a menubar. It can either be a submenu (only name and
* command-id) or a name with an associated value and possibly description. They
* can also have new entries added (if they are submenus).
*
* @author wanke@google.com
*/
import com.google.scrollview.events.SVEventType;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
abstract class SVAbstractMenuItem {
JMenuItem mi;
public String name;
public int id;
/**
* Sets the basic attributes for name, id and the corresponding swing item
*/
SVAbstractMenuItem(int id, String name, JMenuItem jmi) {
this.mi = jmi;
this.name = name;
this.id = id;
}
/** Returns the actual value of the MenuListItem. */
public String getValue() { return null; }
/** Adds a child entry to the submenu. */
public void add(SVAbstractMenuItem mli) { }
/** Adds a child menu to the submenu (or root node). */
public void add(JMenu jli) { }
/**
* What to do when user clicks on this item.
* @param window The window the event happened.
* @param eventType What kind of event will be associated
* (usually SVET_POPUP or SVET_MENU).
*/
public void performAction(SVWindow window, SVEventType eventType) {}
}

View File

@ -0,0 +1,60 @@
// Copyright 2007 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
package com.google.scrollview.ui;
/**
* A MenuListItem is any sort of menu entry. This can either be within a popup
* menu or within a menubar. It can either be a submenu (only name and
* command-id) or a name with an associated value and possibly description. They
* can also have new entries added (if they are submenus).
*
* @author wanke@google.com
*/
import com.google.scrollview.ScrollView;
import com.google.scrollview.events.SVEvent;
import com.google.scrollview.events.SVEventType;
import javax.swing.JCheckBoxMenuItem;
/**
* Constructs a new menulistitem which possesses a flag that can be toggled.
*/
class SVCheckboxMenuItem extends SVAbstractMenuItem {
public String value = null;
public String desc = null;
public boolean bvalue;
SVCheckboxMenuItem(int id, String name, boolean val) {
super(id, name, new JCheckBoxMenuItem(name, val));
bvalue = val;
}
/** What to do when user clicks on this item. */
@Override
public void performAction(SVWindow window, SVEventType eventType) {
// Checkbox entry - trigger and send event.
if (bvalue) {
bvalue = false;
} else {
bvalue = true;
}
SVEvent svme = new SVEvent(eventType, window, id, getValue());
ScrollView.addMessage(svme);
}
/** Returns the actual value of the MenuListItem. */
@Override
public String getValue() {
return Boolean.toString(bvalue);
}
}

48
java/SVEmptyMenuItem.java Normal file
View File

@ -0,0 +1,48 @@
// Copyright 2007 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
package com.google.scrollview.ui;
/**
* A MenuListItem is any sort of menu entry. This can either be within a popup
* menu or within a menubar. It can either be a submenu (only name and
* command-id) or a name with an associated value and possibly description. They
* can also have new entries added (if they are submenus).
*
* @author wanke@google.com
*/
import com.google.scrollview.ScrollView;
import com.google.scrollview.events.SVEvent;
import com.google.scrollview.events.SVEventType;
import javax.swing.JMenuItem;
/**
* Constructs a new menulistitem which just has an ID and a name attached to
* it. In this case, we will have to ask for the value of the item and its
* description if it gets called.
*/
class SVEmptyMenuItem extends SVAbstractMenuItem {
SVEmptyMenuItem(int id, String name) {
super(id, name, new JMenuItem(name));
}
/** What to do when user clicks on this item. */
@Override
public void performAction(SVWindow window, SVEventType eventType) {
// Send an event indicating that someone clicked on an entry.
// Value will be null here.
SVEvent svme =
new SVEvent(eventType, window, id, getValue());
ScrollView.addMessage(svme);
}
}

View File

@ -1,5 +1,5 @@
// Copyright 2007 Google Inc. All Rights Reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
@ -15,86 +15,14 @@ package com.google.scrollview.ui;
* menu or within a menubar. It can either be a submenu (only name and
* command-id) or a name with an associated value and possibly description. They
* can also have new entries added (if they are submenus).
*
*
* @author wanke@google.com
*/
import com.google.scrollview.ScrollView;
import com.google.scrollview.events.SVEvent;
import com.google.scrollview.events.SVEventType;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
abstract class SVAbstractMenuItem {
JMenuItem mi;
public String name;
public int id;
/**
* Sets the basic attributes for name, id and the corresponding swing item
*/
SVAbstractMenuItem(int id, String name, JMenuItem jmi) {
this.mi = jmi;
this.name = name;
this.id = id;
}
/** Returns the actual value of the MenuListItem. */
public String getValue() { return null; }
/** Adds a child entry to the submenu. */
public void add(SVAbstractMenuItem mli) { }
/** Adds a child menu to the submenu (or root node). */
public void add(JMenu jli) { }
/**
* What to do when user clicks on this item.
* @param window The window the event happened.
* @param eventType What kind of event will be associated (usually SVET_POPUP or SVET_MENU).
*/
public void performAction(SVWindow window, SVEventType eventType) {}
}
/** Constructs a new submenu which can hold other entries. */
class SVSubMenuItem extends SVAbstractMenuItem {
public SVSubMenuItem(String name, JMenu jli) {
super(-1, name, jli);
}
/** Adds a child entry to the submenu. */
@Override
public void add(SVAbstractMenuItem mli) {
mi.add(mli.mi);
}
/** Adds a child menu to the submenu (or root node). */
@Override
public void add(JMenu jli) {
mi.add(jli);
}
}
/**
* Constructs a new menulistitem which just has an ID and a name attached to
* it. In this case, we will have to ask for the value of the item and its
* description if it gets called.
*/
class SVEmptyMenuItem extends SVAbstractMenuItem {
SVEmptyMenuItem(int id, String name) {
super(id, name, new JMenuItem(name));
}
/** What to do when user clicks on this item. */
@Override
public void performAction(SVWindow window, SVEventType eventType) {
// Send an event indicating that someone clicked on an entry.
// Value will be null here.
SVEvent svme =
new SVEvent(eventType, window, id, getValue());
ScrollView.addMessage(svme);
}
}
/**
* Constructs a new menulistitem which also has a value and a description. For
* these, we will not have to ask the server what the value is when the user
@ -103,14 +31,14 @@ class SVEmptyMenuItem extends SVAbstractMenuItem {
class SVMenuItem extends SVAbstractMenuItem {
public String value = null;
public String desc = null;
SVMenuItem(int id, String name, String v, String d) {
super(id, name, new JMenuItem(name));
value = v;
desc = d;
}
/**
/**
* Ask the user for new input for a variable and send it.
* Depending on whether there is a description given for the entry, show
* the description in the dialog or just show the name.
@ -123,7 +51,7 @@ class SVMenuItem extends SVAbstractMenuItem {
window.showInputDialog(name, value, id, eventType);
}
}
/** Returns the actual value of the MenuListItem. */
@Override
public String getValue() {
@ -131,36 +59,3 @@ class SVMenuItem extends SVAbstractMenuItem {
}
}
/**
* Constructs a new menulistitem which possesses a flag that can be toggled.
*/
class SVCheckboxMenuItem extends SVAbstractMenuItem {
public String value = null;
public String desc = null;
public boolean bvalue;
SVCheckboxMenuItem(int id, String name, boolean val) {
super(id, name, new JCheckBoxMenuItem(name, val));
bvalue = val;
}
/** What to do when user clicks on this item. */
@Override
public void performAction(SVWindow window, SVEventType eventType) {
// Checkbox entry - trigger and send event.
if (bvalue) {
bvalue = false;
} else {
bvalue = true;
}
SVEvent svme = new SVEvent(eventType, window, id, getValue());
ScrollView.addMessage(svme);
}
/** Returns the actual value of the MenuListItem. */
@Override
public String getValue() {
return Boolean.toString(bvalue);
}
}

41
java/SVSubMenuItem.java Normal file
View File

@ -0,0 +1,41 @@
// Copyright 2007 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
package com.google.scrollview.ui;
/**
* A MenuListItem is any sort of menu entry. This can either be within a popup
* menu or within a menubar. It can either be a submenu (only name and
* command-id) or a name with an associated value and possibly description. They
* can also have new entries added (if they are submenus).
*
* @author wanke@google.com
*/
import javax.swing.JMenu;
/** Constructs a new submenu which can hold other entries. */
class SVSubMenuItem extends SVAbstractMenuItem {
public SVSubMenuItem(String name, JMenu jli) {
super(-1, name, jli);
}
/** Adds a child entry to the submenu. */
@Override
public void add(SVAbstractMenuItem mli) {
mi.add(mli.mi);
}
/** Adds a child menu to the submenu (or root node). */
@Override
public void add(JMenu jli) {
mi.add(jli);
}
}

View File

@ -12,9 +12,7 @@ package com.google.scrollview;
import com.google.scrollview.events.SVEvent;
import com.google.scrollview.ui.SVImageHandler;
import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;
import com.google.scrollview.ui.SVWindow;
import java.io.BufferedReader;
import java.io.IOException;
@ -22,6 +20,8 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
@ -46,13 +46,13 @@ public class ScrollView {
public static float polylineYCoords[]; // The coords being received.
public static int polylineSize; // The size of the coords arrays.
public static int polylineScanned; // The size read so far.
private static ArrayList<SVWindow> windows; // The id to SVWindow map.
private static Pattern intPattern; // For checking integer arguments.
private static Pattern floatPattern; // For checking float arguments.
/** Keeps track of the number of messages received. */
static int nrInputLines = 0;
/** Binding to LUA */
private static LuaState L;
/** Prints all received messages to the console if true. */
static boolean debugViewNetworkTraffic = false;
@ -147,13 +147,232 @@ public class ScrollView {
System.exit(0);
}
// Parse a comma-separated list of arguments into ArrayLists of the
// possible types. Each type is stored in order, but the order
// distinction between types is lost.
// Note that the format is highly constrained to what the client used
// to send to LUA:
// Quoted string -> String.
// true or false -> Boolean.
// %f format number -> Float (no %e allowed)
// Sequence of digits -> Integer
// Nothing else allowed.
private static void parseArguments(String argList,
ArrayList<Integer> intList,
ArrayList<Float> floatList,
ArrayList<String> stringList,
ArrayList<Boolean> boolList) {
// str is only non-null if an argument starts with a single or double
// quote. str is set back to null on completion of the string with a
// matching quote. If the string contains a comma then str will stay
// non-null across multiple argStr values until a matching closing quote.
// Backslash escaped quotes do not count as terminating the string.
String str = null;
for (String argStr : argList.split(",")) {
if (str != null) {
// Last string was incomplete. Append argStr to it and restore comma.
// Execute str += "," + argStr in Java.
int length = str.length() + 1 + argStr.length();
StringBuilder appended = new StringBuilder(length);
appended.append(str);
appended.append(",");
appended.append(argStr);
str = appended.toString();
} else if (argStr.length() == 0) {
continue;
} else {
char quote = argStr.charAt(0);
// If it begins with a quote then it is a string, but may not
// end this time if it contained a comma.
if (quote == '\'' || quote == '"') {
str = argStr;
}
}
if (str != null) {
// It began with a quote. Check that it still does.
assert str.charAt(0) == '\'' || str.charAt(0) == '"';
int len = str.length();
if (len > 1 && str.charAt(len - 1) == str.charAt(0)) {
// We have an ending quote of the right type. Now check that
// it is not escaped. Must have an even number of slashes before.
int slash = len - 1;
while (slash > 0 && str.charAt(slash - 1) == '\\')
--slash;
if ((len - 1 - slash) % 2 == 0) {
// It is now complete. Chop off the quotes and save.
// TODO(rays) remove the first backslash of each pair.
stringList.add(str.substring(1, len - 1));
str = null;
}
}
// If str is not null here, then we have a string with a comma in it.
// Append , and the next argument at the next iteration, but check
// that str is null after the loop terminates in case it was an
// unterminated string.
} else if (floatPattern.matcher(argStr).matches()) {
// It is a float.
floatList.add(Float.parseFloat(argStr));
} else if (argStr.equals("true")) {
boolList.add(true);
} else if (argStr.equals("false")) {
boolList.add(false);
} else if (intPattern.matcher(argStr).matches()) {
// Only contains digits so must be an int.
intList.add(Integer.parseInt(argStr));
}
// else ignore all incompatible arguments for forward compatibility.
}
// All strings must have been terminated.
assert str == null;
}
/** Executes the LUA command parsed as parameter. */
private static void processInput(String inputLine) {
int err = L.LdoString(inputLine);
if (err == 1) {
System.out
.println("LUA Error in:" + inputLine + "(" + nrInputLines + ")");
// Execute a function encoded as a LUA statement! Yuk!
if (inputLine.charAt(0) == 'w') {
// This is a method call on a window. Parse it.
String noWLine = inputLine.substring(1);
String[] idStrs = noWLine.split("[ :]", 2);
int windowID = Integer.parseInt(idStrs[0]);
// Find the parentheses.
int start = inputLine.indexOf('(');
int end = inputLine.lastIndexOf(')');
// Parse the args.
ArrayList<Integer> intList = new ArrayList<Integer>(4);
ArrayList<Float> floatList = new ArrayList<Float>(2);
ArrayList<String> stringList = new ArrayList<String>(4);
ArrayList<Boolean> boolList = new ArrayList<Boolean>(3);
parseArguments(inputLine.substring(start + 1, end),
intList, floatList, stringList, boolList);
int colon = inputLine.indexOf(':');
if (colon > 1 && colon < start) {
// This is a regular function call. Look for the name and call it.
String func = inputLine.substring(colon + 1, start);
if (func.equals("drawLine")) {
windows.get(windowID).drawLine(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else if (func.equals("createPolyline")) {
windows.get(windowID).createPolyline(intList.get(0));
} else if (func.equals("drawPolyline")) {
windows.get(windowID).drawPolyline();
} else if (func.equals("drawRectangle")) {
windows.get(windowID).drawRectangle(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else if (func.equals("setVisible")) {
windows.get(windowID).setVisible(boolList.get(0));
} else if (func.equals("setAlwaysOnTop")) {
windows.get(windowID).setAlwaysOnTop(boolList.get(0));
} else if (func.equals("addMessage")) {
windows.get(windowID).addMessage(stringList.get(0));
} else if (func.equals("addMessageBox")) {
windows.get(windowID).addMessageBox();
} else if (func.equals("clear")) {
windows.get(windowID).clear();
} else if (func.equals("setStrokeWidth")) {
windows.get(windowID).setStrokeWidth(floatList.get(0));
} else if (func.equals("drawEllipse")) {
windows.get(windowID).drawEllipse(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else if (func.equals("pen")) {
if (intList.size() == 4) {
windows.get(windowID).pen(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else {
windows.get(windowID).pen(intList.get(0), intList.get(1),
intList.get(2));
}
} else if (func.equals("brush")) {
if (intList.size() == 4) {
windows.get(windowID).brush(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else {
windows.get(windowID).brush(intList.get(0), intList.get(1),
intList.get(2));
}
} else if (func.equals("textAttributes")) {
windows.get(windowID).textAttributes(stringList.get(0),
intList.get(0),
boolList.get(0),
boolList.get(1),
boolList.get(2));
} else if (func.equals("drawText")) {
windows.get(windowID).drawText(intList.get(0), intList.get(1),
stringList.get(0));
} else if (func.equals("openImage")) {
windows.get(windowID).openImage(stringList.get(0));
} else if (func.equals("drawImage")) {
windows.get(windowID).drawImage(stringList.get(0),
intList.get(0), intList.get(1));
} else if (func.equals("addMenuBarItem")) {
if (boolList.size() > 0) {
windows.get(windowID).addMenuBarItem(stringList.get(0),
stringList.get(1),
intList.get(0),
boolList.get(0));
} else if (intList.size() > 0) {
windows.get(windowID).addMenuBarItem(stringList.get(0),
stringList.get(1),
intList.get(0));
} else {
windows.get(windowID).addMenuBarItem(stringList.get(0),
stringList.get(1));
}
} else if (func.equals("addPopupMenuItem")) {
if (stringList.size() == 4) {
windows.get(windowID).addPopupMenuItem(stringList.get(0),
stringList.get(1),
intList.get(0),
stringList.get(2),
stringList.get(3));
} else {
windows.get(windowID).addPopupMenuItem(stringList.get(0),
stringList.get(1));
}
} else if (func.equals("update")) {
windows.get(windowID).update();
} else if (func.equals("showInputDialog")) {
windows.get(windowID).showInputDialog(stringList.get(0));
} else if (func.equals("showYesNoDialog")) {
windows.get(windowID).showYesNoDialog(stringList.get(0));
} else if (func.equals("zoomRectangle")) {
windows.get(windowID).zoomRectangle(intList.get(0), intList.get(1),
intList.get(2), intList.get(3));
} else if (func.equals("createImage")) {
windows.get(windowID).createImage(stringList.get(0), intList.get(0),
intList.get(1), intList.get(2));
} else if (func.equals("drawImage")) {
windows.get(windowID).drawImage(stringList.get(0),
intList.get(0), intList.get(1));
} else if (func.equals("destroy")) {
windows.get(windowID).destroy();
}
// else for forward compatibility purposes, silently ignore any
// unrecognized function call.
} else {
// No colon. Check for create window.
if (idStrs[1].startsWith("= luajava.newInstance")) {
while (windows.size() <= windowID) {
windows.add(null);
}
windows.set(windowID, new SVWindow(stringList.get(1),
intList.get(0), intList.get(1),
intList.get(2), intList.get(3),
intList.get(4), intList.get(5),
intList.get(6)));
}
// else for forward compatibility purposes, silently ignore any
// unrecognized function call.
}
} else if (inputLine.startsWith("svmain")) {
// Startup or end. Startup is a lua bind, which is now a no-op.
if (inputLine.startsWith("svmain:exit")) {
exit();
}
// else for forward compatibility purposes, silently ignore any
// unrecognized function call.
}
// else for forward compatibility purposes, silently ignore any
// unrecognized function call.
}
/** Called from the client to make the server exit. */
@ -169,8 +388,9 @@ public class ScrollView {
if (args.length > 0) {
SERVER_PORT = Integer.parseInt(args[0]);
}
L = LuaStateFactory.newLuaState();
L.openLibs();
windows = new ArrayList<SVWindow>(100);
intPattern = Pattern.compile("[0-9-][0-9]*");
floatPattern = Pattern.compile("[0-9-][0-9]*\\.[0-9]*");
try {
// Open a socket to listen on.

View File

@ -2,6 +2,9 @@ JAVAC = javac
JAR = jar
SCROLLVIEW_FILES = \
SVAbstractMenuItem.java \
SVCheckboxMenuItem.java \
SVEmptyMenuItem.java \
SVEvent.java \
SVEventHandler.java \
SVEventType.java \
@ -9,10 +12,14 @@ SVImageHandler.java \
SVMenuBar.java \
SVMenuItem.java \
SVPopupMenu.java \
SVSubMenuItem.java \
SVWindow.java \
ScrollView.java
SCROLLVIEW_CLASSES = \
SVAbstractMenuItem.class \
SVCheckboxMenuItem.class \
SVEmptyMenuItem.class \
SVEvent.class \
SVEventHandler.class \
SVEventType.class \
@ -20,15 +27,15 @@ SVImageHandler.class \
SVMenuBar.class \
SVMenuItem.class \
SVPopupMenu.class \
SVSubMenuItem.class \
SVWindow.class \
ScrollView.class
SCROLLVIEW_LIBS = \
luajava-1.1.jar \
piccolo-1.2.jar \
piccolox-1.2.jar
CLASSPATH = ./luajava-1.1.jar:./piccolo-1.2.jar:./piccolox-1.2.jar
CLASSPATH = ./piccolo-1.2.jar:./piccolox-1.2.jar
ScrollView.jar : $(SCROLLVIEW_CLASSES)
$(JAR) -c -f $@ $(SCROLLVIEW_CLASSES)