close
code:
package org.eclipseguide.swt;
import // 略
public class Resolve extends BasicFramework {
private final static String PREFIX = "議案 ";
private static int tableCols;
private static String url = null;
private static String driver = null;
private static String user = null;
private static String passwd = null;
private static String sql = null;
private static String imgPath = null;
private static String servlet = null;
private static ResourceBundle bundle;
private Table table = null;
private TableEditor editor = null;
private Menu menu = null;
private Text txtSum = null;
private Text txtCount = null;
private Text txtServlet = null;
private Label lblBill = null;
private Button btnSave;
private Connection con = null;
private StringBuffer sb = new StringBuffer();
private int msgCnt = 0;
private int bill = 0;
private String choose = " ";
private final static String TABLE_HEAD[] = { "出席證號", "姓名", "股數", "證件字號" };
static {
bundle = ResourceBundle.getBundle("init");
url = bundle.getString("url");
driver = bundle.getString("driver");
user = bundle.getString("user");
passwd = bundle.getString("passwd");
sql = bundle.getString("sql");
imgPath = bundle.getString("imgPath");
servlet = bundle.getString("servlet");
try {
tableCols = Integer.parseInt(bundle.getString("tableCols"));
} catch (NumberFormatException e) {
tableCols = 25;
}
}
public Resolve() {
super(" 表決票 - 記票程式");
fileSubMenuHeader.setText("檔案");
fileExit.setText("離開");
helpSubMenuHeader.setText("說明");
helpAbout.setText("關於");
shell.setLayout(new FormLayout());
createMenu();
createTable();
createEditor();
createPopupMenu();
createOthers();
Thread t = new Thread(new DocnoSocket());
t.start();
this.mainLoop(500, 420);
}
private void createMenu() {
MenuItem msgSubMenu = new MenuItem(fileSubMenu, SWT.SEPARATOR);
msgSubMenu = new MenuItem(fileSubMenu, SWT.PUSH);
msgSubMenu.setText("訊息");
msgSubMenu.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
MessageBox msg = new MessageBox(shell);
msg.setText("訊息說明");
if (sb.length() == 0) {
msg.setMessage("沒有錯誤訊息");
} else {
msg.setMessage(sb.toString());
}
msg.open();
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
}
private void createOthers() {
FormData fd = null;
lblBill = new Label(shell, SWT.BORDER);
lblBill.setText("沒有表決議案");
fd = new FormData();
fd.top = new FormAttachment(1, 0);
fd.left = new FormAttachment(1, 0);
fd.right = new FormAttachment(99, 0);
fd.height = 40;
lblBill.setLayoutData(fd);
lblBill.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Rectangle rect = lblBill.getBounds();
lblBill.setVisible(false);
final Group g = new Group(shell, SWT.NONE);
g.setText(" 議案");
g.setBounds(rect.x, 1, rect.width, rect.height + 5);
g.setLayout(new RowLayout());
appendMsg("共 " + bill + " 個議案" + "@" + rect.x + "," + rect.y);
rect.y += 12;
rect.x += 5;
int base = rect.x;
for (int i = 1; i <= bill; i++) {
if (i > 14)
break;
final Button b1 = new Button(g, SWT.RADIO);
String str = PREFIX + i;
b1.setText(str);
b1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
RGB rgb = new RGB(255, 0, 0);
b1.setForeground(new Color(display, rgb));
int i = bill;
bill = getBill(b1.getText());
if (i != bill) {
appendMsg("目前為" + PREFIX + bill);
}
}
public void focusLost(FocusEvent e) {
RGB rgb = new RGB(0, 0, 0);
b1.setForeground(new Color(display, rgb));
}
});
rect.width = 62;
rect.height = 15;
if (((i % 7) == 1) && (i > 7)) {
rect.x = base;
rect.y += 15;
}
b1.setBounds(rect);
rect.x += 66;
}
bill = 0;
}
});
fd = new FormData();
fd.top = new FormAttachment(lblBill, 10, SWT.BOTTOM);
fd.left = new FormAttachment(1, 0);
fd.right = new FormAttachment(99, 0);
fd.bottom = new FormAttachment(85, 0);
table.setLayoutData(fd);
txtCount = new Text(shell, SWT.NONE | SWT.LEFT);
txtCount.setEditable(false);
fd = new FormData();
fd.top = new FormAttachment(table, 5, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.right = new FormAttachment(30, 0);
txtCount.setLayoutData(fd);
txtSum = new Text(shell, SWT.NONE | SWT.LEFT);
txtSum.setEditable(false);
fd = new FormData();
fd.top = new FormAttachment(table, 25, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.right = new FormAttachment(30, 0);
txtSum.setLayoutData(fd);
final Group g = new Group(shell, SWT.NORMAL);
g.setText("種類");
fd = new FormData();
fd.top = new FormAttachment(table, 10, SWT.BOTTOM);
fd.left = new FormAttachment(0, 220);
fd.width = 110;
fd.height = 20;
g.setLayoutData(fd);
g.setLayout(new RowLayout());
Button rb = new Button(g, SWT.RADIO);
rb.setText("同意");
rb.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
choose = "Y";
}
});
rb = new Button(g, SWT.RADIO);
rb.setText("不同意");
rb.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
choose = "N";
}
});
btnSave = new Button(shell, SWT.PUSH);
btnSave.setText("送出");
fd = new FormData();
fd.top = new FormAttachment(table, 17, SWT.BOTTOM);
fd.left = new FormAttachment(0, 360);
fd.width = 50;
fd.height = 25;
btnSave.setLayoutData(fd);
btnSave.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
MessageBox msg = new MessageBox(shell);
msg.setText("訊息");
if ((bill > 0)
&& ((choose.equalsIgnoreCase("Y")) || (choose
.equalsIgnoreCase("N")))) {
Thread t = new Thread(new SaveResolveSocket());
t.start();
msg.setMessage("\r\n資料已送出 !!");
} else {
msg.setMessage("\r\n未選擇議案 或 種類 !!");
}
msg.open();
}
});
final Button btnClear = new Button(shell, SWT.PUSH);
btnClear.setText("清除");
btnClear.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
clearTable();
}
});
fd = new FormData();
fd.top = new FormAttachment(table, 17, SWT.BOTTOM);
fd.left = new FormAttachment(0, 410);
fd.width = 50;
fd.height = 25;
btnClear.setLayoutData(fd);
txtServlet = new Text(shell, SWT.NONE | SWT.LEFT);
txtServlet.setText(servlet);
txtServlet.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
System.out.println(txtServlet.getText());
Thread t = new Thread(new DocnoSocket());
t.start();
}
}
});
fd = new FormData();
fd.top = new FormAttachment(txtSum, 5, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.width = 200;
fd.height = 15;
txtServlet.setLayoutData(fd);
disableTable();
txtServlet.setVisible(false);
}
private void createTable() {
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
TableColumn tc1 = new TableColumn(table, SWT.RIGHT);
TableColumn tc2 = new TableColumn(table, SWT.NONE);
TableColumn tc3 = new TableColumn(table, SWT.RIGHT);
TableColumn tc4 = new TableColumn(table, SWT.CENTER);
tc1.setText(TABLE_HEAD[0]);
tc2.setText(TABLE_HEAD[1]);
tc3.setText(TABLE_HEAD[2]);
tc4.setText(TABLE_HEAD[3]);
tc1.setWidth(80);
tc2.setWidth(160);
tc3.setWidth(120);
tc4.setWidth(100);
tc2.setResizable(true);
tc3.setResizable(true);
tc4.setResizable(true);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.addListener(SWT.Selection, new SummaryListener());
table.addListener(SWT.MouseDown, new TextEditListener());
for (int i = 0; i < tableCols; i++) {
TableItem item = new TableItem(table, SWT.NONE);
String[] data = new String[4];
item.setText(data);
}
}
private void clearTable() {
table.clearAll();
table.notifyListeners(SWT.Selection, new Event());
}
private void disableTable() {
table.setEnabled(false);
btnSave.setEnabled(false);
txtServlet.setVisible(true);
}
private void enableTable() {
table.setEnabled(true);
btnSave.setEnabled(true);
txtServlet.setVisible(false);
}
private void createEditor() {
editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
}
private void createPopupMenu() {
menu = new Menu(shell, SWT.POP_UP);
table.setMenu(menu);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("刪除");
item.addListener(SWT.Selection, new DelItemListener());
}
private void appendMsg(String msg) {
msgCnt++;
sb.append(msgCnt);
sb.append(". ");
sb.append(msg);
sb.append("\r\n");
}
public void dispose() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
}
con = null;
}
}
public void displayHelpAboutDialog() {
MessageBox msg = new MessageBox(shell);
msg.setText("關於計票程式");
msg.setMessage("\r\n版權所有 : jihsun securities co., ltd."
+ "\r\n\r\n作者\t : jack" + "\r\n\r\n時間\t : 2005-03-04"
+ "\r\n\r\n版本\t : v 1.0");
msg.open();
}
private int getBill(String str) {
String s = str.replaceAll(PREFIX, "");
try {
bill = Integer.parseInt(s);
} catch (NumberFormatException e) {
bill = 0;
}
return bill;
}
public static void main(String[] args) {
new Resolve();
}
class TextEditListener implements Listener {
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
final TableItem item = table.getItem(table.getSelectionIndex());
int i = 0;
Rectangle rect = item.getBounds(i);
if (event.y <= 0) {
pt = new Point(rect.x, rect.y);
}
if (rect.contains(pt)) {
final int column = i;
final Text text = new Text(table, SWT.NONE);
text.setTextLimit(10);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
text.dispose();
break;
case SWT.Traverse:
switch (e.detail) {
case SWT.TRAVERSE_RETURN:
item.setText(column, text.getText());
text.dispose();
if (!item.getText().equals("")) {
// Thread t = new Thread(new Register());
Thread t = new Thread(new ShmmosSocket());
t.start();
}
case SWT.TRAVERSE_ESCAPE:
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
editor.setEditor(text, item, i);
text.setText(item.getText(i));
text.selectAll();
text.setFocus();
return;
}
}
}
class DelItemListener implements Listener {
public void handleEvent(Event event) {
final TableItem item = table.getItem(table.getSelectionIndex());
for (int i = 0; i < table.getColumnCount(); i++) {
item.setText(i, "");
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
}
class SummaryListener implements Listener {
public void handleEvent(Event event) {
long sum = 0;
int count = 0;
for (int i = 0; i < table.getItemCount(); i++) {
TableItem item = table.getItem(i);
if (!item.getText().equals("")) {
String ss = item.getText(2).replaceAll(",", "");
long stkqty = 0;
try {
stkqty = Long.parseLong(ss);
count++;
} catch (NumberFormatException e) {
stkqty = 0;
}
sum += stkqty;
}
}
txtSum.setText("合計權數 : " + Format.longFormat(sum) + " 權");
txtCount.setText("合計人數 : " + Format.longFormat(count) + " 人");
}
}
class Register implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
final TableItem item = table.getItem(table
.getSelectionIndex());
try {
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, item.getText());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String[] data = { rs.getString(1), rs.getString(2),
Format.longFormat(rs.getLong(3)),
rs.getString(4) };
item.setText(data);
} else {
String[] data = { item.getText(), "", "", "" };
item.setText(data);
}
} catch (SQLException e) {
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
});
}
}
class ConnectDB implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
Class.forName(driver);
try {
con = DriverManager
.getConnection(url, user, passwd);
table.addListener(SWT.MouseDown,
new TextEditListener());
appendMsg("連接資料庫==> Ok");
txtCount.setText("");
} catch (SQLException e) {
appendMsg(e.toString());
txtCount.setText("資料庫連接失敗 !!");
}
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
txtCount.setText("資料庫軀動程式載入失敗 !!");
}
}
});
}
}
class ShmmosSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
final TableItem item = table.getItem(table
.getSelectionIndex());
try {
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
String psntid = "method=SHMMOS&psntid="
+ item.getText(0);
out.writeBytes(psntid);
out.flush();
out.close();
InputStream raw = uc.getInputStream();
ObjectInputStream dis = new ObjectInputStream(
new BufferedInputStream(raw));
String[] data = { item.getText(0), "", "", "" };
ShmmosForm shmmos = null;
try {
shmmos = (ShmmosForm) dis.readObject();
data[0] = String.valueOf(shmmos.getPsntid());
data[1] = shmmos.getPatname();
data[2] = shmmos.getVoteqty_fmt();
data[3] = shmmos.getRegid();
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
}
dis.close();
item.setText(data);
} catch (MalformedURLException e) {
appendMsg(e.toString());
} catch (IOException e) {
appendMsg(e.toString());
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
});
}
}
class DocnoSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
String method = "method=DOCNO";
out.writeBytes(method);
out.flush();
out.close();
InputStream raw = uc.getInputStream();
ObjectInputStream dis = new ObjectInputStream(
new BufferedInputStream(raw));
DocnoForm docno = null;
try {
docno = (DocnoForm) dis.readObject();
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
}
dis.close();
if ((docno == null) || (docno.getResolve() == 0)) {
disableTable();
} else {
clearTable();
bill = docno.getResolve();
lblBill.notifyListeners(SWT.Selection, new Event());
}
enableTable();
} catch (MalformedURLException e) {
appendMsg(e.toString());
disableTable();
} catch (IOException e) {
appendMsg(e.toString());
disableTable();
}
}
});
}
}
class SaveResolveSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
btnSave.setEnabled(false);
ArrayList lst = new ArrayList();
ResolveForm resolve = null;
for (int i = 0; i < tableCols; i++) {
TableItem item = table.getItem(i);
if (!item.getText(2).equals("")) {
resolve = new ResolveForm();
resolve.setBill(bill);
resolve.setPsntid(Integer.parseInt(item
.getText(0)));
try {
resolve.setVoteqty(Double.parseDouble(item
.getText(2).replaceAll(",", "")));
} catch (NumberFormatException e) {
resolve.setVoteqty(0);
}
resolve.setChoose(choose);
lst.add(resolve);
}
}
System.out.println("lst.isEmpty==" + lst.isEmpty());
if (lst.isEmpty()) {
return;
}
StringBuffer sb = new StringBuffer("method=RESOLVE");
Iterator it = lst.iterator();
while (it.hasNext()) {
resolve = (ResolveForm) it.next();
sb.append(resolve.toString());
}
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
System.out.println(sb.toString());
out.writeBytes(sb.toString());
out.flush();
out.close();
InputStream raw = uc.getInputStream();
DataInputStream dis = new DataInputStream(
new BufferedInputStream(raw));
... // 太長了,超過 PIXNET 的限制
import // 略
public class Resolve extends BasicFramework {
private final static String PREFIX = "議案 ";
private static int tableCols;
private static String url = null;
private static String driver = null;
private static String user = null;
private static String passwd = null;
private static String sql = null;
private static String imgPath = null;
private static String servlet = null;
private static ResourceBundle bundle;
private Table table = null;
private TableEditor editor = null;
private Menu menu = null;
private Text txtSum = null;
private Text txtCount = null;
private Text txtServlet = null;
private Label lblBill = null;
private Button btnSave;
private Connection con = null;
private StringBuffer sb = new StringBuffer();
private int msgCnt = 0;
private int bill = 0;
private String choose = " ";
private final static String TABLE_HEAD[] = { "出席證號", "姓名", "股數", "證件字號" };
static {
bundle = ResourceBundle.getBundle("init");
url = bundle.getString("url");
driver = bundle.getString("driver");
user = bundle.getString("user");
passwd = bundle.getString("passwd");
sql = bundle.getString("sql");
imgPath = bundle.getString("imgPath");
servlet = bundle.getString("servlet");
try {
tableCols = Integer.parseInt(bundle.getString("tableCols"));
} catch (NumberFormatException e) {
tableCols = 25;
}
}
public Resolve() {
super(" 表決票 - 記票程式");
fileSubMenuHeader.setText("檔案");
fileExit.setText("離開");
helpSubMenuHeader.setText("說明");
helpAbout.setText("關於");
shell.setLayout(new FormLayout());
createMenu();
createTable();
createEditor();
createPopupMenu();
createOthers();
Thread t = new Thread(new DocnoSocket());
t.start();
this.mainLoop(500, 420);
}
private void createMenu() {
MenuItem msgSubMenu = new MenuItem(fileSubMenu, SWT.SEPARATOR);
msgSubMenu = new MenuItem(fileSubMenu, SWT.PUSH);
msgSubMenu.setText("訊息");
msgSubMenu.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
MessageBox msg = new MessageBox(shell);
msg.setText("訊息說明");
if (sb.length() == 0) {
msg.setMessage("沒有錯誤訊息");
} else {
msg.setMessage(sb.toString());
}
msg.open();
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
}
private void createOthers() {
FormData fd = null;
lblBill = new Label(shell, SWT.BORDER);
lblBill.setText("沒有表決議案");
fd = new FormData();
fd.top = new FormAttachment(1, 0);
fd.left = new FormAttachment(1, 0);
fd.right = new FormAttachment(99, 0);
fd.height = 40;
lblBill.setLayoutData(fd);
lblBill.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Rectangle rect = lblBill.getBounds();
lblBill.setVisible(false);
final Group g = new Group(shell, SWT.NONE);
g.setText(" 議案");
g.setBounds(rect.x, 1, rect.width, rect.height + 5);
g.setLayout(new RowLayout());
appendMsg("共 " + bill + " 個議案" + "@" + rect.x + "," + rect.y);
rect.y += 12;
rect.x += 5;
int base = rect.x;
for (int i = 1; i <= bill; i++) {
if (i > 14)
break;
final Button b1 = new Button(g, SWT.RADIO);
String str = PREFIX + i;
b1.setText(str);
b1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
RGB rgb = new RGB(255, 0, 0);
b1.setForeground(new Color(display, rgb));
int i = bill;
bill = getBill(b1.getText());
if (i != bill) {
appendMsg("目前為" + PREFIX + bill);
}
}
public void focusLost(FocusEvent e) {
RGB rgb = new RGB(0, 0, 0);
b1.setForeground(new Color(display, rgb));
}
});
rect.width = 62;
rect.height = 15;
if (((i % 7) == 1) && (i > 7)) {
rect.x = base;
rect.y += 15;
}
b1.setBounds(rect);
rect.x += 66;
}
bill = 0;
}
});
fd = new FormData();
fd.top = new FormAttachment(lblBill, 10, SWT.BOTTOM);
fd.left = new FormAttachment(1, 0);
fd.right = new FormAttachment(99, 0);
fd.bottom = new FormAttachment(85, 0);
table.setLayoutData(fd);
txtCount = new Text(shell, SWT.NONE | SWT.LEFT);
txtCount.setEditable(false);
fd = new FormData();
fd.top = new FormAttachment(table, 5, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.right = new FormAttachment(30, 0);
txtCount.setLayoutData(fd);
txtSum = new Text(shell, SWT.NONE | SWT.LEFT);
txtSum.setEditable(false);
fd = new FormData();
fd.top = new FormAttachment(table, 25, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.right = new FormAttachment(30, 0);
txtSum.setLayoutData(fd);
final Group g = new Group(shell, SWT.NORMAL);
g.setText("種類");
fd = new FormData();
fd.top = new FormAttachment(table, 10, SWT.BOTTOM);
fd.left = new FormAttachment(0, 220);
fd.width = 110;
fd.height = 20;
g.setLayoutData(fd);
g.setLayout(new RowLayout());
Button rb = new Button(g, SWT.RADIO);
rb.setText("同意");
rb.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
choose = "Y";
}
});
rb = new Button(g, SWT.RADIO);
rb.setText("不同意");
rb.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
choose = "N";
}
});
btnSave = new Button(shell, SWT.PUSH);
btnSave.setText("送出");
fd = new FormData();
fd.top = new FormAttachment(table, 17, SWT.BOTTOM);
fd.left = new FormAttachment(0, 360);
fd.width = 50;
fd.height = 25;
btnSave.setLayoutData(fd);
btnSave.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
MessageBox msg = new MessageBox(shell);
msg.setText("訊息");
if ((bill > 0)
&& ((choose.equalsIgnoreCase("Y")) || (choose
.equalsIgnoreCase("N")))) {
Thread t = new Thread(new SaveResolveSocket());
t.start();
msg.setMessage("\r\n資料已送出 !!");
} else {
msg.setMessage("\r\n未選擇議案 或 種類 !!");
}
msg.open();
}
});
final Button btnClear = new Button(shell, SWT.PUSH);
btnClear.setText("清除");
btnClear.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
clearTable();
}
});
fd = new FormData();
fd.top = new FormAttachment(table, 17, SWT.BOTTOM);
fd.left = new FormAttachment(0, 410);
fd.width = 50;
fd.height = 25;
btnClear.setLayoutData(fd);
txtServlet = new Text(shell, SWT.NONE | SWT.LEFT);
txtServlet.setText(servlet);
txtServlet.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
System.out.println(txtServlet.getText());
Thread t = new Thread(new DocnoSocket());
t.start();
}
}
});
fd = new FormData();
fd.top = new FormAttachment(txtSum, 5, SWT.BOTTOM);
fd.left = new FormAttachment(0, 10);
fd.width = 200;
fd.height = 15;
txtServlet.setLayoutData(fd);
disableTable();
txtServlet.setVisible(false);
}
private void createTable() {
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
TableColumn tc1 = new TableColumn(table, SWT.RIGHT);
TableColumn tc2 = new TableColumn(table, SWT.NONE);
TableColumn tc3 = new TableColumn(table, SWT.RIGHT);
TableColumn tc4 = new TableColumn(table, SWT.CENTER);
tc1.setText(TABLE_HEAD[0]);
tc2.setText(TABLE_HEAD[1]);
tc3.setText(TABLE_HEAD[2]);
tc4.setText(TABLE_HEAD[3]);
tc1.setWidth(80);
tc2.setWidth(160);
tc3.setWidth(120);
tc4.setWidth(100);
tc2.setResizable(true);
tc3.setResizable(true);
tc4.setResizable(true);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.addListener(SWT.Selection, new SummaryListener());
table.addListener(SWT.MouseDown, new TextEditListener());
for (int i = 0; i < tableCols; i++) {
TableItem item = new TableItem(table, SWT.NONE);
String[] data = new String[4];
item.setText(data);
}
}
private void clearTable() {
table.clearAll();
table.notifyListeners(SWT.Selection, new Event());
}
private void disableTable() {
table.setEnabled(false);
btnSave.setEnabled(false);
txtServlet.setVisible(true);
}
private void enableTable() {
table.setEnabled(true);
btnSave.setEnabled(true);
txtServlet.setVisible(false);
}
private void createEditor() {
editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
}
private void createPopupMenu() {
menu = new Menu(shell, SWT.POP_UP);
table.setMenu(menu);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("刪除");
item.addListener(SWT.Selection, new DelItemListener());
}
private void appendMsg(String msg) {
msgCnt++;
sb.append(msgCnt);
sb.append(". ");
sb.append(msg);
sb.append("\r\n");
}
public void dispose() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
}
con = null;
}
}
public void displayHelpAboutDialog() {
MessageBox msg = new MessageBox(shell);
msg.setText("關於計票程式");
msg.setMessage("\r\n版權所有 : jihsun securities co., ltd."
+ "\r\n\r\n作者\t : jack" + "\r\n\r\n時間\t : 2005-03-04"
+ "\r\n\r\n版本\t : v 1.0");
msg.open();
}
private int getBill(String str) {
String s = str.replaceAll(PREFIX, "");
try {
bill = Integer.parseInt(s);
} catch (NumberFormatException e) {
bill = 0;
}
return bill;
}
public static void main(String[] args) {
new Resolve();
}
class TextEditListener implements Listener {
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
final TableItem item = table.getItem(table.getSelectionIndex());
int i = 0;
Rectangle rect = item.getBounds(i);
if (event.y <= 0) {
pt = new Point(rect.x, rect.y);
}
if (rect.contains(pt)) {
final int column = i;
final Text text = new Text(table, SWT.NONE);
text.setTextLimit(10);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
text.dispose();
break;
case SWT.Traverse:
switch (e.detail) {
case SWT.TRAVERSE_RETURN:
item.setText(column, text.getText());
text.dispose();
if (!item.getText().equals("")) {
// Thread t = new Thread(new Register());
Thread t = new Thread(new ShmmosSocket());
t.start();
}
case SWT.TRAVERSE_ESCAPE:
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
editor.setEditor(text, item, i);
text.setText(item.getText(i));
text.selectAll();
text.setFocus();
return;
}
}
}
class DelItemListener implements Listener {
public void handleEvent(Event event) {
final TableItem item = table.getItem(table.getSelectionIndex());
for (int i = 0; i < table.getColumnCount(); i++) {
item.setText(i, "");
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
}
class SummaryListener implements Listener {
public void handleEvent(Event event) {
long sum = 0;
int count = 0;
for (int i = 0; i < table.getItemCount(); i++) {
TableItem item = table.getItem(i);
if (!item.getText().equals("")) {
String ss = item.getText(2).replaceAll(",", "");
long stkqty = 0;
try {
stkqty = Long.parseLong(ss);
count++;
} catch (NumberFormatException e) {
stkqty = 0;
}
sum += stkqty;
}
}
txtSum.setText("合計權數 : " + Format.longFormat(sum) + " 權");
txtCount.setText("合計人數 : " + Format.longFormat(count) + " 人");
}
}
class Register implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
final TableItem item = table.getItem(table
.getSelectionIndex());
try {
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, item.getText());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String[] data = { rs.getString(1), rs.getString(2),
Format.longFormat(rs.getLong(3)),
rs.getString(4) };
item.setText(data);
} else {
String[] data = { item.getText(), "", "", "" };
item.setText(data);
}
} catch (SQLException e) {
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
});
}
}
class ConnectDB implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
Class.forName(driver);
try {
con = DriverManager
.getConnection(url, user, passwd);
table.addListener(SWT.MouseDown,
new TextEditListener());
appendMsg("連接資料庫==> Ok");
txtCount.setText("");
} catch (SQLException e) {
appendMsg(e.toString());
txtCount.setText("資料庫連接失敗 !!");
}
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
txtCount.setText("資料庫軀動程式載入失敗 !!");
}
}
});
}
}
class ShmmosSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
final TableItem item = table.getItem(table
.getSelectionIndex());
try {
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
String psntid = "method=SHMMOS&psntid="
+ item.getText(0);
out.writeBytes(psntid);
out.flush();
out.close();
InputStream raw = uc.getInputStream();
ObjectInputStream dis = new ObjectInputStream(
new BufferedInputStream(raw));
String[] data = { item.getText(0), "", "", "" };
ShmmosForm shmmos = null;
try {
shmmos = (ShmmosForm) dis.readObject();
data[0] = String.valueOf(shmmos.getPsntid());
data[1] = shmmos.getPatname();
data[2] = shmmos.getVoteqty_fmt();
data[3] = shmmos.getRegid();
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
}
dis.close();
item.setText(data);
} catch (MalformedURLException e) {
appendMsg(e.toString());
} catch (IOException e) {
appendMsg(e.toString());
}
if (table.getSelectionIndex() < (tableCols - 1)) {
table.setSelection(table.getSelectionIndex() + 1);
table.notifyListeners(SWT.Selection, new Event());
table.notifyListeners(SWT.MouseDown, new Event());
}
}
});
}
}
class DocnoSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
String method = "method=DOCNO";
out.writeBytes(method);
out.flush();
out.close();
InputStream raw = uc.getInputStream();
ObjectInputStream dis = new ObjectInputStream(
new BufferedInputStream(raw));
DocnoForm docno = null;
try {
docno = (DocnoForm) dis.readObject();
} catch (ClassNotFoundException e) {
appendMsg(e.toString());
}
dis.close();
if ((docno == null) || (docno.getResolve() == 0)) {
disableTable();
} else {
clearTable();
bill = docno.getResolve();
lblBill.notifyListeners(SWT.Selection, new Event());
}
enableTable();
} catch (MalformedURLException e) {
appendMsg(e.toString());
disableTable();
} catch (IOException e) {
appendMsg(e.toString());
disableTable();
}
}
});
}
}
class SaveResolveSocket implements Runnable {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
btnSave.setEnabled(false);
ArrayList lst = new ArrayList();
ResolveForm resolve = null;
for (int i = 0; i < tableCols; i++) {
TableItem item = table.getItem(i);
if (!item.getText(2).equals("")) {
resolve = new ResolveForm();
resolve.setBill(bill);
resolve.setPsntid(Integer.parseInt(item
.getText(0)));
try {
resolve.setVoteqty(Double.parseDouble(item
.getText(2).replaceAll(",", "")));
} catch (NumberFormatException e) {
resolve.setVoteqty(0);
}
resolve.setChoose(choose);
lst.add(resolve);
}
}
System.out.println("lst.isEmpty==" + lst.isEmpty());
if (lst.isEmpty()) {
return;
}
StringBuffer sb = new StringBuffer("method=RESOLVE");
Iterator it = lst.iterator();
while (it.hasNext()) {
resolve = (ResolveForm) it.next();
sb.append(resolve.toString());
}
URL url = new URL(txtServlet.getText());
HttpURLConnection uc = (HttpURLConnection) url
.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(uc.getOutputStream()));
System.out.println(sb.toString());
out.writeBytes(sb.toString());
out.flush();
out.close();
InputStream raw = uc.getInputStream();
DataInputStream dis = new DataInputStream(
new BufferedInputStream(raw));
... // 太長了,超過 PIXNET 的限制
全站熱搜