Menu

[r469]: / NoteLab / src / noteLab / util / InfoCenter.java  Maximize  Restore  History

Download this file

581 lines (498 with data), 16.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* NoteLab: An advanced note taking application for pen-enabled platforms
*
* Copyright (C) 2006, Dominic Kramer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* For any questions or comments please contact:
* Dominic Kramer
* kramerd@iastate.edu
*/
package noteLab.util;
import java.io.File;
import java.net.URL;
import javax.swing.ImageIcon;
import noteLab.gui.DefinedIcon;
/**
* This class is used to get information about the application such
* as its name, current version, and file extension.
*
* @author Dominic Kramer
*/
public class InfoCenter
{
/**
* Represents an operating system, either Windows-based or UNIX-based.
*
* @author Dominic Kramer
*/
public enum OSType
{
/** Represents a Windows-based operating system. */
Windows,
/** Represents a UNIX-based operating system. */
Unix;
public boolean isMacOSX()
{
String name = System.getProperty("os.name");
if (name == null)
return false;
return name.toLowerCase().contains("mac");
}
}
/** The application's name. */
private static final String NAME = "NoteLab";
/** The application's version. */
private static final String VERSION = "0.2.1rc2";
/** The file extension for this application's native file. */
private static final String NATIVE_EXT = ".ntlb";
/** The name of this application's author. */
private static final String AUTHOR = "Dominic Kramer";
/** This application's author's email address. */
private static final String AUTHOR_EMAIL = "kramerd@users.sourceforge.net";
/** This application's homepage. */
private static final String HOMEPAGE = "java-notelab.sourceforge.net";
/** The application's copyright statement. */
private static final String COPYRIGHT = "Copyright (C) 2006-2011 "+AUTHOR;
/** A description of this application. */
private static final String DESCRIPTION;
static
{
StringBuffer buffer = new StringBuffer(getAppName());
buffer.append(" version ");
buffer.append(getAppVersion());
buffer.append("\n");
buffer.append(getCopyright());
buffer.append("\n\n");
buffer.append("This application is open source software proudly licensed under the ");
buffer.append("GNU GPL (www.gnu.org).\n\n");
buffer.append(getAppName());
buffer.append(" is an advanced \"digital notebook\" specifically designed for tablet ");
buffer.append("computers. With its stroke smoothing, antialiasing, \"smart rendering\" ");
buffer.append("(for increased performance), and document storage in the industry ");
buffer.append("standard SVG format, ");
buffer.append(getAppName());
buffer.append(" provides a beautiful and powerful note taking environment ");
buffer.append("on any operating system.\n\n");
buffer.append("Webpage: ");
buffer.append(getHomepage());
buffer.append("\n");
buffer.append("Author: ");
buffer.append(getAuthor());
buffer.append("\n");
buffer.append("Email: ");
buffer.append(getAuthorEmail());
buffer.append("\n");
buffer.append("License: GNU GPL\n");
buffer.append("Filetype: ");
buffer.append(getFileExtension());
buffer.append("\n");
DESCRIPTION = buffer.toString();
}
/**
* The path in the classpath that points to the directory that
* contains the application's icons.
*/
private static final String ICON_PATH_NAME = "noteLab/icons/";
/**
* The file on the user's hard drive that points to the
* application's home directory.
*/
private static final File APP_HOME;
static
{
File userHome = new File(System.getProperty("user.home"));
APP_HOME = new File(userHome, "."+getAppName());
}
/** The file extension of executable scripts on Windows-based systems. */
private static final String WINDOWS_SCRIPT_EXT = ".bat";
/** The file extension of executable scripts on UNIX-based systems. */
private static final String UNIX_SCRIPT_EXT = ".sh";
/**
* The file extension of scripts that can be executed through the
* Mac OS X's Finder application.
*/
private static final String MAC_SCRIPT_EXT = ".command";
/** The file extension of Jarnal files. */
private static final String JARNAL_EXT = ".jaj";
/** The file extension of PDF files. */
private static final String PDF_EXT = ".pdf";
/** The array of file extensions that NoteLab supports opening. */
private static final String[] SUPPORTED_EXT_ARR =
new String[] {
NATIVE_EXT,
JARNAL_EXT
};
/** The initial amount of memory given to the application. */
private static final double INIT_MEMORY_MB = getTotalUsedMemoryMb();
private static final String SVG_EXT = ".svg";
private static final String SVGZ_EXT = ".svgz";
private static final String PNG_EXT = ".png";
/** Private to enforce static access to this class's members. */
private InfoCenter()
{}
/**
* Used to retrieve the application's name.
*
* @return The application's name.
*/
public static final String getAppName()
{
return NAME;
}
/**
* Used to retrieve the URL of this application's homepage.
*
* @return The application's homepage.
*/
public static final String getHomepage()
{
return HOMEPAGE;
}
/**
* Used to retrieve the application's author.
*
* @return The author's name.
*/
public static final String getAuthor()
{
return AUTHOR;
}
/**
* Used to retrieve the email address of this
* application's author.
*
* @return The author's email address.
*/
public static final String getAuthorEmail()
{
return AUTHOR_EMAIL;
}
/**
* Used to retrieve this application's copyright
* description.
*
* @return The application's copyright description.
*/
public static final String getCopyright()
{
return COPYRIGHT;
}
/**
* Used to retrieve a human readable description of
* this application.
*
* @return A description of this application.
*/
public static final String getDescription()
{
return DESCRIPTION;
}
/**
* Used to retrieve the application's current version.
*
* @return The application's version.
*/
public static final String getAppVersion()
{
return VERSION;
}
/**
* Used to retrieve the application's native file's extension.
*
* @return The application's native file's extension.
*/
public static final String getFileExtension()
{
return NATIVE_EXT;
}
/**
* Used to retrieve the directory where the application stores any
* files that it needs.
*
* @return The application's home directory.
*/
public static final File getAppHome()
{
return APP_HOME;
}
public static final String getUserHome()
{
return System.getProperty("user.home");
}
/**
* Used to determine if the application's home directory is valid.
* The directory is valid, if it and its subdirectories either
* exist, or can be made, and are readable and writable.
*
* @return <code>null</code> if the directory is valid and a
* <code>non-null<code> string describing the error if
* one exists.
*/
public static final String isAppHomeValid()
{
String result = isDirValid(getAppHome());
if (result != null)
return result;
return null;
}
/**
* Used to build the application's home directory if it doesn't
* already exist. If any of the subdirectories in the application's
* home directory already exist, they will not be made.
*
* @return <code>null</code> if the directory was successfully
* made and a <code>non-null</code> string describing
* the error if one occured.
*/
public static final String buildAppHome()
{
String result = mkdir(getAppHome());
if (result != null)
return result;
return null;
}
/**
* Used to make the given directory if it does not already exist.
*
* @param dir The directory to make.
*
* @return <code>null</code> if the directory either exists or
* was constructed if it doesn't exist. A
* <code>non-null</code> string is returned describing
* that the directory could not be constructed.
*/
private static final String mkdir(File dir)
{
if (dir == null)
throw new NullPointerException();
if (!dir.exists())
{
boolean success = dir.mkdir();
if (!success)
return "The directory '"+dir.getAbsolutePath()+
"' does not exist and could not be created.";
}
return null;
}
/**
* Used to determine if the given file corresponds to a valid
* directory. The file is considered a valid directory if:
* <ol>
* <li>It exists</li>
* <li>It is a directory</li>
* <li>It has read access</li>
* <li>It has write access</li>
* </ol>
*
* @param dir The file to analyze.
*
* @return <code>null</code> if the file is a valid directory
* and a <code>non-null</code> string describing the
* error if one exists.
*/
private static final String isDirValid(File dir)
{
if (dir == null)
throw new NullPointerException();
if (!dir.exists())
return "The directory '"+dir.getAbsolutePath()+
"' does not exist.";
if (!dir.isDirectory())
return "The file '"+dir.getAbsolutePath()+"' needs to be a " +
"directory but is not.";
if (!dir.canRead())
return "The directory '"+dir.getAbsolutePath()+
"' does not have read access.";
if (!dir.canWrite())
return "The directory '"+dir.getAbsolutePath()+
"' does not have write access.";
return null;
}
/**
* Used to construct an image from the given <code>DefinedIcon</code>.
*
* @param icon Describes an icon located in the directory that holds
* the application's icons.
*
* @return The constructed image or <code>null</code> if it could not
* be constructed.
*/
public static ImageIcon getImage(DefinedIcon icon)
{
String name = ICON_PATH_NAME+icon.toString();
URL imageURL = ClassLoader.getSystemClassLoader().getResource(name);
if (imageURL == null)
return null;
return new ImageIcon(imageURL);
}
/**
* Used to get the operating system that NoteLab is currently running on.
*
* @return The operating system NoteLab is currently running on, either
* Windows-based or UNIX-based.
*/
public static OSType getOperatingSystem()
{
String name = System.getProperty("os.name");
if (name == null)
return OSType.Unix;
if (name.toLowerCase().contains("windows"))
return OSType.Windows;
return OSType.Unix;
}
/**
* Used to get the extension of the executable script of the operating system
* NoteLab is currently running on.
*
* @return The extension of the executable script on the current operating system.
*/
public static String getScriptExtension()
{
OSType os = getOperatingSystem();
if (os.equals(OSType.Windows))
return WINDOWS_SCRIPT_EXT;
// the operating system is a UNIX system
if (os.isMacOSX())
return MAC_SCRIPT_EXT;
return UNIX_SCRIPT_EXT;
}
public static String getUnixScriptExtension()
{
return UNIX_SCRIPT_EXT;
}
public static String getWindowsScriptExtension()
{
return WINDOWS_SCRIPT_EXT;
}
public static String getMacScriptExtension()
{
return MAC_SCRIPT_EXT;
}
/**
* Returns the maximum amount of memory, in megabytes, that NoteLab can use.
*
* @return The maximum number of megabytes of memory that NoteLab can use.
*/
public static double getMaxMemoryMb()
{
return convertToMb(Runtime.getRuntime().maxMemory());
}
/**
* Returns the total amount of memory, in megabytes, currently used by NoteLab.
*
* @return The number of megabytes of memory currently used by NoteLab.
*/
public static double getTotalUsedMemoryMb()
{
return convertToMb(Runtime.getRuntime().totalMemory());
}
/**
* Returns the amount of memory free memory, in megabytes, that NoteLab can still use.
* This includes the amount of memory allocated for NoteLab, but not already used, in
* addition to the amount of memory that can be additionally allocated for NoteLab.
*
* @return The number of megabytes of free memory NoteLab can still use.
*/
public static double getTotalFreeMemoryMb()
{
Runtime run = Runtime.getRuntime();
double freeMB = convertToMb(run.freeMemory());
double maxMB = convertToMb(run.maxMemory());
double totalMB = convertToMb(run.totalMemory());
// the total free is the amount free inside the
// virtual machine plus the extra memory not
// already used by the virtual machine.
return freeMB+(maxMB-totalMB);
}
/**
* Returns the amount of memory given to the application when it was first
* started. This value is the amount reported by the runtime system at
* startup.
*
* @return The amount of memory given to the application at startup in
* megabytes.
*/
public static double getInitialMemoryMb()
{
return INIT_MEMORY_MB;
}
/**
* Used to convert a number of bytes to megabytes.
*
* @param numBytes The number of bytes.
*
* @return The corresponding number of megabytes.
*/
private static double convertToMb(long numBytes)
{
double numKb = numBytes/1024.0;
return numKb/1024.0;
}
/**
* Used to get the file extension of Jarnal files.
*
* @return The file extension of Jarnal files.
*/
public static String getJarnalExtension()
{
return JARNAL_EXT;
}
/**
* Used to get the file extension of PDF files.
*
* @return The file extension of PDF files.
*/
public static String getPDFExtension()
{
return PDF_EXT;
}
/**
* Used to get the file extensions of the files that NoteLab supports opening.
*
* @return The array of file extensions that NoteLab supports opening.
*/
public static String[] getSupportedExtensions()
{
return SUPPORTED_EXT_ARR;
}
public static String getSVGExt()
{
return SVG_EXT;
}
public static String getZippedSVGExt()
{
return SVGZ_EXT;
}
public static String getPNGExt()
{
return PNG_EXT;
}
/**
* Testbed.
*
* @param args Unused.
*/
public static void main(String[] args)
{
System.out.println("Building the app home: '"+getAppHome()+"'");
System.out.println(buildAppHome());
System.out.println("Validating the app home");
System.out.println(isAppHomeValid());
System.out.println("Description");
System.out.println(getDescription());
}
}
MongoDB Logo MongoDB