Skip to content

Commit

Permalink
Better adding messages to the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
concon121 committed May 31, 2016
1 parent 0979f87 commit 23edbe9
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 83 deletions.
26 changes: 10 additions & 16 deletions lib/atom-maven.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,31 @@ export default {
},

setup: function () {
console.info('atom-maven is configuring your classpath');
ui.info('atom-maven is configuring your classpath');
var self = this;
mvn.getPoms((pom) => {
var cp = self.getClasspath(pom.pomPath);
pom.registerLoadCompleteEvent(() => {
self.writeClasspath(cp, pom, self);
});
pom.registerChangeEvent(() => {
ui.clearFileMessages(pom.pomPath);
ui.clearSuccess();
ui.clearInfo();
pom.reload();
});
});
},

handleChange: function (pom, writeCp) {
if (!pom.changing) {
try {
pom.reload(writeCp);
} catch (err) {
console.error(err);
pom.changing = false;
}
}
},

writeClasspath: function (cp, pom, self) {

ui.clearFileMessages(pom.pomPath);
//ui.clearFileMessages(pom.pomPath);
var locations = self.initLocations() + self.getClasspathFromDependencies(pom, self);
fs.writeFile(cp, locations, (err) => {
if (err) console.error(err);
if (err) ui.error(err);
pom.changing = false;
});
console.info('atom-maven has finished configuring the classpath: ' + cp);
ui.success('atom-maven has finished configuring the classpath: ' + cp);

},

Expand Down Expand Up @@ -78,8 +70,10 @@ export default {
var substr = xml.substr(0, result.index);
var lineNo = substr.match(/\n/g).length + 1;
var message = mvn.getDependencyNotFoundMessage(dependency);
var preview = '<dependency>\n\t<groupId>' + dependency.groupId + '</groupId>\n\t<artifactId>' + dependency.artifactId + '</artifactId>\n\t<version>' + dependency.version + '</version>\n</dependency>';


ui.addLineMessage(message, lineNo, 0, file, 'error');
ui.error(message, lineNo, 0, file, preview);

}

Expand Down
3 changes: 1 addition & 2 deletions lib/file-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var FileUtils = function () {
fs.accessSync(file, fs.F_OK);
} catch (err) {
if (doReport) {
console.warn('File does not exist: ' + file);
ui.addPlainMessage('File does not exist: ' + file, 'warning');
ui.warning('File does not exist: ' + file);
}
exists = false;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/maven-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var MavenUtils = function () {
var e = mavenElems[0];
settingsFileLocation = (e.endsWith('bin')) ? e.replace('bin', self.settings) : e.concat(self.settings);
} else {
ui.addPlainMessage('Maven has not been found on the PATH, please ensure that Maven has been installed.', 'warning');
ui.warning('Maven has not been found on the PATH, please ensure that Maven has been installed.');
}
return settingsFileLocation;
},
Expand Down Expand Up @@ -124,7 +124,7 @@ var MavenUtils = function () {
result = $($.parseXML(xml)).find(selector);
} catch (err) {
console.error(err);
ui.addLineMessage('Invalid XML Document', null, null, (pom) ? pom.pomPath : null, 'error');
ui.error('Invalid XML Document', null, null, (pom) ? pom.pomPath : null);
}
return (result && children) ? result.children() : result;
},
Expand Down
6 changes: 4 additions & 2 deletions lib/pom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ui = require('./ui-utils');

class Pom {

constructor(opts, path) {
Expand Down Expand Up @@ -229,7 +231,7 @@ Pom.prototype.loadClasspath = function (pom) {
loadCompleteEvent();
}
}
}, 5e3);
}, 1e4);

};

