|
| 1 | +package com.ognize.lz77js.ui; |
| 2 | + |
| 3 | +import net.sf.j2s.ajax.AJAXServletRequest; |
| 4 | +import net.sf.j2s.ajax.AJAXServletSWTRequest; |
| 5 | +import org.eclipse.swt.SWT; |
| 6 | +import org.eclipse.swt.custom.SashForm; |
| 7 | +import org.eclipse.swt.events.SelectionAdapter; |
| 8 | +import org.eclipse.swt.events.SelectionEvent; |
| 9 | +import org.eclipse.swt.layout.FillLayout; |
| 10 | +import org.eclipse.swt.layout.GridData; |
| 11 | +import org.eclipse.swt.layout.GridLayout; |
| 12 | +import org.eclipse.swt.widgets.Button; |
| 13 | +import org.eclipse.swt.widgets.Composite; |
| 14 | +import org.eclipse.swt.widgets.Display; |
| 15 | +import org.eclipse.swt.widgets.Label; |
| 16 | +import org.eclipse.swt.widgets.MessageBox; |
| 17 | +import org.eclipse.swt.widgets.Shell; |
| 18 | +import org.eclipse.swt.widgets.Text; |
| 19 | +import com.ognize.lz77js.LZ77JSSimpleRPCRunnable; |
| 20 | + |
| 21 | +public class LZ77JS { |
| 22 | + |
| 23 | + final static String LZ77_JS_URL = "http://bl.ognize.com/lz77js/lz77js"; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param args |
| 27 | + */ |
| 28 | + public static void main(String[] args) { |
| 29 | + final Display display = new Display(); |
| 30 | + final Shell shell = new Shell(display); |
| 31 | + //shell.setMaximized(true); |
| 32 | + shell.setText("LZ77 JavaScript Compressor"); |
| 33 | + shell.setLayout(new FillLayout()); |
| 34 | + |
| 35 | + SashForm form = new SashForm(shell, SWT.VERTICAL); |
| 36 | + form.setLayout(new FillLayout()); |
| 37 | + |
| 38 | + Composite outlinePanel = new Composite(form, SWT.NONE); |
| 39 | + outlinePanel.setLayout(new GridLayout()); |
| 40 | + new Label(outlinePanel, SWT.NONE).setText("JavaScript Source:"); |
| 41 | + final Text sourceText = new Text(outlinePanel, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); |
| 42 | + sourceText.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 43 | + |
| 44 | + Composite optionPanel = new Composite(outlinePanel, SWT.NONE); |
| 45 | + optionPanel.setLayout(new GridLayout(3, true)); |
| 46 | + optionPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 47 | + final Button regExpButton = new Button(optionPanel, SWT.CHECK); |
| 48 | + regExpButton.setSelection(true); |
| 49 | + regExpButton.setText("RegExp trimming before LZ77"); |
| 50 | + regExpButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 51 | + |
| 52 | + final Button lz77HeaderButton = new Button(optionPanel, SWT.CHECK); |
| 53 | + lz77HeaderButton.setSelection(true); |
| 54 | + lz77HeaderButton.setText("Output with LZ77 decoder"); |
| 55 | + lz77HeaderButton.setAlignment(SWT.CENTER); |
| 56 | + lz77HeaderButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); |
| 57 | + |
| 58 | + Button lz77Button = new Button(optionPanel, SWT.PUSH); |
| 59 | + lz77Button.setText("LZ77 Compress"); |
| 60 | + lz77Button.setAlignment(SWT.RIGHT); |
| 61 | + lz77Button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); |
| 62 | + |
| 63 | + final Composite contentPanel = new Composite(form, SWT.NONE); |
| 64 | + contentPanel.setLayout(new GridLayout()); |
| 65 | + new Label(contentPanel, SWT.NONE).setText("LZ77 Compressed JavaScript:"); |
| 66 | + final Text resultText = new Text(contentPanel, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); |
| 67 | + resultText.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 68 | + |
| 69 | + final Label ratioLabel = new Label(contentPanel, SWT.BORDER); |
| 70 | + ratioLabel.setText("Input source JavaScript and then press \"LZ77 Compress\" button."); |
| 71 | + ratioLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 72 | + |
| 73 | + form.setWeights(new int[] { 68, 32 }); |
| 74 | + |
| 75 | + lz77Button.addSelectionListener(new SelectionAdapter () { |
| 76 | + public void widgetSelected(SelectionEvent e) { |
| 77 | + String jsContent = sourceText.getText(); |
| 78 | + String trimJS = jsContent.trim(); |
| 79 | + if (trimJS.length() == 0) { |
| 80 | + MessageBox messageBox = new MessageBox(shell, SWT.RESIZE | SWT.ICON_INFORMATION); |
| 81 | + messageBox.setMessage("Source JavaScript can not be empty!"); |
| 82 | + messageBox.open(); |
| 83 | + return ; |
| 84 | + } else if (trimJS.length() < 832 * 4 / 3) { |
| 85 | + MessageBox messageBox = new MessageBox(shell, SWT.RESIZE | SWT.ICON_INFORMATION); |
| 86 | + messageBox.setMessage("It's unnecessary to compress raw source that is less than 1k."); |
| 87 | + messageBox.open(); |
| 88 | + return ; |
| 89 | + } |
| 90 | + ratioLabel.setText("Start JavaScript lz77 compressing to server, please wait ..."); |
| 91 | + /* |
| 92 | + * Local Java Thread mode is already the default mode for Java client |
| 93 | + */ |
| 94 | + AJAXServletRequest.switchToLocalJavaThreadMode(); |
| 95 | + /* |
| 96 | + * AJAX mode is only mode for Java2Script client. |
| 97 | + * You can uncomment the follow line to call a LZ77JS RPC of bl.ognize.com. |
| 98 | + * In JavaScript mode, you should modify LZ77JSSimpleRPCRunnable#url so there |
| 99 | + * is no cross site script for XMLHttpRequest. |
| 100 | + */ |
| 101 | + //AJAXServletRequest.switchToAJAXMode(); |
| 102 | + AJAXServletSWTRequest.swtRequest(new LZ77JSSimpleRPCRunnable() { |
| 103 | + public void ajaxIn() { |
| 104 | + jsContent = sourceText.getText(); |
| 105 | + toRegExpCompress = regExpButton.getSelection(); |
| 106 | + addLZ77Header = lz77HeaderButton.getSelection(); |
| 107 | + } |
| 108 | + |
| 109 | + public void ajaxOut() { |
| 110 | + resultText.setText(result); |
| 111 | + ratioLabel.setText("Compressed Ratio: " + result.length() + " / " + sourceText.getText().length() + " = " + (100.0 * result.length() / sourceText.getText().length()) + "%"); |
| 112 | + } |
| 113 | + |
| 114 | + }); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + shell.open(); |
| 119 | + while (!shell.isDisposed()) { |
| 120 | + if (!display.readAndDispatch()) |
| 121 | + display.sleep(); |
| 122 | + } |
| 123 | + display.dispose(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments