Pessoal to fazendo uma especie de "script" para instalaçao de alguns modulos da RM aqui onde eu trabalho, porém os programas da RM esta na rede, entao tenho um método que copia os programas pra máquina pra depois fazer a instalação. Tenho um thread que executa essa operação de copiar os arquivos da rede, e tenho outro thread que faz o processo de instalação. Só que quando o método copyDirectory(...) esta executando eu gostaria que exibisse no meu JList uma frase "Copiando Arquivos...", só que nao quer aparecer no meu JList.
Segue o Codigo:
Meu GUI:
package com.instal.gui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import com.instal.threads.ThreadExec;
import com.instal.threads.ThreadProcesso;
public class GUI extends JFrame{
public JList jList;
public JScrollPane scroll;
public JTextField text;
public JCheckBox box;
JButton button;
public GUI gui;
public DefaultListModel model;
public static void main(String[] args) {
GUI view = new GUI();
view.setVisible(true);
view.gui = view;
}
public GUI(){
super("Install RM");
this.inicializaComponents();
this.setSize(new Dimension(800,500));
this.setLocationRelativeTo(null);
}
public void inicializaComponents(){
JPanel panel = this.getPanel();
MigLayout layout = this.getLayoutManager("","[grow]", "");
panel.setLayout(layout);
this.add(panel);
JLabel label = this.getLabel("Em execução:");
panel.add(label, "split");
this.text = this.getTextField();
panel.add(text, "growx, wrap");
this.jList = this.getJList();
this.model = new DefaultListModel();
this.jList.setModel(model);
this.scroll = this.getScrol();
this.scroll.setViewportView(jList);
panel.add(scroll, "dock center, wrap");
this.box = this.getCheckBox();
panel.add(box, "split");
JLabel labelDesligarAoTerminar = this.getLabel("Desligar ao Terminar?");
panel.add(labelDesligarAoTerminar, "split");
this.button = this.getButton("Iniciar Instalação");
panel.add(button, "gap left 180, w :10, h :10");
}
public JLabel getLabel(String nome){
JLabel label = new JLabel(nome);
return label;
}
public JList getJList(){
JList jList = new JList();
jList.setVisible(true);
return jList;
}
public JTextField getTextField(){
JTextField textField = new JTextField();
return textField;
}
public JPanel getPanel(){
JPanel panel = new JPanel();
return panel;
}
public MigLayout getLayoutManager(String parametro, String linha, String coluna){
MigLayout mig = new MigLayout(parametro, linha, coluna);
return mig;
}
public MigLayout getLayoutManager(){
return new MigLayout();
}
public JButton getButton(String nome){
JButton button = new JButton(nome);
button.setVisible(true);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
Thread thread = new Thread(new ThreadExec(GUI.this.gui));
thread.start(); // starta o thread de instalaçao.
ThreadProcesso threadCopy = new ThreadProcesso(gui);
Thread copy = new Thread(threadCopy);
copy.start(); // starta o de copia.
GUI.this.gui.button.setEnabled(false);
}
});
return button;
}
public JScrollPane getScrol(){
JScrollPane scroll = new JScrollPane();
return scroll;
}
public JCheckBox getCheckBox(){
JCheckBox box = new JCheckBox();
box.setSelected(true);
return box;
}
}
Meu Thread que copia os arquivos:
package com.instal.threads;
import java.io.File;
import java.io.IOException;
import com.instal.gui.GUI;
import com.instal.ler.diretorio.CopiaArquivo;
public class ThreadProcesso implements Runnable {
GUI gui;
public ThreadProcesso(GUI gui){
this.gui = gui;
}
@Override
public void run() {
synchronized (this.gui) {
File source = new File("\\10.0.16.5\nas\Prog_Inst\RM_Sistemas\RM_10.80\CurrentInstall");
File dest = new File("C:\Temp\CurrentInstall");
CopiaArquivo copy = new CopiaArquivo(this.gui);
try {
copy.copyDirectory(source, dest);
this.gui.notifyAll();
System.out.println("notificado");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Meu thread que instala os arquivos:
package com.instal.threads;
import java.io.IOException;
import javax.swing.DefaultListModel;
import com.instal.executar.ExecInstall;
import com.instal.gui.GUI;
public class ThreadExec implements Runnable{
GUI gui;
public ThreadExec(GUI gui){
this.gui = gui;
}
@Override
public void run(){
synchronized (this.gui) {
try {
System.out.println("waiting...");
this.gui.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ExecInstall exec = new ExecInstall(gui);
try {
exec.executar();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.instal.ler.diretorio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.DefaultListModel;
import com.instal.executar.ExecInstall;
import com.instal.gui.GUI;
public class CopiaArquivo {
GUI gui;
public CopiaArquivo(GUI gui){
this.gui = gui;
}
public CopiaArquivo(){
}
public void copyDirectory(File srcPath, File dstPath)throws IOException{
synchronized (this.gui) {
this.gui.model.addElement("Copiando Arquivo..."); // nao quer inserir no meu JLIst, a cada chamada recursiva aqui deveria preencher e uma linha do meu JList a frase "Copiando Arquivo...".
this.gui.text.setText(dstPath.toString()); // nao quer inserir no meu TextField, e aqui o arquivo corrente copiado.
System.out.println("Copiando Arquivos..."); // ja aqui exibi toda hora no console corretamente.
if (srcPath.isDirectory()){
if (!dstPath.exists()){
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++){
System.out.println(i);
copyDirectory(new File(srcPath, files[i]),new File(dstPath, files[i]));
}
}else{
if(!srcPath.exists()){
}else{
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(dstPath);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
}
}
package com.instal.executar;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultListModel;
import com.instal.gui.GUI;
import com.instal.ler.diretorio.LerArquivo;
public class ExecInstall {
GUI gui;
public ExecInstall(){
}
public ExecInstall(GUI gui){
this.gui = gui;
}
public void executar() throws IOException, InterruptedException{
System.out.println("executando...depois do wait");
this.gui.model.addElement("************************Instalando RM************************"); // Não preencher no meu JList
LerArquivo lerArquivo = new LerArquivo();
List<String>listFile = lerArquivo.ordenaListaArquivo();
Process process = null;
Runtime runtime=Runtime.getRuntime();
for(int i = 0; i < listFile.size(); i++){
process = runtime.exec(listFile.get(i));
String programaEmProcesso = listFile.get(i).split("\\")[listFile.get(i).split("\\").length -1];
System.out.println(programaEmProcesso);
if(i != listFile.size() -1){
this.gui.text.setText(programaEmProcesso);
}
this.gui.model.addElement(programaEmProcesso); // Não preencher no meu JList
int verifica = 1;
while(true){
verifica = process.waitFor();
if(verifica == 0){
break;
}else{
break;
}
}
}
this.gui.model.addElement("\n\n******************************* Finalizado com Sucesso ******************************* BJOS ME LIGA!!!");
if(this.gui.box.isSelected()){
runtime.exec("cmd /k shutdown -s -f -c InstallTOTVs");
}
}
}
Alguém me da um help?