/* * Copyright 2004-2007 IDCA. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and * the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions * and the following disclaimer in the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the authors and * should not be interpreted as representing official policies, either expressed or implied, of IDCA. */ package com.idcanet.vasc.frontends.swt; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; import com.idcanet.fff.SwingImageHelper; import com.idcanet.vasc.core.AbstractVascFrontend; import com.idcanet.vasc.core.VascEntry; import com.idcanet.vasc.core.VascEntryField; import com.idcanet.vasc.core.VascFrontendData; import com.idcanet.vasc.core.actions.GlobalVascAction; import com.idcanet.vasc.core.actions.RowVascAction; import com.idcanet.vasc.core.entry.VascEntryExporter; import com.idcanet.vasc.core.ui.VascColumnValueModelListener; import com.idcanet.vasc.core.ui.VascUIComponent; import com.idcanet.vasc.core.ui.VascValueModel; import com.idcanet.vasc.frontends.swt.ui.SwtBoolean; import com.idcanet.vasc.frontends.swt.ui.SwtButton; import com.idcanet.vasc.frontends.swt.ui.SwtLabel; import com.idcanet.vasc.frontends.swt.ui.SwtText; import com.idcanet.vasc.frontends.swt.ui.SwtTextArea; /** * * @author Willem Cazander * @version 1.0 Mar 21, 2007 */ public class SwtVascFrontend extends AbstractVascFrontend { private Logger logger = null; private Composite parent = null; public SwtVascFrontend(Composite parent) { logger = Logger.getLogger(SwtVascFrontend.class.getName()); this.parent=parent; } /** * Add swt implmented ui components */ protected void addUiComponents() { VascFrontendData vfd = getVascEntry().getVascFrontendData(); // required UI components vfd.putVascUIComponent(VascUIComponent.VASC_LABEL,SwtLabel.class.getName()); vfd.putVascUIComponent(VascUIComponent.VASC_TEXT, SwtText.class.getName()); vfd.putVascUIComponent(VascUIComponent.VASC_BUTTON, SwtButton.class.getName()); // optional UI components vfd.putVascUIComponent(VascUIComponent.VASC_BOOLEAN , SwtBoolean.class.getName()); //vfd.putVascUIComponent(VascUIComponent.VASC_DATE , SwtDate.class.getName()); vfd.putVascUIComponent(VascUIComponent.VASC_TEXTAREA, SwtTextArea.class.getName()); //vfd.putVascUIComponent(VascUIComponent.VASC_COLOR, SwtColorChooser.class.getName()); } private static final String[] FILTER_NAMES = { "All Files (*.*)", "Comma Separated Values Files (*.csv)", "Microsoft Excel Spreadsheet Files (*.xls)", }; /** These filter extensions are used to filter which files are displayed. */ private static final String[] FILTER_EXTS = { "*.*","*.csv","*.xls" }; public void renderExport(VascEntryExporter exporter) throws Exception { FileDialog dlg = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); dlg.setFilterNames(FILTER_NAMES); dlg.setFilterExtensions(FILTER_EXTS); dlg.setFileName(entry.getHeaderName()+".csv"); String fileName = dlg.open(); logger.fine("FileName: "+fileName); if (fileName == null) { return; } OutputStream out = new FileOutputStream(fileName); try { exporter.doExport(out, entry); } catch (Exception e) { //MessageDialog.openError(Display.getCurrent().getActiveShell(),crudTable.i18n("crud.event.export.error.title"),crudTable.i18n("crud.event.export.error.message")); logger.log(Level.WARNING,"Error: "+e.getMessage(),e); } finally { if (out!=null) { out.close(); } } } public void renderEdit(Object object) throws Exception { logger.fine("Rending Edit View"); object = entry.getVascFrontendData().getVascFrontendHelper().initEditObject(entry, object); SwtVascEditDialog dialog = new SwtVascEditDialog(Display.getCurrent().getActiveShell(),object,"Vasc Edit","Edit"); Object result = dialog.open(); if(result==null) { return; } /* try { result = entry.getVascDataSource().merge(object); //table.getVascTableController().fireModelUpdateListeners(result); } finally { //TODO: or merge into table == faster entry.getVascTableController().refreshData(entry); } */ } public ImageDescriptor getImageDescriptor(String path) { try { path = i18n(path); logger.fine("Loading image: "+path); ImageDescriptor result = ImageDescriptor.createFromFile(path.getClass(), path); result = ImageDescriptor.createFromFile(SwingImageHelper.class, path); if(result==null) { // try load fff //result = ImageDescriptor.createFromURL(SwingImageHelper.class.getClass().getResource(path)); } if(result==null) { throw new NullPointerException("Can't load resource: "+path); } return result; } catch (Exception e) { logger.warning("Could not load image from path: '"+path+"'"); try { ImageDescriptor result = null; //ImageDescriptor.createFromURL(SwingImageHelper.class.getClass().getResource("/META-INF/images/silk/png/bomb.png")); if(result==null) { throw new NullPointerException("Can't load resource: "+path); } return result; } catch (Exception e2) { return ImageDescriptor.getMissingImageDescriptor(); // default swt missing image fall back } } } public void renderDelete(Object rowBean) throws Exception { } public class SwtVascEditDialog extends Dialog { private Shell shell = null; //private String headerText = null; private String title = null; private Object result = null; private Object bean = null; public SwtVascEditDialog (Shell parent,Object bean,String title,String headerText) { super (parent, 0); ///this.headerText = headerText; this.title = title; this.bean = bean; } public Object open() { shell = new Shell(getParent(), SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL); shell.setText(title); GridLayout layout = new GridLayout(); layout.marginHeight = 0; layout.marginWidth = 0; shell.setLayout(layout); Composite header = new Composite(shell, SWT.NONE); GridLayout headerLayout = new GridLayout(); headerLayout.numColumns = 6; header.setLayout(headerLayout); header.setLayoutData(new GridData(GridData.FILL_BOTH)); Composite body = new Composite(shell, SWT.NONE); GridLayout bodyLayout = new GridLayout(); bodyLayout.numColumns = 1; body.setLayout(bodyLayout); body.setLayoutData(new GridData(GridData.FILL_BOTH)); Composite footer = new Composite(shell, SWT.NONE); GridLayout footerLayout = new GridLayout(); footerLayout.numColumns = 6; footer.setLayout(footerLayout); footer.setLayoutData(new GridData(SWT.NONE)); //createHeader(header); try { createBody(body); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //createFooter(footer); // should be last partCreated(); shell.pack(); shell.open(); Display display = shell.getDisplay(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } //image.dispose(); //closeDialog(); return result; } public void createBody(Composite body) throws Exception { body.setLayout(new GridLayout(2, true)); body.setLayoutData(new GridData(GridData.FILL_BOTH)); for (VascEntryField c:entry.getVascEntryFields()) { entry.getVascFrontendData().getVascFrontendHelper().initEditObjectColumn(c, bean); if (c.getEdit()==false) { continue; } //if (c.isEditReadOnly()==true) { for (int i=0;i