Bonsoir,

Pour ex�cuter une tache qui peut prendre du temps, j'ai ouie dire qu'il fallait utiliser un thread qui utilisait EDT, ou une id�e dans le genre.


Donc ni une, ni deux, c'est ce que j'ai fait avec plus ou moins de mal.

J'ai r�utilis� la d�mo 2 de Sun pour faire mon action (une sorte de compilation).

Le probl�me est qu'avec une petit code, tout ce passe bien, et avec un code un peu plus gros, mais toujours petit, mon IHM principale reste bloqu�.

Je sais que mon programme principal reprend sa route normalement, et puis la paf past�que.

Je soup�onne mon thread de faire une entoure les poules !!!

Si quelqu'un voit d'o� cela pourrait venir, je l'en remercie fortement.


Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package modules.editeurTexte;
 
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
 
import modules.VisionneurGantt.Composant.DiagrammeDeGantt;
import modules.editeurTexte.langage.ParseException;
import modules.editeurTexte.langage.ParserLanguage;
 
public class EditeurTexteCompilateur extends JDialog 
									 implements ActionListener, 
											    PropertyChangeListener {
 
	/**
         * 
         */
	private static final long serialVersionUID = -2932646065554647108L;
 
	private JProgressBar barreProgression = null;
 
	private JTextArea zoneTextCompilation = null;
 
	private JButton boutton = null;
 
	private File inFile = null;
 
    private CompileTask tache;
 
    // Resultat compilation
    private DiagrammeDeGantt resultat = null;
    private String texteErreur = "";
    private String texteCompilation = "";
 
 
	public EditeurTexteCompilateur(File inFile) {
		super();
		setModal(true);
		setPreferredSize(new Dimension(400,200));
 
		JPanel tmp = new JPanel(new BorderLayout());
		tmp.setLayout(new BorderLayout());
 
        boutton = new JButton("Start");
        boutton.setActionCommand("start");
        boutton.addActionListener(this);
 
        barreProgression = new JProgressBar(0, 100);
        barreProgression.setValue(0);
        barreProgression.setStringPainted(true); 
 
        zoneTextCompilation = new JTextArea(10, 50);
        zoneTextCompilation.setMargin(new Insets(5,5,5,5));
        zoneTextCompilation.setEditable(false);
 
        JPanel panel = new JPanel();
        panel.add(boutton);
        panel.add(barreProgression);
 
        tmp.add(panel, BorderLayout.NORTH);
        tmp.add(new JScrollPane(zoneTextCompilation), BorderLayout.CENTER);
        tmp.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
 
        add(tmp);
        pack();
        setLocationRelativeTo(null);
 
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.out.println("fermeture");
            }
        });
 
        this.inFile=inFile;
	}
 
	public DiagrammeDeGantt getResultat(){
		return resultat;
	}
 
	public String getErreur(){
		return texteErreur;
	}
 
	public String getTextCompilation(){
		return texteCompilation;
	}
 
    /**
     * Invoked when the user presses the start button.
     */
    public void actionPerformed(ActionEvent evt) {
        barreProgression.setIndeterminate(true);
        boutton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
 
        tache = new CompileTask();
        tache.addPropertyChangeListener(this);
        tache.execute();
    }
 
    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = ((Integer) evt.getNewValue()).intValue();
            barreProgression.setIndeterminate(false);
            barreProgression.setValue(progress);
            zoneTextCompilation.append(String.format("Completed %d%% of task.\n", new Integer[]{new Integer(progress)}));
        } 
    }
 
 
 
 
 
 
 
 
 
	class CompileTask extends SwingWorker {
        /*
         * Main task. Executed in background thread.
         */
 
        public Object doInBackground() {
 
        	System.out.println("debut compilation");
        	zoneTextCompilation.append("****************** START COMPILATION **********************\n");
 
        	FileReader fis = null;
        	LineNumberReader lnr = null;
        	InputStream in = null;
 
    		try {
    			fis = new FileReader(inFile);            
    		    lnr = new LineNumberReader(fis);
 
    			int longueur = 0;
    			while(lnr.readLine()!=null) longueur++;
    			lnr.close();
 
 
    			in = new FileInputStream(inFile);
 
        		ParserLanguage parser = new ParserLanguage(in);
    			parser.setCompilationReussie(true);
 
    			long res = 1;
 
    			while (res > 0) {
    				try {
    					try {
    						res = parser.programme();
    					} catch (ParseException e) {
    						parser.setCompilationReussie(false);
    						res = -1;
    					}
    				} catch (Exception e) {
    					parser.setCompilationReussie(false);
    					e.printStackTrace();
    					res = -1;
    				}
 
    				long fait = parser.indice();
    				int valeur = (int) (100 * fait / longueur);
                    //Transmet la nouvelle progression.
                    setProgress((int) valeur);
    				barreProgression.setString("Progession: " + valeur + "% ("
    					+ fait + "/" + longueur + ")");
    			}
 
    			in.close();
    			fis.close();
 
    			resultat = parser.getDiagramme();
    			texteCompilation = parser.getTxtCompilation();
    			texteErreur = parser.getTxtErreur();
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
 
    		zoneTextCompilation.append("******************* STOP COMPILATION **********************\n");
    		System.out.println("fin compilation");
 
            return null;
        }
 
        /*
         * Executed in event dispatching thread
         */
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            boutton.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            zoneTextCompilation.append("Done!\n");
        }
    }
}

m�thode d'utilisation :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
	public void resoudre(){
		m_textErreur.setText("Compilation en cours ...");
 
		String l_txtRes = "\n\nResultat : \n";
		String l_txtErr = "\n\nErreur rencontrée : \n";
 
		EditeurTexteCompilateur compiler = new EditeurTexteCompilateur(m_fichier);
		System.out.println("init fenetre passé");
		compiler.setVisible(true);
 
		System.out.println("debut ecriture fichier");
 
		l_txtErr = l_txtErr + compiler.getErreur() + "\n";
		l_txtRes = l_txtRes + compiler.getTextCompilation() + "\n";
 
		DiagrammeDeGantt res = compiler.getResultat();
		compiler.dispose();
 
		if(res != null)
		{
			System.out.println(res.toXML());
	        Iterator l_it = res.getListeVariable().iterator();
			while(l_it.hasNext()) l_txtRes = l_txtRes + ">" + ((Variable)l_it.next()) + "\n";
 
			if(getFichier().isFile())
			{
				String l_nom = m_fichier.getName();
				String l_chemin = m_fichier.getAbsolutePath().replace(l_nom, "");
 
				if(l_nom.contains(".")) l_nom = l_nom.substring(0, l_nom.lastIndexOf("."));
 
				try {
					FileWriter l_out = new FileWriter(l_chemin + l_nom + "." + VisionneurGantt.s_ext);
					l_out.write(res.toXML());
					l_out.close();
					setErrone(false);
				} catch (IOException e) {
					e.printStackTrace();
					l_txtErr = l_txtErr + "Problème de création du fichier :" + (l_nom + "." + VisionneurGantt.s_ext) + " .";
					setErrone(true);
				}
			}
		}
		else setErrone(true);
 
		m_textErreur.setText(l_txtErr + "\n==================================================================" + l_txtRes);
		reactualiserEtiquette();
		System.out.println("fin ecriture fichier");
	}
resultat lors d'un blocage :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
init fenetre pass�
debut compilation
fin compilation
fermeture
debut ecriture fichier
<diagramme nom="null">
<Variable nom="Toujours" couleur="java.awt.Color[r=0,g=0,b=0]">
<ListeIntervalle>
<Intervalle borneInf="1183240800000" borneSup="1183413599999"/>
</ListeIntervalle></Variable>
<Variable nom="Demande" couleur="java.awt.Color[r=0,g=0,b=0]">
<Tableau >
<Moment moment="1183348842210" />

<Moment moment="1183247237273" />

</Tableau>
</Variable>
<Variable nom="New_Heure" couleur="java.awt.Color[r=0,g=0,b=0]">
<ListeIntervalle>
<Intervalle borneInf="1183244399999" borneSup="1183244399999"/>
... 2959 lignes ...
<Intervalle borneInf="1183247237273" borneSup="1183390199999"/>
</ListeIntervalle>
</Tableau>

</Tableau>

</Tableau>

</Tableau>

</Tableau>
</Variable>
</diagramme>

fin ecriture fichier
merci