< prev index next >

application/org.openjdk.jmc.rcp.application/src/main/java/org/openjdk/jmc/rcp/application/commands/OpenFile.java

Print this page

        

*** 32,41 **** --- 32,47 ---- */ package org.openjdk.jmc.rcp.application.commands; import java.io.File; import java.io.PrintStream; + import java.nio.file.Files; + import java.nio.file.Path; + import java.nio.file.Paths; + import java.util.Deque; + import java.util.ArrayDeque; + import java.util.Arrays; import org.openjdk.jmc.commands.IExecute; import org.openjdk.jmc.commands.Statement; import org.openjdk.jmc.ui.MCPathEditorInput; import org.openjdk.jmc.ui.WorkbenchToolkit;
*** 46,59 **** --- 52,105 ---- public class OpenFile implements IExecute { private static final String FILE_NAME_PARAMETER = "filename"; //$NON-NLS-1$ @Override public boolean execute(Statement statement, PrintStream out) { + String fileName = statement.getString(FILE_NAME_PARAMETER); + fileName = fileName.replaceFirst("^~", System.getProperty("user.home")); + Path path = Paths.get(fileName); + if (!Files.exists(path)) { + fileName = findFile(fileName,new File(System.getProperty("user.home"))); + } if (fileName != null) { WorkbenchToolkit.openEditor(new MCPathEditorInput(new File(fileName))); } else { out.println(statement.getCommand().getIdentifier() + " requires a filename"); //$NON-NLS-1$ } return false; } + + public String findFile(String name,File file) { + File next = null; + File list[] = null; + Deque<File> stack = null; + list = file.listFiles(); + if (list != null) { + stack = new ArrayDeque<File>(Arrays.asList(list)); + } + if (stack != null) { + while (!stack.isEmpty()) { + next = stack.pop(); + if (!next.isHidden() && !next.isDirectory()) { + if (name.equals(next.getName())) { + return next.getAbsolutePath(); + } + } else if (!next.isHidden() && next.isDirectory()) { + list = file.listFiles(); + if (list != null) { + for (File fil : list) { + if (!fil.isHidden() && !fil.isDirectory()) { + if (name.equals(fil.getName())) { + return fil.getAbsolutePath(); + } + } else if (!fil.isHidden() && fil.isDirectory()) { + stack.add(fil); + } + } + } + } + } + } + return null; + } }
< prev index next >