fork download
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package edytortekstu;
  7.  
  8. import java.awt.Font;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.swing.JFileChooser;
  22. import javax.swing.JMenuItem;
  23. import javax.swing.text.BadLocationException;
  24. import javax.swing.text.Utilities;
  25.  
  26. /**
  27.  *
  28.  * @author Artur
  29.  */
  30. public class EdytorGUI extends javax.swing.JFrame {
  31. ArrayList<ArrayList<String>> page = new ArrayList<ArrayList<String>>(); //implementacja tablicowa listy przechowującej listy(linie)
  32. ArrayList<String> line = new ArrayList<String>(); //liosta przechowująca ciagi znaków, reprezentuje pojedyncza linie
  33.  
  34. /**
  35.   * Creates new form EdytorGUI
  36.   */
  37. public EdytorGUI() {
  38. initComponents();
  39. JMenuItem saveItem = new JMenuItem();
  40. saveItem.setText("Zapisz plik");
  41. saveItem.addActionListener(new ActionListener() { //listener wywoływany w momencie klikniecia na "zapisz plik"
  42. @Override
  43. public void actionPerformed(ActionEvent e) {
  44. String filePath="";
  45. JFileChooser fileChooser = new JFileChooser();//filechooser, okno za pomoca ktorego wybieramy pliki
  46. fileChooser.showSaveDialog(EdytorGUI.this);
  47. filePath=fileChooser.getSelectedFile().getAbsolutePath();//pobranie sciezki calkowitej do pliku
  48. saveFile(filePath);
  49. }
  50. });
  51. JMenuItem openItem = new JMenuItem();
  52. openItem.setText("Otwórz plik");
  53. openItem.addActionListener(new ActionListener() {//listener wywoływany w momencie klikniecia na "otwórz plik"
  54.  
  55. @Override
  56. public void actionPerformed(ActionEvent e) {
  57. String filePath="";
  58. JFileChooser fileChooser = new JFileChooser(); //filechooser, okno za pomoca ktorego wybieramy pliki
  59. fileChooser.showOpenDialog(EdytorGUI.this);
  60. filePath=fileChooser.getSelectedFile().getAbsolutePath();
  61. openFile(filePath);
  62. }
  63. });
  64. jMenu1.add(openItem);
  65. jMenu1.add(saveItem);
  66. }
  67. //metoda zapisu do pliku
  68. private void saveFile(String path){
  69. File f = new File(path);
  70. try {
  71. FileWriter fw = new FileWriter(f);
  72. for(int i=0; i<page.size(); i++){
  73. for(int j=0; j<page.get(i).size(); j++){
  74. bw.write(page.get(i).get(j)); //dwie petle, zewnetrzna po linijkach, wewnetrzna po znakach
  75. }
  76. bw.newLine(); //wstawienie nowej linii do pliku
  77. }
  78. bw.close();
  79. } catch (IOException ex) {
  80. Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
  81. }
  82. }
  83. //metoda wczytania z pliku
  84. private void openFile(String path){
  85. File f = new File(path);
  86. try {
  87. FileReader fr = new FileReader(f);
  88. String s;
  89. while((s = br.readLine())!=null){ //wczytuj do konca pliku po jednej linii
  90. char[] tmpLine=s.toCharArray();
  91. for(int i=0;i<tmpLine.length;i++){
  92. line.add(String.valueOf(tmpLine[i])); //dodaj do listy
  93. jTextArea1.append(String.valueOf(tmpLine[i])); //dodaj do textArea
  94. }
  95. page.add(new ArrayList(line)); //dodaj liste do listy(reprezentujacej strone)
  96. jTextArea1.append("\n"); // dodaj znak nowej linii do TextArea po wczytanniu linnii z pliku
  97. line.clear();
  98. }
  99. br.close(); //zamkniecie pliku
  100. } catch (FileNotFoundException ex) {
  101. Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
  102. } catch (IOException ex) {
  103. Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
  104. }
  105. }
  106. /**
  107.   * This method is called from within the constructor to initialize the form.
  108.   * WARNING: Do NOT modify this code. The content of this method is always
  109.   * regenerated by the Form Editor.
  110.   */
  111. @SuppressWarnings("unchecked")
  112. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  113. private void initComponents() {
  114.  
  115. jScrollPane1 = new javax.swing.JScrollPane();
  116. jTextArea1 = new javax.swing.JTextArea();
  117. jPanel1 = new javax.swing.JPanel();
  118. jButton1 = new javax.swing.JButton();
  119. jMenuBar1 = new javax.swing.JMenuBar();
  120. jMenu1 = new javax.swing.JMenu();
  121.  
  122. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  123.  
  124. jTextArea1.setColumns(20);
  125. jTextArea1.setRows(5);
  126. jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
  127. public void keyTyped(java.awt.event.KeyEvent evt) {
  128. jTextArea1KeyTyped(evt);
  129. }
  130. });
  131. jScrollPane1.setViewportView(jTextArea1);
  132.  
  133. jPanel1.setBackground(new java.awt.Color(102, 102, 102));
  134. jPanel1.setForeground(new java.awt.Color(102, 102, 102));
  135.  
  136. jButton1.setText("B");
  137. jButton1.addActionListener(new java.awt.event.ActionListener() {
  138. public void actionPerformed(java.awt.event.ActionEvent evt) {
  139. jButton1ActionPerformed(evt);
  140. }
  141. });
  142.  
  143. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  144. jPanel1.setLayout(jPanel1Layout);
  145. jPanel1Layout.setHorizontalGroup(
  146. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147. .addGroup(jPanel1Layout.createSequentialGroup()
  148. .addContainerGap()
  149. .addComponent(jButton1)
  150. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  151. );
  152. jPanel1Layout.setVerticalGroup(
  153. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  154. .addGroup(jPanel1Layout.createSequentialGroup()
  155. .addComponent(jButton1)
  156. .addGap(0, 13, Short.MAX_VALUE))
  157. );
  158.  
  159. jMenu1.setText("File");
  160. jMenu1.addActionListener(new java.awt.event.ActionListener() {
  161. public void actionPerformed(java.awt.event.ActionEvent evt) {
  162. jMenu1ActionPerformed(evt);
  163. }
  164. });
  165. jMenuBar1.add(jMenu1);
  166.  
  167. setJMenuBar(jMenuBar1);
  168.  
  169. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  170. getContentPane().setLayout(layout);
  171. layout.setHorizontalGroup(
  172. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173. .addGroup(layout.createSequentialGroup()
  174. .addContainerGap()
  175. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
  177. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  178. .addContainerGap())
  179. );
  180. layout.setVerticalGroup(
  181. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  182. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  183. .addContainerGap()
  184. .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  185. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  186. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
  187. .addContainerGap())
  188. );
  189.  
  190. pack();
  191. }// </editor-fold>//GEN-END:initComponents
  192.  
  193. //metoda wywoływana podczas wprowadzenia znaku do textArea
  194. private void jTextArea1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyTyped
  195.  
  196. line.add(String.valueOf(evt.getKeyChar())); //dodanie pojedynczego znaku do listy, rzutowanie na String
  197. if(evt.getKeyChar()=='\n'){ //jesli znak nowej linii
  198. page.add(new ArrayList(line)); // dodajemy liste zawierajaca znaki jako element listy reprezentującej strone
  199. line.clear(); // czyscimy liste(linie)
  200. }else{
  201. if(!page.isEmpty()){
  202. page.set(page.size()-1, new ArrayList(line)); //aktualizowanie elementu page, przydatne dla ostatniej linijki
  203. }else{
  204. page.add(page.size(), new ArrayList(line)); //aktualizowanie dla pierwszego znaku pierwszej linii
  205. }
  206. }
  207. if(evt.getKeyChar()=='\b'){ //usuwanie z listy, jesli wcisnieto klawisz backspace
  208. int lineNumber=0; // ktora lista
  209. int colNum=0; //pozycja w liscie
  210. try {
  211. int caretPos = jTextArea1.getCaretPosition();
  212. int offset = Utilities.getRowStart(jTextArea1, caretPos);
  213. colNum = caretPos - offset;
  214. lineNumber=jTextArea1.getLineOfOffset(jTextArea1.getCaretPosition());
  215. } catch (BadLocationException ex) {
  216. Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
  217. }
  218. page.get(lineNumber).remove(colNum);
  219. }
  220. }//GEN-LAST:event_jTextArea1KeyTyped
  221.  
  222. private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
  223. // TODO add your handling code here:
  224. }//GEN-LAST:event_jMenu1ActionPerformed
  225.  
  226. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  227. Font font = jTextArea1.getFont();
  228. jTextArea1.setFont(font.deriveFont(Font.BOLD));
  229.  
  230. }//GEN-LAST:event_jButton1ActionPerformed
  231.  
  232. /**
  233.   * @param args the command line arguments
  234.   */
  235. public static void main(String args[]) {
  236. /* Set the Nimbus look and feel */
  237. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  238. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  239.   * For details see http://d...content-available-to-author-only...e.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  240.   */
  241. try {
  242. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  243. if ("Nimbus".equals(info.getName())) {
  244. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  245. break;
  246. }
  247. }
  248. } catch (ClassNotFoundException ex) {
  249. java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  250. } catch (InstantiationException ex) {
  251. java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  252. } catch (IllegalAccessException ex) {
  253. java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  254. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  255. java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  256. }
  257. //</editor-fold>
  258.  
  259. /* Create and display the form */
  260. java.awt.EventQueue.invokeLater(new Runnable() {
  261. public void run() {
  262. new EdytorGUI().setVisible(true);
  263. }
  264. });
  265. }
  266.  
  267. // Variables declaration - do not modify//GEN-BEGIN:variables
  268. private javax.swing.JButton jButton1;
  269. private javax.swing.JMenu jMenu1;
  270. private javax.swing.JMenuBar jMenuBar1;
  271. private javax.swing.JPanel jPanel1;
  272. private javax.swing.JScrollPane jScrollPane1;
  273. private javax.swing.JTextArea jTextArea1;
  274. // End of variables declaration//GEN-END:variables
  275. }
  276.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:30: error: class EdytorGUI is public, should be declared in a file named EdytorGUI.java
public class EdytorGUI extends javax.swing.JFrame {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty