* Implemented editor for DisplayType.FileName

This commit is contained in:
Heng Sin Low 2008-07-13 04:38:39 +00:00
parent b8f61827db
commit d16a1a74e7
5 changed files with 264 additions and 22 deletions

View File

@ -0,0 +1,89 @@
/******************************************************************************
* Product: Posterita Ajax UI *
* Copyright (C) 2007 Posterita Ltd. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* Posterita Ltd., 3, Draper Avenue, Quatre Bornes, Mauritius *
* or via info@posterita.org or http://www.posterita.org/ *
*****************************************************************************/
package org.adempiere.webui.component;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Hbox;
/**
* URL Box
*/
public class FilenameBox extends Hbox
{
private static final long serialVersionUID = 1L;
private Textbox textbox;
private Button button;
public FilenameBox()
{
initComponents();
}
public FilenameBox(String url)
{
initComponents();
setText(url);
}
public void setButtonImage(String imageSrc)
{
button.setImage(imageSrc);
}
private void initComponents()
{
textbox = new Textbox();
textbox.setWidth("100%");
button = new Button();
button.setHeight("98%");
appendChild(textbox);
appendChild(button);
}
public void setText(String value)
{
textbox.setText(value);
}
public String getText()
{
return textbox.getText();
}
public void setEnabled(boolean enabled)
{
textbox.setReadonly(!enabled);
button.setEnabled(enabled);
}
public void setButtonEnabled(boolean enabled)
{
button.setEnabled(enabled);
}
public boolean addEventListener(String evtnm, EventListener listener)
{
if ("onClick".equals(evtnm))
return button.addEventListener(evtnm, listener);
else
return textbox.addEventListener(evtnm, listener);
}
}

View File

@ -0,0 +1,145 @@
/******************************************************************************
* Copyright (C) 2008 Low Heng Sin All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* Posterita Ltd., 3, Draper Avenue, Quatre Bornes, Mauritius *
* or via info@posterita.org or http://www.posterita.org/ *
*****************************************************************************/
package org.adempiere.webui.editor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import org.adempiere.webui.component.FilenameBox;
import org.adempiere.webui.event.ValueChangeEvent;
import org.compiere.model.GridField;
import org.compiere.util.CLogger;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Fileupload;
/**
*
* @author Low Heng Sin
*
*/
public class WFilenameEditor extends WEditor
{
private static final String[] LISTENER_EVENTS = {Events.ON_CLICK, Events.ON_CHANGE};
private static final CLogger log = CLogger.getCLogger(WFilenameEditor.class);
public WFilenameEditor(GridField gridField)
{
super(new FilenameBox(), gridField);
getComponent().setButtonImage("/images/Open16.gif");
getComponent().addEventListener(Events.ON_CLICK, this);
}
@Override
public FilenameBox getComponent()
{
return (FilenameBox) component;
}
@Override
public void setValue(Object value)
{
if (value == null)
{
getComponent().setText("");
}
else
{
getComponent().setText(String.valueOf(value));
}
}
@Override
public Object getValue()
{
return getComponent().getText();
}
@Override
public String getDisplay()
{
return getComponent().getText();
}
public void onEvent(Event event)
{
if (Events.ON_CHANGE.equals(event.getName()))
{
ValueChangeEvent changeEvent = new ValueChangeEvent(this, this.getColumnName(), getComponent().getText(), getComponent().getText());
fireValueChange(changeEvent);
}
else if (Events.ON_CLICK.equals(event.getName()))
{
cmd_file();
}
}
/**
* Load file
*/
private void cmd_file()
{
// Show File Open Dialog
Media file = null;
try
{
file = Fileupload.get();
if (file == null)
return;
}
catch (InterruptedException e)
{
log.warning(e.getLocalizedMessage());
return;
}
String fileName = System.getProperty("java.io.tmpdir")
+ System.getProperty("file.separator") + file.getName();
File tempFile = new File(fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(tempFile);
fos.write(file.getByteData());
fos.flush();
fos.close();
} catch (IOException e) {
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
return;
} finally {
if (fos != null)
try {
fos.close();
} catch (IOException e) {}
}
getComponent().setText(fileName);
} // cmd_file
public String[] getEvents()
{
return LISTENER_EVENTS;
}
}

