< prev index next >

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

Print this page




  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rcp.application.commands;
  34 
  35 import java.io.File;
  36 import java.io.PrintStream;






  37 
  38 import org.openjdk.jmc.commands.IExecute;
  39 import org.openjdk.jmc.commands.Statement;
  40 import org.openjdk.jmc.ui.MCPathEditorInput;
  41 import org.openjdk.jmc.ui.WorkbenchToolkit;
  42 
  43 /**
  44  * Command that opens a file
  45  */
  46 public class OpenFile implements IExecute {
  47         private static final String FILE_NAME_PARAMETER = "filename"; //$NON-NLS-1$
  48 
  49         @Override
  50         public boolean execute(Statement statement, PrintStream out) {

  51                 String fileName = statement.getString(FILE_NAME_PARAMETER);





  52                 if (fileName != null) {
  53                         WorkbenchToolkit.openEditor(new MCPathEditorInput(new File(fileName)));
  54                 } else {
  55                         out.println(statement.getCommand().getIdentifier() + " requires a filename"); //$NON-NLS-1$
  56                 }
  57                 return false;
  58         }


































  59 }


  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rcp.application.commands;
  34 
  35 import java.io.File;
  36 import java.io.PrintStream;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Deque;
  41 import java.util.ArrayDeque;
  42 import java.util.Arrays;
  43 
  44 import org.openjdk.jmc.commands.IExecute;
  45 import org.openjdk.jmc.commands.Statement;
  46 import org.openjdk.jmc.ui.MCPathEditorInput;
  47 import org.openjdk.jmc.ui.WorkbenchToolkit;
  48 
  49 /**
  50  * Command that opens a file
  51  */
  52 public class OpenFile implements IExecute {
  53         private static final String FILE_NAME_PARAMETER = "filename"; //$NON-NLS-1$
  54 
  55         @Override
  56         public boolean execute(Statement statement, PrintStream out) {
  57 
  58                 String fileName = statement.getString(FILE_NAME_PARAMETER);
  59                 fileName = fileName.replaceFirst("^~", System.getProperty("user.home"));
  60                 Path path = Paths.get(fileName);
  61                 if (!Files.exists(path)) {
  62                         fileName = findFile(fileName,new File(System.getProperty("user.home")));
  63                 }
  64                 if (fileName != null) {
  65                         WorkbenchToolkit.openEditor(new MCPathEditorInput(new File(fileName)));
  66                 } else {
  67                         out.println(statement.getCommand().getIdentifier() + " requires a filename"); //$NON-NLS-1$
  68                 }
  69                 return false;
  70         }
  71 
  72         public String findFile(String name,File file) {
  73                 File next = null;
  74                 File list[] = null;
  75                 Deque<File> stack = null;
  76                 list = file.listFiles();
  77                 if (list != null) {
  78                         stack = new ArrayDeque<File>(Arrays.asList(list));
  79                 }
  80                 if (stack != null) {
  81                         while (!stack.isEmpty()) {
  82                                 next = stack.pop();
  83                                 if (!next.isHidden() && !next.isDirectory()) {
  84                                         if (name.equals(next.getName())) {
  85                                                 return next.getAbsolutePath();
  86                                         }
  87                                 } else if (!next.isHidden() && next.isDirectory()) {
  88                                         list = file.listFiles();
  89                                         if (list != null) {
  90                                                 for (File fil : list) {
  91                                                         if (!fil.isHidden() && !fil.isDirectory()) {
  92                                                                 if (name.equals(fil.getName())) {
  93                                                                         return fil.getAbsolutePath();
  94                                                                 }
  95                                                         } else if (!fil.isHidden() && fil.isDirectory()) {
  96                                                                 stack.add(fil);
  97                                                         }
  98                                                 }
  99                                         }
 100                                 }
 101                         }
 102                 }
 103                 return null;
 104     }
 105 }
< prev index next >