Пример #1
0
  /**
   * Will save the currently selected image as the image of the class if OK is pressed.
   *
   * <p>If performed on the current world this will allow for previewing of the currently selected
   * image, but revert to the original background image of cancelled.
   *
   * @param e ignored
   */
  public void actionPerformed(ActionEvent e) {
    final World currentWorld = WorldHandler.getInstance().getWorld();
    // save the original background if possible
    final GreenfootImage originalBackground =
        ((currentWorld == null) ? null : WorldVisitor.getBackgroundImage(currentWorld));

    // allow the previewing if we are setting the image of the current world.
    ImageSelectionWatcher watcher = null;
    if (currentWorld != null
        && currentWorld.getClass().getName().equals(classView.getGClass().getQualifiedName())) {
      watcher =
          new ImageSelectionWatcher() {
            @Override
            public void imageSelected(final File imageFile) {
              if (imageFile != null) {
                Simulation.getInstance()
                    .runLater(
                        new Runnable() {
                          @Override
                          public void run() {
                            if (WorldHandler.getInstance().getWorld() == currentWorld) {
                              currentWorld.setBackground(imageFile.getAbsolutePath());
                            }
                          }
                        });
              }
            }
          };
    }

    // initialise our image library frame
    JFrame gfFrame = GreenfootMain.getInstance().getFrame();
    ImageLibFrame imageLibFrame = new ImageLibFrame(gfFrame, classView, watcher);
    DialogManager.centreDialog(imageLibFrame);
    imageLibFrame.setVisible(true);

    // set the image of the class to the selected file
    if (imageLibFrame.getResult() == ImageLibFrame.OK) {
      File currentImageFile = imageLibFrame.getSelectedImageFile();
      setClassImage(classView, gclassRole, currentImageFile);
      gfFrame.repaint();
    }
    // or if cancelled reset the world background to the original format
    // to avoid white screens or preview images being left there.
    else if (currentWorld != null && imageLibFrame.getResult() == ImageLibFrame.CANCEL) {
      Simulation.getInstance()
          .runLater(
              new Runnable() {
                @Override
                public void run() {
                  currentWorld.setBackground(originalBackground);
                }
              });
    }
  }
Пример #2
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();
  }