Пример #1
0
/**
 * The <tt>Resources</tt> class manages the access to the internationalization properties files and
 * the image resources used in this plugin.
 *
 * @author Yana Stamcheva
 */
public class Resources {

  private static Logger log = Logger.getLogger(Resources.class);

  /** The name of the resource, where internationalization strings for this plugin are stored. */
  private static final String STRING_RESOURCE_NAME =
      "resources.languages.plugin.contactinfo.resources";

  /** The name of the resource, where paths to images used in this bundle are stored. */
  private static final String IMAGE_RESOURCE_NAME =
      "net.java.sip.communicator.plugin.contactinfo.resources";

  /** The string resource bundle. */
  private static final ResourceBundle STRING_RESOURCE_BUNDLE =
      ResourceBundle.getBundle(STRING_RESOURCE_NAME);

  /** The image resource bundle. */
  private static final ResourceBundle IMAGE_RESOURCE_BUNDLE =
      ResourceBundle.getBundle(IMAGE_RESOURCE_NAME);

  /**
   * Returns an internationalized string corresponding to the given key.
   *
   * @param key The key of the string.
   * @return An internationalized string corresponding to the given key.
   */
  public static String getString(String key) {
    try {
      return STRING_RESOURCE_BUNDLE.getString(key);

    } catch (MissingResourceException e) {
      return '!' + key + '!';
    }
  }

  /**
   * Loads an image from a given image identifier.
   *
   * @param imageID The identifier of the image.
   * @return The image for the given identifier.
   */
  public static ImageIcon getImage(String imageID) {
    BufferedImage image = null;

    String path = IMAGE_RESOURCE_BUNDLE.getString(imageID);

    try {
      image = ImageIO.read(Resources.class.getClassLoader().getResourceAsStream(path));
    } catch (IOException e) {
      log.error("Failed to load image:" + path, e);
    }

    return new ImageIcon(image);
  }
}
 protected void initResourceBundle(UIDefaults table) {
   // The following line of code does not work, when Quaqua has been loaded with
   // a custom class loader. That's why, we have to inject the labels
   // by ourselves:
   // table.addResourceBundle( "ch.randelshofer.quaqua.Labels" );
   ResourceBundle bundle =
       ResourceBundle.getBundle(
           "ch.randelshofer.quaqua.Labels", Locale.getDefault(), getClass().getClassLoader());
   for (Enumeration i = bundle.getKeys(); i.hasMoreElements(); ) {
     String key = (String) i.nextElement();
     table.put(key, bundle.getObject(key));
   }
 }
/**
 * Implementaion of Framebuffer for MeTA Studio. Majorly a copy of <code>
 * org.jrman.ui.FramebufferImpl</code>.
 *
 * @author V.Ganesh
 * @version 2.0 (Part of MeTA v2.0)
 */
public class IDEJRManFramebufferImpl extends JInternalFrame implements Framebuffer {

  private JImageViewerPanel imagePanel = new JImageViewerPanel();
  private ImageViewerPanelSaveAction save =
      new ImageViewerPanelSaveAction(imagePanel, BufferedImage.TYPE_INT_ARGB);
  private String name;

  /** Creates a new instance of IDEJRManFramebufferImpl */
  public IDEJRManFramebufferImpl(String name, BufferedImage image) {
    super("JRMan rendered: " + name, true, true, true, true);
    this.name = name;

    save.setEnabled(false);

    imagePanel.setImage(image);
    imagePanel.addToolbarAction(save);
    if (image.getType() == BufferedImage.TYPE_INT_ARGB
        || image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
      imagePanel.setShowTransparencyPattern(true);
    }

    getRootPane().setDoubleBuffered(false);
    getContentPane().add(imagePanel);
    pack();

    ImageResource images = ImageResource.getInstance();

    // set the frame icon
    setFrameIcon(images.getJrMan());

    // add this to the IDE desktop
    MainMenuEventHandlers.getInstance(null)
        .getIdeInstance()
        .getWorkspaceDesktop()
        .addInternalFrame(this, true);
  }

  /**
   * Signal that a certain rectangular region has changed
   *
   * @param x top-left x coordinate
   * @param y top-left y coordinate
   * @param w rectangle width
   * @param h rectangle height
   */
  @Override
  public void refresh(int x, int y, int w, int h) {
    imagePanel.repaintImage(x, y, w, h);
  }

  /** Signal image is completed */
  @Override
  public void completed() {
    save.setEnabled(true);
  }

