Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / gvsig / libs / formpanel.py @ 743

History | View | Annotate | Download (12.3 KB)

1

    
2
import com.jeta.forms.components.panel.FormPanel
3

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

    
25
import os
26
import os.path
27
import threading
28

    
29
import inspect
30

    
31
from gvsig import getResource
32

    
33
from org.gvsig.tools.swing.api import ToolsSwingLocator
34
from org.gvsig.tools.task import TaskStatus
35
from org.gvsig.tools.observer import Observer
36
from org.gvsig.tools import ToolsLocator
37
import org.gvsig.tools.swing.api.Component
38

    
39

    
40
class ListenerAdapter(object):
41
  def __init__(self,function,componentName=None):
42
    self.function = function
43
    self.synchronous  = True
44
    if componentName == None:
45
      self.componentName = self.function.__name__
46
    else:
47
      self.componentName = componentName
48
    x = getattr(function,"im_func",function)
49
    x.setSynchronous = self.setSynchronous
50
  def setSynchronous(self, value):
51
    self.synchronous = value
52
  def __call__(self, *args):
53
    if self.synchronous:
54
      self.function(*args)
55
    else:
56
      threading.Thread(target=self.function, name=self.componentName, args=args).start()
57

    
58
class ActionListenerAdapter(ListenerAdapter, java.awt.event.ActionListener):
59
  def __init__(self,function,componentName=None):
60
    ListenerAdapter.__init__(self,function,componentName)
61
  def actionPerformed(self,*args):
62
    #print "ActionListenerAdapter %s %s" % (self.componentName, args[0])
63
    self(*args)
64

    
65
class ChangeListenerAdapter(ListenerAdapter, javax.swing.event.ChangeListener):
66
  def __init__(self,function, componentName=None):
67
    ListenerAdapter.__init__(self,function,componentName)
68
  def stateChanged(self,*args):
69
    #print "ChangeListenerAdapter %s %s" % (self.componentName, args[0])
70
    self(*args)
71

    
72
class DocumentListenerAdapter(ListenerAdapter, javax.swing.event.DocumentListener):
73
  def __init__(self,function, componentName=None):
74
    ListenerAdapter.__init__(self,function,componentName)
75
  def insertUpdate(self,*args):
76
    #print "DocumentListenerAdapter %s" % self.componentName
77
    self.function(*args)
78
  def removeUpdate(self,*args):
79
    #print "DocumentListenerAdapter %s" % self.componentName
80
    self(*args)
81
  def changedUpdate(self,*args):
82
    #print "DocumentListenerAdapter %s" % self.componentName
83
    self(*args)
84

    
85
class ItemListenerAdapter(ListenerAdapter, java.awt.event.ItemListener):
86
  def __init__(self,function, componentName=None):
87
    ListenerAdapter.__init__(self,function,componentName)
88
  def itemStateChanged(self,*args):
89
    #print "ItemListenerAdapter %s %s" % (self.componentName, args[0])
90
    self(*args)
91

    
92
class ListSelectionListenerAdapter(ListenerAdapter, javax.swing.event.ListSelectionListener):
93
  def __init__(self,function, componentName=None):
94
    ListenerAdapter.__init__(self,function,componentName)
95
  def valueChanged(self,*args):
96
    #print "ListSelectionListenerAdapter %s %s" % (self.componentName, args[0])
97
    self(*args)
98

    
99
class FocusGainedListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
100
  def __init__(self,function, componentName=None):
101
    ListenerAdapter.__init__(self,function,componentName)
102
  def focusGained(self,*args):
103
    #print "focusGained %s %s" % (self.componentName, args[0])
104
    self(*args)
105
  def focusLost(self,*args):
106
    #print "focusLost %s %s" % (self.componentName, args[0])
107
    pass
108

    
109
class FocusLostListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
110
  def __init__(self,function, componentName=None):
111
    ListenerAdapter.__init__(self,function,componentName)
112
  def focusGained(self,*args):
113
    pass
114
  def focusLost(self,*args):
115
    self(*args)
116

    
117
class MouseListenerAdapter(ListenerAdapter, java.awt.event.MouseListener):
118
  def __init__(self,function, componentName=None):
119
    ListenerAdapter.__init__(self,function,componentName)
120
  def mouseClicked(self,*args):
121
    self(*args)
122
  def mouseEntered(self,*args):
123
    self(*args)
124
  def mouseExited(self,*args):
125
    self(*args)
126
  def mousePressed(self,*args):
127
    self(*args)
128
  def mouseReleased(self,*args):
129
    self(*args)
130

    
131
class KeyPressedListenerAdapter(ListenerAdapter, java.awt.event.KeyListener):
132
  def __init__(self,function, componentName=None):
133
    ListenerAdapter.__init__(self,function,componentName)
134
  def keyTyped(self,*args):
135
    pass
136
  def keyPressed(self,*args):
137
    try:
138
      self(*args)
139
    except java.lang.Throwable, t:
140
      pass
141
  def keyReleased(self,*args):
142
    pass
143

    
144
class FunctionRunnable(java.lang.Runnable):
145
  def __init__(self,fn, *args):
146
    self.__fn = fn
147
    self.__args = args
148

    
149
  def run(self):
150
    self.__fn(*self.__args)
151

    
152
  def __call__(self):
153
    self.__fn(*self.__args)
154

    
155

    
156
class ProgressBarWithTaskStatus(Observer):
157
  def __init__(self, name, progressBar, labelProgress=None):
158
    self.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(name)
159
    self.progressBar = progressBar
160
    self.labelProgress = labelProgress
161
    self.progressBar.setMinimum(0)
162
    self.progressBar.setMaximum(100)
163
    self.progressBar.setValue(0)
164
    self.taskStatus.addObserver(self)
165

    
166
  def getTaskStatus(self):
167
    return self.taskStatus
168

    
169
  def update(self, taskStatus, notification):
170
    if not javax.swing.SwingUtilities.isEventDispatchThread():
171
      javax.swing.SwingUtilities.invokeLater(
172
        FunctionRunnable(
173
          self.updateGUI,
174
          taskStatus.isRunning(),
175
          taskStatus.getCompleted(),
176
          taskStatus.getLabel()
177
        )
178
      )
179
    else:
180
      self.updateGUI(
181
        taskStatus.isRunning(),
182
        taskStatus.getCompleted(),
183
        taskStatus.getLabel()
184
      )
185

    
186
  def updateGUI(self, isRunning, current, label):
187
    if not isRunning:
188
        self.progressBar.setIndeterminate(False)
189
        self.progressBar.setValue(100)
190
        self.labelProgress.setText("")
191
        return
192
    self.progressBar.setIndeterminate(self.taskStatus.isIndeterminate())
193
    self.progressBar.setValue(current)
194
    if self.labelProgress != None:
195
      self.labelProgress.setText(label)
196

    
197
def load_image(afile):
198
  if not isinstance(afile,java.io.File):
199
    if isinstance(afile,tuple) or isinstance(afile,list):
200
      afile = getResource(*afile)
201
    afile = java.io.File(str(afile))
202
  return javax.imageio.ImageIO.read(afile)
203

    
204
def load_icon(afile):
205
  if not isinstance(afile,java.io.File):
206
    if isinstance(afile,tuple) or isinstance(afile,list):
207
      afile = getResource(*afile)
208
    afile = java.io.File(str(afile))
209
  return javax.swing.ImageIcon(javax.imageio.ImageIO.read(afile))
210

    
211
class ComboItem(object):
212
  def __init__(self, label="", value=None):
213
    self.__label = label
214
    self.__value = value
215

    
216
  def getLabel(self):
217
    return self.__label
218

    
219
  def getValue(self):
220
    return self.__value
221

    
222
  __str__ = getLabel
223
  __repr__ = getLabel
224

    
225
class FormComponent(object):
226

    
227
  def load_image(self, afile):
228
    return load_image(afile)
229

    
230
  def load_icon(self, afile):
231
    return load_icon(afile)
232

    
233
  def autobind(self):
234
    members = inspect.getmembers(self)
235
    for methodName, method in members:
236
      if methodName.endswith("_click"):
237
        name = methodName[0:-len("_click")]
238
        component = getattr(self,name, None)
239
        if isinstance(component, javax.swing.JComboBox):
240
          component.addActionListener(ActionListenerAdapter(method,methodName))
241
        elif isinstance(component, javax.swing.AbstractButton):
242
          component.addActionListener(ActionListenerAdapter(method,methodName))
243

    
244
      elif methodName.endswith("_change"):
245
        name = methodName[0:-len("_change")]
246
        component = getattr(self,name, None)
247
        if isinstance(component, javax.swing.JComboBox):
248
          component.addItemListener(ItemListenerAdapter(method,methodName))
249
        elif isinstance(component, javax.swing.JList):
250
          component.addListSelectionListener(ListSelectionListenerAdapter(method,methodName))
251
        elif isinstance(component, javax.swing.JRadioButton):
252
          component.addItemListener(ItemListenerAdapter(method,methodName))
