code::
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.util.*;
class VirtualDosPrompt
{
JFrame f;
MyTextArea ta;
int length;
JScrollPane sp;
String curpath="";
boolean flag=true;
String fileName="";
VirtualDosPrompt()
{
f=new JFrame("Virtual DOS-Prompt");
ta=new MyTextArea();
ta.setBackground(Color.black);
ta.setForeground(Color.white);
ta.setFont(new Font("lucida console",Font.PLAIN,14));
((JTextComponent)ta).setCaretColor(Color.white);
ta.setLineWrap(true);
curpath=System.getProperty("user.dir")+">";
ta.setText(curpath);
length=curpath.length();
ta.setCaretPosition(length);
sp=new JScrollPane(ta);
f.add(sp);
f.setSize(670,340);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(100,100);
ta.addKeyListener(new MyTextAreaListener());
}
public static void main(String... s)
{
new VirtualDosPrompt();
}
class MyTextArea extends JTextArea
{
MyTextArea()
{
super();
}
MyTextArea(String str)
{
super(str);
}
public boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
{
if(ta.getCaretPosition()>length)
return super.processKeyBinding(ks,e,condition,pressed);
else
{
if(e.getKeyCode()==e.VK_BACK_SPACE || e.getKeyCode()==e.VK_LEFT || e.getKeyCode()==e.VK_HOME || e.getKeyCode()==e.VK_DELETE || ta.getCaretPosition()
else
return super.processKeyBinding(ks,e,condition,pressed);
}
}
}//end inner class
class MyTextAreaListener implements KeyListener
{
char c;
String s="";
public void keyPressed(KeyEvent ke)
{
c=ke.getKeyChar();
if(c=='\n' && flag==true)
{
String temp=ta.getText();
int index=temp.lastIndexOf(">");
s=temp.substring(index+1);
performAction(s.trim(),ke);
s="";
}
if(ke.isControlDown() && flag==false)
{
if(ke.getKeyCode()==ke.VK_S)
nowSaveFile(fileName);
else
{}
}
}
public void keyTyped(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{}
public void performAction(String cmd,KeyEvent ke)
{
if(cmd.equals("exit"))
{
System.exit(0);
}
else if(cmd.equals("cls"))
{
ta.setText("");
ta.setText(curpath);
length=curpath.length();
}
else if(cmd.equals("dir"))
{
showDirDetail();
}
else if(cmd.equals("info"))
{
showInfo();
}
else if(cmd.endsWith(":"))
{
changeDrive(cmd);
}
else if(cmd.equals("cd.."))
{
getParentDirectory();
}
else if(cmd.equals("cd\\"))
{
goToRoot();
}
else if(cmd.startsWith("cd"))
{
changeDirectory(cmd);
}
else if(cmd.startsWith("mkdir"))
{
makeDirectory(cmd);
}
else if(cmd.startsWith("del"))
{
deleteDirectory(cmd);
}
else if(cmd.equals("javac *.java"))
{
compileAll(cmd);
}
else if(cmd.startsWith("javac"))
{
compileJavaFile(cmd);
}
else if(cmd.startsWith("java"))
{
runJavaProgram(cmd);
}
else if(cmd.startsWith("bg"))
{
changeBackGroundColor(cmd);
}
else if(cmd.startsWith("fg"))
{
changeForeGroundColor(cmd);
}
else if(cmd.equals("reset"))
{
resetAll();
}
else if(cmd.startsWith("help") || cmd.equals("?"))
{
showHelp();
}
else if(cmd.equalsIgnoreCase("jinfo"))
{
showJavaInfo();
}
else if(cmd.equalsIgnoreCase("osinfo"))
{
showOSInfo();
}
else if(cmd.startsWith("create file"))
{
flag=false;
createFile(cmd,ke);
}
else if(cmd.startsWith("ren"))
{
renameFile(cmd);
}
else if(cmd.equalsIgnoreCase("wv"))
{
ta.setBackground(Color.white);
ta.setForeground(Color.black);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.black);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("nv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.white);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.white);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("ov"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.orange);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.orange);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("yv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.yellow);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.yellow);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("gv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.green);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.green);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("rv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.red);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.red);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("cv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.cyan);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.cyan);
length=ta.getText().length();
}
else if(cmd.equalsIgnoreCase("grv"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.gray);
ta.append("\n"+curpath);
((JTextComponent)ta).setCaretColor(Color.gray);
length=ta.getText().length();
}
else
{
ta.append("\ncommand not found");
ta.append("\nSee help or ? for command list\n");
ta.append(curpath);
length=ta.getText().length();
}
}
public void resetAll()
{
ta.setBackground(Color.black);
ta.setForeground(Color.white);
((JTextComponent)ta).setCaretColor(Color.white);
ta.append("\n"+"DOs-Prompt Resetted");
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void changeBackGroundColor(String cmd)
{
String bg=cmd.substring(3);
Color c=getColor(bg);
ta.setBackground(c);
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void changeForeGroundColor(String cmd)
{
String fg=cmd.substring(3);
Color c=getColor(fg);
ta.setForeground(c);
ta.append("\n"+curpath);
length=ta.getText().length();
}
public Color getColor(String bg)
{
Color c=new Color(0,0,0);
if(bg.equalsIgnoreCase("red"))
c=Color.red;
else if(bg.equalsIgnoreCase("white"))
c=Color.white;
else if(bg.equalsIgnoreCase("gray"))
c=Color.gray;
else if(bg.equalsIgnoreCase("lightgray"))
c=Color.lightGray;
else if(bg.equalsIgnoreCase("darkgray"))
c=Color.darkGray;
else if(bg.equalsIgnoreCase("black"))
c=Color.black;
else if(bg.equalsIgnoreCase("pink"))
c=Color.pink;
else if(bg.equalsIgnoreCase("orange"))
c=Color.orange;
else if(bg.equalsIgnoreCase("yellow"))
c=Color.yellow;
else if(bg.equalsIgnoreCase("green"))
c=Color.green;
else if(bg.equalsIgnoreCase("magenta"))
c=Color.magenta;
else if(bg.equalsIgnoreCase("cyan"))
c=Color.cyan;
else if(bg.equalsIgnoreCase("blue"))
c=Color.blue;
else if(bg.equalsIgnoreCase("default"))
{
ta.setBackground(Color.black);
ta.setForeground(Color.white);
return null;
}
else
{
ta.append("\n"+"This Color is not present,see help for bg");
}
return c;
}
public void renameFile(String cmd)
{
String cp=curpath.substring(0,curpath.length()-1);
String token[]=cmd.split(" ");
if(token.length!=3)
ta.append("\nren takes two arguments,sourcename and desname");
else
{
String sourceName=token[1];
String desName=token[2];
if(sourceName.indexOf(File.separator)==-1)
sourceName=cp+File.separator+sourceName;
if(desName.indexOf(File.separator)==-1)
desName=cp+File.separator+desName;
File f1=new File(sourceName);
File f2=new File(desName);
System.out.println("f1 name:"+f1);
System.out.println("f2 name:"+f2);
if(f1.exists())
{
f1.renameTo(f2);
if(sourceName.charAt(0)==desName.charAt(0))
ta.append("\nFile Renamed");
else
ta.append("\nFile Renamed and moved");
}
else
ta.append("\n source file does not exists");
}
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void nowSaveFile(String fileName)
{
String data=ta.getText().substring(length+System.getProperty("line.separator").toString().length()-1);
String[] a=data.split("\n");
try
{
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
for(String str:a)
{
bw.write(str);
bw.newLine();
}
bw.close();
flag=true;
ta.append("\nFile created");
ta.append("\n"+curpath);
length=ta.getText().length();
}
catch(Exception e)
{
System.out.println("file creation exception"+e);
ta.append("\n"+"can not create file");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
public void createFile(String cmd,KeyEvent ke)
{
flag=false;
String cp=curpath.substring(0,curpath.length()-1);
String tempfileName=cmd.substring(12);
if(tempfileName.indexOf(File.separator)==-1)
{
fileName=cp+File.separator+tempfileName;
}
else
{
fileName=tempfileName;
}
if(new File(fileName).exists())
{
ta.append("\nFile already exists");
ta.append("\n"+curpath);
flag=true;
}
length=ta.getText().length();
}
public void showOSInfo()
{
ta.append("\n\nOperating system name:-" + System.getProperty("os.name").toString());
ta.append("\nOperating system architecture:-" + System.getProperty("os.arch").toString());
ta.append("\nOperating system version:-" + System.getProperty("os.version").toString());
ta.append("\nFile separator (" + "/" + " on UNIX):-" + System.getProperty("file.separator").toString());
ta.append("\nPath separator (" + ":" + " on UNIX):-" + System.getProperty("path.separator").toString());
ta.append("\nLine separator :-" + System.getProperty("line.separator").toString());
ta.append("\nUser's account name:-" + System.getProperty("user.name").toString());
ta.append("\nUser's home directory:-" + System.getProperty("user.home").toString());
ta.append("\nUser's current working directory:-" + System.getProperty("user.dir").toString());
ta.append("\n\n"+curpath);
length=ta.getText().length();
}
public void showJavaInfo()
{
ta.append("\n\nJava Runtime Environment version:-" + System.getProperty("java.version").toString());
ta.append("\nJava Runtime Environment vendor:-" + System.getProperty("java.vendor").toString());
ta.append("\nJava vendor URL:-" + System.getProperty("java.vendor.url").toString());
ta.append("\nJava installation directory:-" + System.getProperty("java.home").toString());
ta.append("\nJava Virtual Machine specification version:-" + System.getProperty("java.vm.specification.version").toString());
ta.append("\nJava Virtual Machine specification vendor:-" + System.getProperty("java.vm.specification.vendor").toString());
ta.append("\nJava Virtual Machine specification name:-" + System.getProperty("java.vm.specification.name").toString());
ta.append("\nJava Virtual Machine implementation version:-" + System.getProperty("java.vm.version").toString());
ta.append("\nJava Virtual Machine implementation vendor:-" + System.getProperty("java.vm.vendor").toString());
ta.append("\nJava Virtual Machine implementation name:-" + System.getProperty("java.vm.name").toString());
ta.append("\nJava Runtime Environment specification version:-" + System.getProperty("java.specification.version").toString());
ta.append("\nJava Runtime Environment specification vendor:-" + System.getProperty("java.specification.vendor").toString());
ta.append("\nJava Runtime Environment specification name:-" + System.getProperty("java.specification.name").toString());
ta.append("\nJava class format version number:-" + System.getProperty("java.class.version").toString());
ta.append("\nJava class path:-" + System.getProperty("java.class.path").toString());
//ta.append("\nJava Library Path:-" + System.getProperty("java.library.path").toString());
ta.append("\n\n"+curpath);
length=ta.getText().length();
}
public void runJavaProgram(String cmd)
{
String cp=curpath.substring(0,curpath.length()-1);
String fileName=cmd.substring(5);
String str="";
String x=new String("\"");
if(fileName.indexOf(File.separator)==-1)
{
if(cp.length()==3)
{
compileandrun("java -cp "+x+cp.substring(0,2)+x+" "+fileName);
}
else
compileandrun("java -cp "+x+cp+x+" "+fileName);
}
else
{
ta.append("\n"+"Can not run in this way");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
public void compileAll(String cmd)
{
String cp=curpath.substring(0,curpath.length()-1);
String str=cp+File.separator+"*.java";
String x=new String("\"");
compileandrun("javac "+x+str+x);
}
public void compileJavaFile(String cmd)
{
String cp=curpath.substring(0,curpath.length()-1);
String fileName=cmd.substring(6);
String str="";
if(fileName.indexOf(File.separator)==-1)
{
str=cp+File.separator+fileName;
}
else
{
str=fileName;
}
File f=new File(str);
String x=new String("\"");
if(!f.exists())
{
ta.append("\n"+"File doesn't exists");
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
compileandrun("javac "+x+str+x);
}
}
public void compileandrun(String str)
{
System.out.println("command 2 run:"+str);
String s="";
try
{
Process p = Runtime.getRuntime().exec(str);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null)
{
ta.append("\n"+s);
}
while ((s = stdError.readLine()) != null)
{
ta.append("\n"+s);
}
}
catch(Exception e)
{
System.out.println("Exception in compilation"+e);
e.printStackTrace();
}
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void deleteDirectory(String cmd)
{
String dirName=cmd.substring(4);
String cp=curpath.substring(0,curpath.length()-1);
String temp="";
if(dirName.indexOf(File.separator)==-1)
temp=cp+File.separator+dirName;
else
temp=dirName;
File path=new File(temp);
if(!path.exists())
{
ta.append("\n"+"Directory doesn't exists");
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
ta.append("\n"+"Deleteing......");
try
{
deleteDir(path);
ta.append("\n"+"Deletion successful");
ta.append("\n"+curpath);
length=ta.getText().length();
}
catch(Exception e)
{
ta.append("\n"+"Can not delete this directory");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
}
public void deleteDir(File path) {
if (path.isDirectory())
{
File[] files = path.listFiles();
for(int i=0; i
if(files[i].isDirectory())
{
deleteDir(files[i]);
}
else
{
files[i].delete();
}
}
}
path.delete();
}
public void makeDirectory(String cmd)
{
String dirName=cmd.substring(6);
String cp=curpath.substring(0,curpath.length()-1);
String str="";
if(dirName.indexOf(File.separator)==-1)
str=cp+File.separator+dirName;
else if(dirName.indexOf(File.separator)!=-1)
str=dirName;
File f=new File(str);
if(f.exists())
{
ta.append("\n"+"Directory Exists");
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
try
{
f.mkdir();
if(f.exists())
{
ta.append("\n"+"Directory created");
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
ta.append("\n"+"Directory can't be created");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
catch(Exception e)
{
System.out.println("can't create directory");
}
}
}
public void goToRoot()
{
String root=curpath.substring(0,3);
curpath=root+">";
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void getParentDirectory()
{
if(curpath.length()>4)
{
String cp=curpath.substring(0,curpath.length()-1);
File f=new File(cp);
String parent=""+f.getParent();
curpath=parent+">";
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
public void showDirDetail()
{
String path=curpath.substring(0,curpath.length()-1);
int nof=0;
int nod=0;
float tfl=0;
long tdl=0;
File f=new File(path);
File[] list=f.listFiles();
ta.append("\n");
ta.append("Type:"+"\t"+"Size:"+"\t"+"\t"+"Name:");
for(int i=0;i
ta.append("\n");
if(list[i].isDirectory())
{
//tdl+=getDirectorySize(list[i]);
ta.append("
nod++;
}
else
{
float length=0;
tfl+=list[i].length();
if(list[i].length()>9999)
{
length=list[i].length()/1024;
ta.append("
}
else
ta.append("
nof++;
}
}
ta.append("\n");
ta.append("Number of Files:"+nof+"\t"+" Total Length:"+tfl/(1024*1024)+" MB");
ta.append("\n");
ta.append("Number of Directories:"+nod);
//ta.append("\nTotal size iof this Directory:"+(tfl+tdl)/1024+" KB");
ta.append("\n"+curpath);
length=ta.getText().length();
}
public void showInfo()
{
ta.append("\n");
ta.append("*******************************************************************\n");
ta.append("Project Name: "+"Virtual Dos-Prompt\n");
ta.append("Creatred By: "+"Er. Milan Kumar\n");
ta.append("Version : "+"v1.0\n");
ta.append("Devoted to: "+"JAVA\n");
ta.append("*******************************************************************\n");
ta.append(curpath);
length=ta.getText().length();
}
public long getDirectorySize(File path)
{
long size=0;
File[] list=path.listFiles();
for(int i=0;i
if(list[i].isFile())
size+=list[i].length();
else
getDirectorySize(list[i]);
}
return size;
}
public void changeDrive(String drive)
{
FileSystemView fsv=FileSystemView.getFileSystemView();
File f=new File(drive+File.separator);
if(fsv.isDrive(f))
{
curpath=drive+File.separator+">";
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
ta.append("\n"+"No such drive on the system");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
public void changeDirectory(String cmd)
{
String str=cmd.substring(2).trim();
String cp=curpath.substring(0,curpath.length()-1);
boolean check=sfd(cp,str);
if(check==true)
{
if(curpath.length()>4)
curpath=cp+File.separator+str+">";
else
curpath=cp+str+">";
ta.append("\n"+curpath);
length=ta.getText().length();
}
else
{
ta.append("\nno such directory exits");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}
public boolean sfd(String dir,String temp)
{
File f=new File(dir);
File ch[]=f.listFiles();
for(int i=0;i
if(ch[i].getName().equalsIgnoreCase(temp) && ch[i].isDirectory())
return true;
}
return false;
}
public void showHelp()
{
ta.append("\n");
ta.append("--------------|------------------------------|-----------------------------\n");
ta.append(" COMMAND | MEANING | EXAMPLE\n");
ta.append("--------------|------------------------------|-----------------------------\n");
ta.append(" javac | Compile the java Program | javac Test.java\n");
ta.append(" javac *.java | Compile all the java files | javac *.java\n");
ta.append(" java | Run the .class file | java Test\n");
ta.append(" cls | Clear the screen | cls\n");
ta.append(" dir | shows the Directory | dir\n");
ta.append(" info | Shows the info of the prgm | info\n");
ta.append(" cd.. | Change Dir(Parent Dir) | cd..\n");
ta.append(" cd\\ | Change Dir to root | cd\\ \n");
ta.append(" mkdir | Make Directory | mkdir dirName or path\n");
ta.append(" del | Delete | del fileName,DirName,or Path\n");
ta.append(" bg | Background Color | bg red\n");
ta.append(" fg | Foreground COlor | fg black\n");
ta.append(" wv | White Video | wv\n");
ta.append(" nv | Normal Video | nv\n");
ta.append(" ov | Orange Video | ov\n");
ta.append(" yv | Yellow Video | yv\n");
ta.append(" gv | Green Video | gv\n");
ta.append(" rv | Red Video | rv\n");
ta.append(" cv | Cyan Video | cv\n");
ta.append(" grv | Gray Video | grv\n");
ta.append(" jinfo | Java Info | jinfo\n");
ta.append(" osinfo | Operating Sys Info | osinfo\n");
ta.append(" ? or help | Shows Help | ? or help\n");
ta.append(" create file | create file,ctrl+s to create | create file milan.txt\n");
ta.append(" ren | Rename file | ren name1.txt name2.txt\n");
ta.append("\n"+curpath);
length=ta.getText().length();
}
}//end inner class
}//end outer class
caution::i have compiled it with jdk1.7,so if you are having trouble recompile it with your version...........
some more commands are yet to be added in the next version....
Download vdp.jar
Download vdp.rar
Nice Work.....................
ReplyDeletethanks....
ReplyDelete