Beispiel #1
0
  /**
   * Returns JPicEdt's install directory w/o trailing "/", provided the command line looks like :
   *
   * <p><code>java -classpath other-class-paths:jpicedt-install-dir/lib/jpicedt.jar jpicedt.JPicEdt
   * </code> (where <code>/</code> may be replaced by the actual respective separator for files on
   * the underlying platform).
   *
   * <p>For Windows platform, the install directory is tried to be detected 1st with the MSWindows
   * file-separator (<code>\</code>), and if this does not work, a subsequent trial is made with
   * <code>/</code>.
   *
   * <p>That is, the old way (java -jar jpicedt.jar) won't work. However, classpath can contain
   * relative pathname (then user.dir get prepended).
   *
   * <p>Code snippet was adapted from jEdit/JEdit.java (http://www.jedit.org).
   *
   * @return the value of the "user.dir" Java property if "lib/jpicedt.jar" wasn't found in the
   *     command line.
   */
  public static String getJPicEdtHome() {

    String classpath = System.getProperty("java.class.path"); // e.g.
    // "/usr/lib/java/jre/lib/rt.jar:/home/me/jpicedt/1.3.2/lib/jpicedt.jar"
    // File.separator = "/" on Unix, "\\" on Windows,...
    String fileSeparator = File.separator;
    int index;
    // File.pathSeparator = ":" on Unix/MacOS-X platforms, ";" on Windows
    // search ":" backward starting from
    // "/usr/lib/java/jre/lib/rt.jar:/home/me/jpicedt/1.3.2/^lib/jpicedt.jar"

    String homeDir = null;
    int trials = 2;
    do {
      index = classpath.toLowerCase().indexOf("lib" + fileSeparator + "jpicedt.jar");
      int start = classpath.lastIndexOf(File.pathSeparator, index);
      if (start == -1)
        start =
            0; // File.pathSeparator not found => lib/jpicedt.jar probably at beginning of classpath
      else start += File.pathSeparator.length(); // e.g. ":^/home..."

      if (index >= start) {
        homeDir = classpath.substring(start, index);
        if (homeDir.endsWith(fileSeparator)) homeDir = homeDir.substring(0, homeDir.length() - 1);
      }
      switch (trials) {
        case 2:
          if (File.pathSeparator.equals(";") && homeDir == null) {
            // MS-Windows case, this must work both with / and \
            trials = 1;
            fileSeparator = "/";
          } else trials = 0;
          break;
        case 1:
          if (homeDir != null && !fileSeparator.equals(File.separator)) {
            homeDir.replace(fileSeparator, File.separator);
          }
          trials = 0;
          break;

        default:
          trials = 0;
          break;
      }
    } while (trials != 0);

    if (homeDir != null) {
      if (homeDir.equals("")) homeDir = System.getProperty("user.dir");
      else if (!new File(homeDir).isAbsolute())
        homeDir = System.getProperty("user.dir") + File.separator + homeDir;
    } else {
      homeDir = System.getProperty("user.dir");
      if (homeDir.endsWith(
          "lib")) // this is the case if jpicedt run as "java -jar jpicedt.jar" from inside lib/ dir
      homeDir = homeDir.substring(0, homeDir.lastIndexOf("lib"));
    }

    // System.out.println("JPicEdt's home = " + homeDir);
    return homeDir;
  }
Beispiel #2
0
 /** Return the platform standard tmp dir, or null if none is standardly defined. */
 public static File getOSTmpDir() {
   // Note : default tmp dir can be obtained through :
   // System.getProperty("java.io.tmp")
   String tmp = System.getProperty("java.io.tmp");
   if (tmp == null) return null;
   else return new File(tmp);
 }
