com.obdobion.howto.Outline Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of howto Show documentation
Show all versions of howto Show documentation
HowTo menuing system for plugins
package com.obdobion.howto;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import com.obdobion.howto.writer.IOutlineWriter;
import com.obdobion.howto.writer.OutlineWriters;
/**
*
* Outline class.
*
*
* @author Chris DeGreef [email protected]
*/
public class Outline
{
Config config;
IOutlineWriter writer;
String contents;
List children;
Outline(final Config config)
{
super();
this.config = config;
writer = OutlineWriters.create(config);
}
Outline(final Outline parentOutline)
{
super();
config = parentOutline.config;
writer = parentOutline.writer;
}
/**
*
* add.
*
*
* @param childContents a {@link java.lang.String} object.
* @param childArguments a {@link java.lang.Object} object.
* @return a {@link com.obdobion.howto.Outline} object.
*/
public Outline add(final String childContents, final Object... childArguments)
{
if (children == null)
children = new ArrayList<>();
final Outline child = new Outline(this);
child.setContents(childContents, childArguments);
children.add(child);
return child;
}
/**
*
* getCurrent.
*
*
* @return a {@link com.obdobion.howto.Outline} object.
*/
public Outline getCurrent()
{
if (children == null || children.size() == 0)
return this;
return children.get(children.size() - 1).getCurrent();
}
/**
*
* Getter for the field writer
.
*
*
* @return a {@link com.obdobion.howto.writer.IOutlineWriter} object.
*/
public IOutlineWriter getWriter()
{
return writer;
}
/**
*
* print.
*
*
* @param context a {@link com.obdobion.howto.Context} object.
*/
public void print(final Context context)
{
if (context.isSubcontext())
return;
// try (IOutlineWriter ow = OutlineWriters.create(config))
// {
print(getWriter());
//
// } catch (final IOException e)
// {
// logger.error("attempting to print outline", e);
// }
}
void print(final IOutlineWriter ow)
{
if (contents != null)
ow.append(contents.trim(), 0);
if (children != null)
{
ow.increaseLevel();
for (final Outline child : children)
child.print(ow);
ow.decreaseLevel();
}
}
/**
*
* printf.
*
*
* @param wrappingIndentSize a int.
* @param format a {@link java.lang.String} object.
* @param args a {@link java.lang.Object} object.
*/
public void printf(final int wrappingIndentSize, final String format, final Object... args)
{
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
pw.printf(format, args);
writer.append(sw.toString(), wrappingIndentSize);
}
/**
*
* printf.
*
*
* @param format a {@link java.lang.String} object.
* @param args a {@link java.lang.Object} object.
*/
public void printf(final String format, final Object... args)
{
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
pw.printf(format, args);
writer.append(sw.toString(), 0);
}
/**
*
* reset.
*
*/
public void reset()
{
children = null;
contents = null;
}
/**
*
* Setter for the field contents
.
*
*
* @param contents a {@link java.lang.String} object.
* @param arguments a {@link java.lang.Object} object.
*/
public void setContents(final String contents, final Object... arguments)
{
final StringBuilder sb = new StringBuilder();
try (Formatter fmtr = new Formatter(sb))
{
fmtr.format(contents, arguments);
this.contents = sb.toString();
}
}
}