gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources / scripting / lib / commonsdialog.py @ 432
History | View | Annotate | Download (5.93 KB)
1 | 355 | jjdelcerro | # -*- coding: utf-8 -*-
|
---|---|---|---|
2 | #
|
||
3 | # File: commonsdialog.py
|
||
4 | #
|
||
5 | # Copyright (c) 2011 by Model Driven Development sl and Antonio Carrasco Valero
|
||
6 | #
|
||
7 | # GNU General Public License (GPL)
|
||
8 | #
|
||
9 | # This program is free software; you can redistribute it and/or
|
||
10 | # modify it under the terms of the GNU General Public License
|
||
11 | # as published by the Free Software Foundation; either version 2
|
||
12 | # of the License, or (at your option) any later version.
|
||
13 | #
|
||
14 | # This program is distributed in the hope that it will be useful,
|
||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
17 | # GNU General Public License for more details.
|
||
18 | #
|
||
19 | # You should have received a copy of the GNU General Public License
|
||
20 | # along with this program; if not, write to the Free Software
|
||
21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||
22 | # 02110-1301, USA.
|
||
23 | #
|
||
24 | #
|
||
25 | |||
26 | __author__ = """Antonio Carrasco Valero
|
||
27 | Model Driven Development sl and Antonio Carrasco Valero
|
||
28 | 399 | vacevedo | <carrasco@modeldd.org>
|
29 | Victor Acevedo Royer <vacevedo@gvsig.com> <vacevedor@gmail.com>
|
||
30 | """
|
||
31 | 355 | jjdelcerro | __docformat__ = 'plaintext'
|
32 | |||
33 | |||
34 | 427 | vacevedo | from javax.swing import JFileChooser |
35 | from javax.swing.filechooser import FileNameExtensionFilter |
||
36 | 355 | jjdelcerro | |
37 | 427 | vacevedo | #from org.gvsig.app import ApplicationLocator
|
38 | 362 | vacevedo | |
39 | from org.gvsig.andami.ui.mdiFrame import DefaultThreadSafeDialogs |
||
40 | |||
41 | 355 | jjdelcerro | #====================================
|
42 | # SWING
|
||
43 | |||
44 | 427 | vacevedo | #
|
45 | #messageType options
|
||
46 | 381 | vacevedo | FORBIDEN = 0
|
47 | IDEA= 1
|
||
48 | WARNING= 2
|
||
49 | QUESTION= 3
|
||
50 | 372 | vacevedo | |
51 | 427 | vacevedo | #
|
52 | #Confirmdialog optionType Options
|
||
53 | 381 | vacevedo | YES_NO = 0
|
54 | YES_NO_CANCEL = 1
|
||
55 | ACEPT_CANCEL = 2
|
||
56 | 372 | vacevedo | |
57 | 427 | vacevedo | #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 | |||
67 | 399 | vacevedo | def msgbox(message, title="", messageType=IDEA): |
68 | 427 | vacevedo | """Shows a message dialog with ok button only
|
69 | 362 | vacevedo | :param message: text to present in the dialog
|
70 | :param title: title of the dialog
|
||
71 | :param messageType: type of icon to use.
|
||
72 | 427 | vacevedo | """
|
73 | DefaultThreadSafeDialogs().messageDialog(message, title, messageType) |
||
74 | 355 | jjdelcerro | |
75 | 362 | vacevedo | |
76 | 399 | vacevedo | def inputbox(message, title="", messageType=IDEA, initialValue=""): |
77 | 427 | vacevedo | """
|
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) |
||
86 | 362 | vacevedo | |
87 | |||
88 | 399 | vacevedo | def confirmDialog(message, title="", optionType=YES_NO, messageType=IDEA): |
89 | 427 | vacevedo | """
|
90 | Create a message dialog with options button
|
||
91 | 362 | vacevedo | :param message: text to present in the dialog
|
92 | :param title: title of the dialog
|
||
93 | :optionType: bottons to show
|
||
94 | 427 | vacevedo | :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) |
||
101 | 362 | vacevedo | |
102 | 427 | vacevedo | 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 |
||
123 | 362 | vacevedo | |
124 | 427 | vacevedo | initialPath = getJavaFile(initialPath) |
125 | 362 | vacevedo | |
126 | 427 | vacevedo | 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)) |
||
150 | 355 | jjdelcerro | |
151 | 432 | vacevedo | def openFolderDialog(title='', initialPath=None): |
152 | 427 | vacevedo | """
|
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())) |
||
197 | 355 | jjdelcerro | |
198 | 427 | vacevedo | if os.path.exists(os.path.expanduser("~")): |
199 | return JFile(os.path.expanduser("~")) |
||
200 | |||
201 | return JFile(os.getcwd())
|
||
202 |