/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edytortekstu;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;
/**
*
* @author Artur
*/
public class EdytorGUI
extends javax.
swing.
JFrame { ArrayList<ArrayList<String>> page = new ArrayList<ArrayList<String>>(); //implementacja tablicowa listy przechowującej listy(linie)
ArrayList<String> line = new ArrayList<String>(); //liosta przechowująca ciagi znaków, reprezentuje pojedyncza linie
/**
* Creates new form EdytorGUI
*/
public EdytorGUI() {
initComponents();
saveItem.setText("Zapisz plik");
saveItem.
addActionListener(new ActionListener() { //listener wywoływany w momencie klikniecia na "zapisz plik" @Override
fileChooser.showSaveDialog(EdytorGUI.this);
filePath=fileChooser.getSelectedFile().getAbsolutePath();//pobranie sciezki calkowitej do pliku
saveFile(filePath);
}
});
openItem.setText("Otwórz plik");
openItem.
addActionListener(new ActionListener() {//listener wywoływany w momencie klikniecia na "otwórz plik"
@Override
fileChooser.showOpenDialog(EdytorGUI.this);
filePath=fileChooser.getSelectedFile().getAbsolutePath();
openFile(filePath);
}
});
jMenu1.add(openItem);
jMenu1.add(saveItem);
}
//metoda zapisu do pliku
private void saveFile
(String path
){ try {
for(int i=0; i<page.size(); i++){
for(int j=0; j<page.get(i).size(); j++){
bw.write(page.get(i).get(j)); //dwie petle, zewnetrzna po linijkach, wewnetrzna po znakach
}
bw.newLine(); //wstawienie nowej linii do pliku
}
bw.close();
Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
//metoda wczytania z pliku
private void openFile
(String path
){ try {
while((s = br.readLine())!=null){ //wczytuj do konca pliku po jednej linii
char[] tmpLine=s.toCharArray();
for(int i=0;i<tmpLine.length;i++){
line.
add(String.
valueOf(tmpLine
[i
])); //dodaj do listy jTextArea1.
append(String.
valueOf(tmpLine
[i
])); //dodaj do textArea }
page.
add(new ArrayList(line
)); //dodaj liste do listy(reprezentujacej strone) jTextArea1.append("\n"); // dodaj znak nowej linii do TextArea po wczytanniu linnii z pliku
line.clear();
}
br.close(); //zamkniecie pliku
Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1
= new javax.
swing.
JPanel(); jButton1
= new javax.
swing.
JButton(); jMenu1
= new javax.
swing.
JMenu();
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.
addKeyListener(new java.
awt.
event.
KeyAdapter() { public void keyTyped
(java.
awt.
event.
KeyEvent evt
) { jTextArea1KeyTyped(evt);
}
});
jScrollPane1.setViewportView(jTextArea1);
jPanel1.
setBackground(new java.
awt.
Color(102,
102,
102)); jPanel1.
setForeground(new java.
awt.
Color(102,
102,
102));
jButton1.setText("B");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.
addContainerGap(javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)) );
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.
addGap(0,
13,
Short.
MAX_VALUE)) );
jMenu1.setText("File");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jMenu1ActionPerformed(evt);
}
});
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(jScrollPane1, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
619,
Short.
MAX_VALUE) .
addComponent(jPanel1, javax.
swing.
GroupLayout.
DEFAULT_SIZE, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)) .addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.
addComponent(jScrollPane1, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
489,
Short.
MAX_VALUE) .addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
//metoda wywoływana podczas wprowadzenia znaku do textArea
private void jTextArea1KeyTyped
(java.
awt.
event.
KeyEvent evt
) {//GEN-FIRST:event_jTextArea1KeyTyped
line.
add(String.
valueOf(evt.
getKeyChar())); //dodanie pojedynczego znaku do listy, rzutowanie na String if(evt.getKeyChar()=='\n'){ //jesli znak nowej linii
page.
add(new ArrayList(line
)); // dodajemy liste zawierajaca znaki jako element listy reprezentującej strone line.clear(); // czyscimy liste(linie)
}else{
if(!page.isEmpty()){
page.
set(page.
size()-1,
new ArrayList(line
)); //aktualizowanie elementu page, przydatne dla ostatniej linijki }else{
page.
add(page.
size(),
new ArrayList(line
)); //aktualizowanie dla pierwszego znaku pierwszej linii }
}
if(evt.getKeyChar()=='\b'){ //usuwanie z listy, jesli wcisnieto klawisz backspace
int lineNumber=0; // ktora lista
int colNum=0; //pozycja w liscie
try {
int caretPos = jTextArea1.getCaretPosition();
int offset
= Utilities.
getRowStart(jTextArea1, caretPos
); colNum = caretPos - offset;
lineNumber=jTextArea1.getLineOfOffset(jTextArea1.getCaretPosition());
Logger.getLogger(EdytorGUI.class.getName()).log(Level.SEVERE, null, ex);
}
page.get(lineNumber).remove(colNum);
}
}//GEN-LAST:event_jTextArea1KeyTyped
private void jMenu1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_jMenu1ActionPerformed // TODO add your handling code here:
}//GEN-LAST:event_jMenu1ActionPerformed
private void jButton1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_jButton1ActionPerformed Font font
= jTextArea1.
getFont(); jTextArea1.
setFont(font.
deriveFont(Font.
BOLD));
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) { /* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://d...content-available-to-author-only...e.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.
swing.
UIManager.
setLookAndFeel(info.
getClassName()); break;
}
}
java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(EdytorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new EdytorGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing.
JButton jButton1
; private javax.
swing.
JMenu jMenu1
; private javax.
swing.
JPanel jPanel1
; // End of variables declaration//GEN-END:variables
}