package net.kenevans.dilbert.plugin;

import javax.swing.SwingUtilities;

import net.kenevans.imagemodel.ImagePanel;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * ImageActions creates and holds the actions for the ImageEditorPlugin.
 * 
 * @author Kenneth Evans, Jr.
 */
public class ImageActions implements IPluginConstants
{
    public ExtendedAction saveAsAction = null;
    public ExtendedAction pageSetupAction = null;
    public ExtendedAction printPreviewAction = null;
    public ExtendedAction printAction = null;
    public ExtendedAction copyAction = null;
    public ExtendedAction refreshAction = null;
    public ExtendedAction fitAction = null;

    private ImagePanel imagePanel;

    public ImageActions(ImagePanel panel) {
        this.imagePanel = panel;

        // Save as
        saveAsAction = new ExtendedAction() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if(imagePanel != null) {
                            imagePanel.saveAs();
                        }
                    }
                });
            }
        };
        saveAsAction.setProps("Save As");

        // Page setup
        pageSetupAction = new ExtendedAction() {
            public void run() {
                if(imagePanel != null) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            imagePanel.pageSetup();
                        }
                    });
                }
            }
        };
        pageSetupAction.setProps("Page Setup");

        // Print preview
        printPreviewAction = new ExtendedAction() {
            public void run() {
                if(imagePanel != null) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            imagePanel.printPreview();
                        }
                    });
                }
            }
        };
        printPreviewAction.setProps("Print Preview");

        // Print
        printAction = new ExtendedAction() {
            public void run() {
                if(imagePanel != null) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            if(imagePanel != null) {
                                imagePanel.print();
                            }
                        }
                    });
                }
            }
        };
        printAction.setProps("Print");

        // Copy
        copyAction = new ExtendedAction() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if(imagePanel != null) {
                            imagePanel.copy();
                        }
                    }
                });
            }
        };
        copyAction.setProps("Copy");

        // Refresh
        refreshAction = new ExtendedAction() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if(imagePanel != null) {
                            imagePanel.repaint();
                        }
                    }
                });
            };
        };
        refreshAction.setProps("Refresh");
        refreshAction.setImageDescriptor(AbstractUIPlugin
            .imageDescriptorFromPlugin(PLUGIN_ID, "icons/refresh.gif"));

        // Fit
        fitAction = new ExtendedAction() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if(imagePanel != null) {
                            imagePanel.setScaled(!imagePanel.isScaled());
                            imagePanel.repaint();
                        }
                    }
                });
            }
        };
        fitAction.setProps("Toggle Fit");
    }

    /**
     * ExtendedAction is a wrapper around Action with some convenience methods.
     * 
     * @author Kenneth Evans, Jr.
     */
    public class ExtendedAction extends Action
    {
        public void setProps(String description) {
            setProps(description, null);
        }

        public void setProps(String description, ImageDescriptor imageDescriptor) {
            setText(description);
            setToolTipText(description);
            setImageDescriptor(imageDescriptor);
        }
    }

    /**
     * @return The value of imagePanel.
     */
    public ImagePanel getImagePanel() {
        return imagePanel;
    }

    /**
     * @param imagePanel The new value for imagePanel.
     */
    public void setImagePanel(ImagePanel imagePanel) {
        this.imagePanel = imagePanel;
    }

}
