KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > Utils


1 package com.opensymphony.workflow.designer;
2
3 import java.awt.*;
4 import java.awt.image.BufferedImage JavaDoc;
5 import java.io.File JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8 import javax.swing.*;
9 import javax.swing.filechooser.FileFilter JavaDoc;
10
11 import com.opensymphony.workflow.loader.AbstractDescriptor;
12 import org.jgraph.JGraph;
13
14 /**
15  * @author Hani Suleiman (hani@formicary.net)
16  * Date: May 21, 2003
17  * Time: 12:12:44 AM
18  */

19 public class Utils
20 {
21   private static Map JavaDoc contexts = new HashMap JavaDoc();
22
23   public static File JavaDoc promptUserForFile(Component component, int type, boolean save, final String JavaDoc suffix, final String JavaDoc description)
24   {
25     JFileChooser chooser = new JFileChooser(Prefs.INSTANCE.get(Prefs.CURRENT_DIR, System.getProperty("user.dir")));
26     chooser.rescanCurrentDirectory();
27     if(type == JFileChooser.FILES_AND_DIRECTORIES)
28     {
29       FileFilter JavaDoc filter = new FileFilter JavaDoc()
30       {
31         public boolean accept(File JavaDoc f)
32         {
33           return f.isDirectory() || f.getName().toLowerCase().endsWith(suffix);
34         }
35
36         public String JavaDoc getDescription()
37         {
38           return description;
39         }
40       };
41       chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
42       chooser.setFileFilter(filter);
43     }
44     else
45     {
46       chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
47     }
48     File JavaDoc selectedFile = null;
49     int result;
50     if(save)
51     {
52       result = chooser.showSaveDialog(component);
53     }
54     else
55     {
56       result = chooser.showOpenDialog(component);
57     }
58
59     if(result == JFileChooser.APPROVE_OPTION)
60     {
61       selectedFile = chooser.getSelectedFile();
62       if(save && type == JFileChooser.FILES_AND_DIRECTORIES && !selectedFile.getName().toLowerCase().endsWith(suffix))
63       {
64         selectedFile = new File JavaDoc(selectedFile.toString() + suffix);
65       }
66     }
67     Prefs.INSTANCE.put(Prefs.CURRENT_DIR, chooser.getCurrentDirectory().toString());
68     return selectedFile;
69   }
70
71   public static void centerComponent(Component parent, Component child)
72   {
73     Point point = parent.getLocationOnScreen();
74     Dimension parentDim = parent.getSize();
75     Dimension childDim = child.getSize();
76     int x;
77     if(parentDim.width > childDim.width)
78       x = point.x + (parentDim.width - childDim.width) / 2;
79     else
80       x = point.x - (childDim.width - parentDim.width) / 2;
81     int y;
82     if(parentDim.height > childDim.height)
83       y = point.y + (parentDim.height - childDim.height) / 2;
84     else
85       y = point.y - (childDim.height - parentDim.height) / 2;
86     child.setLocation(x, y);
87   }
88
89   public static BufferedImage JavaDoc toImage(JGraph graph)
90   {
91     Object JavaDoc[] cells = graph.getRoots();
92     if(cells.length > 0)
93     {
94       Rectangle bounds = graph.getCellBounds(cells).getBounds();
95       graph.toScreen(bounds);
96
97       // Create a Buffered Image
98
Dimension d = bounds.getSize();
99       BufferedImage JavaDoc img = new BufferedImage JavaDoc(d.width + 10, d.height + 10, BufferedImage.TYPE_INT_RGB);
100       Graphics2D graphics = img.createGraphics();
101       graphics.setColor(graph.getBackground());
102       graphics.fillRect(0, 0, img.getWidth(), img.getHeight());
103       graphics.translate(-bounds.x + 5, -bounds.y + 5);
104
105       Object JavaDoc[] selection = graph.getSelectionCells();
106       boolean gridVisible = graph.isGridVisible();
107       graph.setGridVisible(false);
108       graph.clearSelection();
109
110       graph.paint(graphics);
111
112       graph.setSelectionCells(selection);
113       graph.setGridVisible(gridVisible);
114
115       return img;
116     }
117     return null;
118   }
119
120   public static void checkId(Object JavaDoc context, AbstractDescriptor descriptor)
121   {
122     if(descriptor == null) return;
123     Integer JavaDoc i = (Integer JavaDoc)contexts.get(context);
124     int nextId = i == null ? 0 : i.intValue();
125     if(descriptor.getId() >= nextId) nextId = descriptor.getId() + 1;
126     contexts.put(context, new Integer JavaDoc(nextId));
127   }
128
129   public static int getNextId(Object JavaDoc context)
130   {
131     Integer JavaDoc i = (Integer JavaDoc)contexts.get(context);
132     if(i == null) return 0;
133     return i.intValue();
134   }
135
136   public static void centerComponent(Component component)
137   {
138     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
139     Window window;
140     if(component instanceof Window)
141     {
142       window = (Window)component;
143     }
144     else
145     {
146       window = SwingUtilities.getWindowAncestor(component);
147     }
148     window.setLocation((screenSize.width - window.getSize().width) / 2, (screenSize.height - window.getSize().height) / 2);
149   }
150 }
151
Popular Tags