package net.sf.jpluck.apps.jpluckx.ui;
import net.sf.jpluck.apps.jpluckx.JPluckX;
import net.sf.jpluck.apps.jpluckx.Showcase;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.swing.PopupListener;
import net.sf.jpluck.swing.TableList;
import net.sf.jpluck.swing.WindowUtil;
import net.sf.jpluck.ui.Banner;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ShowcaseFrame extends JFrame {
private TableList tableList;
private Showcase showcase;
private ConvertAction convertAction = new ConvertAction();
private OpenAction openAction = new OpenAction();
private CopyAction copyAction = new CopyAction();
private CloseAction closeAction = new CloseAction();
private UpdateAction updateAction = new UpdateAction();
private ShowcasePanel showcasePanel;
private JPopupMenu popupMenu = new JPopupMenu();
private Document selectedDocument;
public ShowcaseFrame(Showcase showcase) {
this.showcase = showcase;
initComponents();
}
private void initComponents() {
try {
URL url = ShowcaseFrame.class.getClassLoader().getResource("net/sf/jpluck/res/jpluck16.gif");
setIconImage(ImageIO.read(url));
} catch (Exception e) {
// Should not occur;
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
setTitle("Showcase (" + df.format(showcase.getLastEdited()) + " / " + showcase.getSize() +
" sites) - JPluck X");
popupMenu.add(convertAction);
popupMenu.add(openAction);
popupMenu.add(copyAction);
Banner banner = new Banner();
tableList = new TableList(showcase, true, true);
tableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableList.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
tableList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
if (SwingUtilities.isLeftMouseButton(ev) && (ev.getClickCount() == 2)) {
convertAction.actionPerformed(null);
}
}
});
tableList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
boolean b = (tableList.getSelectedRows().length == 1);
convertAction.setEnabled(b);
openAction.setEnabled(b);
copyAction.setEnabled(b);
Object[] items = tableList.getSelectedItems();
if (items.length == 1) {
Document document = (Document) items[0];
if (document != selectedDocument) {
selectedDocument = document;
showcasePanel.showPreview(document.getName());
}
}
}
});
tableList.addMouseListener(new PopupListener(tableList, popupMenu) {
protected void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
Point p = new Point(e.getX(), e.getY());
int idx = tableList.rowAtPoint(p);
int[] rows = tableList.getSelectedRows();
boolean inSelection = false;
for (int i = 0; i < rows.length; i++) {
if (rows[i] == idx) {
inSelection = true;
break;
}
}
if (!inSelection && (idx > -1)) {
tableList.setRowSelectionInterval(idx, idx);
}
this.popupMenu.show(component, e.getX(), e.getY());
}
}
});
tableList.optimizeWidths(4);
tableList.sortByColumn(0);
showcasePanel = new ShowcasePanel(new Action[] {
convertAction, openAction, copyAction, /*updateAction,*/
closeAction
});
JScrollPane scrollPane = new JScrollPane(tableList);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(showcasePanel, BorderLayout.WEST);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.Plastic3DLookAndFeel");
Showcase showcase = Showcase.create();
ShowcaseFrame showcaseFrame = new ShowcaseFrame(showcase);
showcaseFrame.setSize(600, 400);
WindowUtil.center(showcaseFrame);
showcaseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
showcaseFrame.show();
}
private class ConvertAction extends AbstractAction {
ConvertAction() {
super("Convert site");
putValue(Action.SMALL_ICON, IconUtil.getIcon("convert.gif"));
setEnabled(false);
}
public void actionPerformed(ActionEvent ev) {
Document site = (Document) tableList.getSelectedItems()[0];
JPluckX.getInstance().convert(ShowcaseFrame.this, new Document[] { site });
}
}
private class OpenAction extends AbstractAction {
OpenAction() {
super("Open in browser");
putValue(Action.SMALL_ICON, IconUtil.getIcon("home.gif"));
setEnabled(false);
}
public void actionPerformed(ActionEvent ev) {
Document document = (Document) tableList.getSelectedItems()[0];
JPluckX.getInstance().openURL(document.getUri(), ShowcaseFrame.this);
}
}
private class CopyAction extends AbstractAction {
CopyAction() {
super("Copy to JXL");
putValue(Action.SMALL_ICON, IconUtil.getIcon("copy.gif"));
setEnabled(false);
}
public void actionPerformed(ActionEvent ev) {
Document document = (Document) tableList.getSelectedItems()[0];
JPluckX.getInstance().copyFromShowcase(document);
}
}
private class CloseAction extends AbstractAction {
CloseAction() {
super("Close Showcase");
putValue(Action.SMALL_ICON, IconUtil.getIcon("remove.gif"));
}
public void actionPerformed(ActionEvent arg0) {
hide();
}
}
private class UpdateAction extends AbstractAction {
UpdateAction() {
super("Update Showcase");
putValue(Action.SMALL_ICON, IconUtil.getIcon("refresh.gif"));
}
public void actionPerformed(ActionEvent ev) {
JPluckX.getInstance().openURL("http://jpluck.sourceforge.net/showcase/", ShowcaseFrame.this);
}
}
}