View File

@ -38,8 +38,6 @@ public class WStringEditor extends WEditor implements ContextMenuListener
{
private static final String[] LISTENER_EVENTS = {Events.ON_CHANGE};
protected Textbox textbox;
private String oldText;
private WEditorPopupMenu popupMenu;
@ -48,35 +46,39 @@ public class WStringEditor extends WEditor implements ContextMenuListener
public WStringEditor(GridField gridField)
{
super(new Textbox(), gridField);
textbox = (Textbox)super.component;
init();
}
@Override
public Textbox getComponent() {
return (Textbox) component;
}
private void init()
{
textbox.setMaxlength(gridField.getFieldLength());
getComponent().setMaxlength(gridField.getFieldLength());
int displayLength = gridField.getDisplayLength();
if (displayLength <= 0 || displayLength > MAX_DISPLAY_LENGTH)
{
displayLength = MAX_DISPLAY_LENGTH;
}
textbox.setCols(displayLength);
getComponent().setCols(displayLength);
if (gridField.getDisplayType() == DisplayType.Text)
{
textbox.setMultiline(true);
textbox.setRows(3);
getComponent().setMultiline(true);
getComponent().setRows(3);
}
else if (gridField.getDisplayType() == DisplayType.TextLong)
{
textbox.setMultiline(true);
textbox.setRows(5);
getComponent().setMultiline(true);
getComponent().setRows(5);
}
else if (gridField.getDisplayType() == DisplayType.Memo)
{
textbox.setMultiline(true);
textbox.setRows(8);
getComponent().setMultiline(true);
getComponent().setRows(8);
}
popupMenu = new WEditorPopupMenu(false, false, true);
@ -84,7 +86,7 @@ public class WStringEditor extends WEditor implements ContextMenuListener
public void onEvent(Event event)
{
String newText = textbox.getValue();
String newText = getComponent().getValue();
ValueChangeEvent changeEvent = new ValueChangeEvent(this, this.getColumnName(), oldText, newText);
super.fireValueChange(changeEvent);
oldText = newText;
@ -93,13 +95,13 @@ public class WStringEditor extends WEditor implements ContextMenuListener
@Override
public String getDisplay()
{
return textbox.getValue();
return getComponent().getValue();
}
@Override
public Object getValue()
{
return textbox.getValue();
return getComponent().getValue();
}
@Override
@ -107,24 +109,24 @@ public class WStringEditor extends WEditor implements ContextMenuListener
{
if (value != null)
{
textbox.setValue(value.toString());
getComponent().setValue(value.toString());
}
else
{
textbox.setValue("");
getComponent().setValue("");
}
oldText = textbox.getValue();
oldText = getComponent().getValue();
}
protected void setTypePassword(boolean password)
{
if (password)
{
textbox.setType("password");
getComponent().setType("password");
}
else
{
textbox.setType("text");
getComponent().setType("text");
}
}

View File

@ -35,6 +35,6 @@ public class WUnknownEditor extends WStringEditor
private void init()
{
super.textbox.setEnabled(false);
getComponent().setReadonly(true);
}
}

View File

@ -58,8 +58,9 @@ public class WebEditorFactory
/** String (clear/password) */
if (displayType == DisplayType.String
|| displayType == DisplayType.PrinterName
|| (tableEditor && (displayType == DisplayType.Text || displayType == DisplayType.TextLong)) )
|| displayType == DisplayType.PrinterName
|| (tableEditor && (displayType == DisplayType.Text || displayType == DisplayType.TextLong))
|| displayType == DisplayType.FilePath)
{
if (gridField.isEncryptedField())
{
@ -70,6 +71,11 @@ public class WebEditorFactory
editor = new WStringEditor(gridField);
}
}
/** File */
else if (displayType == DisplayType.FileName)
{
editor = new WFilenameEditor(gridField);
}
/** Number */
else if (DisplayType.isNumeric(displayType))
{