Revision 629 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/resources-plugin/scripting/scripts/libs/formpanel.py

View differences:

formpanel.py
3 3
import java.io.FileInputStream
4 4
import java.io.File
5 5
import javax.imageio.ImageIO
6
import javax.swing.AbstractButton
6 7
import javax.swing.JButton
7 8
import javax.swing.JComboBox
9
import javax.swing.JRadioButton
10
import javax.swing.JSpinner
8 11
import java.awt.event.ActionListener
12
import javax.swing.event.ChangeListener
13
import javax.swing.event.DocumentListener
14
import java.awt.event.ItemListener
9 15
import javax.swing.SwingUtilities
10 16
import java.lang.Runnable
11 17
import javax.swing.ImageIcon
18
import javax.swing.text.JTextComponent
19
import javax.swing.Timer
12 20

  
13 21

  
14 22
import inspect
......
17 25
from org.gvsig.tools.task import TaskStatus
18 26
from org.gvsig.tools.observer import Observer
19 27
from org.gvsig.tools import ToolsLocator
28
import org.gvsig.tools.swing.api.Component
20 29

  
21
class ComboBoxActionListener(java.awt.event.ActionListener):
22
  def __init__(self,function):
30
class ListenerAdapter:
31
  def __init__(self,function,componentName=None):
23 32
    self.function = function
33
    if componentName == None:
34
      self.componentName = self.function.__name__
35
    else:
36
      self.componentName = componentName
37

  
38
class ActionListenerAdapter(ListenerAdapter, java.awt.event.ActionListener):
39
  def __init__(self,function,componentName=None):
40
    ListenerAdapter.__init__(self,function,componentName)
24 41
  def actionPerformed(self,*args):
42
    #print "ActionListenerAdapter %s %s" % (self.componentName, args[0])
25 43
    self.function(*args)
26 44

  
45
class ChangeListenerAdapter(ListenerAdapter, javax.swing.event.ChangeListener):
46
  def __init__(self,function, componentName=None):
47
    ListenerAdapter.__init__(self,function,componentName)
48
  def stateChanged(self,*args):
49
    #print "ChangeListenerAdapter %s %s" % (self.componentName, args[0])
50
    self.function(*args)
51

  
52
class DocumentListenerAdapter(ListenerAdapter, javax.swing.event.DocumentListener):
53
  def __init__(self,function, componentName=None):
54
    ListenerAdapter.__init__(self,function,componentName)
55
  def insertUpdate(self,*args):
56
    #print "DocumentListenerAdapter %s" % self.componentName
57
    self.function(*args)
58
  def removeUpdate(self,*args):
59
    #print "DocumentListenerAdapter %s" % self.componentName
60
    self.function(*args)
61
  def changedUpdate(self,*args):
62
    #print "DocumentListenerAdapter %s" % self.componentName
63
    self.function(*args)
64

  
65
class ItemListenerAdapter(ListenerAdapter, java.awt.event.ItemListener):
66
  def __init__(self,function, componentName=None):
67
    ListenerAdapter.__init__(self,function,componentName)
68
  def itemStateChanged(self,*args):
69
    #print "ItemListenerAdapter %s %s" % (self.componentName, args[0])
70
    self.function(*args)
71

  
72
class FocusGainedListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
73
  def __init__(self,function, componentName=None):
74
    ListenerAdapter.__init__(self,function,componentName)
75
  def focusGained(self,*args):
76
    #print "focusGained %s %s" % (self.componentName, args[0])
77
    self.function(*args)
78
  def focusLost(self,*args):
79
    #print "focusLost %s %s" % (self.componentName, args[0])
80
    pass
81

  
82
class FocusLostListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
83
  def __init__(self,function, componentName=None):
84
    ListenerAdapter.__init__(self,function,componentName)
85
  def focusGained(self,*args):
86
    #print "focusGained %s %s" % (self.componentName, args[0])
87
    pass
88
  def focusLost(self,*args):
89
    #print "focusLost %s %s" % (self.componentName, args[0])
90
    self.function(*args)
91

  
92

  
93

  
27 94
class FunctionRunnable(java.lang.Runnable):
28 95
  def __init__(self,fn, *args):
29 96
    self.__fn = fn
......
77 144
    if self.labelProgress != None:
78 145
      self.labelProgress.setText(label)
79 146

  
80
class FormPanel(object):
147
def load_image(afile):
148
  if not isinstance(afile,java.io.File):
149
    afile = java.io.File(str(afile))
150
  return javax.imageio.ImageIO.read(afile)
81 151

  
152
def load_icon(afile):
153
  if not isinstance(afile,java.io.File):
154
    afile = java.io.File(str(afile))
155
  return javax.swing.ImageIcon(javax.imageio.ImageIO.read(afile))
156

  
157

  
158
class FormComponent(object):
159

  
160
  def load_image(self, afile):
161
    return load_image(afile)
162

  
163
  def load_icon(self, afile):
164
    return load_icon(afile)
165

  
166
  def autobind(self):
167
    members = inspect.getmembers(self)
168
    for methodName, method in members:
169
      if methodName.endswith("_click"):
170
        name = methodName[0:-len("_click")]
171
        component = getattr(self,name, None)
172
        if isinstance(component, javax.swing.JComboBox):
