Search in sources :

Example 1 with UISWTViewEventListener

use of com.biglybt.ui.swt.pif.UISWTViewEventListener in project BiglyBT by BiglySoftware.

the class BaseMDI method createEntryFromEventListener.

// @see MultipleDocumentInterfaceSWT#createEntryFromEventListener(java.lang.String, java.lang.Class, java.lang.String, boolean, java.lang.Object, java.lang.String)
@Override
public MdiEntry createEntryFromEventListener(final String parentID, Class<? extends UISWTViewEventListener> cla, String id, boolean closeable, Object data, String preferedAfterID) {
    final MultipleDocumentInterfaceSWT mdi = UIFunctionsManagerSWT.getUIFunctionsSWT().getMDISWT();
    if (mdi == null) {
        return null;
    }
    if (id == null) {
        id = cla.getName();
        int i = id.lastIndexOf('.');
        if (i > 0) {
            id = id.substring(i + 1);
        }
    }
    MdiEntry entry = mdi.getEntry(id);
    if (entry != null) {
        if (data != null) {
            entry.setDatasource(data);
        }
        return entry;
    }
    UISWTViewEventListener l = null;
    if (data != null) {
        try {
            Constructor<?> constructor = cla.getConstructor(new Class[] { data.getClass() });
            l = (UISWTViewEventListener) constructor.newInstance(new Object[] { data });
        } catch (Exception e) {
        }
    }
    try {
        if (l == null) {
            l = cla.newInstance();
        }
        return mdi.createEntryFromEventListener(parentID, l, id, closeable, data, preferedAfterID);
    } catch (Exception e) {
        Debug.out(e);
    }
    return null;
}
Also used : UISWTViewEventListener(com.biglybt.ui.swt.pif.UISWTViewEventListener)

Example 2 with UISWTViewEventListener

use of com.biglybt.ui.swt.pif.UISWTViewEventListener in project BiglyBT by BiglySoftware.

the class BaseMdiEntry method importStandAlone.

