/*
 * ====================================================================
 * This software is subject to the terms of the Common Public License
 * Agreement, available at the following URL:
 *   http://www.opensource.org/licenses/cpl.html .
 * You must accept the terms of that agreement to use this software.
 * ====================================================================
 */
package com.eyeq.pivot4j.ui.command;

import com.eyeq.pivot4j.ui.PivotRenderer;
import com.eyeq.pivot4j.ui.RenderContext;
import com.eyeq.pivot4j.ui.SortMode;

public abstract class AbstractSortCommand extends AbstractCellCommand implements
		SortCommand {

	/**
	 * @param renderer
	 */
	public AbstractSortCommand(PivotRenderer renderer) {
		super(renderer);
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.AbstractCellCommand#getMode(com.eyeq.pivot4j.ui.RenderContext)
	 */
	@Override
	public String getMode(RenderContext context) {
		StringBuilder builder = new StringBuilder();

		SortMode mode = getRenderer().getSortMode();
		if (mode != null) {
			builder.append(mode.getName());
			builder.append('-');
		}

		switch (context.getModel().getSortCriteria()) {
		case ASC:
		case BASC:
		case TOPCOUNT:
			builder.append("up");
			break;
		case DESC:
		case BDESC:
		case BOTTOMCOUNT:
			builder.append("down");
			break;
		default:
			assert false;
		}

		return builder.toString();
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#createParameters(com.eyeq.pivot4j
	 *      .ui.RenderContext)
	 */
	@Override
	public CellParameters createParameters(RenderContext context) {
		CellParameters parameters = new CellParameters();
		parameters.setAxisOrdinal(context.getAxis().axisOrdinal());
		parameters.setPositionOrdinal(context.getPosition().getOrdinal());

		return parameters;
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#canExecute(com.eyeq.pivot4j.ui
	 *      .RenderContext)
	 */
	@Override
	public boolean canExecute(RenderContext context) {
		return getRenderer().getEnableSort() && context.getPosition() != null
				&& context.getModel().isSortable(context.getPosition());
	}
}

@@ -1095,16 +1095,261 @@
 );%0A%09%09%7D%0A%0A
+%09%09if (context.getModel().isSorting()) %7B%0A%09%09%09if (context.getPosition() == null%0A%09%09%09%09%09%7C%7C !context.getModel().isSorting(context.getPosition())) %7B%0A%09%09%09%09builder.append(%22other%22);%0A%09%09%09%7D else %7B%0A%09%09%09%09builder.append(%22current%22);%0A%09%09%09%7D%0A%0A%09%09%09builder.append('-');%0A%0A%09
 %09%09switch
@@ -1392,16 +1392,17 @@
 ()) %7B%0A%09%09
+%09
 case ASC
@@ -1399,24 +1399,25 @@
 %09%09case ASC:%0A
+%09
 %09%09case BASC:
@@ -1415,24 +1415,25 @@
 ase BASC:%0A%09%09
+%09
 case TOPCOUN
@@ -1431,24 +1431,25 @@
 e TOPCOUNT:%0A
+%09
 %09%09%09builder.a
@@ -1457,24 +1457,25 @@
 pend(%22up%22);%0A
+%09
 %09%09%09break;%0A%09%09
@@ -1474,16 +1474,17 @@
 reak;%0A%09%09
+%09
 case DES
@@ -1482,24 +1482,25 @@
 %09case DESC:%0A
+%09
 %09%09case BDESC
@@ -1503,16 +1503,17 @@
 DESC:%0A%09%09
+%09
 case BOT
@@ -1522,16 +1522,17 @@
 MCOUNT:%0A
+%09
 %09%09%09build
@@ -1550,16 +1550,17 @@
 down%22);%0A
+%09
 %09%09%09break
@@ -1563,16 +1563,17 @@
 reak;%0A%09%09
+%09
 default:
@@ -1573,16 +1573,17 @@
 efault:%0A
+%09
 %09%09%09asser
@@ -1591,16 +1591,62 @@
  false;%0A
+%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09builder.append(%22natural%22);%0A
 %09%09%7D%0A%0A%09%09r

Make sort command mode to inform active sorting position