Advertisement
Jnk1296

cd command

Feb 14th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. /*
  2.  * Copyright © 2015 Jacob Keep (Jnk1296). All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  *  * Redistributions of source code must retain the above copyright notice,
  8.  *   this list of conditions and the following disclaimer.
  9.  *
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *   this list of conditions and the following disclaimer in the documentation
  12.  *   and/or other materials provided with the distribution.
  13.  *
  14.  *  * Neither the name of JuNK Software nor the names of its contributors may
  15.  *   be used to endorse or promote products derived from this software without
  16.  *   specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. package net.risenphoenix.filebrowser.client.commands;
  32.  
  33. import net.risenphoenix.filebrowser.network.PacketSender;
  34. import net.risenphoenix.filebrowser.packet.Packet;
  35. import net.risenphoenix.filebrowser.packet.PacketType;
  36.  
  37. import java.io.File;
  38. import java.util.ArrayList;
  39. import java.util.Random;
  40.  
  41. public class CmdChangeDir extends Command {
  42.  
  43.     public CmdChangeDir(CommandInterpreter interpreter,
  44.                         String[] callArgs, CommandType type) {
  45.         super(interpreter, callArgs, type);
  46.         setHelp("Change Directory");
  47.         setHelp("Changes your current operating directory. Arguments \n" +
  48.                 "accepted are either a case sensitive file name (for Unix \n" +
  49.                 "support) or the UP operator \"..\".");
  50.         setSyntax("cd <dir_name || .. || .>");
  51.     }
  52.  
  53.     @Override
  54.     public void onExecute(String[] args) {
  55.         StringBuilder sb = new StringBuilder();
  56.         String directoryName;
  57.  
  58.         for (int i = 1; i < args.length; i++) {
  59.             sb.append(args[i] + " ");
  60.         }
  61.  
  62.         directoryName = sb.toString().substring(0, sb.toString().length() - 1);
  63.  
  64.         Object[] data = (Object[])getInterpreter().getDirectoryManager().getPacket().getData();
  65.         ArrayList<File> fileListing = (ArrayList<File>)data[1];
  66.  
  67.         File currentDir = getInterpreter().getDirectoryManager().getCurrentDirectory();
  68.  
  69.         if(directoryName.equals("..")) {
  70.             String dir = currentDir.getPath();
  71.             int endIndex = dir.lastIndexOf("/");
  72.             if (endIndex != -1) {
  73.                 String newDir = dir.substring(0, endIndex);
  74.                 if (newDir.length() >= getInterpreter().getDirectoryManager()
  75.                         .getRootDirectory().getPath().length()) {
  76.                     requestDirectoryUpdate(newDir);
  77.                     return;
  78.                 } else if (currentDir.getPath().equals(getInterpreter()
  79.                         .getDirectoryManager().getRootDirectory().getPath())) {
  80.                     System.out.println("Unable to go higher.");
  81.                     return;
  82.                 } else {
  83.                     requestDirectoryUpdate(getInterpreter()
  84.                             .getDirectoryManager().getRootDirectory()
  85.                             .getPath());
  86.                     return;
  87.                 }
  88.             } else {
  89.                 System.out.println("Unable to go higher.");
  90.             }
  91.         } else if (directoryName.equals(".")) {
  92.             String dir = getInterpreter().getDirectoryManager()
  93.                     .getRootDirectory().getPath();
  94.             requestDirectoryUpdate(dir);
  95.             return;
  96.         } else {
  97.             for(File f:fileListing) {
  98.                 if (f.getName().equals(directoryName)) {
  99.                     if (f.isDirectory()) {
  100.                         // All clear, request Directory Update
  101.                         String path = f.getPath();
  102.                         requestDirectoryUpdate(path);
  103.                         return;
  104.                     } else {
  105.                         System.out.println("The requested object is not a directory.");
  106.                         return;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.  
  112.         System.out.println("The directory requested does not exist.");
  113.     }
  114.  
  115.     private void requestDirectoryUpdate(String directory) {
  116.         int transactionID = new Random().nextInt();
  117.         File newDirectory = new File(directory);
  118.  
  119.         Packet p = new Packet(PacketType.REQUEST_DIRECTORY_LISTING,
  120.                 new Object[]{newDirectory, transactionID});
  121.         PacketSender ps = new PacketSender(getInterpreter().getClient()
  122.                 .getRemoteNetworkInfo(), p);
  123.         ps.start();
  124.  
  125.         while(true) {
  126.             if (getInterpreter().getDirectoryManager().getTransactionID()
  127.                     .equals(transactionID)) {
  128.                 return;
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement