Merge lp:~tapaal-contributor/tapaal/cpn-openPNML2 into lp:~tapaal-contributor/tapaal/cpn-gui-dev
- cpn-openPNML2
- Merge into cpn-gui-dev
Proposed by
Kenneth Yrke Jørgensen
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Jiri Srba | ||||
Approved revision: | 1614 | ||||
Merged at revision: | 1613 | ||||
Proposed branch: | lp:~tapaal-contributor/tapaal/cpn-openPNML2 | ||||
Merge into: | lp:~tapaal-contributor/tapaal/cpn-gui-dev | ||||
Diff against target: |
256 lines (+89/-60) 2 files modified
src/net/tapaal/gui/GuiFrameController.java (+58/-32) src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java (+31/-28) |
||||
To merge this branch: | bzr merge lp:~tapaal-contributor/tapaal/cpn-openPNML2 | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jiri Srba | Approve | ||
Review via email: mp+415720@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Jiri Srba (srba) wrote : | # |
- 1614. By Kenneth Yrke Jørgensen
-
Made .pnml check case insensitive
Revision history for this message
Jiri Srba (srba) wrote : | # |
Tested and works great.
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/net/tapaal/gui/GuiFrameController.java' | |||
2 | --- src/net/tapaal/gui/GuiFrameController.java 2022-02-10 07:46:25 +0000 | |||
3 | +++ src/net/tapaal/gui/GuiFrameController.java 2022-02-17 10:51:44 +0000 | |||
4 | @@ -273,18 +273,35 @@ | |||
5 | 273 | 273 | ||
6 | 274 | @Override | 274 | @Override |
7 | 275 | public void openTAPNFile() { | 275 | public void openTAPNFile() { |
9 | 276 | final File[] files = FileBrowser.constructor("Timed-Arc Petri Net","tapn", "xml", FileBrowser.userPath).openFiles(); | 276 | final File[] files = FileBrowser.constructor(new String[]{"tapn", "xml", "pnml"}, FileBrowser.userPath).openFiles(); |
10 | 277 | //show loading cursor | 277 | //show loading cursor |
11 | 278 | openTAPNFile(files); | ||
12 | 279 | } | ||
13 | 280 | |||
14 | 281 | @Override | ||
15 | 282 | public void importPNMLFile() { | ||
16 | 283 | final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles(); | ||
17 | 284 | |||
18 | 285 | openPNMLFile(files); | ||
19 | 286 | } | ||
20 | 287 | |||
21 | 288 | private void openTAPNFile(File[] files) { | ||
22 | 278 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); | 289 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
23 | 279 | //Do loading | 290 | //Do loading |
25 | 280 | SwingWorker<java.util.List<PetriNetTab>, Void> worker = new SwingWorker<java.util.List<PetriNetTab>, Void>() { | 291 | SwingWorker<List<PetriNetTab>, Void> worker = new SwingWorker<List<PetriNetTab>, Void>() { |
26 | 281 | @Override | 292 | @Override |
29 | 282 | protected java.util.List<PetriNetTab> doInBackground() throws Exception { | 293 | protected List<PetriNetTab> doInBackground() throws Exception { |
30 | 283 | java.util.List<PetriNetTab> filesOpened = new ArrayList<>(); | 294 | List<PetriNetTab> filesOpened = new ArrayList<>(); |
31 | 284 | for(File f : files){ | 295 | for(File f : files){ |
32 | 285 | if(f.exists() && f.isFile() && f.canRead()){ | 296 | if(f.exists() && f.isFile() && f.canRead()){ |
33 | 286 | FileBrowser.userPath = f.getParent(); | 297 | FileBrowser.userPath = f.getParent(); |
35 | 287 | filesOpened.add(PetriNetTab.createNewTabFromFile(f)); | 298 | |
36 | 299 | if (f.getName().toLowerCase().endsWith(".pnml")) { | ||
37 | 300 | filesOpened.add(PetriNetTab.createNewTabFromPNMLFile(f)); | ||
38 | 301 | } else { | ||
39 | 302 | filesOpened.add(PetriNetTab.createNewTabFromFile(f)); | ||
40 | 303 | } | ||
41 | 304 | |||
42 | 288 | } | 305 | } |
43 | 289 | } | 306 | } |
44 | 290 | return filesOpened; | 307 | return filesOpened; |
45 | @@ -293,7 +310,19 @@ | |||
46 | 293 | protected void done() { | 310 | protected void done() { |
47 | 294 | try { | 311 | try { |
48 | 295 | List<PetriNetTab> tabs = get(); | 312 | List<PetriNetTab> tabs = get(); |
50 | 296 | openTab(tabs); | 313 | for (PetriNetTab tab : tabs) { |
51 | 314 | openTab(tab); | ||
52 | 315 | |||
53 | 316 | //Don't autolayout on empty net, hotfix for issue #1960000, we assue only pnml file does not have layout and they always only have one component | ||
54 | 317 | if(!tab.currentTemplate().getHasPositionalInfo() && (tab.currentTemplate().guiModel().getPlaces().length + tab.currentTemplate().guiModel().getTransitions().length) > 0) { | ||
55 | 318 | int dialogResult = JOptionPane.showConfirmDialog (null, "The net does not have any layout information. Would you like to do automatic layout?","Automatic Layout?", JOptionPane.YES_NO_OPTION); | ||
56 | 319 | if(dialogResult == JOptionPane.YES_OPTION) { | ||
57 | 320 | SmartDrawDialog.showSmartDrawDialog(); | ||
58 | 321 | } | ||
59 | 322 | } | ||
60 | 323 | } | ||
61 | 324 | |||
62 | 325 | |||
63 | 297 | } catch (Exception e) { | 326 | } catch (Exception e) { |
64 | 298 | String message = e.getMessage(); | 327 | String message = e.getMessage(); |
65 | 299 | 328 | ||
66 | @@ -301,9 +330,9 @@ | |||
67 | 301 | message = message.split(":", 2)[1]; | 330 | message = message.split(":", 2)[1]; |
68 | 302 | } | 331 | } |
69 | 303 | JOptionPane.showMessageDialog(TAPAALGUI.getApp(), | 332 | JOptionPane.showMessageDialog(TAPAALGUI.getApp(), |
73 | 304 | message, | 333 | message, |
74 | 305 | "Error loading file", | 334 | "Error loading file", |
75 | 306 | JOptionPane.ERROR_MESSAGE); | 335 | JOptionPane.ERROR_MESSAGE); |
76 | 307 | return; | 336 | return; |
77 | 308 | }finally { | 337 | }finally { |
78 | 309 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); | 338 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
79 | @@ -314,20 +343,17 @@ | |||
80 | 314 | 343 | ||
81 | 315 | //Sleep redrawing thread (EDT) until worker is done | 344 | //Sleep redrawing thread (EDT) until worker is done |
82 | 316 | //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better | 345 | //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better |
91 | 317 | while(!worker.isDone()) { | 346 | while(!worker.isDone()) { |
92 | 318 | try { | 347 | try { |
93 | 319 | Thread.sleep(1000); | 348 | Thread.sleep(1000); |
94 | 320 | } catch (InterruptedException e) { | 349 | } catch (InterruptedException e) { |
95 | 321 | // TODO Auto-generated catch block | 350 | // TODO Auto-generated catch block |
96 | 322 | e.printStackTrace(); | 351 | e.printStackTrace(); |
97 | 323 | } | 352 | } |
98 | 324 | } | 353 | } |
99 | 325 | } | 354 | } |
100 | 326 | 355 | ||
105 | 327 | @Override | 356 | private void openPNMLFile(File[] files) { |
102 | 328 | public void importPNMLFile() { | ||
103 | 329 | final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles(); | ||
104 | 330 | |||
106 | 331 | //Show loading cursor | 357 | //Show loading cursor |
107 | 332 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); | 358 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
108 | 333 | //Do loading of net | 359 | //Do loading of net |
109 | @@ -359,9 +385,9 @@ | |||
110 | 359 | 385 | ||
111 | 360 | } catch (Exception e) { | 386 | } catch (Exception e) { |
112 | 361 | JOptionPane.showMessageDialog(TAPAALGUI.getApp(), | 387 | JOptionPane.showMessageDialog(TAPAALGUI.getApp(), |
116 | 362 | e.getMessage(), | 388 | e.getMessage(), |
117 | 363 | "Error loading file", | 389 | "Error loading file", |
118 | 364 | JOptionPane.ERROR_MESSAGE); | 390 | JOptionPane.ERROR_MESSAGE); |
119 | 365 | return; | 391 | return; |
120 | 366 | }finally { | 392 | }finally { |
121 | 367 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); | 393 | guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
122 | @@ -372,14 +398,14 @@ | |||
123 | 372 | 398 | ||
124 | 373 | //Sleep redrawing thread (EDT) until worker is done | 399 | //Sleep redrawing thread (EDT) until worker is done |
125 | 374 | //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better | 400 | //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better |
134 | 375 | while(!worker.isDone()) { | 401 | while(!worker.isDone()) { |
135 | 376 | try { | 402 | try { |
136 | 377 | Thread.sleep(1000); | 403 | Thread.sleep(1000); |
137 | 378 | } catch (InterruptedException e) { | 404 | } catch (InterruptedException e) { |
138 | 379 | // TODO Auto-generated catch block | 405 | // TODO Auto-generated catch block |
139 | 380 | e.printStackTrace(); | 406 | e.printStackTrace(); |
140 | 381 | } | 407 | } |
141 | 382 | } | 408 | } |
142 | 383 | } | 409 | } |
143 | 384 | 410 | ||
144 | 385 | //XXX 2018-05-23 kyrke, moved from CreateGui, static method | 411 | //XXX 2018-05-23 kyrke, moved from CreateGui, static method |
145 | 386 | 412 | ||
146 | === modified file 'src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java' | |||
147 | --- src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java 2022-01-07 14:57:22 +0000 | |||
148 | +++ src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java 2022-02-17 10:51:44 +0000 | |||
149 | @@ -5,8 +5,10 @@ | |||
150 | 5 | import javax.swing.*; | 5 | import javax.swing.*; |
151 | 6 | import java.awt.*; | 6 | import java.awt.*; |
152 | 7 | import java.io.File; | 7 | import java.io.File; |
153 | 8 | import java.util.Arrays; | ||
154 | 8 | import java.util.regex.Matcher; | 9 | import java.util.regex.Matcher; |
155 | 9 | import java.util.regex.Pattern; | 10 | import java.util.regex.Pattern; |
156 | 11 | import java.util.stream.Collectors; | ||
157 | 10 | 12 | ||
158 | 11 | 13 | ||
159 | 12 | public class FileBrowser { | 14 | public class FileBrowser { |
160 | @@ -16,29 +18,33 @@ | |||
161 | 16 | public static String userPath = null; | 18 | public static String userPath = null; |
162 | 17 | static String lastSavePath = "."; | 19 | static String lastSavePath = "."; |
163 | 18 | static String lastOpenPath = "."; | 20 | static String lastOpenPath = "."; |
164 | 21 | |||
165 | 19 | protected final FileDialog fileDialog; | 22 | protected final FileDialog fileDialog; |
168 | 20 | protected final String ext; | 23 | private final String[] fileExtensions; |
167 | 21 | protected final String optionalExt; | ||
169 | 22 | protected String specifiedPath; | 24 | protected String specifiedPath; |
170 | 23 | 25 | ||
172 | 24 | private FileBrowser(String filetype, final String ext, final String optionalExt, String path) { | 26 | private FileBrowser(String filetype, String[] extensions, String path) { |
173 | 25 | fileDialog = new FileDialog(TAPAALGUI.getAppGui(), filetype); | 27 | fileDialog = new FileDialog(TAPAALGUI.getAppGui(), filetype); |
176 | 26 | this.ext = ext; | 28 | this.fileExtensions = extensions; |
175 | 27 | this.optionalExt = optionalExt; | ||
177 | 28 | this.specifiedPath = path; | 29 | this.specifiedPath = path; |
178 | 29 | 30 | ||
191 | 30 | if (filetype == null) { | 31 | // Setup filter if extension specified used on Linux and MacOS |
192 | 31 | filetype = "file"; | 32 | if (fileExtensions.length > 0) { |
193 | 32 | } | 33 | |
194 | 33 | 34 | ||
195 | 34 | // Setup filter if extension specified | 35 | |
196 | 35 | //This is needed for Linux and Mac | 36 | // FilenameFilter is used on Linux and MacOS, but not working on windows |
197 | 36 | if (!ext.equals("")) { | 37 | fileDialog.setFilenameFilter((dir, name) -> { |
198 | 37 | if (!optionalExt.equals("")) { | 38 | for (String fileExtension : fileExtensions) { |
199 | 38 | fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext) || name.endsWith(optionalExt)); | 39 | if (name.endsWith("." + fileExtension)) { |
200 | 39 | } else { | 40 | return true; |
201 | 40 | fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext)); | 41 | } |
202 | 41 | } | 42 | } |
203 | 43 | return false; | ||
204 | 44 | }); | ||
205 | 45 | // Workaround for Windows to filter files in open dialog, overwritten in save menu | ||
206 | 46 | String filter = Arrays.stream(fileExtensions).map(ext -> "*." + ext).collect(Collectors.joining(";")); | ||
207 | 47 | fileDialog.setFile(filter); | ||
208 | 42 | } | 48 | } |
209 | 43 | } | 49 | } |
210 | 44 | 50 | ||
211 | @@ -51,18 +57,18 @@ | |||
212 | 51 | } | 57 | } |
213 | 52 | 58 | ||
214 | 53 | public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) { | 59 | public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) { |
216 | 54 | return new FileBrowser(filetype, ext, optionalExt, path); | 60 | return new FileBrowser(filetype, new String[]{ext, optionalExt}, path); |
217 | 61 | } | ||
218 | 62 | |||
219 | 63 | public static FileBrowser constructor(String[] extensions, String path) { | ||
220 | 64 | return new FileBrowser(null, extensions, path); | ||
221 | 55 | } | 65 | } |
222 | 56 | 66 | ||
223 | 57 | public File openFile() { | 67 | public File openFile() { |
224 | 58 | if (specifiedPath == null) specifiedPath = lastOpenPath; | 68 | if (specifiedPath == null) specifiedPath = lastOpenPath; |
225 | 59 | fileDialog.setDirectory(specifiedPath); | 69 | fileDialog.setDirectory(specifiedPath); |
226 | 60 | //This is needed for Windows | 70 | //This is needed for Windows |
232 | 61 | if (optionalExt.equals("")) { | 71 | |
228 | 62 | fileDialog.setFile(ext.equals("") ? "" : ("*." + ext)); | ||
229 | 63 | } else { | ||
230 | 64 | fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt)); | ||
231 | 65 | } | ||
233 | 66 | fileDialog.setMode(FileDialog.LOAD); | 72 | fileDialog.setMode(FileDialog.LOAD); |
234 | 67 | fileDialog.setMultipleMode(false); | 73 | fileDialog.setMultipleMode(false); |
235 | 68 | fileDialog.setVisible(true); | 74 | fileDialog.setVisible(true); |
236 | @@ -77,11 +83,7 @@ | |||
237 | 77 | if (specifiedPath == null) specifiedPath = lastOpenPath; | 83 | if (specifiedPath == null) specifiedPath = lastOpenPath; |
238 | 78 | fileDialog.setDirectory(specifiedPath); | 84 | fileDialog.setDirectory(specifiedPath); |
239 | 79 | //This is needed for Windows | 85 | //This is needed for Windows |
245 | 80 | if (optionalExt.equals("")) { | 86 | |
241 | 81 | fileDialog.setFile(ext.equals("") ? "" : ("*." + ext)); | ||
242 | 82 | } else { | ||
243 | 83 | fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt)); | ||
244 | 84 | } | ||
246 | 85 | fileDialog.setMultipleMode(true); | 87 | fileDialog.setMultipleMode(true); |
247 | 86 | fileDialog.setMode(FileDialog.LOAD); | 88 | fileDialog.setMode(FileDialog.LOAD); |
248 | 87 | fileDialog.setVisible(true); | 89 | fileDialog.setVisible(true); |
249 | @@ -99,6 +101,7 @@ | |||
250 | 99 | } | 101 | } |
251 | 100 | 102 | ||
252 | 101 | public String saveFile(String suggestedName) { | 103 | public String saveFile(String suggestedName) { |
253 | 104 | String ext = fileExtensions[0]; | ||
254 | 102 | if (specifiedPath == null) specifiedPath = lastSavePath; | 105 | if (specifiedPath == null) specifiedPath = lastSavePath; |
255 | 103 | 106 | ||
256 | 104 | fileDialog.setDirectory(specifiedPath); | 107 | fileDialog.setDirectory(specifiedPath); |
I would maybe try to make this test case insensitive
if (f.getName( ).endsWith( ".pnml" ))
to that a file like net.PNML will be also parsed with the PNML parser.