Revision 427

View differences:

org.gvsig.scripting.app/trunk/org.gvsig.scripting.app/org.gvsig.scripting.app.extension/src/main/resources/scripting/lib/commonsdialog.py
31 31
__docformat__ = 'plaintext'
32 32

  
33 33

  
34
from javax.swing import JOptionPane, JFileChooser
34
from javax.swing import JFileChooser
35
from javax.swing.filechooser import FileNameExtensionFilter
35 36

  
37
#from org.gvsig.app import ApplicationLocator      
36 38

  
37
from org.gvsig.app import ApplicationLocator      
38

  
39 39
from org.gvsig.andami.ui.mdiFrame import DefaultThreadSafeDialogs
40 40

  
41 41
#====================================
42 42
# SWING
43 43

  
44
# messageType options
44
#
45
#messageType options
45 46
FORBIDEN = 0
46 47
IDEA= 1
47 48
WARNING= 2
48 49
QUESTION= 3
49 50

  
50
# Confirmdialog optionType Options
51
#
52
#Confirmdialog optionType Options
51 53
YES_NO = 0
52 54
YES_NO_CANCEL = 1
53 55
ACEPT_CANCEL = 2
54 56

  
57
#filechooser options
58
OPEN_FILE = 0
59
OPEN_DIRECTORY = 1
60
SAVE_FILE = 2
61

  
62
#filechooser selectionMode
63
FILES_ONLY = JFileChooser.FILES_ONLY
64
DIRECTORIES_ONLY = JFileChooser.DIRECTORIES_ONLY
65

  
66

  
55 67
def msgbox(message, title="", messageType=IDEA):
56
  """
57
  Create a message dialog with ok button only
58
  
68
    """Shows a message dialog with ok button only
59 69
    :param message: text to present in the dialog
60 70
    :param title: title of the dialog
61 71
    :param messageType: type of icon to use.
62
  
63
  """
64
  DefaultThreadSafeDialogs().messageDialog(message, title, messageType)
72
    """
73
    DefaultThreadSafeDialogs().messageDialog(message, title, messageType)
65 74

  
66 75
   
67 76
def inputbox(message, title="", messageType=IDEA, initialValue=""):
68
  """
69
  Show a input dialog.
70

  
71
     :param message: text to present in the dialog
72
     :param title: title of the dialog
73
     :messageType: type of icon to use
74
     :initialValue: Initial value of the inputbox
77
    """
78
    Shows a input dialog.
79
    :param message: text to present in the dialog
80
    :param title: title of the dialog
81
    :messageType: type of icon to use
82
    :initialValue: Initial value of the inputbox  
83
    """
84
    return DefaultThreadSafeDialogs().inputDialog(message, title, 
85
        messageType, initialValue) 
75 86
  
76
  """
77
  return DefaultThreadSafeDialogs().inputDialog(message, title, messageType, initialValue) 
78 87
  
79
  
80 88
def confirmDialog(message, title="", optionType=YES_NO, messageType=IDEA):
81
  """
82
  Create a message dialog with options button
83
  
89
    """
90
    Create a message dialog with options button
84 91
    :param message: text to present in the dialog
85 92
    :param title: title of the dialog
86 93
    :optionType: bottons to show
87
    :param messageType: type of icon to use.
88
  
89
  """
90
  #0 : si/no option
91
  #1: si/no/cancelar
92
  #2: aceptar/cancelar
93
  return DefaultThreadSafeDialogs().confirmDialog(message,title, optionType, messageType) 
94
    :param messageType: type of icon to use.    
95
    """
96
    #0: yes/no option
97
    #1: yes/no/cancelar
98
    #2: accept/cancel
99
    return DefaultThreadSafeDialogs().confirmDialog(message, title, 
100
        optionType, messageType) 
94 101

  
95
def filechooser(option, title=""):
102
def filechooser(option, title="", initialPath=None, 
103
        multiselection=False, filter = None, fileHidingEnabled=True):
104
    """
105
    Allows configuration parameters to filechooser dialogs
106
    :int option: file chooser selection mode. Allowed values:
107
        OPEN_FILE, OPEN_DIRECTORY, SAVE_FILE
108
    :String title: Window title
109
    :String initialPath: Initial path to the directory to open in the dialog
110
    :boolean multiselection: Allow select more than one object.
111
    :String[] filter: list of acepted extension files ("jpg", "png", "gif")
112
    :boolean fileHidingEnabled: True if hidden files are not displayed
113
    """ 
