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

History | View | Annotate | Download (10.7 KB)

1

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

    
22
import os
23
import os.path
24

    
25
import inspect
26

    
27
from org.gvsig.tools.swing.api import ToolsSwingLocator
28
from org.gvsig.tools.task import TaskStatus
29
from org.gvsig.tools.observer import Observer
30
from org.gvsig.tools import ToolsLocator
31
import org.gvsig.tools.swing.api.Component
32

    
33
def getResource(*args):
34
  base = args[0]
35
  if os.path.isfile(base):
36
    base = os.path.dirname(base)
37
  x = [ base,]
38
  x.extend(args[1:])
39
  return os.path.join(*x)
40

    
41
class ListenerAdapter:
42
  def __init__(self,function,componentName=None):
43
    self.function = function
44
    if componentName == None:
45
      self.componentName = self.function.__name__
46
    else:
47
      self.componentName = componentName
48

    
49
class ActionListenerAdapter(ListenerAdapter, java.awt.event.ActionListener):
50
  def __init__(self,function,componentName=None):
51
    ListenerAdapter.__init__(self,function,componentName)
52
  def actionPerformed(self,*args):
53
    #print "ActionListenerAdapter %s %s" % (self.componentName, args[0])
54
    self.function(*args)
55

    
56
class ChangeListenerAdapter(ListenerAdapter, javax.swing.event.ChangeListener):
57
  def __init__(self,function, componentName=None):
58
    ListenerAdapter.__init__(self,function,componentName)
59
  def stateChanged(self,*args):
60
    #print "ChangeListenerAdapter %s %s" % (self.componentName, args[0])
61
    self.function(*args)
62

    
63
class DocumentListenerAdapter(ListenerAdapter, javax.swing.event.DocumentListener):
64
  def __init__(self,function, componentName=None):
65
    ListenerAdapter.__init__(self,function,componentName)
66
  def insertUpdate(self,*args):
67
    #print "DocumentListenerAdapter %s" % self.componentName
68
    self.function(*args)
69
  def removeUpdate(self,*args):
70
    #print "DocumentListenerAdapter %s" % self.componentName
71
    self.function(*args)
72
  def changedUpdate(self,*args):
73
    #print "DocumentListenerAdapter %s" % self.componentName
74
    self.function(*args)
75

    
76
class ItemListenerAdapter(ListenerAdapter, java.awt.event.ItemListener):
77
  def __init__(self,function, componentName=None):
78
    ListenerAdapter.__init__(self,function,componentName)
79
  def itemStateChanged(self,*args):
80
    #print "ItemListenerAdapter %s %s" % (self.componentName, args[0])
81
    self.function(*args)
82

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

    
93
class FocusLostListenerAdapter(ListenerAdapter, java.awt.event.FocusListener):
94
  def __init__(self,function, componentName=None):
95
    ListenerAdapter.__init__(self,function,componentName)
96
  def focusGained(self,*args):
97
    pass
98
  def focusLost(self,*args):
99
    self.function(*args)
100

    
101
class MouseListenerAdapter(ListenerAdapter, java.awt.event.MouseListener):
102
  def __init__(self,function, componentName=None):
103
    ListenerAdapter.__init__(self,function,componentName)
104
  def mouseClicked(self,*args):
105
    self.function(*args)
106
  def mouseEntered(self,*args):
107
    self.function(*args)
108
  def mouseExited(self,*args):
109
    self.function(*args)
110
  def mousePressed(self,*args):
111
    self.function(*args)
112
  def mouseReleased(self,*args):
113
    self.function(*args)
114

    
115

    
116
class FunctionRunnable(java.lang.Runnable):
117
  def __init__(self,fn, *args):
118
    self.__fn = fn
119
    self.__args = args
120

    
121
  def run(self):
122
    self.__fn(*self.__args)
123

    
124
  def __call__(self):
125
    self.__fn(*self.__args)
126

    
127

    
128
class ProgressBarWithTaskStatus(Observer):
129
  def __init__(self, name, progressBar, labelProgress=None):
130
    self.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(name)
131
    self.progressBar = progressBar
132
    self.labelProgress = labelProgress
133
    self.progressBar.setMinimum(0)
134
    self.progressBar.setMaximum(100)
135
    self.progressBar.setValue(0)
136
    self.taskStatus.addObserver(self)
137

    
138
  def getTaskStatus(self):
139
    return self.taskStatus
140

    
141
  def update(self, taskStatus, notification):
142
    if not javax.swing.SwingUtilities.isEventDispatchThread():
143
      javax.swing.SwingUtilities.invokeLater(
144
        FunctionRunnable(
145
          self.updateGUI,
146
          taskStatus.isRunning(),
147
          taskStatus.getCompleted(),
148
          taskStatus.getLabel()
149
        )
150
      )
151
    else:
152
      self.updateGUI(
153
        taskStatus.isRunning(),
154
        taskStatus.getCompleted(),
155
        taskStatus.getLabel()
156
      )
157

    
158
  def updateGUI(self, isRunning, current, label):
159
    if not isRunning:
160
        self.progressBar.setIndeterminate(False);
161
        self.progressBar.setValue(100);
162
        self.labelProgress.setText("")
163
        return
164
    self.progressBar.setIndeterminate(self.taskStatus.isIndeterminate())
165
    self.progressBar.setValue(current)
166
    if self.labelProgress != None:
167
      self.labelProgress.setText(label)
168

    
169
def load_image(afile):
170
  if not isinstance(afile,java.io.File):
171
    if isinstance(afile,tuple) or isinstance(afile,list):
172
      afile = getResource(*afile)
173
    afile = java.io.File(str(afile))
174
  return javax.imageio.ImageIO.read(afile)
