/******************************* 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.view; 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.Collections; import javax.swing.JFileChooser; import adiag.model.Version; import adiag.model.VersionComparator; /** * Tools class * @author Andrei Loskutov */ public final class Toolz { /** instance for sorting */ private static VersionComparator comp = new VersionComparator(); /** true, if JDK >= 1.3.* */ private static Boolean isVmOk; /** the clipboard - instance */ private static Clipboard clip; /** * don't initialize this ... */ private Toolz() { super(); } /** * 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 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(); Layout layout = LayoutManager.getLayout(v); al.add(layout.getView(v)); while ((v = v.getNext()) != null) { al.add(layout.getView(v)); } return (VersionView[]) al.toArray(new VersionView[al.size()]); } /** * 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; } /** 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 * @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 * @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(); } /** 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(); } /** * @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 | |
---|---|---|---|---|---|
#4 | 2735 | andrei_loskutov | sources are now included to binary distribution at http://andrei.gmxhome.de/perforce/index.html | ||
#3 | 2554 | andrei_loskutov |
#25.12.2002 23:29 Revision 1.3.0.4 beta 1) Some small refactoring 2) First integration with P4eclipse: properties etc, thanks Boris Pr�ssmann 5) Better integration to Eclipse: use Eclipse internal compare tool for diff. 6) Fix some minor bugs. #14.12.2002 23:15 Revision 1.3.0.3 beta 1) Fix endless loop bug if p4 path was not set. 2) Fix never getting focus bug after error message shown 3) Fix painting bug on JDK 1.4.1 (must be verified!!!) 4) Some small refactoring 5) Fix error handling. 6) Fix some minor bugs. |
||
#2 | 2552 | andrei_loskutov | 1.3.0.4 changes | ||
#1 | 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 |