114
    #
115
    #Values for open files dialog
116
    dialogType = JFileChooser.OPEN_DIALOG
117
    selectionMode = FILES_ONLY
118
    
119
    if option == SAVE_FILE:
120
        dialogType = JFileChooser.SAVE_DIALOG
121
    elif option == OPEN_DIRECTORY:
122
        selectionMode = DIRECTORIES_ONLY
96 123

  
97
  if option == "openFile":
98
    return openFileDialog(title)
99
  elif option == "openFolder":
100
    return openFolderDialog(title)
101
  elif option == "saveFile":
102
    return saveFileDialog(title)
103
  return None
104

  
105
def openFileDialog(title=None):
106
  fc = JFileChooser()
107
  if title!=None:
108
    fc.setDialogTitle(title)
109
  fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES)
110
  val = fc.showOpenDialog(None)
111
  if val == JFileChooser.APPROVE_OPTION:
112
    return fc.getSelectedFile().getAbsolutePath()
124
    initialPath = getJavaFile(initialPath)
113 125
   
114
  return None
126
    if filter:
127
        filter = FileNameExtensionFilter("Files", filter)
128
       
129
    return DefaultThreadSafeDialogs().showChooserDialog(
130
            title,
131
            dialogType,
132
            selectionMode,
133
            multiselection,
134
            initialPath,
135
            filter,
136
            fileHidingEnabled)
137
   
138
def openFileDialog(title='', initialPath=None):
139
    """
140
    Shows a window dialog to choose one file. 
141
    :param title: Window title. Default ''
142
    :type title: string
143
    :param initialPath: Initial path to open in window dialog
144
    :type initialPath: string
145
    """
146
  
147
    return DefaultThreadSafeDialogs().showOpenFileDialog(
148
        title, 
149
        getJavaFile(initialPath))
115 150

  
116
def openFolderDialog(title=None):
117
  fc = JFileChooser()
118
  if title!=None:
119
    fc.setDialogTitle(title)
120
  fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
121
  val = fc.showOpenDialog(None)
122
  if val == JFileChooser.APPROVE_OPTION:
123
    return fc.getSelectedFile().getAbsolutePath()
124
  return None
151
def openDirectoryDialog(title='', initialPath=None):
152
    """
153
    Shows a window dialog to choose one folder. 
154
    :param title: Window title. Default ''
155
    :type title: string
156
    :param initialPath: Initial path to open in window dialog
157
    :type initialPath: string
158
    """    
159
  
160
    return DefaultThreadSafeDialogs().showOpenDirectoryDialog(
161
        title, 
162
        getJavaFile(initialPath))
163
  
164
def saveFileDialog(title='', initialPath=None):
165
    """
166
    Shows a window dialog to choose one file. 
167
    :param title: Window title. Default ''
168
    :type title: string
169
    :param initialPath: Initial path to open in window dialog
170
    :type initialPath: string
171
    """    
172
    
173
    return DefaultThreadSafeDialogs().showSaveFileDialog(
174
        title, 
175
        getJavaFile(initialPath)
176
    )
177
  
178
def getJavaFile(path):
179
    """Returns a java File using parameter path.
180
    If path doesn't exists looks for user home folder and if can not find it, 
181
    returns path will be gvSIG instance directory.
182
    """
183
    #
184
    #Needs because DefaultThreadSafeDialogs files methods use a java File 
185
    #instance as initialPath parameter
186
    #
187
    import os
188
    import java.io.File as JFile  
189
  
190
    if path:
191
        path = os.path.normpath(path)
192
        if os.path.exists(path):
193
            return JFile(path)
194
        
195
    if os.environ.has_key('HOME'):
196
        return JFile(os.environ.get('HOME', os.getcwd()))
125 197

  
126
def saveFileDialog(title=None):
127
  fc = JFileChooser()
128
  if title!=None:
129
    fc.setDialogTitle(title)
130
  fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES)
131
  val = fc.showSaveDialog(None)
132
  if val == JFileChooser.APPROVE_OPTION:
133
    return fc.getSelectedFile().getAbsolutePath()
134
  return None
198
    if os.path.exists(os.path.expanduser("~")):
199
        return JFile(os.path.expanduser("~"))
200
    
201
    return JFile(os.getcwd())
202
    
203
    

Also available in: Unified diff