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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class EDIGEO2SHP extends JFrame implements ActionListener,PropertyChangeListener {
private JButton parcourir;
private JButton selectionner;
private JButton convertir;
private JTextField textParcour;
private JTextField textSelection;
private JProgressBar progressbar;
private boolean traitement;
private int i;
private String ligne;
private Task task;
/**
*
*/
private static final long serialVersionUID = 1L;
public EDIGEO2SHP(){
super();
init();
}
class Task extends SwingWorker<Void, Void> implements Runnable {
protected Void doInBackground() throws Exception {
setProgress(0);
while(traitement){
setProgress(i);
}
return null;
}
}
private void init(){
setTitle("EDIGEO2SHP"); //On donne un titre à l'application
setSize(700,300); //On donne une taille à notre fenêtre
setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
setResizable(false); //On interdit la redimensionnement de la fenêtre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
setContentPane(setContentPanel());
}
private JPanel setContentPanel(){
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.setLayout(new BorderLayout(1,4));
/* Boutons */
setBouton(panel);
/* retour du pannel */
return panel;
}
private void setBouton(JPanel panel){
/* PARCOURIR
* Répertoire source à traiter
* */
JPanel parcour = new JPanel();
parcour.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
parcour.setLayout(new BorderLayout(1,2));
//Titre
JPanel sousParcour = new JPanel();
sousParcour.add((new JLabel("Répertoire EDIGEO")));
parcour.add(sousParcour,BorderLayout.WEST);
//
JPanel sousParcour2 = new JPanel();
sousParcour2.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
sousParcour2.setLayout(new FlowLayout());
//Texte de saisi
textParcour = new JTextField();
textParcour.setColumns(40);
sousParcour2.add(textParcour);
//Parcourir
this.parcourir = new JButton("Parcourir");
parcourir.addActionListener(this);
sousParcour2.add(parcourir);
parcour.add(sousParcour2,BorderLayout.SOUTH);
panel.add(parcour,BorderLayout.NORTH);
/* SELECTIONNER
* Selection du fichier en sortie du traitement
* */
JPanel selection = new JPanel();
selection.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
selection.setLayout(new BorderLayout(1,2));
//Titre
JPanel sousSelection = new JPanel();
sousSelection.add((new JLabel("Répertoire du fichier zip produit")));
selection.add(sousSelection,BorderLayout.WEST);
//
JPanel sousSelection2 = new JPanel();
sousSelection2.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
sousSelection2.setLayout(new FlowLayout());
//Texte de saisi
this.textSelection = new JTextField();
textSelection.setColumns(40);
sousSelection2.add(textSelection);
//Parcourir
this.selectionner = new JButton("Sélectionner");
selectionner.addActionListener(this);
sousSelection2.add(selectionner);
selection.add(sousSelection2,BorderLayout.SOUTH);
panel.add(selection,BorderLayout.CENTER);
/* CONVERTIR
* Action qui va lancer le traitement
* */
JPanel conversion = new JPanel();
conversion.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
this.convertir = new JButton("Convertir");
convertir.addActionListener(this);
conversion.add(convertir);
/* Barre de progression */
progressbar = new JProgressBar(SwingConstants.HORIZONTAL);
progressbar.setMinimum(1);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setSize(300,20);
progressbar.setStringPainted(true);
conversion.add(progressbar);
panel.add(conversion,BorderLayout.SOUTH);
traitement= false;
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == parcourir){
JFileChooser dialogue = new JFileChooser();
dialogue.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(dialogue.showOpenDialog(this)== JFileChooser.APPROVE_OPTION){
String dossier = dialogue.getSelectedFile().getAbsolutePath();
(new File(dossier)).isDirectory();
textParcour.setText(dossier);
}
} else if(source == selectionner){
JFileChooser dialogue = new JFileChooser();
if(dialogue.showOpenDialog(this)== JFileChooser.APPROVE_OPTION){
String fichier = dialogue.getSelectedFile().getAbsolutePath();
textSelection.setText(fichier);
}
} else if(source == convertir){
try {
activation(false);
executeProcess();
/*test*/
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
traitement= true;
/*test*/
}catch (Exception error) {
System.out.println("Erreur :"+error);
}
System.out.println("activation interface");
activation(true);
}
}
public void activation(boolean act){
parcourir.setEnabled(act);
selectionner.setEnabled(act);
convertir.setEnabled(act);
}
/*static public void Message ( String mes){
JDialog box= new JDialog();
box.add(new JLabel(mes));
box.setResizable(false);
box.setVisible(true);
box.setSize(300,100); //On donne une taille à notre fenêtre
box.setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
} */
static public JDialog Message ( JLabel mes){
JDialog box= new JDialog();
box.add(mes);
box.setResizable(false);
box.setVisible(true);
box.setSize(200,100); //On donne une taille à notre fenêtre
box.setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
return box;
}
public static List<String> executeEdigeo2SHP(JTextField textParcour, JTextField textSelection){
final List<String> listCommand = new ArrayList<String>();
listCommand.add("cmd.exe");
listCommand.add("/C");
listCommand.add("Start");
listCommand.add("/B");
listCommand.add("/MIN");
String path = (new File(".")).getAbsolutePath();
listCommand.add(path.substring(0,path.length()-1)+"Edigeo2SHP.bat");
listCommand.add("\""+textParcour.getText()+"\"");
listCommand.add(textSelection.getText());
listCommand.add("exit");
return listCommand;
}
public void executeProcess( ) throws Exception {
progressbar.setString("démarage du traitement");
// Création de la ligne de commande
final List<String> listCommand = executeEdigeo2SHP(textParcour, textSelection);
// Création du processus ogr2ogr
final ProcessBuilder edigeo2SHP = new ProcessBuilder(listCommand);
edigeo2SHP.redirectErrorStream(true);
Process process = edigeo2SHP.start();
BufferedReader br = new BufferedReader(new InputStreamReader( process.getInputStream()));
ligne="";
i = 0;
while ( (( ligne = br.readLine()) != null) && !ligne.isEmpty() ) {
}
process.waitFor();
traitement= false;
// process.destroy();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
//if ("progress" == evt.getPropertyName()) {
progressbar.setString(i+":"+ligne);
progressbar.setValue(i%54);
progressbar.repaint();
//}
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
//On crée une nouvelle instance de notre interface
EDIGEO2SHP fenetre = new EDIGEO2SHP();
//On la rend visible
fenetre.setVisible(true);
}
});
}
} |
Partager