Search in sources :

Example 66 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class RemotePieceDistributionView method dataSourceChanged.

/* (non-Javadoc)
	 * @see com.biglybt.ui.swt.views.PieceDistributionView#dataSourceChanged(java.lang.Object)
	 */
@Override
public void dataSourceChanged(Object newDataSource) {
    if (newDataSource instanceof Object[] && ((Object[]) newDataSource).length > 0) {
        newDataSource = ((Object[]) newDataSource)[0];
    }
    if (newDataSource instanceof PEPeer) {
        peer = (PEPeer) newDataSource;
        pem = peer.getManager();
    } else {
        peer = null;
        pem = null;
    }
    Utils.execSWTThread(new AERunnable() {

        @Override
        public void runSupport() {
            refresh();
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) PEPeer(com.biglybt.core.peer.PEPeer)

Example 67 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class VuzeMessageBox method open.

/* (non-Javadoc)
	 * @see UIFunctionsUserPrompter#open(UserPrompterResultListener)
	 */
@Override
public void open(final UserPrompterResultListener l) {
    opened = true;
    Utils.execSWTThread(new AERunnable() {

        @Override
        public void runSupport() {
            // catch someone calling close() while we are opening
            if (closed) {
                return;
            }
            synchronized (VuzeMessageBox.this) {
                _open(l);
            }
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable)

Example 68 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class CocoaJavaBridge method executeScriptWithAsync.

/**
 * <p>Asynchronously executes a new instance of NSAppleScript</p>
 * <p>This method always returns a "true" event descriptor. Callbacks are currently unsupported
 * , so in the event of an error, the logger is autuomatically notified.</p>
 * <p>The thread's runSupport method is wrapped in an autorelease pool. If there are
 * no format parameters, MessageFormat is not used to parse the format string, and
 * the format string will be treated as the source itself.</p>
 * @see com.biglybt.core.util.AEThread#runSupport()
 * @see MessageFormat#format(String, Object...)
 * @see NSAppleScript#execute(com.apple.cocoa.foundation.NSMutableDictionary)
 * @return NSAppleEventDescriptor.descriptorWithBoolean(true)
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
protected final Object executeScriptWithAsync(final String scriptFormat, final Object[] params) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    final AERunnable worker = new AERunnable() {

        @Override
        public void runSupport() {
            try {
                int pool = NSAutoreleasePool_push();
                long start = System.currentTimeMillis();
                String src;
                if (params == null || params.length == 0) {
                    src = scriptFormat;
                } else {
                    src = MessageFormat.format(scriptFormat, params);
                }
                Debug.outNoStack("Executing: \n" + src);
                Object /*NSMutableDictionary*/
                errorInfo = new_NSMutableDictionary();
                if (NSAppleScript_execute(new_NSAppleScript(src), errorInfo) == null) {
                    Debug.out(String.valueOf(NSMutableDictionary_objectForKey(errorInfo, NSAppleScript_AppleScriptErrorMessage)));
                // logWarning(String.valueOf(errorInfo.objectForKey(NSAppleScript.AppleScriptErrorBriefMessage)));
                }
                Debug.outNoStack(MessageFormat.format("Elapsed time: {0}ms\n", new Object[] { new Long(System.currentTimeMillis() - start) }));
                NSAutoreleasePool_pop(pool);
            } catch (Throwable t) {
                Debug.out(t);
            }
        }
    };
    AEThread t = new AEThread("ScriptObject", true) {

        @Override
        public void runSupport() {
            scriptDispatcher.exec(worker);
        }
    };
    t.setPriority(Thread.NORM_PRIORITY - 1);
    t.start();
    return methNSAppleEventDescriptor_descriptorWithBoolean.invoke(null, true);
// return NSAppleEventDescriptor.descriptorWithBoolean(true);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) AEThread(com.biglybt.core.util.AEThread)

Example 69 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class Legend method createLegendComposite.

public static Composite createLegendComposite(final Composite panel, final Color[] blockColors, final String[] keys, final String[] key_texts, Object layoutData, boolean horizontal, final LegendListener listener) {
    final ConfigurationManager config = ConfigurationManager.getInstance();
    if (blockColors.length != keys.length)
        return null;
    final Color[] defaultColors = new Color[blockColors.length];
    final ParameterListener[] paramListeners = new ParameterListener[keys.length];
    System.arraycopy(blockColors, 0, defaultColors, 0, blockColors.length);
    Composite legend = new Composite(panel, SWT.NONE);
    if (layoutData != null)
        legend.setLayoutData(layoutData);
    RowLayout layout = new RowLayout(horizontal ? SWT.HORIZONTAL : SWT.VERTICAL);
    layout.wrap = true;
    layout.marginBottom = 0;
    layout.marginTop = 0;
    layout.marginLeft = 0;
    layout.marginRight = 0;
    layout.spacing = 0;
    legend.setLayout(layout);
    RowData data;
    final int[] hover_state = { -1, 0 };
    for (int i = 0; i < blockColors.length; i++) {
        int r = config.getIntParameter(keys[i] + ".red", -1);
        if (r >= 0) {
            int g = config.getIntParameter(keys[i] + ".green");
            int b = config.getIntParameter(keys[i] + ".blue");
            Color color = ColorCache.getColor(panel.getDisplay(), r, g, b);
            blockColors[i] = color;
        }
        Composite colorSet = new Composite(legend, SWT.NONE);
        colorSet.setLayout(new RowLayout(SWT.HORIZONTAL));
        final Canvas cColor = new Canvas(colorSet, SWT.BORDER);
        cColor.setData("Index", new Integer(i));
        Messages.setLanguageTooltip(cColor, "label.click.to.change.tooltip");
        // XXX Use paint instead of setBackgrond, because OSX does translucent
        // crap
        cColor.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                int i = ((Integer) cColor.getData("Index")).intValue();
                e.gc.setBackground(blockColors[i]);
                e.gc.fillRectangle(e.x, e.y, e.width, e.height);
            }
        });
        cColor.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                Integer iIndex = (Integer) cColor.getData("Index");
                if (iIndex == null)
                    return;
                int index = iIndex.intValue();
                if (e.button == 1) {
                    RGB rgb = Utils.showColorDialog(panel, blockColors[index].getRGB());
                    if (rgb != null) {
                        config.setRGBParameter(keys[index], rgb.red, rgb.green, rgb.blue, true);
                    }
                } else {
                    config.removeRGBParameter(keys[index]);
                }
            }
        });
        final Label lblDesc = new Label(colorSet, SWT.NULL);
        if (key_texts == null) {
            Messages.setLanguageText(lblDesc, keys[i]);
        } else {
            lblDesc.setText(key_texts[i]);
        }
        if (listener != null) {
            Messages.setLanguageTooltip(lblDesc, "label.click.to.showhide.tooltip");
            final int f_i = i;
            lblDesc.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseUp(MouseEvent e) {
                    boolean vis = !config.getBooleanParameter(keys[f_i] + ".vis", true);
                    config.setParameter(keys[f_i] + ".vis", vis);
                    listener.visibilityChange(vis, f_i);
                    lblDesc.setForeground(vis ? Colors.getSystemColor(lblDesc.getDisplay(), SWT.COLOR_BLACK) : Colors.grey);
                }
            });
            boolean vis = config.getBooleanParameter(keys[f_i] + ".vis", true);
            if (!vis) {
                listener.visibilityChange(vis, i);
                lblDesc.setForeground(Colors.grey);
            }
        }
        data = new RowData();
        data.width = 12;
        data.height = lblDesc.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 3;
        cColor.setLayoutData(data);
        // If color changes, update our legend
        config.addParameterListener(keys[i], paramListeners[i] = new ParameterListener() {

            @Override
            public void parameterChanged(String parameterName) {
                for (int j = 0; j < keys.length; j++) {
                    if (keys[j].equals(parameterName)) {
                        final int index = j;
                        final int r = config.getIntParameter(keys[j] + ".red", -1);
                        if (r >= 0) {
                            final int g = config.getIntParameter(keys[j] + ".green");
                            final int b = config.getIntParameter(keys[j] + ".blue");
                            Utils.execSWTThread(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    if (panel == null || panel.isDisposed())
                                        return;
                                    Color color = ColorCache.getColor(panel.getDisplay(), r, g, b);
                                    blockColors[index] = color;
                                    cColor.redraw();
                                }
                            });
                        } else {
                            Utils.execSWTThread(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    if (panel == null || panel.isDisposed())
                                        return;
                                    blockColors[index] = defaultColors[index];
                                    cColor.redraw();
                                }
                            });
                        }
                    }
                }
            }
        });
        if (listener != null) {
            final int f_i = i;
            Control[] controls = { colorSet, cColor, lblDesc };
            MouseTrackListener ml = new MouseTrackListener() {

                @Override
                public void mouseEnter(MouseEvent e) {
                    handleHover(listener, true, f_i, hover_state);
                }

                @Override
                public void mouseExit(MouseEvent e) {
                    handleHover(listener, false, f_i, hover_state);
                }

                @Override
                public void mouseHover(MouseEvent e) {
                }
            };
            for (Control c : controls) {
                c.addMouseTrackListener(ml);
            }
        }
    }
    legend.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            // System.arraycopy(defaultColors, 0, blockColors, 0, blockColors.length);
            for (int i = 0; i < keys.length; i++) {
                config.removeParameterListener(keys[i], paramListeners[i]);
            }
        }
    });
    List<Control> legControls = Utils.getAllChildren(legend);
    legControls.add(legend);
    String keyVis = keys == null || keys.length == 0 ? null : keys[0] + ".legend.hidden";
    Listener visListener = (Listener) (event) -> {
        if (keyVis != null) {
            COConfigurationManager.setParameter(keyVis, false);
        }
        legend.dispose();
        panel.layout(true, true);
        Composite kidsPanel;
        ;
        List<Control> kids = Utils.getAllChildren(panel);
        if (kids.size() == 0) {
            kidsPanel = panel.getParent();
            kids = Utils.getAllChildren(kidsPanel);
        } else {
            kidsPanel = panel;
        }
        List<Menu> showListenersAdded = new ArrayList<>();
        List<MenuItem> menuItemsAdded = new ArrayList<>();
        Listener showListener = new Listener() {

            public void handleEvent(Event ev) {
                Menu menu = (Menu) ev.widget;
                MenuItem[] items = menu.getItems();
                for (MenuItem x : items) {
                    if (x.getData("Legend.hacked") != null) {
                        return;
                    }
                }
                if (menu.getItemCount() > 0) {
                    menuItemsAdded.add(new MenuItem(menu, SWT.SEPARATOR));
                }
                MenuItem kmi = new MenuItem(menu, SWT.PUSH);
                kmi.setData("Legend.hacked", "");
                menuItemsAdded.add(kmi);
                Messages.setLanguageText(kmi, "menu.show.legend");
                kmi.addListener(SWT.Selection, (event) -> {
                    for (Menu x : showListenersAdded) {
                        x.removeListener(SWT.Show, this);
                    }
                    for (MenuItem x : menuItemsAdded) {
                        x.dispose();
                    }
                    if (keyVis != null) {
                        COConfigurationManager.setParameter(keyVis, true);
                    }
                    createLegendComposite(panel, blockColors, keys, key_texts, layoutData, horizontal, listener);
                    kidsPanel.layout(true, true);
                });
            }
        };
        for (Control kid : kids) {
            Menu km = kid.getMenu();
            if (km == null) {
                km = new Menu(kid);
                kid.setMenu(km);
            }
            km.addListener(SWT.Show, showListener);
            showListenersAdded.add(km);
        }
    };
    for (Control c : legControls) {
        Menu menu = new Menu(c);
        c.setMenu(menu);
        MenuItem mi = new MenuItem(menu, SWT.PUSH);
        Messages.setLanguageText(mi, "menu.hide.legend");
        mi.addListener(SWT.Selection, visListener);
    }
    if (keyVis != null && !COConfigurationManager.getBooleanParameter(keyVis, true)) {
        visListener.handleEvent(null);
    }
    return legend;
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) Listener(org.eclipse.swt.widgets.Listener) ParameterListener(com.biglybt.core.config.ParameterListener) Label(org.eclipse.swt.widgets.Label) ArrayList(java.util.ArrayList) RowData(org.eclipse.swt.layout.RowData) Control(org.eclipse.swt.widgets.Control) RowLayout(org.eclipse.swt.layout.RowLayout) Menu(org.eclipse.swt.widgets.Menu) COConfigurationManager(com.biglybt.core.config.COConfigurationManager) ConfigurationManager(com.biglybt.core.config.impl.ConfigurationManager) Composite(org.eclipse.swt.widgets.Composite) Color(org.eclipse.swt.graphics.Color) Canvas(org.eclipse.swt.widgets.Canvas) MenuItem(org.eclipse.swt.widgets.MenuItem) RGB(org.eclipse.swt.graphics.RGB) ParameterListener(com.biglybt.core.config.ParameterListener) Event(org.eclipse.swt.widgets.Event)

