Posts tagged ‘Icons SWT sistema Windows’

julio 9, 2011

Opteniendo los Iconos del Sistema con SWT

java

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.program.Program;
/**
 *
 * @author Leyer
 *
 */
public class IconProgram extends JDialog{

	private static final long serialVersionUID = 1L;

	public static final String[] EXTENSIONS =Program.getExtensions();
	public BufferedImage convertToBufferedReader(ImageData data) {
	    ColorModel colorModel = null;
	    PaletteData palette = data.palette;
	    if (palette.isDirect) {
	      colorModel = new DirectColorModel(data.depth, palette.redMask,palette.greenMask, palette.blueMask);
	      BufferedImage bufferedImage = new BufferedImage(colorModel,colorModel.createCompatibleWritableRaster(data.width,data.height), false, null);
	      WritableRaster raster = bufferedImage.getRaster();

	      int[] pixelArray = new int[3];
	      for (int y = 0; y < data.height; y++) {
	        for (int x = 0; x < data.width; x++) {
	          int pixel = data.getPixel(x, y);
	          RGB rgb   = palette.getRGB(pixel);
	          if(rgb.red==0&&rgb.green==0&&rgb.blue==0){
		          pixelArray[0] = -1;pixelArray[1] = -1;pixelArray[2] = -1;
	          }else{
	        	  pixelArray[0] = rgb.red;pixelArray[1] = rgb.green;pixelArray[2] = rgb.blue;
	          }
	          raster.setPixels(x, y, 1, 1, pixelArray);
	        }
	      }
	      return bufferedImage;
	  }
		return null;
    }
	private JTable table;
	private DefaultTableModel defaultTableModel;
	private JScrollPane scrollPane;
	private class cellRenderer extends DefaultTableCellRenderer{
		private static final long serialVersionUID = 1L;
		@Override
		public void setValue(Object value) {
			if(value != null){
				try{
					Program program = Program.findProgram(value.toString());
					setIcon(new ImageIcon(convertToBufferedReader(program.getImageData())));
				}catch (Exception e) {

				}
			}
		}
	}
	public IconProgram(){
		defaultTableModel=new DefaultTableModel();
		defaultTableModel.addColumn("Icon");
		defaultTableModel.addColumn("Type");
		table = new JTable(defaultTableModel);
		table.getColumnModel().getColumn(0).setCellRenderer(new cellRenderer());
		for(int index=0;index			defaultTableModel.addRow(
					new Object[]{EXTENSIONS[index],
							Program.findProgram(EXTENSIONS[index])});
		table.getColumnModel().getColumn(1).setPreferredWidth(920);
		scrollPane=new JScrollPane(table);
		getContentPane().add(scrollPane);
		this.setTitle("Icons");
		this.setSize(500, 500);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new IconProgram().setVisible(true);
			}
		});
	}
}