|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package org.scijava.nwidget; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.function.Predicate; |
| 37 | + |
| 38 | +import org.scijava.param.Parameter; |
| 39 | +import org.scijava.plugin.Plugin; |
| 40 | +import org.scijava.plugin.PluginInfo; |
| 41 | +import org.scijava.plugin.PluginService; |
| 42 | +import org.scijava.service.AbstractService; |
| 43 | +import org.scijava.service.Service; |
| 44 | +import org.scijava.struct.Member; |
| 45 | +import org.scijava.struct.StructInstance; |
| 46 | + |
| 47 | +@Plugin(type = Service.class) |
| 48 | +public class NDefaultWidgetService extends AbstractService implements |
| 49 | + NWidgetService |
| 50 | +{ |
| 51 | + |
| 52 | + @Parameter |
| 53 | + private PluginService pluginService; |
| 54 | + |
| 55 | + @Override |
| 56 | + public <C, W extends NWidget> NWidgetPanel<C> |
| 57 | + createPanel(final StructInstance<C> struct, final Predicate<Member<?>> included, |
| 58 | + final Predicate<Member<?>> required, |
| 59 | + final NWidgetPanelFactory<C, W> factory) |
| 60 | + { |
| 61 | + final ArrayList<W> widgets = createWidgets(struct, factory.widgetType(), |
| 62 | + included, required); |
| 63 | + |
| 64 | + return factory.create(struct, widgets); |
| 65 | + } |
| 66 | + |
| 67 | + // -- Helper methods -- |
| 68 | + |
| 69 | + private <W extends NWidget> ArrayList<W> createWidgets(final StructInstance<?> struct, |
| 70 | + final Class<W> widgetType, final Predicate<Member<?>> included, |
| 71 | + final Predicate<Member<?>> required) |
| 72 | + { |
| 73 | + final ArrayList<W> widgets = new ArrayList<>(); |
| 74 | + |
| 75 | + for (final Member<?> item : struct.struct().members()) { |
| 76 | + if (!included.test(item)) continue; |
| 77 | + |
| 78 | + final W widget = createWidget(struct, item, widgetType); |
| 79 | + if (widget == null && required.test(item)) { |
| 80 | + // fail - FIXME |
| 81 | + throw new RuntimeException(item + " is required but none exist."); |
| 82 | + } |
| 83 | + if (widget != null) widgets.add(widget); |
| 84 | + } |
| 85 | + return widgets; |
| 86 | + } |
| 87 | + |
| 88 | + private <T extends NWidget> T createWidget(final StructInstance<?> struct, |
| 89 | + final Member<?> item, final Class<T> widgetType) |
| 90 | + { |
| 91 | + // FIXME FIX THIS CRAP |
| 92 | + for (final PluginInfo<NWidgetFactory> info : pluginService.getPluginsOfType( |
| 93 | + NWidgetFactory.class)) |
| 94 | + { |
| 95 | + final NWidgetFactory<?> factory = pluginService.createInstance(info); |
| 96 | + if (widgetType.isAssignableFrom(factory.widgetType())) continue; |
| 97 | + if (!factory.supports(item)) continue; |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + final T tWidget = (T) factory.create(struct, item.getKey()); |
| 100 | + return tWidget; |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | +} |
0 commit comments