public static SWTSkinObjectContainer importStandAlone(SWTSkinObjectContainer soParent, Map<String, Object> map, Runnable callback) {
    String mdi_type = (String) map.get("mdi");
    String skin_ref = (String) map.get("skin_ref");
    String skin_id = (String) map.get("skin_id");
    SWTSkin skin = SWTSkinFactory.lookupSkin(skin_id);
    String parent_id = (String) map.get("parent_id");
    String id = (String) map.get("id");
    Object data_source = map.get("data_source");
    if (data_source != null) {
        if (data_source instanceof Map) {
            Map<String, Object> ds_map = (Map<String, Object>) data_source;
            if (ds_map != null) {
                ds_map = new HashMap<String, Object>(ds_map);
                ds_map.put("callback", callback);
            }
            data_source = ds_map == null ? null : DataSourceResolver.importDataSource(ds_map);
        } else if (data_source instanceof List) {
            List l = (List) data_source;
            String type = (String) l.get(0);
            Long value = (Long) l.get(1);
            if (type.equals("i")) {
                data_source = value.intValue();
            }
        }
    }
    int control_type = ((Number) map.get("control_type")).intValue();
    Map<String, Object> el_map = (Map<String, Object>) map.get("event_listener");
    UISWTViewEventListener event_listener = null;
    if (el_map != null) {
        try {
            String class_name = (String) el_map.get("name");
            if (class_name != null) {
                Class<? extends UISWTViewCoreEventListenerEx> cla = (Class<? extends UISWTViewCoreEventListenerEx>) Class.forName(class_name);
                List p_types = (List) el_map.get("p_types");
                List p_vals = (List) el_map.get("p_vals");
                if (p_types != null && !p_types.isEmpty()) {
                    List<Class> types = new ArrayList<>();
                    List<Object> args = new ArrayList<>();
                    for (int i = 0; i < p_types.size(); i++) {
                        String type = (String) p_types.get(i);
                        Object val = p_vals.get(i);
                        if (type.equals("bool")) {
                            types.add(boolean.class);
                            args.add(((Long) val) != 0);
                        } else if (type.equals("long")) {
                            types.add(long.class);
                            args.add((Long) val);
                        } else if (type.equals("string")) {
                            types.add(String.class);
                            args.add((String) val);
                        } else {
                            Debug.out("Unsupported type: " + type);
                        }
                    }
                    event_listener = cla.getConstructor(types.toArray(new Class<?>[types.size()])).newInstance(args.toArray(new Object[args.size()]));
                } else {
                    event_listener = cla.newInstance();
                }
            } else {
                String plugin_id = (String) el_map.get("plugin_id");
                PluginInterface pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByID(plugin_id);
                if (pi != null) {
                    String ipc_method = (String) el_map.get("ipc_method");
                    List p_types = (List) el_map.get("p_types");
                    List p_vals = (List) el_map.get("p_vals");
                    List<Object> args = new ArrayList<>();
                    if (p_types != null && !p_types.isEmpty()) {
                        List<Class> types = new ArrayList<>();
                        for (int i = 0; i < p_types.size(); i++) {
                            String type = (String) p_types.get(i);
                            Object val = p_vals.get(i);
                            if (type.equals("bool")) {
                                types.add(boolean.class);
                                args.add(((Long) val) != 0);
                            } else if (type.equals("long")) {
                                types.add(long.class);
                                args.add((Long) val);
                            } else if (type.equals("string")) {
                                types.add(String.class);
                                args.add((String) val);
                            } else {
                                Debug.out("Unsupported type: " + type);
                            }
                        }
                    }
                    event_listener = (UISWTViewEventListener) pi.getIPC().invoke(ipc_method, args.toArray(new Object[args.size()]));
                } else {
                    boolean try_install = false;
                    synchronized (installing_pids) {
                        if (!installing_pids.contains(plugin_id)) {
                            installing_pids.add(plugin_id);
                            try_install = true;
                        }
                    }
                    if (try_install) {
                        boolean went_async = false;
                        try {
                            UIFunctions uif = UIFunctionsManager.getUIFunctions();
                            String plugin_name = (String) el_map.get("plugin_name");
                            String remember_id = "basemdi.import.view.install." + plugin_id;
                            String title = MessageText.getString("plugin.required");
                            String text = MessageText.getString("plugin.required.info", new String[] { plugin_name });
                            UIFunctionsUserPrompter prompter = uif.getUserPrompter(title, text, new String[] { MessageText.getString("Button.yes"), MessageText.getString("Button.no") }, 0);
                            if (remember_id != null) {
                                prompter.setRemember(remember_id, false, MessageText.getString("MessageBoxWindow.nomoreprompting"));
                            }
                            prompter.setAutoCloseInMS(0);
                            prompter.open(null);
                            boolean install = prompter.waitUntilClosed() == 0;
                            if (install) {
                                went_async = true;
                                uif.installPlugin(plugin_id, "plugin.generic.install", new UIFunctions.actionListener() {

                                    @Override
                                    public void actionComplete(Object result) {
                                        try {
                                            if (callback != null) {
                                                if (result instanceof Boolean) {
                                                    if ((Boolean) result) {
                                                        callback.run();
                                                    }
                                                }
                                            }
                                        } finally {
                                            synchronized (installing_pids) {
                                                installing_pids.remove(plugin_id);
                                            }
                                        }
                                    }
                                });
                            }
                        } finally {
                            if (!went_async) {
                                synchronized (installing_pids) {
                                    installing_pids.remove(plugin_id);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
    }
    if (mdi_type.equals("sidebar")) {
        return (SideBarEntrySWT.buildStandAlone(soParent, skin_ref, skin, parent_id, id, data_source, control_type, null, event_listener, true));
    } else {
        return (TabbedEntry.buildStandAlone(soParent, skin_ref, skin, parent_id, id, data_source, control_type, null, event_listener, true));
    }
}
Also used : UIFunctionsUserPrompter(com.biglybt.ui.UIFunctionsUserPrompter) UIFunctions(com.biglybt.ui.UIFunctions) List(java.util.List) PluginInterface(com.biglybt.pif.PluginInterface) UISWTViewEventListener(com.biglybt.ui.swt.pif.UISWTViewEventListener) SWTSkin(com.biglybt.ui.swt.skin.SWTSkin) SWTSkinObject(com.biglybt.ui.swt.skin.SWTSkinObject) PluginUISWTSkinObject(com.biglybt.ui.swt.pif.PluginUISWTSkinObject)

Example 3 with UISWTViewEventListener

use of com.biglybt.ui.swt.pif.UISWTViewEventListener in project BiglyBT by BiglySoftware.

the class TabbedEntry method buildStandAlone.

public static SWTSkinObjectContainer buildStandAlone(SWTSkinObjectContainer soParent, String skinRef, SWTSkin skin, String parentID, String id, Object datasource, int controlType, CTabItem swtItem, UISWTViewEventListener original_event_listener, boolean listener_is_new) {
    Control control = null;
    // SWTSkin skin = soParent.getSkin();
    Composite parent = soParent.getComposite();
    if (skinRef != null) {
        Shell shell = parent.getShell();
        Cursor cursor = shell.getCursor();
        try {
            shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
            // wrap skinRef with a container that we control visibility of
            // (invisible by default)
            SWTSkinObjectContainer soContents = (SWTSkinObjectContainer) skin.createSkinObject("MdiContents." + uniqueNumber++, SO_ID_ENTRY_WRAPPER, soParent, null);
            SWTSkinObject skinObject = skin.createSkinObject(id, skinRef, soContents, datasource);
            control = skinObject.getControl();
            control.setLayoutData(Utils.getFilledFormData());
            control.getParent().layout(true, true);
            soContents.setVisible(true);
            return (soContents);
        } finally {
            shell.setCursor(cursor);
        }
    } else {
        if ((original_event_listener instanceof UISWTViewCoreEventListenerEx && ((UISWTViewCoreEventListenerEx) original_event_listener).isCloneable()) || (original_event_listener instanceof UISWTViewEventListenerEx)) {
            final UISWTViewImpl view = new UISWTViewImpl(id, parentID, true);
            final UISWTViewEventListener event_listener = listener_is_new ? original_event_listener : original_event_listener instanceof UISWTViewEventListenerEx ? ((UISWTViewEventListenerEx) original_event_listener).getClone() : ((UISWTViewCoreEventListenerEx) original_event_listener).getClone();
            try {
                view.setEventListener(event_listener, false);
            } catch (Throwable e) {
                // shouldn't happen as we aren't asking for 'create' to occur which means it can't fail
                Debug.out(e);
            }
            view.setDatasource(datasource);
            try {
                SWTSkinObjectContainer soContents = (SWTSkinObjectContainer) skin.createSkinObject("MdiIView." + uniqueNumber++, SO_ID_ENTRY_WRAPPER, soParent);
                parent.setBackgroundMode(SWT.INHERIT_NONE);
                final Composite viewComposite = soContents.getComposite();
                boolean doGridLayout = true;
                if (controlType == CONTROLTYPE_SKINOBJECT) {
                    doGridLayout = false;
                }
                // SWT.COLOR_WIDGET_FOREGROUND));
                if (doGridLayout) {
                    GridLayout gridLayout = new GridLayout();
                    gridLayout.horizontalSpacing = gridLayout.verticalSpacing = gridLayout.marginHeight = gridLayout.marginWidth = 0;
                    viewComposite.setLayout(gridLayout);
                    viewComposite.setLayoutData(Utils.getFilledFormData());
                }
                view.setPluginSkinObject(soContents);
                view.initialize(viewComposite);
                // without this some views get messed up layouts (chat view for example)
                viewComposite.setData(Utils.RELAYOUT_UP_STOP_HERE, true);
                soContents.addListener(new SWTSkinObjectListener() {

                    @Override
                    public Object eventOccured(SWTSkinObject skinObject, int eventType, Object params) {
                        if (eventType == SWTSkinObjectListener.EVENT_OBFUSCATE) {
                            Map data = new HashMap();
                            data.put("image", (Image) params);
                            data.put("obfuscateTitle", false);
                            view.triggerEvent(UISWTViewEvent.TYPE_OBFUSCATE, data);
                        }
                        return null;
                    }
                });
                // swtItem.setText(view.getFullTitle());
                Composite iviewComposite = view.getComposite();
                control = iviewComposite;
                // that instead of form)
                if (doGridLayout) {
                    Object existingLayoutData = iviewComposite.getLayoutData();
                    Object existingParentLayoutData = iviewComposite.getParent().getLayoutData();
                    if (existingLayoutData == null || !(existingLayoutData instanceof GridData) && (existingParentLayoutData instanceof GridLayout)) {
                        GridData gridData = new GridData(GridData.FILL_BOTH);
                        iviewComposite.setLayoutData(gridData);
                    }
                }
                parent.layout(true, true);
                final UIUpdater updater = UIUpdaterSWT.getInstance();
                if (updater != null) {
                    updater.addUpdater(new UIUpdatable() {

                        @Override
                        public void updateUI() {
                            if (viewComposite.isDisposed()) {
                                updater.removeUpdater(this);
                            } else {
                                view.triggerEvent(UISWTViewEvent.TYPE_REFRESH, null);
                            }
                        }

                        @Override
                        public String getUpdateUIName() {
                            return ("popout");
                        }
                    });
                    if (event_listener instanceof IViewRequiresPeriodicUpdates) {
                        updater.addPeriodicUpdater(new UIUpdatable() {

                            @Override
                            public void updateUI() {
                                if (viewComposite.isDisposed()) {
                                    updater.removePeriodicUpdater(this);
                                } else {
                                    event_listener.eventOccurred(new UISWTViewEvent() {

                                        @Override
                                        public UISWTView getView() {
                                            return null;
                                        }

                                        @Override
                                        public int getType() {
                                            return (StatsView.EVENT_PERIODIC_UPDATE);
                                        }

                                        @Override
                                        public Object getData() {
                                            return null;
                                        }
                                    });
                                }
                            }

                            @Override
                            public String getUpdateUIName() {
                                return ("popout");
                            }
                        });
                    }
                }
                soContents.setVisible(true);
                view.triggerEvent(UISWTViewEvent.TYPE_FOCUSGAINED, null);
                iviewComposite.addDisposeListener(new DisposeListener() {

                    @Override
                    public void widgetDisposed(DisposeEvent arg0) {
                        view.triggerEvent(UISWTViewEvent.TYPE_DESTROY, null);
                    }
                });
                return (soContents);
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    }
    return (null);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) HashMap(java.util.HashMap) SWTSkinObjectListener(com.biglybt.ui.swt.skin.SWTSkinObjectListener) Cursor(org.eclipse.swt.graphics.Cursor) Image(org.eclipse.swt.graphics.Image) MdiEntryVitalityImage(com.biglybt.ui.mdi.MdiEntryVitalityImage) ObfuscateImage(com.biglybt.ui.swt.debug.ObfuscateImage) DisposeEvent(org.eclipse.swt.events.DisposeEvent) SWTSkinObject(com.biglybt.ui.swt.skin.SWTSkinObject) GridLayout(org.eclipse.swt.layout.GridLayout) SWTSkinObjectContainer(com.biglybt.ui.swt.skin.SWTSkinObjectContainer) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) UISWTViewEventListenerEx(com.biglybt.ui.swt.pif.UISWTViewEventListenerEx) UIUpdatable(com.biglybt.ui.common.updater.UIUpdatable) UISWTViewEventListener(com.biglybt.ui.swt.pif.UISWTViewEventListener) Point(org.eclipse.swt.graphics.Point) UISWTViewImpl(com.biglybt.ui.swt.pifimpl.UISWTViewImpl) GridData(org.eclipse.swt.layout.GridData) SWTSkinObject(com.biglybt.ui.swt.skin.SWTSkinObject) UIUpdater(com.biglybt.ui.common.updater.UIUpdater) HashMap(java.util.HashMap) Map(java.util.Map) UISWTViewCoreEventListenerEx(com.biglybt.ui.swt.pifimpl.UISWTViewCoreEventListenerEx) IViewRequiresPeriodicUpdates(com.biglybt.ui.swt.views.IViewRequiresPeriodicUpdates)

Example 4 with UISWTViewEventListener

use of com.biglybt.ui.swt.pif.UISWTViewEventListener in project BiglyBT by BiglySoftware.

the class UISWTViewEventListenerHolder method eventOccurred.

@Override
public boolean eventOccurred(UISWTViewEvent event) {
    if (listener == null) {
        UISWTViewEventListener eventListener = null;
        synchronized (this) {
            int type = event.getType();
            if (type == UISWTViewEvent.TYPE_CREATE) {
                try {
                    eventListener = cla.newInstance();
                    UISWTView view = event.getView();
                    if (eventListener instanceof UISWTViewCoreEventListener) {
                        if (view instanceof UISWTViewCore) {
                            UISWTViewCore coreView = (UISWTViewCore) view;
                            coreView.setUseCoreDataSource(true);
                        }
                    }
                    if (mapSWTViewToEventListener == null) {
                        mapSWTViewToEventListener = new HashMap<>();
                    }
                    mapSWTViewToEventListener.put(view, eventListener);
                    if (datasource != null) {
                        if (view instanceof UISWTViewImpl) {
                            UISWTViewImpl swtView = (UISWTViewImpl) view;
                            swtView.triggerEventRaw(UISWTViewEvent.TYPE_DATASOURCE_CHANGED, PluginCoreUtils.convert(datasource, ((UISWTViewImpl) view).useCoreDataSource()));
                        } else {
                            view.triggerEvent(UISWTViewEvent.TYPE_DATASOURCE_CHANGED, datasource);
                        }
                    }
                } catch (Exception e) {
                    Debug.out(e);
                    return false;
                }
            } else if (type == UISWTViewEvent.TYPE_DATASOURCE_CHANGED) {
                datasource = event.getData();
            }
            if (mapSWTViewToEventListener != null) {
                if (type == UISWTViewEvent.TYPE_DESTROY) {
                    eventListener = mapSWTViewToEventListener.remove(event.getView());
                } else {
                    eventListener = mapSWTViewToEventListener.get(event.getView());
                }
                Iterator<Map.Entry<UISWTView, UISWTViewEventListener>> it = mapSWTViewToEventListener.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<UISWTView, UISWTViewEventListener> entry = it.next();
                    UISWTView v = entry.getKey();
                    if (v instanceof UISWTViewImpl) {
                        UISWTViewImpl impl = (UISWTViewImpl) v;
                        if (impl.isDisposed()) {
                            Debug.out("removing orphan from event holder: " + impl);
                            it.remove();
                        }
                    }
                }
            }
        }
        if (eventListener == null) {
            return false;
        }
        return eventListener.eventOccurred(event);
    } else if (event.getType() == UISWTViewEvent.TYPE_CREATE && (listener instanceof UISWTViewCoreEventListener)) {
        if (event.getView() instanceof UISWTViewCore) {
            UISWTViewCore coreView = (UISWTViewCore) event.getView();
            coreView.setUseCoreDataSource(true);
        }
    }
    return (listener.eventOccurred(event));
}
Also used : UISWTView(com.biglybt.ui.swt.pif.UISWTView) UISWTViewEventListener(com.biglybt.ui.swt.pif.UISWTViewEventListener) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UISWTViewEventListener

use of com.biglybt.ui.swt.pif.UISWTViewEventListener in project BiglyBT by BiglySoftware.

the class UISWTViewImpl method setEventListener.

public void setEventListener(UISWTViewEventListener _eventListener, boolean doCreate) throws UISWTViewEventCancelledException {
    if (this.eventListener instanceof UISWTViewEventListenerHolder) {
        ((UISWTViewEventListenerHolder) this.eventListener).removeListener(this);
    }
    this.eventListener = _eventListener;
    if (eventListener == null) {
        return;
    }
    if (_eventListener instanceof UISWTViewEventListenerHolder) {
        UISWTViewEventListenerHolder h = (UISWTViewEventListenerHolder) _eventListener;
        UISWTViewEventListener delegatedEventListener = h.getDelegatedEventListener(this);
        if (delegatedEventListener != null) {
            h.removeListener(this);
            this.eventListener = delegatedEventListener;
        }
    }
    if (eventListener instanceof IViewAlwaysInitialize) {
        delayInitializeToFirstActivate = false;
    }
    if (eventListener instanceof UISWTViewCoreEventListener) {
        setUseCoreDataSource(true);
    }
    // there's a bunch of crap out there that assumes that data is the view object :(
    if (doCreate && !triggerBooleanEvent(UISWTViewEvent.TYPE_CREATE, this)) {
        throw new UISWTViewEventCancelledException();
    }
// <<
}
Also used : IViewAlwaysInitialize(com.biglybt.ui.swt.views.IViewAlwaysInitialize) UISWTViewEventListener(com.biglybt.ui.swt.pif.UISWTViewEventListener)

Aggregations

UISWTViewEventListener (com.biglybt.ui.swt.pif.UISWTViewEventListener)14 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)4 SWTSkinObject (com.biglybt.ui.swt.skin.SWTSkinObject)4 ObfuscateImage (com.biglybt.ui.swt.debug.ObfuscateImage)3 PluginUISWTSkinObject (com.biglybt.ui.swt.pif.PluginUISWTSkinObject)3 UISWTViewEventListenerEx (com.biglybt.ui.swt.pif.UISWTViewEventListenerEx)3 UISWTViewImpl (com.biglybt.ui.swt.pifimpl.UISWTViewImpl)3 Map (java.util.Map)3 Point (org.eclipse.swt.graphics.Point)3 DownloadManager (com.biglybt.core.download.DownloadManager)2 PluginInterface (com.biglybt.pif.PluginInterface)2 UIFunctions (com.biglybt.ui.UIFunctions)2 UIUpdatable (com.biglybt.ui.common.updater.UIUpdatable)2 UIUpdater (com.biglybt.ui.common.updater.UIUpdater)2 ViewTitleInfo (com.biglybt.ui.common.viewtitleinfo.ViewTitleInfo)2 MdiEntryVitalityImage (com.biglybt.ui.mdi.MdiEntryVitalityImage)2 UISWTView (com.biglybt.ui.swt.pif.UISWTView)2 UISWTViewCore (com.biglybt.ui.swt.pifimpl.UISWTViewCore)2 UISWTViewEventListenerHolder (com.biglybt.ui.swt.pifimpl.UISWTViewEventListenerHolder)2 HashMap (java.util.HashMap)2