Search in sources :

Example 6 with ProgressCallback

use of com.biglybt.core.CoreOperationTask.ProgressCallback in project BiglyBT by BiglySoftware.

the class ColumnFO_Size method refresh.

@Override
public void refresh(TableCell cell) {
    CoreOperation op = (CoreOperation) cell.getDataSource();
    if (op == null) {
        return;
    }
    ProgressCallback prog = op.getTask().getProgressCallback();
    long size = prog == null ? -1 : prog.getSize();
    if (cell.setSortValue(size)) {
        cell.setText(size <= 0 ? "" : DisplayFormatters.formatByteCountToKiBEtc(size));
    }
}
Also used : CoreOperation(com.biglybt.core.CoreOperation) ProgressCallback(com.biglybt.core.CoreOperationTask.ProgressCallback)

Example 7 with ProgressCallback

use of com.biglybt.core.CoreOperationTask.ProgressCallback in project BiglyBT by BiglySoftware.

the class ProgressWindow method showDialog.

protected void showDialog(Shell _shell, CoreOperation _core_op) {
    shell = _shell;
    if (shell == null || shell.isDisposed()) {
        return;
    }
    boolean closeable = (shell.getStyle() & SWT.CLOSE) != 0;
    shell.setText(MessageText.getString("progress.window.title"));
    CoreOperationTask task = _core_op == null ? null : _core_op.getTask();
    ProgressCallback progress = task == null ? null : task.getProgressCallback();
    boolean alreadyPositioned = Utils.linkShellMetricsToConfig(shell, "com.biglybt.ui.swt.progress.ProgressWindow" + "." + _core_op.getOperationType());
    Utils.setShellIcon(shell);
    if (!closeable) {
        shell.addListener(SWT.Close, (ev) -> {
            ev.doit = false;
        });
    }
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    shell.setLayout(layout);
    Color bg = (Utils.isDarkAppearanceNative() || Utils.isDarkAppearancePartial()) ? null : Colors.white;
    shell.setBackground(bg);
    if (task != null) {
        String name = task.getName();
        if (name != null) {
            Label lName = new Label(shell, SWT.NONE);
            FontData fontData = lName.getFont().getFontData()[0];
            Font bold_font = new Font(shell.getDisplay(), new FontData(fontData.getName(), fontData.getHeight(), SWT.BOLD));
            lName.setText(name);
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.horizontalSpan = 2;
            lName.setLayoutData(gridData);
            lName.setBackground(bg);
            lName.setFont(bold_font);
            lName.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent arg0) {
                    bold_font.dispose();
                }
            });
            if (progress != null && (progress.getSupportedTaskStates() & ProgressCallback.ST_SUBTASKS) != 0) {
                subtask_label = new Label(shell, SWT.NONE);
                gridData = new GridData(GridData.FILL_HORIZONTAL);
                gridData.horizontalSpan = 2;
                gridData.horizontalIndent = 25;
                subtask_label.setLayoutData(gridData);
                subtask_label.setBackground(bg);
            }
        }
    }
    spinImages = ImageLoader.getInstance().getImages("working");
    if (spinImages == null || spinImages.length == 0) {
        new Label(shell, SWT.NULL);
    } else {
        final Rectangle spinBounds = spinImages[0].getBounds();
        final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED) {

            @Override
            public Point computeSize(int wHint, int hHint, boolean changed) {
                return (new Point(spinBounds.width, spinBounds.height));
            }
        };
        canvas.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(spinImages[curSpinIndex], 0, 0);
            }
        });
        Utils.execSWTThreadLater(100, new AERunnable() {

            @Override
            public void runSupport() {
                if (canvas == null || canvas.isDisposed()) {
                    return;
                }
                canvas.redraw();
                if (curSpinIndex == spinImages.length - 1) {
                    curSpinIndex = 0;
                } else {
                    curSpinIndex++;
                }
                if (progress != null && progress_bar != null) {
                    if (progress_bar.isDisposed()) {
                        return;
                    }
                    int p = progress.getProgress();
                    progress_bar.setSelection(p);
                    if (subtask_label != null) {
                        String st = progress.getSubTaskName();
                        if (st == null) {
                            st = "";
                        }
                        subtask_label.setText(st);
                    }
                }
                Utils.execSWTThreadLater(100, this);
            }
        });
        canvas.setBackground(bg);
    }
    Label label = new Label(shell, SWT.NONE);
    label.setText(MessageText.getString(resource));
    GridData gridData = new GridData();
    label.setLayoutData(gridData);
    label.setBackground(bg);
    if (progress != null) {
        Composite compProg = new Composite(shell, SWT.BORDER);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalSpan = 2;
        compProg.setLayoutData(gridData);
        GridLayout layoutProgress = new GridLayout();
        layoutProgress.numColumns = 1;
        layoutProgress.marginWidth = layoutProgress.marginHeight = 0;
        compProg.setLayout(layoutProgress);
        compProg.setBackground(bg);
        progress_bar = new ProgressBar(compProg, SWT.HORIZONTAL);
        progress_bar.setMinimum(0);
        progress_bar.setMaximum(1000);
        progress_bar.setBackground(bg);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.widthHint = 400;
        progress_bar.setLayoutData(gridData);
        int states = progress.getSupportedTaskStates();
        if ((states & ProgressCallback.ST_BUTTONS) != 0) {
            Label labelSeparator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            gridData.horizontalSpan = 2;
            labelSeparator.setLayoutData(gridData);
            // buttons
            boolean has_pause_resume = (states & (ProgressCallback.ST_PAUSE | ProgressCallback.ST_RESUME)) != 0;
            boolean has_cancel = (states & ProgressCallback.ST_CANCEL) != 0;
            if (!has_pause_resume) {
                has_cancel = true;
            }
            int num_buttons = 0;
            if (has_pause_resume)
                num_buttons += 2;
            if (has_cancel)
                num_buttons++;
            Composite comp = new Composite(shell, SWT.NULL);
            gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_FILL);
            gridData.grabExcessHorizontalSpace = true;
            gridData.horizontalSpan = 2;
            comp.setLayoutData(gridData);
            GridLayout layoutButtons = new GridLayout();
            layoutButtons.numColumns = num_buttons;
            comp.setLayout(layoutButtons);
            comp.setBackground(bg);
            List<Button> buttons = new ArrayList<>();
            Button bPause;
            Button bResume;
            Button bCancel;
            if (has_pause_resume) {
                bPause = new Button(comp, SWT.PUSH);
                bPause.setText(MessageText.getString("v3.MainWindow.button.pause"));
                gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_FILL);
                gridData.grabExcessHorizontalSpace = true;
                // gridData.widthHint = 70;
                bPause.setLayoutData(gridData);
                buttons.add(bPause);
                bResume = new Button(comp, SWT.PUSH);
                bResume.setText(MessageText.getString("v3.MainWindow.button.resume"));
                gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_FILL);
                gridData.grabExcessHorizontalSpace = false;
                // gridData.widthHint = 70;
                bResume.setLayoutData(gridData);
                buttons.add(bResume);
            } else {
                bPause = null;
                bResume = null;
            }
            if (has_cancel) {
                bCancel = new Button(comp, SWT.PUSH);
                bCancel.setText(MessageText.getString("UpdateWindow.cancel"));
                gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
                gridData.grabExcessHorizontalSpace = false;
                // gridData.widthHint = 70;
                bCancel.setLayoutData(gridData);
                buttons.add(bCancel);
            } else {
                bCancel = null;
            }
            Utils.makeButtonsEqualWidth(buttons);
            if (has_pause_resume) {
                // if we have resume then we have pause
                bResume.setEnabled(false);
                bPause.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event e) {
                        task_paused = true;
                        bPause.setEnabled(false);
                        bResume.setEnabled(true);
                        shell.setDefaultButton(bResume);
                        progress.setTaskState(ProgressCallback.ST_PAUSE);
                    }
                });
                bResume.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event e) {
                        task_paused = false;
                        bPause.setEnabled(true);
                        bResume.setEnabled(false);
                        shell.setDefaultButton(bPause);
                        progress.setTaskState(ProgressCallback.ST_RESUME);
                    }
                });
            }
            if (bCancel != null) {
                bCancel.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event e) {
                        if (has_pause_resume) {
                            bPause.setEnabled(false);
                            bResume.setEnabled(false);
                        }
                        bCancel.setEnabled(false);
                        progress.setTaskState(ProgressCallback.ST_CANCEL);
                    }
                });
            }
            Utils.execSWTThreadLater(250, new Runnable() {

                @Override
                public void run() {
                    if (comp.isDisposed()) {
                        return;
                    }
                    int state = progress.getTaskState();
                    if (state == ProgressCallback.ST_CANCEL) {
                        shell.dispose();
                    } else {
                        if (has_pause_resume) {
                            if (state == ProgressCallback.ST_PAUSE) {
                                bPause.setEnabled(false);
                                bResume.setEnabled(true);
                            } else {
                                bPause.setEnabled(true);
                                bResume.setEnabled(false);
                            }
                        }
                        Utils.execSWTThreadLater(250, this);
                    }
                }
            });
            shell.setDefaultButton(has_pause_resume ? bPause : bCancel);
            shell.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent arg0) {
                    if (task_paused) {
                        progress.setTaskState(ProgressCallback.ST_RESUME);
                    }
                }
            });
        }
    }
    shell.pack();
    if (!alreadyPositioned) {
        Composite parent = shell.getParent();
        if (parent != null) {
            Utils.centerWindowRelativeTo(shell, parent);
        } else {
            Utils.centreWindow(shell);
        }
    }
    shell.open();
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) DisposeListener(org.eclipse.swt.events.DisposeListener) PaintListener(org.eclipse.swt.events.PaintListener) DisposeListener(org.eclipse.swt.events.DisposeListener) UIExitUtilsSWT.canCloseListener(com.biglybt.ui.swt.UIExitUtilsSWT.canCloseListener) CoreOperationListener(com.biglybt.core.CoreOperationListener) ArrayList(java.util.ArrayList) CoreOperationTask(com.biglybt.core.CoreOperationTask) DisposeEvent(org.eclipse.swt.events.DisposeEvent) GridLayout(org.eclipse.swt.layout.GridLayout) PaintEvent(org.eclipse.swt.events.PaintEvent) PaintListener(org.eclipse.swt.events.PaintListener) ProgressCallback(com.biglybt.core.CoreOperationTask.ProgressCallback) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) DisposeEvent(org.eclipse.swt.events.DisposeEvent) DelayedEvent(com.biglybt.core.util.DelayedEvent) PaintEvent(org.eclipse.swt.events.PaintEvent)

