diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaCustomSliderRenderer.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaCustomSliderRenderer.java new file mode 100644 index 000000000..4fdbd3e25 --- /dev/null +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaCustomSliderRenderer.java @@ -0,0 +1,128 @@ +package org.eclipse.nebula.widgets.opal.nebulaslider.snippets; + +import org.eclipse.nebula.widgets.opal.nebulaslider.NebulaSlider; +import org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; + +public class NebulaCustomSliderRenderer extends NebularDefaultSliderRenderer { + + public NebulaCustomSliderRenderer(final NebulaSlider parentSlider) { + super(parentSlider); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getBarInsideColor() + */ + @Override + public Color getBarInsideColor() { + return getAndDisposeColor(231, 225, 219); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getBarBorderColor() + */ + @Override + public Color getBarBorderColor() { + return getAndDisposeColor(219, 211, 203); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getBarSelectionColor() + */ + @Override + public Color getBarSelectionColor() { + return getAndDisposeColor(129, 108, 91); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getSelectorColor() + */ + @Override + public Color getSelectorColor() { + return getAndDisposeColor(148, 130, 113); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getSelectorColorBorder() + */ + @Override + public Color getSelectorColorBorder() { + return getAndDisposeColor(238, 234, 230); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getSelectorTextColor() + */ + @Override + public Color getSelectorTextColor() { + return getAndDisposeColor(255, 255, 204); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getArrowColor() + */ + @Override + public Color getArrowColor() { + return getAndDisposeColor(203, 192, 181); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getTextFont() + */ + @Override + public Font getTextFont() { + final FontData fontData = parentSlider.getFont().getFontData()[0]; + final Font newFont = new Font(parentSlider.getDisplay(), "Arial", Math.max(fontData.getHeight(), 14), SWT.ITALIC); + parentSlider.addDisposeListener(e -> { + if (!newFont.isDisposed()) { + newFont.dispose(); + } + }); + return newFont; + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getHorizontalMargin() + */ + @Override + public int getHorizontalMargin() { + // TODO Auto-generated method stub + return super.getHorizontalMargin(); + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getSelectorWidth() + */ + @Override + public int getSelectorWidth() { + return 100; + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getSelectorHeight() + */ + @Override + public int getSelectorHeight() { + return 40; + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getBarHeight() + */ + @Override + public int getBarHeight() { + return 18; + } + + /** + * @see org.eclipse.nebula.widgets.opal.nebulaslider.NebularDefaultSliderRenderer#getArrowLineWidth() + */ + @Override + public int getArrowLineWidth() { + return 5; + } + +} diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippet.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippet.java index be6f1b4f2..c116558bc 100644 --- a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippet.java +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippet.java @@ -4,7 +4,7 @@ * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-2.0/ - * + * * SPDX-License-Identifier: EPL-2.0 * * Contributors: Laurent CARON (laurent.caron@gmail.com) @@ -13,8 +13,6 @@ import org.eclipse.nebula.widgets.opal.nebulaslider.NebulaSlider; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -53,11 +51,8 @@ public static void main(final String[] args) { slider.setValue(632); slider.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - slider.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - System.out.println("New value is " + slider.getValue()); - } + slider.addListener(SWT.Selection, e -> { + System.out.println("New value is " + slider.getValue()); }); final GridData layoutData = new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1); @@ -116,14 +111,14 @@ public void widgetSelected(SelectionEvent e) { display.dispose(); } - private static void createLeftLabel(Shell shell, String text) { + private static void createLeftLabel(final Shell shell, final String text) { final Label lbl = new Label(shell, SWT.NONE); lbl.setText(text); lbl.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE)); lbl.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false)); } - private static Text createTextWidget(Shell shell, int value) { + private static Text createTextWidget(final Shell shell, final int value) { final Text txt = new Text(shell, SWT.BORDER); txt.setText(String.valueOf(value)); txt.setTextLimit(3); diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippetOtherRenderer.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippetOtherRenderer.java new file mode 100644 index 000000000..3e3d32336 --- /dev/null +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider.snippets/src/org/eclipse/nebula/widgets/opal/nebulaslider/snippets/NebulaSliderSnippetOtherRenderer.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (c) 2011 Laurent CARON. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: Laurent CARON (laurent.caron@gmail.com) + *******************************************************************************/ +package org.eclipse.nebula.widgets.opal.nebulaslider.snippets; + +import org.eclipse.nebula.widgets.opal.nebulaslider.NebulaSlider; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +/** + * A simple snippet for the TextAssist Widget + */ +public class NebulaSliderSnippetOtherRenderer { + public static void main(final String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new GridLayout(2, false)); + shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + + createLeftLabel(shell, "Minimum"); + final Text min = createTextWidget(shell, 0); + + createLeftLabel(shell, "Maximum"); + final Text max = createTextWidget(shell, 1000); + + createLeftLabel(shell, "Value"); + final Text value = createTextWidget(shell, 632); + + final Button update = new Button(shell, SWT.PUSH); + update.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false, 2, 1)); + update.setText("Redraw Slider"); + + final NebulaSlider slider = new NebulaSlider(shell, SWT.NONE); + slider.setMinimum(0); + slider.setMaximum(1000); + slider.setValue(632); + slider.setRenderer(new NebulaCustomSliderRenderer(slider)); + slider.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + + slider.addListener(SWT.Selection, e -> { + System.out.println("New value is " + slider.getValue()); + }); + + final GridData layoutData = new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1); + layoutData.widthHint = 600; + layoutData.heightHint = 50; + slider.setLayoutData(layoutData); + + update.addListener(SWT.Selection, e -> { + int minValue = 0; + try { + minValue = Integer.valueOf(min.getText()); + } catch (final NumberFormatException nfe) { + showError(shell, "The value [" + min.getText() + "] is not a number"); + return; + } + + int maxValue = 0; + try { + maxValue = Integer.valueOf(max.getText()); + } catch (final NumberFormatException nfe) { + showError(shell, "The value [" + max.getText() + "] is not a number"); + return; + } + + if (maxValue < minValue) { + showError(shell, "The minimum is greater than the maximum"); + return; + } + // + int newValue = 0; + try { + newValue = Integer.valueOf(value.getText()); + } catch (final NumberFormatException nfe) { + showError(shell, "The value [" + value.getText() + "] is not a number"); + return; + } + if (newValue < minValue || newValue > maxValue) { + showError(shell, "The value [" + newValue + "] should be between " + minValue + " and " + maxValue); + return; + } + // + slider.setMinimum(minValue); + slider.setMaximum(maxValue); + slider.setValue(newValue); + + }); + + shell.pack(); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + display.dispose(); + } + + private static void createLeftLabel(final Shell shell, final String text) { + final Label lbl = new Label(shell, SWT.NONE); + lbl.setText(text); + lbl.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE)); + lbl.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false)); + } + + private static Text createTextWidget(final Shell shell, final int value) { + final Text txt = new Text(shell, SWT.BORDER); + txt.setText(String.valueOf(value)); + txt.setTextLimit(3); + final GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false); + gd.minimumWidth = 40; + txt.setLayoutData(gd); + return txt; + } + + private static void showError(final Shell shell, final String message) { + final MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); + mb.setText("Error"); + mb.setMessage(message); + mb.open(); + } +} diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java index 7a418479c..d2bbe4415 100644 --- a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java @@ -4,7 +4,7 @@ * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-2.0/ - * + * * SPDX-License-Identifier: EPL-2.0 * * Contributors: Laurent CARON (laurent.caron at gmail dot com) - Initial @@ -17,9 +17,6 @@ import org.eclipse.swt.SWTException; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; @@ -40,26 +37,13 @@ */ public class NebulaSlider extends Canvas { - private static final int H_MARGIN = 5; - private static final int SELECTOR_WIDTH = 78; - private static final int BAR_HEIGHT = 12; - private static final int SELECTOR_HEIGHT = 32; - - private final Color barInsideColor; - private final Color barBorderColor; - private final Color barSelectionColor; - - private final Color selectorColor; - private final Color selectorColorBorder; - private final Color selectorTextColor; - private final Color arrowColor; + private NebulaSliderRenderer renderer; private int minimum; private int maximum; private int value; private int xPosition; private int mouseDeltaX; - private final Font textFont; private boolean moving = false; @@ -93,25 +77,16 @@ public class NebulaSlider extends Canvas { * * @see Widget#getStyle() */ - public NebulaSlider(Composite parent, int style) { + public NebulaSlider(final Composite parent, final int style) { super(parent, checkStyle(style) | SWT.DOUBLE_BUFFERED); - barInsideColor = getAndDisposeColor(225, 225, 225); - barBorderColor = getAndDisposeColor(211, 211, 211); - barSelectionColor = getAndDisposeColor(41, 128, 185); - - selectorColor = getAndDisposeColor(52, 152, 219); - selectorColorBorder = getAndDisposeColor(224, 237, 245); - selectorTextColor = getAndDisposeColor(255, 255, 255); - arrowColor = getAndDisposeColor(153, 203, 237); + renderer = new NebularDefaultSliderRenderer(this); minimum = Integer.MIN_VALUE; maximum = Integer.MAX_VALUE; value = 0; xPosition = -1; - textFont = createTextFont(); - addPaintListener(e -> { paintControl(e.gc); }); @@ -125,28 +100,7 @@ private static int checkStyle(final int style) { return 0; } - private Color getAndDisposeColor(int r, int g, int b) { - final Color color = new Color(getDisplay(), r, g, b); - addDisposeListener(e -> { - if (!color.isDisposed()) { - color.dispose(); - } - }); - return color; - } - - private Font createTextFont() { - final FontData fontData = getFont().getFontData()[0]; - final Font newFont = new Font(getDisplay(), fontData.getName(), Math.max(fontData.getHeight(), 14), SWT.BOLD); - addDisposeListener(e -> { - if (!newFont.isDisposed()) { - newFont.dispose(); - } - }); - return newFont; - } - - private void paintControl(GC gc) { + private void paintControl(final GC gc) { gc.setAdvanced(true); gc.setAntialias(SWT.ON); @@ -160,73 +114,90 @@ private void paintControl(GC gc) { drawSelector(gc); } - private void drawBar(GC gc) { + private void drawBar(final GC gc) { final Rectangle rect = getClientArea(); - gc.setForeground(barBorderColor); - gc.setBackground(barInsideColor); + gc.setForeground(renderer.getBarBorderColor()); + gc.setBackground(renderer.getBarInsideColor()); + + final int hMargin = renderer.getHorizontalMargin(); + final int selectorWidth = renderer.getSelectorWidth(); + final int barHeight = renderer.getBarHeight(); - final int x = H_MARGIN + SELECTOR_WIDTH / 2; - final int y = (rect.height - BAR_HEIGHT) / 2; - final int width = rect.width - H_MARGIN * 2 - SELECTOR_WIDTH; + final int x = hMargin + selectorWidth / 2; + final int y = (rect.height - barHeight) / 2; + final int width = rect.width - hMargin * 2 - selectorWidth; - gc.fillRoundRectangle(x, y, width, BAR_HEIGHT, BAR_HEIGHT, BAR_HEIGHT); - gc.drawRoundRectangle(x, y, width, BAR_HEIGHT, BAR_HEIGHT, BAR_HEIGHT); + gc.fillRoundRectangle(x, y, width, barHeight, barHeight, barHeight); + gc.drawRoundRectangle(x, y, width, barHeight, barHeight, barHeight); } - private void drawSelectionPart(GC gc) { + private void drawSelectionPart(final GC gc) { final Rectangle rect = getClientArea(); - gc.setForeground(barBorderColor); - gc.setBackground(barSelectionColor); + gc.setForeground(renderer.getBarBorderColor()); + gc.setBackground(renderer.getBarSelectionColor()); + + final int barHeight = renderer.getBarHeight(); - final int x = H_MARGIN + SELECTOR_WIDTH / 2; - final int y = (rect.height - BAR_HEIGHT) / 2; + final int x = renderer.getHorizontalMargin() + renderer.getSelectorWidth() / 2; + final int y = (rect.height - barHeight) / 2; - gc.fillRoundRectangle(x, y, xPosition, BAR_HEIGHT, BAR_HEIGHT, BAR_HEIGHT); - gc.drawRoundRectangle(x, y, xPosition, BAR_HEIGHT, BAR_HEIGHT, BAR_HEIGHT); + gc.fillRoundRectangle(x, y, xPosition, barHeight, barHeight, barHeight); + gc.drawRoundRectangle(x, y, xPosition, barHeight, barHeight, barHeight); } private int computeXPosition() { - final int originalWidth = getClientArea().width - H_MARGIN * 2 - SELECTOR_WIDTH; + final int originalWidth = getClientArea().width - renderer.getHorizontalMargin() * 2 - renderer.getSelectorWidth(); final float coeff = value * 1f / (maximum - minimum); final int position = (int) (coeff * originalWidth); return position; } - private void drawSelector(GC gc) { + private void drawSelector(final GC gc) { final Rectangle rect = getClientArea(); - gc.setForeground(selectorColorBorder); - gc.setBackground(selectorColor); + gc.setForeground(renderer.getSelectorColorBorder()); + gc.setBackground(renderer.getSelectorColor()); - final int y = (rect.height - SELECTOR_HEIGHT) / 2; + final int hMargin = renderer.getHorizontalMargin(); + + final int selectorWidth = renderer.getSelectorWidth(); + final int selectorHeight = renderer.getSelectorHeight(); + + final int y = (rect.height - selectorHeight) / 2; // Draw the body - gc.fillRoundRectangle(H_MARGIN + xPosition, y, SELECTOR_WIDTH, SELECTOR_HEIGHT, SELECTOR_HEIGHT, SELECTOR_HEIGHT); - gc.drawRoundRectangle(H_MARGIN + xPosition, y, SELECTOR_WIDTH, SELECTOR_HEIGHT, SELECTOR_HEIGHT, SELECTOR_HEIGHT); + gc.fillRoundRectangle(hMargin + xPosition, y, selectorWidth, selectorHeight, selectorHeight, selectorHeight); + gc.drawRoundRectangle(hMargin + xPosition, y, selectorWidth, selectorHeight, selectorHeight, selectorHeight); // Draw the arrows - gc.setForeground(arrowColor); - gc.setLineWidth(3); - final int baseY = y + SELECTOR_HEIGHT / 2; - gc.drawLine(H_MARGIN + xPosition + 10, baseY, H_MARGIN + xPosition + 17, baseY - 7); - gc.drawLine(H_MARGIN + xPosition + 10, baseY, H_MARGIN + xPosition + 17, baseY + 7); + gc.setForeground(renderer.getArrowColor()); + gc.setLineWidth(renderer.getArrowLineWidth()); + final int baseY = y + selectorHeight / 2; + gc.drawLine(hMargin + xPosition + 10, baseY, hMargin + xPosition + 17, baseY - 7); + gc.drawLine(hMargin + xPosition + 10, baseY, hMargin + xPosition + 17, baseY + 7); - gc.drawLine(H_MARGIN + xPosition + SELECTOR_WIDTH - 10, baseY, H_MARGIN + xPosition + SELECTOR_WIDTH - 17, baseY - 7); - gc.drawLine(H_MARGIN + xPosition + SELECTOR_WIDTH - 10, baseY, H_MARGIN + xPosition + SELECTOR_WIDTH - 17, baseY + 7); + gc.drawLine(hMargin + xPosition + selectorWidth - 10, baseY, hMargin + xPosition + selectorWidth - 17, baseY - 7); + gc.drawLine(hMargin + xPosition + selectorWidth - 10, baseY, hMargin + xPosition + selectorWidth - 17, baseY + 7); // And the value - gc.setForeground(selectorTextColor); - gc.setFont(textFont); + gc.setForeground(renderer.getSelectorTextColor()); + gc.setFont(renderer.getTextFont()); final String valueAsString = String.valueOf(value); final Point textSize = gc.textExtent(valueAsString); - final int xText = H_MARGIN + xPosition + SELECTOR_WIDTH / 2; - gc.drawText(valueAsString, xText - textSize.x / 2, y + 2, true); + final int xText = hMargin + xPosition + selectorWidth / 2; + final int yText = y + selectorHeight / 2; + + gc.drawText(valueAsString, xText - textSize.x / 2, yText - textSize.y / 2, true); } private void addMouseListeners() { + addListener(SWT.MouseDown, e -> { - final int y = (getClientArea().height - SELECTOR_HEIGHT) / 2; - final Rectangle rect = new Rectangle(xPosition + H_MARGIN, y, SELECTOR_WIDTH, SELECTOR_HEIGHT); + final int selectorWidth = renderer.getSelectorWidth(); + final int selectorHeight = renderer.getSelectorHeight(); + + final int y = (getClientArea().height - selectorHeight) / 2; + final Rectangle rect = new Rectangle(xPosition + renderer.getHorizontalMargin(), y, selectorWidth, selectorHeight); if (!rect.contains(e.x, e.y)) { return; } @@ -249,14 +220,14 @@ private void addMouseListeners() { if (xPosition < 0) { xPosition = 0; } - final int originalWidth = getClientArea().width - H_MARGIN * 2 - SELECTOR_WIDTH; + final int originalWidth = getClientArea().width - renderer.getHorizontalMargin() * 2 - renderer.getSelectorWidth(); if (xPosition > originalWidth) { xPosition = originalWidth; } // Update value - float ratio = (float) xPosition / originalWidth; + final float ratio = (float) xPosition / originalWidth; value = (int) Math.floor(ratio * (maximum - minimum)); SelectionListenerUtil.fireSelectionListeners(this,e); @@ -302,7 +273,7 @@ public void addSelectionListener(final SelectionListener listener) { * @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean) */ @Override - public Point computeSize(int wHint, int hHint, boolean changed) { + public Point computeSize(final int wHint, final int hHint, final boolean changed) { return new Point(Math.max(300, wHint), Math.max(40, hHint)); } @@ -361,7 +332,7 @@ public int getMinimum() { *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  • * */ - public void setMinimum(int minimum) { + public void setMinimum(final int minimum) { checkWidget(); if (minimum > maximum) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, new IllegalArgumentException(String.format("Value %d is greater than the maximum value (%d)", minimum, maximum))); @@ -398,7 +369,7 @@ public int getMaximum() { *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  • * */ - public void setMaximum(int maximum) { + public void setMaximum(final int maximum) { checkWidget(); if (maximum < minimum) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, new IllegalArgumentException(String.format("Value %d is lower than the minimum value (%d)", maximum, minimum))); @@ -435,7 +406,7 @@ public int getValue() { *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  • * */ - public void setValue(int value) { + public void setValue(final int value) { checkWidget(); if (value < minimum || value > maximum) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, new IllegalArgumentException(String.format("Value %d is not int the range [%d - %d]", value, minimum, maximum))); @@ -446,4 +417,37 @@ public void setValue(int value) { update(); } + /** + * Return the current renderer for this widget + * + * @return the renderer + * + * @exception SWTException + * + */ + public NebulaSliderRenderer getRenderer() { + checkWidget(); + return renderer; + } + + /** + * Sets the renderer for this widget + * + * @param renderer the new renderer + * + * @exception SWTException + * + */ + public void setRenderer(final NebulaSliderRenderer renderer) { + checkWidget(); + this.renderer = renderer; + redraw(); + } + } diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSliderRenderer.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSliderRenderer.java new file mode 100644 index 000000000..8687d1c0e --- /dev/null +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSliderRenderer.java @@ -0,0 +1,33 @@ +package org.eclipse.nebula.widgets.opal.nebulaslider; + +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; + +public interface NebulaSliderRenderer { + + Color getBarInsideColor(); + + Color getBarBorderColor(); + + Color getBarSelectionColor(); + + Color getSelectorColor(); + + Color getSelectorColorBorder(); + + Color getSelectorTextColor(); + + Color getArrowColor(); + + int getArrowLineWidth(); + + Font getTextFont(); + + int getHorizontalMargin(); + + int getSelectorWidth(); + + int getSelectorHeight(); + + int getBarHeight(); +} diff --git a/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebularDefaultSliderRenderer.java b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebularDefaultSliderRenderer.java new file mode 100644 index 000000000..6c09f6eb4 --- /dev/null +++ b/widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebularDefaultSliderRenderer.java @@ -0,0 +1,101 @@ +package org.eclipse.nebula.widgets.opal.nebulaslider; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; + +public class NebularDefaultSliderRenderer implements NebulaSliderRenderer { + private static final int H_MARGIN = 5; + private static final int SELECTOR_WIDTH = 78; + private static final int BAR_HEIGHT = 12; + private static final int SELECTOR_HEIGHT = 32; + protected final NebulaSlider parentSlider; + + public NebularDefaultSliderRenderer(final NebulaSlider parentSlider) { + this.parentSlider = parentSlider; + } + + @Override + public Color getBarInsideColor() { + return getAndDisposeColor(225, 225, 225); + } + + @Override + public Color getBarBorderColor() { + return getAndDisposeColor(211, 211, 211); + } + + @Override + public Color getBarSelectionColor() { + return getAndDisposeColor(41, 128, 185); + } + + @Override + public Color getSelectorColor() { + return getAndDisposeColor(52, 152, 219); + } + + @Override + public Color getSelectorColorBorder() { + return getAndDisposeColor(224, 237, 245); + } + + @Override + public Color getSelectorTextColor() { + return getAndDisposeColor(255, 255, 255); + } + + @Override + public Color getArrowColor() { + return getAndDisposeColor(153, 203, 237); + } + + @Override + public Font getTextFont() { + final FontData fontData = parentSlider.getFont().getFontData()[0]; + final Font newFont = new Font(parentSlider.getDisplay(), fontData.getName(), Math.max(fontData.getHeight(), 14), SWT.BOLD); + parentSlider.addDisposeListener(e -> { + if (!newFont.isDisposed()) { + newFont.dispose(); + } + }); + return newFont; + } + + @Override + public int getHorizontalMargin() { + return H_MARGIN; + } + + @Override + public int getSelectorWidth() { + return SELECTOR_WIDTH; + } + + @Override + public int getSelectorHeight() { + return SELECTOR_HEIGHT; + } + + @Override + public int getBarHeight() { + return BAR_HEIGHT; + } + + protected Color getAndDisposeColor(final int r, final int g, final int b) { + final Color color = new Color(parentSlider.getDisplay(), r, g, b); + parentSlider.addDisposeListener(e -> { + if (!color.isDisposed()) { + color.dispose(); + } + }); + return color; + } + + @Override + public int getArrowLineWidth() { + return 3; + } + +}