2020년 6월 7일 일요일

[ezGuiLib] ezPySwingLib

1. ezPySwingLib


import os

from java.lang import *
from java.io import *
from javax.swing import *
from java.awt import *
from java.awt.event import *

#
# Control Table
#

_window__ctrl_table = {}

def GetControl(name):
 return _window__ctrl_table.get(name)

def GetNativeControl(name):
 if _window__ctrl_table.get(name): return _window__ctrl_table[name].ctrl;

def DumpControlTable():
 for k,v in _window__ctrl_table.items():
  print(k,v)


#
# Controls
#

class Spacer():
 def __init__(self,h=None):
  if h.get('width'):
   self.ctrl = Box.createRigidArea(Dimension(h['width'],h['height']))
  else:
   self.ctrl = Box.createHorizontalGlue( )
  self.pane = self.ctrl

class Button():
 def __init__(self,h=None):
  if h.get('icon'):
   self.ctrl = JButton(h['label'], ImageIcon(h['icon']))
  else:
   self.ctrl = JButton(h['label'])
  self.pane = self.ctrl
  if h.get('tooltip'): self.ctrl.setToolTipText(h['tooltip'])
  if h.get('handler'): 
   self.ctrl.addActionListener(h['handler'])
   self.ctrl.setActionCommand('hello')

class TextArea():
 def __init__(self,h=None):
  self.ctrl = JTextArea()
  self.pane = JScrollPane(self.ctrl)

#
# Container
#

class SBox():
 def __init__(self,vert):
  self.ctrl = JPanel()
  self.vert = vert
  if vert: self.layout = BoxLayout( self.ctrl, BoxLayout.PAGE_AXIS )
  else:    self.layout = BoxLayout( self.ctrl, BoxLayout.LINE_AXIS )
  self.ctrl.setLayout(self.layout)
 def Add(self, child, expand=False):
  self.ctrl.add(child)
  
def VBox():
 return SBox(True)

def HBox():
 return SBox(False)


#
# Menu
#

def Menu(name,menu_table):
 #menu = ContextMenu()
 menu = JMenu(name)
 for m in menu_table:
  if not m.get('name') or m['name'] == '-':
   menu.add(JSeparator());
  if not m.get('item'): continue # Disabled
  if type(m['item']) == list:
   menu.add(Menu(m['name'],m['item']))
  else:
   item = JMenuItem(m['name'])
   item.addActionListener(m['item'])
   menu.add(item);
 return menu

#def ContextMenu(menu_table):
#    return Menu(None,menu_table)
 
def MenuBar(menubar_table):
 menubar = JMenuBar()
 for m in menubar_table:
  if m.get('name'):
   menubar.add(Menu(m['name'],m['item']))
  #else:
  #    if m.get('fontsize'):
  #        menubar.setStyle("-fx-font: " + m['fontsize'] + " arial;")  
 return menubar


#
# Window
#

def Layout(content):
 vbox = VBox()
 for v in content:
  hbox = HBox()
  expand = False
  for h in v:
   name = h.get('name')
   if not name:
    if h.get('expand'): expand = h['expand']
    continue
   if name == '<>' or name == 'Spacer': f = Spacer(h)
   elif name == 'Button': f = Button(h)
   elif name == 'TextArea': f = TextArea(h)
   else: continue
   hbox.Add(f.pane,h.get('expand'))
  vbox.Add(hbox.ctrl,expand)
 return vbox.ctrl

#
# Dialog
#

def AlertDialog(parent, message, title=None):
 JOptionPane.showMessageDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE);

#
# Window
#

class Window(JFrame):
 def __init__(self,title="",width=640,height=480):
  print('SwingWindow.__init__()');
  self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  #self.setBounds(100, 100, 515, 547);
  self.content = self.getContentPane()
  self.setLayout(BoxLayout(self.content, BoxLayout.Y_AXIS))
  self.setTitle(title);
  self.setSize(width,height)
 def Show(self,pack=False):
  if pack: self.pack()
  self.setVisible(True)
 def Hide(self):
  self.setVisible(False)
 def SetMenuBar(self,menu):
  self.setJMenuBar(MenuBar(menu))
 def SetContent(self,content):
  self.content.add(Layout(content))
 def Add(self,layout):
  self.content.add(layout)

#
# App
#

def onExit(e):
 System.exit(0);
 
mainmenu = [
 { 'name' : "File",
   'item' : [
   { 'name' : "Exit" , 'item' : onExit, 'image' : 'icon/exit.png', 'tooltip' : 'Exit Program' },
   { 'name' : "-" ,  },
   { 'name' : "About" , 'item' : onExit, 'image' : 'exit.png' } ]
 }, { 'name' : "Help",
   'item' : [
   { 'name' : "About", 'item' : onExit, 'check' : True, 'image' : 'new.png' } ]
 }]

content = [ # vbox
    [ # hbox
        { "name" : "TextArea", "key" : "text", "expand" : True, "menu" : mainmenu },
        { "expand" : True },
    ],  
 [ # hbox
    { "name" : "Spacer", 'width':1, 'height':8 },
 ],    
    [ # hbox
    { "name" : "Spacer" },
    { "name" : "Button", "key" : "text", "label" : "Hello", "tooltip" : "HeLLo", "handler" : onExit },
    { "name" : "Spacer", 'width':10, 'height':1 },
    ],                
 [ # hbox
    { "name" : "Spacer", 'width':1, 'height':8 },
 ],    
]

if __name__ == "__main__":
 app = Window("Hello", 300, 400)
 app.SetMenuBar(mainmenu)
 #text = TextArea()
 #app.Add(text.GetCtrl())
 app.SetContent(content)
 app.Show()

댓글 없음:

댓글 쓰기