/**
   * Generates a source code skeleton for this class.
   *
   * @param template the name of the particular class template (just the base name without path and
   *     suffix)
   * @param pkg the package that the class target resides in
   * @param name the name of the class
   * @param sourceFile the name of the source file to be generated
   */
  public boolean generateSkeleton(String template, Package pkg, String name, String sourceFile) {
    Hashtable<String, String> translations = new Hashtable<String, String>();
    translations.put("CLASSNAME", name);

    if (pkg.isUnnamedPackage()) {
      translations.put("PKGLINE", "");
    } else {
      translations.put(
          "PKGLINE", "package " + pkg.getQualifiedName() + ";" + Config.nl + Config.nl);
    }

    try {
      // Check for existing file. Normally this won't happen (the check for duplicate
      // target occurs prior to this) but on Windows filenames are case insensitive.
      File dest = new File(sourceFile);
      if (dest.exists()) {
        pkg.showError("duplicate-name");
        return false;
      }
      BlueJFileReader.translateFile(
          Config.getClassTemplateFile(template),
          new File(sourceFile),
          translations,
          Charset.forName("UTF-8"),
          pkg.getProject().getProjectCharset());
      return true;
    } catch (IOException e) {
      pkg.showError("skeleton-error");
      Debug.reportError("The default skeleton for the class could not be generated");
      Debug.reportError("Exception: " + e);
      return false;
    }
  }
Example #2
0
  @Override
  public void keyTyped(KeyEvent event) {
    // We handle most things we are interested in here. The InputMap filters out
    // most other unwanted actions (but allows copy/paste).

    char ch = event.getKeyChar();

    if ((!isMacOs) && handleFontsizeKeys(event, ch)) {
      // Note: On Mac OS with Java 7+, we don't see command+= / command+- as a
      // key-typed event and so we handle it in keyReleased instead.
      return;
    }

    if ((event.getModifiers() & Event.META_MASK) != 0) {
      return; // return without consuming the event
    }
    if (isActive) {
      switch (ch) {
        case 4: // CTRL-D (unix/Mac EOF)
        case 26: // CTRL-Z (DOS/Windows EOF)
          buffer.signalEOF();
          writeToTerminal("\n");
          event.consume();
          break;

        case '\b': // backspace
          if (buffer.backSpace()) {
            try {
              int length = text.getDocument().getLength();
              text.replaceRange("", length - 1, length);
            } catch (Exception exc) {
              Debug.reportError("bad location " + exc);
            }
          }
          event.consume();
          break;

        case '\r': // carriage return
        case '\n': // newline
          if (buffer.putChar('\n')) {
            writeToTerminal(String.valueOf(ch));
            buffer.notifyReaders();
          }
          event.consume();
          break;

        default:
          if (ch >= 32) {
            if (buffer.putChar(ch)) {
              writeToTerminal(String.valueOf(ch));
            }
            event.consume();
          }
          break;
      }
    }
  }
  /**
   * Create the menu items for the given members (constructors or methods).
   *
   * @return true if any items were created
   */
  public static boolean createMenuItems(
      JPopupMenu menu,
      CallableView[] members,
      ViewFilter filter,
      int first,
      int last,
      String prefix,
      InvokeListener il) {
    // Debug.message("Inside ClassTarget.createMenuItems\n first = " + first
    // + " last = " + last);
    boolean hasEntries = false;
    JMenuItem item;

    for (int i = first; i < last; i++) {
      try {
        CallableView m = members[last - i - 1];
        if (!filter.accept(m)) continue;
        // Debug.message("createSubMenu - creating MenuItem");

        Action callAction = null;
        if (m instanceof MethodView)
          callAction = new InvokeAction((MethodView) m, il, prefix + m.getLongDesc());
        else if (m instanceof ConstructorView)
          callAction = new ConstructAction((ConstructorView) m, il, prefix + m.getLongDesc());

        if (callAction != null) {
          item = menu.add(callAction);
          item.setFont(PrefMgr.getPopupMenuFont());
          hasEntries = true;
        }
      } catch (Exception e) {
        Debug.reportError("Exception accessing methods: " + e);
        e.printStackTrace();
      }
    }
    return hasEntries;
  }
Example #4
0
  public static void setClassImage(ClassView classView, ImageClassRole gclassRole, File imageFile) {
    GClass gclass = classView.getGClass();
    File projImagesDir = gclass.getPackage().getProject().getImageDir();
    if (imageFile != null) {
      if (!imageFile.getParentFile().getAbsoluteFile().equals(projImagesDir)) {
        // An image was selected from an external dir. We need
        // to copy it into the project images directory first.
        File destFile = new File(projImagesDir, imageFile.getName());
        try {
          FileUtility.copyFile(imageFile, destFile);
          imageFile = destFile;
        } catch (IOException e) {
          Debug.reportError("Error when copying file: " + imageFile + " to: " + destFile, e);
        }
      }

      gclass.setClassProperty("image", imageFile.getName());
    } else {
      imageFile = null;
      gclass.setClassProperty("image", null);
    }
    gclassRole.changeImage();
    gclass.getPackage().getProject().getProjectProperties().save();
  }