| <pre> |
| /******************************************************************************* |
| * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, |
| * Canada. All rights reserved. This program and the accompanying materials are |
| * made available under the terms of the Eclipse Public License v1.0 which |
| * accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: The Chisel Group, University of Victoria |
| ******************************************************************************/ |
| package org.eclipse.mylar.zest.examples.swt; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.draw2d.ColorConstants; |
| import org.eclipse.mylar.zest.core.widgets.Graph; |
| import org.eclipse.mylar.zest.core.widgets.GraphConnection; |
| import org.eclipse.mylar.zest.core.widgets.GraphItem; |
| import org.eclipse.mylar.zest.core.widgets.GraphNode; |
| import org.eclipse.mylar.zest.core.widgets.ZestStyles; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.KeyAdapter; |
| import org.eclipse.swt.events.KeyEvent; |
| import org.eclipse.swt.events.PaintEvent; |
| import org.eclipse.swt.events.PaintListener; |
| import org.eclipse.swt.graphics.Font; |
| import org.eclipse.swt.graphics.FontData; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.graphics.Region; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Shell; |
| |
| /** |
| * This snippet shows how you can add a paint listener to a Zest graph to paint on top of |
| * the widget. This snippet allows you to type and it selects all the nodes that match |
| * what you type. |
| * |
| * @author Ian Bull |
| * |
| */ |
| public class GraphSnippet5 { |
| public static final int BACKSPACE = 8; |
| public static final int ENTER = 13; |
| |
| /** |
| * @param args |
| */ |
| public static void main(String[] args) { |
| final Map figureListing = new HashMap(); |
| final StringBuffer stringBuffer = new StringBuffer(); |
| final Display d = new Display(); |
| FontData fontData = d.getSystemFont().getFontData()[0]; |
| fontData.height = 42; |
| |
| final Font font = new Font(d, fontData); |
| |
| Shell shell = new Shell(d); |
| shell.setText("Graph Snippet 4"); |
| Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION); |
| Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING); |
| Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR); |
| shell.setLayout(new FillLayout()); |
| shell.setSize(400, 400); |
| |
| final Graph g = new Graph(shell, SWT.NONE); |
| g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED); |
| GraphNode n1 = new GraphNode(g, SWT.NONE, "org.eclipse.Information", image1); |
| GraphNode n2 = new GraphNode(g, SWT.NONE, "org.eclipse.Warning", image2); |
| GraphNode n3 = new GraphNode(g, SWT.NONE, "org.eclipse.Error", image3); |
| figureListing.put(n1.getText().toLowerCase(), n1); |
| figureListing.put(n2.getText().toLowerCase(), n2); |
| figureListing.put(n3.getText().toLowerCase(), n3); |
| |
| new GraphConnection(g, SWT.NONE, n1, n2); |
| new GraphConnection(g, SWT.NONE, n2, n3); |
| n1.setLocation(10, 10); |
| n2.setLocation(200, 10); |
| n3.setLocation(200, 200); |
| |
| g.addKeyListener(new KeyAdapter() { |
| |
| public void keyPressed(KeyEvent e) { |
| boolean complete = false; |
| if ( e.keyCode == BACKSPACE ) { |
| if ( stringBuffer.length() > 0 ) { |
| stringBuffer.deleteCharAt(stringBuffer.length()-1); |
| } |
| } |
| else if ( e.keyCode == ENTER ) { |
| complete = true; |
| } |
| else if ( ( e.character >= 'a' && e.character <= 'z') || |
| ( e.character >= 'A' && e.character <= 'Z') || |
| ( e.character == '.' ) || |
| ( e.character >= '0' && e.character <='9') ) { |
| stringBuffer.append(e.character); |
| } |
| Iterator iterator = figureListing.keySet().iterator(); |
| List list = new ArrayList(); |
| if ( stringBuffer.length()> 0) { |
| while(iterator.hasNext()) { |
| String string = (String) iterator.next(); |
| if ( string.contains(stringBuffer.toString().toLowerCase()) ) { |
| list.add(figureListing.get(string)); |
| } |
| } |
| } |
| g.setSelection((GraphItem[]) list.toArray(new GraphItem[list.size()])); |
| if ( complete && stringBuffer.length() > 0) { |
| stringBuffer.delete(0, stringBuffer.length()); |
| } |
| |
| g.redraw(); |
| } |
| |
| }); |
| |
| g.addPaintListener(new PaintListener() { |
| public void paintControl(PaintEvent e) { |
| e.gc.setFont(font); |
| e.gc.setClipping((Region)null); |
| e.gc.setForeground(ColorConstants.lightGray); |
| e.gc.drawText(stringBuffer.toString(), 50, 50, true); |
| } |
| }); |
| |
| shell.open(); |
| while (!shell.isDisposed()) { |
| while (!d.readAndDispatch()) { |
| d.sleep(); |
| } |
| } |
| image1.dispose(); |
| image2.dispose(); |
| image3.dispose(); |
| font.dispose(); |
| } |
| } |
| </pre> |