#!/usr/bin/env python import logging from BaseHTTPServer import HTTPServer from BaseHTTPServer import BaseHTTPRequestHandler class HttpServerHandler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers.getheader("Content-Length")) request = self.rfile.read(content_length) logging.info("Request: %s" % request) # BaseHTTPRequestHandler has a property called server and because # we create MyHTTPServer, it has a handler property response = self.server.handler(request) logging.info("Response: %s" % response) self.send_response(200) self.end_headers() self.wfile.write(response) class MyHTTPServer(HTTPServer): """this class is necessary to allow passing custom request handler into the RequestHandlerClass""" def __init__(self, server_address, RequestHandlerClass, handler): HTTPServer.__init__(self, server_address, RequestHandlerClass) self.handler = handler class HttpServer: def __init__(self, name, host, port, handler): self.name = name self.host = host self.port = port self.handler = handler self.server = None def start(self): logging.info('Starting %s at %s:%d' % (self.name, self.host, self.port)) # we need use MyHttpServer here self.server = MyHTTPServer((self.host, self.port), HttpServerHandler, self.handler) self.server.serve_forever() def stop(self): if self.server: logging.info('Stopping %s at %s:%d' % (self.name, self.host, self.port)) self.server.shutdown() def server_handler(request): if request == "foo": return "bar" elif request == "bar": return "foo" else: return "foobar" if __name__ == "__main__": logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO) server = HttpServer("test server", "localhost", 9999, server_handler) server.start()
Wednesday, May 30, 2012
How to Pass Some Arguments to BaseHTTPRequestHandler in Python
Let's say we have an argument, which is a function that we want to pass to BaseHTTRequestHandler to handle a request and return an appropriate response based the request. By looking at HTTPServer class definition, it may not be really obvious how to do it.
It's actually pretty simple and straightforward how to do it. First we need to subclass HTTPServer and override the constructor that takes in our own new arguments. From the RequestHandler class, there'll be a property named server that gives us access to the HTTPServer instance. And since we've subclassed HTTPServer class, we can define as many properties as we want that those properties will be accessible in the server property of the HTTPRequestHandler. The example is shown below.
Thursday, May 24, 2012
How to Implement DFS and BFS in Python
#!/usr/bin/env python class Graph(object): def __init__(self): # the key is the vertex, the value is the list of adjacent vertices self.adjacents = {} self.nedges = 0 def num_vertices(self): return self.adjacents.keys() def num_edges(self): return self.nedges def add_edge(self, v, w): if v not in self.adjacents: self.adjacents[v] = [] self.adjacents[v].append(w) if w not in self.adjacents: self.adjacents[w] = [] self.adjacents[w].append(v) self.nedges += 1 def adjacent(self, v): return self.adjacents[v] class DepthFirstSearch(object): def __init__(self, graph, start): self.count = 0 self.markeddict = {} self.edge_to = {} self.start = start self._dfs(graph, start) def _dfs(self, graph, vertex): self.markeddict[vertex] = True self.count += 1 for v in graph.adjacent(vertex): if not self.marked(v): self.edge_to[v] = vertex self._dfs(graph, v) def marked(self, vertex): return vertex in self.markeddict def has_path_to(self, vertex): return self.marked(vertex) def path_to(self, vertex): if not self.has_path_to(vertex): return None path = [] path.append(vertex) while vertex != self.start: vertex = self.edge_to[vertex] path.append(vertex) path.reverse() return path class BreadthFirstSearch(object): def __init__(self, graph, start): self.count = 0 self.markeddict = {} self.edge_to = {} self.start = start self._bfs(graph, start) def _bfs(self, graph, vertex): self.markeddict[vertex] = True self.count += 1 q = [] q.append(vertex) while q: key = q.pop(0) for v in graph.adjacent(key): if not self.marked(v): q.append(v) self.count += 1 self.markeddict[v] = True self.edge_to[v] = key def marked(self, vertex): return vertex in self.markeddict def has_path_to(self, vertex): return self.marked(vertex) def path_to(self, vertex): if not self.has_path_to(vertex): return None path = [] path.append(vertex) while vertex != self.start: vertex = self.edge_to[vertex] path.append(vertex) path.reverse() return path if __name__ == "__main__": # key is the vertex and value is the list of adjacent vertices graph = Graph() graph.add_edge("0", "2") graph.add_edge("3", "5") graph.add_edge("3", "4") graph.add_edge("0", "1") graph.add_edge("1", "2") graph.add_edge("2", "3") graph.add_edge("2", "4") graph.add_edge("0", "5") dfs = DepthFirstSearch(graph, "0") assert "0" == "-".join(dfs.path_to("0")) assert "0-2-1" == "-".join(dfs.path_to("1")) assert "0-2" == "-".join(dfs.path_to("2")) assert "0-2-3" == "-".join(dfs.path_to("3")) assert "0-2-3-4" == "-".join(dfs.path_to("4")) assert "0-2-3-5" == "-".join(dfs.path_to("5")) assert 6 == dfs.count bfs = BreadthFirstSearch(graph, "0") assert "0" == "-".join(bfs.path_to("0")) assert "0-1" == "-".join(bfs.path_to("1")) assert "0-2" == "-".join(bfs.path_to("2")) assert "0-2-3" == "-".join(bfs.path_to("3")) assert "0-2-4" == "-".join(bfs.path_to("4")) assert "0-5" == "-".join(bfs.path_to("5")) assert 6 == bfs.count
Wednesday, May 23, 2012
How to Reverse Order of Words in Python
#!/usr/bin/env python def reverse_words(string): reverse(string, 0, len(string)-1) prev_space_index = 0 for i in xrange(0, len(string)): if string[i] == " ": reverse(string, prev_space_index, i-1) prev_space_index = i + 1 # the last word hasn't been reversed if prev_space_index < len(string): reverse(string, prev_space_index, len(string)-1) def reverse(string, beg, end): i = beg j = end # print string[beg:end] while i < j: string[i], string[j] = string[j], string[i] i += 1 j -= 1 if __name__ == "__main__": l = list("my first name is foo and my last name is bar") reverse_words(l) assert "bar is name last my and foo is name first my" == "".join(l)
Sunday, May 20, 2012
How to Implement Permutations in Python
#!/usr/bin/env python def permutate(l, n, nl): if n == 0: if len(nl) != len(set(nl)): return nl1 = [] for i in nl: nl1.append(l[i]) print nl1 else: n = n - 1 nl1 = [x for x in nl] for i in xrange(0, len(l)): nl = [x for x in nl1] nl.append(i) permutate(l, n, nl) del nl[:] def permutations(l): permutate(l, len(l), []) if __name__ == "__main__": permutations([1, 2, 3, 4])
Search Algorithms in Python
Below are some popular implementations of searching algorithms.
#!/usr/bin/env python class SequentialSearchDict(object): class Node(object): def __init__(self, key, value): self.key = key self.value = value self.next = None def __init__(self): self.first = None self.size = 0 def put(self, key, value): self._put(key, value) def _put(self, key, value): n = self._get(key) if n: n.value = value return 0 else: # add the new node into the first tmp = self.first self.first = SequentialSearchDict.Node(key, value) self.first.next = tmp self.size += 1 return 1 def _get(self, key): n = self.first while n: if n.key == key: return n n = n.next return None def get(self, key): n = self._get(key) if n: return n.value return n class BinarySearchDict(object): class Node(object): def __init__(self, key, value): self.key = key self.value = value def __init__(self): self.size = 0 self.l = [] def put(self, key, value): # the elements must be kept sorted for the binary search to work index = self._get(key) if index < 0: self.l.append(BinarySearchDict.Node(key, value)) self.size += 1 return n = self.l[index] if n.key == key: n.value = value else: self.l.append(BinarySearchDict.Node(key, value)) # shift all the elements to the right if (index+1) < (len(self.l)-1): for i in xrange(len(self.l)-1, index, -1): self.l[i] = self.l[i-1] self.size += 1 def get(self, key): # search using binary search index = self._get(key) if index < 0: return None n = self.l[index] if n.key == key: return n.value return None def _get(self, key): lo = 0 hi = len(self.l) - 1 mid = (lo + hi) / 2 while lo <= mid and hi >= mid: if self.l[mid].key > key: hi = mid - 1 mid = (lo + hi) / 2 elif self.l[mid].key == key: return mid elif self.l[mid].key < key: lo = mid + 1 mid = (lo + hi) / 2 return mid class BinarySearchTreeDict(object): class Node(object): def __init__(self, key, value): self.key = key self.value = value self.left = None self.right = None def __init__(self): self.size = 0 self.root = None def put(self, key, value): self.root = self._put(key, value, self.root) def get(self, key): return self._get(key, self.root) def _put(self, key, value, node): if node == None: self.size += 1 return BinarySearchTreeDict.Node(key, value) if node.key == key: node.value = value return node elif node.key < key: node.left = self._put(key, value, node.left) else: node.right = self._put(key, value, node.right) return node def _get(self, key, node): if node == None: return None if node.key == key: return node.value elif node.key < key: return self._get(key, node.left) else: return self._get(key, node.right) class BalancedSearchTreeDict(object): class Node(object): RED = True BLACK = False def __init__(self, key, value): self.key = key self.value = value self.left = None self.right = None self.color = BalancedSearchTreeDict.Node.RED def is_red(self, node): if node == None: return False return node.color == BalancedSearchTreeDict.Node.RED def rotate_left(self, node): x = node.right node.right = x.left x.left = node node.color = BalancedSearchTreeDict.Node.RED return x def rotate_right(self, node): x = node.left node.left = x.right x.right = node node.color = BalancedSearchTreeDict.Node.RED return x def flip_colors(self, node): node.color = BalancedSearchTreeDict.Node.RED node.left.color = BalancedSearchTreeDict.Node.BLACK node.right.color = BalancedSearchTreeDict.Node.BLACK def __init__(self): self.size = 0 self.root = None def put(self, key, value): self.root = self._put(key, value, self.root) self.root.color = BalancedSearchTreeDict.Node.BLACK def _put(self, key, value, node): if node == None: self.size += 1 return BalancedSearchTreeDict.Node(key, value) if node.key == key: node.value = value return node elif node.key < key: node.left = self._put(key, value, node.left) else: node.right = self._put(key, value, node.right) # these are the properties of Red-Black tree for balancing # the tree if node.is_red(node.right) and not node.is_red(node.left): node = node.rotate_left(node); if node.is_red(node.left) and node.is_red(node.left.left): node = node.rotate_right(node); if node.is_red(node.left) and node.is_red(node.right): node.flip_colors(node); return node def get(self, key): return self._get(key, self.root) def _get(self, key, node): if node == None: return None if node.key == key: return node.value elif node.key < key: return self._get(key, node.left) else: return self._get(key, node.right) class HashDict(object): # choose this M to be a prime number M = 977 def __init__(self, n=M): self.size = 0 self.l = [] for i in xrange(0, n): self.l.append(SequentialSearchDict()) def _hash(self, key): return (hash(key) & 0x7ffffff) % HashDict.M def put(self, key, value): d = self.l[self._hash(key)] n = d._put(key, value) self.size += n def get(self, key): return self.l[self._hash(key)].get(key) if __name__ == "__main__": searches = [SequentialSearchDict(), BinarySearchDict(), BinarySearchTreeDict(), BalancedSearchTreeDict(), HashDict()] for s in searches: assert None == s.get("foo") s.put("1", "a") s.put("2", "b") s.put("3", "c") assert 3 == s.size assert "b" == s.get("2") assert "c" == s.get("3") assert "a" == s.get("1") s.put("3", "d") assert 3 == s.size assert "d" == s.get("3") assert "b" == s.get("2") assert "a" == s.get("1") assert None == s.get("bar")
Thursday, May 17, 2012
How to Implement Pascal Triangle in Python
My implementation of Pascal Triangle.
#!/usr/bin/env python def pascal(n): if n == 0: return [1] else: l1 = pascal(n-1) print l1 l2 = [1] for i in xrange(1, len(l1)): l2.append(l1[i-1] + l1[i]) l2 += [1] return l2 if __name__ == "__main__": import sys if len(sys.argv) != 2: print "Usage:", sys.argv[0], "n" sys.exit(1) pascal(int(sys.argv[1]))
How to Implement Binary Search in Python
My simple implementations of binary search algorithms (iterative and recursive) in Python.
#!/usr/bin/env python def search(l, i, func): lo = 0 hi = len(l) - 1 mid = (lo + hi) / 2 return func(l, i, lo, mid, hi) def recursive_binary_search(l, i, lo, mid, hi): if lo > mid or hi < mid: return -1 if l[mid] > i: hi = mid - 1 mid = (lo + hi) / 2 return recursive_binary_search(l, i, lo, mid, hi) elif l[mid] == i: return mid elif l[mid] < i: lo = mid + 1 mid = (lo + hi) / 2 return recursive_binary_search(l, i, lo, mid, hi) def iterative_binary_search(l, i, lo, mid, hi): while lo <= mid and hi >= mid: if l[mid] > i: hi = mid - 1 mid = (lo + hi) / 2 elif l[mid] == i: return mid elif l[mid] < i: lo = mid + 1 mid = (lo + hi) / 2 return -1 if __name__ == "__main__": l = [x for x in xrange(0, 10)] print "Using recursive binary search" print "Input:", l for i in xrange(0, len(l)): index = search(l, i, recursive_binary_search) print "Searching for", i, ", got index", index assert i == index i = 10 index = search(l, i, recursive_binary_search) print "Searching for", i, ", got index", index print print "Using iterative binary search" print "Input:", l for i in xrange(0, len(l)): index = search(l, i, iterative_binary_search) print "Searching for", i, ", got index", index assert i == index i = 10 index = search(l, i, iterative_binary_search) print "Searching for", i, ", got index", index
Monday, May 14, 2012
How to Access SWT UI Components from Non-UI Thread
Accessing SWT components in a non-UI thread will cause an SWTException (invalid thread access) to be thrown. This example shows how an SWT application is started in a non-main thread and the main thread manipulates the SWT application by updating the text in the button and stopping the SWT application.
import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; public class SWTTest extends ApplicationWindow { private Thread uiThread; private Button button; public SWTTest() { super(null); } @Override protected Control createContents(Composite parent) { getShell().setText("SWTTest"); getShell().setSize(400, 100); Composite c = new Composite(parent, SWT.NONE); c.setLayout(new FillLayout()); button = new Button(c, SWT.NONE); button.setText("Hello"); return parent; } public Display getDisplay() { // Calling Display.getCurrent() will most likely throw // a NUllPointerException if it's called from a main thread. // The correct way to get the Display instance is by passing the // UI thread return Display.findDisplay(uiThread); } public void start() { // We store the UI thread for the getDisplay() method uiThread= Thread.currentThread(); setBlockOnOpen(true); open(); } public void updateText(final String text) { // Any SWT operations must be done in a UI thread, so we must // use getDisplay().syncExec() or getDisplay().asyncExec(). getDisplay().syncExec(new Runnable() { @Override public void run() { button.setText(text); } }); } public void stop() { // Any SWT operations must be done in a UI thread, so we must // use getDisplay().syncExec() or getDisplay().asyncExec(). getDisplay().syncExec(new Runnable() { @Override public void run() { close(); getDisplay().dispose(); } }); } public static void main(String[] args) throws Exception { final SWTTest app = new SWTTest(); // app.start() blocks, so we need to start it in a new thread. new Thread(new Runnable() { @Override public void run() { System.out.println("Starting the GUI"); app.start(); } }).start(); System.out.println("Sleeping for 3 secs"); Thread.sleep(3000); System.out.println("Updating the GUI"); app.updateText("Bye"); System.out.println("Sleeping for 3 secs"); Thread.sleep(3000); System.out.println("Stopping the GUI"); app.stop(); } }
SWT/JFace Examples
This example contains a lot of examples how to use SWT/JFace components with MigLayout. Not all SWT/JFace components are covered here.
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import net.miginfocom.swt.MigLayout; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.MultiStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.ColorSelector; import org.eclipse.jface.viewers.CellEditor; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.ComboBoxCellEditor; import org.eclipse.jface.viewers.ComboViewer; import org.eclipse.jface.viewers.ICellModifier; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.ListViewer; import org.eclipse.jface.viewers.TableLayout; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TextCellEditor; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DropTarget; import org.eclipse.swt.dnd.DropTargetAdapter; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.FileTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; public class SWTApp extends ApplicationWindow { public SWTApp() { super(null); addStatusLine(); } @Override protected Control createContents(Composite parent) { getStatusLineManager().setMessage("Welcome to SWTApp"); TabFolder tabFolder = new TabFolder(parent, SWT.NONE); TabItem tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("Tab 1"); tabItem.setControl(new MyComposite1(tabFolder)); tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("Tab 2"); tabItem.setControl(new MyComposite2(tabFolder)); tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("Tab 3"); tabItem.setControl(new MyComposite3(tabFolder)); tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("Tab 4"); tabItem.setControl(new MyComposite4(tabFolder)); tabItem = new TabItem(tabFolder, SWT.NONE); tabItem.setText("Tab 5"); tabItem.setControl(new MyComposite5(tabFolder)); getShell().setText("SWTApp"); getShell().setSize(900, 700); return parent; }; static class MyComposite1 extends Composite { public MyComposite1(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow][grow]", "[grow, fill]")); Group g1 = new Group(this, SWT.SHADOW_ETCHED_IN); g1.setLayout(new MigLayout("", "[grow, fill]", "[][grow, fill]")); g1.setLayoutData("grow"); Composite1 c1 = new Composite1(g1); c1.setLayoutData("grow, wrap"); Composite2 c2 = new Composite2(g1); c2.setLayoutData("grow"); Group g2 = new Group(this, SWT.SHADOW_ETCHED_IN); g2.setLayout(new MigLayout("", "[grow, fill]", "[grow, fill]")); g2.setLayoutData("grow"); Composite c3 = new Composite3(g2); c3.setLayoutData("grow"); } } static class Composite1 extends Composite { public Composite1(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow, fill][]", "[grow, fill]")); Composite c1 = new Composite(this, SWT.NONE); c1.setLayout(new MigLayout("", "[][grow]", "")); Label field1Label = new Label(c1, SWT.NONE); field1Label.setText("Field 1"); Text field1Text = new Text(c1, SWT.BORDER); field1Text.setLayoutData("grow, wrap"); Label field2Label = new Label(c1, SWT.NONE); field2Label.setText("Field 2"); Text field2Text = new Text(c1, SWT.BORDER); field2Text.setLayoutData("grow, wrap"); Label field3Label = new Label(c1, SWT.NONE); field3Label.setText("Field 3"); Text field3Text = new Text(c1, SWT.BORDER); field3Text.setLayoutData("grow, wrap"); Label field4Label = new Label(c1, SWT.NONE); field4Label.setText("Field 4"); Text field4Text = new Text(c1, SWT.BORDER); field4Text.setLayoutData("grow"); Composite c2 = new Composite(this, SWT.NONE); c2.setLayout(new MigLayout("", "", "[grow]")); Button startButton = new Button(c2, SWT.NONE); startButton.setText("START"); startButton.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN)); startButton.setLayoutData("grow, wrap"); Button stopButton = new Button(c2, SWT.NONE); stopButton.setText("STOP"); stopButton.setBackground(getDisplay().getSystemColor(SWT.COLOR_RED)); stopButton.setLayoutData("grow, wrap"); Button advancedButton = new Button(c2, SWT.NONE); advancedButton.setText("ADVANCED"); advancedButton.setLayoutData("grow, wrap"); ColorSelector cs = new ColorSelector(c2); Button colorSelectorButton = cs.getButton(); colorSelectorButton.setText("COLOR"); colorSelectorButton.setLayoutData("grow"); } } static class Composite2 extends Composite { public Composite2(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow]", "[grow]")); Text statusText = new Text(this, SWT.MULTI | SWT.BORDER); statusText.setEditable(false); statusText.setLayoutData("grow"); } } static class Composite3 extends Composite { public Composite3(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow]", "[grow][grow]")); Button b1 = new Button(this, SWT.NONE); b1.setText("Button 1"); b1.setLayoutData("grow, wrap"); Button b2 = new Button(this, SWT.NONE); b2.setText("Button 2"); b2.setLayoutData("grow"); } } static class MyComposite2 extends Composite { public MyComposite2(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow, fill]", "[grow, fill][]")); Composite c1 = new Composite(this, SWT.BORDER); c1.setLayout(new MigLayout("", "[grow][grow][grow][grow]", "[grow]")); c1.setLayoutData("wrap"); int[] styles = { SWT.SINGLE, SWT.MULTI }; for (int style = 0; style < styles.length; style++) { ListViewer lv = createListViewer(c1, styles[style]); lv.getList().setLayoutData("grow"); } int[] selectionStyle = { SWT.SINGLE, SWT.MULTI }; int[] checkStyle = { SWT.NONE, SWT.CHECK }; for (int selection = 0; selection < selectionStyle.length; selection++) { for (int check = 0; check < checkStyle.length; check++) { int style = selectionStyle[selection] | checkStyle[check]; TreeViewer tv = createTreeViewer(c1, style); tv.getTree().setLayoutData("grow"); } } Composite c2 = new Composite(this, SWT.BORDER); c2.setLayout(new MigLayout("", "[grow]", "")); ComboViewer cv = createComboViewer(c2); cv.getCombo().setLayoutData("grow"); } private ComboViewer createComboViewer(Composite parent) { ComboViewer viewer = new ComboViewer(parent, SWT.NONE); viewer.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { return ((ListItem) element).name; } }); viewer.setContentProvider(new IStructuredContentProvider() { @SuppressWarnings("unchecked") @Override public Object[] getElements(Object inputElement) { return ((List<ListItem>) inputElement).toArray(); } @Override public void dispose() { } @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); List<ListItem> input = new ArrayList<ListItem>(); for (int i = 0; i < 20; i++) { input.add(new ListItem("Item " + i, i)); } viewer.setInput(input); return viewer; } private ListViewer createListViewer(Composite parent, int style) { ListViewer viewer = new ListViewer(parent, style); viewer.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { return ((ListItem) element).name; } }); viewer.setContentProvider(new IStructuredContentProvider() { @SuppressWarnings("unchecked") @Override public Object[] getElements(Object inputElement) { return ((List<ListItem>) inputElement).toArray(); } @Override public void dispose() { } @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); List<ListItem> input = new ArrayList<ListItem>(); for (int i = 0; i < 20; i++) { input.add(new ListItem("Item " + i, i)); } viewer.setInput(input); return viewer; } private TreeViewer createTreeViewer(Composite parent, int style) { TreeViewer viewer = new TreeViewer(parent, style); viewer.setContentProvider(new ITreeContentProvider() { public Object[] getChildren(Object parentElement) { return ((TreeNode) parentElement).getChildren().toArray(); } public Object getParent(Object element) { return ((TreeNode) element).getParent(); } public boolean hasChildren(Object element) { return ((TreeNode) element).getChildren().size() > 0; } public Object[] getElements(Object inputElement) { return ((TreeNode) inputElement).getChildren().toArray(); } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); viewer.setInput(getRootNode()); return viewer; } private TreeNode getRootNode() { TreeNode root = new TreeNode("root"); root.addChild(new TreeNode("child 1").addChild(new TreeNode("subchild 1"))); root.addChild(new TreeNode("child 2").addChild(new TreeNode("subchild 2") .addChild(new TreeNode("grandchild 1")))); return root; } } static class ListItem { public String name; public int value; public ListItem(String n, int v) { name = n; value = v; } } static class TreeNode { private String name; private List<TreeNode> children = new ArrayList<TreeNode>(); private TreeNode parent; public TreeNode(String n) { name = n; } protected Object getParent() { return parent; } public TreeNode addChild(TreeNode child) { children.add(child); child.parent = this; return this; } public List<TreeNode> getChildren() { return children; } public String toString() { return name; } } static class MyComposite3 extends Composite { public MyComposite3(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow]", "[grow]")); Table table = createTable(); table.setLayoutData("grow"); } private Table createTable() { final String[] VALUE_SET = new String[] {"xxx", "yyy", "zzz"}; final String NAME_PROPERTY = "name"; final String VALUE_PROPERTY = "value"; Table table = new Table(this, SWT.FULL_SELECTION); final TableViewer viewer = new TableViewer(table); TableLayout layout = new TableLayout(); layout.addColumnData(new ColumnWeightData(50, 75, true)); layout.addColumnData(new ColumnWeightData(50, 75, true)); table.setLayout(layout); TableColumn column = new TableColumn(table, SWT.CENTER); column.setText("Name"); column = new TableColumn(table, SWT.NONE); column.setText("Value"); table.setHeaderVisible(true); viewer.setLabelProvider(new ITableLabelProvider() { @Override public void removeListener(ILabelProviderListener lpl) { } @Override public boolean isLabelProperty(Object element, String property) { return false; } @Override public void dispose() { } @Override public void addListener(ILabelProviderListener lpl) { } @Override public String getColumnText(Object element, int columnIndex) { switch (columnIndex) { case 0: return ((EditableTableItem) element).name; case 1: Number index = ((EditableTableItem) element).value; return VALUE_SET[index.intValue()]; default: return "Invalid column: " + columnIndex; } } @Override public Image getColumnImage(Object element, int columnIndex) { return null; } }); viewer.setContentProvider(new IStructuredContentProvider() { @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } @Override public void dispose() { } @Override public Object[] getElements(Object inputElement) { return (Object[]) inputElement; } }); viewer.setCellModifier(new ICellModifier() { @Override public void modify(Object element, String property, Object value) { TableItem tableItem = (TableItem) element; EditableTableItem data = (EditableTableItem) tableItem.getData(); if (NAME_PROPERTY.equals(property)) { data.name = value.toString(); } else { data.value = (Integer) value; } viewer.refresh(data); } @Override public Object getValue(Object element, String property) { if (NAME_PROPERTY.equals(property)) { return ((EditableTableItem) element).name; } else { return ((EditableTableItem) element).value; } } @Override public boolean canModify(Object element, String property) { return true; } }); viewer.setCellEditors(new CellEditor[] { new TextCellEditor(table), new ComboBoxCellEditor(table, VALUE_SET) }); viewer.setInput(new Object[] { new EditableTableItem("Item 1", 0), new EditableTableItem("Item 2", 1) }); viewer.setColumnProperties(new String[] {NAME_PROPERTY, VALUE_PROPERTY}); createMenu(table, viewer); return table; } private void createMenu(Table table, TableViewer viewer) { MenuManager popupMenu = new MenuManager(); IAction newRowAction = new NewRowAction(viewer); popupMenu.add(newRowAction); Menu menu = popupMenu.createContextMenu(table); table.setMenu(menu); } } static class EditableTableItem { public String name; public Integer value; public EditableTableItem(String name, Integer value) { this.name = name; this.value = value; } } static class NewRowAction extends Action { private TableViewer viewer; public NewRowAction(TableViewer viewer) { super("Insert New Row"); this.viewer = viewer; } @Override public void run() { EditableTableItem newItem = new EditableTableItem( "new row", new Integer(2)); viewer.add(newItem); } } static class MyComposite4 extends Composite { public MyComposite4(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout()); createPasswordDialogButton(); createMessageDialogButton(); createMessageBoxButton(); createErrorDialogButton(); createWizardDialogButton(); } private Button createPasswordDialogButton() { Button dialogButton = new Button(this, SWT.PUSH); dialogButton.setText("Password Dialog..."); dialogButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { UserNamePasswordDialog d = new UserNamePasswordDialog(getShell()); d.open(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return dialogButton; } private Button createMessageDialogButton() { Button dialogButton = new Button(this, SWT.PUSH); dialogButton.setText("Message Dialog..."); dialogButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { MessageDialog dialog = new MessageDialog(getShell(), "Greeting Dialog", null, "Hello! How are you today?", MessageDialog.QUESTION, new String[] { "Good", "Been better", "Excited about SWT!" }, 0); dialog.open(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return dialogButton; } private Button createMessageBoxButton() { Button dialogButton = new Button(this, SWT.PUSH); dialogButton.setText("Message Box..."); dialogButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { MessageBox dialog = new MessageBox(getShell(), SWT.OK | SWT.CANCEL); dialog.setMessage("Do you wish to continue?"); dialog.open(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return dialogButton; } private Button createErrorDialogButton() { Button dialogButton = new Button(this, SWT.PUSH); dialogButton.setText("Error Dialog..."); dialogButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { ErrorDialog errorDialog = new ErrorDialog(getShell(), "Test Error Dialog", "This is a test error dialog", createStatus(), IStatus.ERROR | IStatus.INFO); errorDialog.open(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return dialogButton; } private IStatus createStatus() { final String dummyPlugin = "some plugin"; IStatus[] statuses = new IStatus[2]; statuses[0] = new Status(IStatus.ERROR, dummyPlugin, IStatus.OK, "Oh no! An error occurred!", new Exception()); statuses[1] = new Status(IStatus.INFO, dummyPlugin, IStatus.OK, "More errors!?!?", new Exception()); MultiStatus multiStatus = new MultiStatus(dummyPlugin, IStatus.OK, statuses, "Several errors have occurred.", new Exception()); return multiStatus; } private Button createWizardDialogButton() { Button dialogButton = new Button(this, SWT.PUSH); dialogButton.setText("Wizard Dialog..."); dialogButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { WizardDialog dialog = new WizardDialog(getShell(), new ProjectWizard()); dialog.open(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); return dialogButton; } } static class UserNamePasswordDialog extends Dialog { private static final int RESET_ID = IDialogConstants.NO_TO_ALL_ID + 1; private Text userNameText; private Text passwordText; public UserNamePasswordDialog(Shell parentShell) { super(parentShell); } @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Username/Password Dialog"); } @Override protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); composite.setLayout(new MigLayout("", "[][grow]", "")); Label userNameLabel = new Label(composite, SWT.NONE); userNameLabel.setText("Username: " ); userNameText = new Text(composite, SWT.SINGLE | SWT.BORDER); userNameText.setLayoutData("grow, wrap"); Label passwordLabel = new Label(composite, SWT.NONE); passwordLabel.setText("Password: "); passwordText = new Text(composite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER); passwordText.setLayoutData("grow"); return composite; } @Override protected void createButtonsForButtonBar(Composite parent) { super.createButtonsForButtonBar(parent); createButton(parent, RESET_ID, "Reset All", false); } @Override protected void buttonPressed(int buttonId) { if (buttonId == RESET_ID) { userNameText.setText(""); passwordText.setText(""); } else { super.buttonPressed(buttonId); } } } static class DirectoryPage extends WizardPage { public static final String PAGE_NAME = "Directory"; private Button button; public DirectoryPage() { super(PAGE_NAME, "Directory Page", null); } @Override public void createControl(Composite parent) { Composite topLevel = new Composite(parent, SWT.NONE); topLevel.setLayout(new MigLayout()); button = new Button(topLevel, SWT.CHECK); button.setText("Use default directory?"); setControl(topLevel); setPageComplete(true); } public boolean useDefaultDirectory() { return button.getSelection(); } } static class ChooseDirectoryPage extends WizardPage { public static final String PAGE_NAME = "Choose Directory"; private Text text; public ChooseDirectoryPage() { super(PAGE_NAME, "Choose Directory Page", null); } @Override public void createControl(Composite parent) { Composite topLevel = new Composite(parent, SWT.NONE); topLevel.setLayout(new MigLayout("", "[][grow][]", "")); Label l = new Label(topLevel, SWT.CENTER); l.setText("Enter the directory to use:"); text = new Text(topLevel, SWT.SINGLE | SWT.BORDER); text.setEditable(false); text.setLayoutData("grow"); Button b = new Button(topLevel, SWT.PUSH); b.setText("Choose Directory"); b.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { DirectoryDialog dialog = new DirectoryDialog(getShell()); String directory = dialog.open(); text.setText(directory); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); setControl(topLevel); setPageComplete(true); } public String getDirectory() { return text.getText(); } } static class SummaryPage extends WizardPage { public static final String PAGE_NAME = "Summary"; private Label textLabel; private Composite topLevel; public SummaryPage() { super(PAGE_NAME, "Summary Page", null); } @Override public void createControl(Composite parent) { topLevel = new Composite(parent, SWT.NONE); topLevel.setLayout(new MigLayout()); textLabel = new Label(topLevel, SWT.CENTER); textLabel.setText(""); textLabel.setLayoutData("grow"); setControl(topLevel); setPageComplete(true); } public void updateText(String newText) { textLabel.setText(newText); topLevel.pack(); } } static class ProjectWizard extends Wizard { public void addPages() { addPage(new DirectoryPage()); addPage(new ChooseDirectoryPage()); addPage(new SummaryPage()); } @Override public boolean performFinish() { DirectoryPage dirPage = getDirectoryPage(); if (dirPage.useDefaultDirectory()) { System.out.println("Using default directory"); } else { ChooseDirectoryPage choosePage = getChoosePage(); System.out.println("Using directory: " + choosePage.getDirectory()); } return true; } private ChooseDirectoryPage getChoosePage() { return (ChooseDirectoryPage) getPage(ChooseDirectoryPage.PAGE_NAME); } private DirectoryPage getDirectoryPage() { return (DirectoryPage) getPage(DirectoryPage.PAGE_NAME); } @Override public boolean performCancel() { System.out.println("Perform Cancel called"); return true; } @Override public IWizardPage getNextPage(IWizardPage page) { if (page instanceof DirectoryPage) { DirectoryPage dirPage = (DirectoryPage) page; if (dirPage.useDefaultDirectory()) { SummaryPage summaryPage = (SummaryPage) getPage(SummaryPage.PAGE_NAME); summaryPage.updateText("Using default directory"); return summaryPage; } } IWizardPage nextPage = super.getNextPage(page); if (nextPage instanceof SummaryPage) { SummaryPage summary = (SummaryPage) nextPage; DirectoryPage dirPage = getDirectoryPage(); summary.updateText(dirPage.useDefaultDirectory() ? "Using default directory" : "Using directory: " + getChoosePage().getDirectory()); } return nextPage; } } static class MyComposite5 extends Composite { public MyComposite5(Composite parent) { super(parent, SWT.NONE); setLayout(new MigLayout("", "[grow, fill]", "[grow, fill]")); Text text = createTextArea(); text.setLayoutData("grow"); } private Text createTextArea() { Text text = new Text(this, SWT.MULTI | SWT.BORDER); DropTarget target = new DropTarget(text, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT); target.setTransfer(new Transfer[] { FileTransfer.getInstance() }); target.addDropListener(new DropTargetAdapter() { @Override public void drop(DropTargetEvent event) { DropTarget target = (DropTarget) event.widget; Text t = (Text) target.getControl(); String[] fileList = (String[]) event.data; t.setText(read(fileList[0])); } }); return text; } private String read(String path) { String text = ""; BufferedReader br = null; try { File file = new File(path); br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { text += line + System.getProperty("line.separator"); } int lastIndex = text.length() - System.getProperty("line.separator").length(); text = text.substring(0, lastIndex); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return text; } } public static void show() { SWTApp app = new SWTApp(); app.setBlockOnOpen(true); app.open(); Display.getCurrent().dispose(); } }
Wednesday, May 2, 2012
Getting Started with SWT and JFace
Setting up a development environment for SWT/JFace can be pretty difficult because most of the libraries needed are not available in Maven repository. Even if they are available in Maven repository, they are not always up to date. In this blog, I'm going to show you how to setup a development environment for SWT and JFace. This example uses Gradle and Eclipse 3.7
The easiest way to get the libraries needed for SWT/JFace is to copy those libraries from Eclipse plugins directory. For SWT, there are only two libraries required, the SWT library and the SWT native platform library. For JFace, we need the JFace libraries, Workbench libraries, Equinox library, and Command library. The example of build.gradle is shown below.
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' repositories { flatDir { dir 'lib' } } dependencies { // swt dependencies compile name: "org.eclipse.swt_3.7.2.v3740f" compile name: "org.eclipse.swt.gtk.linux.x86_3.7.2.v3740f" // jface dependencies compile name: "org.eclipse.jface_3.7.0.v20110928-1505" compile name: "org.eclipse.jface.text_3.7.2.v20111213-1208" compile name: "org.eclipse.jface.databinding_1.5.0.I20100907-0800" compile name: "org.eclipse.ui.workbench_3.7.1.v20120104-1859" compile name: "org.eclipse.ui.workbench.texteditor_3.7.0.v20110928-1504" compile name: "org.eclipse.equinox.common_3.6.0.v20110523" compile name: "org.eclipse.core.commands_3.6.0.I20110111-0800" }
SWTApp.java
package swtproject; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class SWTApp { public static void show() { Display display = new Display(); Shell shell = new Shell(display); Text helloText = new Text(shell, SWT.CENTER); helloText.setText("Hello SWT"); helloText.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }
JFaceApp.java
package swtproject; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Text; public class JFaceApp extends ApplicationWindow { public JFaceApp() { super(null); } protected Control createContents(Composite parent) { Text helloText = new Text(parent, SWT.CENTER); helloText.setText("Hello JFace"); parent.pack(); return parent; }; public static void show() { JFaceApp app = new JFaceApp(); app.setBlockOnOpen(true); app.open(); Display.getCurrent().dispose(); } }
Main.java
package swtproject; public class Main { public static void main(String[] args) { SWTApp.show(); JFaceApp.show(); } }
Subscribe to:
Posts (Atom)