我试图使用swt构建输入对话框。 我想读取我的类属性,并按指定的顺序甚至词典顺序创建对话框窗口。我将我的类属性保存在linkedhahmap \ treemap中。
示例:
public class MazeProperties {
/** Maze Name. */
private String MazeName;
/** Number of rows in maze. */
private int x;
/** The Floors. */
private int y;
/** Number of columns in maze. */
private int z;
public MazeProperties(String mazeName, int rows, int floors, int columns) {
MazeName = mazeName;
Rows = rows;
Floors = floors;
Columns = columns;
}
public class ClassInputDialog extends Dialog{
/** The shell. */
Shell shell;
/** the generic class. */
private Class<?> template;
/** Property Descriptors give me the ability to see what properties the class contains - and has generic functionalities for setters and getters for fields!. */
PropertyDescriptor[] descs;
/** I wanna have a robust connection between a property to a text box - that way upon pressing OK I could know what class property was written in it.
*
*/
HashMap<PropertyDescriptor,Text> txtMap=new LinkedHashMap<PropertyDescriptor,Text>();
//TreeMap<String,Text> txtMapOrderName=new TreeMap<String,Text>();
//Map<PropertyDescriptor, Text> txtMap = Collections.synchronizedMap(new LinkedHashMap<PropertyDescriptor, Text>());
/** The bonus demanded that this dialog will support all given classes
* but what happens when a class has an enum? a whole new story with combo-boxes and once again I wanna have a connection between the class field enum to the String that was selected in the form.
*
*/
HashMap<PropertyDescriptor,String> enumMap=new **HashMap<PropertyDescriptor,String>();**
/** This is the reference of the instance I will return.
*
*/
private Object input;
/** Ct'r for people that don't know a thing about SWT.
* @param parent - Shell
* @param template - The Class to create form to
*/
public ClassInputDialog(Shell parent,Class<?> template) {
this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL,template);
}
/**
* Ct'r with SWT style.
*
* @param parent - Shell
* @param style - SWT style
* @param template - The Class to create form to
*/
public ClassInputDialog(Shell parent, int style,Class<?> template) {
super(parent, style);
this.template=template;
descs=PropertyUtils.getPropertyDescriptors(template);
setText("Set Properties");
}
/**
* Gets the input.
*
* @return the input
*/
public Object getInput() {
return input;
}
/**
* Sets the input.
*
* @param input the new input
*/
public void setInput(Object input) {
this.input = input;
}
/** Here the window layout is set and the main loop event happens. When window closes User's input is returned.
* @return The user's input
*/
public Object open() {
this.shell = new Shell(getParent(), getStyle());
shell.setText(getText());
createContents(shell);
shell.pack();
shell.open();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
//display.dispose();
return input;
}
/** Creates Window content and layout - sets Labels, Text boxes and combo boxes nicely.
* @param shell - window's parent
*/
private void createContents(final Shell shell) {
shell.setLayout(new GridLayout(2, true));
for(**PropertyDescriptor propDesc: descs**)
if(!propDesc.getName().equals("class"))
{
if(!propDesc.getPropertyType().isEnum())
{
Label label = new Label(shell, SWT.NONE);
label.setText(propDesc.getName());
GridData data = new GridData();
data.horizontalSpan = 2;
label.setLayoutData(data);
final Text text = new Text(shell, SWT.BORDER);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
text.setLayoutData(data);
txtMap.put(propDesc, text);
// txtMapOrderName.put(propDesc.getDisplayName(), text);
System.out.println(propDesc.getDisplayName());
}
else
{
Label label = new Label(shell, SWT.NONE);
label.setText(propDesc.getName());
GridData data = new GridData();
data.horizontalSpan = 2;
label.setLayoutData(data);
final Combo combo = new Combo(shell, SWT.DROP_DOWN);
String[] toCombo=new String[propDesc.getPropertyType().getEnumConstants().length];
for(int i=0;i<propDesc.getPropertyType().getEnumConstants().length;i++)
toCombo[i]=propDesc.getPropertyType().getEnumConstants()[i].toString();
combo.setItems(toCombo);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
combo.setLayoutData(data);
combo.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
enumMap.put(propDesc, combo.getText());
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
}
}
输出不是类属性顺序,它类似于
y
mazeName
x
z
答案 0 :(得分:0)
如果要以确定的,可预测的顺序将键映射到值,则应将它们存储在TreeMap
中,注意密钥类实现Comparator
。如果没有,您必须实现自己的Comparator
并将其作为参数传递给TreeMap
构造函数。
在您的情况下,如果您希望PropertyDescriptor
为关键字,则必须实施自己的Comparator
来比较PropertyDescriptor
个对象。