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
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
6 @Override
7 public void openTAPNFile() {
8- final File[] files = FileBrowser.constructor("Timed-Arc Petri Net","tapn", "xml", FileBrowser.userPath).openFiles();
9+ final File[] files = FileBrowser.constructor(new String[]{"tapn", "xml", "pnml"}, FileBrowser.userPath).openFiles();
10 //show loading cursor
11+ openTAPNFile(files);
12+ }
13+
14+ @Override
15+ public void importPNMLFile() {
16+ final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles();
17+
18+ openPNMLFile(files);
19+ }
20+
21+ private void openTAPNFile(File[] files) {
22 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
23 //Do loading
24- SwingWorker<java.util.List<PetriNetTab>, Void> worker = new SwingWorker<java.util.List<PetriNetTab>, Void>() {
25+ SwingWorker<List<PetriNetTab>, Void> worker = new SwingWorker<List<PetriNetTab>, Void>() {
26 @Override
27- protected java.util.List<PetriNetTab> doInBackground() throws Exception {
28- java.util.List<PetriNetTab> filesOpened = new ArrayList<>();
29+ protected List<PetriNetTab> doInBackground() throws Exception {
30+ List<PetriNetTab> filesOpened = new ArrayList<>();
31 for(File f : files){
32 if(f.exists() && f.isFile() && f.canRead()){
33 FileBrowser.userPath = f.getParent();
34- filesOpened.add(PetriNetTab.createNewTabFromFile(f));
35+
36+ if (f.getName().toLowerCase().endsWith(".pnml")) {
37+ filesOpened.add(PetriNetTab.createNewTabFromPNMLFile(f));
38+ } else {
39+ filesOpened.add(PetriNetTab.createNewTabFromFile(f));
40+ }
41+
42 }
43 }
44 return filesOpened;
45@@ -293,7 +310,19 @@
46 protected void done() {
47 try {
48 List<PetriNetTab> tabs = get();
49- openTab(tabs);
50+ for (PetriNetTab tab : tabs) {
51+ openTab(tab);
52+
53+ //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+ if(!tab.currentTemplate().getHasPositionalInfo() && (tab.currentTemplate().guiModel().getPlaces().length + tab.currentTemplate().guiModel().getTransitions().length) > 0) {
55+ 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+ if(dialogResult == JOptionPane.YES_OPTION) {
57+ SmartDrawDialog.showSmartDrawDialog();
58+ }
59+ }
60+ }
61+
62+
63 } catch (Exception e) {
64 String message = e.getMessage();
65
66@@ -301,9 +330,9 @@
67 message = message.split(":", 2)[1];
68 }
69 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),
70- message,
71- "Error loading file",
72- JOptionPane.ERROR_MESSAGE);
73+ message,
74+ "Error loading file",
75+ JOptionPane.ERROR_MESSAGE);
76 return;
77 }finally {
78 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
79@@ -314,20 +343,17 @@
80
81 //Sleep redrawing thread (EDT) until worker is done
82 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better
83- while(!worker.isDone()) {
84- try {
85- Thread.sleep(1000);
86- } catch (InterruptedException e) {
87- // TODO Auto-generated catch block
88- e.printStackTrace();
89- }
90- }
91+ while(!worker.isDone()) {
92+ try {
93+ Thread.sleep(1000);
94+ } catch (InterruptedException e) {
95+ // TODO Auto-generated catch block
96+ e.printStackTrace();
97+ }
98+ }
99 }
100
101- @Override
102- public void importPNMLFile() {
103- final File[] files = FileBrowser.constructor("Import PNML", "pnml", FileBrowser.userPath).openFiles();
104-
105+ private void openPNMLFile(File[] files) {
106 //Show loading cursor
107 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
108 //Do loading of net
109@@ -359,9 +385,9 @@
110
111 } catch (Exception e) {
112 JOptionPane.showMessageDialog(TAPAALGUI.getApp(),
113- e.getMessage(),
114- "Error loading file",
115- JOptionPane.ERROR_MESSAGE);
116+ e.getMessage(),
117+ "Error loading file",
118+ JOptionPane.ERROR_MESSAGE);
119 return;
120 }finally {
121 guiFrameDirectAccess.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
122@@ -372,14 +398,14 @@
123
124 //Sleep redrawing thread (EDT) until worker is done
125 //This enables the EDT to schedule the many redraws called in createNewTabFromPNMLFile(f); much better
126- while(!worker.isDone()) {
127- try {
128- Thread.sleep(1000);
129- } catch (InterruptedException e) {
130- // TODO Auto-generated catch block
131- e.printStackTrace();
132- }
133- }
134+ while(!worker.isDone()) {
135+ try {
136+ Thread.sleep(1000);
137+ } catch (InterruptedException e) {
138+ // TODO Auto-generated catch block
139+ e.printStackTrace();
140+ }
141+ }
142 }
143
144 //XXX 2018-05-23 kyrke, moved from CreateGui, static method
145
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 import javax.swing.*;
151 import java.awt.*;
152 import java.io.File;
153+import java.util.Arrays;
154 import java.util.regex.Matcher;
155 import java.util.regex.Pattern;
156+import java.util.stream.Collectors;
157
158
159 public class FileBrowser {
160@@ -16,29 +18,33 @@
161 public static String userPath = null;
162 static String lastSavePath = ".";
163 static String lastOpenPath = ".";
164+
165 protected final FileDialog fileDialog;
166- protected final String ext;
167- protected final String optionalExt;
168+ private final String[] fileExtensions;
169 protected String specifiedPath;
170
171- private FileBrowser(String filetype, final String ext, final String optionalExt, String path) {
172+ private FileBrowser(String filetype, String[] extensions, String path) {
173 fileDialog = new FileDialog(TAPAALGUI.getAppGui(), filetype);
174- this.ext = ext;
175- this.optionalExt = optionalExt;
176+ this.fileExtensions = extensions;
177 this.specifiedPath = path;
178
179- if (filetype == null) {
180- filetype = "file";
181- }
182-
183- // Setup filter if extension specified
184- //This is needed for Linux and Mac
185- if (!ext.equals("")) {
186- if (!optionalExt.equals("")) {
187- fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext) || name.endsWith(optionalExt));
188- } else {
189- fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext));
190- }
191+ // Setup filter if extension specified used on Linux and MacOS
192+ if (fileExtensions.length > 0) {
193+
194+
195+
196+ // FilenameFilter is used on Linux and MacOS, but not working on windows
197+ fileDialog.setFilenameFilter((dir, name) -> {
198+ for (String fileExtension : fileExtensions) {
199+ if (name.endsWith("." + fileExtension)) {
200+ return true;
201+ }
202+ }
203+ return false;
204+ });
205+ // Workaround for Windows to filter files in open dialog, overwritten in save menu
206+ String filter = Arrays.stream(fileExtensions).map(ext -> "*." + ext).collect(Collectors.joining(";"));
207+ fileDialog.setFile(filter);
208 }
209 }
210
211@@ -51,18 +57,18 @@
212 }
213
214 public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) {
215- return new FileBrowser(filetype, ext, optionalExt, path);
216+ return new FileBrowser(filetype, new String[]{ext, optionalExt}, path);
217+ }
218+
219+ public static FileBrowser constructor(String[] extensions, String path) {
220+ return new FileBrowser(null, extensions, path);
221 }
222
223 public File openFile() {
224 if (specifiedPath == null) specifiedPath = lastOpenPath;
225 fileDialog.setDirectory(specifiedPath);
226 //This is needed for Windows
227- if (optionalExt.equals("")) {
228- fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
229- } else {
230- fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
231- }
232+
233 fileDialog.setMode(FileDialog.LOAD);
234 fileDialog.setMultipleMode(false);
235 fileDialog.setVisible(true);
236@@ -77,11 +83,7 @@
237 if (specifiedPath == null) specifiedPath = lastOpenPath;
238 fileDialog.setDirectory(specifiedPath);
239 //This is needed for Windows
240- if (optionalExt.equals("")) {
241- fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
242- } else {
243- fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
244- }
245+
246 fileDialog.setMultipleMode(true);
247 fileDialog.setMode(FileDialog.LOAD);
248 fileDialog.setVisible(true);
249@@ -99,6 +101,7 @@
250 }
251
252 public String saveFile(String suggestedName) {
253+ String ext = fileExtensions[0];
254 if (specifiedPath == null) specifiedPath = lastSavePath;
255
256 fileDialog.setDirectory(specifiedPath);

Subscribers

People subscribed via source and target branches

to all changes: