Mercurial > hg > ltpda
view src/MPipeline2/src/mpipeline/utils/Utils.java @ 52:daf4eab1a51e database-connection-manager tip
Fix. Default password should be [] not an empty string
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 07 Dec 2011 17:29:47 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line source
/* * Copyright (c) 2009 Max-Planck-Gesellschaft, Martin Hewitson <martin.hewitson at aei.mpg.de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package mpipeline.utils; //import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; //import com.sun.org.apache.xml.internal.security.utils.Base64; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import java.io.FilenameFilter; import mpipeline.canvas.MNode; import java.awt.Component; import java.awt.Container; import java.awt.Dialog; import java.awt.FileDialog; import java.awt.Frame; import java.awt.Point; import java.awt.Rectangle; import java.awt.Window; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.zip.Deflater; import javax.swing.JDialog; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import mpipeline.main.MainWindow; /** * A utility class of static methods. * * @author hewitson */ public class Utils implements Serializable { /** * */ public static boolean DEBUG = false; /** * * @param s */ public static void msg(String s) { System.out.println("LWB: " + s); } /** * * @param s */ public static void dmsg(String s) { if (DEBUG) { System.out.println("(DEBUG) LWB: " + s); } } /** * * @return */ public boolean debugging() { return DEBUG; } /** * * @param table * @param padding */ public static void autoAdjustColumnSizes(final JTable table, final int padding) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); TableColumnModel colModel = table.getColumnModel(); for (int col = 0; col < colModel.getColumnCount(); col++) { int colMaxWidth = 0; for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer render = table.getCellRenderer(row, col); Object val = table.getValueAt(row, col); Component comp = render.getTableCellRendererComponent(table, val, false, false, row, col); colMaxWidth = Math.max(colMaxWidth, comp.getPreferredSize().width); } // end for row TableColumn column = colModel.getColumn(col); TableCellRenderer render = table.getTableHeader().getDefaultRenderer(); Object val = column.getHeaderValue(); Component comp = render.getTableCellRendererComponent(table, val, false, false, 0, col); colMaxWidth = Math.max(colMaxWidth, comp.getPreferredSize().width); column.setPreferredWidth(colMaxWidth + padding); } // end for col } /** * * @param file * @return */ public final static String removeFileExtension(String file) { int pos = file.lastIndexOf("."); return file.substring(0, pos); } /** * * @param path * @return */ public final static String removeFileName(String path) { // Find the last path seperator int pos = path.lastIndexOf(File.pathSeparator); if (pos != -1) { return path.substring(0, pos); } // Return an empty string, no path seperators return ""; } // public static String CompressString(String input) { // // try { // Deflater compressor = new Deflater(); // compressor.setLevel(Deflater.BEST_COMPRESSION); // // compressor.setInput(input.getBytes("UTF-8")); // compressor.finish(); // // ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length()); // // // Compress the data // byte[] buf = new byte[1024]; // while (!compressor.finished()) { // int count = compressor.deflate(buf); // bos.write(buf, 0, count); // } // // bos.close(); // // // Get the compressed data // byte[] bytes = bos.toByteArray(); // return (Base64.encode(bytes)); // } catch (IOException ex) { // Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); // return null; // } // } // // public static String DecompressString(String compressedString) throws Base64DecodingException { // // try { // Inflater decompressor = new Inflater(); // decompressor.setInput(Base64.decode(compressedString)); // // ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedString.length()); // // Decompress the data // byte[] buf = new byte[1024]; // int i = 0; // while ((!decompressor.finished()) && (i < 100)) { // int count = decompressor.inflate(buf); // bos.write(buf, 0, count); // i++; // } // decompressor.end(); // bos.close(); // // // Get the decompressed data // return (bos.toString()); // } catch (DataFormatException ex) { // Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); // return null; // } catch (IOException ex) { // Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); // return null; // } // } // /** * * @return */ public static MainWindow getMainWindow() { Frame[] frames = Frame.getFrames(); for (int k = 0; k < frames.length; k++) { Frame fr = frames[k]; if (fr instanceof MainWindow) { return (MainWindow) fr; } } return null; } /** * Get the extension of a file. * @param f * @return */ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i + 1).toLowerCase(); } return ext; } /** * * @param s1 * @param s2 * @return */ public static String makeDiagramTitle(String s1, String s2) { if (s1 == null || s1.equals("")) { return s2; } else { return s1 + " / " + s2; } } /** * Return a set of control points suitable for drawing a pipe * between the source and destination nodes. The 'midoffset' parameter * puts an x-offset on the center portion of the pipe. The 'offset' * parameter specifys the overshoot for pipes that need to go around * blocks. * * @param src * @param dst * @param midoffset * @param offset * @return */ public static ArrayList<Point> getControlPointsFromNodes(MNode src, MNode dst, int midoffset, int offset) { ArrayList<Point> points = new ArrayList<Point>(); int R = dst.getRadius(); int w = 0; int h = 0; int sx = src.getX(); int sy = src.getY(); int dsx = dst.getX(); int dsy = dst.getY(); if (dsx > sx) { // case 1 or 2 if (dsy > sy) { // case 1 w = dsx + dst.getWidth() - sx; h = dsy + dst.getHeight() - sy; // set control nodes points.clear(); points.add(new Point(R, R)); points.add(new Point(Math.round(w / 2) + midoffset, R)); points.add(new Point(Math.round(w / 2) + midoffset, h - R)); points.add(new Point(w - R, h - R)); } else { // case 2 w = dsx - sx + 2 * R; h = sy - dsy + 2 * R; // set control nodes points.clear(); points.add(new Point(R, h - R)); points.add(new Point(Math.round(w / 2) + midoffset, h - R)); points.add(new Point(Math.round(w / 2) + midoffset, R)); points.add(new Point(w - R, R)); } } else { // case 3 if (dsy > sy) { w = sx + 2 * R + 2 * offset - dsx; h = dsy + 2 * R - sy; // set control points points.clear(); points.add(new Point(w - offset - R, R)); points.add(new Point(w - R + midoffset, R)); points.add(new Point(w - R + midoffset, midoffset + Math.round(h / 2))); points.add(new Point(R - midoffset, midoffset + Math.round(h / 2))); points.add(new Point(R - midoffset, h - R)); points.add(new Point(offset + R, h - R)); } else { w = sx + 2 * R + 2 * offset - dsx; h = sy + 2 * R - dsy; // set control nodes points.clear(); points.add(new Point(w - offset - R, h - R)); points.add(new Point(w - R + midoffset, h - R)); points.add(new Point(w - R + midoffset, midoffset + Math.round(h / 2))); points.add(new Point(R - midoffset, midoffset + Math.round(h / 2))); points.add(new Point(R - midoffset, R)); points.add(new Point(R + offset, R)); } } return points; } /** * Return a set of control points suitable for drawing a pipe * between the source and destination points. The 'midoffset' parameter * puts an x-offset on the center portion of the pipe. The 'offset' * parameter specifys the overshoot for pipes that need to go around * blocks. * * @param src * @param dst * @param midoffset * @param offset * @return */ public static ArrayList<Point> getControlPointsFromPoints(Point src, Point dst, int midoffset, int offset) { ArrayList<Point> points = new ArrayList<Point>(); int w = 0; int h = 0; int sx = src.x; int sy = src.y; int dx = dst.x; int dy = dst.y; if (dx > sx) { w = dx - sx; h = dy - sy; // set control nodes points.clear(); points.add(new Point(sx, sy)); points.add(new Point(sx + Math.round(w / 2) + midoffset, sy)); points.add(new Point(sx + Math.round(w / 2) + midoffset, dy)); points.add(new Point(dx, dy)); } else { h = (dy - sy); // set control points points.clear(); points.add(new Point(sx, sy)); points.add(new Point(sx + offset + midoffset, sy)); points.add(new Point(sx + offset + midoffset, sy + Math.round(h / 2))); points.add(new Point(dx - offset - midoffset, sy + Math.round(h / 2))); points.add(new Point(dx - offset - midoffset, dy)); points.add(new Point(dx, dy)); } return points; } /** * Return a rectangle that bounds a pipe between two nodes. The 'offset' * parameter specifys the overshoot for pipes that need to go around * blocks. * * @param src * @param dst * @param offset * @return */ public static Rectangle getBoundsFromNodes(MNode src, MNode dst, int offset) { int R = dst.getRadius(); int x = 0; int y = 0; int w = 0; int h = 0; int sx = src.getX(); int sy = src.getY(); int dsx = dst.getX(); int dsy = dst.getY(); if (dsx > sx) { // case 1 or 2 if (dsy > sy) { // case 1 x = sx; y = sy; w = dsx + dst.getWidth() - sx; h = dsy + dst.getHeight() - sy; } else { // case 2 x = sx; y = dsy; w = dsx - sx + 2 * R; h = sy - dsy + 2 * R; } } else { // case 3 if (dsy > sy) { x = dsx - offset; y = sy; w = sx + offset + 2 * R - x; h = dsy + 2 * R - y; } else { x = dsx - offset; y = dsy; w = sx + 2 * R + offset - x; h = sy + 2 * R - y; } } return new Rectangle(x, y, w, h); } /** * * @param input * @return */ public static boolean isInteger(String input) { // System.out.println("Checking if " + input + " is an integer"); try { Integer.parseInt(input); return true; } catch (NumberFormatException e) { return false; } } /** * * @param bname * @return */ public static String incrementName(String bname) { // look for signature _[0-9]+ int idx = bname.lastIndexOf("_"); if (idx > 0) { int sval = -1; if (isInteger(bname.substring(idx + 1))) { sval = Integer.parseInt(bname.substring(idx + 1)); } // System.out.println("string has end value: " + sval); if (sval > 0) { sval++; bname = bname.substring(0, idx) + "_" + sval; } } // System.out.println("updated string to " + bname); return bname; } /** * * @param name * @return */ public static String checkName(String name) { String myName = name; if ((name != null) && (name.length() > 0)) { myName = name.replaceAll("[^\\sa-zA-Z0-9]+", "_"); // myName = myName.replaceAll("\\W", "_"); myName = myName.replaceAll("^_+", ""); // remove _ from beginning of string myName = myName.replaceAll("_+$", "");// remove _ from end of string if (isInteger(myName.substring(0, 1))) { myName = "x" + myName; System.err.println("Block names can't begin with a number. Prepended 'x'."); } if (!myName.equals(name)) { System.err.println("Some not allowed characters were replaced by _"); } } return myName; } /** * * @param parent * @param ext * @param filename * @return */ public static File getFileToLoad(Container parent, String[] ext, String filename) { Utils.dmsg("Getting file to load..."); File f = null; boolean isMacOSX = false; String systemName = System.getProperty("os.name"); if (systemName.equals("Mac OS X")) { isMacOSX = true; } final String[] lext = ext; if (isMacOSX) { Utils.dmsg(" ... with OS X file dialog"); System.setProperty("apple.awt.fileDialogForDirectories", "false"); // try to figure out a parent for the dialog Window w = SwingUtilities.windowForComponent(parent); // System.out.println("window " + w.toString()); Dialog dparent = null; if (w instanceof JDialog) { dparent = (JDialog)w; } else { // System.out.println("Parent is not a JDialog"); } // System.out.println("Opening dialog with parent " + dparent.toString()); FileDialog chooser = new FileDialog(dparent); chooser.setMode(FileDialog.LOAD); if (filename != null) { File startFile = new File(filename); chooser.setDirectory(startFile.getParent()); } chooser.setFilenameFilter(new FilenameFilter() { public boolean accept(File dir, String name) { if (lext != null) { for (int kk = 0; kk < lext.length; kk++) { if (lext[kk].equals("")) { return true; } if (name.endsWith(lext[kk])) { return true; } } } else { return true; } return false; } }); if (filename != null) { // Utils.msg("Setting file path: " + filename); File nf = new File(filename); chooser.setDirectory(Utils.removeFileName(filename)); chooser.setFile(nf.getName()); } chooser.setVisible(true); String selecteddir = chooser.getDirectory(); String selectedFile = chooser.getFile(); if (selectedFile != null) { f = new File(selecteddir + File.separator + selectedFile); } } else { JFileChooser chooser = new JFileChooser("Load..."); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setMultiSelectionEnabled(false); if (filename != null) { chooser.setSelectedFile(new File(filename)); } chooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } if (lext == null) { return true; } for (int kk = 0; kk < lext.length; kk++) { if (lext[kk].equals("")) { return true; } if (f.getAbsolutePath().endsWith(lext[kk])) { return true; } } return false; } @Override public String getDescription() { if (lext == null) { return "All Files"; } String s = ""; for (int kk = 0; kk < lext.length; kk++) { s += "*." + lext[kk] + ","; } if (s.length() == 0) { return s; } return s.substring(0, s.length() - 1); } }); int retVal = chooser.showOpenDialog(parent); if (retVal == JFileChooser.APPROVE_OPTION) { f = chooser.getSelectedFile(); } } if (f != null) { if (lext != null) { if (Utils.getExtension(f) == null && lext.length > 0 && !lext[0].equals("")) { f = new File(f.getAbsolutePath() + "." + lext[0]); } } } return f; } /** * * @param ext * @param filename * @return */ public static File getDirectory(Container parent, String directory) { Utils.dmsg("Getting directory ..."); File f = null; boolean isMacOSX = false; String systemName = System.getProperty("os.name"); if (systemName.equals("Mac OS X")) { isMacOSX = true; } if (isMacOSX) { Utils.dmsg(" ... with OS X file dialog"); System.setProperty("apple.awt.fileDialogForDirectories", "true"); // try to figure out a parent for the dialog Window w = SwingUtilities.windowForComponent(parent); Dialog dparent = null; if (w instanceof JDialog) { dparent = (JDialog)w; } FileDialog chooser = new FileDialog(dparent); chooser.setMode(FileDialog.LOAD); if (directory != null) { // Utils.msg("Setting file path from last load: " + lastLoadPath.getAbsolutePath()); chooser.setDirectory(directory); } chooser.setVisible(true); String selecteddir = chooser.getDirectory(); String selectedFile = chooser.getFile(); if (selectedFile != null) { f = new File(selecteddir + File.separator + selectedFile); } } else { JFileChooser chooser = new JFileChooser("Load..."); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setMultiSelectionEnabled(false); if (directory != null) { chooser.setCurrentDirectory(new File(directory)); } else { chooser.setCurrentDirectory(new File("")); } int retVal = chooser.showOpenDialog(parent); if (retVal == JFileChooser.APPROVE_OPTION) { f = chooser.getSelectedFile(); } } return f; } /** * * @param ext * @param filename * @return */ public static File getFileToSave(Container parent, String[] ext, String filename) { Utils.dmsg("Getting file to save..."); File f = null; final String[] lext = ext; boolean isMacOSX = false; String systemName = System.getProperty("os.name"); if (systemName.equals("Mac OS X")) { isMacOSX = true; } if (isMacOSX) { Utils.dmsg(" ... with OS X file dialog"); System.setProperty("apple.awt.fileDialogForDirectories", "false"); // try to figure out a parent for the dialog Window w = SwingUtilities.windowForComponent(parent); Dialog dparent = null; if (w instanceof JDialog) { dparent = (JDialog)w; } FileDialog chooser = new FileDialog(dparent); chooser.setMode(FileDialog.SAVE); if (filename != null) { File startFile = new File(filename); chooser.setDirectory(startFile.getParent()); } chooser.setFilenameFilter(new FilenameFilter() { public boolean accept(File dir, String name) { for (int kk = 0; kk < lext.length; kk++) { String cext = lext[kk]; if (cext.startsWith(".")) { cext = cext.substring(1); } if (name.endsWith(cext)) { return true; } } return false; } }); if (filename != null) { File nf = new File(filename); chooser.setDirectory(Utils.removeFileName(filename)); chooser.setFile(nf.getName()); } chooser.setVisible(true); String selecteddir = chooser.getDirectory(); String selectedFile = chooser.getFile(); if (selectedFile != null) { f = new File(selecteddir + File.separator + selectedFile); } } else { JFileChooser chooser = new JFileChooser("Save as..."); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setMultiSelectionEnabled(false); if (filename != null) { chooser.setSelectedFile(new File(filename)); } chooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } for (int kk = 0; kk < lext.length; kk++) { if (f.getAbsolutePath().endsWith(lext[kk])) { return true; } } return false; } @Override public String getDescription() { String s = ""; for (int kk = 0; kk < lext.length; kk++) { s += "*." + lext[kk] + ","; } if (s.length() == 0) { return s; } return s.substring(0, s.length() - 1); } }); int retVal = chooser.showSaveDialog(parent); if (retVal == JFileChooser.APPROVE_OPTION) { f = chooser.getSelectedFile(); } } if (f != null) { if (Utils.getExtension(f) == null && lext.length > 0) { f = new File(f.getAbsolutePath() + "." + lext[0]); } Utils.dmsg(" ... got: " + f.getAbsolutePath()); } return f; } }