123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757 |
- /*
- * Copyright (C) 2004-2014 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.tools.configurator;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.List;
- import java.util.Locale;
- import java.util.NoSuchElementException;
- import java.util.ResourceBundle;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTabbedPane;
- import javax.swing.JTextArea;
- import javax.swing.JToolBar;
- import javax.swing.SwingConstants;
- import javax.swing.SwingUtilities;
- import javax.swing.ToolTipManager;
- import javax.swing.UIManager;
- import javolution.util.FastList;
- import com.l2jserver.tools.configurator.ConfigUserInterface.ConfigFile.ConfigComment;
- import com.l2jserver.tools.configurator.ConfigUserInterface.ConfigFile.ConfigProperty;
- import com.l2jserver.tools.i18n.LanguageControl;
- import com.l2jserver.tools.images.ImagesTable;
- /**
- * @author KenM
- */
- public class ConfigUserInterface extends JFrame implements ActionListener
- {
- private static final long serialVersionUID = 2609592249095305857L;
-
- public static final String EOL = System.getProperty("line.separator");
-
- private final JTabbedPane _tabPane = new JTabbedPane();
-
- private List<ConfigFile> _configs = new FastList<>();
-
- private ResourceBundle _bundle;
-
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- try
- {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch (Exception e)
- {
- // couldn't care less
- }
-
- final ResourceBundle bundle = ResourceBundle.getBundle("configurator.Configurator", Locale.getDefault(), LanguageControl.INSTANCE);
-
- SwingUtilities.invokeLater(() ->
- {
- ConfigUserInterface cui = new ConfigUserInterface(bundle);
- cui.setVisible(true);
- });
- }
-
- public ConfigUserInterface(ResourceBundle bundle)
- {
- setBundle(bundle);
- setTitle(bundle.getString("toolName"));
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(750, 500);
- setLayout(new GridBagLayout());
-
- setDefaultLookAndFeelDecorated(true);
- setIconImage(ImagesTable.getImage("l2j.png").getImage());
-
- GridBagConstraints cons = new GridBagConstraints();
- cons.fill = GridBagConstraints.HORIZONTAL;
- cons.gridx = 0;
- cons.gridy = 0;
- cons.weighty = 0;
- cons.weightx = 1;
-
- JMenuBar menubar = new JMenuBar();
-
- JMenu fileMenu = new JMenu(bundle.getString("fileMenu"));
- JMenu helpMenu = new JMenu(bundle.getString("helpMenu"));
-
- JMenuItem exitItem = new JMenuItem(bundle.getString("exitItem"));
- exitItem.setActionCommand("exit");
- exitItem.addActionListener(this);
- fileMenu.add(exitItem);
-
- JMenuItem aboutItem = new JMenuItem(bundle.getString("aboutItem"));
- aboutItem.setActionCommand("about");
- aboutItem.addActionListener(this);
- helpMenu.add(aboutItem);
-
- menubar.add(fileMenu);
- menubar.add(helpMenu);
-
- setJMenuBar(menubar);
-
- JToolBar toolBar = new JToolBar();
- toolBar.setFloatable(false);
- toolBar.setRollover(true);
- toolBar.add(createToolButton("disk.png", bundle.getString("save"), "save"));
- this.add(toolBar, cons);
-
- cons.gridy++;
- cons.fill = GridBagConstraints.BOTH;
- cons.weighty = 1;
- loadConfigs();
- buildInterface();
- this.add(_tabPane, cons);
- }
-
- private JButton createToolButton(String image, String text, String action)
- {
- JButton button = new JButton(text, ImagesTable.getImage(image));
- button.setActionCommand(action);
- button.addActionListener(this);
- return button;
- }
-
- private void buildInterface()
- {
- ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
- ToolTipManager.sharedInstance().setInitialDelay(0);
- ToolTipManager.sharedInstance().setReshowDelay(0);
-
- GridBagConstraints cons = new GridBagConstraints();
- cons.fill = GridBagConstraints.NONE;
- cons.anchor = GridBagConstraints.FIRST_LINE_START;
- cons.insets = new Insets(2, 2, 2, 2);
- for (ConfigFile cf : getConfigs())
- {
- JPanel panel = new JPanel()
- {
- private static final long serialVersionUID = -323928678804839054L;
-
- @Override
- public void scrollRectToVisible(Rectangle r)
- {
- }
- };
- panel.setLayout(new GridBagLayout());
-
- cons.gridy = 0;
- cons.weighty = 0;
- for (ConfigComment cc : cf.getConfigProperties())
- {
- if (!(cc instanceof ConfigProperty))
- {
- continue;
- }
-
- ConfigProperty cp = (ConfigProperty) cc;
- cons.gridx = 0;
-
- JLabel keyLabel = new JLabel(cp.getDisplayName() + ':', ImagesTable.getImage("help.png"), SwingConstants.LEFT);
- String comments = "<b>" + cp.getName() + ":</b><br>" + cp.getComments();
- comments = comments.replace(EOL, "<br>");
- comments = "<html>" + comments + "</html>";
- keyLabel.setToolTipText(comments);
- cons.weightx = 0;
- panel.add(keyLabel, cons);
- cons.gridx++;
-
- JComponent valueComponent = cp.getValueComponent();
- valueComponent.setToolTipText(comments);
- cons.weightx = 1;
- panel.add(valueComponent, cons);
- cons.gridx++;
- cons.gridy++;
- }
- cons.gridy++;
- cons.weighty = 1;
- panel.add(new JLabel(), cons); // filler
- _tabPane.addTab(cf.getName(), new JScrollPane(panel));
- }
- }
-
- private void loadConfigs()
- {
- File configsDir = new File("config");
- for (File file : configsDir.listFiles())
- {
- if (file.getName().endsWith(".properties") && file.isFile() && file.canWrite())
- {
- try
- {
- parsePropertiesFile(file);
- }
- catch (IOException e)
- {
- JOptionPane.showMessageDialog(ConfigUserInterface.this, getBundle().getString("errorReading") + file.getName(), getBundle().getString("error"), JOptionPane.ERROR_MESSAGE);
- System.exit(3);
- }
- }
- }
- }
-
- /**
- * @param file
- * @throws IOException
- */
- private void parsePropertiesFile(File file) throws IOException
- {
- try (FileInputStream fis = new FileInputStream(file);
- InputStreamReader isr = new InputStreamReader(fis);
- LineNumberReader lnr = new LineNumberReader(isr))
- {
- String line;
- StringBuilder commentBuffer = new StringBuilder();
- ConfigFile cf = new ConfigFile(file);
- while ((line = lnr.readLine()) != null)
- {
- line = line.trim();
- if (line.isEmpty())
- {
- // blank line, reset comments
- if (commentBuffer.length() > 0)
- {
- cf.addConfigComment(commentBuffer.toString());
- }
- commentBuffer.setLength(0);
- }
- else if (line.charAt(0) == '#')
- {
- if (commentBuffer.length() > 0)
- {
- commentBuffer.append(EOL);
- }
- commentBuffer.append(line.substring(1));
- }
- else if (line.indexOf('=') >= 0)
- {
- String[] kv = line.split("=");
- String key = kv[0].trim();
- StringBuilder value = new StringBuilder();
- if (kv.length > 1)
- {
- value.append(kv[1].trim());
- }
-
- if (line.indexOf('\\') >= 0)
- {
- while (((line = lnr.readLine()) != null) && (line.indexOf('\\') >= 0))
- {
- value.append(EOL + line);
- }
- value.append(EOL + line);
- }
-
- String comments = commentBuffer.toString();
- commentBuffer.setLength(0); // reset
-
- cf.addConfigProperty(key, parseValue(value.toString()), comments);
- }
- }
- getConfigs().add(cf);
- }
- }
-
- /**
- * @param value
- * @return
- */
- private Object parseValue(String value)
- {
- if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true"))
- {
- return Boolean.parseBoolean(value);
- }
-
- /*
- * try { double parseDouble = Double.parseDouble(value); return parseDouble; } catch (NumberFormatException e) { // not a double, ignore }
- */
-
- // localhost -> 127.0.0.1
- if (value.equals("localhost"))
- {
- value = "127.0.0.1";
- }
-
- String[] parts = value.split("\\.");
- if (parts.length == 4)
- {
- boolean ok = true;
- for (int i = 0; (i < 4) && ok; i++)
- {
- try
- {
- int parseInt = Integer.parseInt(parts[i]);
- if ((parseInt < 0) || (parseInt > 255))
- {
- ok = false;
- }
- }
- catch (NumberFormatException e)
- {
- ok = false;
- }
- }
-
- if (ok)
- {
- try
- {
- InetAddress address = InetAddress.getByName(value);
- return address;
- }
- catch (UnknownHostException e)
- {
- // ignore
- }
- }
- }
-
- return value;
- }
-
- static class ConfigFile
- {
- private final File _file;
- private String _name;
- private final List<ConfigComment> _configs = new FastList<>();
-
- public ConfigFile(File file)
- {
- _file = file;
- int lastIndex = file.getName().lastIndexOf('.');
- setName(file.getName().substring(0, lastIndex));
- }
-
- public void addConfigProperty(String name, Object value, ValueType type, String comments)
- {
- _configs.add(new ConfigProperty(name, value, type, comments));
- }
-
- public void addConfigComment(String comment)
- {
- _configs.add(new ConfigComment(comment));
- }
-
- public void addConfigProperty(String name, Object value, String comments)
- {
- this.addConfigProperty(name, value, ValueType.firstTypeMatch(value), comments);
- }
-
- public List<ConfigComment> getConfigProperties()
- {
- return _configs;
- }
-
- /**
- * @param name The name to set.
- */
- public void setName(String name)
- {
- _name = name;
- }
-
- /**
- * @return Returns the name.
- */
- public String getName()
- {
- return _name;
- }
-
- public void save() throws IOException
- {
- try (FileOutputStream fos = new FileOutputStream(_file);
- OutputStreamWriter osw = new OutputStreamWriter(fos);
- BufferedWriter bufWriter = new BufferedWriter(osw))
- {
- for (ConfigComment cc : _configs)
- {
- cc.save(bufWriter);
- }
- }
- }
-
- class ConfigComment
- {
-
- private String _comments;
-
- /**
- * @param comments
- */
- public ConfigComment(String comments)
- {
- _comments = comments;
- }
-
- /**
- * @return Returns the comments.
- */
- public String getComments()
- {
- return _comments;
- }
-
- /**
- * @param comments The comments to set.
- */
- public void setComments(String comments)
- {
- _comments = comments;
- }
-
- public void save(Writer writer) throws IOException
- {
- StringBuilder sb = new StringBuilder();
- sb.append('#');
- sb.append(getComments().replace(EOL, EOL + "#"));
- sb.append(EOL + EOL);
- writer.write(sb.toString());
- }
- }
-
- class ConfigProperty extends ConfigComment
- {
- private String _propname;
- private Object _value;
- private ValueType _type;
- private JComponent _component;
-
- /**
- * @param name
- * @param value
- * @param type
- * @param comments
- */
- public ConfigProperty(String name, Object value, ValueType type, String comments)
- {
- super(comments);
- if (!type.getType().isAssignableFrom(value.getClass()))
- {
- throw new IllegalArgumentException("Value Instance Type doesn't match the type argument.");
- }
- _propname = name;
- _type = type;
- _value = value;
- }
-
- /**
- * @return Returns the name.
- */
- public String getName()
- {
- return _propname;
- }
-
- /**
- * @return Returns the name.
- */
- public String getDisplayName()
- {
- return unCamelize(_propname);
- }
-
- /**
- * @param name The name to set.
- */
- public void setName(String name)
- {
- _propname = name;
- }
-
- /**
- * @return Returns the value.
- */
- public Object getValue()
- {
- return _value;
- }
-
- /**
- * @param value The value to set.
- */
- public void setValue(String value)
- {
- _value = value;
- }
-
- /**
- * @return Returns the type.
- */
- public ValueType getType()
- {
- return _type;
- }
-
- /**
- * @param type The type to set.
- */
- public void setType(ValueType type)
- {
- _type = type;
- }
-
- public JComponent getValueComponent()
- {
- if (_component == null)
- {
- _component = createValueComponent();
- }
- return _component;
- }
-
- public JComponent createValueComponent()
- {
- switch (getType())
- {
- case BOOLEAN:
- boolean bool = (Boolean) getValue();
- JCheckBox checkBox = new JCheckBox();
- checkBox.setSelected(bool);
- return checkBox;
- case IPv4:
- return new JIPTextField((Inet4Address) getValue());
- case DOUBLE:
- case INTEGER:
- case STRING:
- default:
- String val = getValue().toString();
- JTextArea textArea = new JTextArea(val);
- textArea.setFont(UIManager.getFont("TextField.font"));
- int rows = 1;
- for (int i = 0; i < val.length(); i++)
- {
- if (val.charAt(i) == '\\')
- {
- rows++;
- }
- }
- textArea.setRows(rows);
- textArea.setColumns(Math.max(val.length() / rows, 20));
- return textArea;
- }
- }
-
- @Override
- public void save(Writer writer) throws IOException
- {
- String value;
- if (getValueComponent() instanceof JCheckBox)
- {
- value = Boolean.toString(((JCheckBox) getValueComponent()).isSelected());
- value = value.substring(0, 1).toUpperCase() + value.substring(1);
- }
- else if (getValueComponent() instanceof JIPTextField)
- {
- value = ((JIPTextField) getValueComponent()).getText();
- }
- else if (getValueComponent() instanceof JTextArea)
- {
- value = ((JTextArea) getValueComponent()).getText();
- }
- else
- {
- throw new IllegalStateException("Unhandled component value");
- }
-
- StringBuilder sb = new StringBuilder();
- sb.append('#');
- sb.append(getComments().replace(EOL, EOL + "#"));
- sb.append(EOL);
- sb.append(getName());
- sb.append(" = ");
- sb.append(value);
- sb.append(EOL);
- sb.append(EOL);
- writer.write(sb.toString());
- }
- }
- }
-
- public static enum ValueType
- {
- BOOLEAN(Boolean.class),
- DOUBLE(Double.class),
- INTEGER(Integer.class),
- IPv4(Inet4Address.class),
- STRING(String.class);
-
- private final Class<?> _type;
-
- private ValueType(Class<?> type)
- {
- _type = type;
- }
-
- /**
- * @return Returns the type.
- */
- public Class<?> getType()
- {
- return _type;
- }
-
- public static ValueType firstTypeMatch(Object value)
- {
- for (ValueType vt : ValueType.values())
- {
- if (vt.getType() == value.getClass())
- {
- return vt;
- }
- }
- throw new NoSuchElementException("No match for: " + value.getClass().getName());
- }
- }
-
- @Override
- public void actionPerformed(ActionEvent e)
- {
- String cmd = e.getActionCommand();
-
- StringBuilder errors = new StringBuilder();
-
- if (cmd.equals("save"))
- {
- for (ConfigFile cf : ConfigUserInterface.this.getConfigs())
- {
- try
- {
- cf.save();
- }
- catch (Exception e1)
- {
- e1.printStackTrace();
- errors.append(getBundle().getString("errorSaving") + cf.getName() + ".properties. " + getBundle().getString("reason") + e1.getLocalizedMessage() + EOL);
- }
- }
- if (errors.length() == 0)
- {
- JOptionPane.showMessageDialog(ConfigUserInterface.this, getBundle().getString("success"), "OK", JOptionPane.INFORMATION_MESSAGE);
- }
- else
- {
- JOptionPane.showMessageDialog(ConfigUserInterface.this, errors, getBundle().getString("error"), JOptionPane.ERROR_MESSAGE);
- System.exit(2);
- }
- }
- else if (cmd.equals("exit"))
- {
- System.exit(0);
- }
- else if (cmd.equals("about"))
- {
- JOptionPane.showMessageDialog(ConfigUserInterface.this, getBundle().getString("credits") + EOL + "http://www.l2jserver.com" + EOL + EOL + getBundle().getString("icons") + EOL + EOL + getBundle().getString("langText") + EOL + getBundle().getString("translation"), getBundle().getString("aboutItem"), JOptionPane.INFORMATION_MESSAGE, ImagesTable.getImage("l2jserverlogo.png"));
- }
- }
-
- /**
- * @param configs The configuration to set.
- */
- public void setConfigs(List<ConfigFile> configs)
- {
- _configs = configs;
- }
-
- /**
- * @return Returns the configuration.
- */
- public List<ConfigFile> getConfigs()
- {
- return _configs;
- }
-
- /**
- * @param keyName
- * @return Returns the configuration setting name in a human readable form.
- */
- public static String unCamelize(final String keyName)
- {
- Pattern p = Pattern.compile("\\p{Lu}");
- Matcher m = p.matcher(keyName);
- StringBuffer sb = new StringBuffer();
- int last = 0;
- while (m.find())
- {
- if (m.start() != (last + 1))
- {
- m.appendReplacement(sb, " " + m.group());
- }
- last = m.start();
- }
- m.appendTail(sb);
- return sb.toString().trim();
- }
-
- /**
- * @param bundle The bundle to set.
- */
- public void setBundle(ResourceBundle bundle)
- {
- _bundle = bundle;
- }
-
- /**
- * @return Returns the bundle.
- */
- public ResourceBundle getBundle()
- {
- return _bundle;
- }
- }
|