Skip to content

Commit 238da13

Browse files
committed
de-emphasize use of "Editor" object, make sure it is not null on startup
1 parent 397018b commit 238da13

3 files changed

Lines changed: 26 additions & 32 deletions

File tree

app/src/processing/app/Base.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ void rebuildContribExamples() {
444444
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
445445

446446

447+
/**
448+
* Tools require an 'Editor' object when they're instantiated, but the
449+
* activeEditor will be null when the first Editor that opens is creating
450+
* its Tools menu. This will temporarily set the activeEditor to the one
451+
* that's opening so that we don't go all NPE on startup. If there's already
452+
* an active editor, then this does nothing.
453+
*/
454+
public void checkFirstEditor(Editor editor) {
455+
if (activeEditor == null) {
456+
activeEditor = editor;
457+
}
458+
}
459+
460+
447461
/** Returns the front most, active editor window. */
448462
public Editor getActiveEditor() {
449463
return activeEditor;

app/src/processing/app/tools/CreateFont.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@
5151
* GUI tool for font creation heaven/hell.
5252
*/
5353
public class CreateFont extends JFrame implements Tool {
54-
Editor editor;
55-
//Sketch sketch;
56-
57-
// Dimension windowSize;
54+
Base base;
5855

5956
JList<String> fontSelector;
6057
JTextField sizeSelector;
@@ -64,7 +61,7 @@ public class CreateFont extends JFrame implements Tool {
6461
JButton okButton;
6562
JTextField filenameField;
6663

67-
HashMap<String,Font> table;
64+
Map<String,Font> table;
6865
boolean smooth = true;
6966

7067
Font font;
@@ -86,7 +83,7 @@ public String getMenuTitle() {
8683

8784

8885
public void init(Editor editor) {
89-
this.editor = editor;
86+
base = editor.getBase();
9087

9188
Container paine = getContentPane();
9289
paine.setLayout(new BorderLayout()); //10, 10));
@@ -111,12 +108,12 @@ public void init(Editor editor) {
111108
// also ignore dialog, dialoginput, monospaced, serif, sansserif
112109

113110
// getFontList is deprecated in 1.4, so this has to be used
114-
//long t = System.currentTimeMillis();
111+
//long t = System.currentTimeMillis();
115112
GraphicsEnvironment ge =
116113
GraphicsEnvironment.getLocalGraphicsEnvironment();
117114
Font[] fonts = ge.getAllFonts();
118115
//System.out.println("font startup took " + (System.currentTimeMillis() - t) + " ms");
119-
116+
120117
if (false) {
121118
ArrayList<Font> fontList = new ArrayList<Font>();
122119
File folderList = new File("/Users/fry/coconut/sys/fonts/web");
@@ -136,7 +133,7 @@ public boolean accept(File dir, String name) {
136133
Font font = Font.createFont(Font.TRUETYPE_FONT, input);
137134
input.close();
138135
fontList.add(font);
139-
136+
140137
} catch (Exception e) {
141138
System.out.println("Ignoring " + fontFile.getName());
142139
}
@@ -348,30 +345,14 @@ public void build() {
348345
filename += ".vlw";
349346
}
350347

351-
// Please implement me properly. The schematic is below, but not debugged.
352-
// http://dev.processing.org/bugs/show_bug.cgi?id=1464
353-
354-
// final String filename2 = filename;
355-
// final int fontsize2 = fontsize;
356-
// SwingUtilities.invokeLater(new Runnable() {
357-
// public void run() {
358348
try {
359349
Font instance = table.get(list[selection]);
360350
font = instance.deriveFont(Font.PLAIN, fontsize);
361351
//PFont f = new PFont(font, smooth, all ? null : PFont.CHARSET);
362352
PFont f = new PFont(font, smooth, charSelector.getCharacters());
363353

364-
// PFont f = new PFont(font, smooth, null);
365-
// char[] charset = charSelector.getCharacters();
366-
// ProgressMonitor progressMonitor = new ProgressMonitor(CreateFont.this,
367-
// "Creating font", "", 0, charset.length);
368-
// progressMonitor.setProgress(0);
369-
// for (int i = 0; i < charset.length; i++) {
370-
// System.out.println(charset[i]);
371-
// f.index(charset[i]); // load this char
372-
// progressMonitor.setProgress(i+1);
373-
// }
374-
354+
// the editor may have changed while the window was open
355+
Editor editor = base.getActiveEditor();
375356
// make sure the 'data' folder exists
376357
File folder = editor.getSketch().prepareDataFolder();
377358
f.save(new FileOutputStream(new File(folder, filename)));
@@ -383,8 +364,6 @@ public void build() {
383364
JOptionPane.WARNING_MESSAGE);
384365
e.printStackTrace();
385366
}
386-
// }
387-
// });
388367

389368
setVisible(false);
390369
}

app/src/processing/app/ui/Editor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ protected Editor(final Base base, String path, final EditorState state,
152152
this.state = state;
153153
this.mode = mode;
154154

155-
Toolkit.setIcon(this); // TODO should this be per-mode?
155+
// Make sure Base.getActiveEditor() never returns null
156+
base.checkFirstEditor(this);
156157

157-
// Install default actions for Run, Present, etc.
158-
// resetHandlers();
158+
// This is a Processing window. Get rid of that ugly ass coffee cup.
159+
Toolkit.setIcon(this);
159160

160161
// add listener to handle window close box hit event
161162
addWindowListener(new WindowAdapter() {

0 commit comments

Comments
 (0)