【发布时间】:2019-06-22 10:06:23
【问题描述】:
我正在启动一个新的 Eclipse RCP 应用程序,这是我第一次遇到问题,我想显示可用数据库的列表(顺便说一下,我使用的是 nosql 数据库(MongoDB))但我的代码似乎不工作,谁能帮忙,谁能给我一个好的教程 感谢您的时间和帮助。
package test2.parts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import org.eclipse.swt.widgets.Label;
public class SamplePart {
org.eclipse.swt.widgets.List list ;
private TableViewer tableViewer;
@Inject
private MPart part;
@PostConstruct
public void createComposite(Composite parent) {
parent.setLayout(new GridLayout(1, false));
Text txtInput = new Text(parent, SWT.BORDER);
txtInput.setMessage("Enter text to mark part as dirty");
txtInput.addModifyListener(e -> part.setDirty(true));
txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
list = new org.eclipse.swt.widgets.List(parent, SWT.BORDER);
tableViewer = new TableViewer(parent);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
tableViewer.setInput(createInitialDataModel());
tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
}
@Focus
public void setFocus() {
tableViewer.getTable().setFocus();
}
@Persist
public void save() {
part.setDirty(false);
}
private List<String> createInitialDataModel() {
MongoClient mongoClient = new MongoClient("localhost", 27017);
ArrayList<String> dbs = new ArrayList<String>();
MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
while (dbsCursor.hasNext()) {
list.add(dbsCursor.next());
}
return (List<String>) list;
}
}
【问题讨论】:
-
请将您的代码发布为文本而不是图像。无法复制图像来测试代码。什么不起作用?发生什么了?你预计会发生什么?
-
我希望从我的代码中列出数据库名称,
标签: java eclipse mongodb eclipse-rcp e4