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
|
/**********************************************************************/
/**********************************************************************/
/*** ***/
/*** Class Dico : Classe permettant d'afficher le contenu ***/
/*** de la table qui contient toutes les pieces du dico ***/
/*** ***/
/**********************************************************************/
/**********************************************************************/
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Vector;
//import javax.swing.BoxLayout;
//import java.util.Random;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
public class Dico extends JInternalFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static final int PER_PAGE = 50;
private String sBase;
private MTA win; //page mta
private JTable tab_List;//table d'affichage
private JScrollPane js_Center;
private JPanel jp;
private JComboBox jcb_tri, jcb_tables;//cobobox pr le choix
private JLabel pages_Status;
private JButton jb_previous, jb_next;
private int current_page;
private JTextField jtf;
private String[] fieldName;
//Constructeur
Dico(MTA m,String base){
win=m;
sBase=base;
//construction de l'interface
jp=new JPanel(new BorderLayout());
jp.setPreferredSize(new Dimension(900,600));
JPanel jp1=new JPanel(new BorderLayout());
JButton jb=new JButton("Close");
jb.setName("Close");
jb.addActionListener(this);
jp1.add(jb,BorderLayout.EAST);
JPanel jp2=new JPanel();
BoxLayout bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jb_previous=new JButton("Previous");
jb_previous.setName("Previous");
jb_previous.addActionListener(this);
jp2.add(jb_previous);
jp2.add(Box.createHorizontalStrut(15));
jtf = new JTextField(2);
jp2.add(jtf);
jp2.add(Box.createHorizontalStrut(5));
JButton jb_go = new JButton("Go");
jb_go.setName("Go");
jb_go.addActionListener(this);
jp2.add(jb_go);
jp2.add(Box.createHorizontalStrut(15));
jb_next=new JButton("Next");
jb_next.setName("Next");
jb_next.addActionListener(this);
jp2.add(jb_next);
jp1.add(jp2, BorderLayout.WEST);
pages_Status = new JLabel("", JLabel.CENTER);
jp1.add(pages_Status, BorderLayout.CENTER);
jp.add(jp1,BorderLayout.SOUTH);
jp1 = new JPanel(new BorderLayout());
jp2 = new JPanel();
bhor = new BoxLayout(jp2,BoxLayout.X_AXIS);
jp2.setLayout(bhor);
jcb_tables = new JComboBox();
jcb_tables.setName("tables");
jcb_tri = new JComboBox();
jcb_tri.setName("tri");
//jcb_tri.addActionListener(this);
//jcb_tri.addItemListener(this);
jp2.add(new JLabel("Table:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tables);
jp2.add(Box.createHorizontalStrut(15));
jp2.add(new JLabel("Order by:"));
jp2.add(Box.createHorizontalStrut(15));
jp2.add(jcb_tri);
jp1.add(jp2,BorderLayout.WEST);
jp.add(jp1, BorderLayout.NORTH);
//initialisation de la table, tri par item_code
initTable(sBase,"detail",0,PER_PAGE,"item_code");
//jcb_tri.addItemListener(this);
//ajout de la liste des tables
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst = my.executeQuery("SELECT DISTINCT table_name from headers;");
while(my.goNextRst(rst)){
jcb_tables.addItem(my.getValueRst(rst,"table_name"));
}
rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
my.closeConnection();
//ajout du listener apres la création de la liste
jcb_tri.addActionListener(this);
jcb_tables.addActionListener(this);
current_page=1;
this.getContentPane().add(jp);
//parametre de la fenetre
//this.closable=false;
this.iconable=true;
this.maximizable=true;
this.resizable=true;
this.setTitle("List of Items in the dictionnary");
this.pack();
this.setVisible(true);
Tools.centerInterFrame(this,win);
}
//Génération de la table
private void initTable(String base, String table, int lim_basse, int nb_records, String order){
String s;
int nb_lines, nb_pages;
//connection à la base de donnée
JDBCAdapter data=new JDBCAdapter(
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/"+base,
"root",
"");
tab_List=new JTable();
//on recupere le nombre de lignes et le nombre de pages
nb_lines = getNbLines(base,"ta_"+table);
nb_pages = nb_lines/PER_PAGE+1;
pages_Status.setText("Page "+(lim_basse/PER_PAGE+1)+" of "+String.valueOf(nb_pages));
jtf.setText(String.valueOf(lim_basse/PER_PAGE+1));
s="SELECT * FROM ta_" + table +" ORDER BY " + order + " LIMIT " + lim_basse + "," +nb_records;
data.executeQuery(s);
//on recupere le nom des champs de la table selectionner
MysqlAccess my = new MysqlAccess(base);
my.startConnection();
s = "SELECT name FROM headers WHERE table_name='" + table + "' ORDER BY position";
int rst = my.executeQuery(s);
String[] chmp = new String[my.getResultCount(rst)];
int i=0;
while(my.goNextRst(rst))
{
chmp[i] = my.getValueRst(rst,"name");
i++;
}
my.closeConnection();
//on défini le noms des colonnes
data.setColumnNames(chmp);
data.fireTableStructureChanged();
//on insere les données dans notre tableau
tab_List.setModel(data);
//empeche de pouvoir réorganiser les colonnes
tab_List.getTableHeader().setReorderingAllowed(false);
js_Center=new JScrollPane(tab_List);
jp.add(js_Center,BorderLayout.CENTER);
js_Center.setPreferredSize(new Dimension(300,200));
jb_previous.setEnabled(true);
jb_next.setEnabled(true);
if(lim_basse==0)
jb_previous.setEnabled(false);
if((lim_basse+PER_PAGE)>nb_lines)
jb_next.setEnabled(false);
}
//fonction permettant de retourner le nombre de ligne d'une table d'une base
private int getNbLines(String base, String table)
{
String s;
int rst,nblines;
MysqlAccess my=new MysqlAccess(base);
my.startConnection();
s="SELECT COUNT(*) AS nb_lines FROM "+table;
rst = my.executeQuery(s);
my.goNextRst(rst);
nblines = Integer.valueOf(my.getValueRst(rst,"nb_lines"));
my.closeConnection();
return(nblines);
}
public void actionPerformed (ActionEvent evt){
String s;
s=evt.getSource().getClass().toString();
//détection de la selection d'un critere de tri ou d'une table à afficher
if(s.equals("class javax.swing.JComboBox"))
{
JComboBox cb = (JComboBox)evt.getSource();
String x = cb.getName();
if(x.equals("tri"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
if(x.equals("tables"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
jcb_tri.removeActionListener(this);
jcb_tri.removeAllItems();
MysqlAccess my=new MysqlAccess("mta_dico");
my.startConnection();
int rst =my.executeQuery("DESCRIBE ta_" + (String)jcb_tables.getSelectedItem());
while(my.goNextRst(rst))
{
jcb_tri.addItem(my.getValueRst(rst,"field"));
}
jcb_tri.addActionListener(this);
initTable(sBase,(String)jcb_tables.getSelectedItem(),0,PER_PAGE,(String)jcb_tri.getSelectedItem());
this.validate();
}
}
//détection des actions sur les boutons
if (s.equals("class javax.swing.JButton")){
JButton c = (JButton)evt.getSource();
String x = c.getName();
if(x.equals("Close")){
// fermetutre
//this.closable=true;
//win.setStatus("List of Items in the dictionnary").close();
//this.dispose();
win.setStatus("List of Items in the dictionnary");
this.setVisible(false);
// win.closeMTA();
}
if(x.equals("Next"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*current_page,PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page++;
this.validate();
}
if(x.equals("Previous"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(current_page-2),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page--;
this.validate();
}
if(x.equals("Go"))
{
js_Center.remove(tab_List);
tab_List=null;
jp.remove(js_Center);
js_Center=null;
initTable(sBase,(String)jcb_tables.getSelectedItem(),PER_PAGE*(Integer.valueOf(jtf.getText())-1),PER_PAGE,(String)jcb_tri.getSelectedItem());
current_page = Integer.valueOf(jtf.getText());
this.validate();
}
}
}
} |
Partager