下面列出了org.eclipse.swt.widgets.Combo#getItems ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int calculateComboWidth( Combo combo ) {
Image image = new Image( HopGui.getInstance().getDisplay(), 10, 10 );
GC gc = new GC( image );
int maxWidth = combo.getSize().x;
for ( String item : combo.getItems() ) {
int width = gc.textExtent( item ).x;
if ( width > maxWidth ) {
maxWidth = width;
}
}
gc.dispose();
image.dispose();
return maxWidth;
}
/**
* 获取项目属性字段集合
* @return key 为属性名称,value 中第一个值为选中的属性值,第二个值为该属性对应的所有属性值集合
*/
public LinkedHashMap<String, Object[]> getAttributeMap() {
LinkedHashMap<String, Object[]> mapAttr = new LinkedHashMap<String, Object[]>();
if (lstCombo != null) {
for (Combo cmb : lstCombo) {
if (!cmb.isDisposed()) {
ArrayList<String> lstAttrValue = new ArrayList<String>();
for (String attrVal : cmb.getItems()) {
lstAttrValue.add(TextUtil.stringToXML(attrVal));
}
mapAttr.put(TextUtil.stringToXML((String) cmb.getData()),
new Object[] { TextUtil.stringToXML(cmb.getText()), lstAttrValue });
}
}
}
return mapAttr;
}
/**
* 获取项目属性字段集合
* @return key 为属性名称,value 中第一个值为选中的属性值,第二个值为该属性对应的所有属性值集合
*/
public LinkedHashMap<String, Object[]> getAttributeMap() {
LinkedHashMap<String, Object[]> mapAttr = new LinkedHashMap<String, Object[]>();
if (lstCombo != null) {
for (Combo cmb : lstCombo) {
if (!cmb.isDisposed()) {
ArrayList<String> lstAttrValue = new ArrayList<String>();
for (String attrVal : cmb.getItems()) {
lstAttrValue.add(TextUtil.stringToXML(attrVal));
}
mapAttr.put(TextUtil.stringToXML((String) cmb.getData()),
new Object[] { TextUtil.stringToXML(cmb.getText()), lstAttrValue });
}
}
}
return mapAttr;
}
protected void select(Combo combo, Enum<?> enu) {
String[] items = combo.getItems();
for (int i = 0; i < items.length; i++) {
if (enu.toString().equals(items[i])) {
combo.select(i);
break;
}
}
}
/**
* Set the source path that was selected by the user by various input
* methods (Browse button, typing, etc).
*
* Clients can also call this to set the path programmatically (hard-coded
* initial path) and this can also be overridden to be notified when the
* source path changes.
*
* @param path
* the source path
*/
protected void setSourcePath(String path) {
Combo sourceField = getSourceField();
if (sourceField == null) {
return;
}
if (path.length() > 0) {
String[] currentItems = sourceField.getItems();
int selectionIndex = -1;
for (int i = 0; i < currentItems.length; i++) {
if (currentItems[i].equals(path)) {
selectionIndex = i;
}
}
if (selectionIndex < 0) {
int oldLength = currentItems.length;
String[] newItems = new String[oldLength + 1];
System.arraycopy(currentItems, 0, newItems, 0, oldLength);
newItems[oldLength] = path;
sourceField.setItems(newItems);
selectionIndex = oldLength;
}
sourceField.select(selectionIndex);
}
resetSelection();
}
private void saveComboValues(Combo combo, IDialogSettings settings, String key) {
// update names history
String[] directoryNames = settings.getArray(key);
if (directoryNames == null) {
directoryNames = new String[0];
}
String items[] = combo.getItems();
for (int i = 0; i < items.length; i++) {
directoryNames = addToHistory(directoryNames, items[i]);
}
settings.put(key, directoryNames);
}
@Override
public boolean performOk() {
if (lstText != null) {
LinkedHashMap<String, String> mapField = new LinkedHashMap<String, String>();
for (Text txt : lstText) {
if (!txt.isDisposed()) {
mapField.put(TextUtil.stringToXML((String) txt.getData()), TextUtil.stringToXML(txt.getText()).trim());
}
}
projCfgBean.setMapField(mapField);
}
if (lstCombo != null) {
LinkedHashMap<String, Object[]> mapAttr = new LinkedHashMap<String, Object[]>();
for (Combo cmb : lstCombo) {
if (!cmb.isDisposed()) {
ArrayList<String> lstAttrValue = new ArrayList<String>();
for (String attrVal : cmb.getItems()) {
lstAttrValue.add(TextUtil.stringToXML(attrVal));
}
mapAttr.put(TextUtil.stringToXML((String) cmb.getData()),
new Object[] { TextUtil.stringToXML(cmb.getText()), lstAttrValue });
}
}
projCfgBean.setMapAttr(mapAttr);
}
return super.performOk();
}
@Override
public boolean performOk() {
if (lstText != null) {
LinkedHashMap<String, String> mapField = new LinkedHashMap<String, String>();
for (Text txt : lstText) {
if (!txt.isDisposed()) {
mapField.put(TextUtil.stringToXML((String) txt.getData()), TextUtil.stringToXML(txt.getText()).trim());
}
}
projCfgBean.setMapField(mapField);
}
if (lstCombo != null) {
LinkedHashMap<String, Object[]> mapAttr = new LinkedHashMap<String, Object[]>();
for (Combo cmb : lstCombo) {
if (!cmb.isDisposed()) {
ArrayList<String> lstAttrValue = new ArrayList<String>();
for (String attrVal : cmb.getItems()) {
lstAttrValue.add(TextUtil.stringToXML(attrVal));
}
mapAttr.put(TextUtil.stringToXML((String) cmb.getData()),
new Object[] { TextUtil.stringToXML(cmb.getText()), lstAttrValue });
}
}
projCfgBean.setMapAttr(mapAttr);
}
return super.performOk();
}
/**
* Returns the index of the item, or adds it to the combo.
*
* @param combo
* @param value
* @return
*/
private int indexOf(Combo combo, char value){
for (int i=0; i < combo.getItems().length; i++) {
if (combo.getItem(i).toCharArray()[1]==value) {
return i;
}
}
combo.add("("+String.valueOf(value)+")"); //$NON-NLS-1$ //$NON-NLS-2$
return combo.getItemCount()-1;
}