| /******************************************************************************* |
| * Copyright (c) 2020 Christian Pontesegger and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christian Pontesegger - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.skills.ui.views.character; |
| |
| import java.time.Duration; |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.notify.Adapter; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.jface.layout.GridDataFactory; |
| import org.eclipse.jface.util.Throttler; |
| import org.eclipse.skills.model.ISkill; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.ProgressBar; |
| |
| public class SkillsComposite extends Composite { |
| |
| private static final String FLAG_WITH_PROGRESSBAR = "hasProgressbar"; |
| |
| private final boolean fShowProgress; |
| |
| private final Throttler fUiUpdateThrottler = new Throttler(Display.getDefault(), Duration.ofMillis(500), this::updateControls); |
| |
| private final Adapter fSkillAdapter = new SkillAdapter(); |
| |
| public SkillsComposite(Composite parent, int style, List<ISkill> skills, boolean showProgress) { |
| super(parent, style); |
| |
| fShowProgress = showProgress; |
| |
| setLayout(new GridLayout(showProgress ? 3 : 2, false)); |
| |
| setSkills(skills); |
| } |
| |
| public void setSkills(Collection<ISkill> skills) { |
| |
| for (final ISkill skill : skills) { |
| createSkillElement(this, skill); |
| skill.eAdapters().add(fSkillAdapter); |
| } |
| |
| // trigger fresh layout |
| getParent().layout(); |
| } |
| |
| private void createSkillElement(Composite parent, ISkill skill) { |
| final Label lblTitle = new Label(parent, SWT.NONE); |
| lblTitle.setText(skill.getName()); |
| lblTitle.setToolTipText(skill.getDescription().getText()); |
| lblTitle.setLayoutData(GridDataFactory.fillDefaults().create()); |
| |
| if (fShowProgress) { |
| final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE); |
| progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); |
| progressBar.setData(skill); |
| updateControl(progressBar, skill); |
| |
| final Label lblLevel = new Label(parent, SWT.NONE); |
| lblLevel.setData(skill); |
| lblLevel.setData(FLAG_WITH_PROGRESSBAR, true); |
| updateControl(lblLevel, skill); |
| |
| } else { |
| |
| final Label lblLevel = new Label(parent, SWT.NONE); |
| lblLevel.setData(skill); |
| lblLevel.setData(FLAG_WITH_PROGRESSBAR, false); |
| lblLevel.setLayoutData(GridDataFactory.fillDefaults().indent(20, 0).create()); |
| updateControl(lblLevel, skill); |
| } |
| } |
| |
| public void updateControls() { |
| for (final Control control : getChildren()) { |
| if (control.getData() instanceof ISkill) |
| updateControl(control, (ISkill) control.getData()); |
| } |
| } |
| |
| public void updateControl(Control control, ISkill skill) { |
| final int level = skill.getProgression().getLevel(skill.getExperience()); |
| final int currentLevelMinXP = skill.getProgression().getMinimumXpForLevel(level); |
| final int nextLevelMinXP = skill.getProgression().getMinimumXpForLevel(level + 1); |
| |
| if (control instanceof Label) { |
| if (hasProgressbar(control)) { |
| ((Label) control).setText(String.format("Level %d", level)); |
| ((Label) control).setToolTipText( |
| String.format("Earn %d more %s to reach level %d", (nextLevelMinXP - skill.getExperience()), skill.getName(), level + 1)); |
| |
| } else { |
| ((Label) control).setText(String.format("%d", level)); |
| ((Label) control).setToolTipText( |
| String.format("Earn %d more %s points to reach level %d", (nextLevelMinXP - skill.getExperience()), skill.getName(), level + 1)); |
| } |
| |
| } else if (control instanceof ProgressBar) { |
| ((ProgressBar) control).setMinimum(currentLevelMinXP); |
| ((ProgressBar) control).setMaximum(nextLevelMinXP); |
| ((ProgressBar) control).setSelection(skill.getExperience()); |
| ((ProgressBar) control).setToolTipText(String.format("%d %s XP", skill.getExperience(), skill.getName())); |
| } |
| } |
| |
| private boolean hasProgressbar(Control control) { |
| final Object hasProgressbar = control.getData(FLAG_WITH_PROGRESSBAR); |
| return (hasProgressbar instanceof Boolean) ? (Boolean) hasProgressbar : true; |
| } |
| |
| private class SkillAdapter extends AdapterImpl { |
| @Override |
| public void notifyChanged(Notification msg) { |
| |
| if (msg.getNotifier() instanceof ISkill) |
| fUiUpdateThrottler.throttledExec(); |
| } |
| } |
| } |