Package ru.snake.amazonwatcher.actions

Source Code of ru.snake.amazonwatcher.actions.OpenUrlAction

package ru.snake.amazonwatcher.actions;

import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.AbstractAction;
import javax.swing.ListSelectionModel;

import ru.snake.amazonwatcher.cache.IconCache;
import ru.snake.amazonwatcher.model.AbstractProductModel;
import ru.snake.amazonwatcher.model.ProductInfo;
import ru.snake.amazonwatcher.model.ProductModel;

@SuppressWarnings("serial")
public final class OpenUrlAction extends AbstractAction {
  private final AbstractProductModel dataModel;
  private final ListSelectionModel selectionModel;

  public OpenUrlAction(ProductModel dataModel,
      ListSelectionModel selectionModel) {
    this.dataModel = dataModel;
    this.selectionModel = selectionModel;

    putValue(NAME, "Открыть ссылку");
    putValue(SHORT_DESCRIPTION,
        "<HTML><P>Открыть страницу товара в браузере.</HTML>");
    putValue(LARGE_ICON_KEY, IconCache.getImageIcon("browse_large.png"));
  }

  public void actionPerformed(ActionEvent arg0) {
    int selectedIntex;

    selectedIntex = selectionModel.getMinSelectionIndex();

    if (selectedIntex != -1) {
      URI uri;
      ProductInfo productInfo = this.dataModel.get(selectedIntex);

      if (!java.awt.Desktop.isDesktopSupported())
        return;

      Desktop desktop = java.awt.Desktop.getDesktop();

      if (!desktop.isSupported(Action.BROWSE))
        return;

      try {
        uri = new URI(productInfo.getUrl().toString());
      } catch (URISyntaxException e) {
        return;
      }

      try {
        desktop.browse(uri);
      } catch (IOException e) {
        return;
      }
    }
  }

}
TOP

Related Classes of ru.snake.amazonwatcher.actions.OpenUrlAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.