package xplanetconfigurator.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import xplanetconfigurator.util.OwnPreferences;
/**
* @author @author@
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class JFrameHelp extends JFrame
{
private static String FRAME_X = "dialog_help_x";
private static String FRAME_Y = "dialog_help_y";
private static String FRAME_HEIGHT = "dialog_h";
private static String FRAME_WITH = "dialog_w";
private static JFrameHelp instance;
private static boolean wasCreated;
private JButton jButtonOK;
private JScrollPane jScrollPaneMain;
private JEditorPane jEditorPane;
private String url;
private JFrameHelp(String url)
{
this.url = url;
this.initGui();
this.loadUserPrefs();
this.jScrollPaneMain.scrollRectToVisible(new Rectangle(1, 1, 10, 10));
}
private void initGui()
{
this.setSize(new Dimension(500, 300));
this.addWindowListener(new UtilWindowClosingAdapter());
this.getRootPane().setDefaultButton(this.jButtonOK);
this.jButtonOK = new JButton("OK");
jButtonOK.setMnemonic('O');
this.jEditorPane = new JEditorPane();
this.jEditorPane.setContentType("text/html");
this.loadText();
this.jEditorPane.setCaretPosition(0);
this.jEditorPane.setEditable(false);
this.jScrollPaneMain = new JScrollPane(this.jEditorPane);
GridBagLayout gridbagMainPanel = new GridBagLayout();
this.getContentPane().setLayout(gridbagMainPanel);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(0, 0, 0, 0); // int top, int left, int bottom, int right
gridbagMainPanel.setConstraints(this.jScrollPaneMain, c);
this.getContentPane().add(jScrollPaneMain);
c = new GridBagConstraints();
c.anchor = GridBagConstraints.SOUTHEAST;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(11, 12, 12, 12); // int top, int left, int bottom, int right
gridbagMainPanel.setConstraints(this.jButtonOK, c);
this.getContentPane().add(jButtonOK);
this.jButtonOK.addActionListener(
new ActionListenerOK());
// this.setLocationRelativeTo(null); //center it
// this.setVisible(true);
}
private void loadText(String url)
{
this.url = url;
this.loadText();
}
private void loadText()
{
StringBuffer buf = new StringBuffer();
BufferedReader in = null;
try {
in =
new BufferedReader(
new InputStreamReader(this.getClass().getResourceAsStream(url)));
String s = null;
while((s = in.readLine()) != null)
{
buf.append(s).append("\n");
}
} catch(Exception e) {
} finally {
if(in != null)
{
try {
in.close();
} catch(Exception eio) {
}
}
}
this.jEditorPane.setText(buf.toString());
}
private void storeUserPrefs() {
int x = this.getX();
int y = this.getY();
Dimension d = this.getSize();
int h = (int) d.getHeight();
int w = (int) d.getWidth();
OwnPreferences prefs =
OwnPreferences.userNodeForPackage(this.getClass());
prefs.putInt(FRAME_X, x);
prefs.putInt(FRAME_Y, y);
prefs.putInt(FRAME_HEIGHT, h);
prefs.putInt(FRAME_WITH, w);
}
private void loadUserPrefs() {
OwnPreferences prefs =
OwnPreferences.userNodeForPackage(this.getClass());
int x = prefs.getInt(FRAME_X, 10);
int y = prefs.getInt(FRAME_Y, 10);
int h = prefs.getInt(FRAME_HEIGHT, 600);
int w = prefs.getInt(FRAME_WITH, 500);
this.setSize(new Dimension(w, h));
// System.out.println("Start info = " + info);
if(x == 10 && y == 10) // First use
{
// System.out.println("Start info in if() = " + info);
this.setLocationRelativeTo(null); //center it
}
else
{
this.setLocation(x, y);
}
}
private void closeMySelf() {
this.storeUserPrefs();
this.setVisible(false);
this.dispose();
}
public static JFrameHelp getInstance(String title, String url)
{
if(JFrameHelp.instance == null && ! JFrameHelp.wasCreated)
{
// The creation takes time and allows the user to open
// more than one windows
JFrameHelp.wasCreated = true;
JFrameHelp.instance = new JFrameHelp(url);
}
else
{
JFrameHelp.instance.loadText(url);
}
JFrameHelp.instance.setTitle(title);
return JFrameHelp.instance;
}
class UtilWindowClosingAdapter extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent e) {
closeMySelf();
}
}
class ActionListenerOK implements ActionListener {
public void actionPerformed(ActionEvent e) {
closeMySelf();
}
}
}