Search in sources :

Example 1 with TagCanvas

use of com.biglybt.ui.swt.widgets.TagCanvas in project BiglyBT by BiglySoftware.

the class TagButtonsUI method setEnableWhenNoTaggables.

public void setEnableWhenNoTaggables(boolean enableWhenNoTaggables) {
    this.enableWhenNoTaggables = enableWhenNoTaggables;
    Utils.execSWTThread(() -> {
        for (TagCanvas widget : tagWidgets) {
            widget.getTagPainter().setEnableWhenNoTaggables(enableWhenNoTaggables);
        }
    });
}
Also used : TagCanvas(com.biglybt.ui.swt.widgets.TagCanvas)

Example 2 with TagCanvas

use of com.biglybt.ui.swt.widgets.TagCanvas in project BiglyBT by BiglySoftware.

the class MyTorrentsView method buildCatAndTag.

/**
 * @since 3.1.1.1
 */
private void buildCatAndTag(List<Tag> tags) {
    if (tags.size() == 0 || cCategoriesAndTags == null || cCategoriesAndTags.isDisposed()) {
        return;
    }
    Label spacer = null;
    allTags = tags;
    if (buttonListener == null) {
        buttonListener = new TagButtonTrigger() {

            @Override
            public void tagButtonTriggered(TagPainter painter, int stateMask, boolean longPress) {
                Tag tag = painter.getTag();
                if (longPress) {
                    handleLongPress(painter, tag);
                    return;
                }
                boolean add = (stateMask & SWT.MOD1) != 0;
                boolean isSelected = painter.isSelected();
                if (isSelected) {
                    removeTagFromCurrent(tag);
                    painter.setSelected(!isSelected);
                } else {
                    if (add) {
                        Category catAll = CategoryManager.getCategory(Category.TYPE_ALL);
                        if (tag.equals(catAll)) {
                            setCurrentTags(catAll);
                        } else {
                            synchronized (currentTagsLock) {
                                Tag[] newTags = new Tag[_currentTags.length + 1];
                                System.arraycopy(_currentTags, 0, newTags, 0, _currentTags.length);
                                newTags[_currentTags.length] = tag;
                                newTags = (Tag[]) removeFromArray(newTags, catAll);
                                setCurrentTags(newTags);
                            }
                        }
                        painter.setSelected(!isSelected);
                    } else {
                        setCurrentTags(tag);
                        setSelection(painter.getControl().getParent());
                    }
                }
            }

            private void setSelection(Composite parent) {
                if (parent == null) {
                    return;
                }
                List<Tag> selectedTags = new ArrayList<>();
                Tag[] currentTags = getCurrentTags();
                if (currentTags != null) {
                    selectedTags.addAll(Arrays.asList(currentTags));
                }
                Control[] controls = parent.getChildren();
                for (Control control : controls) {
                    if (!(control instanceof TagCanvas)) {
                        continue;
                    }
                    TagPainter painter = ((TagCanvas) control).getTagPainter();
                    boolean selected = selectedTags.remove(painter.getTag());
                    painter.setSelected(selected);
                }
            }

            private void handleLongPress(TagPainter painter, Tag tag) {
                if (tv == null) {
                    return;
                }
                Object[] ds = tv.getSelectedDataSources().toArray();
                if (tag instanceof Category) {
                    TorrentUtil.assignToCategory(ds, (Category) tag);
                    return;
                }
                boolean doAdd = false;
                for (Object obj : ds) {
                    if (obj instanceof DownloadManager) {
                        DownloadManager dm = (DownloadManager) obj;
                        if (!tag.hasTaggable(dm)) {
                            doAdd = true;
                            break;
                        }
                    }
                }
                try {
                    tag.addTaggableBatch(true);
                    for (Object obj : ds) {
                        if (obj instanceof DownloadManager) {
                            DownloadManager dm = (DownloadManager) obj;
                            if (doAdd) {
                                tag.addTaggable(dm);
                            } else {
                                tag.removeTaggable(dm);
                            }
                        }
                    }
                } finally {
                    tag.addTaggableBatch(false);
                }
                // Quick Visual
                boolean wasSelected = painter.isSelected();
                painter.setGrayed(true);
                painter.setSelected(true);
                Utils.execSWTThreadLater(200, () -> {
                    painter.setGrayed(false);
                    painter.setSelected(wasSelected);
                });
            }

            @Override
            public Boolean tagSelectedOverride(Tag tag) {
                return null;
            }
        };
    }
    TagGroup currentGroup = null;
    for (final Tag tag : tags) {
        boolean isCat = (tag instanceof Category);
        TagGroup tg = tag.getGroupContainer();
        if (tg != currentGroup && currentGroup != null && !currentGroup.getTags().isEmpty()) {
            Divider div = new Divider(cCategoriesAndTags, SWT.NULL);
            RowData rd = new RowData();
            div.setLayoutData(rd);
        }
        currentGroup = tg;
        TagCanvas button = new TagCanvas(cCategoriesAndTags, tag, false, true);
        TagPainter painter = button.getTagPainter();
        button.setTrigger(buttonListener);
        painter.setCompact(true, COConfigurationManager.getBooleanParameter("Library.ShowTagButtons.ImageOverride"));
        if (isCat) {
            if (spacer == null) {
                spacer = new Label(cCategoriesAndTags, SWT.NONE);
                RowData rd = new RowData();
                rd.width = 8;
                spacer.setLayoutData(rd);
                spacer.moveAbove(null);
            }
            button.moveAbove(spacer);
        }
        button.addKeyListener(this);
        if (fontButton == null) {
            fontButton = FontUtils.cache(FontUtils.getFontWithStyle(button.getFont(), SWT.NONE, 0.8f));
        }
        button.setFont(fontButton);
        if (isCurrent(tag)) {
            painter.setSelected(true);
        }
        Menu menu = new Menu(button);
        button.setMenu(menu);
        if (isCat) {
            CategoryUIUtils.setupCategoryMenu(menu, (Category) tag);
        } else {
            TagUIUtils.createSideBarMenuItemsDelayed(menu, tag);
        }
    }
    cCategoriesAndTags.getParent().layout(true, true);
}
Also used : Category(com.biglybt.core.category.Category) TagPainter(com.biglybt.ui.swt.widgets.TagPainter) DownloadManager(com.biglybt.core.download.DownloadManager) TagCanvas(com.biglybt.ui.swt.widgets.TagCanvas) TagButtonTrigger(com.biglybt.ui.swt.widgets.TagCanvas.TagButtonTrigger) List(java.util.List)

