📄 env.java
字号:
{
retValue = ctx.getProperty("#"+context); // Login setting
if (retValue == null)
retValue = ctx.getProperty("$"+context); // Accounting setting
}
//
return (retValue == null ? "" : retValue);
} // getPreference
/****************************************************************************
* Language issues
*/
/** Context Language identifier */
static public final String LANG = "#AD_Language";
/**
* Check Base Language
* @param ctx context
* @param TableName table to be translated
* @return true if base language and table not translated
*/
public static boolean isBaseLanguage (Properties ctx, String TableName)
{
if (TableName.startsWith("AD") || TableName.equals("C_UOM"))
Language.isBaseLanguage (getAD_Language(ctx));
else // No AD Table
if (!isMultiLingualDocument(ctx))
return true; // access base table
return Language.isBaseLanguage (getAD_Language(ctx));
} // isBaseLanguage
/**
* Check Base Language
* @param AD_Language language
* @param TableName table to be translated
* @return true if base language and table not translated
*/
public static boolean isBaseLanguage (String AD_Language, String TableName)
{
if (TableName.startsWith("AD") || TableName.equals("C_UOM"))
Language.isBaseLanguage (AD_Language);
else // No AD Table
if (!isMultiLingualDocument(s_ctx)) // Base Context
return true; // access base table
return Language.isBaseLanguage (AD_Language);
} // isBaseLanguage
/**
* Check Base Language
* @param language language
* @param TableName table to be translated
* @return true if base language and table not translated
*/
public static boolean isBaseLanguage (Language language, String TableName)
{
if (TableName.startsWith("AD") || TableName.equals("C_UOM"))
language.isBaseLanguage();
else // No AD Table
if (!isMultiLingualDocument(s_ctx)) // Base Context
return true; // access base table
return language.isBaseLanguage();
} // isBaseLanguage
/**
* Do we have Multi-Lingual Documents.
* Set in DB.loadOrgs
* @param ctx context
* @return true if multi lingual documents
*/
public static boolean isMultiLingualDocument (Properties ctx)
{
return "Y".equals(Env.getContext(ctx, "#IsMultiLingualDocument"));
} // isMultiLingualDocument
/**
* Get AD_Language
* @param ctx context
* @return AD_Language eg. en_US
*/
public static String getAD_Language (Properties ctx)
{
if (ctx != null)
{
String lang = getContext(ctx, LANG);
if (lang != null || lang.length() > 0)
return lang;
}
return Language.getBaseAD_Language();
} // getAD_Language
/**
* Get AD_Language
* @param ctx context
* @return Language
*/
public static Language getLanguage (Properties ctx)
{
if (ctx != null)
{
String lang = getContext(ctx, LANG);
if (lang != null || lang.length() > 0)
return Language.getLanguage(lang);
}
return Language.getLanguage();
} // getLanguage
/**
* Verify Language.
* Check that language is supported by the system
* @param ctx might be updated with new AD_Language
* @param language language
*/
public static void verifyLanguage (Properties ctx, Language language)
{
ArrayList sysLang = new ArrayList();
String sql = "SELECT AD_Language FROM AD_Language ORDER BY IsBaseLanguage DESC";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
sysLang.add(rs.getString(1));
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("ALogin.verifyLanguage", e);
}
String selectedLanguage = language.getAD_Language();
for (int i = 0; i < sysLang.size(); i++)
{
if (selectedLanguage.equals(sysLang.get(i)))
return;
}
// Language/Country not found - try finding similar language (i.e. first e chars)
selectedLanguage = selectedLanguage.substring(0,2);
for (int i = 0; i < sysLang.size(); i++)
{
String comp = sysLang.get(i).toString().substring(0,2);
if (selectedLanguage.equals(comp))
{
language.setAD_Language(sysLang.get(i).toString());
Env.setContext(ctx, Env.LANG, language.getAD_Language());
return;
}
}
// none found - use Database Base Language
language.setAD_Language(sysLang.get(0).toString());
Env.setContext(ctx, Env.LANG, language.getAD_Language());
} // verifyLanguage
/*************************************************************************/
/**
* Get Context as String array with format: key == value
* @param ctx context
* @return context string
*/
public static String[] getEntireContext(Properties ctx)
{
if (ctx == null)
throw new IllegalArgumentException ("Env.getEntireContext - require Context");
Iterator keyIterator = ctx.keySet().iterator();
String[] sList = new String[ctx.size()];
int i = 0;
while (keyIterator.hasNext())
{
Object key = keyIterator.next();
sList[i++] = key.toString() + " == " + ctx.get(key).toString();
}
return sList;
} // getEntireContext
/**
* Get Header info (connection, org, user)
* @param ctx context
* @param WindowNo window
* @return Header String
*/
public static String getHeader(Properties ctx, int WindowNo)
{
StringBuffer sb = new StringBuffer();
if (WindowNo > 0)
sb.append(getContext(ctx, WindowNo, "WindowName", false)).append(" ");
sb.append(getContext(ctx, "#AD_User_Name")).append("@")
.append(getContext(ctx, "#AD_Client_Name")).append(".")
.append(getContext(ctx, "#AD_Org_Name"))
.append(" [").append(CConnection.get().toString()).append("]");
return sb.toString();
} // getHeader
/**
* Clean up context for Window (i.e. delete it)
* @param ctx context
* @param WindowNo window
*/
public static void clearWinContext(Properties ctx, int WindowNo)
{
if (ctx == null)
throw new IllegalArgumentException ("Env.clearWinContext - require Context");
//
Object[] keys = ctx.keySet().toArray();
for (int i = 0; i < keys.length; i++)
{
String tag = keys[i].toString();
if (tag.startsWith(WindowNo+"|"))
ctx.remove(keys[i]);
}
// Clear Lookup Cache
MLookupCache.cacheReset(WindowNo);
// MLocator.cacheReset(WindowNo);
//
removeWindow(WindowNo);
} // clearWinContext
/**
* Clean up all context (i.e. delete it)
* @param ctx context
*/
public static void clearContext(Properties ctx)
{
if (ctx == null)
throw new IllegalArgumentException ("Env.clearContext - require Context");
ctx.clear();
} // clearContext
/**
* Parse Context replaces global or Window context @tag@ with actual value.
*
* @param ctx context
* @param WindowNo Number of Window
* @param value Message to be parsed
* @param onlyWindow if true, no defaults are used
* @param ignoreUnparsable if true, unsuccessful @tag@ are ignored otherwise "" is returned
* @return parsed String or "" if not successful and ignoreUnparsable
*/
public static String parseContext (Properties ctx, int WindowNo, String value,
boolean onlyWindow, boolean ignoreUnparsable)
{
if (value == null)
return "";
String token;
String inStr = new String(value);
StringBuffer outStr = new StringBuffer();
int i = inStr.indexOf("@");
while (i != -1)
{
outStr.append(inStr.substring(0, i)); // up to @
inStr = inStr.substring(i+1, inStr.length()); // from first @
int j = inStr.indexOf("@"); // next @
if (j < 0)
{
Log.error("Env.parseContext - no second tag: " + inStr);
return ""; // no second tag
}
token = inStr.substring(0, j);
String ctxInfo = getContext(ctx, WindowNo, token, onlyWindow); // get context
if (ctxInfo.length() == 0 && (token.startsWith("#") || token.startsWith("$")) )
ctxInfo = getContext(ctx, token); // get global context
if (ctxInfo.length() == 0)
{
Log.trace(Log.l5_DData, "Env.parseContext - no context (" + WindowNo + ") for: " + token);
if (!ignoreUnparsable)
return ""; // token not found
}
else
outStr.append(ctxInfo); // replace context with Context
inStr = inStr.substring(j+1, inStr.length()); // from second @
i = inStr.indexOf("@");
}
outStr.append(inStr); // add the rest of the string
return outStr.toString();
} // parseContext
/**
* Parse Context replaces global or Window context @tag@ with actual value.
*
* @param ctx context
* @param WindowNo Number of Window
* @param value Message to be parsed
* @param onlyWindow if true, no defaults are used
* @return parsed String or "" if not successful
*/
public static String parseContext (Properties ctx, int WindowNo, String value,
boolean onlyWindow)
{
return parseContext(ctx, WindowNo, value, onlyWindow, false);
} // parseContext
/*************************************************************************/
private static ArrayList s_windows = new ArrayList(20);
/**
* Add Container and return WindowNo.
* The container is a APanel, AWindow or JFrame/JDialog
* @param win window
* @return WindowNo used for context
*/
public static int createWindowNo(Container win)
{
int retValue = s_windows.size();
s_windows.add(win);
return retValue;
} // createWindowNo
/**
* Search Window by comparing the Frames
* @param container container
* @return WindowNo of container or 0
*/
public static int getWindowNo (Container container)
{
if (container == null)
return 0;
JFrame winFrame = getFrame(container);
if (winFrame == null)
return 0;
// loop through windows
for (int i = 0; i < s_windows.size(); i++)
{
Container cmp = (Container)s_windows.get(i);
if (cmp != null)
{
JFrame cmpFrame = getFrame(cmp);
if (winFrame.equals(cmpFrame))
return i;
}
}
return 0;
} // getWindowNo
/**
* Return the JFrame pointer of WindowNo - or null
* @param WindowNo window
* @return JFrame of WindowNo
*/
public static JFrame getWindow (int WindowNo)
{
JFrame retValue = null;
try
{
retValue = getFrame ((Container)s_windows.get(WindowNo));
}
catch (Exception e)
{
System.err.println("Env.getWindow - " + e);
}
return retValue;
} // getWindow
/**
* Remove window from active list
* @param WindowNo window
*/
private static void removeWindow (int WindowNo)
{
if (WindowNo <= s_windows.size())
s_windows.set(WindowNo, null);
} // removeWindow
/**
* Clean up context for Window (i.e. delete it)
* @param WindowNo window
*/
public static void clearWinContext(int WindowNo)
{
clearWinContext (s_ctx, WindowNo);
} // clearWinContext
/**
* Clean up all context (i.e. delete it)
*/
public static void clearContext()
{
s_ctx.clear();
} // clearContext
/*************************************************************************/
/**
* Get Frame of Window
* @param container Container
* @return JFrame of container or null
*/
public static JFrame getFrame (Container container)
{
Container element = container;
while (element != null)
{
if (element instanceof JFrame)
return (JFrame)element;
element = element.getParent();
}
return null;
} // getFrame
/**
* Get Graphics of container or its parent.
* The element may not have a Graphic if not displayed yet,
* but the parent might have.
* @param container Container
* @return Graphics of container or null
*/
public static Graphics getGraphics (Container container)
{
Container element = container;
while (element != null)
{
Graphics g = element.getGraphics();
if (g != null)
return g;
element = element.getParent();
}
return null;
} // getFrame
/**
* Return JDialog or JFrame Parent
* @param container Container
* @return JDialog or JFrame of container
*/
public static Window getParent (Container container)
{
Container element = container;
while (element != null)
{
if (element instanceof JDialog || element instanceof JFrame)
return (Window)element;
element = element.getParent();
}
return null;
} // getParent
/*************************************************************************/
/**
* Get Image with File name
*
* @param fileNameInImageDir full file name in imgaes folder (e.g. Bean16.gif)
* @return image
*/
public static Image getImage (String fileNameInImageDir)
{
URL url = Compiere.class.getResource("images/" + fileNameInImageDir);
if (url == null)
return null;
Toolkit tk = Toolkit.getDefaultToolkit();
return tk.getImage(url);
} // getImage
/**
* Get ImageIcon
*
* @param fileNameInImageDir full file name in imgaes folder (e.g. Bean16.gif)
* @return image
*/
public static ImageIcon getImageIcon (String fileNameInImageDir)
{
URL url = Compiere.class.getResource("images/" + fileNameInImageDir);
if (url == null)
return null;
return new ImageIcon(url);
} // getImageIcon
/**************************************************************************/
/**
* Start Browser
* @param url url
*/
public static void startBrowser (String url)
{
Log.trace(Log.l1_User, "Env.startBrowser", url);
// OS command
String cmd = "explorer ";
if (!System.getProperty("os.name").startsWith("Win"))
cmd = "netscape ";
//
String execute = cmd + url;
try
{
Runtime.getRuntime().exec(execute);
}
catch (Exception e)
{
System.err.println("Env.startBrowser - " + execute + " - " + e);
}
} // startBrowser
/**************************************************************************
* Static Variables
*/
/**
* Big Decimal Zero
*/
static final public java.math.BigDecimal ZERO = new java.math.BigDecimal(0.0);
/**
* New Line
*/
public static final String NL = System.getProperty("line.separator");
/**
* Static initializer
*/
static
{
// Set English as default Language
s_ctx.put(LANG, Language.getBaseAD_Language());
} // static
} // Env
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -