//------------------------------------------------------------------------------ // Logiciel de gestion du parc informatique // // Stage de licence professionnelle informatique option génie logiciel // Mai - Septembre 2004 // Auteur: // Tristan Liebaert // libeur@free.fr // //------------------------------------------------------------------------------ /* * Ce fichier contient un composant graphique permettant la représentation d'une liste * de matériel. Comme chaque matériel dispose de caractéristiques dépendantes de son type, * le composant devra prendre en compte cette souplesse * * Pour ce faire, les 4 champs impératifs (marque, modele, prixunitaire, type) disposeront de * leur colonne avec titre. * Les caractéristiques propres au type seront placées concaténées dans une dernière colonnes. * Le commentaire sur le matériel sera affiché en dessous de la liste. seul le matériel sélectionné * pourra voir son commentaire affiché */ import javax.swing.table.AbstractTableModel; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.*; import java.util.Vector; // description du contenu de la table de listing du matériel class ListePourTable extends AbstractTableModel { private String [] nomCol; private SourceDonneesStockInfo dataSource; private VecMateriel contenu, contenuFiltre; private String filtre; public ListePourTable(SourceDonneesStockInfo source) { dataSource = source; nomCol = new String[5]; nomCol[0] = "Référence interne"; nomCol[1] = "Type"; nomCol[2] = "Marque"; nomCol[3] = "Modele"; nomCol[4] = "Prix unitaire"; contenu = new VecMateriel(); contenuFiltre = new VecMateriel(); filtre = "*"; } public VecMateriel getContenu() { return contenu; } public void setContenu(VecMateriel vec) { contenu = vec; contenuFiltre = new VecMateriel(vec); filtre = "*"; } public int getSize() { return contenu.getSize(); } public void ajouterLigne(Materiel m) { m.nom_type = dataSource.getLibType(m.type); contenu.add(m); setFiltre(filtre); } public void clear() { contenu.clear(); contenuFiltre.clear(); } public double coutTotal() { return contenu.coutTotal(); } public void supprimerLigne(int i) { Materiel temp = contenuFiltre.get(i); contenu.supprimer(temp); contenuFiltre.supprimer(i); } public void setFiltre(String f) { filtre = f; contenuFiltre = new VecMateriel(contenu); for (int i = contenuFiltre.getSize()-1; i>=0; --i) { if(!contenuFiltre.get(i).nom_type.equals(filtre) && !filtre.equals("*")) contenuFiltre.supprimer(i); } } public Materiel get(int pos) {return contenuFiltre.get(pos);} public int getColumnCount(){return 5;} public int getRowCount(){return contenuFiltre.getSize();} public String getColumnName(int col) { return nomCol[col]; } public boolean isCellEdtitable(int row, int col) {return false;} public Object getValueAt(int parm1, int parm2) { Materiel temp = contenuFiltre.get(parm1); switch (parm2) { case 0 : return new Integer(temp.num_matos).toString(); case 1 : return temp.nom_type; case 2 : return temp.marque; case 3 : return temp.modele; case 4 : return new Float(temp.pxUnit).toString(); default : return ""; } } } class ListeMateriel extends JPanel implements MouseListener, ActionListener { private JTable tableau; private ListePourTable contenu; private ListeNom carac; private SourceDonneesStockInfo dataSource; private TexteDefilable commentaire; private JScrollPane ascenceur; private JPanel panneauVariable; private JComboBox selectType; private JButton modifier; public ListeMateriel(SourceDonneesStockInfo source) { // définition du panneau super(new BorderLayout()); dataSource = source; // définition du JComboBox pour la selection du type à afficher selectType = new JComboBox(dataSource.getListeType().getVector()); selectType.addItem("*"); selectType.setSelectedItem("*"); selectType.addActionListener(this); // déinition de la table d'afichage du matériel contenu = new ListePourTable(dataSource); tableau = new JTable(contenu); tableau.setVisible(true); tableau.setShowGrid(true); // définition du panneaux contenant le commentaire et les caractéristiques propres au matériel panneauVariable = new JPanel(new GridLayout(1,2,5,5)); // définition de la zone de texte défilable qui contiendra les commentaires commentaire = new TexteDefilable(4,20); commentaire.setEditable(false); //définition du paneau d'affichage des caractéristiqes spécifiques à caque matériel carac = new ListeNom(0,4); //définition des ascenceurs int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER; ascenceur = new JScrollPane(tableau, v, h); JPanel panneauBas = new JPanel(new BorderLayout()); panneauBas.add(panneauVariable, BorderLayout.CENTER); modifier = new JButton("modifier les caractéristiques"); modifier.addActionListener(this); panneauBas.add(modifier, BorderLayout.SOUTH); // placement du composant dans le panneau add(selectType, BorderLayout.NORTH); add(ascenceur, BorderLayout.CENTER); tableau.addMouseListener(this); add(panneauBas, BorderLayout.SOUTH); panneauVariable.add(carac); panneauVariable.add(commentaire); ascenceur.setPreferredSize(new Dimension(540,150)); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==selectType) { contenu.setFiltre((String)(selectType.getSelectedItem())); tableau.updateUI(); } else if (ae.getSource() == modifier) { FenetreModificationMateriel f = new FenetreModificationMateriel (dataSource, getSelectedElement()); f.setVisible(true); } } public void mouseClicked(MouseEvent e) { ajusterCommentaire(); ajusterCaracteristiques(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} // cette procedure definit le contenu du tableau, et redéfini la table graphique public void setContenu(VecMateriel nouvContenu) { contenu.setContenu(nouvContenu); tableau.updateUI(); } public VecMateriel getContenu() { return contenu.getContenu(); } public int getNbElts() { return contenu.getSize(); } public boolean isSelected() { return (tableau.getSelectedRow()!=-1); } private void ajusterCommentaire() { commentaire.setText(getSelectedElement().commentaire); } private void ajusterCaracteristiques() { carac.setContenu(dataSource.getProprietes(getSelectedElement().num_matos).toListeChaine()); } public void setCaracVisible(boolean flag) { carac.setVisible(flag); } public double coutTotal() { return contenu.coutTotal(); } public Materiel getSelectedElement() { if (tableau.getSelectedRow() != -1) { return contenu.get(tableau.getSelectedRow()); } else { Materiel temp = new Materiel(); temp.commentaire =("Aucun élément sélectionné"); return temp; } } public VecMateriel getSelection() { int [] selectedIndexes = tableau.getSelectedRows(); VecMateriel res = new VecMateriel(); for(int i = 0; i< selectedIndexes.length; ++i) { res.add(contenu.get(selectedIndexes[i])); } return res; } public void add(Materiel matos) { contenu.ajouterLigne(matos); tableau.updateUI(); } public void add(VecMateriel matos) { for(int i = 0; i < matos.getSize(); ++i) { contenu.ajouterLigne(matos.get(i)); } tableau.updateUI(); } public void retirerSelection() { int [] selectedIndexes = tableau.getSelectedRows(); System.out.println(selectedIndexes.length); for(int i = selectedIndexes.length - 1; i>=0; --i) { contenu.supprimerLigne(selectedIndexes[i]); } System.out.println("this"); tableau.updateUI(); } } class VecMateriel { private Vector contenu; public VecMateriel() { contenu = new Vector(0); } public double coutTotal() { double res = 0; for(int i = 0; i<getSize(); ++i) { res = res + get(i).pxUnit; } return res; } public VecMateriel(VecMateriel v) { contenu = new Vector(v.contenu); } public void add(Materiel matos) { contenu.add(new Materiel(matos)); } public String toString() { String res = ""; for(int i = 0; i<getSize(); ++i) { res = res + get(i).toString() + "\n"; } return res; } public Materiel get(int i) { return((Materiel)contenu.get(i)); } public int getSize() { return contenu.size(); } public void supprimer(Materiel mat) { boolean suppr = false; int i = 0; while(!suppr && i<getSize()) { if(get(i).num_matos == mat.num_matos) { supprimer(i); suppr = true; } ++i; } } public void supprimer(int i) { contenu.remove(i); } public void clear() { contenu.clear(); } public boolean contient(Materiel matos) { boolean res = false; for(int i =0; i< getSize(); ++i) {res res || (get(i).num_matos matos.num_matos) ; } return res; } } // description d'une class permettant la manipulation d'un matériel, quelque soit // son type, en mémorisant toutes les caractéristiques et attributs stockés // dans la source de données // cette classe existe car le Java e propose pas de structure. class Materiel { public String marque,modele,commentaire, nom_type; public int num_matos, type; public float pxUnit; public ListeCoupleChaine carac; public Materiel() { num_matos = -1; type = -1; pxUnit = 0; carac = new ListeCoupleChaine(); nom_type = new String(); marque = new String(); modele = new String(); commentaire = new String(); } public String toString() { return num_matos + " " + nom_type + " " + marque + " "+ modele+"\n"; } public Materiel(Materiel m) { nom_type = m.nom_type; marque = m.marque; modele = m.modele; commentaire = m.commentaire; type = m.type; num_matos = m.num_matos; pxUnit = m.pxUnit; carac = new ListeCoupleChaine(m.carac); } public void setType(int arg) { type = arg; } public void setNumMatos(int arg) {num_matos = arg;} public void setMarque(String argmarque) {marque = argmarque;} public void setModele(String argmodele) {modele = argmodele;} public void setPxUnit(float argpx) {pxUnit = argpx;} public void setCommentaire(String argcomment) {commentaire = argcomment;} public void setCarac(ListeCoupleChaine arg) {carac = arg;} public String getMarque() {return marque;} public String getModele() {return modele;} public float getPxUnit() {return pxUnit;} public String getCommentaire() {return commentaire;} public int getNumMatos() {return num_matos;} public ListeCoupleChaine getCarac() {return carac;} }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question