From d66b7d4c9b28a16ee6444ee4afc1083533e52e4c Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 31 Aug 2015 11:47:41 -0400 Subject: [PATCH] de-emphasize use of "Editor" object, make sure it is not null on startup --- app/src/processing/app/Base.java | 14 ++++++++ app/src/processing/app/tools/CreateFont.java | 37 +++++--------------- app/src/processing/app/ui/Editor.java | 7 ++-- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 46079ae8c..bca516373 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -444,6 +444,20 @@ public class Base { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + /** + * Tools require an 'Editor' object when they're instantiated, but the + * activeEditor will be null when the first Editor that opens is creating + * its Tools menu. This will temporarily set the activeEditor to the one + * that's opening so that we don't go all NPE on startup. If there's already + * an active editor, then this does nothing. + */ + public void checkFirstEditor(Editor editor) { + if (activeEditor == null) { + activeEditor = editor; + } + } + + /** Returns the front most, active editor window. */ public Editor getActiveEditor() { return activeEditor; diff --git a/app/src/processing/app/tools/CreateFont.java b/app/src/processing/app/tools/CreateFont.java index ba9cf3a0a..1c38cfe36 100644 --- a/app/src/processing/app/tools/CreateFont.java +++ b/app/src/processing/app/tools/CreateFont.java @@ -51,10 +51,7 @@ import javax.swing.event.*; * GUI tool for font creation heaven/hell. */ public class CreateFont extends JFrame implements Tool { - Editor editor; - //Sketch sketch; - -// Dimension windowSize; + Base base; JList fontSelector; JTextField sizeSelector; @@ -64,7 +61,7 @@ public class CreateFont extends JFrame implements Tool { JButton okButton; JTextField filenameField; - HashMap table; + Map table; boolean smooth = true; Font font; @@ -86,7 +83,7 @@ public class CreateFont extends JFrame implements Tool { public void init(Editor editor) { - this.editor = editor; + base = editor.getBase(); Container paine = getContentPane(); paine.setLayout(new BorderLayout()); //10, 10)); @@ -111,12 +108,12 @@ public class CreateFont extends JFrame implements Tool { // also ignore dialog, dialoginput, monospaced, serif, sansserif // getFontList is deprecated in 1.4, so this has to be used - //long t = System.currentTimeMillis(); + //long t = System.currentTimeMillis(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font[] fonts = ge.getAllFonts(); //System.out.println("font startup took " + (System.currentTimeMillis() - t) + " ms"); - + if (false) { ArrayList fontList = new ArrayList(); File folderList = new File("/Users/fry/coconut/sys/fonts/web"); @@ -136,7 +133,7 @@ public class CreateFont extends JFrame implements Tool { Font font = Font.createFont(Font.TRUETYPE_FONT, input); input.close(); fontList.add(font); - + } catch (Exception e) { System.out.println("Ignoring " + fontFile.getName()); } @@ -348,30 +345,14 @@ public class CreateFont extends JFrame implements Tool { filename += ".vlw"; } - // Please implement me properly. The schematic is below, but not debugged. - // http://dev.processing.org/bugs/show_bug.cgi?id=1464 - -// final String filename2 = filename; -// final int fontsize2 = fontsize; -// SwingUtilities.invokeLater(new Runnable() { -// public void run() { try { Font instance = table.get(list[selection]); font = instance.deriveFont(Font.PLAIN, fontsize); //PFont f = new PFont(font, smooth, all ? null : PFont.CHARSET); PFont f = new PFont(font, smooth, charSelector.getCharacters()); -// PFont f = new PFont(font, smooth, null); -// char[] charset = charSelector.getCharacters(); -// ProgressMonitor progressMonitor = new ProgressMonitor(CreateFont.this, -// "Creating font", "", 0, charset.length); -// progressMonitor.setProgress(0); -// for (int i = 0; i < charset.length; i++) { -// System.out.println(charset[i]); -// f.index(charset[i]); // load this char -// progressMonitor.setProgress(i+1); -// } - + // the editor may have changed while the window was open + Editor editor = base.getActiveEditor(); // make sure the 'data' folder exists File folder = editor.getSketch().prepareDataFolder(); f.save(new FileOutputStream(new File(folder, filename))); @@ -383,8 +364,6 @@ public class CreateFont extends JFrame implements Tool { JOptionPane.WARNING_MESSAGE); e.printStackTrace(); } -// } -// }); setVisible(false); } diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 4e0b0a313..0dff258db 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -152,10 +152,11 @@ public abstract class Editor extends JFrame implements RunnerListener { this.state = state; this.mode = mode; - Toolkit.setIcon(this); // TODO should this be per-mode? + // Make sure Base.getActiveEditor() never returns null + base.checkFirstEditor(this); - // Install default actions for Run, Present, etc. -// resetHandlers(); + // This is a Processing window. Get rid of that ugly ass coffee cup. + Toolkit.setIcon(this); // add listener to handle window close box hit event addWindowListener(new WindowAdapter() {