Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b216663188311d4fe7f4c0879176bbcca23bc664 @JSansalone committed Jul 20, 2012
BIN bin/org/jdevelopment/JCMD.class
Binary file not shown.
BIN bin/org/jdevelopment/Rotulable.class
Binary file not shown.
BIN bin/org/jdevelopment/modules/JavaCommandPrompt.class
Binary file not shown.
BIN bin/org/jdevelopment/modules/JavaTextEditor.class
Binary file not shown.
BIN img.jar
Binary file not shown.
BIN img/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
237 src/org/jdevelopment/JCMD.java
@@ -0,0 +1,237 @@
+package org.jdevelopment;
+
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.net.URL;
+
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JPanel;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+import org.jdevelopment.modules.JavaCommandPrompt;
+import org.jdevelopment.modules.JavaTextEditor;
+
+import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
+import com.jgoodies.looks.plastic.theme.DarkStar;
+
+public class JCMD extends JFrame implements Rotulable, ActionListener {
+
+ private Object activeWindow;
+
+ private JavaCommandPrompt prompt;
+ private JavaTextEditor editor;
+
+ // Editor ============================
+ private JMenuBar editorBar;
+ // Menu File
+ private JMenuItem itmEditorNew;
+ private JMenuItem itmEditorSave;
+ private JMenuItem itmEditorSaveAs;
+ private JMenuItem itmEditorOpen;
+ private JMenuItem itmEditorExit;
+ // --------------
+ // Menu Edit
+ private JMenuItem itmEditorCopy;
+ private JMenuItem itmEditorPaste;
+ private JMenuItem itmEditorSelectAll;
+ private JMenuItem itmEditorFind;
+ private JMenuItem itmEditorReplace;
+ // --------------
+ // Menu Format
+ private JMenuItem itmEditorBackground;
+ private JMenuItem itmEditorFont;
+ // --------------
+ // Menu Help
+ private JMenuItem itmEditorHelp;
+ private JMenuItem itmEditorAbout;
+ // --------------
+ // ================================
+
+ {
+ prompt = new JavaCommandPrompt(this);
+ activeWindow = prompt;
+ editor = new JavaTextEditor(this);
+ editor.setVisible(false);
+ }
+
+ {
+ editorBar = new JMenuBar();
+
+ // ----------------------------------------
+ JMenu menuEditorFile = new JMenu("File");
+ menuEditorFile.setMnemonic('F');
+
+ itmEditorNew = new JMenuItem("New");
+ itmEditorNew.addActionListener(this);
+ itmEditorNew.setMnemonic('N');
+ itmEditorSave = new JMenuItem("Save");
+ itmEditorSave.addActionListener(this);
+ itmEditorSave.setMnemonic('S');
+ itmEditorSaveAs = new JMenuItem("Save as");
+ itmEditorSaveAs.addActionListener(this);
+ itmEditorSaveAs.setMnemonic('a');
+ itmEditorOpen = new JMenuItem("Open");
+ itmEditorOpen.addActionListener(this);
+ itmEditorOpen.setMnemonic('O');
+ itmEditorExit = new JMenuItem("Exit");
+ itmEditorExit.addActionListener(this);
+ itmEditorExit.setMnemonic('x');
+
+ menuEditorFile.add(itmEditorNew);
+ menuEditorFile.add(itmEditorSave);
+ menuEditorFile.add(itmEditorSaveAs);
+ menuEditorFile.add(itmEditorOpen);
+ menuEditorFile.addSeparator();
+ menuEditorFile.add(itmEditorExit);
+ // ---------------------------------------------
+
+ // ---------------------------------------------
+ JMenu menuEditorEdit = new JMenu("Edit");
+ menuEditorEdit.setMnemonic('E');
+
+ itmEditorCopy = new JMenuItem("Copy");
+ itmEditorCopy.addActionListener(this);
+ itmEditorCopy.setMnemonic('C');
+ itmEditorPaste = new JMenuItem("Paste");
+ itmEditorPaste.addActionListener(this);
+ itmEditorPaste.setMnemonic('P');
+ itmEditorSelectAll = new JMenuItem("Select all");
+ itmEditorSelectAll.addActionListener(this);
+ itmEditorSelectAll.setMnemonic('l');
+ itmEditorFind = new JMenuItem("Find");
+ itmEditorFind.addActionListener(this);
+ itmEditorFind.setMnemonic('F');
+ itmEditorReplace = new JMenuItem("Replace");
+ itmEditorReplace.addActionListener(this);
+ itmEditorReplace.setMnemonic('R');
+
+ menuEditorEdit.add(itmEditorCopy);
+ menuEditorEdit.add(itmEditorPaste);
+ menuEditorEdit.add(itmEditorSelectAll);
+ menuEditorEdit.add(itmEditorFind);
+ menuEditorEdit.add(itmEditorReplace);
+ // ---------------------------------------------
+
+ // ---------------------------------------------
+ JMenu menuEditorFormat = new JMenu("Format");
+ menuEditorFormat.setMnemonic('t');
+
+ itmEditorBackground = new JMenuItem("Background");
+ itmEditorBackground.addActionListener(this);
+ itmEditorBackground.setMnemonic('B');
+ itmEditorFont = new JMenuItem("Font");
+ itmEditorFont.addActionListener(this);
+ itmEditorFont.setMnemonic('F');
+
+ menuEditorFormat.add(itmEditorBackground);
+ menuEditorFormat.add(itmEditorFont);
+ // ---------------------------------------------
+
+ // ---------------------------------------------
+ JMenu menuEditorHelp = new JMenu("Help");
+ menuEditorHelp.setMnemonic('H');
+
+ itmEditorHelp = new JMenuItem("Help");
+ itmEditorHelp.addActionListener(this);
+ itmEditorHelp.setMnemonic('H');
+ itmEditorAbout = new JMenuItem("About");
+ itmEditorAbout.addActionListener(this);
+ itmEditorAbout.setMnemonic('A');
+
+ menuEditorHelp.add(itmEditorHelp);
+ menuEditorHelp.add(itmEditorAbout);
+ // ---------------------------------------------
+
+ editorBar.add(menuEditorFile);
+ editorBar.add(menuEditorEdit);
+ editorBar.add(menuEditorFormat);
+ editorBar.add(menuEditorHelp);
+ }
+
+ public JCMD() {
+
+ setTitle("Java Command Prompt");
+ setIconImage(getToolkit().getImage(
+ (URL) getClass().getResource("/img/icon.png")));
+ setSize(700, 400);
+ setMinimumSize(new Dimension(700, 400));
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
+ setLocationRelativeTo(null);
+
+ // getContentPane().add(editor);
+ getContentPane().add(prompt);
+
+ setVisible(true);
+
+ }
+
+ public static void main(String[] args) {
+
+ PlasticXPLookAndFeel laf = new PlasticXPLookAndFeel();
+ PlasticXPLookAndFeel.setCurrentTheme(new DarkStar());
+
+ try {
+ UIManager.setLookAndFeel(laf);
+ } catch (UnsupportedLookAndFeelException e1) {
+ e1.printStackTrace();
+ }
+
+ new JCMD();
+
+ }
+
+ @Override
+ public void setFrameTitle(String title) {
+
+ setTitle(title);
+
+ }
+
+ @Override
+ public void notifyExit(String cmd) {
+
+ if (cmd.equals("cmdtojedit")) {
+
+ chooseActiveWindow(editor, activeWindow);
+
+ } else if (cmd.equals("jedittocmd")) {
+
+ chooseActiveWindow(prompt, activeWindow);
+
+ }
+
+ }
+
+ private void chooseActiveWindow(Object nextWindow, Object previousWindow) {
+
+ add((JPanel) nextWindow);
+ remove((JPanel) previousWindow);
+ if (nextWindow instanceof JavaTextEditor) {
+ setJMenuBar(editorBar);
+ } else {
+ setJMenuBar(null);
+ }
+ ((JPanel) nextWindow).setVisible(true);
+ ((JPanel) previousWindow).setVisible(false);
+
+ activeWindow = nextWindow;
+
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+
+ if (e.getSource() == itmEditorExit) {
+
+ notifyExit("jedittocmd");
+
+ }
+
+ }
+
+}
8 src/org/jdevelopment/Rotulable.java
@@ -0,0 +1,8 @@
+package org.jdevelopment;
+
+public interface Rotulable {
+
+ void setFrameTitle(String title);
+ void notifyExit(String cmd);
+
+}
406 src/org/jdevelopment/modules/JavaCommandPrompt.java
@@ -0,0 +1,406 @@
+package org.jdevelopment.modules;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.io.File;
+import java.io.IOException;
+import java.util.Scanner;
+
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+
+import org.jdevelopment.Rotulable;
+
+public class JavaCommandPrompt extends JPanel implements ActionListener,
+ FocusListener, KeyListener {
+
+ private JTextArea area;
+ private JTextField field;
+
+ private String currentDirectory;
+
+ private Rotulable parent;
+
+ private final Runtime runtime = Runtime.getRuntime();
+
+ {
+
+ try {
+ File tmp = new File("tmp01020304");
+ tmp.createNewFile();
+ currentDirectory = tmp.getAbsolutePath().replace("tmp01020304", "");
+ currentDirectory = currentDirectory.substring(0,
+ currentDirectory.length() - 1);
+ tmp.delete();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public JavaCommandPrompt(Rotulable parent) {
+
+ setLayout(new BorderLayout());
+
+ this.parent = parent;
+
+ area = new JTextArea();
+ area.setBackground(Color.BLACK);
+ area.setForeground(Color.GREEN);
+ area.setFont(new Font("Courier New", Font.BOLD, 12));
+ area.setEditable(false);
+ area.setLineWrap(true);
+ area.setWrapStyleWord(true);
+ area.setText(""
+ + "Welcome to Java Command Prompt!\n\n"
+ + "You can type:\n\n"
+ + " > jedit - To use the Java Text Editor\n"
+ + " > exit - To exit the application\n"
+ + " > DOS commands - Many commands of MS-DOS are supported\n"
+ + " > listdos - To list all supported commands\n\n");
+
+ field = new JTextField();
+ field.setBackground(Color.BLACK);
+ field.setForeground(Color.RED);
+ field.setFont(new Font("Courier New", Font.BOLD, 12));
+ field.setCaretColor(Color.RED);
+ field.setBorder(null);
+ field.addActionListener(this);
+ field.addFocusListener(this);
+ field.addKeyListener(this);
+ field.setText("type your command here");
+
+ add("Center", new JScrollPane(area));
+ add("South", field);
+
+ }
+
+ private void refineCommand(String cmd) {
+
+ String[] splited = cmd.split(" ");
+ System.out.println(splited[0]);
+
+ String path = "";
+ String curAux = "";
+
+ if (splited[0].equals("cls")) {
+
+ area.setText("");
+
+ } else if (splited[0].equals("exit")) {
+
+ System.exit(0);
+
+ } else if (splited[0].equals("cd")) {
+
+ path = cmd.trim().substring(2, cmd.trim().length());
+ path = path.trim();
+
+ if (path.equals("..")) {
+ currentDirectory = moveUP(currentDirectory);
+ execute("cd " + currentDirectory);
+ } else {
+
+ if (path.length() > 1) {
+
+ if (path.substring(0, 2).equals("..")) {
+
+ String aux = path;
+ aux = aux.replace("/", "\\");
+ String[] aux2 = aux.split("\\\\");
+
+ for (int i = 0; i < aux2.length; i++) {
+ if (aux2[i].equals("..")) {
+ currentDirectory = moveUP(currentDirectory);
+ } else {
+ currentDirectory = currentDirectory + "\\"
+ + aux2[i];
+ }
+ }
+
+ execute("cd " + currentDirectory);
+
+ } else {
+
+ execute(cmd);
+ if (!path.contains(":")) {
+ currentDirectory = currentDirectory + "\\" + path;
+ } else {
+ currentDirectory = path;
+ }
+
+ }
+
+ } else if (path.equals(".")) {
+
+ execute("cd " + currentDirectory);
+
+ } else if (path.equals("\\")) {
+
+ String aux2[] = currentDirectory.split("\\\\");
+
+ currentDirectory = aux2[0] + "\\";
+
+ execute("cd " + currentDirectory);
+
+ }
+
+ }
+
+ } else if (splited[0].equals("dir")) {
+
+ if (splited[0].length() == 3) {
+
+ currentDirectory = currentDirectory.replace("\\\\", "\\");
+
+ execute("dir \"" + currentDirectory + "\"");
+
+ } else {
+
+ if (splited[1].equals("..")) {
+
+ curAux = "";
+ String aux = splited[1];
+ aux = aux.replace("/", "\\");
+ String[] aux2 = aux.split("\\\\");
+
+ for (int i = 0; i < aux2.length; i++) {
+ if (aux2[i].equals("..")) {
+ curAux = moveUP(currentDirectory);
+ } else {
+ curAux = currentDirectory + "\\" + aux2[i];
+ }
+ }
+
+ execute("dir \"" + curAux + "\"");
+
+ } else {
+
+ if (!splited[1].contains(":")) {
+ execute("dir \"" + currentDirectory + "\\" + splited[1]
+ + "\"");
+ } else {
+ execute(cmd);
+ }
+
+ }
+
+ }
+
+ } else if (splited[0].equals("del")) {
+
+ if(splited.length > 1){
+
+ execute("del " + currentDirectory + "\\" + splited[1]);
+
+ }
+
+ } else if (splited[0].equals("mkdir")) {
+
+ if (splited[1].equals("..")) {
+ curAux = moveUP(currentDirectory);
+ execute("mkdir \"" + curAux + "\"");
+ } else {
+
+ if (path.length() > 1) {
+
+ if (splited[1].substring(0, 2).equals("..")) {
+
+ String aux = splited[1];
+ aux = aux.replace("/", "\\");
+ String[] aux2 = aux.split("\\\\");
+
+ for (int i = 0; i < aux2.length; i++) {
+ if (aux2[i].equals("..")) {
+ curAux = moveUP(currentDirectory);
+ } else {
+ curAux = currentDirectory + "\\" + aux2[i];
+ }
+ }
+
+ execute("mkdir \"" + curAux + "\"");
+
+ } else {
+
+ if (!splited[1].contains(":")) {
+ curAux = currentDirectory + "\\" + splited[1];
+ execute("mkdir \"" + curAux + "\"");
+ } else {
+ curAux = splited[1];
+ }
+
+ }
+
+ } else {
+
+ execute("mkdir \"" + currentDirectory + "\"");
+
+ }
+
+ }
+
+ } else if (splited[0].equals("rmdir")) {
+
+ if (splited[1].equals("..")) {
+ curAux = moveUP(currentDirectory);
+ execute("rmdir \"" + curAux + "\"");
+ } else {
+
+ if (splited[1].length() > 1) {
+
+ if (splited[1].substring(0, 2).equals("..")) {
+
+ String aux = splited[1];
+ aux = aux.replace("/", "\\");
+ String[] aux2 = aux.split("\\\\");
+
+ for (int i = 0; i < aux2.length; i++) {
+ if (aux2[i].equals("..")) {
+ curAux = moveUP(currentDirectory);
+ } else {
+ curAux = currentDirectory + "\\" + aux2[i];
+ }
+ }
+
+ execute("rmdir \"" + curAux + "\"");
+
+ } else {
+
+ if (!splited[1].contains(":")) {
+ curAux = currentDirectory + "\\" + splited[1];
+ execute("rmdir \"" + curAux + "\"");
+ } else {
+ curAux = splited[1];
+ }
+
+ }
+
+ } else {
+
+ execute("rmdir \"" + currentDirectory + "\"");
+
+ }
+
+ }
+
+ } else if (splited[0].equals("jedit")) {
+
+ parent.notifyExit("cmdtojedit");
+
+ } else {
+
+ if(!execute(cmd)){
+ area.append("Command unknow");
+ }
+
+ }
+
+ parent.setFrameTitle("Java Command Prompt - "
+ + currentDirectory.replace("\\\\", "\\"));
+
+ }
+
+ private boolean execute(String cmd) {
+
+ try {
+ Scanner sc = new Scanner(runtime.exec("cmd /c " + cmd)
+ .getInputStream());
+ while (sc.hasNext()) {
+ area.append(sc.nextLine() + "\n");
+ }
+ return true;
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return false;
+ }
+
+ }
+
+ private String moveUP(String currentDir) {
+
+ currentDir = currentDir.replace("/", "\\");
+ String[] aux = currentDir.split("\\\\");
+ String newDir = aux[0];
+
+ for (int i = 1; i < aux.length - 1; i++) {
+ newDir += "\\" + aux[i];
+ }
+
+ return newDir.length() == 2 && newDir.contains(":") ? newDir + "\\"
+ : newDir;
+
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+
+ if (e.getSource() == field) {
+
+ area.append("> "
+ + field.getText().substring(2, field.getText().length())
+ + "\n");
+ refineCommand(field.getText()
+ .substring(2, field.getText().length()));
+ field.setText("> ");
+
+ }
+
+ }
+
+ @Override
+ public void focusGained(FocusEvent e) {
+
+ field.setText("> ");
+
+ }
+
+ @Override
+ public void focusLost(FocusEvent e) {
+
+ if (e.getSource() == field) {
+
+ field.setText("type your command here");
+
+ }
+
+ }
+
+ @Override
+ public void keyTyped(KeyEvent e) {
+
+ if (e.getSource() == field) {
+ if (field.getText().length() < 2) {
+ field.setText("> ");
+ field.setCaretPosition(2);
+ }
+ }
+
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+
+ }
+
+ @Override
+ public void keyReleased(KeyEvent e) {
+ if (field.getCaretPosition() < 3) {
+ field.setCaretPosition(2);
+ }
+ if (!field.getText().substring(0, 2).equals("> ")) {
+ field.setText("> ");
+ }
+
+ }
+
+}
29 src/org/jdevelopment/modules/JavaTextEditor.java
@@ -0,0 +1,29 @@
+package org.jdevelopment.modules;
+
+import java.awt.BorderLayout;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+
+import org.jdevelopment.Rotulable;
+
+public class JavaTextEditor extends JPanel {
+
+ private JTextArea area;
+
+ private Rotulable parent;
+
+ public JavaTextEditor(Rotulable parent) {
+
+ this.parent = parent;
+
+ setLayout(new BorderLayout());
+
+ area = new JTextArea();
+ add("Center",new JScrollPane(area));
+
+ }
+
+}

0 comments on commit b216663

Please sign in to comment.