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 @ 724

History | View | Annotate | Download (12.5 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 org.gvsig.tools.swing.api import ToolsSwingLocator
32
from org.gvsig.tools.task import TaskStatus
33
from org.gvsig.tools.observer import Observer
34
from org.gvsig.tools import ToolsLocator
35
import org.gvsig.tools.swing.api.Component
36

    
37
def getResource(*args):
38
  base = args[0]
39
  if os.path.isfile(base):
40
    base = os.path.dirname(base)
41
  x = [ base,]
42
  x.extend(args[1:])
43
  return os.path.join(*x)
44

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

    
63
class ActionListenerAdapter(ListenerAdapter, java.awt.event.ActionListener):
64
  def __init__(self,function,componentName=None):
65
    ListenerAdapter.__init__(self,function,componentName)
66
  def actionPerformed(self,*args):
67
    #print "ActionListenerAdapter %s %s" % (self.componentName, args[0])
68
    self(*args)
69

    
70
class ChangeListenerAdapter(ListenerAdapter, javax.swing.event.ChangeListener):
71
  def __init__(self,function, componentName=None):
72
    ListenerAdapter.__init__(self,function,componentName)
73
  def stateChanged(self,*args):
74
    #print "ChangeListenerAdapter %s %s" % (self.componentName, args[0])
75
    self(*args)
76

    
77
class DocumentListenerAdapter(ListenerAdapter, javax.swing.event.DocumentListener):
78
  def __init__(self,function, componentName=None):
79
    ListenerAdapter.__init__(self,function,componentName)
80
  def insertUpdate(self,*args):
81
    #print "DocumentListenerAdapter %s" % self.componentName
82
    self.function(*args)
83
  def removeUpdate(self,*args):
84
    #print "DocumentListenerAdapter %s" % self.componentName
85
    self(*args)
86
  def changedUpdate(self,*args):
87
    #print "DocumentListenerAdapter %s" % self.componentName
88
    self(*args)
89

    
90
class ItemListenerAdapter(ListenerAdapter, java.awt.event.ItemListener):
91
  def __init__(self,function, componentName=None):
92
    ListenerAdapter.__init__(self,function,componentName)
93
  def itemStateChanged(self,*args):
94
    #print "ItemListenerAdapter %s %s" % (self.componentName, args[0])
95
    self(*args)
96

    
97
class ListSelectionListenerAdapter(ListenerAdapter, javax.swing.event.ListSelectionListener):
98
  def __init__(self,function, componentName=None):
99
    ListenerAdapter.__init__(self,function,componentName)
100
  def valueChanged(self,*args):
101
    #print "ListSelectionListenerAdapter %s %s" % (self.componentName, args[0])
102
    self(*args)
103

    
104
class FocusGainedListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
105
  def __init__(self,function, componentName=None):
106
    ListenerAdapter.__init__(self,function,componentName)
107
  def focusGained(self,*args):
108
    #print "focusGained %s %s" % (self.componentName, args[0])
109
    self(*args)
110
  def focusLost(self,*args):
111
    #print "focusLost %s %s" % (self.componentName, args[0])
112
    pass
113

    
114
class FocusLostListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
115
  def __init__(self,function, componentName=None):
116
    ListenerAdapter.__init__(self,function,componentName)
117
  def focusGained(self,*args):
118
    pass
119
  def focusLost(self,*args):
120
    self(*args)
121

    
122
class MouseListenerAdapter(ListenerAdapter, java.awt.event.MouseListener):
123
  def __init__(self,function, componentName=None):
124
    ListenerAdapter.__init__(self,function,componentName)
125
  def mouseClicked(self,*args):
126
    self(*args)
127
  def mouseEntered(self,*args):
128
    self(*args)
129
  def mouseExited(self,*args):
130
    self(*args)
131
  def mousePressed(self,*args):
132
    self(*args)
133
  def mouseReleased(self,*args):
134
    self(*args)
135

    
136
class KeyPressedListenerAdapter(ListenerAdapter, java.awt.event.KeyListener):
137
  def __init__(self,function, componentName=None):
138
    ListenerAdapter.__init__(self,function,componentName)
139
  def keyTyped(self,*args):
140
    pass
141
  def keyPressed(self,*args):
142
    try:
143
      self(*args)
144
    except java.lang.Throwable, t:
145
      pass
146
  def keyReleased(self,*args):
147
    pass
148
    
149
class FunctionRunnable(java.lang.Runnable):
150
  def __init__(self,fn, *args):
151
    self.__fn = fn
152
    self.__args = args
153

    
154
  def run(self):
155
    self.__fn(*self.__args)
156

    
157
  def __call__(self):
158
    self.__fn(*self.__args)
159

    
160

    
161
class ProgressBarWithTaskStatus(Observer):
162
  def __init__(self, name, progressBar, labelProgress=None):
163
    self.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(name)
164
    self.progressBar = progressBar
165
    self.labelProgress = labelProgress
166
    self.progressBar.setMinimum(0)
167
    self.progressBar.setMaximum(100)
168
    self.progressBar.setValue(0)
169
    self.taskStatus.addObserver(self)
170

    
171
  def getTaskStatus(self):
172
    return self.taskStatus
173

    
174
  def update(self, taskStatus, notification):
175
    if not javax.swing.SwingUtilities.isEventDispatchThread():
176
      javax.swing.SwingUtilities.invokeLater(
177
        FunctionRunnable(
178
          self.updateGUI,
179
          taskStatus.isRunning(),
180
          taskStatus.getCompleted(),
181
          taskStatus.getLabel()
182
        )
183
      )
184
    else:
185
      self.updateGUI(
186
        taskStatus.isRunning(),
187
        taskStatus.getCompleted(),
188
        taskStatus.getLabel()
189
      )
190

    
191
  def updateGUI(self, isRunning, current, label):
192
    if not isRunning:
193
        self.progressBar.setIndeterminate(False)
194
        self.progressBar.setValue(100)
195
        self.labelProgress.setText("")
196
        return
197
    self.progressBar.setIndeterminate(self.taskStatus.isIndeterminate())
198
    self.progressBar.setValue(current)
199
    if self.labelProgress != None:
200
      self.labelProgress.setText(label)
201

    
202
def load_image(afile):
203
  if not isinstance(afile,java.io.File):
204
    if isinstance(afile,tuple) or isinstance(afile,list):
205
      afile = getResource(*afile)
206
    afile = java.io.File(str(afile))
207
  return javax.imageio.ImageIO.read(afile)
208

    
209
def load_icon(afile):
210
  if not isinstance(afile,java.io.File):
211
    if isinstance(afile,tuple) or isinstance(afile,list):
212
      afile = getResource(*afile)
213
    afile = java.io.File(str(afile))
214
  return javax.swing.ImageIcon(javax.imageio.ImageIO.read(afile))
215

    
216
class ComboItem(object):
217
  def __init__(self, label="", value=None):
218
    self.__label = label
219
    self.__value = value
220

    
221
  def getLabel(self):
222
    return self.__label
223

    
224
  def getValue(self):
225
    return self.__value
226

    
227
  __str__ = getLabel
228
  __repr__ = getLabel
229

    
230
class FormComponent(object):
231

    
232
  def load_image(self, afile):
233
    return load_image(afile)
234

    
235
  def load_icon(self, afile):
236
    return load_icon(afile)
237

    
238
  def autobind(self):
239
    members = inspect.getmembers(self)
240
    for methodName, method in members:
241
      if methodName.endswith("_click"):
242
        name = methodName[0:-len("_click")]
243
        component = getattr(self,name, None)
244
        if isinstance(component, javax.swing.JComboBox):
245
          component.addActionListener(ActionListenerAdapter(method,methodName))
246
        elif isinstance(component, javax.swing.AbstractButton):
247
          component.addActionListener(ActionListenerAdapter(method,methodName))
248
  
249
      elif methodName.endswith("_change"):
250
        name = methodName[0:-len("_change")]
251
        component = getattr(self,name, None)
252
        if isinstance(component, javax.swing.JComboBox):
253
          component.addItemListener(ItemListenerAdapter(method,methodName))
254
        elif isinstance(component, javax.swing.JList):
255
          component.addListSelectionListener(ListSelectionListenerAdapter(method,methodName))
256
        elif isinstance(component, javax.swing.JRadioButton):
257
          component.addItemListener(ItemListenerAdapter(method,methodName))
258
        elif isinstance(component, javax.swing.JSpinner):
259
          component.addChangeListener(ChangeListenerAdapter(method,methodName))
260
        elif isinstance(component, javax.swing.text.JTextComponent):
261
          component.getDocument().addDocumentListener(DocumentListenerAdapter(method,methodName))
262
        elif isinstance(component, javax.swing.AbstractButton):
263
          component.addActionListener(ActionListenerAdapter(method,methodName))
264
  
265
      elif methodName.endswith("_perform"):
266
        name = methodName[0:-len("_perform")]
267
        component = getattr(self,name, None)
268
        if isinstance(component, javax.swing.Timer):
269
          component.addActionListener(ActionListenerAdapter(method,methodName)) 
270

    
271
      elif methodName.endswith("_focusGained"):
272
        name = methodName[0:-len("_focusGained")]
273
        component = getattr(self,name, None)
274
        if component!=None:
275
          addFocusListener = getattr(component,"addFocusListener",None)
276
          if addFocusListener !=None:
277
            addFocusListener(FocusGainedListenerAdapter(method,methodName)) 
278
          
279
      elif methodName.endswith("_focusLost"):
280
        name = methodName[0:-len("_focusLost")]
281
        component = getattr(self,name, None)
282
        if component!=None:
283
          addFocusListener = getattr(component,"addFocusListener",None)
284
          if addFocusListener !=None:
285
            addFocusListener(FocusLostListenerAdapter(method,methodName)) 
286

    
287
      elif methodName.endswith("_mouseClick"):
288
        name = methodName[0:-len("_mouseClick")]
289
        component = getattr(self,name, None)
290
        if component!=None:
291
          addMouseListener = getattr(component,"addMouseListener",None)
292
          if addMouseListener !=None:
293
            addMouseListener(MouseListenerAdapter(method,methodName)) 
294

    
295
      elif methodName.endswith("_keyPressed"):
296
        name = methodName[0:-len("_keyPressed")]
297
        component = getattr(self,name, None)
298
        if component!=None:
299
          addKeyListener = getattr(component,"addKeyListener",None)
300
          if addKeyListener != None:
301
            addKeyListener(KeyPressedListenerAdapter(method,methodName))
302

    
303
class FormPanel(FormComponent):
304

    
305
  def __init__(self,formfile=None):
306
    FormComponent.__init__(self)
307
    self._panel = None
308
    if formfile!=None:
309
      self.load(formfile)
310

    
311
  def getRootPane(self):
312
    return self.asJComponent().getParent().getParent()
313
    
314
  def getPreferredSize(self):
315
    return self.asJComponent().getPreferredSize()
316
    
317
  def setPreferredSize(self, withOrDimension, height=None):
318
    if height == None:
319
      self.asJComponent().setPreferredSize(withOrDimension)
320
    else:
321
      self.asJComponent().setPreferredSize(java.awt.Dimension(withOrDimension,height))
322
      
323
  def getPanel(self):
324
    return self.asJComponent()
325

    
326
  def asJComponent(self):
327
    return self._panel
328
  
329
  def hide(self):
330
    self._panel.setVisible(False)
331

    
332
  def __getattr__(self, name):
333
    if name in ("__members__", "__methods__"):
334
      raise AttributeError("FormPanel",name)
335
    attr = self._panel.getComponentByName(name)
336
    if attr == None:
337
      raise AttributeError("FormPanel",name)
338
    return attr
339

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

    
350
  def btnClose_click(self,*args):
351
    self.hide()
352

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

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

    
363
  def showDialog(self, title):
364
    self._panel.setVisible(True)
365
    windowManager = ToolsSwingLocator.getWindowManager()
366
    windowManager.showWindow(self._panel,title, windowManager.MODE.DIALOG)
367