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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
| import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Formulaire extends JFrame implements KeyListener, ActionListener,
PropertyChangeListener {
private static final long serialVersionUID = 1L;
JLabel titre = new JLabel("Titre :");
JLabel dernierScanLu = new JLabel("Dernier Scan Lu:");
static JTextField textField = new JTextField(20);
JTextField textField2 = new JTextField(20);
SimpleDateFormat format;
JFormattedTextField scanLu;
NumberFormat scanFormat;
JButton annuler = new JButton("Annuler");
JButton ok = new JButton("Valider");
String sel = null;
MainFrame frame;
Date now = new Date();
InfosManga info;
ConnexionMangaLu con = new ConnexionMangaLu();
Affichage model;
boolean champsValide = true;
boolean rechercheEffectue = false;
Object[] elements = new Object[] { "Shonen", "Shojo", "Seinen", "WebToons",
"Ecchi" };
static double chap = 0;
JCheckBox dernierChap;
@SuppressWarnings({ "rawtypes" })
JComboBox choixType = new JComboBox();
// implémentation de l'auto complétion
// AutoCompleteSupport support =
// AutoCompleteSupport.install(choixType,GlazedLists.eventListOf(elements));
public Formulaire() {
this.setTitle("Ajouter un Manga");
this.setSize(500, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setLayout(new GridBagLayout());
setUpFormats();
annuler.setToolTipText("Annuler et revenir a la page precedente");
textField.addKeyListener(this);
textField2.addKeyListener(this);
// Create the text fields and set them up.
scanLu = new JFormattedTextField(scanFormat);
scanLu.setValue(new Double(0));
scanLu.setColumns(10);
scanLu.addPropertyChangeListener("value", this);
// Create the check boxes.
dernierChap = new JCheckBox("Dernier Chap", false);
dernierChap.setMnemonic(KeyEvent.VK_D);
// dernierChap.addItemListener(this);
dernierChap.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (lancementCheckBox())
rechercheEffectue = true;
}
}
});
// Tell accessibility tools about label/textfield pairs.
dernierScanLu.setLabelFor(scanLu);
GridBagConstraints c = new GridBagConstraints();
/********************* positionnement des objet en x et y ********************/
c.gridx = 0;
c.gridy = 0;
c.gridheight = c.gridwidth = 1;
c.anchor = GridBagConstraints.BASELINE_LEADING;
c.insets = new Insets(40, 20, 0, 0);
this.add(titre, c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.BASELINE_LEADING;
c.insets = new Insets(10, 10, 0, 20);
this.add(textField, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = c.gridwidth = 1;
c.insets = new Insets(0, 20, 0, 0);
this.add(dernierScanLu, c);
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(10, 10, 0, 20);
this.add(scanLu, c);
c.gridx = 2;
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.BASELINE_LEADING;
c.insets = new Insets(10, 10, 0, 20);
this.add(dernierChap, c);
c.gridx = 1;
c.gridy = 8;
c.gridwidth = GridBagConstraints.RELATIVE;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.BASELINE_TRAILING;
c.insets = new Insets(0, 5, 30, 0);
this.add(ok, c);
c.gridx = 2;
c.gridy = 8;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.BASELINE_LEADING;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 5, 30, 20);
this.add(annuler, c);
// Rcupération du choix sur la combobox
sel = (String) choixType.getSelectedItem();
ok.setEnabled(false);
/* formatage de la date (jour Mois Année) */
format = new SimpleDateFormat("dd MMM yyyy");
/* utiliser le calendrier par défaut */
Calendar calendar = Calendar.getInstance();
String date = format.format(calendar.getTime());
// action listener sur bouton Ok
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// System.out.println(support.getSelectedItem());
if (!rechercheEffectue) {
lancement();
} else {
if (con.enregistrement(getTitre().toUpperCase(), chap, date)
&& con.infosTableau(getTitre().toUpperCase(), chap)) {
JOptionPane.showMessageDialog(Formulaire.this,
"Données enregistrées");
con.close();
choix();
} else {
close();
}
}
}
});
annuler.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
close();
}
});
scanLu.getDocument().addDocumentListener(new DocumentListener() {
/**** Méthodes Héritées de la classe DocumentEvent ******/
protected void update() {
// Activation du bouton Ok si le champs Password contient des
// données
ok.setEnabled(scanLu.getDocument().getLength() > 0);
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
});
pack();
this.setVisible(true);
}
/**
* renvoie l'etat de la recherche ( a savoir si les infos du mangas ont deja
* été reupérées )
**/
public boolean etatRecherche() {
return rechercheEffectue;
}
/*** appel de la fonction permettant l'enregistrement des infos ***/
private void lancement() {
if (con.titreValide(getTitre())) {
con.connection();
if (con.addManga(getTitre(), getDernierScanLu())
&& con.infosTableau(getTitre().toUpperCase(), chap)) {
JOptionPane.showMessageDialog(Formulaire.this,
"Données enregistrées");
con.close();
choix();
} else {
close();
}
} else {
close();
new Formulaire();
}
}
/*** appel de la fonction permettant la recherche des infos ***/
private boolean lancementCheckBox() {
boolean lan = true;
if (con.titreValide(getTitre())) {
con.connection();
String dernierChap = con.addMangaCheckBox(getTitre(),
getDernierScanLu());
if (dernierChap.equals("invalide")) {
close();
lan = false;
} else {
double Chap = Double.parseDouble(dernierChap);
scanLu.setValue(new Double(Chap));
}
} else {
lan = false;
close();
new Formulaire();
}
return lan;
}
private void setUpFormats() {
scanFormat = NumberFormat.getNumberInstance();
}
private void close() {
this.dispose();
}
public static String getTitre() {
return textField.getText().toUpperCase();
}
public String getChoix() {
return sel;
}
public static Double getDernierScanLu() {
return chap;
}
/** choisir si l'on veut ajouter un nouveau mangas ou pas **/
private void choix() {
final JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent) e.getSource());
pane.setValue(okButton);
}
});
final JButton cancelButton = new JButton("Annuler");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent) e.getSource());
pane.setValue(cancelButton);
}
});
// choix pour si oui ou non on veux inserer un nouveau manga
if (JOptionPane.showConfirmDialog(null,
"Voulez vous rentrer un nouveau manga ?", "Fais ton choix",
JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
this.dispose();
new Formulaire();
} else {
/*** on determine si l'appel vient du tableau ***/
if (!frame.appelTableau()) {
this.dispose();
frame = new MainFrame();
} else {
this.dispose();
// frame.dispose();
// new MainFrame();
String titr=getTitre();
Double chapLu=getDernierScanLu();
System.out.println(getTitre());
System.out.println(info.getAuteur());
System.out.println(info.getGenre());
System.out.println(info.getDernierChap());
System.out.println(info.getDate());
System.out.println(getDernierScanLu());
System.out.println(info.getStatut());
System.out.println(con.getDate());
frame.recupInfo(titr, InfosManga.getGenre(), info.getAuteur(), info.getDernierChap(),
info.getDate(), chapLu, con.getDate(), info.getStatut());
/* model.addManga(new Manga(getTitre(), InfosManga.getGenre(), info.getAuteur(), info.getDernierChap(),
info.getDate(), getDernierScanLu(), con.getDate(), info.getStatut()));
// frame.traitement();
*/
}
}
}
private static JOptionPane getOptionPane(JComponent parent) {
while (!(parent instanceof JOptionPane)) {
parent = (JComponent) parent.getParent();
}
return (JOptionPane) parent;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER)
ok.doClick();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
/****
* Fonction permettant l'élimination des lettres dans le champs Dernier chap
****/
@Override
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == scanLu)
/* on ne garde que la partie numérique */
chap = ((Number) scanLu.getValue()).doubleValue();
}
} |