Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- directorio pkgdir/CargarClase.java
- package pkgdir.graficos;
- import javax.swing.*;
- import java.awt.Color;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.awt.Font;
- import java.awt.Dimension;
- import javax.imageio.ImageIO;
- import java.net.URL;
- /**
- * Hereda de la clase JFrame
- **/
- public class GuiUser extends JFrame{
- private JPanel panel;
- private JButton butWrite;
- private JButton butRead;
- private JButton butDel;
- private JLabel labWrite;
- private JTextField texfWrite;
- private JTextArea areaRead;
- private JScrollPane scrollRead;
- private String namel;
- private Long docl;
- private int edadl;
- /**
- *Metodo constructor de la clase
- */
- public GuiUser() {
- }
- /**
- *Metodo que pinta el JFrame
- *Retorna vacio
- */
- public void showWin(String name, Long documento, int edad){
- try{
- this.namel = name;
- this.docl = documento;
- this.edadl = edad;
- System.out.println("Pintando ventana namel= "+namel+" docl= "+docl+" edadl= "+edadl);
- this.setSize(550, 350);
- this.setTitle("HV: "+namel+" doc: "+docl+" edad: "+edadl);
- this.setLocationRelativeTo(null);
- this.setResizable(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.getContentPane().add(drawPanel());
- this.setVisible(true);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- /**
- *Metodo que pinta el JButton butWrite
- *Retorna JButton
- */
- private JButton drawButtonWrite(){
- try{
- URL url = GuiUser.class.getResource("../../res/write_1.png");
- BufferedImage img = ImageIO.read(url);
- butWrite = new JButton("Escribir",new ImageIcon(img));
- butWrite.setBackground(Color.WHITE);
- butWrite.setFont(butWrite.getFont().deriveFont(Font.BOLD | Font.ITALIC));
- butWrite.setAlignmentX(panel.CENTER_ALIGNMENT);
- butWrite.setFocusPainted(true);
- }catch(Exception e){
- e.printStackTrace();
- }
- return butWrite;
- }
- /**
- *Metodo que pinta el JButton butRead
- *Retorna JButton
- */
- private JButton drawButtonRead(){
- try{
- URL url = GuiUser.class.getResource("../../res/read_1.png");
- BufferedImage img = ImageIO.read(url);
- butRead = new JButton("Leer",new ImageIcon(img));
- butRead.setBackground(Color.WHITE);
- butRead.setFont(butWrite.getFont().deriveFont(Font.BOLD | Font.ITALIC));
- butRead.setAlignmentX(panel.CENTER_ALIGNMENT);
- butRead.setFocusPainted(true);
- }catch(Exception e){
- e.printStackTrace();
- }
- return butRead;
- }
- /**
- *Metodo que pinta el JButton butDel
- *Retorna JButton
- */
- private JButton drawButtonDel(){
- try{
- URL url = GuiUser.class.getResource("../../res/read_1.png");
- BufferedImage img = ImageIO.read(url);
- butDel = new JButton("Borrar",new ImageIcon(img));
- butDel.setBackground(Color.WHITE);
- butDel.setFont(butDel.getFont().deriveFont(Font.BOLD | Font.ITALIC));
- butDel.setAlignmentX(panel.CENTER_ALIGNMENT);
- butDel.setFocusPainted(true);
- }catch(Exception e){
- e.printStackTrace();
- }
- return butDel;
- }
- /**
- *Metodo que pinta el JTextField texffWrite Jlabel dentro de un JPanel
- *Retorna JTextField
- */
- private JPanel drawTextfWrite(){
- JPanel jtmp = new JPanel();
- try{
- jtmp.setLayout(new BoxLayout(jtmp,BoxLayout.X_AXIS));
- labWrite = new JLabel("Texto: ");
- texfWrite = new JTextField();
- labWrite.setLabelFor(texfWrite);
- texfWrite.setFont(new Font("Serif", Font.PLAIN, 16));
- texfWrite.setPreferredSize(new Dimension(350, 30));
- texfWrite.setMaximumSize(new Dimension(350, 30));
- jtmp.add(labWrite);
- jtmp.add(texfWrite);
- }catch(Exception e){
- e.printStackTrace();
- }
- return jtmp;
- }
- /**
- *Metodo que pinta el JTextArea dentro de un JScrollPane
- *Retorna JScrollPane
- */
- private JScrollPane drawAreaRead(){
- try{
- areaRead = new JTextArea();
- areaRead.setEditable(false);
- scrollRead = new JScrollPane(areaRead);
- areaRead.setFont(new Font("Serif", Font.PLAIN, 16));
- scrollRead.setPreferredSize(new Dimension(350, 120));
- scrollRead.setMaximumSize(new Dimension(350, 120));
- }catch(Exception e){
- e.printStackTrace();
- }
- return scrollRead;
- }
- /**
- *Metodo que pinta el JPanel
- *Retorna JPanel
- */
- private JPanel drawPanel(){
- try{
- panel = new JPanel();
- panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
- panel.add(Box.createVerticalStrut(10));
- panel.setBackground(Color.CYAN);
- panel.add(drawTextfWrite());
- panel.add(Box.createVerticalStrut(5));
- panel.add(drawButtonWrite());
- panel.add(Box.createVerticalStrut(5));
- panel.add(drawButtonRead());
- panel.add(Box.createVerticalStrut(5));
- panel.add(drawAreaRead());
- panel.add(Box.createVerticalStrut(5));
- panel.add(drawButtonDel());
- }catch(Exception e){
- e.printStackTrace();
- }
- return panel;
- }
- /**
- * Devuelve el boton escribir
- * @return
- */
- public JButton getBotonWrite() {
- return butWrite;
- }
- /**
- * Devuelve el boton leer
- * @return
- */
- public JButton getBotonRead() {
- return butRead;
- }
- /**
- * Devuelve el boton borrar
- * @return
- */
- public JButton getBotonDel() {
- return butDel;
- }
- /**
- * Devuelve el textarea read
- * @return
- */
- public JTextArea gettextAreaRead() {
- return areaRead;
- }
- /**
- * Devuelve el texField write
- * @return
- */
- public JTextField getTextField() {
- return texfWrite;
- }
- }
- directorio pkgdir/control/Controller.java
- package pkgdir.control;
- import pkgdir.modelo.FileServices;
- import pkgdir.graficos.GuiUser;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.event.CaretEvent;
- import javax.swing.event.CaretListener;
- public class Controller implements ActionListener{
- private GuiUser guiUserl;
- private FileServices fileServices;
- private CaretListener listener;
- private String stmpg;
- /**
- * Constructor sin parametros
- * @see empty
- */
- public Controller() {
- super();
- }
- /**
- * Constructor GuiUser como parametros
- * @param GuiUser
- */
- public Controller(GuiUser guiUser) {
- super();
- this.guiUserl = guiUser;
- agregarEventos();
- }
- /**
- * Metodo que administra los eventos sobre los botones
- * pertenece a la clase ActionListener
- * @param ae
- */
- @Override
- public void actionPerformed(ActionEvent ae) {
- if( ae.getSource() == guiUserl.getBotonWrite()){
- fileServices = new FileServices();
- String stmp = guiUserl.getTextField().getText();
- fileServices.writeFile( stmp, "historial.txt" );
- guiUserl.getTextField().setText("");
- }
- if( ae.getSource() == guiUserl.getBotonRead()){
- fileServices = new FileServices();
- String stmp = fileServices.readFile( "historial.txt" );
- guiUserl.gettextAreaRead().setText(stmp);
- }
- if( ae.getSource() == guiUserl.getBotonDel()){
- fileServices = new FileServices();
- fileServices.delText( "historial.txt", stmpg);
- }
- }
- /**
- * Metodo que agrega eventos sobre los componentes de GuiUser
- */
- private void agregarEventos(){
- guiUserl.getBotonWrite().addActionListener(this);
- guiUserl.getBotonRead().addActionListener(this);
- guiUserl.getBotonDel().addActionListener(this);
- listener = new CaretListener() {
- public void caretUpdate(CaretEvent caretEvent) {
- stmpg = "";
- int posM = caretEvent.getMark();
- int posD = caretEvent.getDot();
- if( posD > posM){
- stmpg = (guiUserl.gettextAreaRead().getText()).substring( posM, posD );
- }else{
- stmpg = (guiUserl.gettextAreaRead().getText()).substring( posD, posM );
- }
- System.out.println("stmpg: "+stmpg);
- }
- };
- guiUserl.gettextAreaRead().addCaretListener(listener);
- }
- }
- directorio pkgdir/modelo/FileServices.java
- package pkgdir.modelo;
- import java.io.FileWriter;
- import java.io.File;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class FileServices{
- private File textfile;
- private FileWriter writer;
- private Scanner txtReader;
- /**
- * Constructor sin parametros
- * @see empty
- */
- public FileServices(){
- super();
- }
- /**
- * Metodo que escribe texto en archivo plano
- * @param String mess
- * @param String fileName
- */
- public void writeFile(String mess, String fileName){
- try {
- textfile = new File(fileName);
- writer = new FileWriter(textfile, true);
- if (textfile.length() == 0){
- writer.append(mess);
- } else {
- writer.append("\n"+mess);
- }
- writer.flush();
- writer.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /**
- * Metodo que lee texto desde archivo plano
- * @param String fileName
- */
- public String readFile(String fileName) {
- String data = new String("");
- try {
- textfile = new File(fileName);
- if(textfile.exists() && !textfile.isDirectory()) {
- txtReader = new Scanner(textfile);
- while (txtReader.hasNextLine()) {
- data += txtReader.nextLine()+"\n";
- }
- txtReader.close();
- }else{
- data = "No se encontro archivo: "+textfile.getName();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return data;
- }
- /**
- * Metodo que borra texto en archivo plano
- * @param String fileName
- */
- public void delText(String fileName, String dataModel) {
- try {
- System.out.println("dataModel: "+dataModel);
- textfile = new File(fileName);
- //tmpPath guarda nombre para nuevo archivo temporal
- String tmpPath = (fileName.substring( 0, fileName.indexOf( "." ))) + ".tmp";
- System.out.println("tmpPath: "+tmpPath);
- //Se crea elobjeto File para el archivo temporal
- File tmpfile = new File( tmpPath );
- if(textfile.exists() && !textfile.isDirectory()) {
- //Lee sobre archivo original
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- //Escribe sobre archivo temporal
- PrintWriter pw = new PrintWriter(new FileWriter(tmpfile));
- String line = null;
- while ((line = br.readLine()) != null) {
- if ( !dataModel.trim().contains(line)) {
- System.out.println("line: "+line.trim());
- pw.println(line.trim());
- pw.flush();
- }
- }
- pw.close();
- br.close();
- if (tmpfile.renameTo( new File(fileName) )) {
- System.out.println("archivo renombrado");
- } else {
- System.out.println("error");
- }
- }else{
- System.out.println( "No se encontro archivo: "+textfile.getName() );
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- directorio pkgdir/java_build.sh
- #!/bin/bash
- # -*- ENCODING: UTF-8 -*-
- echo "Compilando paquetes"
- javac CargarClase.java graficos/GuiUser.java control/Controller.java modelo/FileServices.java -d ./dist/
- echo "Generando JAR"
- cd dist/
- jar -cmf META-INF/MANIFEST.MF java_clases.jar pkgdir/CargarClase.class pkgdir res/*.png
- echo "Ejecutando aplicacion"
- java -jar java_clases.jar
- cd ..
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement