// to avoid adding templates to the saved recently list in the application
  @Override
  protected void saveViewToURI(
      final View view, final URI file, @Nullable final URIChooser chooser) {
    view.execute(
        new Worker() {

          @Override
          protected Object construct() throws IOException {
            view.write(file, chooser);
            return null;
          }

          @Override
          protected void done(Object value) {
            view.setURI(file);
            view.markChangesAsSaved();
            int multiOpenId = 1;
            for (View p : view.getApplication().views()) {
              if (p != view && p.getURI() != null && p.getURI().equals(file)) {
                multiOpenId = Math.max(multiOpenId, p.getMultipleOpenId() + 1);
              }
            }
            // getApplication().addRecentURI(file);
            view.setMultipleOpenId(multiOpenId);
          }

          @Override
          protected void failed(Throwable value) {
            value.printStackTrace();
            String message = value.getMessage() != null ? value.getMessage() : value.toString();
            ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels");
            JSheet.showMessageSheet(
                getActiveView().getComponent(),
                "<html>"
                    + UIManager.getString("OptionPane.css")
                    + "<b>"
                    + labels.getFormatted("file.save.couldntSave.message", URIUtil.getName(file))
                    + "</b><p>"
                    + ((message == null) ? "" : message),
                JOptionPane.ERROR_MESSAGE);
          }

          @Override
          protected void finished() {
            view.setEnabled(true);
            SwingUtilities.getWindowAncestor(view.getComponent()).toFront();
            if (oldFocusOwner != null) {
              oldFocusOwner.requestFocus();
            }
          }
        });
  }
  @Override
  public void actionPerformed(ActionEvent evt) {
    final Application app = getApplication();
    if (app.isEnabled()) {
      app.setEnabled(false);
      // Search for an empty view
      View emptyView = app.getActiveView();
      if (emptyView == null || !emptyView.isEmpty() || !emptyView.isEnabled()) {
        emptyView = null;
      }

      final View p;
      if (emptyView == null) {
        p = app.createView();
        app.add(p);
        app.show(p);
      } else {
        p = emptyView;
      }
      openView(p);
    }
  }
  protected void openView(final View view) {
    final Application app = getApplication();
    app.setEnabled(true);

    // If there is another view with the same URI we set the multiple open
    // id of our view to max(multiple open id) + 1.
    int multipleOpenId = 1;
    for (View aView : app.views()) {
      if (aView != view && aView.isEmpty()) {
        multipleOpenId = Math.max(multipleOpenId, aView.getMultipleOpenId() + 1);
      }
    }
    view.setMultipleOpenId(multipleOpenId);
    view.setEnabled(false);

    // Open the file
    view.execute(
        new Worker() {

          @Override
          protected Object construct() throws IOException {
            boolean exists = true;
            try {
              File f = new File(uri);
              exists = f.exists();
            } catch (IllegalArgumentException e) {
              // The URI does not denote a file, thus we can not check whether the file exists.
            }
            if (exists) {
              view.read(uri, null);
              return null;
            } else {
              ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels");
              throw new IOException(
                  labels.getFormatted("file.open.fileDoesNotExist.message", URIUtil.getName(uri)));
            }
          }

          @Override
          protected void done(Object value) {
            view.setURI(uri);
            Frame w = (Frame) SwingUtilities.getWindowAncestor(view.getComponent());
            if (w != null) {
              w.setExtendedState(w.getExtendedState() & ~Frame.ICONIFIED);
              w.toFront();
            }
            view.setEnabled(true);
            view.getComponent().requestFocus();
          }

          @Override
          protected void failed(Throwable value) {
            value.printStackTrace();
            String message = value.getMessage() != null ? value.getMessage() : value.toString();
            ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels");
            JSheet.showMessageSheet(
                view.getComponent(),
                "<html>"
                    + UIManager.getString("OptionPane.css")
                    + "<b>"
                    + labels.getFormatted("file.open.couldntOpen.message", URIUtil.getName(uri))
                    + "</b><p>"
                    + (message == null ? "" : message),
                JOptionPane.ERROR_MESSAGE,
                new SheetListener() {

                  @Override
                  public void optionSelected(SheetEvent evt) {
                    view.setEnabled(true);
                  }
                });
          }
        });
  }