1 19 package org.lucane.applications.whiteboard.graph.shapes; 20 21 import java.awt.Color ; 22 import java.awt.Graphics ; 23 import java.awt.Point ; 24 import java.io.File ; 25 import java.util.Date ; 26 import java.util.Hashtable ; 27 import java.util.Map ; 28 29 import javax.swing.ImageIcon ; 30 import javax.swing.JFileChooser ; 31 import javax.swing.filechooser.FileFilter ; 32 33 import org.jgraph.graph.AttributeMap; 34 import org.jgraph.graph.DefaultPort; 35 import org.jgraph.graph.GraphConstants; 36 import org.lucane.applications.whiteboard.WhiteBoard; 37 import org.lucane.applications.whiteboard.graph.MyGraph; 38 import org.lucane.applications.whiteboard.graph.cells.ImageCell; 39 import org.lucane.client.widgets.DialogBox; 40 41 class Image implements Shape 42 { 43 private WhiteBoard plugin; 44 45 public Image(WhiteBoard plugin) 46 { 47 this.plugin = plugin; 48 } 49 50 public void paint(Graphics g, Point start, Point end) 51 { 52 java.awt.Rectangle bounds = ShapeUtils.getBounds(start, end); 53 g.setColor(Color.LIGHT_GRAY); 54 g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height); 55 } 56 57 public void addToGraph(MyGraph graph, Point start, Point end) 58 { 59 ImageCell cell = new ImageCell(""); 60 AttributeMap attrs = new AttributeMap(); 61 attrs.put("timestamp", new Date ()); 62 63 ImageIcon icon = getIcon(); 64 if(icon == null) 65 return; 66 67 GraphConstants.setIcon(attrs, icon); 68 GraphConstants.setBounds(attrs, ShapeUtils.getBounds(start, end)); 69 DefaultPort hp = new DefaultPort(); 70 cell.add(hp); 71 72 Object [] cells = new Object [] { cell }; 73 74 Map attributes = new Hashtable (); 75 attributes.put(cell, attrs); 76 graph.getModel().insert(cells, attributes, null, null, null); 77 } 78 79 private ImageIcon getIcon() 80 { 81 JFileChooser jfc = new JFileChooser (); 82 jfc.setFileFilter(new ImageFileFilter()); 83 int returnVal = jfc.showOpenDialog(null); 84 if(returnVal == JFileChooser.APPROVE_OPTION) 85 { 86 try { 87 return new ImageIcon (jfc.getSelectedFile().getAbsolutePath()); 88 } catch(Exception e) { 89 DialogBox.error("Unable to load image : " + e); 90 } 91 } 92 93 return null; 94 } 95 96 class ImageFileFilter extends FileFilter 97 { 98 private String [] knownTypes = { 99 ".gif", 100 ".png", 101 ".jpg", 102 ".jpeg" 103 }; 104 105 public boolean accept(File f) 106 { 107 if(f.isDirectory()) 108 return true; 109 110 String name = f.getName().toLowerCase(); 111 for(int i=0;i<knownTypes.length;i++) 112 { 113 if(name.endsWith(knownTypes[i])) 114 return true; 115 } 116 117 return false; 118 } 119 120 public String getDescription() 121 { 122 return plugin.tr("filter.imageFiles"); 123 } 124 } 125 } | Popular Tags |