当我在 nameText 中输入库名称(类型 Text )并且该名称已存在于库列表中时,我遇到了一个我想要的问题,按钮 ok 被阻止并且我不能这样做(一条消息显示并通知我会写另一个名字)
我不确切知道我使用的是什么treeSet或arrayList以及如何使用?代码是:
public static void removeDuplicates(List list)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
}
在上下文中:
public class EditLibraryDialog extends Dialog implements ItemListener {
ILibrary library;
Text pathText;
Text nameText;
static boolean okPressed;
public EditLibraryDialog(Shell parent) {
super(parent);
library = null;
}
public EditLibraryDialog(Shell parent, ILibrary library) {
super(parent);
this.library = library;
}
public ILibrary getLibrary() {
return library;
}
public boolean open() {
return open(null);
}
public boolean open(String message) {
final Shell parent = getParent();
final Shell dialog = new Shell(parent, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
okPressed = false;
dialog.setSize(700, 150);
dialog.setLayout(new GridLayout(4, false));
GridData gridData = new GridData();
dialog.setLayoutData(gridData);
Label l;
if (message != null) {
l = new Label(dialog, SWT.NULL);
l.setText(message);
}
l = new Label(dialog, SWT.NULL);
l.setText("Name");
nameText = new Text(dialog, SWT.BORDER);
l = new Label(dialog, SWT.NULL);
l = new Label(dialog, SWT.NULL);
l = new Label(dialog, SWT.NULL);
l.setText("Path");
pathText = new Text(dialog, SWT.BORDER);
});
if (library == null) {
dialog.setText("Add Library");
} else {
dialog.setText("Edit Library");
nameText.setText(library.getName());
pathText.setText(library.getPath());
}
final Button okButton = new Button(dialog, SWT.PUSH);
okButton.setText("Ok");
GridData gridData4 = new GridData(GridData.END, GridData.CENTER, false,
false);
gridData4.horizontalSpan = 3;
gridData4.horizontalIndent = 20;
okButton.setLayoutData(gridData4);
okButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String id;
if (library != null) {
id = library.getId();
} else {
id = UUID.randomUUID().toString();
}
library = new LibraryFactory().create(id, nameText.getText(),
pathText.getText());
dialog.dispose();
okPressed = true;
}});
nameText.addModifyListener(new ModifyListener() {
String lastValidText = "";
@Override
public void modifyText(ModifyEvent e) {
// avoid setting invalid values for the name text field
String textValue = ((Text) e.getSource()).getText();
if (parseName(textValue)) {
lastValidText = textValue;
okButton.setEnabled(textValue.equals("") ? false : true);
} else {
nameText.setText(lastValidText);
}
}
});
nameText.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
Pattern pattern = Pattern.compile("[\\w| ]*");
Matcher matcher = pattern.matcher(e.text);
e.doit = matcher.matches();
}
});
pathText.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9_.-\\\\\\/\\$]*");
Matcher matcher = pattern.matcher(e.text);
e.doit = matcher.matches();
}
});
Button cancelButton = new Button(dialog, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialog.dispose();
}
});
public static void removeDuplicates(List list)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
}
if (nameText.getText().equals(""))
okButton.setEnabled(false);
JOptionPane.showMessageDialog(null,"Existed Library"); }
dialog.open();
Display display = parent.getDisplay();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return okPressed;
}
private boolean parseName(String newText) {
return true;
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}