--- /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.widget.documenteditor;
+
+import java.util.Set;
+
+import com.axeiya.gwtckeditor.client.CKConfig;
+import com.axeiya.gwtckeditor.client.CKConfig.TOOLBAR_OPTIONS;
+import com.axeiya.gwtckeditor.client.CKEditor;
+import com.axeiya.gwtckeditor.client.event.SaveEvent;
+import com.axeiya.gwtckeditor.client.event.SaveHandler;
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NodeList;
+import com.google.gwt.http.client.URL;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.gwtext.client.core.EventObject;
+import com.gwtext.client.core.Ext;
+import com.gwtext.client.core.ExtElement;
+import com.gwtext.client.core.RegionPosition;
+import com.gwtext.client.core.UrlParam;
+import com.gwtext.client.data.ArrayReader;
+import com.gwtext.client.data.FieldDef;
+import com.gwtext.client.data.IntegerFieldDef;
+import com.gwtext.client.data.MemoryProxy;
+import com.gwtext.client.data.RecordDef;
+import com.gwtext.client.data.Store;
+import com.gwtext.client.data.StringFieldDef;
+import com.gwtext.client.widgets.BoxComponent;
+import com.gwtext.client.widgets.Button;
+import com.gwtext.client.widgets.Panel;
+import com.gwtext.client.widgets.Toolbar;
+import com.gwtext.client.widgets.ToolbarButton;
+import com.gwtext.client.widgets.event.ButtonListenerAdapter;
+import com.gwtext.client.widgets.event.PanelListenerAdapter;
+import com.gwtext.client.widgets.layout.BorderLayout;
+import com.gwtext.client.widgets.layout.BorderLayoutData;
+import com.gwtextux.client.widgets.upload.UploadDialog;
+import com.gwtextux.client.widgets.upload.UploadDialogListenerAdapter;
+import com.pentila.evalcomp.client.ConnexionServer;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.utilities.Tools;
+import com.pentila.evalcomp.client.utilities.UserTools;
+import com.pentila.evalcomp.domain.RessourceAbstract;
+import com.pentila.evalcomp.domain.transit.RessourceFile;
+import com.pentila.evalcomp.domain.transit.TDataStore;
+
+public class DocumentEditor extends Panel {
+
+ private ImageChooser ic;
+
+ private Store store;
+
+ private static RessourceAbstract ressourceAbstract;
+
+ private CKEditor ckee;
+
+ public DocumentEditor(){
+ super();
+ exportStaticMethod();
+ }
+
+ public DocumentEditor(RessourceAbstract ressourceAbstract){
+ super();
+ exportStaticMethod();
+ DocumentEditor.ressourceAbstract = ressourceAbstract;
+
+ }
+
+
+ public void setRessourceAbstarct(RessourceAbstract ra){
+ DocumentEditor.ressourceAbstract = ra;
+ }
+
+
+ public void createRessource(){
+
+ CKConfig CONFIG_MODIFICATION = new CKConfig();
+
+ // Creating personalized toolbar
+ MyToolbarLine line = new MyToolbarLine();
+ // Define the first line
+ String[] t1 = { TOOLBAR_OPTIONS.Save.toString(), "exportpdf", TOOLBAR_OPTIONS.NewPage.toString(), TOOLBAR_OPTIONS.Preview.toString(), TOOLBAR_OPTIONS.Print.toString(),"/",
+ TOOLBAR_OPTIONS.SelectAll.toString(),TOOLBAR_OPTIONS.Cut.toString(), TOOLBAR_OPTIONS.Copy.toString(),TOOLBAR_OPTIONS.Paste.toString(), TOOLBAR_OPTIONS.PasteText.toString(),TOOLBAR_OPTIONS.PasteFromWord.toString(), "/",
+ TOOLBAR_OPTIONS.SpellChecker.toString(), TOOLBAR_OPTIONS.Scayt.toString(),"/",
+ TOOLBAR_OPTIONS.Undo.toString(), TOOLBAR_OPTIONS.Redo.toString(),"/",
+ TOOLBAR_OPTIONS.Find.toString(),TOOLBAR_OPTIONS.Replace.toString(), "/" };
+ line.addAll(t1);
+
+ // Define the second line
+ MyToolbarLine line2 = new MyToolbarLine();
+ String[] t2 = {
+ TOOLBAR_OPTIONS.Bold.toString(), TOOLBAR_OPTIONS.Italic.toString(),TOOLBAR_OPTIONS.Underline.toString(), TOOLBAR_OPTIONS.Strike.toString(),
+ TOOLBAR_OPTIONS.Subscript.toString(), TOOLBAR_OPTIONS.Superscript.toString(), "/", TOOLBAR_OPTIONS.RemoveFormat.toString(), "/",
+ TOOLBAR_OPTIONS.NumberedList.toString(),TOOLBAR_OPTIONS.BulletedList.toString(),"/",
+ TOOLBAR_OPTIONS.Outdent.toString(), TOOLBAR_OPTIONS.Indent.toString(),TOOLBAR_OPTIONS.Blockquote.toString(), "/",
+ TOOLBAR_OPTIONS.JustifyLeft.toString(), TOOLBAR_OPTIONS.JustifyCenter.toString(), TOOLBAR_OPTIONS.JustifyRight.toString(), TOOLBAR_OPTIONS.JustifyBlock.toString(), "/", TOOLBAR_OPTIONS.Link.toString(), TOOLBAR_OPTIONS.Unlink.toString(), "/",
+ TOOLBAR_OPTIONS.Table.toString(), TOOLBAR_OPTIONS.HorizontalRule.toString(),TOOLBAR_OPTIONS.SpecialChar.toString(), TOOLBAR_OPTIONS.PageBreak.toString() };
+ line2.addAll(t2);
+
+ //define the third line
+ MyToolbarLine line3 = new MyToolbarLine();
+
+ String[] t3 = {
+ "Styles", "Format", "Font", "FontSize", "/",
+ "TextColor", "BGColor", "/",
+ "ShowBlocks" , "insertvar"};
+ line3.addAll(t3);
+
+ // Creates the toolbar
+ com.axeiya.gwtckeditor.client.Toolbar t = new com.axeiya.gwtckeditor.client.Toolbar();
+ t.add(line);
+ t.addSeparator();
+ t.add(line2);
+ t.addSeparator();
+ t.add(line3);
+
+ CONFIG_MODIFICATION.setResizeEnabled(false);
+
+ // Set the toolbar to the config (replace the FULL preset toolbar)
+ CONFIG_MODIFICATION.setToolbar(t);
+
+ CONFIG_MODIFICATION.setUseFormPanel(true);
+ CONFIG_MODIFICATION.setBaseFloatZIndex(3000000);
+
+ CONFIG_MODIFICATION.setHeight((600-170) + "px");
+ //CONFIG_MODIFICATION.addExtraPlugin("insertvar");
+ CONFIG_MODIFICATION.addExtraPlugin("exportpdf");
+
+ ckee = new CKEditor( CONFIG_MODIFICATION );
+
+ ckee.setWidth("100%");
+
+ ckee.addSaveHandler(new SaveHandler<CKEditor>() {
+ public void onSave(SaveEvent<CKEditor> event) {
+ event.getTarget();
+ String content = event.getText(); //The content to save (also available in target)
+
+
+
+
+ content = content.replaceAll("src=\"getFile", "src=\"" + GWT.getHostPageBaseURL() + "getFile");
+
+
+ saveFile(ressourceAbstract.getPath(), ressourceAbstract.getPluginKey(), content);
+
+
+ }
+ });
+
+ this.setLayout(new BorderLayout());
+
+ MemoryProxy dataProxy = new MemoryProxy(new Object[][]{});
+ RecordDef recordDef = new RecordDef(new FieldDef[]{
+ new StringFieldDef("id"),
+ new StringFieldDef("name"),
+ new IntegerFieldDef("size"),
+ new IntegerFieldDef("lastmod", "timestamp"),
+ new StringFieldDef("url"),
+ new StringFieldDef("ressourceAbstractId"),
+ new StringFieldDef("className")
+ });
+ ArrayReader reader = new ArrayReader(recordDef);
+ store = new Store(dataProxy, reader, true);
+ store.load();
+
+ ic = new ImageChooser("Image Chooser", 515, 400, store, this);
+
+ Toolbar topToolbarRef = new Toolbar();
+
+ ToolbarButton addFileButton = new ToolbarButton();
+
+ addFileButton.setIcon(Tools.getBaseURL() + "images/page_add.png");
+ addFileButton.setTooltip(Messages.getString("Ajouter_un_fichier"));
+
+ addFileButton.addListener(new ButtonListenerAdapter() {
+ @Override
+ public void onClick(Button button, EventObject e) {
+ uploadTemplate();
+ }
+ });
+
+ topToolbarRef.addButton(addFileButton);
+ ic.setTopToolbar(topToolbarRef);
+
+
+
+
+
+ BorderLayoutData eastLayout = new BorderLayoutData(RegionPosition.EAST);
+ eastLayout.setMinSize(150);
+ eastLayout.setMaxSize(300);
+
+ eastLayout.setSplit(true);
+ ic.setTitle("Images");
+ ic.setCollapsible(true);
+ ic.setWidth(150);
+
+
+ this.add(ic,eastLayout);
+ this.add(ckee, new BorderLayoutData(RegionPosition.CENTER));
+
+
+
+ this.addListener(new PanelListenerAdapter(){
+ public void onResize(BoxComponent component, int adjWidth,
+ int adjHeight, int rawWidth, int rawHeight) {
+
+ try{
+
+ NodeList<Element> l = ckee.getElement().getElementsByTagName("textarea");
+
+ String name = l.getItem(0).getAttribute("name");
+
+
+ ExtElement ee = Ext.get("cke_contents_" + name);
+
+ ee.setStyle("height", (adjHeight-140)+"px");
+ }catch (Exception e){
+ //e.printStackTrace();
+ }
+ }
+
+
+
+ });
+
+
+ initDocument();
+
+ }
+
+
+
+ protected void saveFile(String pathTo, String pluginkey, String content) {
+
+ AsyncCallback callback = new AsyncCallback() {
+
+ public void onFailure(Throwable caught) {
+ System.out.println("Probleme dans la recup des emplacements");
+ Tools.hideLoadingRegion(getId());
+ }
+
+ public void onSuccess(Object result) {
+
+ Tools.hideLoadingRegion(getId());
+ }
+ };
+ Tools.showLoadingRegion(getId(), Messages.getString("Chargement"), Messages.getString("Chargement"));
+
+ String parentPath = ressourceAbstract.getPath().substring(0, ressourceAbstract.getPath().lastIndexOf("/"));
+
+ ConnexionServer.ressourceService.saveRessource(ressourceAbstract.getId(), ressourceAbstract.getName(), parentPath, ressourceAbstract.getPluginKey(), content, callback);
+ }
+
+
+
+ public void uploadTemplate() {
+ UploadDialog dialog = new UploadDialog();
+ dialog.setUrl("uploadWSFile");
+
+ UrlParam param[] = new UrlParam[3];
+
+ param[0] = new UrlParam("type", "templateUpload");
+ param[1] = new UrlParam("objectid", "" + ressourceAbstract.getId());
+ param[2] = new UrlParam("classname", "com.pentila.evalcomp.domain.RessourceAbstract" );
+
+ dialog.setBaseParams(param);
+
+ dialog.setPostVarName("file");
+
+ dialog.addListener(new UploadDialogListenerAdapter() {
+
+ @Override
+ public void onUploadComplete(UploadDialog source) {
+ updateImageChooser();
+ }
+ });
+
+ dialog.show();
+ }
+
+ protected void updateImageChooser() {
+ if (ressourceAbstract != null){
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable caught) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void onSuccess(Object result) {
+ Set<TDataStore> stds = (Set<TDataStore>) result;
+
+ if (stds != null){
+
+
+
+ Object[][] objList = new Object[stds.size()][];
+ int i = 0;
+ for (TDataStore tds : stds){
+
+ String code = Tools.base64encode(ressourceAbstract.getId() + ":" + "com.pentila.evalcomp.domain.RessourceAbstract" + ":" + URL.encodeQueryString(tds.getName()));
+
+ String url = Tools.getJSPURL() + "getFile.jsp?objectId=" + code;
+ objList[i++] = new Object[]{tds.getId(), tds.getName(), 10, 1, url, ressourceAbstract.getId().toString(), "com.pentila.evalcomp.domain.RessourceAbstract"};
+ }
+
+ MemoryProxy dataProxy = new MemoryProxy(objList);
+ store.setDataProxy(dataProxy);
+
+ store.reload();
+ }
+
+
+ }
+
+ };
+
+ ConnexionServer.ressourceService.getFilesDataStore(ressourceAbstract.getId(), "com.pentila.evalcomp.domain.RessourceAbstract", callback);
+ }
+ }
+
+
+ public void deleteImageChooserItems(Set<Long> idsDataStore) {
+ if (idsDataStore != null && !idsDataStore.isEmpty()){
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable caught) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void onSuccess(Object result) {
+
+ updateImageChooser();
+ }
+
+ };
+
+ ConnexionServer.ressourceService.deleteFilesDataStore(idsDataStore, callback);
+
+
+ }
+ }
+
+ public void initDocument(){
+ // Clear editor text
+ ckee.setData("");
+
+ // Clear Image Chooser
+ MemoryProxy dataProxy = new MemoryProxy(new Object[][]{});
+ store.setDataProxy(dataProxy);
+ store.reload();
+
+ if (ressourceAbstract != null){
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable caught) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void onSuccess(Object result) {
+ String html = (String) result;
+
+
+ ckee.setData(html);
+ updateImageChooser();
+
+
+ }
+
+ };
+
+ RessourceFile rf = new RessourceFile();
+
+ rf.setRessourceKey(ressourceAbstract.getPath());
+ rf.setPluginKey(ressourceAbstract.getPluginKey());
+
+ ConnexionServer.ressourceService.getRessourceAsString(rf, UserTools.getUser().getUid(), callback);
+ }else{
+ updateImageChooser();
+ }
+ }
+
+ public static void exportPdfTest(){
+ final String path = Tools.encodePath(ressourceAbstract.getPath());
+
+ final String url = Tools.getRessourceAccessURL() + "?pluginKey="
+ + ressourceAbstract.getPluginKey() + "&ressourceKey="
+ + path + "&mode=pdf:NULL";
+
+ Tools.displayRessourceUrl(url, path, ressourceAbstract.getName(), "");
+
+ }
+
+
+
+ public static native void exportStaticMethod() /*-{
+ $wnd.exportPdfTest =
+ @com.pentila.evalcomp.client.widget.documenteditor.DocumentEditor::exportPdfTest();
+ }-*/;
+
+
+}