use of com.biglybt.ui.swt.widgets.TagCanvas.TagButtonTrigger 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);
}
use of com.biglybt.ui.swt.widgets.TagCanvas.TagButtonTrigger in project BiglyBT by BiglySoftware.
the class TaggingView method rebuildComposite.
private void rebuildComposite() {
if (mainComposite == null || mainComposite.isDisposed()) {
if (parent == null || parent.isDisposed()) {
return;
}
mainComposite = new Composite(parent, SWT.NONE);
mainComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
GridLayout layout = new GridLayout();
layout.marginHeight = layout.marginWidth = layout.verticalSpacing = 0;
mainComposite.setLayout(layout);
Layout parentLayout = parent.getLayout();
if (parentLayout instanceof GridLayout) {
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
mainComposite.setLayoutData(gd);
} else if (parentLayout instanceof FormLayout) {
mainComposite.setLayoutData(Utils.getFilledFormData());
}
} else {
Utils.disposeComposite(mainComposite, false);
}
tagButtonsUI = new TagButtonsUI();
List<Tag> listAllTags = getTags();
boolean hasGroup = false;
for (Tag tag : listAllTags) {
String group = tag.getGroup();
if (group != null && group.length() > 0) {
hasGroup = true;
break;
}
}
// / Buttons
Composite buttonComp = new Composite(mainComposite, SWT.NONE);
GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
buttonComp.setLayoutData(layoutData);
GridLayout bcLayout = new GridLayout(hasGroup ? 7 : 6, false);
bcLayout.marginHeight = 0;
buttonComp.setLayout(bcLayout);
GridData gridData;
Button buttonAdd = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonAdd.setLayoutData(gridData);
Messages.setLanguageText(buttonAdd, "label.add.tag");
buttonAdd.addListener(SWT.Selection, event -> askForNewTag());
buttonCopy = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonCopy.setLayoutData(gridData);
Messages.setLanguageText(buttonCopy, "label.copy");
Messages.setLanguageTooltip(buttonCopy, "tags.copy.tooltip");
buttonCopy.addListener(SWT.Selection, event -> {
if (taggables == null) {
return;
}
TagManager tm = TagManagerFactory.getTagManager();
copied_tag_assignment.clear();
for (Taggable taggable : taggables) {
List<Tag> has_tags = tm.getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, taggable);
for (Tag tag : has_tags) {
boolean[] auto = tag.isTagAuto();
if (!auto[0] && !auto[1]) {
copied_tag_assignment.add(tag);
}
}
}
for (Consumer<String> c : cta_listeners) {
c.accept("");
}
});
buttonPaste = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonPaste.setLayoutData(gridData);
Messages.setLanguageText(buttonPaste, "label.paste");
Messages.setLanguageTooltip(buttonPaste, "tags.paste.tooltip");
buttonPaste.addListener(SWT.Selection, event -> {
if (taggables == null) {
return;
}
TagManager tm = TagManagerFactory.getTagManager();
for (Taggable taggable : taggables) {
for (Tag t : tm.getTagType(TagType.TT_DOWNLOAD_MANUAL).getTags()) {
boolean[] auto = t.isTagAuto();
if (!auto[0] && !auto[1]) {
if (!copied_tag_assignment.contains(t)) {
if (t.hasTaggable(taggable)) {
t.removeTaggable(taggable);
}
}
}
}
for (Tag t : copied_tag_assignment) {
if (!t.hasTaggable(taggable)) {
t.addTaggable(taggable);
}
}
}
});
cta_listeners.add(new Consumer<String>() {
@Override
public void accept(String t) {
if (buttonPaste.isDisposed()) {
cta_listeners.remove(this);
} else {
buttonPaste.setEnabled(!copied_tag_assignment.isEmpty());
}
}
});
buttonPaste.setEnabled(!copied_tag_assignment.isEmpty());
buttonClear = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonClear.setLayoutData(gridData);
Messages.setLanguageText(buttonClear, "Button.clear");
Messages.setLanguageTooltip(buttonClear, "tags.clear.tooltip");
buttonClear.addListener(SWT.Selection, event -> {
if (taggables == null) {
return;
}
TagManager tm = TagManagerFactory.getTagManager();
for (Taggable taggable : taggables) {
List<Tag> has_tags = tm.getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, taggable);
for (Tag tag : has_tags) {
boolean[] auto = tag.isTagAuto();
if (!auto[0] && !auto[1]) {
tag.removeTaggable(taggable);
}
}
}
});
buttonInvert = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonInvert.setLayoutData(gridData);
Messages.setLanguageText(buttonInvert, "label.invert");
Messages.setLanguageTooltip(buttonInvert, "tags.invert.tooltip");
buttonInvert.addListener(SWT.Selection, event -> {
if (taggables == null) {
return;
}
TagManager tm = TagManagerFactory.getTagManager();
List<Tag> all_tags = new ArrayList<>();
for (Tag t : tm.getTagType(TagType.TT_DOWNLOAD_MANUAL).getTags()) {
boolean[] auto = t.isTagAuto();
if (!auto[0] && !auto[1]) {
all_tags.add(t);
}
}
for (Taggable taggable : taggables) {
List<Tag> has_tags = tm.getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, taggable);
Set<Tag> tags_to_add = new HashSet<>(all_tags);
for (Tag tag : has_tags) {
boolean[] auto = tag.isTagAuto();
if (!auto[0] && !auto[1]) {
tag.removeTaggable(taggable);
tags_to_add.remove(tag);
}
}
for (Tag tag : tags_to_add) {
tag.addTaggable(taggable);
}
}
});
buttonExplain = new Button(buttonComp, SWT.PUSH);
gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
buttonExplain.setLayoutData(gridData);
Messages.setLanguageText(buttonExplain, "button.explain");
buttonExplain.addListener(SWT.Selection, event -> explain());
Utils.makeButtonsEqualWidth(buttonCopy, buttonPaste, buttonClear, buttonInvert);
Utils.makeButtonsEqualWidth(buttonAdd, buttonInvert, buttonExplain);
if (hasGroup) {
int layoutStyle = tagButtonsUI.getLayoutStyle();
ToolBar toolBar = new ToolBar(buttonComp, SWT.NONE);
gridData = new GridData(SWT.RIGHT, SWT.CENTER, true, false);
toolBar.setLayoutData(gridData);
ToolItem buttonRowMode = new ToolItem(toolBar, SWT.RADIO);
if (layoutStyle == SWT.HORIZONTAL) {
buttonRowMode.setSelection(true);
}
ToolItem buttonColumnMode = new ToolItem(toolBar, SWT.RADIO);
if (layoutStyle == SWT.VERTICAL) {
buttonColumnMode.setSelection(true);
}
ToolItem buttonRowCompactMode = new ToolItem(toolBar, SWT.RADIO);
if (layoutStyle == (SWT.HORIZONTAL | SWT.FILL)) {
buttonRowCompactMode.setSelection(true);
}
ImageLoader.getInstance().setToolItemImage(buttonRowMode, "row_mode");
buttonRowMode.addListener(SWT.Selection, event -> tagButtonsUI.setLayoutStyle(SWT.HORIZONTAL));
ImageLoader.getInstance().setToolItemImage(buttonColumnMode, "column_mode");
buttonColumnMode.addListener(SWT.Selection, event -> tagButtonsUI.setLayoutStyle(SWT.VERTICAL));
ImageLoader.getInstance().setToolItemImage(buttonRowCompactMode, "row_compact_mode");
buttonRowCompactMode.addListener(SWT.Selection, event -> tagButtonsUI.setLayoutStyle(SWT.HORIZONTAL | SWT.FILL));
}
// /
sc = new ScrolledComposite(mainComposite, SWT.V_SCROLL);
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
sc.setLayoutData(gd);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.getVerticalBar().setIncrement(16);
Composite cTagComposite = new Composite(sc, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = layout.marginWidth = 0;
cTagComposite.setLayout(layout);
sc.setContent(cTagComposite);
Composite tagArea = tagButtonsUI.buildTagGroup(listAllTags, cTagComposite, true, new TagButtonTrigger() {
@Override
public void tagButtonTriggered(TagPainter painter, int stateMask, boolean longPress) {
if (taggables == null || longPress) {
return;
}
Tag tag = painter.getTag();
boolean doTag = !(painter.isSelected() && !painter.isGrayed());
try {
tag.addTaggableBatch(true);
for (Taggable taggable : taggables) {
if (doTag) {
tag.addTaggable(taggable);
} else {
tag.removeTaggable(taggable);
}
painter.setSelected(doTag);
}
} finally {
tag.addTaggableBatch(false);
}
}
@Override
public Boolean tagSelectedOverride(Tag tag) {
return null;
}
});
tagArea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
sc.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
Rectangle r = sc.getClientArea();
Point size = cTagComposite.computeSize(r.width, SWT.DEFAULT);
sc.setMinSize(size);
}
});
swt_updateFields();
Rectangle r = sc.getClientArea();
Point size = cTagComposite.computeSize(r.width, SWT.DEFAULT);
sc.setMinSize(size);
}
Aggregations