  // the resource bundels
  private static final ResourceBundle messagesBundle =
      ResourceBundle.getBundle(
          net.falappa.imageio.ImageViewerPanelSaveAction.class.getPackage().getName()
              + ".res.ImageViewerPanelActions");

  /** inner class to handle save action */
  public class ImageViewerPanelSaveAction extends AbstractAction {
    private JImageViewerPanel viewerPanel;
    private int imageType;
    private IDEFileChooser fc;

    /**
     * Constructs and initializes this object
     *
     * @param viewerPanel the <code>JImageViewerPanel</code> this action is linked to
     * @param imageType
     */
    public ImageViewerPanelSaveAction(JImageViewerPanel viewerPanel, int imageType) {
      super(messagesBundle.getString("ImageViewerPanelSaveAction.Save_1")); // $NON-NLS-1$
      assert viewerPanel != null;
      this.imageType = imageType;
      this.viewerPanel = viewerPanel;
      putValue(
          SHORT_DESCRIPTION,
          messagesBundle.getString("ImageViewerPanelSaveAction.Save_image_to_file_2"));
      //$NON-NLS-1$
      putValue(SMALL_ICON, UIManager.getIcon("FileView.floppyDriveIcon")); // $NON-NLS-1$
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      if (fc == null) {
        fc = new IDEFileChooser();
        fc.setFileView(new IDEFileView());
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);
        fc.setDialogTitle(
            messagesBundle.getString("ImageViewerPanelSaveAction.Choose_filename_to_save_4"));

        //$NON-NLS-1$

        // prepare file filters
        IIORegistry theRegistry = IIORegistry.getDefaultInstance();
        Iterator it = theRegistry.getServiceProviders(ImageWriterSpi.class, false);
        while (it.hasNext()) {
          ImageWriterSpi writer = (ImageWriterSpi) it.next();
          if ((imageType == BufferedImage.TYPE_INT_ARGB
                  || imageType == BufferedImage.TYPE_INT_ARGB_PRE)
              && "JPEG".equals(writer.getFormatNames()[0].toUpperCase())) continue;
          ImageWriterSpiFileFilter ff = new ImageWriterSpiFileFilter(writer);
          fc.addChoosableFileFilter(ff);
        }
      }

      if (fc.showSaveDialog(viewerPanel) == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fc.getSelectedFile();

        if (selectedFile != null) {
          String fileName = selectedFile.getAbsolutePath();
          ImageWriterSpiFileFilter ff = (ImageWriterSpiFileFilter) fc.getFileFilter();
          if (!ff.hasCorrectSuffix(fileName)) fileName = ff.addSuffix(fileName);
          selectedFile = new File(fileName);
          if (selectedFile.exists()) {
            String message =
                MessageFormat.format(
                    messagesBundle.getString("ImageViewerPanelSaveAction.Overwrite_question_5"),
                    //$NON-NLS-1$
                    fileName);
            if (JOptionPane.NO_OPTION
                == JOptionPane.showConfirmDialog(
                    viewerPanel,
                    message,
                    messagesBundle.getString("ImageViewerPanelSaveAction.Warning_6"),
                    //$NON-NLS-1$
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE)) return;
          }
          writeToFile(selectedFile, ff);
        }
      }
    }

    private void writeToFile(File selectedFile, ImageWriterSpiFileFilter ff) {
      try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(selectedFile);
        ImageWriter iw = ff.getImageWriterSpi().createWriterInstance();
        iw.setOutput(ios);
        ImageWriteParam iwp = iw.getDefaultWriteParam();
        if (iwp.canWriteCompressed()) {
          iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          // set maximum image quality
          iwp.setCompressionQuality(1.f);
        }
        Image image = viewerPanel.getImage();
        BufferedImage bufferedImage;
        if (viewerPanel.getImage() instanceof BufferedImage)
          bufferedImage = (BufferedImage) viewerPanel.getImage();
        else {
          bufferedImage =
              new BufferedImage(
                  image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
          bufferedImage.createGraphics().drawImage(image, 0, 0, null);
        }
        iw.write(null, new IIOImage(bufferedImage, null, null), iwp);
        iw.dispose();
        ios.close();
      } catch (IOException ioe) {
        JOptionPane.showMessageDialog(
            viewerPanel,
            messagesBundle.getString(
                "ImageViewerPanelSaveAction." + "Error_during_image_saving_message_7"),
            //$NON-NLS-1$
            messagesBundle.getString("ImageViewerPanelSaveAction." + "Error_dialog_title_8"),
            //$NON-NLS-1$
            JOptionPane.ERROR_MESSAGE);
        ioe.printStackTrace();
      }
    }
  }
} // end of class IDEJRManFramebufferImpl