Impressions en Java : listage des services d'impression

import java.awt.BorderLayout;
import java.awt.Print.PrinterJob;
import javax.Print.PrintService;
import javax.Print.attribute.Attribute;
import javax.Print.attribute.PrintServiceAttributeSet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.SwingConstants;

public class ListeServicesImp extends JFrame implements Runnable{

   private static final long serialVersionUID = 1L;
   private JPanel jContentPane = null;
   private JTabbedPane jTabbedPane = null;
   private Cursor attendre = new Cursor(Cursor.WAIT_CURSOR);
   private Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);
   private String[] titres={"Attribut", "Valeur", "Rôle"};
   private Dimension dim = new Dimension(10,100);

   public ListeServicesImp(JFrame parent) {
      super();
      initialize();
      setLocationRelativeTo(parent);
      Thread t = new Thread(this);
      t.start();
   }
   
   private void initialize() {
      this.setSize(549, 496);
      this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imprimante.gif")));      
      this.setTitle("Les services d'impression");
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jTabbedPane = new JTabbedPane();
      jTabbedPane.setTabPlacement(JTabbedPane.LEFT);
      jTabbedPane.setForeground(new Color(255, 51, 51));
      jTabbedPane.setFont(new Font("Courier new", Font.BOLD, 14));
      jContentPane.add(jTabbedPane, BorderLayout.CENTER);
      this.setContentPane(jContentPane);
   }
   
   class Props extends JPanel {
      private static final long serialVersionUID = 1L;
      private JScrollPane jScrollPane = null;
      private JScrollPane jScrollPane1 = null;
      private JTable jTable = null;
      private JTable jTable1 = null;
      private DefaultTableModel dtm =null;
      private DefaultTableModel dtm1 =null;
      private JPanel panneau = null;
      
      public Props( DefaultTableModel dtm, DefaultTableModel dtm1 ) {
         super();
         this.dtm = dtm;
         this.dtm1 = dtm1;
         this.setSize(300, 200);
         this.setLayout(new BorderLayout());
         jScrollPane = new JScrollPane();
         jTable = new JTable(dtm);
         jTable.setFont(new java.awt.Font("Comic Sans MS", java.awt.Font.PLAIN, 14));
         jTable.setForeground(new java.awt.Color(0,0,247));
         jScrollPane.setViewportView(jTable);
         jScrollPane.setPreferredSize(dim);
         this.add(jScrollPane, BorderLayout.NORTH);
         panneau = new JPanel();
         panneau.setLayout(new BorderLayout());
         JLabel b = new JLabel("<html><center>Capacités et valeurs par défaut<br> de l'imprimante</center></html>");
         b.setFont(new java.awt.Font("Comic Sans MS", java.awt.Font.PLAIN, 14));
         b.setForeground(new java.awt.Color(0,0,247));
         b.setHorizontalAlignment(SwingConstants.CENTER);
         panneau.add(b, BorderLayout.NORTH);
         jScrollPane1 = new JScrollPane();
         jTable1 = new JTable(dtm1);
         jTable1.setFont(new java.awt.Font("Comic Sans MS", java.awt.Font.PLAIN, 14));
         jTable1.setForeground(new java.awt.Color(0,0,247));
         jScrollPane1.setViewportView(jTable1);
         panneau.add(jScrollPane1, BorderLayout.CENTER);
         this.add(panneau, BorderLayout.CENTER);
      }

   }
   
   public void run(){
      setCursor(attendre);
      PrintService []services = PrinterJob.lookupPrintServices();
      for(int i =0; i<services.length; ++i) {
         // attributs de l'imprimante ...
         PrintServiceAttributeSet psas = services[i].getAttributes();
         Attribute[] atts = psas.toArray();
         String[][] data = new String[atts.length][3];
         for( int j=0; j<atts.length; ++j){
            data[j][0] = atts[j].getClass().getSimpleName();
            data[j][1] = atts[j].toString();
            data[j][2] = role(atts[j].getClass>());
         }
         // Les capacités et valeurs par défaut de l'imprimante.
         Class<?>[] categories = services[i].getSupportedAttributeCategories();
         String[][] data1 = new String[categories.length][3];
         for(int j = 0; j<categories.length; ++j){
            Class<? extends Attribute> categorie = (Class<? extends Attribute>)categories[j];
            data1[j][0] = categorie.getSimpleName();            
            data1[j][1] = services[i].getDefaultAttributeValue(categorie)==null
                          ?""
                          :services[i].getDefaultAttributeValue(categorie).toString();         
            data1[j][2] = role(categorie);
         }
                  
         Props ps = new Props(new DefaultTableModel(data, titres),new DefaultTableModel(data1, titres));
         jTabbedPane.add(services[i].getName(), ps);
         repaint();
         try {
            Thread.sleep(100);
         } catch (interruptedException e) {
         }
      }
      setCursor(normal);
   }
   
   public String role(Class<? extends Attribute> cat){
      Class<?>[] is = cat.getInterfaces();
      StringBuffer res = new StringBuffer("00000");
      for(int i = 0; i<is.length; ++i){
         if(is[i].getSimpleName().equals("PrintRequestAttribute"))   res.replace(0, 1, "X"); 
         if(is[i].getSimpleName().equals("DocAttribute"))            res.replace(1, 2, "X"); 
         if(is[i].getSimpleName().equals("PrintJobAttribute"))       res.replace(2, 3, "X");
         if(is[i].getSimpleName().equals("PrintServiceAttribute"))   res.replace(3, 4, "X"); 
         if(is[i].getSimpleName().equals("SupportedValuesAttribute"))res.replace(4, 5, "X"); ;
      }
      return res.toString();
   }
   
   public static void main(String[] args) {
      ListeServicesImp application = new ListeServicesImp(null);
      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      application.setVisible(true);
   }
}  

haut de la page