175

    
176
def load_icon(afile):
177
  if not isinstance(afile,java.io.File):
178
    if isinstance(afile,tuple) or isinstance(afile,list):
179
      afile = getResource(*afile)
180
    afile = java.io.File(str(afile))
181
  return javax.swing.ImageIcon(javax.imageio.ImageIO.read(afile))
182

    
183

    
184
class FormComponent(object):
185

    
186
  def load_image(self, afile):
187
    return load_image(afile)
188

    
189
  def load_icon(self, afile):
190
    return load_icon(afile)
191

    
192
  def autobind(self):
193
    members = inspect.getmembers(self)
194
    for methodName, method in members:
195
      if methodName.endswith("_click"):
196
        name = methodName[0:-len("_click")]
197
        component = getattr(self,name, None)
198
        if isinstance(component, javax.swing.JComboBox):
199
          component.addActionListener(ActionListenerAdapter(method,methodName))
200
        elif isinstance(component, javax.swing.AbstractButton):
201
          component.addActionListener(ActionListenerAdapter(method,methodName))
202
  
203
      elif methodName.endswith("_change"):
204
        name = methodName[0:-len("_change")]
205
        component = getattr(self,name, None)
206
        if isinstance(component, javax.swing.JComboBox):
207
          component.addItemListener(ItemListenerAdapter(method,methodName))
208
        if isinstance(component, javax.swing.JRadioButton):
209
          component.addItemListener(ItemListenerAdapter(method,methodName))
210
        elif isinstance(component, javax.swing.JSpinner):
211
          component.addChangeListener(ChangeListenerAdapter(method,methodName))
212
        elif isinstance(component, javax.swing.text.JTextComponent):
213
          component.getDocument().addDocumentListener(DocumentListenerAdapter(method,methodName))
214
        elif isinstance(component, javax.swing.AbstractButton):
215
          component.addActionListener(ActionListenerAdapter(method,methodName))
216
  
217
      elif methodName.endswith("_perform"):
218
        name = methodName[0:-len("_perform")]
219
        component = getattr(self,name, None)
220
        if isinstance(component, javax.swing.Timer):
221
          component.addActionListener(ActionListenerAdapter(method,methodName)) 
222

    
223
      elif methodName.endswith("_focusGained"):
224
        name = methodName[0:-len("_focusGained")]
225
        component = getattr(self,name, None)
226
        if component!=None:
227
          addFocusListener = getattr(component,"addFocusListener",None)
228
          if addFocusListener !=None:
229
            addFocusListener(FocusGainedListenerAdapter(method,methodName)) 
230
          
231
      elif methodName.endswith("_focusLost"):
232
        name = methodName[0:-len("_focusLost")]
233
        component = getattr(self,name, None)
234
        if component!=None:
235
          addFocusListener = getattr(component,"addFocusListener",None)
236
          if addFocusListener !=None:
237
            addFocusListener(FocusLostListenerAdapter(method,methodName)) 
238

    
239
      elif methodName.endswith("_mouseClick"):
240
        name = methodName[0:-len("_mouseClick")]
241
        component = getattr(self,name, None)
242
        if component!=None:
243
          addMouseListener = getattr(component,"addMouseListener",None)
244
          if addMouseListener !=None:
245
            addMouseListener(MouseListenerAdapter(method,methodName)) 
246
      
247
class FormPanel(FormComponent):
248

    
249
  def __init__(self,formfile=None):
250
    FormComponent.__init__(self)
251
    self._panel = None
252
    if formfile!=None:
253
      self.load(formfile)
254

    
255
  def getRootPane(self):
256
    return self.asJComponent().getParent().getParent()
257
    
258
  def getPreferredSize(self):
259
    return self.asJComponent().getPreferredSize()
260
    
261
  def setPreferredSize(self, withOrDimension, height=None):
262
    if height == None:
263
      self.asJComponent().setPreferredSize(withOrDimension)
264
    else:
265
      self.asJComponent().setPreferredSize(java.awt.Dimension(withOrDimension,height))
266
      
267
  def getPanel(self):
268
    return self.asJComponent()
269

    
270
  def asJComponent(self):
271
    return self._panel
272
  
273
  def hide(self):
274
    self._panel.setVisible(False)
275

    
276
  def __getattr__(self, name):
277
    if name in ("__members__", "__methods__"):
278
      raise AttributeError("FormPanel",name)
279
    attr = self._panel.getComponentByName(name)
280
    if attr == None:
281
      raise AttributeError("FormPanel",name)
282
    return attr
283

    
284
  def load(self, formfile):
285
    if not isinstance(formfile,java.io.File):
286
      if isinstance(formfile,tuple) or isinstance(formfile,list):
287
        formfile = getResource(*formfile)
288
      formfile = java.io.File(str(formfile))
289
    #print "FormPanel.load(%s)" % formfile
290
    self._panel = com.jeta.forms.components.panel.FormPanel( java.io.FileInputStream(formfile) )
291
    #print "FormPanel.load: _panel=%s" % self._panel
292
    self.autobind()
293

    
294
  def btnClose_click(self,*args):
295
    self.hide()
296

    
297
  def showWindow(self, title):
298
    self._panel.setVisible(True)
299
    windowManager = ToolsSwingLocator.getWindowManager()
300
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.WINDOW)
301

    
302
  def showTool(self, title):
303
    self._panel.setVisible(True)
304
    windowManager = ToolsSwingLocator.getWindowManager()
305
    windowManager.showWindow(self.asJComponent(),title, windowManager.MODE.TOOL)
306

    
307
  def showDialog(self, title):
308
    self._panel.setVisible(True)
309
    windowManager = ToolsSwingLocator.getWindowManager()
310
    windowManager.showWindow(self._panel,title, windowManager.MODE.DIALOG)
311