Skip to content

Commit

Permalink
Merge pull request #867 from gmeiser/master
Browse files Browse the repository at this point in the history
implemented an autocomplete feature for the "move page" (version 2)
  • Loading branch information
amolenaar committed Feb 12, 2016
2 parents e685484 + 2c1944e commit 52f7ca8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/fitnesse/resources/templates/refactorForm.vm
Expand Up @@ -14,9 +14,9 @@
<input type="submit" name="replace" value="Replace!"/>
<a class="button" href="$viewLocation">Cancel</a>
</fieldset>

<p><strong>Search &amp; Replace: </strong>
Please note that this feature is experimental! It uses java-based regular expressions. For an introduction, take a look <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" target="_new">here</a> (new window).
Please note that this feature is experimental! It uses java-based regular expressions. For an introduction, take a look <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" target="_new">here</a> (new window).
</form>
#end

Expand Down Expand Up @@ -56,7 +56,12 @@
<input type="hidden" name="responder" value="movePage"/>
<fieldset>
<label for="newLocation">New Location:</label>
<input type="text" name="newLocation" value="" size="80" class="wikipath" />
<input type="text" name="newLocation" value="" size="80" class="wikipath" list="suites" placeholder="enter first two chars of suite name here" autocomplete="off" />
<datalist id="suites">
#foreach ($suiteEntry in $suiteMap)
<option value="#escape($suiteEntry)">
#end
</datalist>
</fieldset>
<fieldset>
<label class="checkbox" for="refactorReferences_move"><input type="checkbox" id="refactorReferences_move" name="refactorReferences"/>Find all references to this page and change them accordingly (May take several minutes)</label>
Expand All @@ -66,4 +71,4 @@
<a class="button" href="$viewLocation">Cancel</a>
</fieldset>
</form>
#end
#end
96 changes: 96 additions & 0 deletions src/fitnesse/responders/AutoCompleteUtil.java
@@ -0,0 +1,96 @@
package fitnesse.responders;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

import fitnesse.FitNesseContext;
import fitnesse.wiki.PageCrawler;
import fitnesse.wiki.PathParser;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikiPagePath;

public class AutoCompleteUtil {

// create tree structure with suites or testcases
private static List<String> createTree(List<String> tree, FitNesseContext context, String root, String type) {
PageCrawler crawler = context.getRootPage().getPageCrawler();
WikiPagePath RootPath = PathParser.parse(root);
WikiPage RootPage = crawler.getPage(RootPath);

if (RootPage != null) {
// only create tree if there are nodes
if (RootPage.getChildren().size()>0) {
// iterate through all childs of the root page
List<WikiPage> NodeList = RootPage.getChildren();

WikiPage CurrentNode;
for (int i = 0; i < NodeList.size(); i++) {
CurrentNode = NodeList.get(i);
String currentNodePath = getPathOfCurrentElement(CurrentNode.toString());
// only get suites or testcases
if (CurrentNode.getData().hasAttribute("Suite") || CurrentNode.getData().hasAttribute("Test")) {
// if CurrentNode is suite
if (CurrentNode.getChildren().size() > 0) {
// get suites only if requested type is "suite"
if (CurrentNode.getData().hasAttribute("Suite") && type.equals("Suite")) {
tree.add(currentNodePath);
}
// recursive call for suites that are laying deeper in the tree
createTree(tree, context, currentNodePath, type);
}
else {
// if CurrentNode has no children and it is a test, then final test node found
if (CurrentNode.getData().hasAttribute("Test") && type.equals("Test")) {
tree.add(currentNodePath);
}
// if CurrentNode has no children and it is a suite, then final suite node found
if (CurrentNode.getData().hasAttribute("Suite") && type.equals("Suite")) {
tree.add(currentNodePath);
}
}
}
}
}
}
return tree;
}
// modify path string to usable form
private static String getPathOfCurrentElement (String currentNodeToString) {
// needs Node.toString() as input
String[] parts = currentNodeToString.split("\\.");
// only take the path part
String path = parts[(parts.length-1)];
// cut the first 14 chars
path = path.substring(14);
// cut the last 7 chars
path = path.substring(0,path.length()-7);
// replace \ by .
if (path.contains("\\")) {
path = path.replace("\\", ".");
}
return path;
}



public static List<String> createTestList(FitNesseContext context) {
List<String> TestList = new ArrayList<String>();
TestList = createTree(TestList, context, "/", "Test");
if ((TestList.size() > 0)) {
return TestList;
}
else return null;
}

public static List<String> createSuiteList(FitNesseContext context) {
List<String> SuiteList = new ArrayList<String>();
SuiteList = createTree(SuiteList, context, "/", "Suite");
if ((SuiteList.size() > 0)) {
return SuiteList;
}
else return null;
}
}
Expand Up @@ -15,6 +15,7 @@
import fitnesse.wiki.PathParser;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikiPagePath;
import fitnesse.responders.AutoCompleteUtil;

public class RefactorPageResponder implements SecureResponder {

Expand All @@ -31,7 +32,7 @@ public Response makeResponse(FitNesseContext context, Request request) {
tags = pageData.getAttribute(PageData.PropertySUITES);
}
}

HtmlPage page = context.pageFactory.newPage();

page.setMainTemplate("refactorForm");
Expand All @@ -41,6 +42,7 @@ public Response makeResponse(FitNesseContext context, Request request) {
page.put("request", request);
page.put("type", request.getInput("type"));
page.put("viewLocation", request.getResource());
page.put("suiteMap", AutoCompleteUtil.createSuiteList(context));
SimpleResponse response = new SimpleResponse();
response.setContent(page.html());
return response;
Expand Down

0 comments on commit 52f7ca8

Please sign in to comment.