SmComboBoxDatasource 是数据源下拉控件,可在其下拉按钮中选择数据源。其接口说明可参见 SmComboBoxDatasource 接口说明文档。
示范代码
SmComboBoxDatasource 控件使用的示例代码如下:
protected void run() {
MyDialog myDialog = new MyDialog();
myDialog.showDialog();
}
@Override
public boolean enable() {
Dataset[] activeDatasets = Application.getActiveApplication().getActiveDatasets();
return activeDatasets.length > 0 && activeDatasets[0] instanceof DatasetVector;
}
public class MyDialog extends SmDialog {
private JLabel labelDatasource;
private SmComboBoxDatasource smComboBoxDatasource;
private JLabel labelDatasetType;
private SmComboBoxDatasetType smComboBoxDatasetType;
private JLabel labelDataset;
private SmComboBoxDataset smComboBoxDataset;
private JLabel labelFieldInfo;
private SmComboBoxFieldInfo smComboBoxFieldInfo;
public MyDialog() {
this.setSize(new Dimension(300,200));
initComponents();
initLayouts();
initListener();
}
private void initComponents() {
this.labelDatasource = new JLabel("数据源:");
//数据源下拉控件
this.smComboBoxDatasource = new SmComboBoxDatasource();
//设置是否包含只读数据源,此处设置为否
this.smComboBoxDatasource.setIncludeReadOnly(false);
//设置是否支持新建数据源,此处未支持
this.smComboBoxDatasource.setSupportCreate(true);
//支设置不支持的数据源引擎类型,也可以用setSupportEngineTypes()方法来设置支持的引擎类型,测出设置未不支持内存数据源
this.smComboBoxDatasource.setUnSupportEngineTypes(EngineType.MEMORY);
this.labelDatasetType = new JLabel("数据集类型:");
this.smComboBoxDatasetType = new SmComboBoxDatasetType();
//设置是否显示“简单数据集”选项,默认不显示
this.smComboBoxDatasetType.setHasSimpleDataset(true);
//设置是否显示“所有数据集”选项,默认不显示
this.smComboBoxDatasetType.setHasAll(false);
//设置支持的数据集类型,此处设置为点、线、面数据集
this.smComboBoxDatasetType.setSupportedDatasetTypes(DatasetType.REGION,DatasetType.LINE,DatasetType.POINT);
this.labelDataset = new JLabel("数据集:");
//初始化数据集下拉列表控件,也可传入要展示的数据集集合
this.smComboBoxDataset = new SmComboBoxDataset();
//根据数据源或数据集集合设置下拉列表中展示的数据集
this.smComboBoxDataset.setDatasource(this.smComboBoxDatasource.getSelectedDatasource());
this.smComboBoxDataset.setDatasets(this.smComboBoxDatasource.getSelectedDatasource().getDatasets());
//设置是否显示未空选项,默认为false
this.smComboBoxDataset.setSelectNullValue(false);
//设置下拉选项中支持的数据集类型,此处设置为仅显示点、线、面数据集
this.smComboBoxDataset.setSupportedDatasetTypes(DatasetType.REGION, DatasetType.LINE, DatasetType.POINT);
//设置网络数据集是否支持显示子数据集,默认为false
this.smComboBoxDataset.setSupportedChildDataset(false);
this.labelFieldInfo = new JLabel("字段:");
this.smComboBoxFieldInfo = new SmComboBoxFieldInfo();
//设置第一项是否为空,默认为false不为空
this.smComboBoxFieldInfo.setFirstItemEmpty(false);
//设置是否支持系统字段,默认为true
this.smComboBoxFieldInfo.setHasSystemField(true);
//设置下拉选项线是哪一个数据集的字段信息(矢量数据集)
this.smComboBoxFieldInfo.setDatasetVector((DatasetVector) this.smComboBoxDataset.getSelectedDataset());
//设置支持的字段类型
this.smComboBoxFieldInfo.setSupportFieldTypes(FieldType.INT16, FieldType.INT32, FieldType.INT64);
}
private void initLayouts() {
this.setLayout(new GridBagLayout());
this.add(this.labelDatasource, new GridBagConstraintsHelper(0,0,1,1).setFill(GridBagConstraints.NONE).setWeight(0,0).setInsets(10,10,0,5));
this.add(this.smComboBoxDatasource, new GridBagConstraintsHelper(1, 0, 1, 1).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0).setInsets(10, 10, 0, 5));
this.add(this.labelDatasetType, new GridBagConstraintsHelper(0,1,1,1).setFill(GridBagConstraints.NONE).setWeight(0,0).setInsets(10,10,0,5));
this.add(this.smComboBoxDatasetType, new GridBagConstraintsHelper(1,1,1,1).setFill(GridBagConstraints.HORIZONTAL).setWeight(1,0).setInsets(10,10,0,5));
this.add(this.labelDataset, new GridBagConstraintsHelper(0,2,1,1).setFill(GridBagConstraints.NONE).setWeight(0,0).setInsets(10,10,0,5));
this.add(this.smComboBoxDataset, new GridBagConstraintsHelper(1,2,1,1).setFill(GridBagConstraints.HORIZONTAL).setWeight(1,0).setInsets(10,10,0,5));
this.add(this.labelFieldInfo, new GridBagConstraintsHelper(0,3,1,1).setFill(GridBagConstraints.NONE).setWeight(0,0).setInsets(10,10,0,0));
this.add(this.smComboBoxFieldInfo, new GridBagConstraintsHelper(1,3,1,1).setFill(GridBagConstraints.HORIZONTAL).setWeight(1,0).setInsets(10,10,0,5));
}
private void initListener() {
this.smComboBoxDatasource.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
smComboBoxDataset.setDatasource(smComboBoxDatasource.getSelectedDatasource());
}
}
});
this.smComboBoxDatasetType.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
smComboBoxDataset.setSupportedDatasetTypes(smComboBoxDatasetType.getSelectedDatasetTypes());
}
}
});
this.smComboBoxDataset.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
smComboBoxFieldInfo.setDatasetVector((DatasetVector) smComboBoxDataset.getSelectedDataset());
}
}
});
}
}
SmComboBoxDatasource 控件显示效果如下: