FileDocCategorySizeDatePackage
LinkArea.javaAPI DocAzureus 3.0.3.45270Mon Jul 02 14:00:24 BST 2007org.gudy.azureus2.ui.swt.components

LinkArea

public class LinkArea extends Object

Fields Summary
private org.eclipse.swt.custom.StyledText
styled_text
private ArrayList
links
private int
ofs
private String
relative_url_base
Constructors Summary
public LinkArea(org.eclipse.swt.widgets.Composite comp)

	
	
	
			 
	
		styled_text = new StyledText(comp,SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);    
		styled_text.setWordWrap(true);

		styled_text.addListener(SWT.MouseUp, new Listener() {
			public void handleEvent(Event event) {
				if (links.size() == 0) {
					return;
				}
				try {
					int ofs = styled_text.getOffsetAtLocation(new Point(event.x, event.y));
					for (int i = 0; i < links.size(); i++) {
						linkInfo linkInfo = (linkInfo)links.get(i);
						if (ofs >= linkInfo.ofsStart && ofs <= linkInfo.ofsEnd) {
							Utils.launch(linkInfo.url);
							break;
						}
					}
				} catch (Exception e) {

				}
			}
		});

		final Cursor handCursor = new Cursor(comp.getDisplay(), SWT.CURSOR_HAND);
		styled_text.addListener(SWT.MouseMove, new Listener() {
			Cursor curCursor = null;

			public void handleEvent(Event event) {
				if (links.size() == 0) {
					return;
				}
				boolean onLink = false;
				try {
					int ofs = styled_text.getOffsetAtLocation(new Point(event.x, event.y));
					for (int i = 0; i < links.size(); i++) {
						linkInfo linkInfo = (linkInfo)links.get(i);
						if (ofs >= linkInfo.ofsStart && ofs <= linkInfo.ofsEnd) {
							onLink = true;
							break;
						}
					}
				} catch (Exception e) {

				}

				try {
					Cursor cursor = onLink ? handCursor : null;
					if (curCursor != cursor) {
						styled_text.setCursor(cursor);
						curCursor = cursor;
					}
				} catch (Exception e) {

				}
			}
		});

		styled_text.addListener(SWT.Dispose, new Listener() {
			public void handleEvent(Event event) {
				styled_text.setCursor(null);
				handCursor.dispose();
			}
		});
	
Methods Summary
public voidaddLine(java.lang.String line)

		if( styled_text.isDisposed()){
			return;
		}

		try{
			line = HTMLUtils.expand( line );
			
			Object[]	url_details = HTMLUtils.getLinks( line );
			
			String	modified_line = (String)url_details[0];
			
			styled_text.append(modified_line + "\n");
	
			List	urls = (List)url_details[1];
			
			for (int i=0;i<urls.size();i++){
				Object[]	entry = (Object[])urls.get(i);
				
				String	url = (String)entry[0];
				
				int[]	det = (int[])entry[1];
				
				if ( !url.toLowerCase().startsWith("http") && relative_url_base.length() > 0 ){
					
					url = relative_url_base + url;
				}
				
				linkInfo info = new linkInfo(ofs + det[0], ofs + det[0] + det[1], url );
	
				links.add(info);
	
				StyleRange sr = new StyleRange();
				sr.start = info.ofsStart;
				sr.length = info.ofsEnd - info.ofsStart;
				sr.underline = true;
				sr.foreground = Colors.blue;
				
				styled_text.setStyleRange(sr);
			}
		
			ofs += modified_line.length() + 1;
			
		}catch( Throwable e ){
			
				// just in case something borks
			
			Debug.printStackTrace( e );
			
			styled_text.append(line + "\n");
		}
	
public org.eclipse.swt.widgets.CompositegetComponent()

		return( styled_text );
	
public voidreset()

		if( styled_text.isDisposed()){
			return;
		}

		ofs = 0;
		styled_text.setText("");
		
		links.clear();
	
public voidsetRelativeURLBase(java.lang.String str)

		relative_url_base = str;