package gve.calc.formula;

import java.util.Hashtable;
import java.awt.*;

public abstract class PostfixUnaryOp extends UnaryOp {
	public PostfixUnaryOp() {
		super();
	}

	public PostfixUnaryOp(Part ch) {
		super(ch);
	}

	public UnaryOp getInstance(Part ch) {
		return getInstance(getName(),ch);
	}

	//----------static stuff----------
	private static Hashtable repository = new Hashtable();

	public static void registerFactory(String name,UnaryOpFactory factory) {
		repository.put(name,factory);
	}

	public static UnaryOp getInstance(String name,Part ch) {
		UnaryOpFactory fact = (UnaryOpFactory)repository.get(name);
		if (fact == null)
			throw new Error("No factory for "+name);
		return fact.createUnaryOp(name,ch);
	}

	public static boolean isOperatorName(String name) {
		return repository.containsKey(name);
	}

	public static String operatorPrefix(String name) {
		return InfixBinaryOp.operatorPrefix(name,repository);
	}

	//----------instance stuff----------

	public boolean simpleRotate(Formula f,FormulaView view) {
		if (parent instanceof InfixBinaryOp) {
			InfixBinaryOp par = (InfixBinaryOp)parent;
			// Rotate myself upwards: ... I (arg P) -> (... I arg)P
			if (this==par.right && getPri()<par.getRightPri()) {
				Identifier dummy = new Identifier();
				f.replace(this,dummy);
				f.replace(par,this);
				Part oldchild = child;
				f.replace(child,par);
				f.replace(dummy,oldchild);
				return true;
			}
		}
		return false;
	}

	Identifier dismantle(String str,Formula f) {
		Identifier result = new Identifier(str);
		f.replace(this,new OperatorSpace(child,result));
		return result;
	}

	public Component createView(FormulaView view) {
		return new PostfixUnaryOpView(this,view);
	}

	
A default strategy, so a subclass only has to implement saveOperatorLatex()
public void saveLatex(java.io.BufferedWriter w) throws java.io.IOException { child.saveLatex(w); w.write(" "); saveOperatorLatex(w); } public void saveOperatorLatex(java.io.BufferedWriter w) throws java.io.IOException { w.write(getName()); } }