--- /dev/null
+package com.pentila.evalcomp.client.plan.widget;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import com.google.gwt.http.client.URL;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwt.user.client.ui.Hidden;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.gwtext.client.core.EventObject;
+import com.gwtext.client.core.SortDir;
+import com.gwtext.client.core.UrlParam;
+import com.gwtext.client.data.ArrayReader;
+import com.gwtext.client.data.FieldDef;
+import com.gwtext.client.data.MemoryProxy;
+import com.gwtext.client.data.Record;
+import com.gwtext.client.data.RecordDef;
+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.Toolbar;
+import com.gwtext.client.widgets.ToolbarButton;
+import com.gwtext.client.widgets.event.ButtonListenerAdapter;
+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.GridPanel;
+import com.gwtext.client.widgets.grid.Renderer;
+import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;
+import com.gwtext.client.widgets.layout.RowLayout;
+import com.gwtext.client.widgets.layout.RowLayoutData;
+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.TrashableContainer;
+import com.pentila.evalcomp.domain.transit.TDataStore;
+
+public class TemplateFileWidget extends Panel implements TrashableContainer{
+
+
+ private Long planRefId;
+ private Long sicId;
+ private boolean addExportButtons = false;
+
+ public TemplateFileWidget(Long planRefId, Long sicId, boolean addExportButtons) {
+ super();
+ this.planRefId = planRefId;
+ this.sicId = sicId;
+ this.addExportButtons = addExportButtons;
+ }
+
+
+ /** The grid. */
+ TrashableGridPanel grid;
+
+ /** The store. */
+ private Store store;
+
+ /** The cm. */
+ private ColumnModel cm;
+
+ /** The cfg columns role. */
+ private ColumnConfig[] cc;
+
+ /** The record def. */
+ RecordDef recordDef = new RecordDef(new FieldDef[] {new StringFieldDef("id"),
+ new StringFieldDef("filename"), });
+
+ /** The obj list. */
+ String[][] objList;
+
+
+
+ /** The render file. */
+ private Renderer renderFile = new Renderer() {
+ public String render(Object value, CellMetadata cellMetadata,
+ Record record, int rowIndex, int colNum, Store store) {
+ String icon = "<img src=\"" + Tools.getBaseURL()
+ + "js/ext2/resources/images/default/tree/leaf.gif\"> {0}";
+
+ return Format.format(icon, new String[] { record
+ .getAsString("filename"), });
+ }
+ };
+
+
+ /** The data proxy. */
+ private MemoryProxy dataProxy;
+
+
+ public void initializeInterface(){
+
+ setLayout(new RowLayout());
+
+ setBorder(false);
+ 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);
+
+
+ grid = new TrashableGridPanel(this);
+ if (addExportButtons){
+ initilizePreview();
+ }
+ grid.setTopToolbar(topToolbarRef);
+
+
+
+ cc = new ColumnConfig[] { new ColumnConfig(
+ Messages.getString("Fichier"), "filename", 169, true, renderFile, "filename") };
+ cm = new ColumnModel(cc);
+
+
+ objList = new String[][] {};
+ dataProxy = new MemoryProxy(objList);
+
+ ArrayReader reader = new ArrayReader(recordDef);
+
+ store = new Store(dataProxy, reader);
+
+ store.setSortInfo(new SortState("filename", SortDir.ASC));
+
+ store.load();
+
+ grid.setStore(store);
+ grid.setColumnModel(cm);
+
+
+ grid.setEnableDragDrop(true);
+ grid.setDdGroup("evalCompDDGroup");
+ grid.setDragDropText(Messages.getString("x_fichier_x"));
+
+ grid.getView().setAutoFill(true);
+ grid.getView().setForceFit(true);
+
+ grid.addGridRowListener(new GridRowListenerAdapter(){
+
+
+ public void onRowDblClick(GridPanel grid, int rowIndex,
+ EventObject e) {
+
+ Record r = grid.getStore().getAt(rowIndex);
+
+ String filename = r.getAsString("filename");
+
+
+ String code = Tools.base64encode(planRefId + ":" + "com.pentila.evalcomp.domain.plan.PlanRef" + ":" + URL.encodeQueryString(filename));
+ String url = Tools.getJSPURL() + "/getFile.jsp?objectId=" + code;
+
+
+ Tools.displayRessourceUrl(url,"",filename, "");
+
+ }
+
+
+
+ });
+
+
+ add(grid, new RowLayoutData("100%"));
+
+
+ }
+
+ public void update() {
+ 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){
+
+
+ objList[i++] = new Object[]{tds.getId(), tds.getName()};
+ }
+
+ MemoryProxy dataProxy = new MemoryProxy(objList);
+ store.setDataProxy(dataProxy);
+
+ store.reload();
+ }
+
+
+
+
+
+ }
+
+ };
+
+ ConnexionServer.ressourceService.getFilesDataStore(planRefId, "com.pentila.evalcomp.domain.plan.PlanRef", callback);
+
+ }
+
+
+ /**
+ * Upload template.
+ */
+ 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", "" + planRefId);
+ param[2] = new UrlParam("classname", "com.pentila.evalcomp.domain.plan.PlanRef" );
+
+ dialog.setBaseParams(param);
+
+ dialog.setPostVarName("file"); //$NON-NLS-1$
+
+ dialog.addListener(new UploadDialogListenerAdapter() {
+
+ @Override
+ public void onUploadComplete(UploadDialog source) {
+ update();
+ }
+ });
+
+ dialog.show();
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.client.utilities.TrashableContainer#isTrashable(java.lang.Object[])
+ */
+ public boolean isTrashable(Object ... objects) {
+ return true;
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.client.utilities.TrashableContainer#trashedElements(java.lang.Object[])
+ */
+ public void trashedElements(Object ...objects) {
+ AsyncCallback callback = new AsyncCallback(){
+
+ public void onFailure(Throwable caught) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void onSuccess(Object result) {
+ update();
+
+
+
+ }
+
+ };
+
+ Record[] records = grid.getSelectionModel().getSelections();
+ Set<Long> idsDataStore = new HashSet<Long>();
+
+ for (Record r : records){
+ idsDataStore.add(new Long(r.getAsString("id")));
+ }
+
+ ConnexionServer.ressourceService.deleteFilesDataStore(idsDataStore, callback);
+
+
+ }
+
+
+ public void initilizePreview(){
+ VerticalPanel vp = new VerticalPanel();
+ final com.google.gwt.user.client.ui.FormPanel fp = new com.google.gwt.user.client.ui.FormPanel("_target");
+ fp.setWidget(vp);
+ fp.setAction(Tools.getJSPURL() + "previewExport.jsp");
+ fp.setMethod(com.google.gwt.user.client.ui.FormPanel.METHOD_POST);
+
+
+ final Hidden planRefIdField = new Hidden("planrefid" , "" + this.planRefId);
+
+ String sicIdString = "";
+
+ if (this.sicId != null){
+ sicIdString = "" + this.sicId;
+ }
+
+ final Hidden sicIdField = new Hidden("sicid", sicIdString);
+ final Hidden cityNameField = new Hidden("cityname", "Ville");
+ final Hidden typeField = new Hidden("type", "certificate");
+ final Hidden generateFakeField = new Hidden("generateFake", "true");
+
+ vp.add(planRefIdField);
+ vp.add(sicIdField);
+ vp.add(cityNameField);
+ vp.add(typeField);
+ vp.add(generateFakeField);
+
+ ToolbarButton certButton = new ToolbarButton(Messages.getString("apercu_certificat"),
+ new ButtonListenerAdapter() {
+ public void onClick(Button button, EventObject e) {
+
+
+
+ typeField.setValue("certificate");
+ fp.submit();
+
+
+
+
+ }
+ });
+
+ ToolbarButton attButton = new ToolbarButton(Messages.getString("apercu_attestation"),
+ new ButtonListenerAdapter() {
+ public void onClick(Button button, EventObject e) {
+
+
+
+ typeField.setValue("attestation");
+ fp.submit();
+
+
+
+
+ }
+ });
+
+ add(fp);
+
+ Toolbar topToolbarRef = new Toolbar();
+ certButton.setIcon(Tools.getBaseURL() + "images/cc-acroread-16x16.png");
+ certButton.setTooltip(Messages.getString("apercu_certificat"));
+ topToolbarRef.addButton(certButton);
+
+ attButton.setIcon(Tools.getBaseURL() + "images/cc-acroread-16x16.png");
+ attButton.setTooltip(Messages.getString("apercu_attestation"));
+ topToolbarRef.addButton(attButton);
+
+
+ grid.setBottomToolbar(topToolbarRef);
+ }
+
+}
\ No newline at end of file