1+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+ /*
4+ Part of the Processing project - http://processing.org
5+
6+ Copyright (c) 2015 The Processing Foundation
7+
8+ This program is free software; you can redistribute it and/or
9+ modify it under the terms of the GNU General Public License
10+ version 2, as published by the Free Software Foundation.
11+
12+ This program is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+
17+ You should have received a copy of the GNU General Public License
18+ along with this program; if not, write to the Free Software Foundation,
19+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+ */
21+
22+ package processing .app ;
23+
24+ import java .awt .Frame ;
25+ import java .io .PrintWriter ;
26+ import java .io .StringWriter ;
27+
28+ import javax .swing .JDialog ;
29+ import javax .swing .JFrame ;
30+ import javax .swing .JOptionPane ;
31+
32+ import processing .app .ui .Editor ;
33+
34+ public class Messages {
35+ /**
36+ * "No cookie for you" type messages. Nothing fatal or all that
37+ * much of a bummer, but something to notify the user about.
38+ */
39+ static public void showMessage (String title , String message ) {
40+ if (title == null ) title = "Message" ;
41+
42+ if (Base .isCommandLine ()) {
43+ System .out .println (title + ": " + message );
44+
45+ } else {
46+ JOptionPane .showMessageDialog (new Frame (), message , title ,
47+ JOptionPane .INFORMATION_MESSAGE );
48+ }
49+ }
50+
51+
52+ /**
53+ * Non-fatal error message.
54+ */
55+ static public void showWarning (String title , String message ) {
56+ showWarning (title , message , null );
57+ }
58+
59+ /**
60+ * Non-fatal error message with optional stack trace side dish.
61+ */
62+ static public void showWarning (String title , String message , Throwable e ) {
63+ if (title == null ) title = "Warning" ;
64+
65+ if (Base .isCommandLine ()) {
66+ System .out .println (title + ": " + message );
67+
68+ } else {
69+ JOptionPane .showMessageDialog (new Frame (), message , title ,
70+ JOptionPane .WARNING_MESSAGE );
71+ }
72+ if (e != null ) e .printStackTrace ();
73+ }
74+
75+
76+ /**
77+ * Non-fatal error message with optional stack trace side dish.
78+ */
79+ static public void showWarningTiered (String title ,
80+ String primary , String secondary ,
81+ Throwable e ) {
82+ if (title == null ) title = "Warning" ;
83+
84+ final String message = primary + "\n " + secondary ;
85+ if (Base .isCommandLine ()) {
86+ System .out .println (title + ": " + message );
87+
88+ } else {
89+ // JOptionPane.showMessageDialog(new Frame(), message,
90+ // title, JOptionPane.WARNING_MESSAGE);
91+ if (!Platform .isMacOS ()) {
92+ JOptionPane .showMessageDialog (new JFrame (),
93+ "<html><body>" +
94+ "<b>" + primary + "</b>" +
95+ "<br>" + secondary , title ,
96+ JOptionPane .WARNING_MESSAGE );
97+ } else {
98+ // Pane formatting adapted from the Quaqua guide
99+ // http://www.randelshofer.ch/quaqua/guide/joptionpane.html
100+ JOptionPane pane =
101+ new JOptionPane ("<html> " +
102+ "<head> <style type=\" text/css\" >" +
103+ "b { font: 13pt \" Lucida Grande\" }" +
104+ "p { font: 11pt \" Lucida Grande\" ; margin-top: 8px; width: 300px }" +
105+ "</style> </head>" +
106+ "<b>" + primary + "</b>" +
107+ "<p>" + secondary + "</p>" ,
108+ JOptionPane .WARNING_MESSAGE );
109+
110+ // String[] options = new String[] {
111+ // "Yes", "No"
112+ // };
113+ // pane.setOptions(options);
114+
115+ // highlight the safest option ala apple hig
116+ // pane.setInitialValue(options[0]);
117+
118+ JDialog dialog = pane .createDialog (new JFrame (), null );
119+ dialog .setVisible (true );
120+
121+ // Object result = pane.getValue();
122+ // if (result == options[0]) {
123+ // return JOptionPane.YES_OPTION;
124+ // } else if (result == options[1]) {
125+ // return JOptionPane.NO_OPTION;
126+ // } else {
127+ // return JOptionPane.CLOSED_OPTION;
128+ // }
129+ }
130+ }
131+ if (e != null ) e .printStackTrace ();
132+ }
133+
134+
135+ /**
136+ * Show an error message that's actually fatal to the program.
137+ * This is an error that can't be recovered. Use showWarning()
138+ * for errors that allow P5 to continue running.
139+ */
140+ static public void showError (String title , String message , Throwable e ) {
141+ if (title == null ) title = "Error" ;
142+
143+ if (Base .isCommandLine ()) {
144+ System .err .println (title + ": " + message );
145+
146+ } else {
147+ JOptionPane .showMessageDialog (new Frame (), message , title ,
148+ JOptionPane .ERROR_MESSAGE );
149+ }
150+ if (e != null ) e .printStackTrace ();
151+ System .exit (1 );
152+ }
153+
154+
155+ /**
156+ * Testing a new warning window that includes the stack trace.
157+ */
158+ static void showBadnessTrace (String title , String message ,
159+ Throwable t , boolean fatal ) {
160+ if (title == null ) title = fatal ? "Error" : "Warning" ;
161+
162+ if (Base .isCommandLine ()) {
163+ System .err .println (title + ": " + message );
164+ if (t != null ) {
165+ t .printStackTrace ();
166+ }
167+
168+ } else {
169+ StringWriter sw = new StringWriter ();
170+ t .printStackTrace (new PrintWriter (sw ));
171+ // Necessary to replace \n with <br/> (even if pre) otherwise Java
172+ // treats it as a closed tag and reverts to plain formatting.
173+ message = ("<html>" + message +
174+ "<br/><font size=2><br/>" +
175+ sw + "</html>" ).replaceAll ("\n " , "<br/>" );
176+
177+ JOptionPane .showMessageDialog (new Frame (), message , title ,
178+ fatal ?
179+ JOptionPane .ERROR_MESSAGE :
180+ JOptionPane .WARNING_MESSAGE );
181+
182+ if (fatal ) {
183+ System .exit (1 );
184+ }
185+ }
186+ }
187+
188+
189+ // ...................................................................
190+
191+
192+
193+ // incomplete
194+ static public int showYesNoCancelQuestion (Editor editor , String title ,
195+ String primary , String secondary ) {
196+ if (!Platform .isMacOS ()) {
197+ int result =
198+ JOptionPane .showConfirmDialog (null , primary + "\n " + secondary , title ,
199+ JOptionPane .YES_NO_CANCEL_OPTION ,
200+ JOptionPane .QUESTION_MESSAGE );
201+ return result ;
202+ // if (result == JOptionPane.YES_OPTION) {
203+ //
204+ // } else if (result == JOptionPane.NO_OPTION) {
205+ // return true; // ok to continue
206+ //
207+ // } else if (result == JOptionPane.CANCEL_OPTION) {
208+ // return false;
209+ //
210+ // } else {
211+ // throw new IllegalStateException();
212+ // }
213+
214+ } else {
215+ // Pane formatting adapted from the Quaqua guide
216+ // http://www.randelshofer.ch/quaqua/guide/joptionpane.html
217+ JOptionPane pane =
218+ new JOptionPane ("<html> " +
219+ "<head> <style type=\" text/css\" >" +
220+ "b { font: 13pt \" Lucida Grande\" }" +
221+ "p { font: 11pt \" Lucida Grande\" ; margin-top: 8px; width: 300px }" +
222+ "</style> </head>" +
223+ "<b>" + Language .text ("save.title" ) + "</b>" +
224+ "<p>" + Language .text ("save.hint" ) + "</p>" ,
225+ JOptionPane .QUESTION_MESSAGE );
226+
227+ String [] options = new String [] {
228+ Language .text ("save.btn.save" ),
229+ Language .text ("prompt.cancel" ),
230+ Language .text ("save.btn.dont_save" )
231+ };
232+ pane .setOptions (options );
233+
234+ // highlight the safest option ala apple hig
235+ pane .setInitialValue (options [0 ]);
236+
237+ // on macosx, setting the destructive property places this option
238+ // away from the others at the lefthand side
239+ pane .putClientProperty ("Quaqua.OptionPane.destructiveOption" ,
240+ Integer .valueOf (2 ));
241+
242+ JDialog dialog = pane .createDialog (editor , null );
243+ dialog .setVisible (true );
244+
245+ Object result = pane .getValue ();
246+ if (result == options [0 ]) {
247+ return JOptionPane .YES_OPTION ;
248+ } else if (result == options [1 ]) {
249+ return JOptionPane .CANCEL_OPTION ;
250+ } else if (result == options [2 ]) {
251+ return JOptionPane .NO_OPTION ;
252+ } else {
253+ return JOptionPane .CLOSED_OPTION ;
254+ }
255+ }
256+ }
257+
258+
259+ static public int showYesNoQuestion (Frame editor , String title ,
260+ String primary , String secondary ) {
261+ if (!Platform .isMacOS ()) {
262+ return JOptionPane .showConfirmDialog (editor ,
263+ "<html><body>" +
264+ "<b>" + primary + "</b>" +
265+ "<br>" + secondary , title ,
266+ JOptionPane .YES_NO_OPTION ,
267+ JOptionPane .QUESTION_MESSAGE );
268+ } else {
269+ // Pane formatting adapted from the Quaqua guide
270+ // http://www.randelshofer.ch/quaqua/guide/joptionpane.html
271+ JOptionPane pane =
272+ new JOptionPane ("<html> " +
273+ "<head> <style type=\" text/css\" >" +
274+ "b { font: 13pt \" Lucida Grande\" }" +
275+ "p { font: 11pt \" Lucida Grande\" ; margin-top: 8px; width: 300px }" +
276+ "</style> </head>" +
277+ "<b>" + primary + "</b>" +
278+ "<p>" + secondary + "</p>" ,
279+ JOptionPane .QUESTION_MESSAGE );
280+
281+ String [] options = new String [] {
282+ "Yes" , "No"
283+ };
284+ pane .setOptions (options );
285+
286+ // highlight the safest option ala apple hig
287+ pane .setInitialValue (options [0 ]);
288+
289+ JDialog dialog = pane .createDialog (editor , null );
290+ dialog .setVisible (true );
291+
292+ Object result = pane .getValue ();
293+ if (result == options [0 ]) {
294+ return JOptionPane .YES_OPTION ;
295+ } else if (result == options [1 ]) {
296+ return JOptionPane .NO_OPTION ;
297+ } else {
298+ return JOptionPane .CLOSED_OPTION ;
299+ }
300+ }
301+ }
302+
303+
304+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
305+
306+
307+ static public void log (Object from , String message ) {
308+ if (Base .DEBUG ) {
309+ System .out .println (from .getClass ().getName () + ": " + message );
310+ }
311+ }
312+
313+
314+ static public void log (String message ) {
315+ if (Base .DEBUG ) {
316+ System .out .println (message );
317+ }
318+ }
319+
320+
321+ static public void logf (String message , Object ... args ) {
322+ if (Base .DEBUG ) {
323+ System .out .println (String .format (message , args ));
324+ }
325+ }
326+
327+
328+ static public void loge (String message , Throwable e ) {
329+ if (Base .DEBUG ) {
330+ System .err .println (message );
331+ e .printStackTrace ();
332+ }
333+ }
334+
335+
336+ static public void loge (String message ) {
337+ if (Base .DEBUG ) {
338+ System .out .println (message );
339+ }
340+ }
341+ }
0 commit comments