001: package com.opensymphony.workflow.designer;
002:
003: import java.awt.*;
004: import java.awt.image.BufferedImage;
005: import java.io.File;
006: import java.util.HashMap;
007: import java.util.Map;
008: import javax.swing.*;
009: import javax.swing.filechooser.FileFilter;
010:
011: import com.opensymphony.workflow.loader.AbstractDescriptor;
012: import org.jgraph.JGraph;
013:
014: /**
015: * @author Hani Suleiman (hani@formicary.net)
016: * Date: May 21, 2003
017: * Time: 12:12:44 AM
018: */
019: public class Utils {
020: private static Map contexts = new HashMap();
021:
022: public static File promptUserForFile(Component component, int type,
023: boolean save, final String suffix, final String description) {
024: JFileChooser chooser = new JFileChooser(Prefs.INSTANCE.get(
025: Prefs.CURRENT_DIR, System.getProperty("user.dir")));
026: chooser.rescanCurrentDirectory();
027: if (type == JFileChooser.FILES_AND_DIRECTORIES) {
028: FileFilter filter = new FileFilter() {
029: public boolean accept(File f) {
030: return f.isDirectory()
031: || f.getName().toLowerCase().endsWith(
032: suffix);
033: }
034:
035: public String getDescription() {
036: return description;
037: }
038: };
039: chooser
040: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
041: chooser.setFileFilter(filter);
042: } else {
043: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
044: }
045: File selectedFile = null;
046: int result;
047: if (save) {
048: result = chooser.showSaveDialog(component);
049: } else {
050: result = chooser.showOpenDialog(component);
051: }
052:
053: if (result == JFileChooser.APPROVE_OPTION) {
054: selectedFile = chooser.getSelectedFile();
055: if (save
056: && type == JFileChooser.FILES_AND_DIRECTORIES
057: && !selectedFile.getName().toLowerCase().endsWith(
058: suffix)) {
059: selectedFile = new File(selectedFile.toString()
060: + suffix);
061: }
062: }
063: Prefs.INSTANCE.put(Prefs.CURRENT_DIR, chooser
064: .getCurrentDirectory().toString());
065: return selectedFile;
066: }
067:
068: public static void centerComponent(Component parent, Component child) {
069: Point point = parent.getLocationOnScreen();
070: Dimension parentDim = parent.getSize();
071: Dimension childDim = child.getSize();
072: int x;
073: if (parentDim.width > childDim.width)
074: x = point.x + (parentDim.width - childDim.width) / 2;
075: else
076: x = point.x - (childDim.width - parentDim.width) / 2;
077: int y;
078: if (parentDim.height > childDim.height)
079: y = point.y + (parentDim.height - childDim.height) / 2;
080: else
081: y = point.y - (childDim.height - parentDim.height) / 2;
082: child.setLocation(x, y);
083: }
084:
085: public static BufferedImage toImage(JGraph graph) {
086: Object[] cells = graph.getRoots();
087: if (cells.length > 0) {
088: Rectangle bounds = graph.getCellBounds(cells).getBounds();
089: graph.toScreen(bounds);
090:
091: // Create a Buffered Image
092: Dimension d = bounds.getSize();
093: BufferedImage img = new BufferedImage(d.width + 10,
094: d.height + 10, BufferedImage.TYPE_INT_RGB);
095: Graphics2D graphics = img.createGraphics();
096: graphics.setColor(graph.getBackground());
097: graphics.fillRect(0, 0, img.getWidth(), img.getHeight());
098: graphics.translate(-bounds.x + 5, -bounds.y + 5);
099:
100: Object[] selection = graph.getSelectionCells();
101: boolean gridVisible = graph.isGridVisible();
102: graph.setGridVisible(false);
103: graph.clearSelection();
104:
105: graph.paint(graphics);
106:
107: graph.setSelectionCells(selection);
108: graph.setGridVisible(gridVisible);
109:
110: return img;
111: }
112: return null;
113: }
114:
115: public static void checkId(Object context,
116: AbstractDescriptor descriptor) {
117: if (descriptor == null)
118: return;
119: Integer i = (Integer) contexts.get(context);
120: int nextId = i == null ? 0 : i.intValue();
121: if (descriptor.getId() >= nextId)
122: nextId = descriptor.getId() + 1;
123: contexts.put(context, new Integer(nextId));
124: }
125:
126: public static int getNextId(Object context) {
127: Integer i = (Integer) contexts.get(context);
128: if (i == null)
129: return 0;
130: return i.intValue();
131: }
132:
133: public static void centerComponent(Component component) {
134: Dimension screenSize = Toolkit.getDefaultToolkit()
135: .getScreenSize();
136: Window window;
137: if (component instanceof Window) {
138: window = (Window) component;
139: } else {
140: window = SwingUtilities.getWindowAncestor(component);
141: }
142: window.setLocation(
143: (screenSize.width - window.getSize().width) / 2,
144: (screenSize.height - window.getSize().height) / 2);
145: }
146: }
|