Merge lp:~tapaal-contributor/tapaal/cpn-openPNML2 into lp:~tapaal-contributor/tapaal/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
Reviewer Review Type Date Requested Status
Jiri Srba Approve
Review via email: mp+415720@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jiri Srba (srba) wrote :

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.

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
=== modified file 'src/net/tapaal/gui/GuiFrameController.java'
--- src/net/tapaal/gui/GuiFrameController.java 2022-02-10 07:46:25 +0000
+++ src/net/tapaal/gui/GuiFrameController.java 2022-02-17 10:51:44 +0000
@@ -273,18 +273,35 @@
273273
274 @Override274 @Override
275 public void openTAPNFile() {275 public void openTAPNFile() {
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();
277 //show loading cursor277 //show loading cursor
278 openTAPNFile(files);
279 }
280
281 @Override
282 public void importPNMLFile() {
283 final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles();
284
285 openPNMLFile(files);
286 }
287
288 private void openTAPNFile(File[] files) {
278 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));289 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
279 //Do loading290 //Do loading
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>() {
281 @Override292 @Override
282 protected java.util.List<PetriNetTab> doInBackground() throws Exception {293 protected List<PetriNetTab> doInBackground() throws Exception {
283 java.util.List<PetriNetTab> filesOpened = new ArrayList<>();294 List<PetriNetTab> filesOpened = new ArrayList<>();
284 for(File f : files){295 for(File f : files){
285 if(f.exists() && f.isFile() && f.canRead()){296 if(f.exists() && f.isFile() && f.canRead()){
286 FileBrowser.userPath = f.getParent();297 FileBrowser.userPath = f.getParent();
287 filesOpened.add(PetriNetTab.createNewTabFromFile(f));298
299 if (f.getName().toLowerCase().endsWith(".pnml")) {
300 filesOpened.add(PetriNetTab.createNewTabFromPNMLFile(f));
301 } else {
302 filesOpened.add(PetriNetTab.createNewTabFromFile(f));
303 }
304
288 }305 }
289 }306 }
290 return filesOpened;307 return filesOpened;
@@ -293,7 +310,19 @@
293 protected void done() {310 protected void done() {
294 try {311 try {
295 List<PetriNetTab> tabs = get();312 List<PetriNetTab> tabs = get();
296 openTab(tabs);313 for (PetriNetTab tab : tabs) {
314 openTab(tab);
315
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
317 if(!tab.currentTemplate().getHasPositionalInfo() && (tab.currentTemplate().guiModel().getPlaces().length + tab.currentTemplate().guiModel().getTransitions().length) > 0) {
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);
319 if(dialogResult == JOptionPane.YES_OPTION) {
320 SmartDrawDialog.showSmartDrawDialog();
321 }
322 }
323 }
324
325
297 } catch (Exception e) {326 } catch (Exception e) {
298 String message = e.getMessage();327 String message = e.getMessage();
299328
@@ -301,9 +330,9 @@
301 message = message.split(":", 2)[1];330 message = message.split(":", 2)[1];
302 }331 }
303 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),332 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),
304 message,333 message,
305 "Error loading file",334 "Error loading file",
306 JOptionPane.ERROR_MESSAGE);335 JOptionPane.ERROR_MESSAGE);
307 return;336 return;
308 }finally {337 }finally {
309 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));338 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
@@ -314,20 +343,17 @@
314343
315 //Sleep redrawing thread (EDT) until worker is done344 //Sleep redrawing thread (EDT) until worker is done
316 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better345 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better
317 while(!worker.isDone()) {346 while(!worker.isDone()) {
318 try {347 try {
319 Thread.sleep(1000);348 Thread.sleep(1000);
320 } catch (InterruptedException e) {349 } catch (InterruptedException e) {
321 // TODO Auto-generated catch block350 // TODO Auto-generated catch block
322 e.printStackTrace();351 e.printStackTrace();
323 }352 }
324 }353 }
325 }354 }
326355
327 @Override356 private void openPNMLFile(File[] files) {
328 public void importPNMLFile() {
329 final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles();
330
331 //Show loading cursor357 //Show loading cursor
332 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));358 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
333 //Do loading of net359 //Do loading of net
@@ -359,9 +385,9 @@
359385
360 } catch (Exception e) {386 } catch (Exception e) {
361 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),387 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),
362 e.getMessage(),388 e.getMessage(),
363 "Error loading file",389 "Error loading file",
364 JOptionPane.ERROR_MESSAGE);390 JOptionPane.ERROR_MESSAGE);
365 return;391 return;
366 }finally {392 }finally {
367 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));393 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
@@ -372,14 +398,14 @@
372398
373 //Sleep redrawing thread (EDT) until worker is done399 //Sleep redrawing thread (EDT) until worker is done
374 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better400 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better
375 while(!worker.isDone()) {401 while(!worker.isDone()) {
376 try {402 try {
377 Thread.sleep(1000);403 Thread.sleep(1000);
378 } catch (InterruptedException e) {404 } catch (InterruptedException e) {
379 // TODO Auto-generated catch block405 // TODO Auto-generated catch block
380 e.printStackTrace();406 e.printStackTrace();
381 }407 }
382 }408 }
383 }409 }
384410
385 //XXX 2018-05-23 kyrke, moved from CreateGui, static method411 //XXX 2018-05-23 kyrke, moved from CreateGui, static method
386412
=== modified file 'src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java'
--- src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java 2022-01-07 14:57:22 +0000
+++ src/pipe/gui/swingcomponents/filebrowser/FileBrowser.java 2022-02-17 10:51:44 +0000
@@ -5,8 +5,10 @@
5import javax.swing.*;5import javax.swing.*;
6import java.awt.*;6import java.awt.*;
7import java.io.File;7import java.io.File;
8import java.util.Arrays;
8import java.util.regex.Matcher;9import java.util.regex.Matcher;
9import java.util.regex.Pattern;10import java.util.regex.Pattern;
11import java.util.stream.Collectors;
1012
1113
12public class FileBrowser {14public class FileBrowser {
@@ -16,29 +18,33 @@
16 public static String userPath = null;18 public static String userPath = null;
17 static String lastSavePath = ".";19 static String lastSavePath = ".";
18 static String lastOpenPath = ".";20 static String lastOpenPath = ".";
21
19 protected final FileDialog fileDialog;22 protected final FileDialog fileDialog;
20 protected final String ext;23 private final String[] fileExtensions;
21 protected final String optionalExt;
22 protected String specifiedPath;24 protected String specifiedPath;
2325
24 private FileBrowser(String filetype, final String ext, final String optionalExt, String path) {26 private FileBrowser(String filetype, String[] extensions, String path) {
25 fileDialog = new FileDialog(TAPAALGUI.getAppGui(), filetype);27 fileDialog = new FileDialog(TAPAALGUI.getAppGui(), filetype);
26 this.ext = ext;28 this.fileExtensions = extensions;
27 this.optionalExt = optionalExt;
28 this.specifiedPath = path;29 this.specifiedPath = path;
2930
30 if (filetype == null) {31 // Setup filter if extension specified used on Linux and MacOS
31 filetype = "file";32 if (fileExtensions.length > 0) {
32 }33
3334
34 // Setup filter if extension specified35
35 //This is needed for Linux and Mac36 // FilenameFilter is used on Linux and MacOS, but not working on windows
36 if (!ext.equals("")) {37 fileDialog.setFilenameFilter((dir, name) -> {
37 if (!optionalExt.equals("")) {38 for (String fileExtension : fileExtensions) {
38 fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext) || name.endsWith(optionalExt));39 if (name.endsWith("." + fileExtension)) {
39 } else {40 return true;
40 fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext));41 }
41 }42 }
43 return false;
44 });
45 // Workaround for Windows to filter files in open dialog, overwritten in save menu
46 String filter = Arrays.stream(fileExtensions).map(ext -> "*." + ext).collect(Collectors.joining(";"));
47 fileDialog.setFile(filter);
42 }48 }
43 }49 }
4450
@@ -51,18 +57,18 @@
51 }57 }
5258
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) {
54 return new FileBrowser(filetype, ext, optionalExt, path);60 return new FileBrowser(filetype, new String[]{ext, optionalExt}, path);
61 }
62
63 public static FileBrowser constructor(String[] extensions, String path) {
64 return new FileBrowser(null, extensions, path);
55 }65 }
5666
57 public File openFile() {67 public File openFile() {
58 if (specifiedPath == null) specifiedPath = lastOpenPath;68 if (specifiedPath == null) specifiedPath = lastOpenPath;
59 fileDialog.setDirectory(specifiedPath);69 fileDialog.setDirectory(specifiedPath);
60 //This is needed for Windows70 //This is needed for Windows
61 if (optionalExt.equals("")) {71
62 fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
63 } else {
64 fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
65 }
66 fileDialog.setMode(FileDialog.LOAD);72 fileDialog.setMode(FileDialog.LOAD);
67 fileDialog.setMultipleMode(false);73 fileDialog.setMultipleMode(false);
68 fileDialog.setVisible(true);74 fileDialog.setVisible(true);
@@ -77,11 +83,7 @@
77 if (specifiedPath == null) specifiedPath = lastOpenPath;83 if (specifiedPath == null) specifiedPath = lastOpenPath;
78 fileDialog.setDirectory(specifiedPath);84 fileDialog.setDirectory(specifiedPath);
79 //This is needed for Windows85 //This is needed for Windows
80 if (optionalExt.equals("")) {86
81 fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
82 } else {
83 fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
84 }
85 fileDialog.setMultipleMode(true);87 fileDialog.setMultipleMode(true);
86 fileDialog.setMode(FileDialog.LOAD);88 fileDialog.setMode(FileDialog.LOAD);
87 fileDialog.setVisible(true);89 fileDialog.setVisible(true);
@@ -99,6 +101,7 @@
99 }101 }
100102
101 public String saveFile(String suggestedName) {103 public String saveFile(String suggestedName) {
104 String ext = fileExtensions[0];
102 if (specifiedPath == null) specifiedPath = lastSavePath;105 if (specifiedPath == null) specifiedPath = lastSavePath;
103106
104 fileDialog.setDirectory(specifiedPath);107 fileDialog.setDirectory(specifiedPath);

Subscribers

People subscribed via source and target branches

to all changes: