eclipse plugin: order tags/children by cardinality,
...showing first the required tags/keys
This commit is contained in:
parent
33f1cf9ad3
commit
3e4467e951
3 changed files with 78 additions and 22 deletions
|
@ -9,6 +9,7 @@
|
|||
package wesnoth_eclipse_plugin.schema;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Stack;
|
||||
|
||||
|
@ -42,7 +43,6 @@ public class SchemaParser
|
|||
*/
|
||||
public void parseSchema(boolean force)
|
||||
{
|
||||
//TODO: sort tags's keys by cardinality (required first) ??
|
||||
if (parsingDone_ && !force)
|
||||
{
|
||||
Logger.getInstance().log("schema not parsed since there is already in cache.");
|
||||
|
@ -201,29 +201,36 @@ public class SchemaParser
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
sortTags();
|
||||
Logger.getInstance().log("parsing done");
|
||||
parsingDone_ = true;
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// BufferedWriter bw = new BufferedWriter(new PrintWriter(new File("D:\\timo\\gw\\data\\schema-out.cfg")));
|
||||
// // print primitives
|
||||
// for (Entry<String, String> primitive : primitives.entrySet())
|
||||
// {
|
||||
// bw.write(primitive.getKey() + ": " + primitive.getValue() + "\n");
|
||||
// }
|
||||
// // print tags
|
||||
// Tag root = tags.get("root");
|
||||
// for (Tag tag : root.TagChildren)
|
||||
// {
|
||||
// bw.write(getOutput(tag, ""));
|
||||
// }
|
||||
// bw.close();
|
||||
// } catch (Exception e)
|
||||
// {
|
||||
// Logger.getInstance().logException(e);
|
||||
// }
|
||||
// System.out.println("End writing result");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the tags in the hashmap
|
||||
*/
|
||||
private void sortTags()
|
||||
{
|
||||
for(Tag tag : tags_.values())
|
||||
{
|
||||
sortChildren(tag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts all tag's children by using the cardinality comparator
|
||||
* @param tag
|
||||
*/
|
||||
private void sortChildren(Tag tag)
|
||||
{
|
||||
Collections.sort(tag.TagChildren, new Tag.CardinalityComparator());
|
||||
Collections.sort(tag.KeyChildren, new TagKey.CardinalityComparator());
|
||||
|
||||
for (Tag childTag : tag.TagChildren)
|
||||
{
|
||||
sortChildren(childTag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
package wesnoth_eclipse_plugin.schema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -30,6 +31,7 @@ public class Tag
|
|||
TagChildren = tagChildren;
|
||||
KeyChildren = keyChildren;
|
||||
Cardinality = cardinality;
|
||||
safeInit();
|
||||
}
|
||||
|
||||
public Tag(String name, char cardinality) {
|
||||
|
@ -38,6 +40,7 @@ public class Tag
|
|||
TagChildren = new ArrayList<Tag>();
|
||||
KeyChildren = new ArrayList<TagKey>();
|
||||
Cardinality = cardinality;
|
||||
safeInit();
|
||||
}
|
||||
|
||||
public Tag(String name, String extendedTagName, char cardinality) {
|
||||
|
@ -46,6 +49,15 @@ public class Tag
|
|||
TagChildren = new ArrayList<Tag>();
|
||||
KeyChildren = new ArrayList<TagKey>();
|
||||
Cardinality = cardinality;
|
||||
safeInit();
|
||||
}
|
||||
|
||||
private void safeInit()
|
||||
{
|
||||
if (TagChildren == null)
|
||||
TagChildren = new ArrayList<Tag>();
|
||||
if (KeyChildren == null)
|
||||
KeyChildren = new ArrayList<TagKey>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,4 +90,22 @@ public class Tag
|
|||
{
|
||||
addKey(new TagKey(name, cardinality, valueType, translatable));
|
||||
}
|
||||
|
||||
/**
|
||||
* A tag comparator that sorts just after required cardinality.
|
||||
*/
|
||||
public static class CardinalityComparator implements Comparator<Tag>
|
||||
{
|
||||
@Override
|
||||
public int compare(Tag o1, Tag o2)
|
||||
{
|
||||
if (o1.Cardinality == o2.Cardinality)
|
||||
return 0;
|
||||
if (o1.Cardinality == '1')
|
||||
return 1;
|
||||
else if (o2.Cardinality == '1')
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
*******************************************************************************/
|
||||
package wesnoth_eclipse_plugin.schema;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* This class represents a tag's key parsed by the schema
|
||||
*/
|
||||
|
@ -45,4 +47,21 @@ public class TagKey
|
|||
IsTranslatable = trans;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A tag comparator that sorts just after required cardinality.
|
||||
*/
|
||||
public static class CardinalityComparator implements Comparator<TagKey>
|
||||
{
|
||||
@Override
|
||||
public int compare(TagKey o1, TagKey o2)
|
||||
{
|
||||
if (o1.Cardinality == o2.Cardinality)
|
||||
return 0;
|
||||
if (o1.Cardinality == '1')
|
||||
return -1;
|
||||
else if (o2.Cardinality == '1')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue