|
| 1 | +// |
| 2 | +// LogWindow |
| 3 | +// This is UI code that provides a window for logging information |
| 4 | +// |
| 5 | +// $Id: LogWindow.js,v 1.15 2010/03/29 02:23:24 anonymous Exp $ |
| 6 | +// Copyright: (c)2005, xbytor |
| 7 | +// License: http://www.opensource.org/licenses/bsd-license.php |
| 8 | +// Contact: xbytor@gmail.com |
| 9 | +// |
| 10 | +//@show include |
| 11 | + |
| 12 | +LogWindow = function LogWindow(title, bounds, text) { |
| 13 | + var self = this; |
| 14 | + |
| 15 | + self.title = (title || 'Log Window'); |
| 16 | + self.bounds = (bounds || [100,100,740,580]); |
| 17 | + self.text = (text ? text : ''); |
| 18 | + self.useTS = false; |
| 19 | + self.textType = 'edittext'; // or 'statictext' |
| 20 | + self.inset = 15; |
| 21 | + self.debug = false; |
| 22 | + |
| 23 | + LogWindow.prototype.textBounds = function() { |
| 24 | + var self = this; |
| 25 | + var ins = self.inset; |
| 26 | + var bnds = self.bounds; |
| 27 | + var tbnds = [ins,ins,bnds[2]-bnds[0]-ins,bnds[3]-bnds[1]-35]; |
| 28 | + return tbnds; |
| 29 | + } |
| 30 | + LogWindow.prototype.btnPanelBounds = function() { |
| 31 | + var self = this; |
| 32 | + var ins = self.inset; |
| 33 | + var bnds = self.bounds; |
| 34 | + var tbnds = [ins,bnds[3]-bnds[1]-35,bnds[2]-bnds[0]-ins,bnds[3]-bnds[1]]; |
| 35 | + return tbnds; |
| 36 | + } |
| 37 | + |
| 38 | + LogWindow.prototype.setText = function setText(text) { |
| 39 | + var self = this; |
| 40 | + self.text = text; |
| 41 | + //fullStop(); |
| 42 | + if (self.win != null) { |
| 43 | + try { self.win.log.text = self.text; } catch (e) {} |
| 44 | + } |
| 45 | + } |
| 46 | + LogWindow.prototype.init = function(text) { |
| 47 | + var self = this; |
| 48 | + if (!text) text = ''; |
| 49 | + self.win = new Window('dialog', self.title, self.bounds); |
| 50 | + var win = self.win; |
| 51 | + win.owner = self; |
| 52 | + win.log = win.add(self.textType, self.textBounds(), text, |
| 53 | + {multiline:true}); |
| 54 | + win.btnPanel = win.add('panel', self.btnPanelBounds()); |
| 55 | + var pnl = win.btnPanel; |
| 56 | + pnl.okBtn = pnl.add('button', [15,5,115,25], 'OK', {name:'ok'}); |
| 57 | + pnl.clearBtn = pnl.add('button', [150,5,265,25], 'Clear', {name:'clear'}); |
| 58 | + if (self.debug) { |
| 59 | + pnl.debugBtn = pnl.add('button', [300,5,415,25], 'Debug', |
| 60 | + {name:'debug'}); |
| 61 | + } |
| 62 | + pnl.saveBtn = pnl.add('button', [450,5,565,25], 'Save', {name:'save'}); |
| 63 | + self.setupCallbacks(); |
| 64 | + } |
| 65 | + LogWindow.prototype.setupCallbacks = function() { |
| 66 | + var self = this; |
| 67 | + var pnl = self.win.btnPanel; |
| 68 | + |
| 69 | + pnl.okBtn.onClick = function() { this.parent.parent.owner.okBtn(); } |
| 70 | + pnl.clearBtn.onClick = function() { this.parent.parent.owner.clearBtn(); } |
| 71 | + if (self.debug) { |
| 72 | + pnl.debugBtn.onClick = function() { |
| 73 | + this.parent.parent.owner.debugBtn(); |
| 74 | + } |
| 75 | + } |
| 76 | + pnl.saveBtn.onClick = function() { this.parent.parent.owner.saveBtn(); } |
| 77 | + } |
| 78 | + LogWindow.prototype.okBtn = function() { this.close(1); } |
| 79 | + LogWindow.prototype.clearBtn = function() { this.clear(); } |
| 80 | + LogWindow.prototype.debugBtn = function() { $.level = 1; debugger; } |
| 81 | + LogWindow.prototype.saveBtn = function() { |
| 82 | + var self = this; |
| 83 | + // self.setText(self.text + self._prefix() + '\r\n'); |
| 84 | + self.save(); |
| 85 | + } |
| 86 | + |
| 87 | + LogWindow.prototype.save = function() { |
| 88 | + try { |
| 89 | + var self = this; |
| 90 | + var f = LogWindow.selectFileSave("Log File", |
| 91 | + "Log file:*.log,All files:*", |
| 92 | + "/c/temp"); |
| 93 | + if (f) { |
| 94 | + f.open("w") || throwError(f.error); |
| 95 | + try { f.write(self.text); } |
| 96 | + finally { try { f.close(); } catch (e) {} } |
| 97 | + } |
| 98 | + } catch (e) { |
| 99 | + alert(e.toSource()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + LogWindow.prototype.show = function(text) { |
| 104 | + var self = this; |
| 105 | + if (self.win == undefined) { |
| 106 | + self.init(); |
| 107 | + } |
| 108 | + self.setText(text || self.text); |
| 109 | + return self.win.show(); |
| 110 | + } |
| 111 | + LogWindow.prototype.close = function(v) { |
| 112 | + var self = this; |
| 113 | + self.win.close(v); |
| 114 | + self.win = undefined; |
| 115 | + } |
| 116 | + LogWindow.prototype._prefix = function() { |
| 117 | + var self = this; |
| 118 | + if (self.useTS) { |
| 119 | + return LogWindow.toISODateString() + "$ "; |
| 120 | + } |
| 121 | + return ''; |
| 122 | + } |
| 123 | + LogWindow.prototype.prefix = LogWindow.prototype._prefix; |
| 124 | + LogWindow.prototype.append = function(str) { |
| 125 | + var self = this; |
| 126 | + self.setText(self.text + self.prefix() + str + '\r\n'); |
| 127 | + } |
| 128 | + LogWindow.prototype.clear = function clear() { |
| 129 | + this.setText(''); |
| 130 | + } |
| 131 | + |
| 132 | + LogWindow.toISODateString = function (date) { |
| 133 | + if (!date) date = new Date(); |
| 134 | + var str = ''; |
| 135 | + function _zeroPad(val) { return (val < 10) ? '0' + val : val; } |
| 136 | + if (date instanceof Date) { |
| 137 | + str = date.getFullYear() + '-' + |
| 138 | + _zeroPad(date.getMonth()+1) + '-' + |
| 139 | + _zeroPad(date.getDate()) + ' ' + |
| 140 | + _zeroPad(date.getHours()) + ':' + |
| 141 | + _zeroPad(date.getMinutes()) + ':' + |
| 142 | + _zeroPad(date.getSeconds()); |
| 143 | + } |
| 144 | + return str; |
| 145 | + } |
| 146 | + |
| 147 | + LogWindow.selectFileSave = function(prompt, select, startFolder) { |
| 148 | + var oldFolder = Folder.current; |
| 149 | + if (startFolder) { |
| 150 | + if (typeof(startFolder) == "object") { |
| 151 | + if (!(startFolder instanceof "Folder")) { |
| 152 | + throw "Folder object wrong type"; |
| 153 | + } |
| 154 | + Folder.current = startFolder; |
| 155 | + } else if (typeof(startFolder) == "string") { |
| 156 | + var s = startFolder; |
| 157 | + startFolder = new Folder(s); |
| 158 | + if (startFolder.exists) { |
| 159 | + Folder.current = startFolder; |
| 160 | + } else { |
| 161 | + startFolder = undefined; |
| 162 | + // throw "Folder " + s + "does not exist"; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + var file = File.saveDialog(prompt, select); |
| 167 | + //alert("File " + file.path + '/' + file.name + " selected"); |
| 168 | + if (Folder.current == startFolder) { |
| 169 | + Folder.current = oldFolder; |
| 170 | + } |
| 171 | + return file; |
| 172 | + }; |
| 173 | +}; |
| 174 | + |
| 175 | +LogWindow.open = function(str, title) { |
| 176 | + var logwin = new LogWindow(title, undefined, str); |
| 177 | + logwin.show(); |
| 178 | + return logwin; |
| 179 | +}; |
| 180 | + |
| 181 | +function throwError(e) { |
| 182 | + throw e; |
| 183 | +}; |
| 184 | + |
| 185 | +"LogWindow.js"; |
| 186 | +// EOF |
| 187 | + |
0 commit comments