下面列出了org.eclipse.swt.widgets.Table#getItem ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void selectProposal(ICompletionProposal proposal) {
try{
Object fProposalPopup = ReflectionUtils.getField(this.getClass(), "fProposalPopup", this, true);
Object fProposalTable = ReflectionUtils.getField(fProposalPopup.getClass(), "fProposalTable", fProposalPopup, true);
if (fProposalTable instanceof Table) {
Table table = (Table) fProposalTable;
int i = 0;
for (;i < table.getItemCount(); i++) {
TableItem item = table.getItem(i);
if (Objects.equals(item.getData(), proposal)) {
break;
}
}
if (i < table.getItemCount()) {
Method selectMethod = ReflectionUtils.findMethod(fProposalPopup.getClass(), "selectProposal", int.class, boolean.class);
ReflectionUtils.invokeMethod(selectMethod, fProposalPopup, i, false);
}
}
}
catch(AssertionError e) {
}
}
/**
* Export content of a table to TSV file
* @param table
* the table to export
* @param stream
* the output stream
*/
public static void exportTableToTsv(Table table, @Nullable OutputStream stream) {
if (table == null || stream == null) {
return;
}
try (PrintWriter pw = new PrintWriter(stream)) {
int size = table.getItemCount();
List<String> columns = new ArrayList<>();
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
if (column == null) {
return;
}
String columnName = String.valueOf(column.getText());
if (columnName.isEmpty() && i == table.getColumnCount() - 1) {
// Linux GTK2 undocumented feature
break;
}
columns.add(columnName);
}
pw.println(Joiner.on('\t').join(columns));
for (int i = 0; i < size; i++) {
TableItem item = table.getItem(i);
if (item == null) {
continue;
}
List<String> data = new ArrayList<>();
for (int col = 0; col < columns.size(); col++) {
data.add(String.valueOf(item.getText(col)));
}
pw.println(Joiner.on('\t').join(data));
}
}
}
public void updateTable() {
Table table = propertiesViewer.getTable();
table.setItemCount(properties.size());
int i = 0;
for (String key : properties.keySet()) {
RequiredPropertyWrapper property = properties.get(key);
TableItem item = table.getItem(i++);
item.setText(0, property.getKey());
item.setText(1, property.getValue() != null ? property.getValue() : "");
item.setText(2, property.getDefaultValue() != null ? property.getDefaultValue() : "");
item.setData(item);
}
}
protected boolean shouldCreateToolTip(Event event) {
Table table = tableViewer.getTable();
Point coords = new Point(event.x, event.y);
TableItem item = table.getItem(coords);
if(item != null) {
List<CandidateRefactoring> prerequisiteRefactorings = getPrerequisiteRefactorings((CandidateRefactoring)item.getData());
if(!prerequisiteRefactorings.isEmpty())
return true;
}
return false;
}
private void saveResults() {
FileDialog fd = new FileDialog(getSite().getWorkbenchWindow().getShell(), SWT.SAVE);
fd.setText("Save Results");
String[] filterExt = { "*.txt" };
fd.setFilterExtensions(filterExt);
String selected = fd.open();
if(selected != null) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(selected));
Table table = tableViewer.getTable();
TableColumn[] columns = table.getColumns();
for(int i=0; i<columns.length; i++) {
if(i == columns.length-1)
out.write(columns[i].getText());
else
out.write(columns[i].getText() + "\t");
}
out.newLine();
for(int i=0; i<table.getItemCount(); i++) {
TableItem tableItem = table.getItem(i);
for(int j=0; j<table.getColumnCount(); j++) {
if(j == table.getColumnCount()-1)
out.write(tableItem.getText(j));
else
out.write(tableItem.getText(j) + "\t");
}
out.newLine();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private ItemPkg getCell(Event event, Table table) {
ItemPkg result = null;
TableItem item = table.getItem(new Point(event.x,event.y));
if (item != null) {
for (int i = 0; i < table.getColumnCount(); i++) {
if (item.getBounds(i).contains(event.x, event.y)) {
result = new ItemPkg(item,i);
break;
}
}
}
return result;
}
private ItemPkg getCell(MouseEvent event, Table table) {
ItemPkg result = null;
TableItem item = table.getItem(new Point(event.x,event.y));
if (item != null) {
for (int i = 0; i < table.getColumnCount(); i++) {
if (item.getBounds(i).contains(event.x, event.y)) {
result = new ItemPkg(item,i);
break;
}
}
}
return result;
}
/**
* Update
* @param attribute
*/
private void updateSelectedAttribute(String attribute) {
if (model != null && model.getInputConfig() != null && model.getInputConfig().getInput() != null) {
Table table = this.table.getViewer().getTable();
for (int i=0; i < table.getItemCount(); i++) {
TableItem item = table.getItem(i);
if (item.getData().equals(attribute)) {
table.select(i);
break;
}
}
}
}
/**
* Get the table item where the given event occurred. Returns null if the event
* occurred in the empty table area.
*/
public static TableItem getItem(TableViewer viewer, MouseEvent event) {
if (viewer == null || event == null)
return null;
Table table = viewer.getTable();
if (table == null)
return null;
return table.getItem(new Point(event.x, event.y));
}
/**
* Handle table selection. In case it's a single selection, enable/disable the 'Up' and 'Down' buttons according to
* the selection. We only allow up and down for checked items.
*/
private void handleTableSelection()
{
ISelection selection = tableViewer.getSelection();
if (selection instanceof StructuredSelection)
{
StructuredSelection structuredSelection = (StructuredSelection) selection;
Table table = tableViewer.getTable();
if (structuredSelection.size() == 1 && table.getItemCount() > 1)
{
int selectionIndex = table.getSelectionIndex();
TableItem item = table.getItem(selectionIndex);
IBuildPathEntry data = (IBuildPathEntry) item.getData();
if (item.getChecked())
{
upButton.setEnabled(selectionIndex != 0);
downButton.setEnabled(selectionIndex < table.getItemCount() - 1
&& selectionIndex < tableViewer.getCheckedElements().length - 1);
if (!selectedEntries.contains(data))
{
selectedEntries.add(data);
tableViewer.refresh();
}
}
else
{
if (selectedEntries.contains(data))
{
selectedEntries.remove(data);
tableViewer.refresh();
}
upButton.setEnabled(false);
downButton.setEnabled(false);
}
}
else
{
upButton.setEnabled(false);
downButton.setEnabled(false);
}
}
}
protected Composite createToolTipContentArea(Event event, Composite parent) {
Composite comp = new Composite(parent,SWT.NONE);
GridLayout gl = new GridLayout(1,false);
gl.marginBottom=0;
gl.marginTop=0;
gl.marginHeight=0;
gl.marginWidth=0;
gl.marginLeft=0;
gl.marginRight=0;
gl.verticalSpacing=1;
comp.setLayout(gl);
Composite topArea = new Composite(comp,SWT.NONE);
GridData data = new GridData(SWT.FILL,SWT.FILL,true,false);
data.widthHint=200;
topArea.setLayoutData(data);
topArea.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
gl = new GridLayout(1,false);
gl.marginBottom=2;
gl.marginTop=2;
gl.marginHeight=0;
gl.marginWidth=0;
gl.marginLeft=5;
gl.marginRight=2;
topArea.setLayout(gl);
Label label = new Label(topArea,SWT.NONE);
label.setText("APPLY FIRST");
label.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
label.setFont(JFaceResources.getFontRegistry().get(HEADER_FONT));
//label.setForeground(JFaceResources.getColorRegistry().get(HEADER_FG_COLOR));
label.setLayoutData(new GridData(GridData.FILL_BOTH));
Table table = tableViewer.getTable();
Point coords = new Point(event.x, event.y);
TableItem item = table.getItem(coords);
if(item != null) {
List<CandidateRefactoring> prerequisiteRefactorings = getPrerequisiteRefactorings((CandidateRefactoring)item.getData());
if(!prerequisiteRefactorings.isEmpty()) {
final CandidateRefactoring firstPrerequisite = prerequisiteRefactorings.get(0);
Composite comp2 = new Composite(comp,SWT.NONE);
comp2.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout();
layout.marginWidth=5;
comp2.setLayout(layout);
Link link = new Link(comp2,SWT.NONE);
link.setText("<a>" + firstPrerequisite.getSourceEntity() + "\n->" + firstPrerequisite.getTarget() + "</a>");
link.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
link.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setSelectedLine(firstPrerequisite);
}
});
comp2.setLayoutData(new GridData(GridData.FILL_BOTH));
}
}
return comp;
}