/******************************* Copyright notice ******************************* Copyright (C)2002 by Andrei Loskutov <loskutov@gmx.de>. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ********************************************************************************/ package adiag.util; import java.awt.Component; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; import javax.swing.JFileChooser; import adiag.model.Version; import adiag.model.VersionComparator; import adiag.view.VersionView; /** * Tools class * @author Andrei Loskutov */ public final class Toolz { /** instance for sorting */ private static VersionComparator comp = new VersionComparator(); /** the clipboard - instance */ private static Clipboard clip; /** true, if JDK == 1.4.* */ private static Boolean is1_4vm; /** true, if JDK >= 1.3.* */ private static Boolean isVmOk; /** * don't initialize this ... */ private Toolz() { super(); } /** * See return description * @param vv VersionView any one version from branch * @return adiag.VersionView[] all known VersionView objects from same branch * or empty array */ public static VersionView[] getAllFromPath(VersionView vv) { Version v = Toolz.getFirst(vv.getVersion()); ArrayList al = new ArrayList(); al.add(v.getView()); while ((v = v.getNext()) != null) { al.add(v.getView()); } return (VersionView[]) al.toArray(new VersionView[al.size()]); } /** * See return description * @param vv VersionView any one version from branch * @return a list with all known VersionView objects from same branch or empty list */ public static ArrayList getAllFromPathAsList(VersionView vv) { Version v = Toolz.getFirst(vv.getVersion()); ArrayList al = new ArrayList(); al.add(v.getView()); while ((v = v.getNext()) != null) { al.add(v.getView()); } return al; } /** * See return description * @param v Version any one version from branch * @return Version the first Version (with the smallest version number) * from given branch */ public static Version getFirst(Version v) { Version p = v; while ((v = v.getPrev()) != null) { p = v; } return p; } /** * See return description * @param list java.util.ArrayList entries are Version objects * @return Version the smallest Version (with the smallest change number) * from given list or null, if list is empty */ public static Version getFirst(ArrayList list) { if (list.size() == 0) { return null; } ArrayList al = new ArrayList(); Object v; // make a copy from list for (int i = 0; i < list.size(); i++) { v = list.get(i); if (v != null) { al.add(v); } } if (al.size() == 0) { return null; } Collections.sort(al, comp); return (Version) al.get(0); } /** * See return description * @param c ArrayList, entries are Version objects * @param num the version number * @return Version with given version number,or null, if not found */ public static Version getForNummer(ArrayList c, int num) { if (c == null || c.size() == 0) { return null; } Version v = null; for (int i = 0; i < c.size(); i++) { v = (Version) c.get(i); if (num == v.getVersNumber()) { return v; } } return null; } /** Check String to match real path name * @return false if this path is may be not a File/Dir path, i.e. contains * not alloved characters etc. */ public static boolean isPath(String path) { if (path == null) { return false; } path = path.trim(); if(path.length() == 0 || path.length() > 400){ return false; } if (path.indexOf("\n") >= 0 // || path.indexOf("\t")>= 0 // || path.indexOf("\b")>= 0 // || path.indexOf("\r")>= 0 // || path.indexOf("\f")>= 0 || path.indexOf("*") >= 0 || path.indexOf("?") >= 0 || path.indexOf(":") >= 0 || path.indexOf(">") >= 0 || path.indexOf("<") >= 0 || path.indexOf("|") >= 0) { return false; } return true; } /** See return description * @param sa String array * @param value string to count * @return count number of occurrences of given string in given array */ public static int count(String[] sa, String value) { int c = 0; int length = sa.length; for (int i = 0; i < length; i++) { if (sa[i].equals(value)) c++; } return c; } /** Split one String into many, as separator is the " " * @param s string to split * @return array with substrings, at least with the same string */ public static String[] split(String s) { return split(s, " "); } /** Split one String into many, as separator is the " " * @param s string to split * @param delimiters any character in a string is separator * @return array with substrings, at least with the same string */ public static String[] split(String s, String delimiters) { StringTokenizer st = new StringTokenizer(s, delimiters); String[] arr = new String[st.countTokens()]; for (int i = 0; i < arr.length; i++) { arr[i] = st.nextToken(); } return arr; } /** See return description * @param strings array to search * @param string String to search * @return -1 if not found or one of arguments is null, * else index of given string in array */ public static int indexOfString(String[] strings, String string) { if (strings == null) { return -1; } return indexOfString(strings, string, 0, strings.length); } /** See return description * @param strings array to search * @param string String to search * @param from start index, inclusive * @param to stop index, exclusive * @return -1 if not found or one of arguments is null, * else index of given string in array */ public static int indexOfString(String[] strings, String string, int from, int to) { if (strings == null || string == null || from > strings.length || to < 0) return -1; to = to > strings.length ? strings.length : to; from = from < 0 ? 0 : from; for (int i = from; i < to; i++) if (string.equals(strings[i])) return i; return -1; } /** * See return description * @return int value of given string as int or 0, if it is not a int * @param s java.lang.String */ public static int intValue(String s) { int i = 0; try { i = Integer.parseInt(s); } catch (NumberFormatException e) { System.out.println(e); } return i; } /** * See return description * @return true, if operation system is Windows 9X or NT */ public static boolean isWin9xNT() { String os = System.getProperty("os.name", "unknown OS"); return os.indexOf("Windows") >= 0; } /** * See return description * @return true, if JRE Version >= 1.3 ist */ public static boolean isVmOk() { if(isVmOk == null){ String os = System.getProperty("java.vm.version", "1.2"); isVmOk = new Boolean(os.charAt(2) >= '3'); } return isVmOk.booleanValue(); } /** * See return description * @return true, wenn JRE Version >= 1.3 ist */ public static boolean is_1_4_Vm() { if(is1_4vm == null){ String os = System.getProperty("java.vm.version", "1.2"); is1_4vm = new Boolean(os.charAt(2) > '3'); } return is1_4vm.booleanValue(); } /** * See return description * @param arr the array with VersionView Objekts * @return smallest x, y coordinates or [0,0] if array is empty */ public static float[] findMin(VersionView[] arr) { if (arr == null || arr.length == 0) return new float[] { 0, 0 }; double xmin = arr[0].getX(); double ymin = arr[0].getY(); double x, y; for (int i = 1; i < arr.length; i++) { x = arr[i].getX(); if (x < xmin) xmin = x; y = arr[i].getY(); if (y < ymin) ymin = y; } return new float[] {(float) xmin, (float) ymin }; } /** See return description * @param e Throwable * @return stack trace from given Throwable as one String */ public static String getStackTrace(Throwable e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw, true)); return sw.toString(); } /** See return description * @param sa array * @param s String to remove from array * @return new array without given string */ public static String[] remove(String[] sa, String s) { ArrayList al = new ArrayList(Arrays.asList(sa)); String st = null; for (int i = al.size() - 1; i >= 0; i--) { st = al.get(i).toString(); if (st.equals(s)) { al.remove(i); } } return (String[]) al.toArray(new String[al.size()]); } /** * See return description * @param v Version[] array, not null * @return Version[] the same list, sorted */ public static Version[] sort(Version[] v) { Arrays.sort(v, comp); return v; } /** * See return description * @param v list with versions, not null * @return Version[] sorted array */ public static Version[] sort(ArrayList v) { return sort((Version[]) v.toArray(new Version[v.size()])); } /** Write string to file * @param s content to write * @param file file to write to * @throws Exception, if file not exist,cannot be open, is directory etc. */ public static void write(String s, File file) throws Exception { if (file == null) throw new Exception("Given file is null."); if (file.isDirectory()) throw new Exception( "Given file is directory, cannot write in:" + file.getPath()); if (file.exists() && !file.canWrite()) throw new Exception("Given file don't exist or you have not right to write in:" + file.getPath()); PrintWriter out; try { out = new PrintWriter(new BufferedWriter(new FileWriter(file)), false); } catch (IOException e) { e.printStackTrace(); throw new Exception("I/O error during write into file:" + file.getPath()); } out.print(s); out.flush(); out.close(); } /** * See return description * @param words strings to join * @param seperator between * @return one big line with given strings separated with given separator */ public static String makeLine(String[] words, String seperator) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < words.length; i++) { sb.append(words[i]); if (i < words.length - 1) sb.append(seperator); } return sb.toString(); } /** * @param startDir i.e. "." * @param dialogType JFileChooser.SAVE_DIALOG, JFileChooser.OPEN_DIALOG * @param exts allowed file extensions, i.e. EXTS_DIAG or null for all files allowed * @param desc file description, i.e. DESC_DIAG * @param multi allows multiple selection * @param dir JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES * @param parent user interface main frame or panel * @return file(s), that user select or empty array */ public static File[] getFilesFromChooser( String startDir, int dialogType, String[] exts, String[] desc, boolean multi, int dir, Component parent) { JFileChooser jfileDialog = new JFileChooser(startDir); // Bug??? in JFileChooser // wenn relative Pfade bergegeben werden, kann man nicht per Knopf rechts oben // in das Parent- Verzeichniss wechseln. // durch einmaliges hin und her geht es aber dann trotzdem File currDir = jfileDialog.getCurrentDirectory(); jfileDialog.changeToParentDirectory(); jfileDialog.setCurrentDirectory(currDir); jfileDialog.setDialogType(dialogType); if (exts == null || exts.length == 0) { jfileDialog.setFileFilter(jfileDialog.getAcceptAllFileFilter()); } else { String descr = ""; for (int i = 0; i < exts.length; i++) { descr = i < desc.length ? desc[i] : "'" + exts[i] + "'-files"; jfileDialog.addChoosableFileFilter(new MutableFilter(exts[i], descr)); } if (exts.length > 1 && dialogType == JFileChooser.OPEN_DIALOG) { descr = "All supported file types"; jfileDialog.addChoosableFileFilter(new MutableFilter(exts, descr)); } } jfileDialog.setMultiSelectionEnabled(multi); jfileDialog.setFileSelectionMode(dir); int optionSelected = JFileChooser.CANCEL_OPTION; if (dialogType == JFileChooser.OPEN_DIALOG) { optionSelected = jfileDialog.showOpenDialog(parent); } else if (dialogType == JFileChooser.SAVE_DIALOG) { optionSelected = jfileDialog.showSaveDialog(parent); } else { // default to an OPEN_DIALOG optionSelected = jfileDialog.showOpenDialog(parent); } if (optionSelected == JFileChooser.APPROVE_OPTION) { if (multi) { return jfileDialog.getSelectedFiles(); } else { return new File[] { jfileDialog.getSelectedFile()}; } } System.runFinalization(); System.gc(); return new File[0]; } /** * See return description * @return String current content of clipboard as string or empty string */ public static String getClipboardContent(){ if(clip == null){ clip = Toolkit.getDefaultToolkit().getSystemClipboard(); } String text = ""; if(clip != null){ Transferable t = clip.getContents(Toolz.class); try { Object o = t.getTransferData(DataFlavor.stringFlavor); if(o!= null){ text = o.toString().trim(); } } catch(UnsupportedFlavorException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } return text; } /** * Set given string to clipboard * @param String text */ public static void setClipboardContent(String text){ if(clip == null){ clip = Toolkit.getDefaultToolkit().getSystemClipboard(); } if(clip != null){ StringSelection string_with_clipboard = new StringSelection(text); clip.setContents(string_with_clipboard, string_with_clipboard); } } } /** Class for providing JFileChooser with a FileFilter */ class MutableFilter extends javax.swing.filechooser.FileFilter { /** extensions to accept */ private String[] acceptableExtension; /** description */ private String descriptor; /** * @param exts extension to accept * @param desc description */ public MutableFilter(String exts, String desc) { acceptableExtension = new String[] { exts }; StringBuffer strbDesc = new StringBuffer(desc + " ("); strbDesc.append("*." + exts); strbDesc.append(")"); descriptor = strbDesc.toString(); } /** * @param exts extensions to accept * @param desc description */ public MutableFilter(String[] exts, String desc) { acceptableExtension = exts; StringBuffer strbDesc = new StringBuffer(desc + " ("); for (int i = 0; i < exts.length; i++) { strbDesc.append("*." + exts[i]); } strbDesc.append(")"); descriptor = strbDesc.toString(); } /** See return description * @param file to test * @return true, if file match to filter, or is directory */ public boolean accept(File file) { if (file.isDirectory()) { return true; } String fileName = file.getName(); String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase(); if (fileExt != null) { for (int i = 0; i < acceptableExtension.length; i++) { if (fileExt.equals(acceptableExtension[i])) { return true; } } return false; } else { return false; } } /** * @return String description */ public String getDescription() { return descriptor; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 2460 | andrei_loskutov |
1) Change configuration: always reed properties 2) Change password store to hex format/password field 3) Create standalone/not standalone state for new branchview eclipse wrapper 4) Refactor all main classes to remove cyclic package dependency 5) Add workaround to fix new JDK 1.4.1 Swing bugs on JScrollPane/JViewPort 6) Add dispose methods to panel/connector 7) Minor internal GUI fixes/refactorings |
||
#1 | 2222 | andrei_loskutov |
#13.10.2002 22:16 Revision 1.2.1 1) Documentation task is finished... Yes!!! 2) New feature: copy P4 depot path 3) Some small fixes to menu shortkeys belongs ctrl + c and ctrl + d 4) Clipboard fix because of JDK 1.2 - 1.3 bug 5) Smallest refactoring on some methods. 6) Html help updated |