Example 8 with ProgressCallback

use of com.biglybt.core.CoreOperationTask.ProgressCallback in project BiglyBT by BiglySoftware.

the class Initializer method stopIt.

@Override
public void stopIt(boolean isForRestart) throws CoreException {
    if (core != null) {
        if (isForRestart) {
            core.checkRestartSupported();
        }
    }
    try {
        if (core != null) {
            try {
                long lStopStarted = System.currentTimeMillis();
                System.out.println("core.stop");
                AESemaphore stop_sem = new AESemaphore("stop");
                ProgressCallback prog = new ProgressCallbackAdapter() {

                    @Override
                    public int getStyle() {
                        return (STYLE_NO_CLOSE | STYLE_MODAL);
                    }

                    @Override
                    public int getDelay() {
                        return (3 * 1000);
                    }

                    public int getSupportedTaskStates() {
                        return (ProgressCallback.ST_SUBTASKS);
                    }
                };
                boolean show_progress = COConfigurationManager.getBooleanParameter("Tidy Close With Progress");
                if (show_progress) {
                    if (Utils.isSWTThread()) {
                        Debug.out("Can't run closedown progress window as already on SWT thread");
                    } else {
                        Utils.execSWTThread(() -> {
                            FileUtil.runAsTask(CoreOperation.OP_PROGRESS, new CoreOperationTask() {

                                @Override
                                public String getName() {
                                    return (MessageText.getString(isForRestart ? "label.restarting.app" : "label.closing.app"));
                                }

                                @Override
                                public DownloadManager getDownload() {
                                    return (null);
                                }

                                @Override
                                public void run(CoreOperation operation) {
                                    try {
                                        stop_sem.reserve();
                                    } catch (Throwable e) {
                                        throw (new RuntimeException(e));
                                    }
                                }

                                @Override
                                public ProgressCallback getProgressCallback() {
                                    return (prog);
                                }
                            });
                        });
                    }
                }
                try {
                    if (isForRestart) {
                        core.restart(prog);
                    } else {
                        core.stop(prog);
                    }
                } finally {
                    stop_sem.release();
                }
                System.out.println("core.stop done in " + (System.currentTimeMillis() - lStopStarted));
            } catch (Throwable e) {
                // don't let any failure here cause the stop operation to fail
                Debug.out(e);
            }
        }
    } finally {
        // do this later than before so we get UI updates during closedown and can see progress of stats etc
        UIUpdaterSWT.destroyInstance();
        if (startServer != null) {
            startServer.stopIt();
        }
    }
    SWTThread instance = SWTThread.getInstance();
    if (instance != null) {
        instance.terminate();
    }
}
Also used : ProgressCallback(com.biglybt.core.CoreOperationTask.ProgressCallback) DownloadManager(com.biglybt.core.download.DownloadManager) ProgressCallbackAdapter(com.biglybt.core.CoreOperationTask.ProgressCallbackAdapter)

