package xyz.veronie.bgg.ui.parts; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import javax.inject.Inject; import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.EclipseContextFactory; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.workbench.modeling.EModelService; import org.eclipse.e4.ui.workbench.modeling.EPartService; import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.wb.swt.ResourceManager; import org.eclipse.wb.swt.SWTResourceManager; import xyz.veronie.bgg.result.BggApi; import xyz.veronie.bgg.result.ResultConfig; import xyz.veronie.bgg.result.ResultConfigManager; import xyz.veronie.bgg.result.Thing; import xyz.veronie.bgg.result.ThingProvider; import xyz.veronie.bgg.types.EventConstants; import xyz.veronie.bgg.types.ResultAction; import xyz.veronie.bgg.types.SourceFilter; import xyz.veronie.bgg.ui.filters.BggUserSourceFilter; import xyz.veronie.bgg.ui.filters.FamilySourceFilter; import xyz.veronie.bgg.ui.filters.GeeklistSourceFilter; import xyz.veronie.bgg.ui.helpers.BatColors; import xyz.veronie.bgg.ui.helpers.BatLayouts; import org.eclipse.swt.widgets.Label; public class FetchPart { @Inject private ResultConfigManager configManager; @Inject private IEventBroker eventBroker; @Inject private IEclipseContext context; @Inject private MApplication application; @Inject private EModelService modelService; @Inject private EPartService partService; @Inject private ThingProvider thingProvider; @Inject private BggApi bggApi; private Button btnBggUser; private Button btnFamily; private Button btnGeeklist; private Composite main; private Composite filterComposite; private Composite userFilterPage; private Composite familyPage; private StackLayout filterLayout; private Composite geeklistPage; private Composite centerComposite; private Label lblResultAction; @Inject public FetchPart() { } @PostConstruct public void postConstruct(Composite parent) { parent.setBackground(BatColors.getBackgroundColor()); parent.setBackgroundMode(SWT.INHERIT_FORCE); parent.setLayout(new GridLayout(1, false)); main = new Composite(parent, SWT.NONE); GridLayout gl_main = new GridLayout(1, false); BatLayouts.applyStandardSpacing(gl_main); main.setLayout(gl_main); main.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1)); Composite buttonRow = new Composite(main, SWT.NONE); buttonRow.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1)); GridLayout gl_buttonRow = new GridLayout(3, false); BatLayouts.applyStandardSpacing(gl_buttonRow); buttonRow.setLayout(gl_buttonRow); btnBggUser = new Button(buttonRow, SWT.NONE); btnBggUser.setToolTipText("Fetch by bgg user"); btnBggUser.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/noun_Meeple_60x60.png")); btnFamily = new Button(buttonRow, SWT.NONE); btnFamily.setToolTipText("Fetch by family"); btnFamily.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/noun_Family_60x60.png")); btnGeeklist = new Button(buttonRow, SWT.NONE); btnGeeklist.setToolTipText("Fetch by geeklist"); btnGeeklist.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/noun_List_60x60.png")); centerComposite = new Composite(main, SWT.NONE); centerComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); GridLayout gl_centerComposite = new GridLayout(1, false); BatLayouts.applyStandardSpacing(gl_centerComposite); centerComposite.setLayout(gl_centerComposite); createFilterComposites(centerComposite); // init filter using config manager SourceFilter source = configManager.getResultConfig().source; selectSource(source); createApplyToResultRow(); } private void createApplyToResultRow() { Composite applyComposite = new Composite(main, SWT.NONE); GridLayout gl_applyComposite = new GridLayout(5, false); BatLayouts.applyStandardSpacing(gl_applyComposite); applyComposite.setLayout(gl_applyComposite); applyComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 1)); lblResultAction = new Label(applyComposite, SWT.NONE); lblResultAction.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 5, 1)); lblResultAction.setText("How do you want to apply the result?"); Button btnReplace = new Button(applyComposite, SWT.NONE); btnReplace.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_replace_60x60.png")); btnReplace.setToolTipText("Replace"); btnReplace.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { fetchEntries(ResultAction.REPLACE); } }); Button btnOnlyNew = new Button(applyComposite, SWT.NONE); btnOnlyNew.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/only_new_60x60.png")); btnOnlyNew.setToolTipText("Keep only new"); btnOnlyNew.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { fetchEntries(ResultAction.ONLY_NEW); } }); Button btnAdd = new Button(applyComposite, SWT.NONE); btnAdd.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_add_60x60.png")); btnAdd.setToolTipText("Add"); btnAdd.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { fetchEntries(ResultAction.ADD); } }); Button btnIntersect = new Button(applyComposite, SWT.NONE); btnIntersect.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_intersect_60x60.png")); btnIntersect.setToolTipText("Intersect"); btnIntersect.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { fetchEntries(ResultAction.INTERSECT); } }); Button btnSubtract = new Button(applyComposite, SWT.NONE); btnSubtract.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_subtract_60x60.png")); btnSubtract.setToolTipText("Subtract"); btnSubtract.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { fetchEntries(ResultAction.SUBTRACT); } }); } private void createFilterComposites(Composite parent) { filterComposite = new Composite(parent, SWT.NONE); filterComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND)); filterComposite.setBackgroundMode(SWT.INHERIT_FORCE); filterLayout = new StackLayout(); filterComposite.setLayout(filterLayout); filterComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); userFilterPage = new Composite(filterComposite, SWT.FILL); GridLayout gl_userFilter = new GridLayout(2, false); BatLayouts.applyZeroSpacing(gl_userFilter); userFilterPage.setLayout(gl_userFilter); userFilterPage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); IEclipseContext userFilterCtx = EclipseContextFactory.create(); userFilterCtx.set(Composite.class, userFilterPage); // parent ContextInjectionFactory.make(BggUserSourceFilter.class, context, userFilterCtx); btnBggUser.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { selectSource(SourceFilter.BGG_USER); } @Override public void widgetDefaultSelected(SelectionEvent e) {} }); familyPage = new Composite(filterComposite, SWT.FILL); GridLayout gl_familyFilter = new GridLayout(2, false); BatLayouts.applyZeroSpacing(gl_familyFilter); familyPage.setLayout(gl_familyFilter); familyPage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); IEclipseContext familyFilterCtx = EclipseContextFactory.create(); familyFilterCtx.set(Composite.class, familyPage); // parent ContextInjectionFactory.make(FamilySourceFilter.class, context, familyFilterCtx);; btnFamily.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { selectSource(SourceFilter.FAMILY); } @Override public void widgetDefaultSelected(SelectionEvent e) {} }); geeklistPage = new Composite(filterComposite, SWT.FILL); GridLayout gl_geeklistFilter = new GridLayout(2, false); BatLayouts.applyZeroSpacing(gl_geeklistFilter); geeklistPage.setLayout(gl_geeklistFilter); geeklistPage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); IEclipseContext geeklistFilterCtx = EclipseContextFactory.create(); geeklistFilterCtx.set(Composite.class, geeklistPage); // parent ContextInjectionFactory.make(GeeklistSourceFilter.class, context, geeklistFilterCtx); btnGeeklist.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { selectSource(SourceFilter.GEEKLIST); } @Override public void widgetDefaultSelected(SelectionEvent e) {} }); } private void selectSource(SourceFilter source) { switch(source) { case BGG_USER: filterLayout.topControl = userFilterPage; btnBggUser.setBackground(BatColors.getButtonBgColor()); btnFamily.setBackground(null); btnGeeklist.setBackground(null); break; case FAMILY: filterLayout.topControl = familyPage; btnBggUser.setBackground(null); btnFamily.setBackground(BatColors.getButtonBgColor()); btnGeeklist.setBackground(null); break; case GEEKLIST: filterLayout.topControl = geeklistPage; btnBggUser.setBackground(null); btnFamily.setBackground(null); btnGeeklist.setBackground(BatColors.getButtonBgColor()); break; } filterComposite.pack(); centerComposite.layout(true,true); eventBroker.send(EventConstants.TOPIC_SOURCE_CHANGED, source); } private void fetchEntries(ResultAction action) { Object fetchId = getFetchId(); if(fetchId == null) return; try { SourceFilter source = configManager.getResultConfig().source; eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching " + source.toString() + " '" + fetchId + "'..."); ArrayList things = bggApi.getThings(source, fetchId); useThingsOnResult(things, action); goToHomePart(); } catch(IllegalArgumentException ex) { MessageDialog.openError(main.getShell(), "", ex.getMessage()); } } private Object getFetchId() { ResultConfig resultConfig = configManager.getResultConfig(); SourceFilter source = configManager.getResultConfig().source; if(source == SourceFilter.BGG_USER) { String user = resultConfig.user; if(user == null || user.isEmpty()) { MessageDialog.openError(main.getShell(), "", "Please enter a user name."); return null; } return user; } else if(source == SourceFilter.GEEKLIST) { Integer geeklistId = resultConfig.geeklistId; if(geeklistId == null) { MessageDialog.openError(main.getShell(), "", "Please enter a geeklist id."); return null; } return geeklistId; } else if(source == SourceFilter.FAMILY) { Integer familyId = resultConfig.familyId; if(familyId == null) { MessageDialog.openError(main.getShell(), "", "Please enter a family id."); return null; } return familyId; } return null; } private void useThingsOnResult(ArrayList things, ResultAction action) { switch(action) { case REPLACE: thingProvider.replaceThings(things); break; case ADD: thingProvider.addThings(things); break; case SUBTRACT: thingProvider.subtractThings(things); break; case INTERSECT: thingProvider.intersectThings(things); break; case ONLY_NEW: thingProvider.keepOnlyNew(things); break; } eventBroker.send(EventConstants.TOPIC_RESULT_CHANGED, ""); eventBroker.send(EventConstants.TOPIC_STATUS, "Fetched " + Integer.toString(things.size()) + " things."); } private void goToHomePart() { // TODO Auto-generated method stub List parts = modelService.findElements(application, "xyz.veronie.bgg.ui.part.batmain", MPart.class); if(parts != null && parts.size() >= 1) { MPart fetchPart = parts.get(0); partService.showPart(fetchPart, PartState.ACTIVATE); } } }