📄 jext.java
字号:
* Exits Jext by closing all the windows. If backgrounding, it doesn't kill the jext server. * This is synchronized because creating two windows at a time can be problematic. */ public static void exit() { synchronized (instances) { Object[] o = instances.toArray(); for (int i = o.length - 1; i >= 0; i--) { JextFrame instance = ((JextFrame) o[i]); /*if (i == 0) { instance.fireJextEvent(JextEvent.KILLING_JEXT); if (!isRunningBg()) stopPlugins(); } else instance.fireJextEvent(JextEvent.CLOSE_WINDOW);*/ closeToQuit(instance); } /*if (isRunningBg()) { builtTextArea = newWindow(null, false); //instances.add(builtTextArea); } else finalCleanupBeforeExit();*/ } } /** * Do the final cleanup that must be done at the real end of the session(when closing the server if * running background server, or after killing the last window if no server is running). */ /* friendly*/ static void finalCleanupAndExit() { //currently it does almost nothing, but it's used. System.exit(0); } /** * Stop plugins. */ /* friendly */ static void stopPlugins() { Plugin[] plugins = getPlugins(); for (int i = 0; i < plugins.length; i++) try { plugins[i].stop(); } catch (Throwable t) { System.err.println("#--An exception has occurred while stopping plugin:"); t.printStackTrace(); } } /** * Close a {@link JextFrame} by calling {@link JextFrame#closeToQuit()} and if it it the last window and we are not keeping the server open * we close Jext completely. */ public static void closeToQuit(JextFrame frame) { closeToQuit(frame,false); } //For JextLoader when it's killing Jext /*friendly*/ static void closeToQuit(JextFrame frame, boolean isKillingServer) { if (isKillingServer) runInBg = false;//so when calling closeWindow(frame), which will happen, it will close completely Jext //and stop plugins. frame.closeToQuit(); } public static void closeWindow(JextFrame frame) { synchronized (instances) { if (getWindowsCount() == 1/* && !isRunningBg()*/) frame.fireJextEvent(JextEvent.KILLING_JEXT); else frame.fireJextEvent(JextEvent.CLOSING_WINDOW); frame.closeWindow(); if (getWindowsCount() == 0) { if (!isRunningBg()) stopServer(); Search.save(); if (!isRunningBg()) stopPlugins(); frame.saveConsole(); GUIUtilities.saveGeometry(frame, "jext"); saveXMLProps("Jext v" + Jext.RELEASE + " b" + Jext.BUILD); //frame.cleanMemory(); frame = null; System.gc();//this way, the garbage collector should do its work, without any NullPointerEx at all. if (isRunningBg()) builtTextArea = newWindow(null, false); else System.exit(0); } } } /** * Returns splash screen */ public static SplashScreen getSplashScreen() { return splash; } /** * Set the splash screen progress value after * having checked if it isn't null (in case of a new window). * @param val The new value */ public static void setSplashProgress(int val) { if (splash != null) splash.setProgress(val); } /** * Set the splash screen text value after having checked if it * isn't null (in case of a new window). * @param text The new text */ public static void setSplashText(String text) { if (splash != null) splash.setText(text); } /** * Kills splash screen */ public static void killSplashScreen() { if (splash != null) { splash.dispose(); splash = null; } } /** * Stops the Jext server which loads every Jext instance in the same JVM. */ public static void stopServer() { if (jextLoader != null) { jextLoader.stop(); jextLoader = null; } } /** * Returns true if the server is enabled. */ public static boolean isServerEnabled() { return isServerEnabled; } /** * Returns true if the backgrounding is enabled. */ public static boolean isDefaultKeepInMemory() { return defaultKeepInMemory; } public static void setDefaultKeepInMemory(boolean val) { defaultKeepInMemory = val; if (val) //TODO:hack to active it from this session onwards { } } /** * Used by security options panel to remember of the server status. * @param on If true, the server will be runned at next start */ public static void setServerEnabled(boolean on) { isServerEnabled = on; } /** * Attempts to load Jext in a single JVM instance only. If this instance of * Jext is the very first to be loaded, then a ServerSocket is opened. Otherwise, * this instance attemps to connect on to a specific socket port to tell other * Jext instance to create a new window. This avoids to load on JVM for each * launch of Jext. Opened port is securised so that no security hole is created. * @param args The arguments of the new Jext window */ public static void loadInSingleJVMInstance(String[] args) { try { File security = new File(SETTINGS_DIRECTORY + ".security"); if (!security.exists()) isServerEnabled = true; else { BufferedReader reader = new BufferedReader(new FileReader(security)); isServerEnabled = new Boolean(reader.readLine()).booleanValue(); reader.close(); } } catch (IOException ioe) { } if (!isServerEnabled && !runInBg) return; File authorizationKey = new File(SETTINGS_DIRECTORY + ".auth-key"); // if the authorization key exists, another Jext instance may // be running if (authorizationKey.exists()) { // we attempt to log onto the other instance of Jext(but only if we are not backgrounding; no // more than one bg instance is started, and if we are bg we don't pass anything to the other instance. try { BufferedReader reader = new BufferedReader(new FileReader(authorizationKey)); int port = Integer.parseInt(reader.readLine()); String key = reader.readLine(); reader.close(); Socket client = new Socket("127.0.0.1", JEXT_SERVER_PORT + port); if (!runInBg) { //now that we made sure that the other instance exists, if backgrounding we do //nothing PrintWriter writer = new PrintWriter(client.getOutputStream()); StringBuffer _args = new StringBuffer(); if (goingToKill) { _args.append("kill"); } else { _args.append("load_jext:"); for (int i = 0; i < args.length; i++) { _args.append(args[i]); if (i != args.length - 1) _args.append('?'); } } _args.append(':').append(key); writer.write(_args.toString()); writer.flush(); writer.close(); } else System.out.println("Jext is already running, either in background or foreground."); client.close(); System.exit(5); } catch (Exception e) { // no other jext instance is running, we delete the auth. file authorizationKey.delete(); if (goingToKill) { System.err.println("No jext instance found!"); System.exit(0); } else jextLoader = new JextLoader(); } } else if (!goingToKill) { jextLoader = new JextLoader(); } else { System.err.println("No jext instance found!"); System.exit(0); } } /** * As Jext can be runned in background mode, some operations may need to know wether * or not current instance is "up" or "crouched". This is the purpose of this method. * @return A true boolean value is returned whenever Jext is running in background mode */ public static boolean isRunningBg() { return runInBg; } // check the command line arguments private static String[] parseOptions(String [] args) { // Trap bg flag int argLn = args.length; ArrayList newArgs = new ArrayList(argLn); //First, it checks defaults: if the user actived -showbg by default, read this setting. try { File showbg = new File(SETTINGS_DIRECTORY + ".showBg"); if (!showbg.exists()) keepInMemory = false; else { BufferedReader reader = new BufferedReader(new FileReader(showbg)); keepInMemory = new Boolean(reader.readLine()).booleanValue(); reader.close(); } } catch (IOException ioe) { } defaultKeepInMemory = runInBg = keepInMemory; //Then, let's read options. for (int i = 0; i < argLn; i++) { //Whenever it encounter an option it resets all contrary ones. if ("-bg".equals(args[i])) { runInBg = true; keepInMemory = false; goingToKill = false; } else if ("-kill".equals(args[i])) { goingToKill = true; keepInMemory = false; runInBg = false; } else if ("-showbg".equals(args[i])) { runInBg = true; keepInMemory = true; goingToKill = false; } //This option is unrelated. else if ("-debug".equals(args[i])) DEBUG = true; else newArgs.add(args[i]); } return (String[]) newArgs.toArray(new String[0]); } ////////////////////////////////////////////////////////////////////////////////////////////// // END OF STATIC PART ////////////////////////////////////////////////////////////////////////////////////////////// // MAIN ENTRY POINT ////////////////////////////////////////////////////////////////////////////////////////////// /** * Start method. Load the property file, set the look and feel, create a new GUI, * load the options. If a file name is specified as first argument, we pass it * to the window contructor which will construct its full path (because you can * specify, for example, ..\..\test.java or ~\jext.props or ...../hello.cpp - * both / and \ can be used -) */ public static void main(String args[]) { ///////////////////////////////// DEBUG System.setErr(System.out); initDirectories(); args = parseOptions(args); synchronized (instances) { loadInSingleJVMInstance(args); initProperties(); if (!isRunningBg()) { splash = new SplashScreen(); newWindow(args); } else { if (keepInMemory) splash = new SplashScreen(); //FIXME:maybe it should ignore arguments when backgrounding. builtTextArea = newWindow(args, false); if (keepInMemory) newWindow(null, true); } } if (getBooleanProperty("check")) check = new VersionCheck(); }}// End of Jext.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -