Skip to content

Instantly share code, notes, and snippets.

@ThomasRohde
Created October 6, 2022 08:21
Show Gist options
  • Save ThomasRohde/a4f4353ffee2e05b63d67cb033e5e52c to your computer and use it in GitHub Desktop.
Save ThomasRohde/a4f4353ffee2e05b63d67cb033e5e52c to your computer and use it in GitHub Desktop.
console.show();
console.clear();
let source = [];
$(selection).filter("concept").each(c => {
let element = {
obj: c.concept,
id: c.concept.id,
};
source.push(element);
})
let matches = [];
source.forEach(s => {
let name = "";
if (s.obj.type.includes("relation")) {
let src = s.obj.source;
let tgt = s.obj.target;
name = `${s.obj.type} (${src.name} - ${tgt.name})`
}
else name = s.obj.name;
element = s.obj;
$("view").find("concept").each(e => {
if (e.concept.id == s.id) {
let target = {
source: s.obj,
sourceName: name,
target: e.view,
id: e.view.id,
name: e.view.name,
type: e.view.type
}
matches.push(target);
}
});
$("relationship").each(r => {
let src = r.source;
let tgt = r.target;
if (src.id == s.id || tgt.id == s.id) {
let relationTarget = {
source: s.obj,
sourceName: name,
target: r,
id: r.id,
name: `${r.type} (${src.name} - ${tgt.name})`,
type: r.type
};
matches.push(relationTarget);
}
});
});
let table = {
columns: ["Element", "is referenced here", "Type"],
selectors: ["sourceName","name", "type"],
key: "target",
rows: matches
}
// Dialog code inspired by https://gist.github.com/rchevallier/d155527dcc3e956848a90e51bf21d7e1
const SWT = Java.type('org.eclipse.swt.SWT');
const GridDataFactory = Java.type('org.eclipse.jface.layout.GridDataFactory');
const GridLayoutFactory = Java.type('org.eclipse.jface.layout.GridLayoutFactory');
const GridData = Java.type('org.eclipse.swt.layout.GridData');
const CompositeWidget = Java.type('org.eclipse.swt.widgets.Composite');
const TableWidget = Java.type('org.eclipse.swt.widgets.Table');
const TableItemWidget = Java.type('org.eclipse.swt.widgets.TableItem');
const TableColumnWidget = Java.type('org.eclipse.swt.widgets.TableColumn');
const LabelWidget = Java.type('org.eclipse.swt.widgets.Label');
const IMessageProvider = Java.type('org.eclipse.jface.dialogs.IMessageProvider');
const SelectionAdapter = Java.type('org.eclipse.swt.events.SelectionAdapter')
const Dialog = Java.extend(Java.type('org.eclipse.jface.dialogs.Dialog'));
let worksetDialog = {
table: {},
selected: {},
widgets: {},
open: function (t) {
worksetDialog.table = t;
return (this.dialog.open() == 0); // OK = 0, Cancel = 1, Closed = -1
},
dialog: new Dialog(shell,
{
tableWidget: null,
widths: [],
configureShell: function (newShell) {
Java.super(worksetDialog.dialog).configureShell(newShell);
newShell.setText("Search result");
},
widgetSelected: function (e) {
worksetDialog.selected = worksetDialog.widgets.table.getSelection()[0].getData();
},
isResizable: function () {
return true;
},
createDialogArea: function (parent) {
let area = Java.super(worksetDialog.dialog).createDialogArea(parent);
let container = new CompositeWidget(area, SWT.NONE);
GridDataFactory.swtDefaults().align(SWT.FILL, SWT.BEGINNING).applyTo(container);
GridLayoutFactory.swtDefaults().numColumns(1).margins(10, 10).spacing(10, 5).applyTo(container);
this.tableWidget = new TableWidget(container, SWT.VIRTUAL | SWT.BORDER);
this.tableWidget.setLinesVisible(true);
this.tableWidget.setHeaderVisible(true);
const data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 400;
this.tableWidget.setLayoutData(data);
worksetDialog.widgets["table"] = this.tableWidget;
for (let index = 0; index < table.columns.length; index++) {
const column = table.columns[index];
const columnWidget = new TableColumnWidget(this.tableWidget, SWT.NONE);
columnWidget.setText(column);
this.widths[index] = Java.super(worksetDialog.dialog).convertWidthInCharsToPixels(column.length);
}
worksetDialog.table.rows.forEach(row => {
let item = new TableItemWidget(this.tableWidget, SWT.NONE);
let c = 0;
worksetDialog.table.selectors.forEach(column => {
let w = Java.super(worksetDialog.dialog).convertWidthInCharsToPixels(row[column].length);
if (w > this.widths[c]) this.widths[c] = w;
item.setText(c++, row[column]);
item.setData(row[worksetDialog.table.key])
})
});
for (let index = 0; index < table.columns.length; index++) {
this.tableWidget.getColumn(index).setWidth(this.widths[index]);
}
this.tableWidget.addSelectionListener(this);
// Filler widget that is needed for some reason to not clip the bottom of the dialog
let labeWidget = new LabelWidget(container, SWT.NONE);
return area;
},
okPressed: function () {
Java.super(worksetDialog.dialog).okPressed();
}
})
}
if (table.rows.length > 0 && worksetDialog.open(table)) {
console.log("> Where used script executed.");
console.log(`> Selection: ${worksetDialog.selected}`)
}
else if (table.rows.length == 0) console.log("> Nothing found!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment