Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JavArrow MicroEdition - JavArrow.java

JavArrow.java

Caricato da: Netarrow
Scarica il programma completo

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.GridLayout;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.io.BufferedInputStream;
  12. import java.io.BufferedOutputStream;
  13. import java.io.BufferedWriter;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.util.Properties;
  21. import java.util.Vector;
  22.  
  23. import javax.swing.JFileChooser;
  24. import javax.swing.JFrame;
  25. import javax.swing.JMenu;
  26. import javax.swing.JMenuBar;
  27. import javax.swing.JMenuItem;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JPanel;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.JTextPane;
  32. import javax.swing.UIManager;
  33. import javax.swing.UnsupportedLookAndFeelException;
  34. import javax.swing.WindowConstants;
  35. import javax.swing.UIManager.LookAndFeelInfo;
  36. import javax.swing.filechooser.FileFilter;
  37. /**
  38. @author Netarrow
  39. */
  40.  
  41. public class JavArrow extends JFrame implements ActionListener {
  42.         public static String pathsdk;
  43.         public static String lookandfeel;
  44.         public static FileOutputStream fos;
  45.         public static FileInputStream fis;
  46.         public static Properties p;
  47.         public static boolean isFirst;
  48.         static {
  49.                 try {
  50.                         p = new Properties();
  51.                         fis = new FileInputStream("JavArrow.dat");
  52.                         p.load(fis);
  53.                         pathsdk = p.getProperty("pathsdk");
  54.                         lookandfeel = p.getProperty("lookandfeel");
  55.                         UIManager.setLookAndFeel(lookandfeel);
  56.                 } catch(FileNotFoundException e) {
  57.                         isFirst = true;
  58.                         p.put("pathsdk", "");
  59.                         p.put("lookandfeel", "");
  60.                 } catch (IOException e) {
  61.                         e.printStackTrace();
  62.                 } catch (ClassNotFoundException e) {
  63.                         e.printStackTrace();
  64.                 } catch (InstantiationException e) {
  65.                         e.printStackTrace();
  66.                 } catch (IllegalAccessException e) {
  67.                         e.printStackTrace();
  68.                 } catch (UnsupportedLookAndFeelException e) {
  69.                         e.printStackTrace();
  70.                 }
  71.         }
  72.         private Document doc;
  73.         private JPanel jPanel1;
  74.         private JMenuItem nuovo;
  75.         private JMenuItem run;
  76.         private JMenuItem compile;
  77.         private JMenu options;
  78.         private JMenu project;
  79.         private JTextPane console;
  80.         private JMenuItem esci;
  81.         private JMenuItem salva;
  82.         private JMenuItem saveAs;
  83.         private JMenuItem apri;
  84.         private JMenu file;
  85.         private JTextPane source;
  86.         private JScrollPane jScrollPane1;
  87.         private JScrollPane sourcecontent;
  88.         private JPanel jPanel2;
  89.         private JMenuBar jmb, jmb2, jmb3;
  90.         private static Vector uim;
  91.         private JMenuItem about;
  92.        
  93.         public static void main(String[] args) {
  94.                 JavArrow inst = new JavArrow();
  95.                 inst.setVisible(true);
  96.                 inst.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  97.         }
  98.        
  99.         public static void saveOption(String path, int laf) throws IOException {
  100.                 fos = new FileOutputStream("JavArrow.dat");
  101.                 p.setProperty("pathsdk", path);
  102.                 p.setProperty("lookandfeel", ((LookAndFeelInfo)JavArrow.uim.get(laf)).getClassName());
  103.                 p.store(fos, "JavArrow");
  104.         }
  105.        
  106.         public JavArrow() {
  107.                 super();
  108.                 initGUI();
  109.                 centerFrame();
  110.                 LookAndFeelInfo[] uim = UIManager.getInstalledLookAndFeels();
  111.                 JavArrow.uim = new Vector();
  112.                 for(int i = 0; i < uim.length; i++) {
  113.                         JavArrow.uim.add(uim[i]);
  114.                 }
  115.                 if(isFirst) {
  116.                         new SetOptions(this, uim, true);
  117.                 }
  118.                 doc = new Document(this);
  119.         }
  120.        
  121.         private void initGUI() {
  122.                 try {
  123.                         BorderLayout thisLayout = new BorderLayout();
  124.                         this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  125.                         this.addWindowListener(new WindowAdapter() {
  126.                                 public void windowClosing(WindowEvent evt) {
  127.                                         doc.onExit();
  128.                                 }
  129.                         });
  130.                         this.setTitle("JavArrow MicroEdition - by Netarrow");
  131.                         this.getContentPane().setLayout(thisLayout);
  132.                         this.setSize(511, 419);
  133.                         {
  134.                                 jPanel1 = new JPanel();
  135.                                 BorderLayout jPanel1Layout = new BorderLayout();
  136.                                 jPanel1.setLayout(jPanel1Layout);
  137.                                 this.getContentPane().add(jPanel1, BorderLayout.WEST);
  138.                                 {
  139.                                 jmb = new JMenuBar();
  140.                                         jPanel1.add(jmb, BorderLayout.NORTH);
  141.                                         jmb2 = new JMenuBar();
  142.                                         jmb3 = new JMenuBar();
  143.                                         jPanel1.add(jmb2, BorderLayout.CENTER);
  144.                                         {
  145.                                                 jPanel1.add(jmb3, BorderLayout.SOUTH);
  146.                                                 {
  147.                                                         options = new JMenu();
  148.                                                         jmb3.add(options);
  149.                                                         options.setText("Extras");
  150.                                                         {
  151.                                                                 about = new JMenuItem();
  152.                                                                 options.add(about);
  153.                                                                 about.setText("About");
  154.                                                                 about.addActionListener(new ActionListener() {
  155.                                                                         public void actionPerformed(ActionEvent evt) {
  156.                                                                                 JOptionPane.showMessageDialog(null, "JavArrow MicroEdition by Netarrow 2004", "About", JOptionPane.INFORMATION_MESSAGE);
  157.                                                                         }
  158.                                                                 });
  159.                                                         }
  160.                                                         {
  161.                                                                 JMenuItem op = new JMenuItem();
  162.                                                                 options.add(op);
  163.                                                                 op.setText("Options");
  164.                                                                 op.addActionListener(new ActionListener() {
  165.                                                                         public void actionPerformed(ActionEvent evt) {
  166.                                                                                 onOption();
  167.                                                                         }
  168.                                                                 });
  169.                                                         }
  170.                                                 }
  171.                                                 project = new JMenu();
  172.                                                 jmb2.add(project);
  173.                                                 project.setText("Project");
  174.                                                 {
  175.                                                         compile = new JMenuItem();
  176.                                                         project.add(compile);
  177.                                                         compile.setText("Compile");
  178.                                                         compile.addActionListener(new ActionListener() {
  179.                                                                 public void actionPerformed(ActionEvent evt) {
  180.                                                                         try {
  181.                                                                                 doc.compile(pathsdk, console);
  182.                                                                         } catch (IOException e) {
  183.                                                                                 e.printStackTrace();
  184.                                                                         }
  185.                                                                         doc.compilato = true;
  186.                                                                 }
  187.                                                         });
  188.                                                 }
  189.                                                 {
  190.                                                         run = new JMenuItem();
  191.                                                         project.add(run);
  192.                                                         run.setText("Run");
  193.                                                         run.addActionListener(new ActionListener() {
  194.                                                                 public void actionPerformed(ActionEvent evt) {
  195.                                                                         try {
  196.                                                                                 doc.runn(pathsdk, console);
  197.                                                                         } catch (Exception e) {
  198.                                                                                 e.printStackTrace();
  199.                                                                         }
  200.                                                                 }
  201.                                                         });
  202.                                                 }
  203.                                         }
  204.                                         file = new JMenu();
  205.                                         jmb.add(file);
  206.                                         GridLayout fileLayout = new GridLayout(1, 1);
  207.                                         file.setLayout(fileLayout);
  208.                                         file.setText("File");
  209.                                         {
  210.                                                 apri = new JMenuItem();
  211.                                                 file.add(apri);
  212.                                                 apri.setText("Open");
  213.                                                 apri.addActionListener(this);
  214.                                         }
  215.                                         {
  216.                                                 nuovo = new JMenuItem();
  217.                                                 file.add(nuovo);
  218.                                                 nuovo.setText("New");
  219.                                                 nuovo.addActionListener(this);
  220.                                         }
  221.                                         {
  222.                                                 salva = new JMenuItem();
  223.                                                 file.add(salva);
  224.                                                 salva.setText("Save");
  225.                                                 salva.addActionListener(this);
  226.                                         }
  227.                                         {
  228.                                                 saveAs = new JMenuItem();
  229.                                                 file.add(saveAs);
  230.                                                 saveAs.setText("Save As");
  231.                                                 saveAs.addActionListener(this);
  232.                                         }
  233.                                         {
  234.                                                 esci = new JMenuItem();
  235.                                                 file.add(esci);
  236.                                                 esci.setText("Exit");
  237.                                                 esci.addActionListener(this);
  238.                                         }
  239.                                 }
  240.                         }
  241.                         {
  242.                                 jPanel2 = new JPanel();
  243.                                 BorderLayout jPanel2Layout = new BorderLayout();
  244.                                 jPanel2.setLayout(jPanel2Layout);
  245.                                 this.getContentPane().add(jPanel2, BorderLayout.CENTER);
  246.                                 {
  247.                                         jScrollPane1 = new JScrollPane();
  248.                                         jPanel2.add(jScrollPane1, BorderLayout.SOUTH);
  249.                                         {
  250.                                                 console = new JTextPane();
  251.                                                 jScrollPane1.setViewportView(console);
  252.                                                 console.setPreferredSize(new java.awt.Dimension(469, 116));
  253.                                                 console.setEditable(false);
  254.                                         }
  255.                                 }
  256.                                 {
  257.                                         sourcecontent = new JScrollPane();
  258.                                         jPanel2.add(sourcecontent, BorderLayout.CENTER);
  259.                                         {
  260.                                                 source = new JTextPane();
  261.                                                 sourcecontent.setViewportView(source);
  262.                                                 source.setPreferredSize(new java.awt.Dimension(443, 271));
  263.                                                 source.addKeyListener(new KeyAdapter() {
  264.                                                         public void keyTyped(KeyEvent e) {
  265.                                                                 doc.edit = true;
  266.                                                         }
  267.                                                 });
  268.                                         }
  269.                                 }
  270.                         }
  271.                 } catch (Exception e) {
  272.                         e.printStackTrace();
  273.                 }
  274.         }
  275.        
  276.         public String getSource() {
  277.                 return source.getText();
  278.         }
  279.  
  280.         private void onOption() {
  281.                 LookAndFeelInfo[] uim = UIManager.getInstalledLookAndFeels();
  282.                 JavArrow.uim = new Vector();
  283.                 for(int i = 0; i < uim.length; i++) {
  284.                         JavArrow.uim.add(uim[i]);
  285.                 }
  286.                 new SetOptions(this, uim, false);
  287.         }
  288.        
  289.         public void actionPerformed(ActionEvent e) {
  290.                 String source = e.getActionCommand();
  291.                 if(source.equals("Save")) {
  292.                 try {
  293.                         doc.salva();
  294.                 } catch (IOException e1) {
  295.                         e1.printStackTrace();
  296.                 }
  297.                 } else if(source.equals("Save As")) {
  298.                         try {
  299.                                 doc.saveAs();
  300.                         } catch (IOException e1) {
  301.                                 e1.printStackTrace();
  302.                         }
  303.                 } else if(source.equals("Open")) {
  304.                         try {
  305.                                 String text = doc.open();
  306.                                 if(text != null) {
  307.                                         this.source.setText(text);
  308.                                         doc.salvato = true;
  309.                                 }
  310.                         } catch (IOException e1) {
  311.                                 e1.printStackTrace();
  312.                         }
  313.                 } else if(source.equals("Exit")) {
  314.                         doc.onExit();
  315.         } else if(source.equals("New")) {
  316.                 doc.nuovo(this.source);
  317.         }
  318.         }
  319.  
  320.         public void centerFrame() {
  321.                 Toolkit t = Toolkit.getDefaultToolkit();
  322.                 Dimension d=t.getScreenSize();
  323.                 Dimension win=getSize();
  324.                 win=getSize();
  325.                 setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
  326.         }
  327. }
  328.  
  329. class Document extends FileFilter {
  330.         static {
  331.                 System.loadLibrary("Document");
  332.         }
  333.         public boolean salvato, compilato, edit;
  334.         int j = 0;
  335.         private File file;
  336.         private BufferedOutputStream bos;
  337.         private FileOutputStream fos;
  338.         private FileInputStream fis;
  339.         private JavArrow parent;
  340.         private JFileChooser fc;
  341.         private String pathsdk;
  342.         private JTextPane console;
  343.         private JMenuItem op;
  344.         private JMenuItem about;
  345.  
  346.         public native void Run(String path);
  347.        
  348.         Document(JavArrow parent) {
  349.                 salvato = false;
  350.                 compilato = false;
  351.                 edit = false;
  352.                 this.parent = parent;
  353.         }
  354.        
  355.         public void runn(String pathsdk, JTextPane p) throws Exception {
  356.                 if(!compilato) compile(pathsdk, p);
  357.                 FileWriter fww = new FileWriter(file.toString().substring(0, file.toString().lastIndexOf('.')) + ".bat");
  358.                 BufferedWriter fw = new BufferedWriter(fww);
  359.                 fw.write(pathsdk.replace('/', '\\') + "\\java -cp " + file.getCanonicalPath().substring(0, file.getCanonicalPath().lastIndexOf('\\')) + " " + file.getName().substring(0, file.getName().lastIndexOf('.')));
  360.                 fw.close();
  361.                 Run(file.toString().substring(0, file.toString().lastIndexOf('.')) + ".bat");
  362.         }
  363.  
  364.         public void nuovo(JTextPane c) {
  365.                 boolean nuovo = false;
  366.                 if(!edit) {
  367.                         salvato = false;
  368.                         compilato = false;
  369.                         edit = true;
  370.                         c.setText("");
  371.                 } else {
  372.                 int resp = JOptionPane.showConfirmDialog(null, "E' aperto un documento non salvato, salvare ora?", "Salvataggio", JOptionPane.YES_NO_CANCEL_OPTION);
  373.                 switch(resp) {
  374.                         case JOptionPane.OK_OPTION:
  375.                                 try {
  376.                                         salva();
  377.                                         salvato = false;
  378.                                         compilato = false;
  379.                                         edit = true;
  380.                                         c.setText("");
  381.                                 } catch (IOException e1) {
  382.                                         e1.printStackTrace();
  383.                                 }
  384.                         break;
  385.                         case JOptionPane.NO_OPTION:
  386.                                 salvato = false;
  387.                                 compilato = false;
  388.                                 edit = true;
  389.                                 c.setText("");
  390.                         break;
  391.                         case JOptionPane.CANCEL_OPTION:
  392.                                 ;
  393.         }      
  394.         }
  395.         }
  396.        
  397.         public void onExit() {
  398.                 if(!edit) {
  399.                         System.exit(0);
  400.                 } else {
  401.                 int resp = JOptionPane.showConfirmDialog(null, "E' aperto un documento non salvato, salvare ora?", "Salvataggio", JOptionPane.YES_NO_CANCEL_OPTION);
  402.                 switch(resp) {
  403.                         case JOptionPane.OK_OPTION:
  404.                                 try {
  405.                                         salva();
  406.                                 } catch (IOException e1) {
  407.                                         e1.printStackTrace();
  408.                                 }
  409.                         System.exit(0);
  410.                         break;
  411.                         case JOptionPane.NO_OPTION:
  412.                                 System.exit(0);
  413.                         break;
  414.                         case JOptionPane.CANCEL_OPTION:
  415.                                 ;
  416.         }
  417.         }
  418.         }
  419.        
  420.         public void salva() throws IOException {
  421.                 if(salvato) {
  422.                         fos = new FileOutputStream(file);
  423.                         fos.write(parent.getSource().getBytes());
  424.                         fos.close();
  425.                         edit = false;
  426.                 } else {
  427.                         saveAs();
  428.                 }
  429.         }
  430.        
  431.         public void saveAs() throws IOException {
  432.                 fc = new JFileChooser();
  433.                 fc.setFileFilter(this);
  434.                 fc.setApproveButtonText("Salva");
  435.                 fc.addChoosableFileFilter(this);
  436.                 int resp = fc.showOpenDialog(parent);
  437.                 if(resp == JFileChooser.APPROVE_OPTION){
  438.                 file = new File((fc.getSelectedFile().toString().endsWith(".java") ? fc.getSelectedFile().toString().replace('\\','/'):(fc.getSelectedFile().toString()+ ".java").replace('\\', '/')));
  439.                 fos = new FileOutputStream(file);
  440.                 fos.write(parent.getSource().getBytes());
  441.                 fos.close();
  442.                 salvato = true;
  443.                 edit = false;
  444.                 }
  445.         }
  446.        
  447.         private String getStr() throws IOException {
  448.                 StringBuffer text = new StringBuffer();
  449.                 String ret = null;
  450.                 fc = new JFileChooser();
  451.                 fc.addChoosableFileFilter(this);
  452.                 fc.setFileFilter(this);
  453.                 fc.setApproveButtonText("Apri");
  454.                 int resp = fc.showOpenDialog(parent);
  455.                 if(resp == JFileChooser.APPROVE_OPTION){
  456.                         file = new File(fc.getSelectedFile().toString().replace('\\', '/'));
  457.                         fis = new FileInputStream(file);
  458.                         int i = 0;
  459.                         while((i = fis.read()) != -1) {
  460.                                 text.append((char)i);
  461.                         }
  462.                         ret = text.toString();
  463.         }
  464.                 return ret;
  465.         }
  466.        
  467.         public String open() throws IOException {
  468.                 String text = null;
  469.                 if(!edit) {
  470.                         text = getStr();
  471.                 } else {
  472.                         int resp = JOptionPane.showConfirmDialog(parent, "E' aperto un altro file non salvato, salvare ora le modifiche?", "Salvataggio", JOptionPane.YES_NO_CANCEL_OPTION);
  473.                         switch(resp) {
  474.                                 case JOptionPane.OK_OPTION:
  475.                                         salva();
  476.                                 text = getStr();
  477.                                 if(text != null) {
  478.                                 salvato = true;
  479.                                 compilato = false;
  480.                                 edit = false;
  481.                                 }
  482.                                 break;
  483.                                 case JOptionPane.NO_OPTION:
  484.                                         text = getStr();
  485.                                 if(text != null) {
  486.                                         salvato = true;
  487.                                         compilato = false;
  488.                                         edit = false;
  489.                                         }
  490.                                 break;
  491.                                 case JOptionPane.CANCEL_OPTION:
  492.                                         text = null;
  493.                         }
  494.                 }
  495.                
  496.                 return text;
  497.         }
  498.        
  499.         public void compile(String path, JTextPane console) throws IOException {
  500.                 salva();
  501.                 try {
  502.                         int i = 0;
  503.                         Process p = Runtime.getRuntime().exec(path + "/javac " + file.toString());
  504.                         BufferedInputStream is = new BufferedInputStream(p.getErrorStream());
  505.                         console.setText("");
  506.                         while((i = is.read())!= -1) {
  507.                                 console.setText(console.getText() + (char)i);
  508.                         }
  509.                         is.close();
  510.                 } catch (IOException e) {
  511.                         e.printStackTrace();
  512.                 }
  513. compilato = true;
  514.         }
  515.  
  516.         public boolean accept(File f) {
  517.                 return f.toString().toLowerCase().endsWith(".java") || f.isDirectory();
  518.         }
  519.  
  520.         public String getDescription() {
  521.                 return "Sorgente java(*.java)";
  522.         }      
  523. }