Example 9 with ProgressCallback

use of com.biglybt.core.CoreOperationTask.ProgressCallback in project BiglyBT by BiglySoftware.

the class SBC_DiskOpsView method toolBarItemActivated.

@Override
public boolean toolBarItemActivated(ToolBarItem item, long activationType, Object datasource) {
    Object[] selectedDS;
    synchronized (this) {
        if (tvDiskOps == null) {
            return (false);
        }
        selectedDS = tvDiskOps.getSelectedDataSources().toArray();
    }
    if (selectedDS.length == 0) {
        return (false);
    }
    String itemKey = item.getID();
    boolean is_start_stop = itemKey.equals("startstop");
    boolean is_start = itemKey.equals("start");
    boolean is_stop = itemKey.equals("stop");
    boolean is_remove = itemKey.equals("remove");
    boolean did_something = false;
    for (Object ds : selectedDS) {
        CoreOperation op = (CoreOperation) ds;
        ProgressCallback prog = op.getTask().getProgressCallback();
        int states = prog.getSupportedTaskStates();
        int state = prog.getTaskState();
        boolean ds_did_something = false;
        if ((states & ProgressCallback.ST_PAUSE) != 0) {
            if (is_stop || is_start_stop) {
                if (state == ProgressCallback.ST_NONE || state == ProgressCallback.ST_QUEUED) {
                    prog.setTaskState(ProgressCallback.ST_PAUSE);
                    ds_did_something = true;
                } else if (prog.isAutoPause()) {
                    prog.setAutoPause(false);
                    ds_did_something = true;
                }
            }
        }
        if (!ds_did_something && (states & ProgressCallback.ST_RESUME) != 0) {
            if (is_start || is_start_stop) {
                if (state == ProgressCallback.ST_PAUSE) {
                    prog.setTaskState(ProgressCallback.ST_RESUME);
                    ds_did_something = true;
                }
            }
        }
        if (!ds_did_something && (states & ProgressCallback.ST_CANCEL) != 0) {
            if (is_remove) {
                prog.setTaskState(ProgressCallback.ST_CANCEL);
                ds_did_something = true;
            }
        }
        if (ds_did_something) {
            did_something = true;
        }
    }
    if (did_something) {
        refreshToolbar();
    }
    return (did_something);
}
Also used : CoreOperation(com.biglybt.core.CoreOperation) ProgressCallback(com.biglybt.core.CoreOperationTask.ProgressCallback) SWTSkinObject(com.biglybt.ui.swt.skin.SWTSkinObject)

Example 10 with ProgressCallback

use of com.biglybt.core.CoreOperationTask.ProgressCallback in project BiglyBT by BiglySoftware.

the class SBC_DiskOpsView method initTable.

private void initTable(Composite control) {
    tvDiskOps = TableViewFactory.createTableViewSWT(CoreOperation.class, TABLE_DISK_OPS, TABLE_DISK_OPS, new TableColumnCore[0], ColumnFO_Type.COLUMN_ID, SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
    // tvDiskOps.setRowDefaultHeightEM(1.5f);
    tvDiskOps.setHeaderVisible(true);
    tableParent = new Composite(control, SWT.BORDER);
    tableParent.setLayoutData(Utils.getFilledFormData());
    GridLayout layout = new GridLayout();
    layout.marginHeight = layout.marginWidth = layout.verticalSpacing = layout.horizontalSpacing = 0;
    tableParent.setLayout(layout);
    tvDiskOps.addSelectionListener(new TableSelectionListener() {

        @Override
        public void selected(TableRowCore[] row) {
            updateSelectedContent();
        }

        @Override
        public void mouseExit(TableRowCore row) {
        }

        @Override
        public void mouseEnter(TableRowCore row) {
        }

        @Override
        public void focusChanged(TableRowCore focus) {
        }

        @Override
        public void deselected(TableRowCore[] rows) {
            updateSelectedContent();
        }

        @Override
        public void defaultSelected(TableRowCore[] rows, int stateMask) {
        }
    }, false);
    tvDiskOps.addLifeCycleListener(new TableLifeCycleListener() {

        @Override
        public void tableLifeCycleEventOccurred(TableView tv, int eventType, Map<String, Object> data) {
            switch(eventType) {
                case EVENT_TABLELIFECYCLE_INITIALIZED:
                    for (CoreOperation op : core.getOperations()) {
                        if (ourOperation(op)) {
                            tvDiskOps.addDataSource(op);
                        }
                    }
                    updateSelectedContent();
                    break;
            }
        }
    });
    tvDiskOps.addMenuFillListener(new TableViewSWTMenuFillListener() {

        @Override
        public void fillMenu(String sColumnName, Menu menu) {
            SBC_DiskOpsView.this.fillMenu(menu);
        }

        @Override
        public void addThisColumnSubMenu(String columnName, Menu menuThisColumn) {
        }
    });
    tvDiskOps.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.stateMask == 0 && e.keyCode == SWT.DEL) {
                Object[] selectedDS;
                synchronized (this) {
                    if (tvDiskOps == null) {
                        selectedDS = new Object[0];
                    }
                    selectedDS = tvDiskOps.getSelectedDataSources().toArray();
                }
                boolean did_something = false;
                for (Object ds : selectedDS) {
                    CoreOperation op = (CoreOperation) ds;
                    ProgressCallback prog = op.getTask().getProgressCallback();
                    int states = prog.getSupportedTaskStates();
                    if ((states & ProgressCallback.ST_CANCEL) != 0) {
                        prog.setTaskState(ProgressCallback.ST_CANCEL);
                        did_something = true;
                    }
                }
                if (did_something) {
                    refreshToolbar();
                }
                e.doit = false;
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        }
    });
    tvDiskOps.initialize(tableParent);
    control.layout(true, true);
}
Also used : TableViewSWTMenuFillListener(com.biglybt.ui.swt.views.table.TableViewSWTMenuFillListener) CoreOperation(com.biglybt.core.CoreOperation) ProgressCallback(com.biglybt.core.CoreOperationTask.ProgressCallback) GridLayout(org.eclipse.swt.layout.GridLayout) SWTSkinObject(com.biglybt.ui.swt.skin.SWTSkinObject)

Aggregations

ProgressCallback (com.biglybt.core.CoreOperationTask.ProgressCallback)15 CoreOperation (com.biglybt.core.CoreOperation)13 SWTSkinObject (com.biglybt.ui.swt.skin.SWTSkinObject)4 CoreOperationTask (com.biglybt.core.CoreOperationTask)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 CoreOperationListener (com.biglybt.core.CoreOperationListener)1 ProgressCallbackAdapter (com.biglybt.core.CoreOperationTask.ProgressCallbackAdapter)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 AERunnable (com.biglybt.core.util.AERunnable)1 DelayedEvent (com.biglybt.core.util.DelayedEvent)1 UIExitUtilsSWT.canCloseListener (com.biglybt.ui.swt.UIExitUtilsSWT.canCloseListener)1 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)1 TableViewSWTMenuFillListener (com.biglybt.ui.swt.views.table.TableViewSWTMenuFillListener)1 ArrayList (java.util.ArrayList)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 PaintEvent (org.eclipse.swt.events.PaintEvent)1 PaintListener (org.eclipse.swt.events.PaintListener)1 GridData (org.eclipse.swt.layout.GridData)1