--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.client.message;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwt.user.client.ui.RichTextArea;
+import com.gwtext.client.core.EventObject;
+import com.gwtext.client.core.Function;
+import com.gwtext.client.core.NameValuePair;
+import com.gwtext.client.core.SortDir;
+import com.gwtext.client.data.ArrayReader;
+import com.gwtext.client.data.FieldDef;
+import com.gwtext.client.data.MemoryProxy;
+import com.gwtext.client.data.ObjectFieldDef;
+import com.gwtext.client.data.Record;
+import com.gwtext.client.data.RecordDef;
+import com.gwtext.client.data.SimpleStore;
+import com.gwtext.client.data.SortState;
+import com.gwtext.client.data.Store;
+import com.gwtext.client.data.StringFieldDef;
+import com.gwtext.client.util.Format;
+import com.gwtext.client.widgets.Button;
+import com.gwtext.client.widgets.Panel;
+import com.gwtext.client.widgets.Window;
+import com.gwtext.client.widgets.event.ButtonListenerAdapter;
+import com.gwtext.client.widgets.event.WindowListenerAdapter;
+import com.gwtext.client.widgets.form.ComboBox;
+import com.gwtext.client.widgets.grid.CellMetadata;
+import com.gwtext.client.widgets.grid.ColumnConfig;
+import com.gwtext.client.widgets.grid.ColumnModel;
+import com.gwtext.client.widgets.grid.GridEditor;
+import com.gwtext.client.widgets.grid.GridPanel;
+import com.gwtext.client.widgets.grid.GridView;
+import com.gwtext.client.widgets.grid.PropertyGridPanel;
+import com.gwtext.client.widgets.grid.Renderer;
+import com.gwtext.client.widgets.grid.RowParams;
+import com.gwtext.client.widgets.grid.event.EditorGridListenerAdapter;
+import com.gwtext.client.widgets.grid.event.GridMouseListenerAdapter;
+import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;
+import com.gwtext.client.widgets.layout.FitLayout;
+import com.gwtext.client.widgets.layout.RowLayout;
+import com.gwtext.client.widgets.layout.RowLayoutData;
+import com.gwtext.client.widgets.menu.BaseItem;
+import com.gwtext.client.widgets.menu.Item;
+import com.gwtext.client.widgets.menu.Menu;
+import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
+import com.pentila.evalcomp.client.ConnexionServer;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.utilities.GridPanelDropable;
+import com.pentila.evalcomp.client.utilities.Tools;
+import com.pentila.evalcomp.client.utilities.UserTools;
+import com.pentila.evalcomp.client.widget.ExtWindow;
+import com.pentila.evalcomp.domain.Entity;
+import com.pentila.evalcomp.domain.Group;
+import com.pentila.evalcomp.domain.Message;
+import com.pentila.evalcomp.domain.User;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class MessageViewTab. View of a message thread
+ */
+public class MessageViewTab extends Panel {
+
+ /** The reply window. */
+ private ExtWindow replyWindow = null;
+
+ /** The cfg columns msg. */
+ private ColumnConfig[] cfgColumnsMsg;
+
+ /** The ta. */
+ private RichTextArea ta;
+
+ //private TextArea ta2;
+ /** The render msg detail. */
+ private Renderer renderMsgDetail = new Renderer() {
+ public String render(Object value, CellMetadata cellMetadata,
+ Record record, int rowIndex, int colNum, Store store) {
+
+ final String icon = "<img src=\"" + Tools.getBaseURL()
+ + "images/add_comment-16x16.png\">";
+
+ Message msg = (Message) record.getAsObject("msgObject");
+ cellMetadata.setCssClass("alignRight");
+
+ String fn = "";
+ String ln = "";
+
+ if (msg.getAuthor() != null) {
+ fn = msg.getAuthor().getFirstname();
+ ln = msg.getAuthor().getLastname();
+ }
+
+
+
+ return icon + Format.format(Messages
+ .getString("format_auteur_date_message"), //$NON-NLS-1$
+ new String[] { ln, fn,
+ Tools.getDateFormated(msg.getCreationDate()),
+ Tools.getHourFormated(msg.getCreationDate()), });
+ }
+ };
+
+ /** The cfg column model msg. */
+ private ColumnModel cfgColumnModelMsg;
+
+ /** The current msg. */
+ private Message currentMsg = null;
+
+ /** tell if it's a one to one or one to many message. */
+ private boolean isOneToOneMessage = true;
+
+
+
+ // Definition des differents panel
+ /** The msg grid. */
+ private GridPanelDropable msgGrid;
+
+
+
+
+
+ /** The rich text. */
+ private RichTextArea richText = null;
+
+ /** The store message. */
+ private Store storeMessage;
+
+ /** The pgp. */
+ private PropertyGridPanel pgp = null;
+
+
+ /** The cb. */
+ private ComboBox cb = null;
+ /**
+ * Instantiates a new message view tab.
+ */
+
+ Menu menuEntity;
+
+ /** The content. */
+ private Panel content;
+
+
+
+ /**
+ * Instantiates a new message view tab.
+ */
+ public MessageViewTab() {
+ super();
+
+ ColumnConfig cc = new ColumnConfig(Messages
+ .getString("Details"), "msgDetail");
+ cc.setSortable(true);
+ cc.setRenderer(renderMsgDetail);
+ cc.setDataIndex("msgDetail");
+
+
+ cfgColumnsMsg = new ColumnConfig[] { cc
+
+ };
+ cfgColumnModelMsg = new ColumnModel(cfgColumnsMsg);
+
+ initializeInterface();
+ }
+
+ /**
+ * Gets the msg set.
+ *
+ * @param msg
+ * the msg
+ * @param msgSet
+ * the msg set
+ *
+ * @return the msg set
+ */
+ private Set<Message> getMsgSet(Message msg, Set<Message> msgSet) {
+ msgSet.add(msg);
+ for (Message m : msg.getReplies()) {
+ for (Message mBis : getMsgSet(m, msgSet)) {
+ msgSet.add(mBis);
+ }
+ }
+ return msgSet;
+ }
+
+ /**
+ * Initialize view msg thread interface.
+ *
+ * @param msg
+ * the msg
+ */
+ public void initializeViewMsgInterface(Message msg) {
+ currentMsg = msg;
+
+
+
+ AsyncCallback callback = new AsyncCallback() {
+ public void onFailure(Throwable caught) {
+ Tools.hideLoading();
+ System.out
+ .println("problem dans la recup des messages non lus");
+ }
+
+ public void onSuccess(Object result) {
+ Message mes = (Message) result;
+ Set<Message> setMsg = new HashSet<Message>();
+ int size = 2;
+ String app = null;
+
+ try {
+ String compLabel = mes.getEsi().getEvaluationSubject()
+ .getComputedName();
+ String evalLabel = mes.getEsi().getEvaluationSubject()
+ .getEvaluationDefinition().getName();
+
+
+ app = Messages.getString("A_propos_de") + compLabel
+ + Messages.getString("_dans_la_campagne") + evalLabel;
+
+ size++;
+
+ } catch (Exception ex) {
+
+ }
+
+ List<Entity> msgRecipList = new ArrayList<Entity>(mes
+ .getCurrentRecipients());
+
+ isOneToOneMessage = true;
+ if (msgRecipList.size() > 1) {
+ isOneToOneMessage = false;
+ }
+
+ Set<String> msgRecipSetName = new HashSet<String>();
+ for (Entity e : msgRecipList) {
+ if (e instanceof User) {
+ msgRecipSetName.add(((User) e).getLastname()
+ + " " + ((User) e).getFirstname()); //$NON-NLS-1$
+ } else {
+ // on met 1 to 1 à false car groupe
+ isOneToOneMessage = false;
+ msgRecipSetName
+ .add(Messages.getString("le_groupe") + ((Group) e).getUid()); //$NON-NLS-1$
+ }
+ }
+
+ NameValuePair[] source = new NameValuePair[size];
+ source[0] = new NameValuePair("Sujet", mes.getSubject());
+ String[][] mp = new String[msgRecipSetName.size()][];
+
+ int j = 0;
+
+ for (String l :msgRecipSetName) {
+ mp[j] = new String[] { l};
+ j++;
+ }
+
+ RecordDef rdef = new RecordDef(new FieldDef[] {
+ new StringFieldDef("username") });
+ MemoryProxy memProxy = new MemoryProxy(mp);
+ ArrayReader cbreader = new ArrayReader(rdef);
+ Store cbStore = new Store(memProxy, cbreader);
+ cbStore.setSortInfo(new SortState("username", SortDir.ASC));
+ cbStore.load();
+
+ cb = new ComboBox();
+
+ cb.setDisplayField("username");
+ cb.setStore(cbStore);
+ cb.setTriggerAction(ComboBox.ALL);
+ cb.setMode(ComboBox.LOCAL);
+ cb.setReadOnly(true);
+ cb.setForceSelection(true);
+ cb.setEditable(false);
+ cb.setSelectOnFocus(true);
+ cb.setAutoShow(true);
+ cb.addListener("select", new Function(){
+
+ public void execute() {
+ cb.expand();
+ }
+
+
+ });
+
+ GridEditor ge = new GridEditor(cb);
+
+ String ret = "";
+ String firstMember = "";
+
+ String ownerName = mes.getAuthor().getLastname() + " " + mes.getAuthor().getFirstname();
+ firstMember = ownerName;
+
+ if (cbStore.getCount() > 1){
+ ret = Format.format(Messages.getString("et_n_participants"), "" + (cbStore.getCount()-1));
+ firstMember += ", ";
+ }else if (cbStore.getCount() == 1){
+ firstMember += " et ";
+ firstMember = firstMember + cbStore.getAt(0).getAsString("username");
+ }
+
+ source[1] = new NameValuePair(Messages.getString("Destinataires"), firstMember + ret);
+
+ if (size > 2){
+ source[2] = new NameValuePair(Messages.getString("Evaluation"), app);
+ }
+
+
+ Map<String, GridEditor> ce = new HashMap<String, GridEditor>();
+
+ ce.put(Messages.getString("Destinataires"), ge);
+ pgp.setCustomEditors(ce);
+
+
+ pgp.setSource(source);
+ //pgp.getColumnModel().setColumnWidth(0, 100);
+ //pgp.getColumnModel().setColumnWidth(1, 470);
+
+
+
+ List<String> msgRecipListName = new ArrayList<String>(
+ msgRecipSetName);
+
+ Collections.sort(msgRecipListName, new Comparator<String>() {
+ public int compare(String r1, String r2) {
+
+ return r1.compareTo(r2);
+ }
+ });
+
+ setMsg = getMsgSet(mes, setMsg);
+
+ List<Message> msgList = new ArrayList<Message>(setMsg);
+ Collections.sort(msgList, new Comparator<Message>() {
+ public int compare(Message msg1, Message msg2) {
+
+ return msg2.getCreationDate().compareTo(
+ msg1.getCreationDate());
+ }
+ });
+
+ Object[][] objList = new Object[msgList.size()][];
+ for (int i = 0; i < msgList.size(); i++) {
+ Message msg = (Message) msgList.toArray()[i];
+ Object[] monObjet = new Object[] { msg };
+ objList[i] = monObjet;
+ }
+ storeMessage = new SimpleStore(0,
+ new FieldDef[] { new ObjectFieldDef("msgObject") },
+ objList);
+ storeMessage.load();
+ msgGrid.reconfigure(storeMessage, cfgColumnModelMsg);
+
+ Tools.hideLoading();
+ // On met a jour la portlet message
+ //MessagerieWidget.updateMessageList();
+ }
+ };
+ Tools
+ .showLoading(
+ Messages.getString("MAJ_fil_de_discussion"), Messages.getString("Chargement"));
+ RequestBuilder rb = ConnexionServer.messageService.readMessage(
+ UserTools.getUser().getUid(), msg.getId(), callback);
+ rb.setHeader("code", "A12");
+
+ ConnexionServer.doSend(rb);
+
+ }
+
+ /**
+ * Initialize GUI.
+ */
+ private void initializeInterface() {
+
+ this.setLayout(new FitLayout());
+
+ this.setPaddings(5);
+ this.setBorder(false);
+
+ content = new Panel();
+ content.setBorder(false);
+ content.setLayout(new RowLayout());
+
+
+ Panel p = new Panel();
+ ta = new RichTextArea();
+
+ ta.setSize("500px", "300px");
+ p.add(ta);
+
+ final ExtWindow w = new ExtWindow();
+ w.setCollapsible(true);
+ w.setTitle("Presse-papiers");
+ w.setCloseAction(Window.HIDE);
+ w.add(p);
+
+ storeMessage = new SimpleStore(new String[] { "msgObject" },
+ new Object[][] {});
+ storeMessage.load();
+
+ msgGrid = new GridPanelDropable() {
+ @Override
+ public boolean isTrashable(Object ...objects) {
+ return false;
+ }
+
+ @Override
+ public void trashedElements(Object ...objects) {
+ }
+
+ };
+
+
+
+ GridView gridView = new GridView() {
+ @Override
+ public String getRowClass(Record record, int index,
+ RowParams rowParams, Store store) {
+ rowParams.setBody(Format.format(
+ "<p style=\"text-align:justify\">{0}</p>",
+ ((Message) record.getAsObject("msgObject")).getBody()));
+ return "";
+ }
+ };
+
+ gridView.setEnableRowBody(true);
+
+ msgGrid.setColumnModel(cfgColumnModelMsg);
+ msgGrid.setStore(storeMessage);
+
+ msgGrid.setEnableDragDrop(false);
+
+ msgGrid.setHeader(false);
+ msgGrid.setBorder(true);
+ msgGrid.setHideColumnHeader(true);
+ msgGrid.setAutoScroll(true);
+ msgGrid.setDdGroup("evalCompDDGroup");
+ gridView.setForceFit(true);
+ gridView.setAutoFill(true);
+ msgGrid.setView(gridView);
+ msgGrid.addGridRowListener(new GridRowListenerAdapter() {
+ @Override
+ public void onRowClick(GridPanel grid, int rowIndex, EventObject e) {
+
+ }
+
+ @Override
+ public void onRowContextMenu(GridPanel grid, int rowIndex,
+ EventObject e) {
+
+ msgGrid.getSelectionModel().selectRow(rowIndex, true);
+ e.stopEvent();
+
+ if (menuEntity == null) {
+ menuEntity = new Menu();
+
+ Item copyItem = new Item(Messages
+ .getString("copier"), //$NON-NLS-1$
+ new BaseItemListenerAdapter() {
+ @Override
+ public void onClick(BaseItem item, EventObject e) {
+
+
+
+ copyToWindow();
+ w.show();
+ w.getEl().setStyle("z-index", "1000000");
+ }
+ });
+
+
+
+ menuEntity.addItem(copyItem);
+ }
+ menuEntity.showAt(e.getXY());
+
+ }
+
+ @Override
+ public void onRowDblClick(GridPanel grid, int rowIndex,
+ EventObject e) {
+ copyToWindow();
+ w.show();
+ w.getEl().setStyle("z-index", "1000000");
+ }
+
+ });
+
+
+ replyWindow = new ExtWindow();
+ replyWindow.setTitle(Messages.getString("Ajouter_un_commentaire"), "iconAddComment16");
+ replyWindow.setWidth("600px");
+
+ richText = new RichTextArea();
+ richText.setSize("550px", "200px");
+
+ com.gwtext.client.widgets.form.Label l = new com.gwtext.client.widgets.form.Label();
+ l.setHtml("<br><b>" + Messages.getString("message_warning") + "</b><br><br>");
+ replyWindow.add(l);
+ replyWindow.add(richText);
+ replyWindow.setCloseAction(Window.HIDE);
+ replyWindow.addListener(new WindowListenerAdapter() {
+ @Override
+ public void onActivate(Window source) {
+ richText.setText("");
+ richText.setFocus(true);
+ }
+ });
+
+ // add a couple of buttons to the form
+ Button sendButton = new Button(Messages.getString("Envoyer")); //$NON-NLS-1$
+ sendButton.addListener(new ButtonListenerAdapter() {
+ @Override
+ public void onClick(Button button, EventObject e) {
+ replyMsg();
+ }
+ });
+ replyWindow.addButton(sendButton);
+
+ Panel replyVpBas = new Panel();
+ replyVpBas.setBorder(false);
+
+ Button reponseLabelBas = new Button(Messages.getString("Repondre_"), new ButtonListenerAdapter() {
+ public void onClick(Button button, EventObject e) {
+
+ replyWindow.show();
+ replyWindow.getEl().setStyle("z-index", "1000000");
+ }
+ });
+
+ reponseLabelBas.setIconCls("iconAddComment16");
+ replyVpBas.add(reponseLabelBas);
+
+
+
+ Panel replyVpHaut = new Panel();
+ replyVpHaut.setBorder(false);
+
+ Button reponseLabelHaut = new Button(Messages.getString("Repondre_"), new ButtonListenerAdapter() {
+ public void onClick(Button button, EventObject e) {
+
+ replyWindow.show();
+ replyWindow.getEl().setStyle("z-index", "1000000");
+ }
+ });
+ reponseLabelHaut.setIconCls("iconAddComment16");
+ replyVpHaut.add(reponseLabelHaut);
+
+ pgp = new PropertyGridPanel();
+ pgp.hideColumnHeader();
+
+ GridView view = new GridView();
+ view.setForceFit(true);
+ view.setAutoFill(true);
+ pgp.setView(view);
+
+ pgp.addGridMouseListener(new GridMouseListenerAdapter() {
+ public void onMouseOver(EventObject e) {
+
+ GridView gv = pgp.getView();
+ int col = findCellIndex(pgp.getView(), e);
+ int row = gv.findRowIndex(e);
+
+ if (row == 1) {
+ pgp.startEditing(row, col);
+ cb.expand();
+ }
+
+ }
+
+ public void onMouseOut(EventObject e) {
+
+ }
+
+ });
+
+ pgp.addEditorGridListener(new EditorGridListenerAdapter(){
+ public boolean doBeforeEdit(GridPanel grid, Record record, String field, Object value, int rowIndex, int colIndex){
+ if (rowIndex == 1){
+ return true;
+ }
+ return false;
+ }
+ });
+
+ content.add(pgp, new RowLayoutData(100));
+
+
+ content.add(replyVpHaut, new RowLayoutData(30));
+ content.add(msgGrid, new RowLayoutData("60%"));
+ content.add(replyVpBas, new RowLayoutData(30));
+
+
+ add(content);
+
+ }
+
+ /**
+ * Reply to a msg.
+ */
+ private void replyMsg() {
+ Message msg = new Message();
+ msg.setAuthor(UserTools.getUser());
+ msg.setBody(richText.getHTML());
+
+
+ AsyncCallback callback = new AsyncCallback() {
+ public void onFailure(Throwable caught) {
+ replyWindow.hide();
+ }
+
+ public void onSuccess(Object result) {
+ Message msg = (Message) result;
+ initializeViewMsgInterface(msg);
+ replyWindow.hide();
+ }
+ };
+
+ RequestBuilder rb = ConnexionServer.messageService.reply(currentMsg
+ .getId(), msg, callback);
+ rb.setHeader("code", "A12");
+ ConnexionServer.doSend(rb);
+ }
+
+
+
+ /**
+ * Copy to window.
+ */
+ public void copyToWindow(){
+ Record[] ar = msgGrid.getSelectionModel().getSelections();
+ String text = "";
+
+ final String icon = "<img src=\"" + Tools.getBaseURL() //$NON-NLS-1$
+ + "images/add_comment-16x16.png\">";
+
+
+
+ for (Record r : ar){
+
+ Message msg = (Message) r.getAsObject("msgObject");
+
+
+
+ String fn = "";
+ String ln = "";
+
+ if (msg.getAuthor() != null) {
+ fn = msg.getAuthor().getFirstname();
+ ln = msg.getAuthor().getLastname();
+ }
+
+ String head = icon + Format.format(Messages
+ .getString("format_auteur_date_message"),
+ new String[] { ln, fn,
+ Tools.getDateFormated(msg.getCreationDate()),
+ Tools.getHourFormated(msg.getCreationDate()), });
+
+
+
+
+ text += "<b>" + msg.getSubject()+ "</b> " +head+"<br/><br/>" +msg.getBody()+"<br/>" +"<hr/>";
+
+ }
+
+
+ ta.setHTML(text);
+ }
+
+
+ /**
+ * Find cell index.
+ *
+ * @param gv the gv
+ * @param evtObj the evt obj
+ *
+ * @return the int
+ */
+ public native int findCellIndex(GridView gv, EventObject evtObj)/*-{
+ var index = -1;
+ var view = gv.@com.gwtext.client.core.JsObject::getJsObj()();
+ var jsObj = evtObj.@com.gwtext.client.core.JsObject::getJsObj()();
+
+ try{
+ var t = $wnd.Ext.lib.Event.getTarget(jsObj);
+ index = view.grid.getView().findCellIndex(t);
+ if(index === false) index = -1;
+ }catch(e){}
+
+ return index;
+ }-*/;
+
+
+
+
+}