diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/component/FolderBrowser.java b/zkwebui/WEB-INF/src/org/adempiere/webui/component/FolderBrowser.java new file mode 100644 index 0000000000..ec98a9c2de --- /dev/null +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/component/FolderBrowser.java @@ -0,0 +1,179 @@ +/****************************************************************************** + * Copyright (C) 2008 Low Heng Sin * + * 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. * + *****************************************************************************/ +package org.adempiere.webui.component; + +import java.io.File; + +import org.adempiere.webui.apps.AEnv; +import org.adempiere.webui.window.FDialog; +import org.compiere.util.Ini; +import org.compiere.util.ValueNamePair; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zk.ui.event.Events; +import org.zkoss.zkex.zul.Borderlayout; +import org.zkoss.zkex.zul.Center; +import org.zkoss.zkex.zul.North; +import org.zkoss.zkex.zul.South; + +/** + * Directory and File Browser + * @author Elaine + * + */ +public class FolderBrowser extends Window implements EventListener +{ + private Textbox txtPath = new Textbox(); + private Listbox listDir = new Listbox(); + private ConfirmPanel confirmPanel = new ConfirmPanel(true); + + private final File root = new File(Ini.getAdempiereHome()); + + private boolean showDirOnly = false; + private String path; + + public FolderBrowser() + { + this(false); + } + + public FolderBrowser(boolean showDirOnly) + { + this.showDirOnly = showDirOnly; + + setTitle(showDirOnly ? "Directory Browser" : "File Browser"); + setWidth("500px"); + setHeight("500px"); + setBorder("normal"); + + Borderlayout contentLayout = new Borderlayout(); + appendChild(contentLayout); + + North north = new North(); + contentLayout.appendChild(north); + north.appendChild(txtPath); + + Center center = new Center(); + center.setFlex(true); + contentLayout.appendChild(center); + center.appendChild(listDir); + + South south = new South(); + south.setStyle("border: none"); + contentLayout.appendChild(south); + south.appendChild(confirmPanel); + + txtPath.setWidth("475px"); + txtPath.setReadonly(true); + + getFileListing(root.getPath()); + listDir.setMultiple(false); + listDir.addDoubleClickListener(this); + + confirmPanel.addActionListener(this); + + AEnv.showWindow(this); + } + + private void getFileListing(String dirPath) + { + File dir = new File(dirPath); + if(!dir.exists()) + return; + + if(dir.isDirectory()) + { + listDir.removeAllItems(); + + if(!dir.getParent().equals(root.getParent())) + { + ListItem li = new ListItem(dir.getName(), dir.getParent()); + li.setImage("images/Undo16.png"); + listDir.appendChild(li); + } + + File[] files = dir.listFiles(); + + for(File file: files) + { + if(file.isDirectory()) + { + ListItem li = new ListItem(file.getName(), file.getAbsolutePath()); + li.setImage("images/Folder16.png"); + listDir.appendChild(li); + } + } + + if(!showDirOnly) + { + for(File file: files) + { + if(file.isFile()) + { + listDir.addItem(new ValueNamePair(file.getAbsolutePath(), file.getName())); + } + } + } + } + + txtPath.setValue(dir.getAbsolutePath()); + } + + public void onEvent(Event e) throws Exception + { + if(e.getName() == Events.ON_DOUBLE_CLICK && e.getTarget() instanceof ListItem) + { + int index = listDir.getSelectedIndex(); + ValueNamePair vnp = listDir.getItemAtIndex(index).toValueNamePair(); + getFileListing(vnp.getValue()); + } + if(e.getTarget() == confirmPanel.getButton(ConfirmPanel.A_OK)) + { + path = txtPath.getValue(); + if(path != null) + { + File file = new File(path); + + if(showDirOnly) + { + if(!file.isDirectory() || !file.exists()) + { + FDialog.error(0, "Invalid directory"); + return; + } + } + else + { + if(!file.isFile() || !file.exists()) + { + FDialog.error(0, "Invalid file"); + return; + } + } + + path = file.getAbsolutePath(); + } + dispose(); + } + else if(e.getTarget() == confirmPanel.getButton(ConfirmPanel.A_CANCEL)) + { + path = null; + dispose(); + } + } + + public String getPath() + { + return path; + } +} diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WFileDirectoryEditor.java b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WFileDirectoryEditor.java new file mode 100644 index 0000000000..ff6f9d796e --- /dev/null +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WFileDirectoryEditor.java @@ -0,0 +1,122 @@ +/****************************************************************************** + * 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. * + *****************************************************************************/ + +package org.adempiere.webui.editor; + + +import org.adempiere.webui.component.FilenameBox; +import org.adempiere.webui.component.FolderBrowser; +import org.adempiere.webui.event.ValueChangeEvent; +import org.compiere.model.GridField; +import org.compiere.util.CLogger; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.Events; + +/** + * + * @author Low Heng Sin + * + */ +public class WFileDirectoryEditor extends WEditor +{ + private static final String[] LISTENER_EVENTS = {Events.ON_CLICK, Events.ON_CHANGE}; + + private static final CLogger log = CLogger.getCLogger(WFileDirectoryEditor.class); + + private String oldValue; + + public WFileDirectoryEditor(GridField gridField) + { + super(new FilenameBox(), gridField); + getComponent().setButtonImage("/images/Open16.png"); + getComponent().addEventListener(Events.ON_CLICK, this); + } + + @Override + public FilenameBox getComponent() + { + return (FilenameBox) component; + } + + @Override + public void setValue(Object value) + { + if (value == null) + { + oldValue = null; + getComponent().setText(""); + } + else + { + oldValue = String.valueOf(value); + getComponent().setText(oldValue); + } + } + + @Override + public Object getValue() + { + return getComponent().getText(); + } + + @Override + public String getDisplay() + { + return getComponent().getText(); + } + + @Override + public boolean isReadWrite() { + return getComponent().isEnabled(); + } + + @Override + public void setReadWrite(boolean readWrite) { + getComponent().setEnabled(readWrite); + } + + public void onEvent(Event event) + { + if (Events.ON_CHANGE.equals(event.getName())) + { + String newValue = getComponent().getText(); + if (oldValue != null && newValue != null && oldValue.equals(newValue)) { + return; + } + if (oldValue == null && newValue == null) { + return; + } + ValueChangeEvent changeEvent = new ValueChangeEvent(this, this.getColumnName(), oldValue, newValue); + fireValueChange(changeEvent); + } + else if (Events.ON_CLICK.equals(event.getName())) + { + cmd_file(); + } + } + + /** + * Load file + */ + private void cmd_file() + { + FolderBrowser directoryDialog = new FolderBrowser(true); + String directory = directoryDialog.getPath(); + getComponent().setText(directory); + } // cmd_file + + public String[] getEvents() + { + return LISTENER_EVENTS; + } +} diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java index 30ea499186..cee0603dfd 100644 --- a/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/editor/WebEditorFactory.java @@ -66,8 +66,7 @@ public class WebEditorFactory /** String (clear/password) */ if (displayType == DisplayType.String || displayType == DisplayType.PrinterName - || (tableEditor && (displayType == DisplayType.Text || displayType == DisplayType.TextLong)) - || displayType == DisplayType.FilePath) + || (tableEditor && (displayType == DisplayType.Text || displayType == DisplayType.TextLong))) { if (gridField.isEncryptedField()) { @@ -83,6 +82,11 @@ public class WebEditorFactory { editor = new WFilenameEditor(gridField); } + /** File Path */ + else if (displayType == DisplayType.FilePath) + { + editor = new WFileDirectoryEditor(gridField); + } /** Number */ else if (DisplayType.isNumeric(displayType)) {