我必须定义一个名为“路径”的变量,如 ActionListener 内的代码中所示......但我无法在 actionPerformed 方法之外访问“路径”变量!它在方法之外变为空......我如何访问这个变量?!
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Search " + textField.getText()
+ " in Project");
try {
System.out.println(project.members().length);
IResource[] ires = project.members();
String path = "";
String findResult = "notFound";
for (int len = 0; len < ires.length; len++) {
if (!(path = loopInFolders(project, ires[len],
textField.getText())).equals("")) {
System.out.println("found at :" + path);
findResult = "found";
showResultBox(findResult, path);
break;
}
}
if (path.equals("")) {
showResultBox(findResult, path);
}
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
即使我使用 getter 和 setter 将变量定义为类中的全局变量..但是该变量不会保留它在 actionPerformed 中的值!只有在 Listener 内部才有我想要的正确值......我想在其他方法中使用这个值,但它在那里变成了 null!
这是我的课!我想在execute方法中访问path变量,这个变量会在createOutput方法里面填写actionListener,但是在actionListener外面总是null!
public class FindHandler extends AbstractHandler {
private String path;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Shell shell = HandlerUtil.getActiveShell(event);
ISelection sel = HandlerUtil.getActiveMenuSelection(event);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(sel.toString()
.substring(1, sel.toString().indexOf(" ")).trim());
System.out.println("selected folder "
+ sel.toString().substring(1, sel.toString().indexOf(" "))
.trim());
createOutput(shell, project);
if (path != null) {
System.out.println("Pathhhh***444"+ path);
IPath iPath = new Path(path);
IFile file = project.getFile(iPath);
System.out.println("test file*****" + file.getName());
file = ResourcesPlugin.getWorkspace().getRoot()
.getFileForLocation(iPath);
ISelection selection = new StructuredSelection(file);
IViewReference[] views = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getViewReferences();
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().resetPerspective();
for (IViewReference view : views) {
if ("org.eclipse.jdt.ui.PackageExplorer".equals(view.getId())) {
IViewPart pExplorer = view.getView(true);
pExplorer.getViewSite().getSelectionProvider()
.setSelection(selection);
break;
}
}
}
return null;
}
private void createOutput(Shell shell, final IProject project) {
// Creating the window with a textBox and two buttons of 'Search' and
// 'Cancel'
System.out.println(project.getLocation());
final JTextField textField = new JTextField();
final JFrame frame = new JFrame("Search File");
frame.setLayout(null);
frame.setSize(350, 250);
frame.setLocation(350, 250);
JLabel label = new JLabel("Enter File Name:");
label.setSize(120, 20);
label.setLocation(20, 25);
textField.setSize(150, 20);
textField.setLocation(150, 25);
frame.getContentPane().add(textField);
frame.getContentPane().add(label);
JButton button = new JButton("Search");
button.setSize(100, 30);
button.setLocation(70, 100);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Search " + textField.getText()
+ " in Project");
try {
System.out.println(project.members().length);
IResource[] ires = project.members();
path = "";
String findResult = "notFound";
for (int len = 0; len < ires.length; len++) {
if (!(path = loopInFolders(project, ires[len],
textField.getText())).equals("")) {
System.out.println("found at :" + path);
findResult = "found";
showResultBox(findResult, path);
break;
}
}
if (path.equals("")) {
showResultBox(findResult, path);
}
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame.getContentPane().add(button); // Adds Button to content pane of
// frame
JButton button_2 = new JButton("Cancel");
button_2.setSize(100, 30);
button_2.setLocation(180, 100);
button_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
}
});
frame.getContentPane().add(button_2);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
System.out.println("Pathhhh***333"+ path);
}
public String loopInFolders(IProject project, IResource ires,
String fileName) {
if (ires.getName().equals(fileName)) {
return ires.getLocation().toString();
} else {
System.out.println(ires.getName());
IFolder secondFolder = null;
try {
secondFolder = project.getFolder(ires.getName());
System.out.println(secondFolder.members().length);
if (secondFolder.members().length > 0) {
IResource[] ires1 = secondFolder.members();
for (int i = 0; i < ires1.length; i++) {
if (!loopInFolders(project, ires1[i], fileName).equals(
""))
return ires1[i].getLocation().toString();
}
}
} catch (Exception ex) {
}
}
return "";
}
public void showResultBox(String findResult, String path) {
if (findResult.equals("notFound")) {
final JFrame frame = new JFrame("File Not Found");
frame.setLayout(null);
frame.setSize(350, 250);
frame.setLocation(350, 250);
JLabel label = new JLabel("There is no such file in the project!");
label.setSize(600, 20);
label.setLocation(20, 25);
frame.getContentPane().add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
} else {
System.out.println("Pathhhh***111"+ path);
final JFrame frame = new JFrame("File Found");
frame.setLayout(null);
frame.setSize(350, 250);
frame.setLocation(350, 250);
JLabel label = new JLabel("File is found at: " + path);
label.setSize(600, 20);
label.setLocation(20, 25);
frame.getContentPane().add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
}
}