Title: [jbehave] [596] trunk/plugins/eclipse: [FL] Improved class names.

Diff

Modified: trunk/plugins/eclipse/plugin.xml (595 => 596)

--- trunk/plugins/eclipse/plugin.xml	2006-11-30 11:53:58 UTC (rev 595)
+++ trunk/plugins/eclipse/plugin.xml	2006-11-30 12:16:26 UTC (rev 596)
@@ -101,11 +101,11 @@
    <extension
          point="org.eclipse.ui.editors">
       <editor
-            class="jbehave.plugin.eclipse.editors.BehaviourEditor"
+            class="jbehave.plugin.eclipse.editors.StoryEditor"
             contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"
             extensions="story"
             icon="icons/behave.gif"
-            id="jbehave.plugin.eclipse.editors.BehaviourEditor"
+            id="jbehave.plugin.eclipse.editors.StoryEditor"
             name="Behaviour Editor"/>
    </extension>
    <extension
@@ -114,7 +114,7 @@
          point="org.eclipse.ui.editorActions">
       <editorContribution
             id="jbehave-eclipse.behaviourEditorContribution"
-            targetID="jbehave.plugin.eclipse.editors.BehaviourEditor">
+            targetID="jbehave.plugin.eclipse.editors.StoryEditor">
          <action
                class="jbehave.plugin.eclipse.actions.NavigateToJavaSource"
                id="jbehave-eclipse.navigate-source"

Modified: trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/actions/NavigateToJavaSource.java (595 => 596)

--- trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/actions/NavigateToJavaSource.java	2006-11-30 11:53:58 UTC (rev 595)
+++ trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/actions/NavigateToJavaSource.java	2006-11-30 12:16:26 UTC (rev 596)
@@ -1,11 +1,10 @@
 package jbehave.plugin.eclipse.actions;
 
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
 import java.util.List;
 
-import jbehave.plugin.eclipse.editors.BehaviourEditor;
+import jbehave.plugin.eclipse.editors.StoryEditor;
+import jbehave.plugin.eclipse.model.StoryLine;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jdt.core.IType;
@@ -17,7 +16,6 @@
 import org.eclipse.jdt.core.search.SearchParticipant;
 import org.eclipse.jdt.core.search.SearchPattern;
 import org.eclipse.jdt.core.search.SearchRequestor;
-import org.eclipse.jdt.core.search.TypeNameRequestor;
 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.text.BadLocationException;
@@ -26,31 +24,20 @@
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.ui.IEditorActionDelegate;
 import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.INavigationLocation;
-import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 import org.eclipse.ui.texteditor.TextSelectionNavigationLocation;
 
 public class NavigateToJavaSource implements IEditorActionDelegate {
 
-	List keyWords=Arrays.asList(new String[]{"title:","scenario:","when","then","given"});
-	private BehaviourEditor editor;
+	
+	private StoryEditor editor;
 
 	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
-		this.editor=(BehaviourEditor)targetEditor;
+		this.editor=(StoryEditor)targetEditor;
 		
 	}
 	
-	 public static String capitalize(String str) {
-	        int strLen;
-	        if (str == null || (strLen = str.length()) == 0) {
-	            return str;
-	        }
-	        return new StringBuffer(strLen)
-	            .append(Character.toTitleCase(str.charAt(0)))
-	            .append(str.substring(1))
-	            .toString();
-	    }
+	
 
 
 	public void run(IAction action) {
@@ -61,42 +48,18 @@
 		int line=selection.getStartLine();
 		try {
 			String text=document.get(document.getLineOffset(line),document.getLineLength(line));
-			processLine(text);
+			jbehave.plugin.eclipse.model.StoryLine storyLine = StoryLine.parseLine(text);
+			if (storyLine!=null){
+				findAndOpenClass(storyLine.asClassName());
+			}
 		} catch (BadLocationException e) {
 			e.printStackTrace();
 		}
 	}
 
