Skip to content

Commit

Permalink
#28 Adapt API for Passage
Browse files Browse the repository at this point in the history
Extract label calculation code

Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
  • Loading branch information
ruspl-afed committed May 21, 2020
1 parent d472f95 commit 9518e5a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
Expand Up @@ -30,6 +30,8 @@
*/
public class GroupRendererImpl implements ChronographGroupRenderer {

private final Labels labels = new Labels();

@Override
public void draw(GC gc, String label, Rectangle groupBound, Display display, int width, int hintY) {
int fontHeight = gc.getFontMetrics().getHeight();
Expand Down Expand Up @@ -57,30 +59,18 @@ public void draw(GC gc, String label, Rectangle groupBound, Display display, int
gc.setTransform(tr);
gc.setForeground(GroupStyler.GROUP_TEXT_COLOR);
gc.drawString(msg, -groupRectangle.height + (groupRectangle.height - stringExtent.x) / 2, fontHeight / 2, true);
// gc.setBackground(GroupStyler.GROUP_BTM_COLOR);
// gc.fillOval(-groupNameRectangle.height, 0, width, width);

tr.dispose();
gc.setTransform(null);
}

private String calculateLabel(GC gc, String label, final Rectangle groupRectangle, Point stringExtent) {
String msg = ""; //$NON-NLS-1$
String ends = "..."; //$NON-NLS-1$
Point endsExt = gc.stringExtent(ends);
int chWidths = 0;
if (stringExtent.x > groupRectangle.height) {
for (char ch : label.toCharArray()) {
chWidths += gc.getCharWidth(ch);
if (chWidths < groupRectangle.height - endsExt.x) {
msg += ch;
}
}
msg += ends;
private String calculateLabel(GC gc, String label, Rectangle rectangle, Point extent) {
int limit = rectangle.height;
if (extent.x > limit) {
return labels.fit(label, limit, gc);
} else {
msg = label;
return label;
}
return msg;
}

}
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.chronograph.internal.swt.renderers.impl;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

public class Labels {

String fit(String label, int limit, GC gc) {
StringBuilder builder = new StringBuilder();
String ellipsis = "..."; //$NON-NLS-1$
Point point = gc.stringExtent(ellipsis);
int consumed = 0;
for (char ch : label.toCharArray()) {
consumed += gc.getCharWidth(ch);
if (consumed < limit - point.x) {
builder.append(ch);
} else {
builder.append(ellipsis);
break;
}
}
return builder.toString();
}
}
Expand Up @@ -30,6 +30,8 @@
*/
public class SectionRendererImpl implements ChronographSectionRenderer {

private final Labels labels = new Labels();

@Override
public void draw(GC gc, String label, Rectangle bounds, Display display, int width, int hintY) {
gc.setAntialias(SWT.ON);
Expand All @@ -41,7 +43,6 @@ public void draw(GC gc, String label, Rectangle bounds, Display display, int wid
gc.drawRoundRectangle(0, sectionRectangle.y - hintY, width, sectionRectangle.height, width, width);

gc.setForeground(SectionStyler.SECTION_TOP_COLOR);
Point lblPoint = gc.stringExtent(label);
Transform tr = new Transform(display);
tr.translate(0, -hintY);
tr.rotate(-90);
Expand All @@ -54,29 +55,18 @@ public void draw(GC gc, String label, Rectangle bounds, Display display, int wid
int x = -sectionRectangle.height - sectionRectangle.y;
gc.drawString(msg, x + (sectionRectangle.height - stringExtent.x) / 2, fontHeight / 2, true);
gc.setBackground(SectionStyler.SECTION_BTM_COLOR);
// gc.fillOval(-bounds.y - bounds.height, 0, width, width);

tr.dispose();
gc.setTransform(null);
}

private String calculateLabel(GC gc, String label, final Rectangle rectangle, Point stringExtent) {
String msg = ""; //$NON-NLS-1$
String ends = "..."; //$NON-NLS-1$
Point endsExt = gc.stringExtent(ends);
int chWidths = 0;
if (stringExtent.x > rectangle.height) {
for (char ch : label.toCharArray()) {
chWidths += gc.getCharWidth(ch);
if (chWidths < rectangle.height - endsExt.x) {
msg += ch;
}
}
msg += ends;
private String calculateLabel(GC gc, String label, Rectangle rectangle, Point extent) {
int limit = rectangle.height;
if (extent.x > limit) {
return labels.fit(label, limit, gc);
} else {
msg = label;
return label;
}
return msg;
}

}

0 comments on commit 9518e5a

Please sign in to comment.