Example 70 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class PrivacyView method updatePeersEtc.

private void updatePeersEtc(final DownloadManager dm) {
    final PEPeerManager pm;
    if (dm != null) {
        pm = dm.getPeerManager();
    } else {
        pm = null;
    }
    Utils.execSWTThread(new AERunnable() {

        @Override
        public void runSupport() {
            if (peer_info == null || peer_info.isDisposed()) {
                return;
            }
            if (pm == null) {
                peer_info.setText(dm == null ? "" : MessageText.getString("privacy.view.dl.not.running"));
            } else {
                AEProxySelector proxy_selector = AEProxySelectorFactory.getSelector();
                Proxy proxy = proxy_selector.getActiveProxy();
                boolean socks_bad_incoming = false;
                List<PEPeer> peers = pm.getPeers();
                String[] all_nets = AENetworkClassifier.AT_NETWORKS;
                int[] counts = new int[all_nets.length];
                int incoming = 0;
                int outgoing = 0;
                int outgoing_connected = 0;
                for (PEPeer peer : peers) {
                    String net = PeerUtils.getNetwork(peer);
                    for (int i = 0; i < all_nets.length; i++) {
                        if (all_nets[i] == net) {
                            counts[i]++;
                            break;
                        }
                    }
                    boolean is_incoming = peer.isIncoming();
                    if (is_incoming) {
                        incoming++;
                    } else {
                        outgoing++;
                        if (peer.getPeerState() == PEPeer.TRANSFERING) {
                            outgoing_connected++;
                        }
                    }
                    if (proxy != null) {
                        if (is_incoming) {
                            if (!peer.isLANLocal()) {
                                try {
                                    if (InetAddress.getByAddress(HostNameToIPResolver.hostAddressToBytes(peer.getIp())).isLoopbackAddress()) {
                                        continue;
                                    }
                                } catch (Throwable e) {
                                }
                                socks_bad_incoming = true;
                                break;
                            }
                        }
                    }
                }
                String str = "";
                for (int i = 0; i < all_nets.length; i++) {
                    int num = counts[i];
                    if (num > 0) {
                        str += (str.length() == 0 ? "" : ", ") + all_nets[i] + "=" + num;
                    }
                }
                if (str.length() == 0) {
                    str = MessageText.getString("privacy.view.no.peers");
                } else {
                    str += ", " + MessageText.getString("label.incoming") + "=" + incoming + ", " + MessageText.getString("label.outgoing") + "=" + outgoing_connected + "/" + outgoing;
                }
                if (socks_bad_incoming) {
                    str += " (" + MessageText.getString("privacy.view.non.local.peer") + ")";
                }
                peer_info.setText(str);
            }
            updateVPNSocks();
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) Proxy(java.net.Proxy) PEPeer(com.biglybt.core.peer.PEPeer) AEProxySelector(com.biglybt.core.proxy.AEProxySelector) PEPeerManager(com.biglybt.core.peer.PEPeerManager) List(java.util.List)

Aggregations

AERunnable (com.biglybt.core.util.AERunnable)92 GridLayout (org.eclipse.swt.layout.GridLayout)12 DownloadManager (com.biglybt.core.download.DownloadManager)11 Image (org.eclipse.swt.graphics.Image)10 ArrayList (java.util.ArrayList)9 GridData (org.eclipse.swt.layout.GridData)9 CoreRunningListener (com.biglybt.core.CoreRunningListener)7 File (java.io.File)7 PEPeer (com.biglybt.core.peer.PEPeer)6 Shell (org.eclipse.swt.widgets.Shell)6 Core (com.biglybt.core.Core)5 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)5 Point (org.eclipse.swt.graphics.Point)5 PEPeerManager (com.biglybt.core.peer.PEPeerManager)4 Subscription (com.biglybt.core.subs.Subscription)3 TableColumnCore (com.biglybt.ui.common.table.TableColumnCore)3 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)3 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)3 MouseAdapter (org.eclipse.swt.events.MouseAdapter)3 MouseEvent (org.eclipse.swt.events.MouseEvent)3