173
          component.addActionListener(ActionListenerAdapter(method,methodName))
174
        elif isinstance(component, javax.swing.AbstractButton):
175
          component.addActionListener(ActionListenerAdapter(method,methodName))
176
  
177
      elif methodName.endswith("_change"):
178
        name = methodName[0:-len("_change")]
179
        component = getattr(self,name, None)
180
        if isinstance(component, javax.swing.JComboBox):
181
          component.addItemListener(ItemListenerAdapter(method,methodName))
182
        if isinstance(component, javax.swing.JRadioButton):
183
          component.addItemListener(ItemListenerAdapter(method,methodName))
184
        elif isinstance(component, javax.swing.JSpinner):
185
          component.addChangeListener(ChangeListenerAdapter(method,methodName))
186
        elif isinstance(component, javax.swing.text.JTextComponent):
187
          component.getDocument().addDocumentListener(DocumentListenerAdapter(method,methodName))
188
        elif isinstance(component, javax.swing.AbstractButton):
189
          component.addActionListener(ActionListenerAdapter(method,methodName))
190
  
191
      elif methodName.endswith("_perform"):
192
        name = methodName[0:-len("_perform")]
193
        component = getattr(self,name, None)
194
        if isinstance(component, javax.swing.Timer):
195
          component.addActionListener(ActionListenerAdapter(method,methodName)) 
196

  
197
      elif methodName.endswith("_focusGained"):
198
        name = methodName[0:-len("_focusGained")]
199
        component = getattr(self,name, None)
200
        if component!=None:
201
          addFocusListener = getattr(component,"addFocusListener",None)
202
          if addFocusListener !=None:
203
            addFocusListener(FocusGainedListenerAdapter(method,methodName)) 
204
          
205
      elif methodName.endswith("_focusLost"):
206
        name = methodName[0:-len("_focusLost")]
207
        component = getattr(self,name, None)
208
        if component!=None:
209
          addFocusListener = getattr(component,"addFocusListener",None)
210
          if addFocusListener !=None:
211
            addFocusListener(FocusLostListenerAdapter(method,methodName)) 
212

  
213
      
214
class FormPanel(FormComponent):
215

  
82 216
  def __init__(self,formfile=None):
217
    FormComponent.__init__(self)
83 218
    self._panel = None
84 219
    if formfile!=None:
85 220
      self.load(formfile)
86 221

  
222
  def getRootPane(self):
223
    return self.asJComponent().getParent().getParent()
224
    
225
  def getPreferredSize(self):
226
    return self.asJComponent().getPreferredSize()
227
    
228
  def setPreferredSize(self, withOrDimension, height=None):
229
    if height == None:
230
      self.asJComponent().setPreferredSize(withOrDimension)
231
    else:
232
      self.asJComponent().setPreferredSize(java.awt.Dimension(withOrDimension,height))
233
      
87 234
  def getPanel(self):
235
    return self.asJComponent()
236

  
237
  def asJComponent(self):
88 238
    return self._panel
89

  
239
  
90 240
  def hide(self):
91 241
    self._panel.setVisible(False)
92 242

  
......
98 248
      raise AttributeError("FormPanel",name)
99 249
    return attr
100 250

  
101

  
102 251
  def load(self, formfile):
252
    if not isinstance(formfile,java.io.File):
253
      formfile = java.io.File(str(formfile))
254
    #print "FormPanel.load(%s)" % formfile
103 255
    self._panel = com.jeta.forms.components.panel.FormPanel( java.io.FileInputStream(formfile) )
104
    members = inspect.getmembers(self)
105
    for name, value in members:
106
      if name.endswith("_click"):
107
        name = name[0:-len("_click")]
108
        try:
109
              component = getattr(self,name)
110
              if isinstance(component, javax.swing.JButton):
111
                component.actionPerformed.append(value)
112
              elif isinstance(component, javax.swing.JComboBox):
113
                component.addActionListener(ComboBoxActionListener(value))
114
              else:
115
                print component
116
        except:
117
          pass
256
    #print "FormPanel.load: _panel=%s" % self._panel
257
    self.autobind()
118 258

  
119 259
  def btnClose_click(self,*args):
120 260
    self.hide()
121 261

  
122
  def load_image(self, afile):
123
    if not isinstance(afile,java.io.File):
124
      afile = java.io.File(str(afile))
125
    return javax.imageio.ImageIO.read(afile)
126

  
127
  def load_icon(self, afile):
128
    if not isinstance(afile,java.io.File):
129
      afile = java.io.File(str(afile))
130
    return javax.swing.ImageIcon(javax.imageio.ImageIO.read(afile))
131

  
132 262
  def showWindow(self, title):
133 263
    windowManager = ToolsSwingLocator.getWindowManager()
134
    windowManager.showWindow(self._panel,title, windowManager.MODE.WINDOW)
264
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.WINDOW)
135 265

  
136 266
  def showTool(self, title):
137 267
    windowManager = ToolsSwingLocator.getWindowManager()
138
    windowManager.showWindow(self._panel,title, windowManager.MODE.TOOL)
268
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.TOOL)
139 269

  
140 270
  def showDialog(self, title):
141 271
    windowManager = ToolsSwingLocator.getWindowManager()

Also available in: Unified diff