Expand Down Expand Up @@ -442,7 +444,7 @@ Pom.prototype.reload = function (callback) {
if (err) console.error(self.pomPath, err);
self.xml = xml;
self.xml2js.parseString(xml, function (err, result) {
if (err) console.error(self.pomPath, err);
if (err) ui.error(err);
self.$.extend(self, result);
if (callback && typeof callback === 'function') callback();
self.events.emit(self.eventPrefix + 'loaded');
Expand Down
213 changes: 152 additions & 61 deletions lib/ui-utils.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,164 @@
var UiUtils = function () {
const $ = require('jquery');

const $ = require('jquery');
class UiUtils {

return {
constructor() {
this.messages = null;
}

messages: null,
}

initMessagePanel: function () {
if (this.messages === null) {
var MessagePanelView = require('atom-message-panel').MessagePanelView;
this.messages = new MessagePanelView({
title: 'atom-maven report'
});
this.messages.attach();
UiUtils.prototype.initMessagePanel = function () {
if (this.messages === null) {
var MessagePanelView = require('atom-message-panel').MessagePanelView;
this.messages = new MessagePanelView({
title: 'atom-maven report'
});
this.messages.attach();
}
};

UiUtils.prototype.addPlainMessage = function (text, level) {
this.initMessagePanel();
if (!this.containsMessage(text)) {
var PlainMessageView = require('atom-message-panel').PlainMessageView;
this.messages.add(new PlainMessageView({
message: text,
className: 'text-' + level
}));
}
};

UiUtils.prototype.addLineMessage = function (text, fileLine, lineChar, path, level, pre) {
this.initMessagePanel();
var LineMessageView = require('atom-message-panel').LineMessageView;
if (!this.containsLine(path, text)) {
var line = new LineMessageView({
message: text,
line: fileLine,
character: lineChar,
className: 'text-' + level,
file: path,
preview: pre
});
this.messages.add(line);
}
};

UiUtils.prototype.clearFileMessages = function (path) {
if (this.messages) {
const self = this;
var decrement = 0;
for (var index in self.messages.messages) {
var elem = self.messages.messages[index];
if (elem && elem.file === path && elem.attr('class') === 'line-message') {
var viewArry = self.messages.messages.splice(index - decrement, 1);
var view = (viewArry.length > 0) ? viewArry[0] : null;
var html = (view !== null && view.length > 0) ? view[0] : null;
decrement++;
$(html).remove();
}
},

addPlainMessage: function (text, level) {
this.initMessagePanel();
var PlainMessageView = require('atom-message-panel').PlainMessageView;
this.messages.add(new PlainMessageView({
message: text,
className: 'text-' + level
}));
},

addLineMessage: function (text, fileLine, lineChar, path, level) {
this.initMessagePanel();
var LineMessageView = require('atom-message-panel').LineMessageView;
var line = new LineMessageView({
message: text,
line: fileLine,
character: lineChar,
className: 'text-' + level,
file: path
});
if (!this.contains(line)) this.messages.add(line);
},

clearFileMessages: function (path) {
if (this.messages) {
const self = this;
var decrement = 0;
$.each(self.messages.messages, (index, elem) => {
if (elem && elem.file === path) {
var viewArry = self.messages.messages.splice(elem - decrement, 1);
var view = (viewArry.length > 0) ? viewArry[0] : null;
var html = (view !== null && view.length > 0) ? view[0] : null;
decrement++;
$(html).remove();
}
});
}
if (self.messages.messages.length === 0) {
self.messages.toggle();
}
}
};

UiUtils.prototype.clearInfo = function (path) {
if (this.messages) {
const self = this;
var decrement = 0;
for (var index in self.messages.messages) {
var elem = self.messages.messages[index];
if (elem && elem.attr('class').indexOf('text-info') >= 0) {
var viewArry = self.messages.messages.splice(index - decrement, 1);
var view = (viewArry.length > 0) ? viewArry[0] : null;
var html = (view !== null && view.length > 0) ? view[0] : null;
decrement++;
$(html).remove();
}
},

contains: function (line, type) {
const self = this;
var contains = false;
if (this.messages) {
$.each(self.messages.messages, (index, message) => {
contains = (message.file === line.file && message.message === line.message);
return !(contains);
});
}
if (self.messages.messages.length === 0) {
self.messages.toggle();
}
}
};

UiUtils.prototype.clearSuccess = function (path) {
if (this.messages) {
const self = this;
var decrement = 0;
for (var index in self.messages.messages) {
var elem = self.messages.messages[index];
if (elem && elem.attr('class').indexOf('text-success') >= 0) {
var viewArry = self.messages.messages.splice(index - decrement, 1);
var view = (viewArry.length > 0) ? viewArry[0] : null;
var html = (view !== null && view.length > 0) ? view[0] : null;
decrement++;
$(html).remove();
}
return contains;
}
if (self.messages.messages.length === 0) {
self.messages.toggle();
}
}
};

UiUtils.prototype.containsLine = function (file, text) {
const self = this;
if (self.messages && file && text) {
for (var message of self.messages.messages) {
if (message && message.file === file && message.message === text) {
return true;
}
}
}
return false;
};

UiUtils.prototype.containsMessage = function (text) {
const self = this;
if (self.messages && text) {
for (var message of self.messages.messages) {
if (message && message.html() === text) {
return true;
}
}
}
return false;
};

UiUtils.prototype.success = function (message, fileLine, lineChar, path, pre) {
if (path) {
this.addLineMessage(message, fileLine, lineChar, path, 'success', pre);
} else {
this.addPlainMessage(message, 'success');
}
};

};
UiUtils.prototype.info = function (message, fileLine, lineChar, path, pre) {
if (path) {
this.addLineMessage(message, fileLine, lineChar, path, 'info', pre);
} else {
this.addPlainMessage(message, 'info');
}
};

UiUtils.prototype.warning = function (message, fileLine, lineChar, path, pre) {
if (path) {
this.addLineMessage(message, fileLine, lineChar, path, 'warning', pre);
} else {
this.addPlainMessage(message, 'warning');
}
};

UiUtils.prototype.error = function (message, fileLine, lineChar, path, pre) {
if (path) {
this.addLineMessage(message, fileLine, lineChar, path, 'error', pre);
} else {
this.addPlainMessage(message, 'error');
}
};

module.exports = UiUtils();
module.exports = new UiUtils();

0 comments on commit 23edbe9

Please sign in to comment.