Beispiel #3
0
  /**
   * @return the clipboard content as a String (DataFlavor.stringFlavor) Code snippet adapted from
   *     jEdit (Registers.java), http://www.jedit.org. Returns null if clipboard is empty.
   */
  public static String getClipboardStringContent(Clipboard clipboard) {
    // Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    try {
      String selection =
          (String) (clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
      if (selection == null) return null;

      boolean trailingEOL =
          (selection.endsWith("\n") || selection.endsWith(System.getProperty("line.separator")));

      // Some Java versions return the clipboard contents using the native line separator,
      // so have to convert it here , see jEdit's "registers.java"
      BufferedReader in = new BufferedReader(new StringReader(selection));
      StringBuffer buf = new StringBuffer();
      String line;
      while ((line = in.readLine()) != null) {
        buf.append(line);
        buf.append('\n');
      }
      // remove trailing \n
      if (!trailingEOL) buf.setLength(buf.length() - 1);
      return buf.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Beispiel #4
0
 private String generateOverviewText() throws InsufficientDataException {
   StringBuilder sb = new StringBuilder();
   final String team = config.getTeam();
   double total = checkTotal();
   final String nl = System.getProperty("line.separator");
   for (Entry<String, Row> entry : rows.entrySet()) {
     double hours = Double.parseDouble(entry.getValue().hoursTF.getText());
     double fraction = hours / total;
     if (fraction < 0.004) continue;
     String line = team + ", " + decimalFormat.format(fraction) + ", " + entry.getKey();
     sb.append(line + nl);
   }
   return sb.toString();
 }
 public static String getHomePath() {
   String home = System.getProperty("HOME");
   if (home == null) {
     try {
       String thisPath = SystemUtils.class.getResource("SystemUtils.class").getPath();
       String thisPkg = SystemUtils.class.getPackage().getName();
       home = thisPath.substring(0, thisPath.indexOf(thisPkg.substring(0, thisPkg.indexOf('.'))));
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   if (home == null) home = "";
   return home;
 }
Beispiel #6
0
  private void setupFlavors() {
    //  If it's NOT Windows
    _windows = System.getProperty("os.name").indexOf("indows") != -1;

    //  Deprecated, generally unused, but deprecation didn't provide
    // a useful alternative w/o rewriting a lot of code, so I'm keeping it for now.
    if (_plainFlavor == null) {
      //      _plainFlavor = DataFlavor.plainTextFlavor;
      _plainFlavor = DataFlavor.getTextPlainUnicodeFlavor();
    }

    _isoFlavor = getDataFlavor(_isoFlavor, "isoFlavor");
    _ascFlavor = getDataFlavor(_ascFlavor, "ascFlavor");
    _pl2Flavor = getDataFlavor(_pl2Flavor, "pl2Flavor");
    _htmlFlavor = getDataFlavor(_htmlFlavor, "htmlFlavor");
    _utf8HtmlFlavor = getDataFlavor(_htmlFlavor, "UTF8Html");
    _thtmlFlavor = getDataFlavor(_htmlFlavor, "thtmlFlavor");
    _urlFlavor = getDataFlavor(_urlFlavor, "urlFlavor");
  }
/**
 * A reader for console applications. It supports custom tab-completion, saveable command history,
 * and command line editing. On some platforms, platform-specific commands will need to be issued
 * before the reader will function properly. See {@link Terminal#initializeTerminal} for convenience
 * methods for issuing platform-specific setup commands.
 *
 * @author <a href="mailto:[email protected]">Marc Prud'hommeaux</a>
 */
public class ConsoleReader implements ConsoleOperations {

  static final int TAB_WIDTH = 4;

  String prompt;

  private boolean useHistory = true;

  private boolean usePagination = false;

  public static final String CR = System.getProperty("line.separator");

  private static ResourceBundle loc =
      ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName());

  /** Map that contains the operation name to keymay operation mapping. */
  public static SortedMap KEYMAP_NAMES;

  static {
    Map names = new TreeMap();

    names.put("MOVE_TO_BEG", new Short(MOVE_TO_BEG));
    names.put("MOVE_TO_END", new Short(MOVE_TO_END));
    names.put("PREV_CHAR", new Short(PREV_CHAR));
    names.put("NEWLINE", new Short(NEWLINE));
    names.put("KILL_LINE", new Short(KILL_LINE));
    names.put("PASTE", new Short(PASTE));
    names.put("CLEAR_SCREEN", new Short(CLEAR_SCREEN));
    names.put("NEXT_HISTORY", new Short(NEXT_HISTORY));
    names.put("PREV_HISTORY", new Short(PREV_HISTORY));
    names.put("START_OF_HISTORY", new Short(START_OF_HISTORY));
    names.put("END_OF_HISTORY", new Short(END_OF_HISTORY));
    names.put("REDISPLAY", new Short(REDISPLAY));
    names.put("KILL_LINE_PREV", new Short(KILL_LINE_PREV));
    names.put("DELETE_PREV_WORD", new Short(DELETE_PREV_WORD));
    names.put("NEXT_CHAR", new Short(NEXT_CHAR));
    names.put("REPEAT_PREV_CHAR", new Short(REPEAT_PREV_CHAR));
    names.put("SEARCH_PREV", new Short(SEARCH_PREV));
    names.put("REPEAT_NEXT_CHAR", new Short(REPEAT_NEXT_CHAR));
    names.put("SEARCH_NEXT", new Short(SEARCH_NEXT));
    names.put("PREV_SPACE_WORD", new Short(PREV_SPACE_WORD));
    names.put("TO_END_WORD", new Short(TO_END_WORD));
    names.put("REPEAT_SEARCH_PREV", new Short(REPEAT_SEARCH_PREV));
    names.put("PASTE_PREV", new Short(PASTE_PREV));
    names.put("REPLACE_MODE", new Short(REPLACE_MODE));
    names.put("SUBSTITUTE_LINE", new Short(SUBSTITUTE_LINE));
    names.put("TO_PREV_CHAR", new Short(TO_PREV_CHAR));
    names.put("NEXT_SPACE_WORD", new Short(NEXT_SPACE_WORD));
    names.put("DELETE_PREV_CHAR", new Short(DELETE_PREV_CHAR));
    names.put("ADD", new Short(ADD));
    names.put("PREV_WORD", new Short(PREV_WORD));
    names.put("CHANGE_META", new Short(CHANGE_META));
    names.put("DELETE_META", new Short(DELETE_META));
    names.put("END_WORD", new Short(END_WORD));
    names.put("NEXT_CHAR", new Short(NEXT_CHAR));
    names.put("INSERT", new Short(INSERT));
    names.put("REPEAT_SEARCH_NEXT", new Short(REPEAT_SEARCH_NEXT));
    names.put("PASTE_NEXT", new Short(PASTE_NEXT));
    names.put("REPLACE_CHAR", new Short(REPLACE_CHAR));
    names.put("SUBSTITUTE_CHAR", new Short(SUBSTITUTE_CHAR));
    names.put("TO_NEXT_CHAR", new Short(TO_NEXT_CHAR));
    names.put("UNDO", new Short(UNDO));
    names.put("NEXT_WORD", new Short(NEXT_WORD));
    names.put("DELETE_NEXT_CHAR", new Short(DELETE_NEXT_CHAR));
    names.put("CHANGE_CASE", new Short(CHANGE_CASE));
    names.put("COMPLETE", new Short(COMPLETE));
    names.put("EXIT", new Short(EXIT));
    names.put("CLEAR_LINE", new Short(CLEAR_LINE));

    KEYMAP_NAMES = new TreeMap(Collections.unmodifiableMap(names));
  }

  /** The map for logical operations. */
  private final short[] keybindings;

  /** If true, issue an audible keyboard bell when appropriate. */
  private boolean bellEnabled = true;

  /** The current character mask. */
  private Character mask = null;

  /** The null mask. */
  private static final Character NULL_MASK = new Character((char) 0);

  /**
   * The number of tab-completion candidates above which a warning will be prompted before showing
   * all the candidates.
   */
  private int autoprintThreshhold =
      Integer.getInteger("jline.completion.threshold", 100).intValue(); // same default as

  // bash

  /** The Terminal to use. */
  private final Terminal terminal;

  private CompletionHandler completionHandler = new CandidateListCompletionHandler();

  InputStream in;

  final Writer out;

  final CursorBuffer buf = new CursorBuffer();

  static PrintWriter debugger;

  History history = new History();

  final List completors = new LinkedList();

  private Character echoCharacter = null;

  private Map triggeredActions = new HashMap();

  /**
   * Adding a triggered Action allows to give another curse of action if a character passed the
   * preprocessing.
   *
   * <p>Say you want to close the application if the user enter q. addTriggerAction('q', new
   * ActionListener(){ System.exit(0); }); would do the trick.
   *
   * @param c
   * @param listener
   */
  public void addTriggeredAction(char c, ActionListener listener) {
    triggeredActions.put(new Character(c), listener);
  }

  /**
   * Create a new reader using {@link FileDescriptor#in} for input and {@link System#out} for
   * output. {@link FileDescriptor#in} is used because it has a better chance of being unbuffered.
   */
  public ConsoleReader() throws IOException {
    this(new FileInputStream(FileDescriptor.in), new PrintWriter(System.out));
  }

  /**
   * Create a new reader using the specified {@link InputStream} for input and the specific writer
   * for output, using the default keybindings resource.
   */
  public ConsoleReader(final InputStream in, final Writer out) throws IOException {
    this(in, out, null);
  }

  public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings)
      throws IOException {
    this(in, out, bindings, Terminal.getTerminal());
  }

  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }

  public Terminal getTerminal() {
    return this.terminal;
  }

  /** Set the stream for debugging. Development use only. */
  public void setDebug(final PrintWriter debugger) {
    ConsoleReader.debugger = debugger;
  }

  /** Set the stream to be used for console input. */
  public void setInput(final InputStream in) {
    this.in = in;
  }

  /** Returns the stream used for console input. */
  public InputStream getInput() {
    return this.in;
  }

  /** Read the next line and return the contents of the buffer. */
  public String readLine() throws IOException {
    return readLine((String) null);
  }

  /**
   * Read the next line with the specified character mask. If null, then characters will be echoed.
   * If 0, then no characters will be echoed.
   */
  public String readLine(final Character mask) throws IOException {
    return readLine(null, mask);
  }

  /** @param bellEnabled if true, enable audible keyboard bells if an alert is required. */
  public void setBellEnabled(final boolean bellEnabled) {
    this.bellEnabled = bellEnabled;
  }

  /** @return true is audible keyboard bell is enabled. */
  public boolean getBellEnabled() {
    return this.bellEnabled;
  }

  /**
   * Query the terminal to find the current width;
   *
   * @see Terminal#getTerminalWidth
   * @return the width of the current terminal.
   */
  public int getTermwidth() {
    return Terminal.setupTerminal().getTerminalWidth();
  }

  /**
   * Query the terminal to find the current width;
   *
   * @see Terminal#getTerminalHeight
   * @return the height of the current terminal.
   */
  public int getTermheight() {
    return Terminal.setupTerminal().getTerminalHeight();
  }

  /** @param autoprintThreshhold the number of candidates to print without issuing a warning. */
  public void setAutoprintThreshhold(final int autoprintThreshhold) {
    this.autoprintThreshhold = autoprintThreshhold;
  }

  /** @return the number of candidates to print without issing a warning. */
  public int getAutoprintThreshhold() {
    return this.autoprintThreshhold;
  }

  int getKeyForAction(short logicalAction) {
    for (int i = 0; i < keybindings.length; i++) {
      if (keybindings[i] == logicalAction) {
        return i;
      }
    }

    return -1;
  }

  /** Clear the echoed characters for the specified character code. */
  int clearEcho(int c) throws IOException {
    // if the terminal is not echoing, then just return...
    if (!terminal.getEcho()) {
      return 0;
    }

    // otherwise, clear
    int num = countEchoCharacters((char) c);
    back(num);
    drawBuffer(num);

    return num;
  }

  int countEchoCharacters(char c) {
    // tabs as special: we need to determine the number of spaces
    // to cancel based on what out current cursor position is
    if (c == 9) {
      int tabstop = 8; // will this ever be different?
      int position = getCursorPosition();

      return tabstop - (position % tabstop);
    }

    return getPrintableCharacters(c).length();
  }

  /**
   * Return the number of characters that will be printed when the specified character is echoed to
   * the screen. Adapted from cat by Torbjorn Granlund, as repeated in stty by David MacKenzie.
   */
  StringBuffer getPrintableCharacters(char ch) {
    StringBuffer sbuff = new StringBuffer();

    if (ch >= 32) {
      if (ch < 127) {
        sbuff.append(ch);
      } else if (ch == 127) {
        sbuff.append('^');
        sbuff.append('?');
      } else {
        sbuff.append('M');
        sbuff.append('-');

        if (ch >= (128 + 32)) {
          if (ch < (128 + 127)) {
            sbuff.append((char) (ch - 128));
          } else {
            sbuff.append('^');
            sbuff.append('?');
          }
        } else {
          sbuff.append('^');
          sbuff.append((char) (ch - 128 + 64));
        }
      }
    } else {
      sbuff.append('^');
      sbuff.append((char) (ch + 64));
    }

    return sbuff;
  }

  int getCursorPosition() {
    // FIXME: does not handle anything but a line with a prompt
    // absolute position
    return ((prompt == null) ? 0 : prompt.length()) + buf.cursor;
  }

  public String readLine(final String prompt) throws IOException {
    return readLine(prompt, null);
  }

  /** The default prompt that will be issued. */
  public void setDefaultPrompt(String prompt) {
    this.prompt = prompt;
  }

  /** The default prompt that will be issued. */
  public String getDefaultPrompt() {
    return prompt;
  }

  /**
   * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing
   * newlines).
   *
   * @param prompt the prompt to issue to the console, may be null.
   * @return a line that is read from the terminal, or null if there was null input (e.g.,
   *     <i>CTRL-D</i> was pressed).
   */
  public String readLine(final String prompt, final Character mask) throws IOException {
    this.mask = mask;
    if (prompt != null) this.prompt = prompt;

    try {
      terminal.beforeReadLine(this, this.prompt, mask);

      if ((this.prompt != null) && (this.prompt.length() > 0)) {
        out.write(this.prompt);
        out.flush();
      }

      // if the terminal is unsupported, just use plain-java reading
      if (!terminal.isSupported()) {
        return readLine(in);
      }

      while (true) {
        int[] next = readBinding();

        if (next == null) {
          return null;
        }

        int c = next[0];
        int code = next[1];

        if (c == -1) {
          return null;
        }

        boolean success = true;

        switch (code) {
          case EXIT: // ctrl-d
            if (buf.buffer.length() == 0) {
              return null;
            }

          case COMPLETE: // tab
            success = complete();
            break;

          case MOVE_TO_BEG:
            success = setCursorPosition(0);
            break;

          case KILL_LINE: // CTRL-K
            success = killLine();
            break;

          case CLEAR_SCREEN: // CTRL-L
            success = clearScreen();
            break;

          case KILL_LINE_PREV: // CTRL-U
            success = resetLine();
            break;

          case NEWLINE: // enter
            moveToEnd();
            printNewline(); // output newline
            return finishBuffer();

          case DELETE_PREV_CHAR: // backspace
            success = backspace();
            break;

          case DELETE_NEXT_CHAR: // delete
            success = deleteCurrentCharacter();
            break;

          case MOVE_TO_END:
            success = moveToEnd();
            break;

          case PREV_CHAR:
            success = moveCursor(-1) != 0;
            break;

          case NEXT_CHAR:
            success = moveCursor(1) != 0;
            break;

          case NEXT_HISTORY:
            success = moveHistory(true);
            break;

          case PREV_HISTORY:
            success = moveHistory(false);
            break;

          case REDISPLAY:
            break;

          case PASTE:
            success = paste();
            break;

          case DELETE_PREV_WORD:
            success = deletePreviousWord();
            break;

          case PREV_WORD:
            success = previousWord();
            break;

          case NEXT_WORD:
            success = nextWord();
            break;

          case START_OF_HISTORY:
            success = history.moveToFirstEntry();
            if (success) setBuffer(history.current());
            break;

          case END_OF_HISTORY:
            success = history.moveToLastEntry();
            if (success) setBuffer(history.current());
            break;

          case CLEAR_LINE:
            moveInternal(-(buf.buffer.length()));
            killLine();
            break;

          case INSERT:
            buf.setOvertyping(!buf.isOvertyping());
            break;

          case UNKNOWN:
          default:
            if (c != 0) { // ignore null chars
              ActionListener action =
                  (ActionListener) triggeredActions.get(new Character((char) c));
              if (action != null) action.actionPerformed(null);
              else putChar(c, true);
            } else success = false;
        }

        if (!(success)) {
          beep();
        }

        flushConsole();
      }
    } finally {
      terminal.afterReadLine(this, this.prompt, mask);
    }
  }

  private String readLine(InputStream in) throws IOException {
    StringBuffer buf = new StringBuffer();

    while (true) {
      int i = in.read();

      if ((i == -1) || (i == '\n') || (i == '\r')) {
        return buf.toString();
      }

      buf.append((char) i);
    }

    // return new BufferedReader (new InputStreamReader (in)).readLine ();
  }

  /** Reads the console input and returns an array of the form [raw, key binding]. */
  private int[] readBinding() throws IOException {
    int c = readVirtualKey();

    if (c == -1) {
      return null;
    }

    // extract the appropriate key binding
    short code = keybindings[c];

    if (debugger != null) {
      debug("    translated: " + (int) c + ": " + code);
    }

    return new int[] {c, code};
  }

  /**
   * Move up or down the history tree.
   *
   * @param direction less than 0 to move up the tree, down otherwise
   */
  private final boolean moveHistory(final boolean next) throws IOException {
    if (next && !history.next()) {
      return false;
    } else if (!next && !history.previous()) {
      return false;
    }

    setBuffer(history.current());

    return true;
  }

  /**
   * Paste the contents of the clipboard into the console buffer
   *
   * @return true if clipboard contents pasted
   */
  public boolean paste() throws IOException {
    Clipboard clipboard;
    try { // May throw ugly exception on system without X
      clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    } catch (Exception e) {
      return false;
    }

    if (clipboard == null) {
      return false;
    }

    Transferable transferable = clipboard.getContents(null);

    if (transferable == null) {
      return false;
    }

    try {
      Object content = transferable.getTransferData(DataFlavor.plainTextFlavor);

      /*
       * This fix was suggested in bug #1060649 at
       * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056
       * to get around the deprecated DataFlavor.plainTextFlavor, but it
       * raises a UnsupportedFlavorException on Mac OS X
       */
      if (content == null) {
        try {
          content = new DataFlavor().getReaderForText(transferable);
        } catch (Exception e) {
        }
      }

      if (content == null) {
        return false;
      }

      String value;

      if (content instanceof Reader) {
        // TODO: we might want instead connect to the input stream
        // so we can interpret individual lines
        value = "";

        String line = null;

        for (BufferedReader read = new BufferedReader((Reader) content);
            (line = read.readLine()) != null; ) {
          if (value.length() > 0) {
            value += "\n";
          }

          value += line;
        }
      } else {
        value = content.toString();
      }

      if (value == null) {
        return true;
      }

      putString(value);

      return true;
    } catch (UnsupportedFlavorException ufe) {
      if (debugger != null) debug(ufe + "");

      return false;
    }
  }

  /**
   * Kill the buffer ahead of the current cursor position.
   *
   * @return true if successful
   */
  public boolean killLine() throws IOException {
    int cp = buf.cursor;
    int len = buf.buffer.length();

    if (cp >= len) {
      return false;
    }

    int num = buf.buffer.length() - cp;
    clearAhead(num);

    for (int i = 0; i < num; i++) {
      buf.buffer.deleteCharAt(len - i - 1);
    }

    return true;
  }

  /** Clear the screen by issuing the ANSI "clear screen" code. */
  public boolean clearScreen() throws IOException {
    if (!terminal.isANSISupported()) {
      return false;
    }

    // send the ANSI code to clear the screen
    printString(((char) 27) + "[2J");
    flushConsole();

    // then send the ANSI code to go to position 1,1
    printString(((char) 27) + "[1;1H");
    flushConsole();

    redrawLine();

    return true;
  }

  /**
   * Use the completors to modify the buffer with the appropriate completions.
   *
   * @return true if successful
   */
  private final boolean complete() throws IOException {
    // debug ("tab for (" + buf + ")");
    if (completors.size() == 0) {
      return false;
    }

    List candidates = new LinkedList();
    String bufstr = buf.buffer.toString();
    int cursor = buf.cursor;

    int position = -1;

    for (Iterator i = completors.iterator(); i.hasNext(); ) {
      Completor comp = (Completor) i.next();

      if ((position = comp.complete(bufstr, cursor, candidates)) != -1) {
        break;
      }
    }

    // no candidates? Fail.
    if (candidates.size() == 0) {
      return false;
    }

    return completionHandler.complete(this, candidates, position);
  }

  public CursorBuffer getCursorBuffer() {
    return buf;
  }

  /**
   * Output the specified {@link Collection} in proper columns.
   *
   * @param stuff the stuff to print
   */
  public void printColumns(final Collection stuff) throws IOException {
    if ((stuff == null) || (stuff.size() == 0)) {
      return;
    }

    int width = getTermwidth();
    int maxwidth = 0;

    for (Iterator i = stuff.iterator();
        i.hasNext();
        maxwidth = Math.max(maxwidth, i.next().toString().length())) {;
    }

    StringBuffer line = new StringBuffer();

    int showLines;

    if (usePagination) showLines = getTermheight() - 1; // page limit
    else showLines = Integer.MAX_VALUE;

    for (Iterator i = stuff.iterator(); i.hasNext(); ) {
      String cur = (String) i.next();

      if ((line.length() + maxwidth) > width) {
        printString(line.toString().trim());
        printNewline();
        line.setLength(0);
        if (--showLines == 0) { // Overflow
          printString(loc.getString("display-more"));
          flushConsole();
          int c = readVirtualKey();
          if (c == '\r' || c == '\n') showLines = 1; // one step forward
          else if (c != 'q') showLines = getTermheight() - 1; // page forward

          back(loc.getString("display-more").length());
          if (c == 'q') break; // cancel
        }
      }

      pad(cur, maxwidth + 3, line);
    }

    if (line.length() > 0) {
      printString(line.toString().trim());
      printNewline();
      line.setLength(0);
    }
  }

  /**
   * Append <i>toPad</i> to the specified <i>appendTo</i>, as well as (<i>toPad.length () - len</i>)
   * spaces.
   *
   * @param toPad the {@link String} to pad
   * @param len the target length
   * @param appendTo the {@link StringBuffer} to which to append the padded {@link String}.
   */
  private final void pad(final String toPad, final int len, final StringBuffer appendTo) {
    appendTo.append(toPad);

    for (int i = 0; i < (len - toPad.length()); i++, appendTo.append(' ')) {;
    }
  }

  /**
   * Add the specified {@link Completor} to the list of handlers for tab-completion.
   *
   * @param completor the {@link Completor} to add
   * @return true if it was successfully added
   */
  public boolean addCompletor(final Completor completor) {
    return completors.add(completor);
  }

  /**
   * Remove the specified {@link Completor} from the list of handlers for tab-completion.
   *
   * @param completor the {@link Completor} to remove
   * @return true if it was successfully removed
   */
  public boolean removeCompletor(final Completor completor) {
    return completors.remove(completor);
  }

  /** Returns an unmodifiable list of all the completors. */
  public Collection getCompletors() {
    return Collections.unmodifiableList(completors);
  }

  /**
   * Erase the current line.
   *
   * @return false if we failed (e.g., the buffer was empty)
   */
  final boolean resetLine() throws IOException {
    if (buf.cursor == 0) {
      return false;
    }

    backspaceAll();

    return true;
  }

  /** Move the cursor position to the specified absolute index. */
  public final boolean setCursorPosition(final int position) throws IOException {
    return moveCursor(position - buf.cursor) != 0;
  }

  /**
   * Set the current buffer's content to the specified {@link String}. The visual console will be
   * modified to show the current buffer.
   *
   * @param buffer the new contents of the buffer.
   */
  private final void setBuffer(final String buffer) throws IOException {
    // don't bother modifying it if it is unchanged
    if (buffer.equals(buf.buffer.toString())) {
      return;
    }

    // obtain the difference between the current buffer and the new one
    int sameIndex = 0;

    for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) && (i < l2); i++) {
      if (buffer.charAt(i) == buf.buffer.charAt(i)) {
        sameIndex++;
      } else {
        break;
      }
    }

    int diff = buf.buffer.length() - sameIndex;

    backspace(diff); // go back for the differences
    killLine(); // clear to the end of the line
    buf.buffer.setLength(sameIndex); // the new length
    putString(buffer.substring(sameIndex)); // append the differences
  }

  /** Clear the line and redraw it. */
  public final void redrawLine() throws IOException {
    printCharacter(RESET_LINE);
    flushConsole();
    drawLine();
  }

  /** Output put the prompt + the current buffer */
  public final void drawLine() throws IOException {
    if (prompt != null) {
      printString(prompt);
    }

    printString(buf.buffer.toString());

    if (buf.length() != buf.cursor) // not at end of line
    back(buf.length() - buf.cursor); // sync
  }

  /** Output a platform-dependant newline. */
  public final void printNewline() throws IOException {
    printString(CR);
    flushConsole();
  }

  /**
   * Clear the buffer and add its contents to the history.
   *
   * @return the former contents of the buffer.
   */
  final String finishBuffer() {
    String str = buf.buffer.toString();

    // we only add it to the history if the buffer is not empty
    // and if mask is null, since having a mask typically means
    // the string was a password. We clear the mask after this call
    if (str.length() > 0) {
      if (mask == null && useHistory) {
        history.addToHistory(str);
      } else {
        mask = null;
      }
    }

    history.moveToEnd();

    buf.buffer.setLength(0);
    buf.cursor = 0;

    return str;
  }

  /** Write out the specified string to the buffer and the output stream. */
  public final void putString(final String str) throws IOException {
    buf.write(str);
    printString(str);
    drawBuffer();
  }

  /** Output the specified string to the output stream (but not the buffer). */
  public final void printString(final String str) throws IOException {
    printCharacters(str.toCharArray());
  }

  /** Output the specified character, both to the buffer and the output stream. */
  private final void putChar(final int c, final boolean print) throws IOException {
    buf.write((char) c);

    if (print) {
      // no masking...
      if (mask == null) {
        printCharacter(c);
      }
      // null mask: don't print anything...
      else if (mask.charValue() == 0) {;
      }
      // otherwise print the mask...
      else {
        printCharacter(mask.charValue());
      }

      drawBuffer();
    }
  }

  /**
   * Redraw the rest of the buffer from the cursor onwards. This is necessary for inserting text
   * into the buffer.
   *
   * @param clear the number of characters to clear after the end of the buffer
   */
  private final void drawBuffer(final int clear) throws IOException {
    // debug ("drawBuffer: " + clear);
    char[] chars = buf.buffer.substring(buf.cursor).toCharArray();
    if (mask != null) Arrays.fill(chars, mask.charValue());

    printCharacters(chars);

    clearAhead(clear);
    back(chars.length);
    flushConsole();
  }

  /**
   * Redraw the rest of the buffer from the cursor onwards. This is necessary for inserting text
   * into the buffer.
   */
  private final void drawBuffer() throws IOException {
    drawBuffer(0);
  }

  /** Clear ahead the specified number of characters without moving the cursor. */
  private final void clearAhead(final int num) throws IOException {
    if (num == 0) {
      return;
    }

    // debug ("clearAhead: " + num);

    // print blank extra characters
    printCharacters(' ', num);

    // we need to flush here so a "clever" console
    // doesn't just ignore the redundancy of a space followed by
    // a backspace.
    flushConsole();

    // reset the visual cursor
    back(num);

    flushConsole();
  }

  /** Move the visual cursor backwards without modifying the buffer cursor. */
  private final void back(final int num) throws IOException {
    printCharacters(BACKSPACE, num);
    flushConsole();
  }

  /** Issue an audible keyboard bell, if {@link #getBellEnabled} return true. */
  public final void beep() throws IOException {
    if (!(getBellEnabled())) {
      return;
    }

    printCharacter(KEYBOARD_BELL);
    // need to flush so the console actually beeps
    flushConsole();
  }

  /**
   * Output the specified character to the output stream without manipulating the current buffer.
   */
  private final void printCharacter(final int c) throws IOException {
    if (c == '\t') {
      char cbuf[] = new char[TAB_WIDTH];
      Arrays.fill(cbuf, ' ');
      out.write(cbuf);
      return;
    }

    out.write(c);
  }

  /**
   * Output the specified characters to the output stream without manipulating the current buffer.
   */
  private final void printCharacters(final char[] c) throws IOException {
    int len = 0;
    for (int i = 0; i < c.length; i++)
      if (c[i] == '\t') len += TAB_WIDTH;
      else len++;

    char cbuf[];
    if (len == c.length) cbuf = c;
    else {
      cbuf = new char[len];
      int pos = 0;
      for (int i = 0; i < c.length; i++) {
        if (c[i] == '\t') {
          Arrays.fill(cbuf, pos, pos + TAB_WIDTH, ' ');
          pos += TAB_WIDTH;
        } else {
          cbuf[pos] = c[i];
          pos++;
        }
      }
    }

    out.write(cbuf);
  }

  private final void printCharacters(final char c, final int num) throws IOException {
    if (num == 1) {
      printCharacter(c);
    } else {
      char[] chars = new char[num];
      Arrays.fill(chars, c);
      printCharacters(chars);
    }
  }

  /**
   * Flush the console output stream. This is important for printout out single characters (like a
   * backspace or keyboard) that we want the console to handle immedately.
   */
  public final void flushConsole() throws IOException {
    out.flush();
  }

  private final int backspaceAll() throws IOException {
    return backspace(Integer.MAX_VALUE);
  }

  /**
   * Issue <em>num</em> backspaces.
   *
   * @return the number of characters backed up
   */
  private final int backspace(final int num) throws IOException {
    if (buf.cursor == 0) {
      return 0;
    }

    int count = 0;

    count = moveCursor(-1 * num) * -1;
    // debug ("Deleting from " + buf.cursor + " for " + count);
    buf.buffer.delete(buf.cursor, buf.cursor + count);
    drawBuffer(count);

    return count;
  }

  /**
   * Issue a backspace.
   *
   * @return true if successful
   */
  public final boolean backspace() throws IOException {
    return backspace(1) == 1;
  }

  private final boolean moveToEnd() throws IOException {
    if (moveCursor(1) == 0) {
      return false;
    }

    while (moveCursor(1) != 0) {;
    }

    return true;
  }

  /** Delete the character at the current position and redraw the remainder of the buffer. */
  private final boolean deleteCurrentCharacter() throws IOException {
    boolean success = buf.buffer.length() > 0;
    if (!success) {
      return false;
    }

    if (buf.cursor == buf.buffer.length()) {
      return false;
    }

    buf.buffer.deleteCharAt(buf.cursor);
    drawBuffer(1);
    return true;
  }

  private final boolean previousWord() throws IOException {
    while (isDelimiter(buf.current()) && (moveCursor(-1) != 0)) {;
    }

    while (!isDelimiter(buf.current()) && (moveCursor(-1) != 0)) {;
    }

    return true;
  }

  private final boolean nextWord() throws IOException {
    while (isDelimiter(buf.current()) && (moveCursor(1) != 0)) {;
    }

    while (!isDelimiter(buf.current()) && (moveCursor(1) != 0)) {;
    }

    return true;
  }

  private final boolean deletePreviousWord() throws IOException {
    while (isDelimiter(buf.current()) && backspace()) {;
    }

    while (!isDelimiter(buf.current()) && backspace()) {;
    }

    return true;
  }

  /**
   * Move the cursor <i>where</i> characters.
   *
   * @param where if less than 0, move abs(<i>where</i>) to the left, otherwise move <i>where</i> to
   *     the right.
   * @return the number of spaces we moved
   */
  public final int moveCursor(final int num) throws IOException {
    int where = num;

    if ((buf.cursor == 0) && (where < 0)) {
      return 0;
    }

    if ((buf.cursor == buf.buffer.length()) && (where > 0)) {
      return 0;
    }

    if ((buf.cursor + where) < 0) {
      where = -buf.cursor;
    } else if ((buf.cursor + where) > buf.buffer.length()) {
      where = buf.buffer.length() - buf.cursor;
    }

    moveInternal(where);

    return where;
  }

  /**
   * debug.
   *
   * @param str the message to issue.
   */
  public static void debug(final String str) {
    if (debugger != null) {
      debugger.println(str);
      debugger.flush();
    }
  }

  /**
   * Move the cursor <i>where</i> characters, withough checking the current buffer.
   *
   * @see #where
   * @param where the number of characters to move to the right or left.
   */
  private final void moveInternal(final int where) throws IOException {
    // debug ("move cursor " + where + " ("
    // + buf.cursor + " => " + (buf.cursor + where) + ")");
    buf.cursor += where;

    char c;

    if (where < 0) {
      int len = 0;
      for (int i = buf.cursor; i < buf.cursor - where; i++) {
        if (buf.getBuffer().charAt(i) == '\t') len += TAB_WIDTH;
        else len++;
      }

      char cbuf[] = new char[len];
      Arrays.fill(cbuf, BACKSPACE);
      out.write(cbuf);

      return;
    } else if (buf.cursor == 0) {
      return;
    } else if (mask != null) {
      c = mask.charValue();
    } else {
      printCharacters(buf.buffer.substring(buf.cursor - where, buf.cursor).toCharArray());
      return;
    }

    // null character mask: don't output anything
    if (NULL_MASK.equals(mask)) {
      return;
    }

    printCharacters(c, Math.abs(where));
  }

  /**
   * Read a character from the console.
   *
   * @return the character, or -1 if an EOF is received.
   */
  public final int readVirtualKey() throws IOException {
    int c = terminal.readVirtualKey(in);

    if (debugger != null) {
      debug("keystroke: " + c + "");
    }

    // clear any echo characters
    clearEcho(c);

    return c;
  }

  public final int readCharacter(final char[] allowed) throws IOException {
    // if we restrict to a limited set and the current character
    // is not in the set, then try again.
    char c;

    Arrays.sort(allowed); // always need to sort before binarySearch

    while (Arrays.binarySearch(allowed, c = (char) readVirtualKey()) < 0) ;

    return c;
  }

  /**
   * Issue <em>num</em> deletes.
   *
   * @return the number of characters backed up
   */
  private final int delete(final int num) throws IOException {
    /* Commented out beacuse of DWA-2949:
    if (buf.cursor == 0)
                return 0;*/

    buf.buffer.delete(buf.cursor, buf.cursor + 1);
    drawBuffer(1);

    return 1;
  }

  public final boolean replace(int num, String replacement) {
    buf.buffer.replace(buf.cursor - num, buf.cursor, replacement);
    try {
      moveCursor(-num);
      drawBuffer(Math.max(0, num - replacement.length()));
      moveCursor(replacement.length());
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * Issue a delete.
   *
   * @return true if successful
   */
  public final boolean delete() throws IOException {
    return delete(1) == 1;
  }

  public void setHistory(final History history) {
    this.history = history;
  }

  public History getHistory() {
    return this.history;
  }

  public void setCompletionHandler(final CompletionHandler completionHandler) {
    this.completionHandler = completionHandler;
  }

  public CompletionHandler getCompletionHandler() {
    return this.completionHandler;
  }

  /**
   * Set the echo character. For example, to have "*" entered when a password is typed:
   *
   * <pre>
   * myConsoleReader.setEchoCharacter(new Character('*'));
   * </pre>
   *
   * <p>Setting the character to
   *
   * <pre>
   * null
   * </pre>
   *
   * will restore normal character echoing. Setting the character to
   *
   * <pre>
   * new Character(0)
   * </pre>
   *
   * will cause nothing to be echoed.
   *
   * @param echoCharacter the character to echo to the console in place of the typed character.
   */
  public void setEchoCharacter(final Character echoCharacter) {
    this.echoCharacter = echoCharacter;
  }

  /** Returns the echo character. */
  public Character getEchoCharacter() {
    return this.echoCharacter;
  }

  /** No-op for exceptions we want to silently consume. */
  private void consumeException(final Throwable e) {}

  /**
   * Checks to see if the specified character is a delimiter. We consider a character a delimiter if
   * it is anything but a letter or digit.
   *
   * @param c the character to test
   * @return true if it is a delimiter
   */
  private boolean isDelimiter(char c) {
    return !Character.isLetterOrDigit(c);
  }

  /** Whether or not to add new commands to the history buffer. */
  public void setUseHistory(boolean useHistory) {
    this.useHistory = useHistory;
  }

  /** Whether or not to add new commands to the history buffer. */
  public boolean getUseHistory() {
    return useHistory;
  }

  /**
   * Whether to use pagination when the number of rows of candidates exceeds the height of the
   * temrinal.
   */
  public void setUsePagination(boolean usePagination) {
    this.usePagination = usePagination;
  }

  /**
   * Whether to use pagination when the number of rows of candidates exceeds the height of the
   * temrinal.
   */
  public boolean getUsePagination() {
    return this.usePagination;
  }
}
  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }
Beispiel #9
0
  /**
   * The constructor creates all the components for the main Artemis window and sets up all the menu
   * callbacks.
   */
  public ArtemisMain(final String args[]) {
    super("Artemis", "Artemis", version);

    ActionListener menu_listener =
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            if (filemanager == null) filemanager = new FileManager(ArtemisMain.this);
            else filemanager.setVisible(true);
          }
        };
    makeMenuItem(file_menu, "Open File Manager ...", menu_listener);

    ActionListener menu_listener_ssh =
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            if (fm == null) fm = new LocalAndRemoteFileManager(ArtemisMain.this);
            else fm.setVisible(true);
          }
        };

    if (System.getProperty("chado") != null)
      makeMenuItem(file_menu, "Open Database and SSH File Manager ...", menu_listener_ssh);
    else makeMenuItem(file_menu, "Open SSH File Manager ...", menu_listener_ssh);

    final EntrySourceVector entry_sources = getEntrySources(this);

    for (int source_index = 0; source_index < entry_sources.size(); ++source_index) {
      final EntrySource this_entry_source = entry_sources.elementAt(source_index);

      String entry_source_name = this_entry_source.getSourceName();
      String menu_name = null;

      if (entry_source_name.equals("Filesystem")) menu_name = "Open ...";
      else menu_name = "Open from " + entry_source_name + " ...";

      menu_listener =
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              getEntryEditFromEntrySource(this_entry_source);
            }
          };
      makeMenuItem(file_menu, menu_name, menu_listener);
    }

    menu_listener =
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            launchDatabaseJFrame();
          }
        };

    // final boolean sanger_options =
    //  Options.getOptions().getPropertyTruthValue("sanger_options");

    // makeMenuItem(file_menu, "Database Entry ...", menu_listener);

    menu_listener =
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            exit();
          }
        };
    makeMenuItem(file_menu, "Quit", menu_listener);

    getCanvas()
        .addMouseListener(
            new MouseAdapter() {
              /** Listen for mouse press events so that we can do popup menus and selection. */
              public void mousePressed(MouseEvent event) {
                handleCanvasMousePress(event);
              }
            });
  }
Beispiel #10
0
  /** Read the entries named in args and in the diana.ini file. */
  protected void readArgsAndOptions(final String[] args) {
    if (args.length == 0) {
      if (System.getProperty("chado") != null && (args.length < 1 || args[0].indexOf(':') == -1))
        fm = new LocalAndRemoteFileManager(ArtemisMain.this);

      // open the entries given in the options file(diana.ini)
      readDefaultEntries();
      return;
    }

    if (args[0].equals("-biojava")) {
      handleBioJava(args);
      return;
    }

    final EntryInformation artemis_entry_information = Options.getArtemisEntryInformation();

    EntryEdit last_entry_edit = null;
    boolean seen_plus = false;

    for (int i = 0; i < args.length; ++i) {
      String new_entry_name = args[i];

      if (new_entry_name.length() == 0) continue;

      if (new_entry_name.equals("+")) {
        seen_plus = true;
        continue;
      }

      if (new_entry_name.startsWith("+") && last_entry_edit != null || seen_plus) {
        // new feature file

        final Document entry_document;

        if (seen_plus) entry_document = DocumentFactory.makeDocument(new_entry_name);
        else entry_document = DocumentFactory.makeDocument(new_entry_name.substring(1));

        final InputStreamProgressListener progress_listener = getInputStreamProgressListener();

        entry_document.addInputStreamProgressListener(progress_listener);

        final uk.ac.sanger.artemis.io.Entry new_embl_entry =
            EntryFileDialog.getEntryFromFile(
                this, entry_document, artemis_entry_information, false);

        if (new_embl_entry == null) // the read failed
        break;

        try {
          final Entry new_entry =
              new Entry(last_entry_edit.getEntryGroup().getBases(), new_embl_entry);

          last_entry_edit.getEntryGroup().add(new_entry);
        } catch (OutOfRangeException e) {
          new MessageDialog(
              this,
              "read failed: one of the features in "
                  + new_entry_name
                  + " has an out of range "
                  + "location: "
                  + e.getMessage());
        }
      } else if (System.getProperty("chado") != null && new_entry_name.indexOf(':') > -1) {
        // open from database e.g. Pfalciparum:Pf3D7_09:95000..150000
        Splash.logger4j.info("OPEN ENTRY " + new_entry_name);
        getStatusLabel().setText("Connecting ...");
        DatabaseEntrySource entry_source = new DatabaseEntrySource();

        boolean promptUser = true;
        if (System.getProperty("read_only") != null) {
          promptUser = false;
          entry_source.setReadOnly(true);
        }

        if (!entry_source.setLocation(promptUser)) return;

        last_entry_edit =
            DatabaseJPanel.show(
                entry_source, this, getInputStreamProgressListener(), new_entry_name);
      } else {
        // new sequence file

        if (last_entry_edit != null) {
          last_entry_edit.setVisible(true);
          last_entry_edit = null;
        }

        if (new_entry_name.indexOf("://") == -1) {
          File file = new File(new_entry_name);
          if (!file.exists()) {
            JOptionPane.showMessageDialog(
                null,
                "File " + new_entry_name + " not found.\n" + "Check the file name.",
                "File Not Found",
                JOptionPane.WARNING_MESSAGE);
          }
        }

        final Document entry_document = DocumentFactory.makeDocument(new_entry_name);

        entry_document.addInputStreamProgressListener(getInputStreamProgressListener());

        final uk.ac.sanger.artemis.io.Entry new_embl_entry =
            EntryFileDialog.getEntryFromFile(
                this, entry_document, artemis_entry_information, false);

        if (new_embl_entry == null) // the read failed
        break;

        try {
          final Entry entry = new Entry(new_embl_entry);
          last_entry_edit = makeEntryEdit(entry);
          addEntryEdit(last_entry_edit);
          getStatusLabel().setText("");
        } catch (OutOfRangeException e) {
          new MessageDialog(
              this,
              "read failed: one of the features in "
                  + new_entry_name
                  + " has an out of range "
                  + "location: "
                  + e.getMessage());
          break;
        } catch (NoSequenceException e) {
          new MessageDialog(this, "read failed: " + new_entry_name + " contains no sequence");
          break;
        }
      }
    }

    if (System.getProperty("offset") != null)
      last_entry_edit.getGotoEventSource().gotoBase(Integer.parseInt(System.getProperty("offset")));

    last_entry_edit.setVisible(true);
    for (int entry_index = 0; entry_index < entry_edit_objects.size(); ++entry_index) {
      if (System.getProperty("offset") != null)
        entry_edit_objects
            .elementAt(entry_index)
            .getGotoEventSource()
            .gotoBase(Integer.parseInt(System.getProperty("offset")));
    }
  }
 public static boolean isJava7() {
   return System.getProperty("java.version").startsWith("1.7");
 }
  /**
   * Try to determine whether this application is running under Windows or some other platform by
   * examining the "os.name" property.
   */
  static {
    String os = System.getProperty("os.name");
    // String version = System.getProperty("os.version"); // for Win7, reports "6.0" on JDK7, should
    // be "6.1"; for Win8, reports "6.2" as of JDK7u17

    if (SystemUtils.startsWithIgnoreCase(os, "windows 7"))
      isWin7 = true; // reports "Windows Vista" on JDK7
    else if (SystemUtils.startsWithIgnoreCase(os, "windows 8"))
      isWin7 = true; // reports "Windows 8" as of JDK7u17
    else if (SystemUtils.startsWithIgnoreCase(os, "windows vista")) isVista = true;
    else if (SystemUtils.startsWithIgnoreCase(os, "windows xp")) isWinXP = true;
    else if (SystemUtils.startsWithIgnoreCase(os, "windows 2000")) isWin2k = true;
    else if (SystemUtils.startsWithIgnoreCase(os, "windows nt")) isWinNT = true;
    else if (SystemUtils.startsWithIgnoreCase(os, "windows"))
      isWin9X = true; // win95 or win98 (what about WinME?)
    else if (SystemUtils.startsWithIgnoreCase(os, "mac")) isMac = true;
    else if (SystemUtils.startsWithIgnoreCase(os, "so")) isSolaris = true; // sunos or solaris
    else if (os.equalsIgnoreCase("linux")) isLinux = true;
    else isUnix = true; // assume UNIX, e.g. AIX, HP-UX, IRIX

    String osarch = System.getProperty("os.arch");
    String arch = (osarch != null && osarch.contains("64")) ? "_x64" /* eg. 'amd64' */ : "_x32";
    String syslib = SYSLIB + arch;

    try { // loading a native lib in a static initializer ensures that it is available before any
          // method in this class is called:
      System.loadLibrary(syslib);
      System.out.println(
          "Done loading '" + System.mapLibraryName(syslib) + "', PID=" + getProcessID());
    } catch (Error e) {
      System.err.println(
          "Native library '"
              + System.mapLibraryName(syslib)
              + "' not found in 'java.library.path': "
              + System.getProperty("java.library.path"));
      throw e; // re-throw
    }

    if (isWinPlatform()) {
      System.setProperty(
          "line.separator", "\n"); // so we won't have to mess with DOS line endings ever again
      comSpec =
          getEnv(
              "comSpec"); // use native method here since getEnvironmentVariable() needs to know
                          // comSpec
      comSpec = (comSpec != null) ? comSpec + " /c " : "";

      try (BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  RUNTIME.exec(comSpec + "ver").getInputStream()))) { // fix for Win7,8
        for (String line = null; (line = br.readLine()) != null; ) {
          if (isVista && (line.contains("6.1" /*Win7*/) || line.contains("6.2" /*Win8*/))) {
            isVista = false;
            isWin7 = true;
          }
        }
      } catch (IOException e) {
        e.printStackTrace();
      }

      String cygdir = getEnv("cygdir"); // this is set during CygWin install to "?:/cygwin/bin"
      isCygWin = (cygdir != null && !cygdir.equals("%cygdir%"));
      cygstartPath = cygdir + "/cygstart.exe"; // path to CygWin's cygutils' "cygstart" binary

      if (getDebug() && Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        for (Desktop.Action action : Desktop.Action.values())
          System.out.println(
              "Desktop action " + action + " supported?  " + desktop.isSupported(action));
      }
    }
  }
    public void actionPerformed(ActionEvent e) {
      if (isDirectorySelected()) {
        File dir = getDirectory();
        if (dir != null) {
          try {
            // Strip trailing ".."
            dir = ShellFolder.getNormalizedFile(dir);
          } catch (IOException ex) {
            // Ok, use f as is
          }
          changeDirectory(dir);
          return;
        }
      }

      JFileChooser chooser = getFileChooser();

      String filename = getFileName();
      FileSystemView fs = chooser.getFileSystemView();
      File dir = chooser.getCurrentDirectory();

      if (filename != null) {
        // Remove whitespaces from end of filename
        int i = filename.length() - 1;

        while (i >= 0 && filename.charAt(i) <= ' ') {
          i--;
        }

        filename = filename.substring(0, i + 1);
      }

      if (filename == null || filename.length() == 0) {
        // no file selected, multiple selection off, therefore cancel the approve action
        resetGlobFilter();
        return;
      }

      File selectedFile = null;
      File[] selectedFiles = null;

      // Unix: Resolve '~' to user's home directory
      if (File.separatorChar == '/') {
        if (filename.startsWith("~/")) {
          filename = System.getProperty("user.home") + filename.substring(1);
        } else if (filename.equals("~")) {
          filename = System.getProperty("user.home");
        }
      }

      if (chooser.isMultiSelectionEnabled()
          && filename.length() > 1
          && filename.charAt(0) == '"'
          && filename.charAt(filename.length() - 1) == '"') {
        List<File> fList = new ArrayList<File>();

        String[] files = filename.substring(1, filename.length() - 1).split("\" \"");
        // Optimize searching files by names in "children" array
        Arrays.sort(files);

        File[] children = null;
        int childIndex = 0;

        for (String str : files) {
          File file = fs.createFileObject(str);
          if (!file.isAbsolute()) {
            if (children == null) {
              children = fs.getFiles(dir, false);
              Arrays.sort(children);
            }
            for (int k = 0; k < children.length; k++) {
              int l = (childIndex + k) % children.length;
              if (children[l].getName().equals(str)) {
                file = children[l];
                childIndex = l + 1;
                break;
              }
            }
          }
          fList.add(file);
        }

        if (!fList.isEmpty()) {
          selectedFiles = fList.toArray(new File[fList.size()]);
        }
        resetGlobFilter();
      } else {
        selectedFile = fs.createFileObject(filename);
        if (!selectedFile.isAbsolute()) {
          selectedFile = fs.getChild(dir, filename);
        }
        // check for wildcard pattern
        FileFilter currentFilter = chooser.getFileFilter();
        if (!selectedFile.exists() && isGlobPattern(filename)) {
          changeDirectory(selectedFile.getParentFile());
          if (globFilter == null) {
            globFilter = new GlobFilter();
          }
          try {
            globFilter.setPattern(selectedFile.getName());
            if (!(currentFilter instanceof GlobFilter)) {
              actualFileFilter = currentFilter;
            }
            chooser.setFileFilter(null);
            chooser.setFileFilter(globFilter);
            return;
          } catch (PatternSyntaxException pse) {
            // Not a valid glob pattern. Abandon filter.
          }
        }

        resetGlobFilter();

        // Check for directory change action
        boolean isDir = (selectedFile != null && selectedFile.isDirectory());
        boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile));
        boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();
        boolean isFileSelEnabled = chooser.isFileSelectionEnabled();
        boolean isCtrl =
            (e != null
                && (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0);

        if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) {
          changeDirectory(selectedFile);
          return;
        } else if ((isDir || !isFileSelEnabled)
            && (!isDir || !isDirSelEnabled)
            && (!isDirSelEnabled || selectedFile.exists())) {
          selectedFile = null;
        }
      }

      if (selectedFiles != null || selectedFile != null) {
        if (selectedFiles != null || chooser.isMultiSelectionEnabled()) {
          if (selectedFiles == null) {
            selectedFiles = new File[] {selectedFile};
          }
          chooser.setSelectedFiles(selectedFiles);
          // Do it again. This is a fix for bug 4949273 to force the
          // selected value in case the ListSelectionModel clears it
          // for non-existing file names.
          chooser.setSelectedFiles(selectedFiles);
        } else {
          chooser.setSelectedFile(selectedFile);
        }
        chooser.approveSelection();
      } else {
        if (chooser.isMultiSelectionEnabled()) {
          chooser.setSelectedFiles(null);
        } else {
          chooser.setSelectedFile(null);
        }
        chooser.cancelSelection();
      }
    }