📄 jremserverutils.java
字号:
/* * Copyright (C) 2007 Filippo Di Vattimo - See LICENSE * */package fildiv.jremcntl.server.utils;import java.io.File;import java.io.FileFilter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.util.Collection;import java.util.Enumeration;import java.util.Iterator;import java.util.Map;import javax.bluetooth.RemoteDevice;import javax.swing.ComboBoxModel;import javax.swing.JComboBox;import fildiv.jremcntl.common.core.Command;import fildiv.jremcntl.common.core.Context;import fildiv.jremcntl.common.core.JRemRuntimeException;import fildiv.jremcntl.common.util.JRemUtils;import fildiv.jremcntl.server.core.Environment;import fildiv.jremcntl.server.core.JRemEnv;import fildiv.jremcntl.server.core.JRemPropertyDomainValue;import fildiv.jremcntl.server.core.JRemPropertyManager;import fildiv.jremcntl.server.core.JRemSystemVariable;import fildiv.jremcntl.server.gui.core.JRemUIManager;public class JRemServerUtils { private JRemServerUtils() { } public static class ExtFileFilter implements FileFilter { private String ext; public ExtFileFilter(String ext) { this.ext = ext; } public boolean accept(File pathname) { if (!pathname.isFile()) return false; String filePath = pathname.getAbsolutePath().toLowerCase(); return filePath.endsWith(ext); } }; public static String getRemoteDeviceDesc(RemoteDevice rd) { String desc = ""; try { desc = rd.getFriendlyName(false) + " (" + rd.getFriendlyName(false) + ")"; } catch (Exception e) { throw new JRemRuntimeException(e); } return desc; } public static String getFilePath(JRemEnv env, String basePath, String fileName) { String filePath = ""; String firstQuote = ""; if (fileName.startsWith("\"")) { firstQuote = "\""; fileName = fileName.substring(fileName.indexOf("\"") + 1); } if ("".equals(JRemUtils.safeString(basePath))) filePath = fileName; else if (basePath.endsWith("/") || basePath.endsWith("\\")) filePath = basePath + fileName; else filePath = basePath + File.separator + fileName; filePath = firstQuote + env.expand(filePath); return JRemServerUtils.convertToSystemPath(filePath); } public static String expand(String expr, String macroName, String macroValue) { String re = "\\$\\{[ ]*" + macroName + "[ ]*\\}"; return expr.replaceAll(re, macroValue); } public static String getCmdFullExePath(JRemEnv env, Command cmd) { String basePath = getBasePath(cmd); String exePath = JRemServerUtils.getFilePath(env, basePath, cmd.getExePath()); return exePath; } public static String getBasePath(Command cmd) { Context ctx = cmd.getContext(); if (ctx == null) return ""; String basePath = ctx.getBaseDir(); if ("".equals(basePath)) basePath = ctx.getConfig().getBaseDir(); else if (".".equals(basePath.trim())) basePath = ""; return basePath; } public static void wait(int millis) { Object out = new Object(); synchronized(out) { try { out.wait(millis); } catch (InterruptedException e) { } } } public static String stripPathSeparator(String filePath) { if (JRemUtils.safeString(filePath).equals("")) return ""; if (filePath.endsWith(File.separator)) return stripPathSeparator( filePath.substring(0, filePath.length() -1)); return filePath; } public static String inputStream2String(InputStream in) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1;) { out.append(new String(b, 0, n)); } return out.toString(); } public static boolean existsFile(String filePath) { File f = new File(filePath); return f.exists(); } public static String[] sysVar2EnvPairArr(Map systemVariables) { String[] envs = new String[systemVariables.size() + System.getProperties().size()]; Collection values = systemVariables.values(); int index = 0; for (Iterator i = values.iterator(); i.hasNext(); ) { JRemSystemVariable sv = (JRemSystemVariable) i.next(); envs[index++] = sv.toString(); } Enumeration e = System.getProperties().keys(); while (e.hasMoreElements()) { String key = e.nextElement().toString(); envs[index++] = key + "=" + System.getProperty(key); } return envs; } public static void setSelectDomainCombo(JComboBox cb, String domainName, JRemPropertyManager pm) { Object value = pm.getPropertyValue(domainName); setSelectDomainComboFromValue(cb, value); } public static void setSelectDomainComboFromValue(JComboBox cb, Object value) { ComboBoxModel cbm = cb.getModel(); for (int index = 0; index < cbm.getSize(); ++index) { JRemPropertyDomainValue domainValue = (JRemPropertyDomainValue) cbm.getElementAt(index); if (domainValue.getKey().equals(value)) { cb.setSelectedIndex(index); return; } } } public static void addComboItemsFromDomain(JComboBox cb, String domainName, JRemPropertyManager pm) { Collection domainValues = pm.getPropertyDomainValues(domainName); if (domainValues == null) return; addComboItemsFromDomainValues(cb, domainValues); } public static void addComboItemsFromDomainValues(JComboBox cb, Collection domainValues) { for (Iterator i = domainValues.iterator(); i.hasNext(); ) { JRemPropertyDomainValue pdv = (JRemPropertyDomainValue) i.next(); cb.addItem(new JRemPropertyDomainValue( pdv.getKey(), pdv.getValue())); } } public static String getExceptionStack(Exception e) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw); e.printStackTrace(pw); return sw.toString(); } finally { try { if (pw != null) pw.close(); if (sw != null) sw.close(); } catch (IOException ignore) { } } } public static boolean isWindows() { return System.getProperty("os.name").toLowerCase().startsWith("windows"); } public static String convertToSystemPath(String filePath) { String[] ss = filePath.split("/"); StringBuffer s = new StringBuffer(); for (int index = 0; index < ss.length; ++index) { s.append(ss[index]).append(File.separator); } String toSystemPath = s.toString(); if (toSystemPath.endsWith(File.separator)) toSystemPath = toSystemPath.substring(0, toSystemPath.length() - File.separator.length()); return toSystemPath; } public static String getCommandDescForLog(JRemEnv env, Command c) { return c.getDesc() + " [" + getCmdFullExePath(env, c) + "]"; } public static boolean isShowServerOutput(JRemEnv env, JRemUIManager uiMng) { if (!env.getAppConfiguration().isShowServerOutput()) return false; if (!uiMng.isGUIVisible()) return !env.getAppConfiguration().isSuppressServerOutputWhenGUINotVisible(); return true; } public static boolean isShowServerStdOutput(JRemEnv env, JRemUIManager uiMng) { if (!env.getAppConfiguration().isShowServerStdOutput()) return false; if (!uiMng.isGUIVisible()) return !env.getAppConfiguration().isSuppressServerOutputWhenGUINotVisible(); return true; } public static boolean isShowServerStdError(JRemEnv env, JRemUIManager uiMng) { if (!env.getAppConfiguration().isShowServerStdError()) return false; if (!uiMng.isGUIVisible()) return !env.getAppConfiguration().isSuppressServerOutputWhenGUINotVisible(); return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -