KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > graph > shapes > Image


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.graph.shapes;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JFileChooser JavaDoc;
31 import javax.swing.filechooser.FileFilter JavaDoc;
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 JavaDoc g, Point JavaDoc start, Point JavaDoc end)
51     {
52         java.awt.Rectangle JavaDoc 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 JavaDoc start, Point JavaDoc end)
58     {
59         ImageCell cell = new ImageCell("");
60         AttributeMap attrs = new AttributeMap();
61         attrs.put("timestamp", new Date JavaDoc());
62         
63         ImageIcon JavaDoc 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 JavaDoc[] cells = new Object JavaDoc[] { cell };
73
74         Map JavaDoc attributes = new Hashtable JavaDoc();
75         attributes.put(cell, attrs);
76         graph.getModel().insert(cells, attributes, null, null, null);
77     }
78     
79     private ImageIcon JavaDoc getIcon()
80     {
81         JFileChooser JavaDoc jfc = new JFileChooser JavaDoc();
82         jfc.setFileFilter(new ImageFileFilter());
83         int returnVal = jfc.showOpenDialog(null);
84         if(returnVal == JFileChooser.APPROVE_OPTION)
85         {
86             try {
87                 return new ImageIcon JavaDoc(jfc.getSelectedFile().getAbsolutePath());
88             } catch(Exception JavaDoc e) {
89                 DialogBox.error("Unable to load image : " + e);
90             }
91         }
92         
93         return null;
94     }
95
96     class ImageFileFilter extends FileFilter JavaDoc
97     {
98         private String JavaDoc[] knownTypes = {
99                 ".gif",
100                 ".png",
101                 ".jpg",
102                 ".jpeg"
103         };
104         
105         public boolean accept(File JavaDoc f)
106         {
107             if(f.isDirectory())
108                 return true;
109             
110             String JavaDoc 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 JavaDoc getDescription()
121         {
122             return plugin.tr("filter.imageFiles");
123         }
124     }
125 }
Popular Tags