1 package net.mograsim.plugin.nature;
3 import java.util.Collections;
4 import java.util.HashSet;
6 import java.util.Optional;
8 import java.util.function.Consumer;
9 import java.util.function.Function;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.jface.viewers.ComboViewer;
13 import org.eclipse.jface.viewers.IStructuredContentProvider;
14 import org.eclipse.jface.viewers.LabelProvider;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.jface.viewers.ViewerComparator;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
22 import net.mograsim.machine.MachineDefinition;
23 import net.mograsim.machine.MachineRegistry;
25 public final class MachineContextSwtTools
27 private static final Map<String, MachineDefinition> INSTALLED_MACHINES = MachineRegistry.getInstalledMachines();
28 private static final Map<IProject, MachineContext> PROJECT_MACHINE_CONTEXTS = ProjectMachineContext.getAllProjectMachineContexts();
30 private MachineContextSwtTools()
35 public static MachineCombo createMachineSelector(Composite parent, int style)
37 return new MachineCombo(parent, style);
40 public static MograsimProjectCombo createMograsimProjectSelector(Composite parent, int style)
42 return new MograsimProjectCombo(parent, style);
45 public abstract static class AdvancedCombo<T>
47 final ComboViewer combo;
48 private Set<Consumer<T>> listeners;
50 public AdvancedCombo(Composite parent, Function<T, String> labelProvider)
52 this(parent, SWT.NONE, labelProvider);
55 public AdvancedCombo(Composite parent, int style, Function<T, String> labelProvider)
57 listeners = Collections.synchronizedSet(new HashSet<>());
58 combo = new ComboViewer(parent, style);
59 combo.addSelectionChangedListener(e -> updateSelection());
60 combo.setComparator(new ViewerComparator());
61 combo.setLabelProvider(new LabelProvider()
63 @SuppressWarnings("unchecked")
65 public String getText(Object element)
69 return labelProvider.apply((T) element);
71 catch (ClassCastException e)
73 return "Invalid Element: " + e.getLocalizedMessage();
79 public final ComboViewer getCombo()
84 public boolean setSelection(T newSelected)
86 if (newSelected == null)
87 combo.setSelection(new StructuredSelection(), true);
89 combo.setSelection(new StructuredSelection(newSelected), true); // TODO: can this cause errors or other problems?
90 return isValidSelection();
93 @SuppressWarnings("unchecked")
94 public T getSelection()
96 return (T) combo.getStructuredSelection().getFirstElement();
99 private void updateSelection()
101 T active = getSelection();
102 listeners.forEach(l -> l.accept(active));
105 public final void addListener(Consumer<T> listener)
107 listeners.add(listener);
110 public final void removeListener(Consumer<T> listener)
112 listeners.remove(listener);
115 public void refreshContent()
117 Display.getDefault().asyncExec(combo::refresh);
120 public abstract boolean isValidSelection();
123 public static class MachineCombo extends AdvancedCombo<MachineDefinition>
125 private static final Set<MachineCombo> machineComboListeners = Collections.synchronizedSet(new HashSet<>());
129 MachineRegistry.addMachineRegistryListener(newMap -> machineComboListeners.forEach(AdvancedCombo::refreshContent));
132 public MachineCombo(Composite parent)
134 this(parent, SWT.NONE);
137 public MachineCombo(Composite parent, int style)
139 super(parent, style, MachineDefinition::getId);
140 combo.setContentProvider(new IStructuredContentProvider()
143 public void dispose()
145 machineComboListeners.remove(MachineCombo.this);
149 public Object[] getElements(Object inputElement)
151 return INSTALLED_MACHINES.values().toArray();
154 combo.setInput(this);
155 machineComboListeners.add(this);
159 public boolean isValidSelection()
161 MachineDefinition md = super.getSelection();
164 return MachineRegistry.getMachine(md.getId()) != null;
168 public static class MograsimProjectCombo extends AdvancedCombo<MachineContext>
170 private static final Set<MograsimProjectCombo> projectComboListeners = Collections.synchronizedSet(new HashSet<>());
174 ProjectMachineContext.addProjectContextListener(projectEvent -> projectComboListeners.forEach(AdvancedCombo::refreshContent));
177 public MograsimProjectCombo(Composite parent)
179 this(parent, SWT.NONE);
182 public MograsimProjectCombo(Composite parent, int style)
184 super(parent, style, mc -> mc.getProject().getName());
185 combo.setContentProvider(new IStructuredContentProvider()
188 public void dispose()
190 projectComboListeners.remove(MograsimProjectCombo.this);
194 public Object[] getElements(Object inputElement)
196 return PROJECT_MACHINE_CONTEXTS.values().stream().filter(MachineContext::isCurrentyValid).toArray();
199 combo.setInput(this);
200 projectComboListeners.add(this);
204 public boolean isValidSelection()
206 MachineContext mc = super.getSelection();
209 return mc.isCurrentyValid();
214 * XXX: is this now of no use?
216 static Optional<String> getSelection(Combo c)
218 int selectionIndex = c.getSelectionIndex();
219 if (selectionIndex == -1)
220 return Optional.empty();
221 return Optional.of(c.getItem(selectionIndex));