<commit_before>/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.csstudio.swt.widgets.figures;

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.geometry.Transposer;

/**
 * Lays out the Figures that make up a ScrollBar.
 */
public class ScrollBarFigureLayout
	extends AbstractLayout
{

/** Used as a constraint for the up arrow figure. */
public static final String UP_ARROW   = "up arrow";   //$NON-NLS-1$
/** Used as a constraint for the down arrow figure. */
public static final String DOWN_ARROW = "down arrow"; //$NON-NLS-1$
/** Used as a constraint for the thumb figure. */
public static final String THUMB      = "thumb";      //$NON-NLS-1$
/** Used as a constraint for the page up figure. */
public static final String PAGE_UP    = "page_up";    //$NON-NLS-1$
/** Used as a constraint for the page down figure. */
public static final String PAGE_DOWN  = "page_down";  //$NON-NLS-1$

IFigure up, down, thumb, pageUp, pageDown;

/**
 * Transposes values if the ScrollBar is horizontally oriented. When used properly, the 
 * layout manager just needs to code for one case: vertical orientation.
 */
protected final Transposer transposer;

/**
 * Constructs a ScrollBarLayout. If the given Transposer is enabled, the Scrollbar will 
 * be horizontally oriented. Otherwise, the ScrollBar will be vertically oriented.
 * 
 * @param t the Transposer
 * @since 2.0
 */
public ScrollBarFigureLayout(Transposer t) {
	transposer = t;
}

/**
 * @see AbstractLayout#setConstraint(IFigure, Object)
 */
public void setConstraint(IFigure figure, Object constraint) {
	if (constraint.equals(UP_ARROW))
		up = figure;
	else if (constraint.equals(DOWN_ARROW))
		down = figure;
	else if (constraint.equals(THUMB))
		thumb = figure;
	else if (constraint.equals(PAGE_UP))
		pageUp = figure;
	else if (constraint.equals(PAGE_DOWN))
		pageDown = figure;
}

/**
 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
 */
protected Dimension calculatePreferredSize(IFigure parent, int w, int h) {
	Insets insets = transposer.t(parent.getInsets());
	Dimension d = new Dimension(16, 16 * 4);
	d.expand(insets.getWidth(), insets.getHeight());
	return transposer.t(d);
}

/**
 * @see LayoutManager#layout(IFigure)
 */
public void layout(IFigure parent) {
	ScrollbarFigure scrollBar = (ScrollbarFigure) parent;

	Rectangle trackBounds = layoutButtons(scrollBar);

	double extent = scrollBar.getExtent();
	double max = scrollBar.getMaximum() + extent;
	double min = scrollBar.getMinimum();
	double totalRange =  max - min;
	double valueRange = totalRange - extent;
	if (!scrollBar.isEnabled()) {
		Rectangle boundsUpper = new Rectangle(trackBounds),
		          boundsLower = new Rectangle(trackBounds);
		boundsUpper.height /= 2;
		boundsLower.y += boundsUpper.height;
		boundsLower.height = trackBounds.height - boundsUpper.height;
		if (pageUp != null)
			pageUp.setBounds(transposer.t(boundsUpper));
		if (pageDown != null)
			pageDown.setBounds(transposer.t(boundsLower));
		return;
	}

	if (totalRange == 0)
		return;
	int thumbHeight = (int) Math.max(thumb == null ? 0 : thumb.getMinimumSize().height,
								trackBounds.height * extent / totalRange);

	if (thumb != null)
		thumb.setVisible(trackBounds.height > thumbHeight);

	int thumbY = (int) (trackBounds.y + (trackBounds.height - thumbHeight) 
					* (scrollBar.getCoercedValue() - min) / valueRange);

	Rectangle thumbBounds =  new Rectangle(
		trackBounds.x,
		thumbY,
		trackBounds.width,
		thumbHeight);

	if (thumb != null)<commit_msg>swt.widgets: fix a scrollbar layout bug when min=max<commit_after>/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.csstudio.swt.widgets.figures;

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.geometry.Transposer;

/**
 * Lays out the Figures that make up a ScrollBar.
 */
public class ScrollBarFigureLayout
	extends AbstractLayout
{

/** Used as a constraint for the up arrow figure. */
public static final String UP_ARROW   = "up arrow";   //$NON-NLS-1$
/** Used as a constraint for the down arrow figure. */
public static final String DOWN_ARROW = "down arrow"; //$NON-NLS-1$
/** Used as a constraint for the thumb figure. */
public static final String THUMB      = "thumb";      //$NON-NLS-1$
/** Used as a constraint for the page up figure. */
public static final String PAGE_UP    = "page_up";    //$NON-NLS-1$
/** Used as a constraint for the page down figure. */
public static final String PAGE_DOWN  = "page_down";  //$NON-NLS-1$

IFigure up, down, thumb, pageUp, pageDown;

/**
 * Transposes values if the ScrollBar is horizontally oriented. When used properly, the 
 * layout manager just needs to code for one case: vertical orientation.
 */
protected final Transposer transposer;

/**
 * Constructs a ScrollBarLayout. If the given Transposer is enabled, the Scrollbar will 
 * be horizontally oriented. Otherwise, the ScrollBar will be vertically oriented.
 * 
 * @param t the Transposer
 * @since 2.0
 */
public ScrollBarFigureLayout(Transposer t) {
	transposer = t;
}

/**
 * @see AbstractLayout#setConstraint(IFigure, Object)
 */
public void setConstraint(IFigure figure, Object constraint) {
	if (constraint.equals(UP_ARROW))
		up = figure;
	else if (constraint.equals(DOWN_ARROW))
		down = figure;
	else if (constraint.equals(THUMB))
		thumb = figure;
	else if (constraint.equals(PAGE_UP))
		pageUp = figure;
	else if (constraint.equals(PAGE_DOWN))
		pageDown = figure;
}

/**
 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
 */
protected Dimension calculatePreferredSize(IFigure parent, int w, int h) {
	Insets insets = transposer.t(parent.getInsets());
	Dimension d = new Dimension(16, 16 * 4);
	d.expand(insets.getWidth(), insets.getHeight());
	return transposer.t(d);
}

/**
 * @see LayoutManager#layout(IFigure)
 */
public void layout(IFigure parent) {
	ScrollbarFigure scrollBar = (ScrollbarFigure) parent;

	Rectangle trackBounds = layoutButtons(scrollBar);

	double extent = scrollBar.getExtent();
	double max = scrollBar.getMaximum() + extent;
	double min = scrollBar.getMinimum();
	double totalRange =  max - min;
	double valueRange = totalRange - extent;
	if (!scrollBar.isEnabled()) {
		Rectangle boundsUpper = new Rectangle(trackBounds),
		          boundsLower = new Rectangle(trackBounds);
		boundsUpper.height /= 2;
		boundsLower.y += boundsUpper.height;
		boundsLower.height = trackBounds.height - boundsUpper.height;
		if (pageUp != null)
			pageUp.setBounds(transposer.t(boundsUpper));
		if (pageDown != null)
			pageDown.setBounds(transposer.t(boundsLower));
		return;
	}

	if (totalRange == 0)
		return;
	int thumbHeight = (int) Math.max(thumb == null ? 0 : thumb.getMinimumSize().height,
								trackBounds.height * extent / totalRange);

	if (thumb != null)
		thumb.setVisible(trackBounds.height > thumbHeight);
	int thumbY;
	if(valueRange <=0){
		thumbHeight=0;
		thumb.setVisible(false);
		thumbY=trackBounds.y;
	}
	else
		thumbY = (int) (trackBounds.y + (trackBounds.height - thumbHeight) 
					* (scrollBar.getCoercedValue() - min) / valueRange);

	Rectangle thumbBounds =  new Rectangle(
		trackBounds.x,
		thumbY,
		trackBounds.width,
		thumbHeight);

	if (thumb != null && thumb.isVisible())
{'fraction_non_alphanumeric': 0.1459227467811159, 'fraction_numerical': 0.00811970769052314, 'mean_word_length': 4.375311720698255, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6726335', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2540, 'n_words': 766}