from pyswt import *
import os

# action listener
class ActionListener(SelectionListener):
    def __init__(self, callback):
        SelectionListener.__init__(self)
        self.callback = callback

    def widgetSelected(self, event):
        self.callback(event)

    def widgetDefaultSelected(self, event):
        self.callback(event)

# main window
class MainWnd(SelectionListener):
    def __init__(self):
        self.display = Display()
        self.shell = Shell(self.display)
        self.shell.setText("PySWT Zip 1.0")
        self.shell.setImage(self.loadImage("images/javazip.gif"))
        gd = GridLayout()
        gd.marginWidth = gd.marginHeight = 1
        self.shell.setLayout(gd)

        self.addMenuBar()
        self.addSeparator()
        self.addToolBar()
        self.addSeparator()
        self.addContents()
        self.addStatusLine()

    def newAction(self, event):
        print "new action"

    def openAction(self, event):
        dlg = FileDialog(self.shell, SWT.OPEN)
        dlg.setFilterNames(["Zip Files (*.zip)", "All Files (*.*)"])
        dlg.setFilterExtensions(["*.zip", "*.*"])
        if dlg.open() != None:
            name = str(dlg.getFilterPath()) + os.sep + str(dlg.getFileName())
            print name

    def closeAction(self, event):
        print "close action"

    def exitAction(self, event):
        self.shell.close()

    def addAction(self, event):
        print "add action"

    def deleteAction(self, event):
        print "delete action"

    def extractAction(self, event):
        print "extract action"

    def viewAction(self, event):
        print "view action"

    def configAction(self, event):
        print "config action"

    def aboutAction(self, event):
        box = MessageBox(self.shell, SWT.YES)
        box.setText("About PySWT Zip 1.0")
        box.setMessage("PySWT Zip 1.0\n Copyright 2005")
        box.open()

    def loadImage(self, name):
        imageData = ImageData(name)
        if imageData.transparentPixel >= 0:
            maskData = imageData.getTransparencyMask()
            return Image(Display.getCurrent(), imageData, maskData)
        else:
            return Image(Display.getCurrent(), imageData)

    def addMenuBar(self):
        menubar = Menu(self.shell, SWT.BAR)
        self.shell.setMenuBar(menubar)

        menus = [
                  ["File",
                    ["&New ...\tCtrl+N", "images/menu_new16.gif", SWT.CTRL+ord('N'), self.newAction],
                    ["&Open ...\tCtrl+O", "images/menu_open16.gif", SWT.CTRL+ord('O'), self.openAction],
                    ["&Close\tCtrl+C", "images/menu_close16.gif", SWT.CTRL+ord('C'), self.closeAction],
                    ["--"],
                    ["E&xit\tCtrl+X", "images/menu_exit16.gif", SWT.CTRL+ord('X'), self.exitAction],
                  ],
                  ["Actions",
                    ["&Add ...\tShift+A", "images/menu_add16.gif", SWT.SHIFT+ord('A'), self.addAction],
                    ["&Delete ...\tShift+D", "images/menu_delete16.gif", SWT.SHIFT+ord('D'), self.deleteAction],
                    ["&Extract ...\tShift+E", "images/menu_extract16.gif", SWT.SHIFT+ord('E'), self.extractAction],
                    ["&View ...\tShift+V", "images/menu_view16.gif", SWT.SHIFT+ord('V'), self.viewAction],
                  ],
                  ["Options",
                    ["&Configuration...", "images/menu_config16.gif", 0, self.configAction],
                  ],
                  ["Help",
                    ["&About PySWT Zip...\tCtrl+A", "images/menu_about16.gif", SWT.CTRL+ord('A'), self.aboutAction],
                  ],
                ]

        for topmenu in menus:
            cascadeMenu = MenuItem(menubar, SWT.CASCADE)
            cascadeMenu.setText(topmenu[0])
            dropdownMenu= Menu(self.shell, SWT.DROP_DOWN)
            cascadeMenu.setMenu(dropdownMenu)
            for i in range(1, len(topmenu)):
                menu = topmenu[i]
                if menu[0] == "--":
                    menuItem = MenuItem(dropdownMenu, SWT.SEPARATOR)
                else:
                    menuItem = MenuItem(dropdownMenu, SWT.NONE)
                    menuItem.setText(menu[0])
                    if menu[1] != None: menuItem.setImage(self.loadImage(menu[1]))
                    if menu[2] != 0: menuItem.setAccelerator(menu[2])
                    if menu[3] != None:
                        menuItem.addSelectionListener(ActionListener(menu[3]))

    def addSeparator(self):
        return Label(self.shell, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(GridData(GridData.FILL_HORIZONTAL))

    def addToolBar(self):
        toolBar = ToolBar(self.shell, SWT.FLAT)
        toolBar.setLayoutData(GridData(GridData.FILL_HORIZONTAL))

        toolbtns = [
                    ["New", "images/tb_new.gif", self.newAction],
                    ["Open", "images/tb_open.gif", self.openAction],
                    ["Add", "images/tb_add.gif", self.addAction],
                    ["Delete", "images/tb_delete.gif", self.deleteAction],
                    ["Extract", "images/tb_extract.gif", self.extractAction],
                    ["View", "images/tb_view.gif", self.viewAction],
                    ["Configuration", "images/tb_config.gif", self.configAction],
                    ["About", "images/tb_about.gif", self.aboutAction],
                    ["Exit", "images/tb_exit.gif", self.exitAction],
                   ]

        for btn in toolbtns:
            item = ToolItem(toolBar, SWT.NONE)
            item.setText(btn[0])
            item.setImage(self.loadImage(btn[1]))
            item.addSelectionListener(ActionListener(btn[2]))

    def addContents(self):
        table = Table(self.shell, SWT.BORDER | SWT.FULL_SELECTION)
        table.setLayoutData(GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL))
        table.setHeaderVisible(True)

        columns = [
                  [SWT.LEFT, "Name", 200],
                  [SWT.LEFT, "modified", 100],
                  [SWT.RIGHT, "Size", 60],
                  [SWT.CENTER, "Ratio", 40],
                  [SWT.RIGHT, "Packed", 60],
                  [SWT.LEFT, "Path", 300],
                 ]

        for col in columns:
            tabColumn = TableColumn(table, col[0])
            tabColumn.setText(col[1])
            tabColumn.setWidth(col[2])

        table.setItemCount(10)
        table.setFocus()

    def addStatusLine(self):
        statusLine = Label(self.shell, SWT.SHADOW_IN);
        statusLine.setLayoutData(GridData(GridData.FILL_HORIZONTAL));

    def run(self):
        self.shell.pack()
        self.shell.open()
        while not self.shell.isDisposed():
            if not self.display.readAndDispatch():
                self.display.sleep()
        self.display.dispose()

if __name__ == "__main__":
    h = MainWnd()
    h.run()