66import java .awt .*;
77import java .awt .event .MouseAdapter ;
88import java .awt .event .MouseEvent ;
9- import java .io .BufferedReader ;
109import java .io .File ;
11- import java .io .FileReader ;
1210import java .io .IOException ;
13- import java .nio .charset .StandardCharsets ;
14- import java .util .*;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Path ;
13+ import java .nio .file .Paths ;
14+ import java .util .ArrayList ;
1515import java .util .List ;
1616import javax .swing .*;
1717import javax .swing .border .TitledBorder ;
2626
2727import org .key_project .util .java .IOUtil ;
2828
29+ import org .jspecify .annotations .Nullable ;
2930import org .slf4j .Logger ;
3031import org .slf4j .LoggerFactory ;
3132
@@ -61,23 +62,21 @@ public final class ExampleChooser extends JDialog {
6162 /**
6263 * The result value of the dialog. <code>null</code> if nothing to be loaded
6364 */
64- private File fileToLoad = null ;
65+ private Path fileToLoad = null ;
6566
6667 /**
6768 * The currently selected example. <code>null</code> if none selected
6869 */
6970 private Example selectedExample ;
7071
7172
72-
7373 // -------------------------------------------------------------------------
7474 // constructors
7575 // -------------------------------------------------------------------------
7676
77- private ExampleChooser (File examplesDir ) {
77+ private ExampleChooser (Path examplesDir ) {
7878 super (MainWindow .getInstance (), "Load Example" , true );
7979 assert examplesDir != null ;
80- assert examplesDir .isDirectory ();
8180
8281 // create list panel
8382 final JPanel listPanel = new JPanel ();
@@ -190,31 +189,30 @@ public void mouseClicked(MouseEvent e) {
190189 // internal methods
191190 // -------------------------------------------------------------------------
192191
193- public static File lookForExamples () {
192+ public static Path lookForExamples () {
194193 // weigl: using java properties: -Dkey.examples.dir="..."
195194 if (System .getProperty (KEY_EXAMPLE_DIR ) != null ) {
196- return new File (System .getProperty (KEY_EXAMPLE_DIR ));
195+ return Paths . get (System .getProperty (KEY_EXAMPLE_DIR ));
197196 }
198197
199198 // greatly simplified version without parent path lookup.
200199 File folder = new File (IOUtil .getProjectRoot (ExampleChooser .class ), EXAMPLES_PATH );
201200 if (!folder .exists ()) {
202201 folder = new File (IOUtil .getClassLocation (ExampleChooser .class ), EXAMPLES_PATH );
203202 }
204- return folder ;
203+ return folder . toPath () ;
205204 }
206205
207- private static String fileAsString (File f ) {
206+ private static String fileAsString (Path f ) {
208207 try {
209- return IOUtil . readFrom (f );
208+ return Files . readString (f );
210209 } catch (IOException e ) {
211210 LOGGER .error ("Could not read file '{}'" , f , e );
212211 return "<Error reading file: " + f + ">" ;
213212 }
214213 }
215214
216215
217-
218216 private void updateDescription () {
219217
220218 TreePath selectionPath = exampleList .getSelectionModel ().getSelectionPath ();
@@ -235,8 +233,8 @@ private void updateDescription() {
235233 if (p >= 0 ) {
236234 addTab (fileAsString .substring (p ), "Proof Obligation" , false );
237235 }
238- for (File file : example .getAdditionalFiles ()) {
239- addTab (fileAsString (file ), file .getName (), false );
236+ for (Path file : example .getAdditionalFiles ()) {
237+ addTab (fileAsString (file ), file .getFileName (). toString (), false );
240238 }
241239 loadButton .setEnabled (true );
242240 loadProofButton .setEnabled (example .hasProof ());
@@ -268,16 +266,16 @@ private void addTab(String string, String name, boolean wrap) {
268266 * Shows the dialog, using the passed examples directory. If null is passed, tries to find
269267 * examples directory on its own.
270268 */
271- public static File showInstance (String examplesDirString ) {
269+ public static @ Nullable Path showInstance (Path examplesDirString ) {
272270 // get examples directory
273- File examplesDir ;
271+ Path examplesDir ;
274272 if (examplesDirString == null ) {
275273 examplesDir = lookForExamples ();
276274 } else {
277- examplesDir = new File ( examplesDirString ) ;
275+ examplesDir = examplesDirString ;
278276 }
279277
280- if (!examplesDir .isDirectory ()) {
278+ if (!Files .isDirectory (examplesDir )) {
281279 JOptionPane .showMessageDialog (MainWindow .getInstance (),
282280 "The examples directory cannot be found.\n " + "Please install them at "
283281 + (examplesDirString == null ? IOUtil .getProjectRoot (ExampleChooser .class ) + "/"
@@ -304,19 +302,17 @@ public static File showInstance(String examplesDirString) {
304302 * @param examplesDir The examples directory to list examples in.
305303 * @return The found examples.
306304 */
307- public static List <Example > listExamples (File examplesDir ) {
308- List <Example > result = new LinkedList <>();
309-
310- String line ;
311- final File index = new File (new File (examplesDir , "index" ), "samplesIndex.txt" );
312- try (BufferedReader br =
313- new BufferedReader (new FileReader (index , StandardCharsets .UTF_8 ))) {
314- while ((line = br .readLine ()) != null ) {
305+ public static List <Example > listExamples (Path examplesDir ) {
306+ List <Example > result = new ArrayList <>(64 );
307+
308+ final Path index = examplesDir .resolve ("index" ).resolve ("samplesIndex.txt" );
309+ try {
310+ for (var line : Files .readAllLines (index )) {
315311 line = line .trim ();
316312 if (line .startsWith ("#" ) || line .isEmpty ()) {
317313 continue ;
318314 }
319- File f = new File ( examplesDir , line );
315+ Path f = examplesDir . resolve ( line );
320316 try {
321317 result .add (new Example (f ));
322318 } catch (IOException e ) {
0 commit comments