Example 3 with TagCanvas

use of com.biglybt.ui.swt.widgets.TagCanvas in project BiglyBT by BiglySoftware.

the class TagButtonsUI method updateTag.

public int updateTag(Tag tag, List<Taggable> taggables) {
    if (cMainComposite == null || cMainComposite.isDisposed()) {
        return UPDATETAG_NOCHANGE;
    }
    for (TagCanvas tagWidget : tagWidgets) {
        TagPainter painter = tagWidget.getTagPainter();
        if (tag.equals(painter.getTag())) {
            Composite parent = tagWidget.getParent();
            String oldGroup = (parent instanceof Group) ? ((Group) parent).getText() : "";
            String newGroup = painter.getTag().getGroup();
            if (newGroup == null) {
                newGroup = "";
            }
            if (!oldGroup.equals(newGroup)) {
                return UPDATETAG_REQUIRES_REBUILD;
            }
            painter.updateState(taggables);
            return UPDATETAG_SUCCESS;
        }
    }
    return UPDATETAG_SUCCESS;
}
Also used : ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) TagCanvas(com.biglybt.ui.swt.widgets.TagCanvas) TagPainter(com.biglybt.ui.swt.widgets.TagPainter)

Example 4 with TagCanvas

use of com.biglybt.ui.swt.widgets.TagCanvas in project BiglyBT by BiglySoftware.

the class TagButtonsUI method buildTagGroup.

public Composite buildTagGroup(List<Tag> tags, Composite parent, boolean allowContextMenu, TagButtonTrigger trigger) {
    cMainComposite = new Composite(parent, SWT.NONE);
    RowLayout mainLayout = Utils.getSimpleRowLayout(false);
    mainLayout.type = SWT.VERTICAL;
    cMainComposite.setLayout(mainLayout);
    tagWidgets.clear();
    Listener menuDetectListener = allowContextMenu ? event -> {
        final TagCanvas tagCanvas = (TagCanvas) event.widget;
        Menu menu = new Menu(tagCanvas);
        tagCanvas.setMenu(menu);
        MenuBuildUtils.addMaintenanceListenerForMenu(menu, (menu1, menuEvent) -> TagUIUtils.createSideBarMenuItems(menu1, tagCanvas.getTagPainter().getTag()));
    } : null;
    tags = TagUtils.sortTags(tags);
    Composite g = null;
    String group = null;
    for (Tag tag : tags) {
        String newGroup = tag.getGroup();
        if (g == null || (group != null && !group.equals(newGroup)) || (group == null && newGroup != null)) {
            group = newGroup;
            g = group == null ? new Composite(cMainComposite, SWT.DOUBLE_BUFFERED) : new Group(cMainComposite, SWT.DOUBLE_BUFFERED);
            if (group != null) {
                Group groupControl = (Group) g;
                groupControl.setText(group);
                Menu menu = new Menu(g);
                g.setMenu(menu);
                MenuBuildUtils.addMaintenanceListenerForMenu(menu, (root_menu, menuEvent) -> {
                    TagUIUtils.createSideBarMenuItems(root_menu, tag.getGroupContainer());
                });
                // Limit menu to top area (label) of group, otherwise the menu
                // shows when right clicking next to a tag, which is confusing
                g.addMenuDetectListener(e -> {
                    Group thisGroup = (Group) e.widget;
                    Point point = thisGroup.toControl(e.x, e.y);
                    Rectangle clientArea = thisGroup.getClientArea();
                    if (point.y > clientArea.y) {
                        e.doit = false;
                    }
                });
                DropTarget dropTarget = new DropTarget(groupControl, DND.DROP_MOVE);
                dropTarget.setTransfer(TextTransfer.getInstance());
                dropTarget.addDropListener(new DropTargetAdapter() {

                    @Override
                    public void dragEnter(DropTargetEvent event) {
                        Object data = event.data == null ? DragDropUtils.getLastDraggedObject() : event.data;
                        List<Tag> droppedTags = DragDropUtils.getTagsFromDroppedData(data);
                        if (droppedTags.isEmpty()) {
                            event.detail = DND.DROP_NONE;
                        }
                    }

                    @Override
                    public void drop(DropTargetEvent event) {
                        Object data = event.data == null ? DragDropUtils.getLastDraggedObject() : event.data;
                        List<Tag> droppedTags = DragDropUtils.getTagsFromDroppedData(data);
                        if (droppedTags.isEmpty()) {
                            event.detail = DND.DROP_NONE;
                            return;
                        }
                        for (Tag droppedTag : droppedTags) {
                            droppedTag.setGroup(groupControl.getText());
                        }
                    }
                });
            }
            RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
            rowLayout.pack = true;
            rowLayout.spacing = 5;
            g.setLayout(rowLayout);
        }
        TagCanvas p = new TagCanvas(g, tag, disableAuto, enableWhenNoTaggables);
        p.setTrigger(trigger);
        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 3;
        if (Constants.isWindows) {
            layout.marginWidth = 0;
            layout.marginLeft = 4;
            layout.marginRight = 7;
            layout.marginTop = 1;
        } else {
            layout.marginWidth = 0;
            layout.marginLeft = 3;
            layout.marginRight = 11;
        }
        p.setLayout(layout);
        if (allowContextMenu) {
            p.addListener(SWT.MenuDetect, menuDetectListener);
        }
        p.addDisposeListener(e -> tagWidgets.remove((TagCanvas) e.widget));
        tagWidgets.add(p);
    }
    setLayoutStyle(layoutStyle);
    return cMainComposite;
}
Also used : ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) TagCanvas(com.biglybt.ui.swt.widgets.TagCanvas) List(java.util.List) Tag(com.biglybt.core.tag.Tag)

Example 5 with TagCanvas

use of com.biglybt.ui.swt.widgets.TagCanvas in project BiglyBT by BiglySoftware.

the class TagButtonsUI method setSelectedTags.

public void setSelectedTags(List<Tag> tags) {
    Set<Tag> tag_set = new HashSet<>(tags);
    for (TagCanvas widget : tagWidgets) {
        TagPainter painter = widget.getTagPainter();
        Tag tag = painter.getTag();
        if (tag == null) {
            continue;
        }
        boolean select = tag_set.contains(tag);
        painter.setSelected(select);
    }
}
Also used : TagCanvas(com.biglybt.ui.swt.widgets.TagCanvas) TagPainter(com.biglybt.ui.swt.widgets.TagPainter) Tag(com.biglybt.core.tag.Tag)

Aggregations

TagCanvas (com.biglybt.ui.swt.widgets.TagCanvas)6 TagPainter (com.biglybt.ui.swt.widgets.TagPainter)3 Tag (com.biglybt.core.tag.Tag)2 List (java.util.List)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 Category (com.biglybt.core.category.Category)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 TagButtonTrigger (com.biglybt.ui.swt.widgets.TagCanvas.TagButtonTrigger)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1