-	private void processLine(String line) {
-		line=line.replaceAll("\n", "");
-		line=line.replaceAll("\r", "");
-		List words=new ArrayList(Arrays.asList(line.split(" ")));
-		if (words.size()<2)
-			return;
-		
-		if (((String)words.get(0)).equalsIgnoreCase("and")){
-			words.remove(0);
-			if (words.size()<2)
-				return;
-		}
-		
-		if (keyWords.contains(((String)words.get(0)).toLowerCase())){	
-			words.remove(0);
-			String className = renderClassName(words);
-			findAndOpenClass(className);
-		}
-		
-	}
+	
 
-	private String renderClassName(List words) {
-		String className="";
-		for (Iterator iter = words.iterator(); iter.hasNext();) {
-			String word = (String) iter.next();
-			className+=capitalize(word);				
-		}
-		System.out.println("Class: '"+className+"'");
-		return className;
-	}
+	
 
 	private void findAndOpenClass(String className) {
 		try {

Deleted: trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/BehaviourEditor.java (595 => 596)

--- trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/BehaviourEditor.java	2006-11-30 11:53:58 UTC (rev 595)
+++ trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/BehaviourEditor.java	2006-11-30 12:16:26 UTC (rev 596)
@@ -1,17 +0,0 @@
-package jbehave.plugin.eclipse.editors;
-
-import org.eclipse.ui.editors.text.TextEditor;
-
-public class BehaviourEditor extends TextEditor {
-
-	
-	public BehaviourEditor() {
-		super();
-		
-	}
-	public void dispose() {
-	
-		super.dispose();
-	}
-
-}

Added: trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryContentOutlinePage.java (0 => 596)

--- trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryContentOutlinePage.java	                        (rev 0)
+++ trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryContentOutlinePage.java	2006-11-30 12:16:26 UTC (rev 596)
@@ -0,0 +1,241 @@
+package jbehave.plugin.eclipse.editors;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.BadPositionCategoryException;
+import org.eclipse.jface.text.DefaultPositionUpdater;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IPositionUpdater;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
+
+public class StoryContentOutlinePage  extends ContentOutlinePage{
+
+
+	/**
+	 * A segment element.
+	 */
+	protected static class Segment {
+		public String name;
+		public Position position;
+
+		public Segment(String name, Position position) {
+			this.name= name;
+			this.position= position;
+		}
+
+		public String toString() {
+			return name;
+		}
+	}
+
+	/**
+	 * Divides the editor's document into ten segments and provides elements for them.
+	 */
+	protected class ContentProvider implements ITreeContentProvider {
+
+		protected final static String SEGMENTS= "__java_segments"; //$NON-NLS-1$
+		protected IPositionUpdater fPositionUpdater= new DefaultPositionUpdater(SEGMENTS);
+		protected List fContent= new ArrayList(10);
+
+		protected void parse(IDocument document) {
+
+			int lines= document.getNumberOfLines();
+						
+			for (int line= 0; line < lines; line += 1) {
+				int offset;
+				try {
+
+					offset = document.getLineOffset(line);
+					int length=document.getLineLength(line);
+				
+					String text=document.get(offset, length);
+					jbehave.plugin.eclipse.model.StoryLine storyLine = jbehave.plugin.eclipse.model.StoryLine.parseLine(text);
+					if ((storyLine!=null)){
+						if (storyLine.getKeyWord().equals("Scenario:")){
+						Position p= new Position(offset, length);
+						document.addPosition(SEGMENTS, p);
+						fContent.add(new Segment(storyLine.asText(), p)); //$NON-NLS-1$
+						}
+					}
+					
+				} catch (BadPositionCategoryException x) {
+				} catch (BadLocationException x) {
+				}
+			}
+		}
+
+		/*
+		 * @see IContentProvider#inputChanged(Viewer, Object, Object)
+		 */
+		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+			if (oldInput != null) {
+				IDocument document= fDocumentProvider.getDocument(oldInput);
+				if (document != null) {
+					try {
+						document.removePositionCategory(SEGMENTS);
+					} catch (BadPositionCategoryException x) {
+					}
+					document.removePositionUpdater(fPositionUpdater);
+				}
+			}
+
+			fContent.clear();
+
+			if (newInput != null) {
+				IDocument document= fDocumentProvider.getDocument(newInput);
+				if (document != null) {
+					document.addPositionCategory(SEGMENTS);
+					document.addPositionUpdater(fPositionUpdater);
+
+					parse(document);
+				}
+			}
+		}
+
+		/*
+		 * @see IContentProvider#dispose
+		 */
+		public void dispose() {
+			if (fContent != null) {
+				fContent.clear();
+				fContent= null;
+			}
+		}
+
+		/*
+		 * @see IContentProvider#isDeleted(Object)
+		 */
+		public boolean isDeleted(Object element) {
+			return false;
+		}
+
+		/*
+		 * @see IStructuredContentProvider#getElements(Object)
+		 */
+		public Object[] getElements(Object element) {
+			return fContent.toArray();
+		}
+
+		/*
+		 * @see ITreeContentProvider#hasChildren(Object)
+		 */
+		public boolean hasChildren(Object element) {
+			return element == fInput;
+		}
+
+		/*
+		 * @see ITreeContentProvider#getParent(Object)
+		 */
+		public Object getParent(Object element) {
+			if (element instanceof Segment)
+				return fInput;
+			return null;
+		}
+
+		/*
+		 * @see ITreeContentProvider#getChildren(Object)
+		 */
+		public Object[] getChildren(Object element) {
+			if (element == fInput)
+				return fContent.toArray();
+			return new Object[0];
+		}
+	}
+
+	protected Object fInput;
+	protected IDocumentProvider fDocumentProvider;
+	protected ITextEditor fTextEditor;
+
+	/**
+	 * Creates a content outline page using the given provider and the given editor.
+	 * 
+	 * @param provider the document provider
+	 * @param editor the editor
+	 */
+	public StoryContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
+		super();
+		fDocumentProvider= provider;
+		fTextEditor= editor;
+	}
+	
+	/* (non-Javadoc)
+	 * Method declared on ContentOutlinePage
+	 */
+	public void createControl(Composite parent) {
+
+		super.createControl(parent);
+
+		TreeViewer viewer= getTreeViewer();
+		viewer.setContentProvider(new ContentProvider());
+		viewer.setLabelProvider(new LabelProvider());
+		viewer.addSelectionChangedListener(this);
+
+		if (fInput != null)
+			viewer.setInput(fInput);
+	}
+	
+	/* (non-Javadoc)
+	 * Method declared on ContentOutlinePage
+	 */
+	public void selectionChanged(SelectionChangedEvent event) {
+
+		super.selectionChanged(event);
+
+		ISelection selection= event.getSelection();
+		if (selection.isEmpty())
+			fTextEditor.resetHighlightRange();
+		else {
+			Segment segment= (Segment) ((IStructuredSelection) selection).getFirstElement();
+			int start= segment.position.getOffset();
+			int length= segment.position.getLength();
+			try {
+				fTextEditor.setHighlightRange(start, length, true);
+			} catch (IllegalArgumentException x) {
+				fTextEditor.resetHighlightRange();
+			}
+		}
+	}
+	
+	/**
+	 * Sets the input of the outline page
+	 * 
+	 * @param input the input of this outline page
+	 */
+	public void setInput(Object input) {
+		fInput= input;
+		update();
+	}
+	
+	/**
+	 * Updates the outline page.
+	 */
+	public void update() {
+		TreeViewer viewer= getTreeViewer();
+
+		if (viewer != null) {
+			Control control= viewer.getControl();
+			if (control != null && !control.isDisposed()) {
+				control.setRedraw(false);
+				viewer.setInput(fInput);
+				viewer.expandAll();
+				control.setRedraw(true);
+			}
+		}
+	}
+}
+
+