253
        elif isinstance(component, javax.swing.JSpinner):
254
          component.addChangeListener(ChangeListenerAdapter(method,methodName))
255
        elif isinstance(component, javax.swing.text.JTextComponent):
256
          component.getDocument().addDocumentListener(DocumentListenerAdapter(method,methodName))
257
        elif isinstance(component, javax.swing.AbstractButton):
258
          component.addActionListener(ActionListenerAdapter(method,methodName))
259

    
260
      elif methodName.endswith("_perform"):
261
        name = methodName[0:-len("_perform")]
262
        component = getattr(self,name, None)
263
        if isinstance(component, javax.swing.Timer):
264
          component.addActionListener(ActionListenerAdapter(method,methodName))
265

    
266
      elif methodName.endswith("_focusGained"):
267
        name = methodName[0:-len("_focusGained")]
268
        component = getattr(self,name, None)
269
        if component!=None:
270
          addFocusListener = getattr(component,"addFocusListener",None)
271
          if addFocusListener !=None:
272
            addFocusListener(FocusGainedListenerAdapter(method,methodName))
273

    
274
      elif methodName.endswith("_focusLost"):
275
        name = methodName[0:-len("_focusLost")]
276
        component = getattr(self,name, None)
277
        if component!=None:
278
          addFocusListener = getattr(component,"addFocusListener",None)
279
          if addFocusListener !=None:
280
            addFocusListener(FocusLostListenerAdapter(method,methodName))
281

    
282
      elif methodName.endswith("_mouseClick"):
283
        name = methodName[0:-len("_mouseClick")]
284
        component = getattr(self,name, None)
285
        if component!=None:
286
          addMouseListener = getattr(component,"addMouseListener",None)
287
          if addMouseListener !=None:
288
            addMouseListener(MouseListenerAdapter(method,methodName))
289

    
290
      elif methodName.endswith("_keyPressed"):
291
        name = methodName[0:-len("_keyPressed")]
292
        component = getattr(self,name, None)
293
        if component!=None:
294
          addKeyListener = getattr(component,"addKeyListener",None)
295
          if addKeyListener != None:
296
            addKeyListener(KeyPressedListenerAdapter(method,methodName))
297

    
298
class FormPanel(FormComponent):
299

    
300
  def __init__(self,formfile=None):
301
    FormComponent.__init__(self)
302
    self._panel = None
303
    if formfile!=None:
304
      self.load(formfile)
305

    
306
  def getRootPane(self):
307
    return self.asJComponent().getParent().getParent()
308

    
309
  def getPreferredSize(self):
310
    return self.asJComponent().getPreferredSize()
311

    
312
  def setPreferredSize(self, withOrDimension, height=None):
313
    if height == None:
314
      self.asJComponent().setPreferredSize(withOrDimension)
315
    else:
316
      self.asJComponent().setPreferredSize(java.awt.Dimension(withOrDimension,height))
317

    
318
  def getPanel(self):
319
    return self.asJComponent()
320

    
321
  def asJComponent(self):
322
    return self._panel
323

    
324
  def hide(self):
325
    self._panel.setVisible(False)
326

    
327
  def __getattr__(self, name):
328
    if name in ("__members__", "__methods__"):
329
      raise AttributeError("FormPanel",name)
330
    attr = self._panel.getComponentByName(name)
331
    if attr == None:
332
      raise AttributeError("FormPanel",name)
333
    return attr
334

    
335
  def load(self, formfile):
336
    if not isinstance(formfile,java.io.File):
337
      if isinstance(formfile,tuple) or isinstance(formfile,list):
338
        formfile = getResource(*formfile)
339
      formfile = java.io.File(str(formfile))
340
    #print "FormPanel.load(%s)" % formfile
341
    self._panel = com.jeta.forms.components.panel.FormPanel( java.io.FileInputStream(formfile) )
342
    #print "FormPanel.load: _panel=%s" % self._panel
343
    self.autobind()
344

    
345
  def btnClose_click(self,*args):
346
    self.hide()
347

    
348
  def showWindow(self, title):
349
    self._panel.setVisible(True)
350
    windowManager = ToolsSwingLocator.getWindowManager()
351
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.WINDOW)
352

    
353
  def showTool(self, title):
354
    self._panel.setVisible(True)
355
    windowManager = ToolsSwingLocator.getWindowManager()
356
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.TOOL)
357

    
358
  def showDialog(self, title):
359
    self._panel.setVisible(True)
360
    windowManager = ToolsSwingLocator.getWindowManager()
361
    windowManager.showWindow(self._panel,title, windowManager.MODE.DIALOG)
362