Refactor custom form - ID: 2787613 for Translation Import/Export

This commit is contained in:
Heng Sin Low 2009-10-13 09:16:16 +00:00
parent 1fead30f6c
commit e2168bccef
6 changed files with 417 additions and 91 deletions

View File

@ -261,10 +261,12 @@ public class Translation
catch (SQLException e)
{
log.log(Level.SEVERE, sql.toString(), e);
return e.toString();
}
catch (Exception e)
{
log.log(Level.SEVERE, "", e);
return e.toString();
}
return "";

View File

@ -0,0 +1,134 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* 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.compiere.install;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import org.compiere.apps.IStatusBar;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.KeyNamePair;
import org.compiere.util.ValueNamePair;
public class TranslationController
{
public TranslationController()
{
}
/** Logger */
public static CLogger log = CLogger.getCLogger(TranslationController.class);
/** Window No */
public int m_WindowNo = 0;
public ArrayList<KeyNamePair> getClientList()
{
ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
list.add(new KeyNamePair (-1, ""));
String sql = "SELECT Name, AD_Client_ID "
+ "FROM AD_Client "
+ "WHERE IsActive='Y' "
+ "ORDER BY AD_Client_ID";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
KeyNamePair kp = new KeyNamePair (rs.getInt(2), rs.getString(1));
list.add(kp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
return list;
}
public ArrayList<ValueNamePair> getLanguageList()
{
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
// Fill Language
String sql = "SELECT Name, AD_Language "
+ "FROM AD_Language "
+ "WHERE IsActive='Y' AND (IsSystemLanguage='Y' OR IsBaseLanguage='Y')"
+ "ORDER BY Name";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ValueNamePair vp = new ValueNamePair (rs.getString(2), rs.getString(1));
list.add(vp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
return list;
}
public ArrayList<ValueNamePair> getTableList()
{
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
// Fill Table
list.add(new ValueNamePair ("", ""));
String sql = "SELECT Name, TableName "
+ "FROM AD_Table "
+ "WHERE TableName LIKE '%_Trl' AND TableName<>'AD_Column_Trl' "
+ "ORDER BY Name";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ValueNamePair vp = new ValueNamePair (rs.getString(2), rs.getString(1));
list.add(vp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
return list;
}
public void setStatusBar(IStatusBar statusBar)
{
// Info
statusBar.setStatusLine(" ");
statusBar.setStatusDB(" ");
} // dynInit
}

View File

@ -24,9 +24,7 @@ import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.swing.JButton;
@ -40,7 +38,6 @@ import org.compiere.apps.form.FormFrame;
import org.compiere.apps.form.FormPanel;
import org.compiere.swing.CPanel;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Ini;
import org.compiere.util.KeyNamePair;
@ -53,7 +50,7 @@ import org.compiere.util.ValueNamePair;
* @author Jorg Janke
* @version $Id: TranslationDialog.java,v 1.3 2006/07/30 00:51:28 jjanke Exp $
*/
public class TranslationDialog extends CPanel
public class VTranslationDialog extends TranslationController
implements FormPanel, ActionListener
{
/**
@ -61,21 +58,21 @@ public class TranslationDialog extends CPanel
*/
private static final long serialVersionUID = -5072470836657762574L;
private CPanel panel = new CPanel();
/**
* TranslationDialog Constructor.
* (Initiated via init())
*/
public TranslationDialog()
public VTranslationDialog()
{
} // TranslationDialog
/** Window No */
private int m_WindowNo = 0;
/** FormFrame */
private FormFrame m_frame;
/** Logger */
private static CLogger log = CLogger.getCLogger(TranslationDialog.class);
private static CLogger log = CLogger.getCLogger(VTranslationDialog.class);
//
private GridBagLayout mainLayout = new GridBagLayout();
private JComboBox cbLanguage = new JComboBox();
@ -96,7 +93,7 @@ public class TranslationDialog extends CPanel
*/
private void jbInit() throws Exception
{
this.setLayout(mainLayout);
panel.setLayout(mainLayout);
lClient.setText(Msg.translate(Env.getCtx(), "AD_Client_ID"));
lLanguage.setText(Msg.translate(Env.getCtx(), "AD_Language"));
lLanguage.setToolTipText(Msg.translate(Env.getCtx(), "IsSystemLanguage"));
@ -107,21 +104,21 @@ public class TranslationDialog extends CPanel
bImport.setText(Msg.getMsg(Env.getCtx(), "Import"));
bImport.addActionListener(this);
//
this.add(cbLanguage, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
panel.add(cbLanguage, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
this.add(lLanguage, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
panel.add(lLanguage, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(lTable, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
panel.add(lTable, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(cbTable, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
panel.add(cbTable, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
this.add(bExport, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
panel.add(bExport, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(bImport, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
panel.add(bImport, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(lClient, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
panel.add(lClient, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(cbClient, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
panel.add(cbClient, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
} // jbInit
@ -132,76 +129,22 @@ public class TranslationDialog extends CPanel
private void dynInit()
{
// Fill Client
cbClient.addItem(new KeyNamePair (-1, ""));
String sql = "SELECT Name, AD_Client_ID "
+ "FROM AD_Client "
+ "WHERE IsActive='Y' "
+ "ORDER BY AD_Client_ID";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
KeyNamePair kp = new KeyNamePair (rs.getInt(2), rs.getString(1));
cbClient.addItem(kp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
ArrayList<KeyNamePair> clients = getClientList();
for(KeyNamePair client: clients)
cbClient.addItem(client);
// Fill Language
sql = "SELECT Name, AD_Language "
+ "FROM AD_Language "
+ "WHERE IsActive='Y' AND (IsSystemLanguage='Y' OR IsBaseLanguage='Y')"
+ "ORDER BY Name";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ValueNamePair vp = new ValueNamePair (rs.getString(2), rs.getString(1));
cbLanguage.addItem(vp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
ArrayList<ValueNamePair> languages = getLanguageList();
for(ValueNamePair language: languages)
cbLanguage.addItem(language);
// Fill Table
cbTable.addItem(new ValueNamePair ("", ""));
sql = "SELECT Name, TableName "
+ "FROM AD_Table "
+ "WHERE TableName LIKE '%_Trl' AND TableName<>'AD_Column_Trl' "
+ "ORDER BY Name";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ValueNamePair vp = new ValueNamePair (rs.getString(2), rs.getString(1));
cbTable.addItem(vp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
ArrayList<ValueNamePair> tables = getTableList();
for(ValueNamePair table: tables)
cbTable.addItem(table);
// Info
statusBar.setStatusLine(" ");
statusBar.setStatusDB(" ");
setStatusBar(statusBar);
} // dynInit
/**
@ -219,7 +162,7 @@ public class TranslationDialog extends CPanel
{
jbInit();
dynInit();
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
}
catch(Exception ex)
@ -261,33 +204,37 @@ public class TranslationDialog extends CPanel
JFileChooser chooser = new JFileChooser(startDir);
chooser.setMultiSelectionEnabled(false);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = imp ? chooser.showOpenDialog(this) : chooser.showSaveDialog(this);
int returnVal = imp ? chooser.showOpenDialog(panel) : chooser.showSaveDialog(panel);
if (returnVal != JFileChooser.APPROVE_OPTION)
return;
String directory = chooser.getSelectedFile().getAbsolutePath();
//
statusBar.setStatusLine(directory);
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
panel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Translation t = new Translation(Env.getCtx());
String msg = t.validateLanguage(AD_Language.getValue());
if (msg.length() > 0)
{
ADialog.error(m_WindowNo, this, "LanguageSetupError", msg);
ADialog.error(m_WindowNo, panel, "LanguageSetupError", msg);
return;
}
// All Tables
if (AD_Table.getValue().equals(""))
{
msg = null;
for (int i = 1; i < cbTable.getItemCount(); i++)
{
AD_Table = (ValueNamePair)cbTable.getItemAt(i);
msg = null;
msg = imp
? t.importTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue())
: t.exportTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue());
statusBar.setStatusLine(msg);
}
if(msg == null || msg.length() == 0)
msg = (imp ? "Import" : "Export") + " Successful. [" + directory + "]";
statusBar.setStatusLine(directory);
}
else // single table
@ -296,10 +243,14 @@ public class TranslationDialog extends CPanel
msg = imp
? t.importTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue())
: t.exportTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue());
if(msg == null || msg.length() == 0)
msg = (imp ? "Import" : "Export") + " Successful. [" + directory + "]";
statusBar.setStatusLine(msg);
}
//
this.setCursor(Cursor.getDefaultCursor());
panel.setCursor(Cursor.getDefaultCursor());
} // actionPerformed
} // Translation

View File

@ -0,0 +1 @@
UPDATE AD_Form SET ClassName = 'org.compiere.install.VTranslationDialog' WHERE AD_Form_ID = 109;

View File

@ -0,0 +1 @@
UPDATE AD_Form SET ClassName = 'org.compiere.install.VTranslationDialog' WHERE AD_Form_ID = 109;

View File

@ -0,0 +1,237 @@
package org.adempiere.webui.install;
import java.util.ArrayList;
import java.util.logging.Level;
import org.adempiere.webui.LayoutUtils;
import org.adempiere.webui.component.Button;
import org.adempiere.webui.component.FolderBrowser;
import org.adempiere.webui.component.Grid;
import org.adempiere.webui.component.GridFactory;
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.Listbox;
import org.adempiere.webui.component.ListboxFactory;
import org.adempiere.webui.component.Panel;
import org.adempiere.webui.component.Row;
import org.adempiere.webui.component.Rows;
import org.adempiere.webui.panel.ADForm;
import org.adempiere.webui.panel.CustomForm;
import org.adempiere.webui.panel.IFormController;
import org.adempiere.webui.panel.StatusBarPanel;
import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.window.FDialog;
import org.compiere.install.Translation;
import org.compiere.install.TranslationController;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
import org.compiere.util.ValueNamePair;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkex.zul.Borderlayout;
import org.zkoss.zkex.zul.Center;
import org.zkoss.zkex.zul.South;
import org.zkoss.zul.Div;
public class WTranslationDialog extends TranslationController implements IFormController, EventListener {
private CustomForm form = new CustomForm();
public WTranslationDialog()
{
m_WindowNo = form.getWindowNo();
Env.setContext(Env.getCtx(), form.getWindowNo(), "IsSOTrx", "Y");
try
{
zkInit();
dynInit();
Borderlayout contentLayout = new Borderlayout();
contentLayout.setWidth("100%");
contentLayout.setHeight("100%");
form.appendChild(contentLayout);
Center center = new Center();
contentLayout.appendChild(center);
center.appendChild(centerPanel);
South south = new South();
south.setStyle("border: none");
contentLayout.appendChild(south);
south.appendChild(statusBar);
LayoutUtils.addSclass("status-border", statusBar);
south.setHeight("22px");
}
catch(Exception e)
{
log.log(Level.SEVERE, "", e);
}
}
private Panel centerPanel = new Panel();
private Grid centerLayout = GridFactory.newGridLayout();
private Button bExport = new Button();
private Button bImport = new Button();
private Label lClient = new Label();
private Listbox cbClient = ListboxFactory.newDropdownListbox();
private Label lLanguage = new Label();
private Listbox cbLanguage = ListboxFactory.newDropdownListbox();
private Label lTable = new Label();
private Listbox cbTable = ListboxFactory.newDropdownListbox();
private StatusBarPanel statusBar = new StatusBarPanel();
private void zkInit() throws Exception
{
centerPanel.appendChild(centerLayout);
lClient.setText(Msg.translate(Env.getCtx(), "AD_Client_ID"));
lLanguage.setText(Msg.translate(Env.getCtx(), "AD_Language"));
lLanguage.setTooltiptext(Msg.translate(Env.getCtx(), "IsSystemLanguage"));
lTable.setText(Msg.translate(Env.getCtx(), "AD_Table_ID"));
//
bExport.setLabel(Msg.getMsg(Env.getCtx(), "Export"));
bExport.addActionListener(this);
bImport.setLabel(Msg.getMsg(Env.getCtx(), "Import"));
bImport.addActionListener(this);
Rows rows = centerLayout.newRows();
Row row = rows.newRow();
row.appendChild(lClient.rightAlign());
row.appendChild(cbClient);
row = rows.newRow();
row.appendChild(lLanguage.rightAlign());
row.appendChild(cbLanguage);
row = rows.newRow();
row.appendChild(lTable.rightAlign());
row.appendChild(cbTable);
Div div = new Div();
div.setAlign("right");
div.appendChild(bExport);
row = rows.newRow();
row.appendChild(div);
row.appendChild(bImport);
}
/**
* Dynamic Init.
* - fill Language & Table
*/
private void dynInit()
{
// Fill Client
ArrayList<KeyNamePair> clients = getClientList();
for(KeyNamePair client: clients)
cbClient.addItem(client);
// Fill Language
ArrayList<ValueNamePair> languages = getLanguageList();
for(ValueNamePair language: languages)
cbLanguage.addItem(language);
// Fill Table
ArrayList<ValueNamePair> tables = getTableList();
for(ValueNamePair table: tables)
cbTable.addItem(table);
// Info
setStatusBar(statusBar);
} // dynInit
/**
* Dispose
*/
public void dispose()
{
SessionManager.getAppDesktop().closeActiveWindow();
} // dispose
/**************************************************************************
* Action Listener
* @param e event
*/
public void onEvent(Event e)
{
if (cbLanguage.getSelectedIndex() == -1)
{
statusBar.setStatusLine(Msg.getMsg(Env.getCtx(), "LanguageSetupError"), true);
return;
}
ValueNamePair AD_Language = (ValueNamePair)cbLanguage.getSelectedItem().toValueNamePair();
if (AD_Language == null)
{
statusBar.setStatusLine(Msg.getMsg(Env.getCtx(), "LanguageSetupError"), true);
return;
}
if (cbTable.getSelectedIndex() == -1)
return;
ValueNamePair AD_Table = (ValueNamePair)cbTable.getSelectedItem().toValueNamePair();
if (AD_Table == null)
return;
boolean imp = (e.getTarget() == bImport);
int AD_Client_ID = -1;
KeyNamePair AD_Client = null;
if (cbTable.getSelectedIndex() != -1)
AD_Client = (KeyNamePair)cbClient.getSelectedItem().toKeyNamePair();
if (AD_Client != null)
AD_Client_ID = AD_Client.getKey();
FolderBrowser directoryDialog = new FolderBrowser(true);
String directory = directoryDialog.getPath();
if(directory == null) return;
//
statusBar.setStatusLine(directory);
Translation t = new Translation(Env.getCtx());
String msg = t.validateLanguage(AD_Language.getValue());
if (msg.length() > 0)
{
FDialog.error(m_WindowNo, form, "LanguageSetupError", msg);
return;
}
// All Tables
if (AD_Table.getValue().equals(""))
{
msg = null;
for (int i = 1; i < cbTable.getItemCount(); i++)
{
AD_Table = (ValueNamePair)cbTable.getItemAtIndex(i).toValueNamePair();
msg += imp
? t.importTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue())
: t.exportTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue());
}
if(msg == null || msg.length() == 0)
msg = (imp ? "Import" : "Export") + " Successful. [" + directory + "]";
statusBar.setStatusLine(msg);
}
else // single table
{
msg = null;
msg = imp
? t.importTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue())
: t.exportTrl (directory, AD_Client_ID, AD_Language.getValue(), AD_Table.getValue());
if(msg == null || msg.length() == 0)
msg = (imp ? "Import" : "Export") + " Successful. [" + directory + "]";
statusBar.setStatusLine(msg);
}
} // actionPerformed
public ADForm getForm() {
return form;
}
}