Copied: trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryEditor.java (from rev 594, trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/BehaviourEditor.java) (0 => 596)

--- trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryEditor.java	                        (rev 0)
+++ trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/editors/StoryEditor.java	2006-11-30 12:16:26 UTC (rev 596)
@@ -0,0 +1,33 @@
+package jbehave.plugin.eclipse.editors;
+
+import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
+
+public class StoryEditor extends TextEditor {
+
+	private StoryContentOutlinePage fOutlinePage;
+
+	public StoryEditor() {
+		super();
+		
+	}
+	public void dispose() {
+	
+		super.dispose();
+	}
+	
+	public Object getAdapter(Class required) {
+		if (IContentOutlinePage.class.equals(required)) {
+			if (fOutlinePage == null) {
+				fOutlinePage= new StoryContentOutlinePage(getDocumentProvider(), this);
+				if (getEditorInput() != null)
+					fOutlinePage.setInput(getEditorInput());
+			}
+			return fOutlinePage;
+		}
+		
+		
+		return super.getAdapter(required);
+	}
+
+}

Added: trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/model/StoryLine.java (0 => 596)

--- trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/model/StoryLine.java	                        (rev 0)
+++ trunk/plugins/eclipse/src/main/java/jbehave/plugin/eclipse/model/StoryLine.java	2006-11-30 12:16:26 UTC (rev 596)
@@ -0,0 +1,93 @@
+package jbehave.plugin.eclipse.model;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+public class StoryLine {
+    /** represents a parsed line of a story. Instances are returned by #parseLine */
+
+
+    static List keyWords=Arrays.asList(new String[]{"title:","scenario:","when","then","given"});
+	List words;
+	private String keyWord;
+	private StoryLine(String keyWord, List words) {
+		this.words=words;
+		this.keyWord=keyWord;
+	}
+	
+	
+
+	
+	public String getKeyWord() {
+		return keyWord;
+	}
+
+
+
+
+	public void setKeyWord(String keyWord) {
+		this.keyWord = keyWord;
+	}
+
+
+
+    /** returns null if line is not valid story line*/
+
+    public static  StoryLine parseLine(String line) {
+		line=line.replaceAll("\n", "");
+		line=line.replaceAll("\r", "");
+		List words=new ArrayList(Arrays.asList(line.split(" ")));
+		if (words.size()<2)
+			return null;
+		
+		if (((String)words.get(0)).equalsIgnoreCase("and")){
+			words.remove(0);
+			if (words.size()<2)
+				return null;
+		}
+		
+		if (keyWords.contains(((String)words.get(0)).toLowerCase())){	
+			String kw=(String) words.get(0);
+			words.remove(0);
+			return new StoryLine(kw,words);
+			
+		}
+		return null;
+		
+	}
+
+	
+	public String asText(){
+		StringBuffer returnValue=new StringBuffer();
+		for (Iterator iter = words.iterator(); iter.hasNext();) {
+			String element = (String) iter.next();
+			returnValue.append(element);
+			if (iter.hasNext())
+				returnValue.append(' ');
+		}
+		return returnValue.toString();
+	}
+	
+	public String asClassName() {
+		String className="";
+		for (Iterator iter = words.iterator(); iter.hasNext();) {
+			String word = (String) iter.next();
+			className+=capitalize(word);				
+		}
+		System.out.println("Class: '"+className+"'");
+		return className;
+	}
+	
+	 public static String capitalize(String str) {
+	        int strLen;
+	        if (str == null || (strLen = str.length()) == 0) {
+	            return str;
+	        }
+	        return new StringBuffer(strLen)
+	            .append(Character.toTitleCase(str.charAt(0)))
+	            .append(str.substring(1))
+	            .toString();
+	    }
+}


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to