| package org.eclipse.skills.ui.status; |
| |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.skills.model.ISkill; |
| import org.eclipse.skills.model.ISkillsPackage; |
| import org.eclipse.skills.service.ISkillService; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.menus.WorkbenchWindowControlContribution; |
| |
| public class StatusBarProgressContribution extends WorkbenchWindowControlContribution { |
| |
| public StatusBarProgressContribution() { |
| } |
| |
| public StatusBarProgressContribution(String id) { |
| super(id); |
| } |
| |
| @Override |
| protected Control createControl(Composite parent) { |
| final CustomDrawProgressBar progressBar = new CustomDrawProgressBar(parent); |
| |
| progressBar.setMinimum(0); |
| progressBar.setMaximum(100); |
| progressBar.setSelection(0); |
| progressBar.setStatusText("Level 1"); |
| progressBar.setToolTipText(""); |
| progressBar.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_YELLOW)); |
| |
| addModelListener(progressBar); |
| |
| return progressBar; |
| } |
| |
| private void addModelListener(CustomDrawProgressBar progressBar) { |
| final ISkillService skillService = PlatformUI.getWorkbench().getService(ISkillService.class); |
| skillService.getUser().getExperience().eAdapters().add(new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification msg) { |
| if (ISkillsPackage.eINSTANCE.getSkill_Experience().equals(msg.getFeature())) { |
| if (msg.getNotifier() instanceof ISkill) |
| updateProgressBar(progressBar, msg); |
| } |
| } |
| }); |
| } |
| |
| private void updateProgressBar(CustomDrawProgressBar progressBar, Notification msg) { |
| final ISkill skill = (ISkill) msg.getNotifier(); |
| final int currentLevel = skill.getProgression().getLevel(skill.getExperience()); |
| final int levelStart = skill.getProgression().getMinimumXpForLevel(currentLevel); |
| final int nextLevelStart = skill.getProgression().getMinimumXpForLevel(currentLevel + 1); |
| |
| Display.getDefault().asyncExec(() -> { |
| progressBar.setMinimum(levelStart); |
| progressBar.setMaximum(nextLevelStart); |
| progressBar.setSelection(skill.getExperience()); |
| |
| progressBar.setStatusText(String.format("Level %d", currentLevel)); |
| progressBar.setToolTipText(String.format("%d XP to reach next level", (nextLevelStart - skill.getExperience()))); |
| |
| progressBar.redraw(); |
| }); |
| } |
| |
| } |