📄 client.java
字号:
/* * Copyright (c) 2000, Niklas Mehner * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.j3de;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RMISecurityManager;
import java.rmi.server.RemoteServer;
import javax.swing.JWindow;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.OverlayLayout;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.j3de.exception.ExceptionHandler;
import org.j3de.exception.LogExceptionHandler;
import org.j3de.interfaces.Environment;
import org.j3de.security.SecurityFactory;
import org.j3de.security.SystemSecurity;
import org.j3de.security.UserInfo;
import org.j3de.ui.Gui3d;
import org.j3de.util.Configurable;
import org.j3de.util.Configuration;
import org.j3de.util.ConfigurationException;
import org.j3de.util.EnvironmentHelper;
import org.j3de.util.InitializationException;
/**
* Client is the main class of the j3de client. It is responsible for parsing the configuration file
* and creating all necessairy components, as well as setting up exception-handling and logging.
* * @author Niklas Mehner
* @version $Revision: 1.5 $, $Date: 2000/09/06 21:42:50 $
* @since j3de 1.0
*/
public class Client extends JPanel {
private Configuration configuration;
private String configFileName;
private Gui3d gui3d;
private ServerComponent server;
private Environment startupEnvironment;
/**
* Instantiates a new client using the information from the given file. First logging and exception handling is set up.
* After this the Gui3D is instantiated. If a server component is contained in the configuration it is also instantiated.
* When all is ready, the startup environment is loaded.
* @param configFileName The configfile, the information about the client is read from.
* @throws IOException if the logfile cannot be opened.
* @throws SAXException if the configuration cannot be parsed.
* @throws ParserConfigurationException if the xml parser is misconfigured.
* @throws ConfigurationException if the configuration file contains errors (unknown classes, * components cannot be instantiated etc.)
*/
public Client(String configFileName) throws IOException,
ConfigurationException,
ParserConfigurationException,
SAXException {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
RemoteServer.setLog(new FileOutputStream("log" + File.separator + "rmiclient.log"));
System.setErr(new PrintStream(new FileOutputStream("log" + File.separator + "error.log")));
this.configFileName = configFileName;
new LogExceptionHandler("log" + File.separator + "log.txt");
try {
configuration = new Configuration(configFileName);
} catch (IOException e) {
int result = JOptionPane.showConfirmDialog(null, "Create a new ?",
"Configuration File Not Found", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
configuration = new Configuration();
else
throw e;
}
gui3d = (Gui3d)configuration.getComponent("gui3d", org.j3de.ui.impl.DefaultGui3d.class);
server = (ServerComponent)configuration.getComponent("local_server", (Class)null);
String startupEnvironmentUrl = (String)configuration.getProperty("startup_environment",
"//localhost:1099/j3de-envserver#LoginEnvironment",
true);
startupEnvironment = EnvironmentHelper.getEnvironment(startupEnvironmentUrl);
this.setLayout(new BorderLayout());
this.add(BorderLayout.CENTER, gui3d.getComponent());
}
/**
* Starts the Client. The startup environment is inserted in the gui.
* @throws InitializationException if the startup environment cannot be initialized.
*/
public void run() throws InitializationException {
gui3d.startModalEnvironment(startupEnvironment);
}
/** Saves the configuration-file of the client after asking the user to confirm this.
* @param client The client the information is to be saved for.
* @param fileName The filename the configuration should be written to.
*/
public static void saveConfiguration(Client client, String fileName) {
if ((client != null) && client.configuration.isConfigurationChanged()) {
int result = JOptionPane.showConfirmDialog(null, "Save changes ?",
"Configuration Changed", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
try {
client.configuration.saveConfiguration(fileName);
} catch (IOException e2) {
ExceptionHandler.handleFatalException(e2);
}
}
}
}
/** Starts the client application.
* @param args The array should only contain the configuration-file name.
*/
public static void main(String args[]) {
java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
Client client = null;
String configFileName = "config.xml";
try {
if (args.length == 1)
configFileName = args[0];
client = new Client(configFileName);
JWindow window = new JWindow();
window.getContentPane().add( client);
window.pack();
window.validate();
window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
window.show();
window.addWindowListener(new SaveAdapter(client, configFileName));
client.run();
} catch (Exception e) {
e.printStackTrace();
ExceptionHandler.handleFatalException(e);
saveConfiguration(client, configFileName);
System.exit(-1);
} finally {
saveConfiguration(client, configFileName);
}
}
}
class SaveAdapter extends WindowAdapter {
private Client client;
private String fileName;
public SaveAdapter(Client client, String fileName) {
this.client = client;
this.fileName = fileName;
}
public void windowClosed(WindowEvent e) {
Client.saveConfiguration(client, fileName);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -