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
|
import java.awt.BorderLayout;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Date;
import javax.swing.JCheckBox;
public class Calendrier extends JPanel {
Fenetre fen= new Fenetre();
/**
*
*/
private static final long serialVersionUID = 1L;
static PreparedStatement stmt = null;
/** The currently-interesting year (not modulo 1900!) */
protected int yy;
/** Currently-interesting month and day */
protected int mm, dd;
/** The buttons to be displayed */
protected JButton labs[][];
/** The number of day squares to leave blank at the start of this month */
protected int leadGap = 0;
/** A Calendar object used throughout */
Calendar calendar = new GregorianCalendar();
/** Today's year */
protected final int thisYear = calendar.get(Calendar.YEAR);
/** Today's month */
protected final int thisMonth = calendar.get(Calendar.MONTH);
/** One of the buttons. We just keep its reference for getBackground(). */
private JButton b0;
/** The month choice */
private JComboBox monthChoice;
/** The year choice */
private JComboBox yearChoice;
static Connection c = null;
/**
* Construct a Cal, starting with today.
* @throws ParseException
*/
public Calendrier() throws ParseException {
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
}
/**
* Construct a Cal, given the leading days and the total days
* @throws ParseException
*
* @exception IllegalArgumentException
* If year out of range
*/
public Calendrier(int year, int month, int today) throws ParseException {
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
}
private void setYYMMDD(int year, int month, int today) {
yy = year;
mm = month;
dd = today;
}
String[] months = { "Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre" };
/** Build the GUI. Assumes that setYYMMDD has been called. */
private void buildGUI() {
getAccessibleContext().setAccessibleDescription(
"Calendar not accessible yet. Sorry!");
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
JPanel tp = new JPanel();
tp.add(monthChoice = new JComboBox());
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
// System.out.println("Month=" + mm);
monthChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = monthChoice.getSelectedIndex();
if (i >= 0) {
mm = i;
try {
recompute();
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription(
"Choose a month of the year");
tp.add(yearChoice = new JComboBox());
yearChoice.setEditable(true);
for (int i = yy - 5; i < yy + 5; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
try {
recompute();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
add(BorderLayout.CENTER, tp);
JPanel bp = new JPanel();
bp.setLayout(new GridLayout(7, 7));
labs = new JButton[6][7]; // first row is days
bp.add(b0 = new JButton("D"));
bp.add(new JButton("L"));
bp.add(new JButton("Ma"));
bp.add(new JButton("Me"));
bp.add(new JButton("J"));
bp.add(new JButton("V"));
bp.add(new JButton("S"));
ActionListener dateSetter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (!num.equals("")) {
// set the current day highlighted
try {
setDayActive(Integer.parseInt(num));
} catch (NumberFormatException | ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
};
// Construct all the buttons, and add them.
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
bp.add(labs[i][j] = new JButton(""));
labs[i][j].addActionListener(dateSetter);
}
add(BorderLayout.SOUTH, bp);
}
public final static int dom[] = { 31, 28, 31, 30, /* jan feb mar apr */
31, 30, 31, 31, /* may jun jul aug */
30, 31, 30, 31 /* sep oct nov dec */
};
/** Compute which days to put where, in the Cal panel
* @throws ParseException */
protected void recompute() throws ParseException {
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("Month " + mm
+ " bad, must be 0-11");
clearDayActive();
// Compute how much to leave before the first.
// getDay() returns 0 for Sunday, which is just right.
leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
int daysInMonth = dom[mm];
if (isLeap(calendar.get(Calendar.YEAR)) && mm > 0)
++daysInMonth;
// Blank out the labels before 1st day of month
for (int i = 0; i < leadGap; i++) {
labs[0][i].setText("");
}
// Fill in numbers for the day of month.
for (int i = 1; i <= daysInMonth; i++) {
JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
b.setText(Integer.toString(i));
}
// 7 days/week * up to 6 rows
for (int i = leadGap + daysInMonth; i < 6 * 7; i++) {
labs[(i) / 7][(i) % 7].setText("");
}
// Shade current day, only if current month
if (thisYear == yy && mm == thisMonth)
setDayActive(dd); // shade the box for today
// Say we need to be drawn on the screen
repaint();
}
public void askdate() throws ParseException{
JPanel panDate = new JPanel();
panDate.setBackground(Color.white);
panDate.setPreferredSize(new Dimension(220, 100));
JTextField dateField = new JTextField();
dateField.setPreferredSize(new Dimension(100, 25));
JLabel labelDate = new JLabel();
panDate.add(labelDate);
panDate.add(dateField);
JPanel content = new JPanel();
content.setBackground(Color.white);
calendar = new GregorianCalendar(yy, mm, dd);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String DATE = sdf.format(calendar.getTime());
//On récupere le contenu de la zone de texte
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:/home/isen/Documents/Projet_tech/agenda.db");
c.setAutoCommit(false);
//System.out.println("Opened database successfully");
stmt = c.prepareStatement("SELECT * FROM activite WHERE date = ? ;");
stmt.setString(1, DATE);
ResultSet rs ;
rs = stmt.executeQuery();
while ( rs.next() ) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String but = rs.getString("but");
Date date = df.parse(rs.getString("date"));
int part_journee = rs.getInt("part_journee");
String lieu = rs.getString("lieu");
String affiche = "but:"+but+"\ndate:"+df.format(date)+"\npartie journee:"+part_journee+"\nlieu:"+lieu+"\n\n";
JOptionPane.showMessageDialog(null, affiche, "Tache", JOptionPane.INFORMATION_MESSAGE);
}
rs.close();
stmt.close();
c.close();
}
catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
/**
* isLeap() returns true if the given year is a Leap Year.
*
* "a year is a leap year if it is divisible by 4 but not by 100, except
* that years divisible by 400 *are* leap years." -- Kernighan & Ritchie,
* _The C Programming Language_, p 37.
*/
public boolean isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
}
/** Set the year, month, and day
* @throws ParseException */
public void setDate(int yy, int mm, int dd) throws ParseException {
this.yy = yy;
this.mm = mm; // starts at 0, like Date
this.dd = dd;
recompute();
}
/** Unset any previously highlighted day
* @throws ParseException */
private void clearDayActive() throws ParseException {
JButton b;
// First un-shade the previously-selected square, if any
if (activeDay > 0) {
b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
}
private int activeDay = -1;
/** Set just the day, on the current month
* @throws ParseException */
public void setDayActive(int newDay) throws ParseException {
clearDayActive();
// Set the new one
if (newDay <= 0)
dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd = newDay;
// Now shade the correct square
Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
choice();
}
// Ma fonction qui me permettrait de choisir l'action selon le bouton cliqué
public void choice() throws ParseException{
if(fen.bouton1.isSelected()==true){
System.out.println("ok1");
}
else
//askdate();
System.out.println("ok2");
}
/** La méthode principale
* @throws ParseException */
public void ExecCal() throws ParseException
{
JFrame f = new JFrame("Mon Calendrier");
Container c = f.getContentPane();
c.setLayout(new FlowLayout());
// and beside it, the current month.
c.add(new Calendrier());
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
} |
Partager