function grideditCommand ( command )
%disp(command)

global grid_obj;


pslice_axis = grid_obj.pslice_axis;
axes(pslice_axis);
slider = findobj ( grid_obj.edit_fig, ...
					'tag', 'value slider' );
edit = findobj ( grid_obj.edit_fig, 'tag', 'value edit' );
edit_fig = grid_obj.edit_fig;

switch ( command )
	

	case 'close'

		delete(grid_obj.figure);
		delete ( edit_fig );
		return;



	case 'commit'

		%
		% first check to see what is the value to set.
		% If any of the radio buttons are set, use that.
		% If not, use the edit window value.
		rb = findobj ( grid_obj.edit_fig, ...
						'style', 'radiobutton', ...
						'value', 1 );
		if (length(rb)==1)
			val_to_set = str2num ( get ( rb, 'string' ) );
		else
			val_to_set = get ( slider, 'value' );
		end


		%
		% Now set the grid values.
		grid_obj.u(grid_obj.point_index_list) = ...
			val_to_set * ones(size(grid_obj.point_index_list));

		grid_obj.point_index_list = [];



		%
		% Grab the current color limits, then replot the data.
		clims = get ( grid_obj.pslice_axis, 'CLim' );
		cla;
		pslice2(grid_obj.x,grid_obj.y,grid_obj.u,grid_obj.range);
		set ( gca, 'CLim', clims );
		if ( length(grid_obj.coastline) ~= 0 )
			line ( grid_obj.coastline(:,1), grid_obj.coastline(:,2), 'color', 'red' );
		end

		grid_obj.pslice_axis = gca;
		%set ( grid_obj.pslice_axis, 'ButtonDownFcn', 'grideditCommand get_point' );

		%
		% Don't need to hang on to these any longer.  Should
		% clear them to keep things consistent in case of "undo".
		grid_obj.marker_list = [];

	case 'display_value'
		%
		% first reset the window buttondown callback to what it was
		%set ( grid_obj.figure, 'WindowButtonDownFcn', 'grideditCommand get_point' );
		set ( grid_obj.figure, 'WindowButtonDownFcn', 'grideditCommand buttondown' );

		% 
		% retrieve the value of the point chosen and patch it 
		% in to the slider and edit
		current_point = get ( pslice_axis, 'CurrentPoint' );
		x0 = current_point(1,1);
		y0 = current_point(1,2);
		ind = nearxy(grid_obj.nearx,grid_obj.neary,x0,y0);
		display_value = grid_obj.u(ind);
		slider_max = get(slider,'max');
		slider_min = get(slider,'max');
		if ( display_value > get(slider,'max') )
			set ( slider, 'max', display_value );
		end
		if ( display_value < get(slider,'min') )
			set ( slider, 'min', display_value );
		end
			
		set ( slider, 'value', display_value );
		set ( edit, 'string', num2str(display_value) );


	case 'edit'

		% set the slider value to be the same
		set ( slider, 'value', str2num(get(gcbo,'string')) );


	%
	% The selectiontype property tells us which mouse button was used.
	%
	% normal:  mouse button 1 ==> select a point on the grid
	% extend:  mouse button 2 ==> select corner of new axis extents
	%                             rectangle, save current axis settings
	% alt:  moust button 3 ==> restore previous axis settings
	case 'buttondown'
		
		switch ( get(gcf, 'SelectionType') )

			case 'normal' 
				current_point = get ( pslice_axis, 'CurrentPoint' );
				x0 = current_point(1,1);
				y0 = current_point(1,2);
				ind = nearxy(grid_obj.nearx,grid_obj.neary,x0,y0);
				grid_obj.point_index_list = [grid_obj.point_index_list; ind];
				hold on;
				marker = plot ( x0, y0, 'm*' );
				set ( marker, 'MarkerSize', 15 );
				grid_obj.marker_list = [grid_obj.marker_list; marker];

			case 'extend'
				grid_obj.corner1 = get ( pslice_axis, 'CurrentPoint' );

			case 'alt'
				[r,c] = size ( grid_obj.axis_stack );
				if ( r ~= 0 )
					old_axis_lims = grid_obj.axis_stack(r,:);
					grid_obj.axis_stack = grid_obj.axis_stack(1:r-1,:);
					axis ( old_axis_lims );
				end

		end



	%
	% The selectiontype property tells us which mouse button was used.
	%
	% normal:  mouse button 1 ==> nothing
	% extend:  mouse button 2 ==> select 2nd corner of new axis extents
	%                             rectangle, save current axis settings
	% alt:  moust button 3 ==> nothing
	case 'buttonup'
		
		switch ( get(gcf, 'SelectionType') )

			case 'normal' 

			case 'extend'
				grid_obj.corner2 = get ( pslice_axis, 'CurrentPoint' );
				x1 = min(grid_obj.corner1(1,1), grid_obj.corner2(1,1));
				x2 = max(grid_obj.corner1(1,1), grid_obj.corner2(1,1));
				y1 = min(grid_obj.corner1(1,2), grid_obj.corner2(1,2));
				y2 = max(grid_obj.corner1(1,2), grid_obj.corner2(1,2));
				new_axis = [x1 x2 y1 y2];
				old_axis = axis;
				grid_obj.axis_stack = [grid_obj.axis_stack; old_axis] ;
				axis ( new_axis );

			case 'alt'

		end



	case 'get_value'
		
		set ( grid_obj.figure, 'WindowButtonDownFcn', 'grideditCommand display_value' );



		

	%
	% The next three cases guarantee mutual exclusion for
	% the radio buttons, as only one of NaN, -Inf, and Inf
	% can be applied at any one time.
	case 'inf'
		ninfbutton = findobj(0,'tag','-Inf rbutton');
		set ( ninfbutton, 'value', 0 );
		NaNbutton = findobj(0,'tag','NaN rbutton');
		set ( NaNbutton, 'value', 0 );
		set ( edit, 'string', 'Inf' );

	case 'ninf'
		infbutton = findobj(0,'tag','Inf rbutton');
		set ( infbutton, 'value', 0 );
		NaNbutton = findobj(0,'tag','NaN rbutton');
		set ( NaNbutton, 'value', 0 );
		set ( edit, 'string', '-Inf' );

	case 'nan'
		infbutton = findobj(0,'tag','Inf rbutton');
		set ( infbutton, 'value', 0 );
		ninfbutton = findobj(0,'tag','-Inf rbutton');
		set ( ninfbutton, 'value', 0 );
		set ( edit, 'string', 'NaN' );


	case 'slider'

		% set the edit value to be the same
		set ( edit, 'string', num2str(get(gcbo,'value')) );


	case 'undo'
		delete ( grid_obj.marker_list );
		grid_obj.marker_list = [];


	case 'write'
		
		x = grid_obj.x;
		y = grid_obj.y;
		u = grid_obj.u;
		[filename, pathname] = uiputfile('*.mat', 'Save The New Grid');
		fullname = sprintf ( '%s%s', pathname, filename );
		eval ( sprintf ( 'save %s x y u;', fullname ) );


end

shading ( 'faceted' );
figure ( edit_fig );

		
