diff --git a/README.md b/README.md index 34fb81b..1931d62 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,20 @@ some Apache Server basics statistics. ## Installation -For Windows, Hoster uses `httpd -k restart` to restart Apache Server. +Obviously, in order to use Hoster, it's necessary have installed Apache Server +and PHP in your computer. + +In Windows systems, Hoster uses `httpd -k restart` to restart Apache Server. If this command fails, Hoster runs `httpd -k install` which register Apache as a Service in the system. This can be avoided if `httpd.exe` is added to `PATH` being accessible through console globally. +For Linux systems, Hoster uses `service apache2 status` and `service apache2 restart` +commands to manage Apache Server, so they need to be accessible through terminal. + +To compile, it's necessary add `lib/flatlaf-1.3.jar` as library in your IDE or +code editor and configure `Java 1.8` as minimum JDK. + ## Usage A GIF is worth a thousand words: @@ -47,6 +56,11 @@ $ git clone git@github.com:dprietob/hoster.git $ cd hoster ``` +## Dependencies + +Hoster use [FlatLaf](https://github.com/JFormDesigner/FlatLaf) to GUI customization, +also some [FarmFresh icons](https://www.fatcow.com/free-icons). + ## License This project is licensed under the terms of the MIT license. diff --git a/src/com/hoster/Hoster.java b/src/com/hoster/Hoster.java index e0f5bde..6a4b2b6 100644 --- a/src/com/hoster/Hoster.java +++ b/src/com/hoster/Hoster.java @@ -2,26 +2,34 @@ import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatLightLaf; +import com.hoster.data.HList; import com.hoster.data.Host; import com.hoster.data.Properties; import com.hoster.files.HostsFile; import com.hoster.files.PropertiesFile; -import com.hoster.gui.HostFrame; +import com.hoster.files.VHostsFile; +import com.hoster.gui.MainFrame; import javax.swing.*; import java.awt.*; -import java.util.List; public class Hoster { public static void main(String[] args) { EventQueue.invokeLater(() -> { - Properties properties = new Properties(PropertiesFile.load()); - List hostsList = HostsFile.load(properties.getString("hosts_file")); + PropertiesFile propertiesFile = new PropertiesFile(); + HostsFile hostsFile = new HostsFile(); + VHostsFile vHostsFile = new VHostsFile(); + + Properties properties = new Properties(propertiesFile, hostsFile, vHostsFile); + HList hostsList = mergeHostLists( + hostsFile.load(properties.getString("hosts_file")), + vHostsFile.load(properties.getString("vhosts_file")) + ); setTheme(properties.getString("theme")); - HostFrame frame = new HostFrame(hostsList, properties); + MainFrame frame = new MainFrame(properties, hostsList); frame.build(); }); } @@ -38,4 +46,22 @@ private static void setTheme(String themeType) JOptionPane.showMessageDialog(null, e.getMessage()); } } + + private static HList mergeHostLists(HList hosts, HList vhosts) + { + Host h; + for (Host vh : vhosts) { + h = hosts.getEquals(vh); + if (h != null) { + vh.setIp(h.getIp()); + vh.setActive(h.isActive()); + } + } + /* + * Adds the rest of the hosts defined in the hosts file that are + * not defined in the vhosts file. + */ + vhosts.addAll(hosts.diff(vhosts)); + return vhosts; + } } diff --git a/src/com/hoster/data/HList.java b/src/com/hoster/data/HList.java new file mode 100644 index 0000000..f944989 --- /dev/null +++ b/src/com/hoster/data/HList.java @@ -0,0 +1,11 @@ +package com.hoster.data; + +import java.util.Collection; +import java.util.List; + +public interface HList extends List +{ + Host getEquals(Host h); + + Collection diff(Collection hl); +} diff --git a/src/com/hoster/data/Host.java b/src/com/hoster/data/Host.java index 3bbcbf2..d78025e 100644 --- a/src/com/hoster/data/Host.java +++ b/src/com/hoster/data/Host.java @@ -9,7 +9,6 @@ public class Host private boolean active; private String ip; - private String domain; private String serverAdmin; private String serverName; private String documentRoot; @@ -34,24 +33,26 @@ public void setActive(boolean active) this.active = active; } - public String getIp() + public HostStatus getStatus() { - return ip; - } + if (!hasDomainData()) { + return HostStatus.NO_DOMAIN; - public void setIp(String ip) - { - this.ip = ip; + } else if (!hasVirtualHostData()) { + return HostStatus.NO_VHOST; + } + + return HostStatus.ALL_OK; } - public String getDomain() + public String getIp() { - return domain; + return ip; } - public void setDomain(String domain) + public void setIp(String ip) { - this.domain = domain; + this.ip = ip; } public String getServerAdmin() @@ -97,7 +98,7 @@ public void setServerAlias(String serverAlias) public String getPort() { - return port; + return (port != null) ? port : DEFAULT_PORT; } public void setPort(String port) @@ -135,7 +136,15 @@ public void setDirectory(Directory directory) this.directory = directory; } - public boolean isValid() + private boolean hasDomainData() + { + return getIp() != null + && !getIp().isEmpty() + && getServerName() != null + && !getServerName().isEmpty(); + } + + private boolean hasVirtualHostData() { return getDocumentRoot() != null && !getDocumentRoot().isEmpty() @@ -143,10 +152,24 @@ && getServerName() != null && !getServerName().isEmpty(); } + @Override + public boolean equals(Object obj) + { + if (this == obj) { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + Host host = (Host) obj; + return serverName.equals(host.serverName); + } + public String parseToXML() { - if (isValid()) { - String port = getPort() != null ? getPort() : DEFAULT_PORT; + if (hasVirtualHostData()) { StringBuilder out = new StringBuilder(); Map data = new LinkedHashMap<>(); Directory directory = getDirectory(); @@ -158,10 +181,10 @@ public String parseToXML() data.put("ErrorLog", getErrorLog()); data.put("CustomLog", getCustomLog()); - out.append("").append("\n"); + out.append("").append("\n"); insertTagsToXML(out, data); out.append(directory.parseToXML(true)); - out.append("").append("\n"); + out.append(""); return out.toString(); } diff --git a/src/com/hoster/data/HostList.java b/src/com/hoster/data/HostList.java new file mode 100644 index 0000000..6f63e96 --- /dev/null +++ b/src/com/hoster/data/HostList.java @@ -0,0 +1,30 @@ +package com.hoster.data; + +import java.util.ArrayList; +import java.util.Collection; + +public class HostList extends ArrayList implements HList +{ + @Override + public Host getEquals(Host h) + { + for (Host host : this) { + if (host.equals(h)) { + return host; + } + } + return null; + } + + @Override + public Collection diff(Collection hl) + { + Collection c = new ArrayList<>(); + for (Host h : this) { + if (!hl.contains(h)) { + c.add(h); + } + } + return c; + } +} diff --git a/src/com/hoster/data/HostStatus.java b/src/com/hoster/data/HostStatus.java new file mode 100644 index 0000000..988a8c0 --- /dev/null +++ b/src/com/hoster/data/HostStatus.java @@ -0,0 +1,6 @@ +package com.hoster.data; + +public enum HostStatus +{ + NO_DOMAIN, NO_VHOST, ALL_OK +} diff --git a/src/com/hoster/data/Properties.java b/src/com/hoster/data/Properties.java index ba1c0cc..eae68a6 100644 --- a/src/com/hoster/data/Properties.java +++ b/src/com/hoster/data/Properties.java @@ -1,20 +1,45 @@ package com.hoster.data; +import com.hoster.files.HostsFile; +import com.hoster.files.PropertiesFile; +import com.hoster.files.VHostsFile; + import java.util.HashMap; import java.util.Map; public class Properties { + private PropertiesFile propertiesFile; + private HostsFile hostsFile; + private VHostsFile vHostsFile; private Map propertiesMap; private Map properties; - public Properties(Map map) + public Properties(PropertiesFile pf, HostsFile hf, VHostsFile vf) { - propertiesMap = map; + propertiesFile = pf; + hostsFile = hf; + vHostsFile = vf; + propertiesMap = pf.load(); properties = new HashMap<>(); parseToProperties(); } + public PropertiesFile getPropertiesFile() + { + return propertiesFile; + } + + public HostsFile getHostsFile() + { + return hostsFile; + } + + public VHostsFile getVHostsFile() + { + return vHostsFile; + } + public Map getPropertiesMap() { return propertiesMap; @@ -33,7 +58,7 @@ public String getString(String key) public boolean getBoolean(String key) { - return (boolean)properties.get(key); + return (boolean) properties.get(key); } public Directory getMainDirectory() @@ -52,6 +77,7 @@ private void parseToProperties() properties.put("theme", propertiesMap.get("theme")); properties.put("hosts_file", propertiesMap.get("hosts_file")); properties.put("vhosts_file", propertiesMap.get("vhosts_file")); + properties.put("console_log", propertiesMap.get("console_log").equals("1")); properties.put("apache_path", propertiesMap.get("apache_path")); properties.put("restart_server", propertiesMap.get("restart_server").equals("1")); properties.put("main_directory", mainDirectory); diff --git a/src/com/hoster/data/Server.java b/src/com/hoster/data/Server.java index d8b9e6f..ddd151b 100644 --- a/src/com/hoster/data/Server.java +++ b/src/com/hoster/data/Server.java @@ -1,5 +1,7 @@ package com.hoster.data; +import com.hoster.gui.listeners.ConsoleListener; + import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; @@ -13,7 +15,14 @@ public class Server private static final String WINDOWS_RESTART_COMMAND = "\\bin\\httpd -k restart"; // runservice private static final String WINDOWS_INSTALL_COMMAND = "\\bin\\httpd -k install"; - public static void restart(String apachePath) + private ConsoleListener consoleListener; + + public void addConsoleListener(ConsoleListener listener) + { + consoleListener = listener; + } + + public void restart(String apachePath) { switch (getOS()) { case LINUX: @@ -30,24 +39,22 @@ public static void restart(String apachePath) } } - private static void restartWindowsServer(String apachePath) + private void restartWindowsServer(String apachePath) { if (apachePath != null) { String cmdOutput = getCommandOutput(apachePath + WINDOWS_RESTART_COMMAND); - System.out.println(cmdOutput); if (cmdOutput != null && cmdOutput.contains("No installed service")) { - System.out.println("Servicio no instalado"); exeCommand(WINDOWS_INSTALL_COMMAND); exeCommand(WINDOWS_RESTART_COMMAND); } else { - // TODO + consoleListener.onConsoleError("Unable to restart server: unknown restart response."); } } else { - // TODO + consoleListener.onConsoleError("Unable to restart server: Apache path is empty."); } } - public static boolean isActive() + public boolean isActive() { switch (getOS()) { case LINUX: @@ -60,7 +67,7 @@ public static boolean isActive() return false; } - private static boolean isLinuxServerActive() + private boolean isLinuxServerActive() { String cmdOutput = getCommandOutput(LINUX_STATUS_COMMAND); @@ -68,31 +75,30 @@ private static boolean isLinuxServerActive() && cmdOutput.contains("Active: active (running)"); } - private static boolean isWindowsServerActive() + private boolean isWindowsServerActive() { String cmdOutput = getCommandOutput(WINDOWS_STATUS_COMMAND); return cmdOutput != null && cmdOutput.contains("httpd.exe"); } - private static boolean isMacServerActive() + private boolean isMacServerActive() { // TODO return false; } - private static void exeCommand(String cmd) + private void exeCommand(String cmd) { try { - System.out.println(cmd); Runtime run = Runtime.getRuntime(); Process pr = run.exec(cmd); pr.waitFor(); } catch (Exception e) { - e.printStackTrace(); + consoleListener.onConsoleError(e.getMessage()); } } - private static String getCommandOutput(String cmd) + private String getCommandOutput(String cmd) { try { Runtime run = Runtime.getRuntime(); @@ -116,12 +122,12 @@ private static String getCommandOutput(String cmd) return scOutput.toString(); } catch (Exception e) { - e.printStackTrace(); + consoleListener.onConsoleError(e.getMessage()); } return null; } - public static OperatingSystem getOS() + public OperatingSystem getOS() { String osName = System.getProperty("os.name").toLowerCase(); diff --git a/src/com/hoster/files/ConfigFile.java b/src/com/hoster/files/ConfigFile.java index 67fbf95..e1cbf6c 100644 --- a/src/com/hoster/files/ConfigFile.java +++ b/src/com/hoster/files/ConfigFile.java @@ -1,11 +1,20 @@ package com.hoster.files; +import com.hoster.gui.listeners.ConsoleListener; + import java.io.File; import java.io.IOException; public class ConfigFile { - public static boolean deleteCurrentFile(String fileName) + protected ConsoleListener consoleListener; + + public void addConsoleListener(ConsoleListener listener) + { + consoleListener = listener; + } + + public boolean deleteCurrentFile(String fileName) { File file = new File(fileName); if (file.exists()) { @@ -14,13 +23,14 @@ public static boolean deleteCurrentFile(String fileName) return true; } - public static boolean createNewFile(String fileName) + public boolean createNewFile(String fileName) { try { File file = new File(fileName); return file.createNewFile(); } catch (IOException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return false; } diff --git a/src/com/hoster/files/HostsFile.java b/src/com/hoster/files/HostsFile.java index 43f9e01..77f97ba 100644 --- a/src/com/hoster/files/HostsFile.java +++ b/src/com/hoster/files/HostsFile.java @@ -1,47 +1,50 @@ package com.hoster.files; +import com.hoster.data.HList; import com.hoster.data.Host; +import com.hoster.data.HostList; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Scanner; public class HostsFile extends ConfigFile { - public static List load(String fileName) + public HList load(String fileName) { - List hostsList = new ArrayList<>(); + HList hostsList = new HostList(); + File file = new File(fileName); + Host h; try { - Host h; - File file = new File(fileName); if (file.exists()) { - Scanner myReader = new Scanner(file); - while (myReader.hasNextLine()) { - String[] line = myReader.nextLine().split(" "); + Scanner reader = new Scanner(file); + while (reader.hasNextLine()) { + String[] line = reader.nextLine().split(" "); if (line.length == 2) { h = new Host(); - h.setActive(isHostActive(line[0])); - h.setIp(line[0].replaceAll("#", "")); - h.setDomain(line[1]); + h.setActive(isHostActive(line[0].trim())); + h.setIp(line[0].trim().replaceAll("#", "")); + h.setServerName(line[1].trim()); hostsList.add(h); } } - myReader.close(); + reader.close(); + } else { + // TODO } } catch (FileNotFoundException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return hostsList; } - public static boolean save(String fileName, List hostsList, String appName, String appVersion) + public boolean save(String fileName, HList hostsList, String appName, String appVersion) { if (deleteCurrentFile(fileName)) { if (createNewFile(fileName)) { @@ -51,28 +54,29 @@ public static boolean save(String fileName, List hostsList, String appName return false; } - private static boolean writeFile(String fileName, List hostsList, String appName, String appVersion) + private boolean writeFile(String fileName, HList hostsList, String appName, String appVersion) { try { if (!hostsList.isEmpty()) { FileWriter fileWriter = new FileWriter(fileName); - fileWriter.write("# Generated by " + appName + " " + appVersion + "\n"); + fileWriter.write("# Generated by " + appName + " " + appVersion + "\n\n"); String ip; for (Host h : hostsList) { ip = (!h.isActive() ? "#" : "") + h.getIp(); - fileWriter.write(ip + " " + h.getDomain() + "\n"); + fileWriter.write(ip + " " + h.getServerName() + "\n"); } fileWriter.close(); return true; } } catch (IOException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return false; } - private static boolean isHostActive(String ip) + private boolean isHostActive(String ip) { return ip.charAt(0) != '#'; } diff --git a/src/com/hoster/files/PropertiesFile.java b/src/com/hoster/files/PropertiesFile.java index 7b59fed..2cf9d7e 100644 --- a/src/com/hoster/files/PropertiesFile.java +++ b/src/com/hoster/files/PropertiesFile.java @@ -15,29 +15,31 @@ public class PropertiesFile extends ConfigFile private static final String CONFIG_FILE = "config.properties"; - public static Map load() + public Map load() { Map initialConfig = getEmptyProperties(); + File file = new File(CONFIG_FILE); + try { - File file = new File(CONFIG_FILE); if (file.exists()) { - Scanner myReader = new Scanner(file); - while (myReader.hasNextLine()) { - String[] line = myReader.nextLine().split("="); + Scanner reader = new Scanner(file); + while (reader.hasNextLine()) { + String[] line = reader.nextLine().split("="); if (line.length == 2) { initialConfig.put(line[0].trim(), line[1].trim()); } } - myReader.close(); + reader.close(); } } catch (FileNotFoundException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return initialConfig; } - public static boolean save(Properties properties) + public boolean save(Properties properties) { if (deleteCurrentFile(CONFIG_FILE)) { if (createNewFile(CONFIG_FILE)) { @@ -47,7 +49,7 @@ public static boolean save(Properties properties) return false; } - private static boolean writeFile(Properties properties) + private boolean writeFile(Properties properties) { try { FileWriter fileWriter = new FileWriter(CONFIG_FILE); @@ -58,17 +60,18 @@ private static boolean writeFile(Properties properties) fileWriter.close(); return true; } catch (IOException e) { - e.printStackTrace(); + consoleListener.onConsoleError(e.getMessage()); } return false; } - private static Map getEmptyProperties() + private Map getEmptyProperties() { Map properties = new HashMap<>(); properties.put("theme", "light"); properties.put("hosts_file", ""); properties.put("vhosts_file", ""); + properties.put("console_log", "0"); properties.put("directory_path", ""); properties.put("directory_require", ""); properties.put("directory_allow_override", ""); diff --git a/src/com/hoster/files/VHostsFile.java b/src/com/hoster/files/VHostsFile.java index 66792fc..1016461 100644 --- a/src/com/hoster/files/VHostsFile.java +++ b/src/com/hoster/files/VHostsFile.java @@ -1,80 +1,173 @@ package com.hoster.files; import com.hoster.data.Directory; +import com.hoster.data.HList; import com.hoster.data.Host; +import com.hoster.data.HostList; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Scanner; public class VHostsFile extends HostsFile { - // TODO - public static List load(String fileName) + public HList load(String fileName) { - List hostsList = new ArrayList<>(); + HList hostsList = new HostList(); + File file = new File(fileName); + String line; try { - Host h; - File file = new File(fileName); if (file.exists()) { - Scanner myReader = new Scanner(file); - while (myReader.hasNextLine()) { - String[] line = myReader.nextLine().split(" "); - if (line.length == 2) { - h = new Host(); - h.setIp(line[0]); - h.setDomain(line[1]); - - hostsList.add(h); + Scanner reader = new Scanner(file); + while (reader.hasNextLine()) { + line = reader.nextLine().trim(); + if (isDirectory(line)) { + loadDirectory(reader, line); + } else if (isVirtualHost(line)) { + hostsList.add(loadVirtualHost(reader, line)); } } - myReader.close(); + reader.close(); + } else { + // TODO } } catch (FileNotFoundException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return hostsList; } - public static boolean save(String fileName, List hostsList, Directory mainDirectory, String appName, String appVersion) + public boolean save(String fileName, Directory mainDirectory, HList hostsList, String appName, String appVersion) { if (deleteCurrentFile(fileName)) { if (createNewFile(fileName)) { - return writeFile(fileName, hostsList, appName, appVersion); + return writeFile(fileName, mainDirectory, hostsList, appName, appVersion); } } return false; } - private static boolean writeFile(String fileName, List hostsList, String appName, String appVersion) + private boolean writeFile(String fileName, Directory mainDirectory, HList hostsList, String appName, String appVersion) { try { if (!hostsList.isEmpty()) { FileWriter fileWriter = new FileWriter(fileName); - fileWriter.write("# Generated by " + appName + " " + appVersion + "\n"); - - fileWriter.write(getMainDirectoryXML()); + fileWriter.write("# Generated by " + appName + " " + appVersion + "\n\n"); + fileWriter.write(mainDirectory.parseToXML() + "\n\n"); for (Host h : hostsList) { - fileWriter.write(h.parseToXML()); + fileWriter.write(h.parseToXML() + "\n\n"); } + fileWriter.close(); return true; } } catch (IOException e) { - e.printStackTrace(); + // TODO: consoleListener is null on main() call + consoleListener.onConsoleError(e.getMessage()); } return false; } - private static String getMainDirectoryXML() + private boolean isDirectory(String line) + { + return line.toLowerCase().indexOf("", "")); + } + + while (reader.hasNextLine()) { + line = reader.nextLine().trim(); + if (line.equalsIgnoreCase("")) { + break; + + } else { + defSplit = line.split(" "); + if (defSplit.length == 2) { + setDirectoryValue(directory, defSplit[0], defSplit[1]); + } + } + } + + return directory; + } + + private Host loadVirtualHost(Scanner reader, String line) { - return ""; + Host host = new Host(); + String[] defSplit = line.split(":"); + + if (defSplit.length == 2) { + host.setPort(defSplit[1].trim().replaceAll(">", "")); + } + + while (reader.hasNextLine()) { + line = reader.nextLine().trim(); + if (line.equalsIgnoreCase("")) { + break; + + } else if (line.contains("
- + - + @@ -11,7 +11,7 @@ - + @@ -39,34 +39,25 @@ - + - + - - - - - - - - - - + - + @@ -74,18 +65,19 @@ - + + - + - + @@ -95,7 +87,15 @@ - + + + + + + + + + @@ -103,7 +103,7 @@ - + @@ -135,7 +135,7 @@ - + @@ -167,7 +167,7 @@ - + @@ -198,6 +198,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/hoster/gui/AboutDialog.java b/src/com/hoster/gui/AboutDialog.java index daf2a10..5f9d110 100644 --- a/src/com/hoster/gui/AboutDialog.java +++ b/src/com/hoster/gui/AboutDialog.java @@ -1,5 +1,7 @@ package com.hoster.gui; +import com.hoster.gui.listeners.ConsoleListener; + import javax.swing.*; import java.awt.*; import java.awt.event.*; @@ -13,6 +15,8 @@ public class AboutDialog extends JDialog implements MouseListener private final String REPORT_URL = "https://github.com/dprietob/hoster/issues"; private final Color LINK_COLOR = new Color(42, 155, 187); + private ConsoleListener consoleListener; + private JFrame parent; private JPanel aboutPane; private JLabel developerLink; @@ -48,6 +52,11 @@ public void windowClosing(WindowEvent e) aboutPane.registerKeyboardAction(e -> onClose(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } + public void addConsoleListener(ConsoleListener listener) + { + consoleListener = listener; + } + public void build() { pack(); @@ -67,7 +76,7 @@ private boolean openWebpage(String url) desktop.browse(new URL(url).toURI()); return true; } catch (Exception e) { - e.printStackTrace(); + consoleListener.onConsoleError(e.getMessage()); } } return false; diff --git a/src/com/hoster/gui/EditHostDialog.java b/src/com/hoster/gui/EditHostDialog.java deleted file mode 100644 index 7cd0424..0000000 --- a/src/com/hoster/gui/EditHostDialog.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.hoster.gui; - -import com.hoster.data.Host; - -import javax.swing.*; - -public class EditHostDialog extends AddHostDialog -{ - private Host host; - private int hostPosition; - - public EditHostDialog(JFrame p, Host h, int pos) - { - super(p); - host = h; - hostPosition = pos; - - active.setSelected(h.isActive()); - ip.setText(h.getIp()); - domain.setText(h.getDomain()); - } - - public void build() - { - super.build(); - setIconImage(new ImageIcon(getClass().getResource("icons/edit.png")).getImage()); - setTitle("Edit host"); - } - - protected void onAccept() - { - if (fieldsFilled()) { - host.setActive(active.isSelected()); - host.setIp(ip.getText()); - host.setDomain(domain.getText()); - - hostListener.onHostEdited(host, hostPosition); - dispose(); - } else { - JOptionPane.showMessageDialog( - this, - "IP and domain fields can not be empty.", - "Empty fields", - JOptionPane.ERROR_MESSAGE); - } - } -} diff --git a/src/com/hoster/gui/AddHostDialog.form b/src/com/hoster/gui/HostDialog.form similarity index 98% rename from src/com/hoster/gui/AddHostDialog.form rename to src/com/hoster/gui/HostDialog.form index 7c419cd..bddd02f 100644 --- a/src/com/hoster/gui/AddHostDialog.form +++ b/src/com/hoster/gui/HostDialog.form @@ -1,5 +1,5 @@ -
+ diff --git a/src/com/hoster/gui/AddHostDialog.java b/src/com/hoster/gui/HostDialog.java similarity index 62% rename from src/com/hoster/gui/AddHostDialog.java rename to src/com/hoster/gui/HostDialog.java index 83f398e..c8b67fb 100644 --- a/src/com/hoster/gui/AddHostDialog.java +++ b/src/com/hoster/gui/HostDialog.java @@ -8,9 +8,13 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -public class AddHostDialog extends JDialog +public class HostDialog extends JDialog { + private Host host; + private int hostPosition; + private boolean isNew; protected HostListener hostListener; + private JFrame parent; private JPanel addHostPane; protected JTextField ip; @@ -19,12 +23,15 @@ public class AddHostDialog extends JDialog private JButton cancel; private JButton accept; - public AddHostDialog(JFrame p) + public HostDialog(JFrame p, Host h, int pos) { parent = p; + host = h; + hostPosition = pos; setContentPane(addHostPane); getRootPane().setDefaultButton(accept); + setHostConfig(); accept.addActionListener(e -> onAccept()); cancel.addActionListener(e -> onCancel()); @@ -50,24 +57,47 @@ public void addHostListener(HostListener listener) public void build() { + if (isNew) { + setIconImage(new ImageIcon(getClass().getResource("icons/add.png")).getImage()); + setTitle("Add new host"); + } else { + setIconImage(new ImageIcon(getClass().getResource("icons/edit.png")).getImage()); + setTitle("Edit host"); + } + pack(); - setIconImage(new ImageIcon(getClass().getResource("icons/add.png")).getImage()); - setTitle("Add new host"); setLocationRelativeTo(parent); setModal(true); setResizable(false); setVisible(true); } + protected void setHostConfig() + { + if (host == null) { + host = new Host(); + isNew = true; + } else { + active.setSelected(host.isActive()); + ip.setText(host.getIp()); + domain.setText(host.getServerName()); + isNew = false; + } + } + protected void onAccept() { if (fieldsFilled()) { - Host host = new Host(); host.setActive(active.isSelected()); host.setIp(ip.getText()); - host.setDomain(domain.getText()); + host.setServerName(domain.getText()); + + if (isNew) { + hostListener.onHostAdded(host); + } else { + hostListener.onHostEdited(host, hostPosition); + } - hostListener.onHostAdded(host); dispose(); } else { JOptionPane.showMessageDialog( @@ -85,6 +115,7 @@ protected void onCancel() protected boolean fieldsFilled() { - return !ip.getText().equals("") || !domain.getText().equals(""); + return !ip.getText().isEmpty() + && !domain.getText().isEmpty(); } } diff --git a/src/com/hoster/gui/HostsTableRenderer.java b/src/com/hoster/gui/HostsTableRenderer.java index 760f6af..7318c52 100644 --- a/src/com/hoster/gui/HostsTableRenderer.java +++ b/src/com/hoster/gui/HostsTableRenderer.java @@ -1,17 +1,32 @@ package com.hoster.gui; +import com.hoster.data.HList; import com.hoster.data.Host; +import com.hoster.data.HostList; +import com.hoster.data.HostStatus; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import java.awt.*; -import java.util.List; public class HostsTableRenderer extends DefaultTableCellRenderer { - private List hostList; + private final Color bgStandard = UIManager.getColor("Table.background"); + private final Color fgStandard = UIManager.getColor("Table.foreground"); + private final Color bgSelected = UIManager.getColor("Table.selectionBackground"); + private final Color fgSelected = UIManager.getColor("Table.selectionForeground"); + private final Color bgFocus = UIManager.getColor("Table.focusCellBackground"); + private final Color fgFocus = UIManager.getColor("Table.focusCellForeground"); + private final Color bgInactive = UIManager.getColor("Table.background").darker(); + private final Color fgInactive = UIManager.getColor("Table.background").brighter(); + private final Color bgNoDomain = new Color(255, 212, 212); + private final Color fgNoDomain = bgNoDomain.darker(); + private final Color bgNoVhost = new Color(255, 255, 170); + private final Color fgNoVhost = bgNoVhost.darker(); - public HostsTableRenderer(List hl) + private HList hostList; + + public HostsTableRenderer(HList hl) { hostList = hl; } @@ -21,23 +36,62 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); Host h = hostList.get(row); - Color background = UIManager.getColor("Table.background"); - Color foreground = UIManager.getColor("Table.foreground"); + + Color background; + Color foreground; + String details; + ImageIcon icon; + + if (h.getStatus() == HostStatus.NO_DOMAIN) { + background = bgNoDomain; + foreground = fgNoDomain; + details = "No IP or domain defined"; + icon = new ImageIcon(getClass().getResource("icons/no_domain.png")); + + } else if (h.getStatus() == HostStatus.NO_VHOST) { + background = bgNoVhost; + foreground = fgNoVhost; + details = "No virtual host defined"; + icon = new ImageIcon(getClass().getResource("icons/no_vhost.png")); + + } else if (!h.isActive()) { + background = bgInactive; + foreground = fgInactive; + details = "Domain inactive"; + icon = new ImageIcon(getClass().getResource("icons/inactive.png")); + + } else { + background = bgStandard; + foreground = fgStandard; + details = ""; + icon = new ImageIcon(getClass().getResource("icons/all_ok.png")); + } if (isSelected) { - background = UIManager.getColor("Table.selectionBackground"); - foreground = UIManager.getColor("Table.selectionForeground"); + background = bgSelected; + foreground = fgSelected; + } else if (hasFocus) { - background = UIManager.getColor("Table.focusCellBackground"); - foreground = UIManager.getColor("Table.focusCellForeground"); - } else if (!h.isActive()) { - background = UIManager.getColor("Table.background").darker(); - foreground = UIManager.getColor("Table.background").brighter(); + background = bgFocus; + foreground = fgFocus; } c.setBackground(background); c.setForeground(foreground); + // Status column + if (column == 0) { + ((JLabel) c).setText(""); + ((JLabel) c).setIcon(icon); + + // Details column + } else if (column == 3) { + ((JLabel) c).setText(details); + + } else { + ((JLabel) c).setIcon(null); + } + return c; } } diff --git a/src/com/hoster/gui/HostFrame.form b/src/com/hoster/gui/MainFrame.form similarity index 83% rename from src/com/hoster/gui/HostFrame.form rename to src/com/hoster/gui/MainFrame.form index 82b69ea..f1b874a 100644 --- a/src/com/hoster/gui/HostFrame.form +++ b/src/com/hoster/gui/MainFrame.form @@ -1,12 +1,12 @@ - - + + - + @@ -28,7 +28,7 @@ - + @@ -156,6 +156,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/hoster/gui/HostFrame.java b/src/com/hoster/gui/MainFrame.java similarity index 64% rename from src/com/hoster/gui/HostFrame.java rename to src/com/hoster/gui/MainFrame.java index c8221f6..ddecd2b 100644 --- a/src/com/hoster/gui/HostFrame.java +++ b/src/com/hoster/gui/MainFrame.java @@ -1,32 +1,48 @@ package com.hoster.gui; +import com.hoster.data.HList; import com.hoster.data.Host; import com.hoster.data.Properties; import com.hoster.data.Server; -import com.hoster.files.HostsFile; -import com.hoster.files.PropertiesFile; -import com.hoster.files.VHostsFile; +import com.hoster.gui.listeners.ConsoleListener; import com.hoster.gui.listeners.HostListener; import com.hoster.gui.listeners.PropertiesListener; import javax.swing.*; +import javax.swing.text.BadLocationException; +import javax.swing.text.Style; +import javax.swing.text.StyleConstants; +import javax.swing.text.StyledDocument; import java.awt.*; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; +import java.awt.event.WindowEvent; +import java.awt.event.WindowFocusListener; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; import java.util.Map; -public class HostFrame extends JFrame implements HostListener, PropertiesListener +public class MainFrame extends JFrame implements HostListener, PropertiesListener, ConsoleListener { private final String APP_NAME = "Hoster"; private final String APP_VERSION = "0.1.0"; + private final Color ERROR_COLOR = new Color(220, 0, 0); + private final Color INFO_COLOR = UIManager.getColor("TextArea.foreground"); + private final int SERVER_ACTIVED = 1; private final int SERVER_STOPPED = 2; private final int SERVER_RESTARTING = 3; - private List hostsList; + private Properties properties; + private HList hostsList; + private Server server; + private StyledDocument styledDocument; + private Style consoleStyle; + private JPanel domainPane; private JButton addHostBtn; private JButton editHostBtn; @@ -36,17 +52,26 @@ public class HostFrame extends JFrame implements HostListener, PropertiesListene private JButton mainConfigBtn; private JButton aboutBtn; private JTable hostsTable; + private JPanel consolePane; + private JTextPane consoleLog; private JButton restartServerBtn; private JLabel serverStatus; private JButton serverStatsBtn; - public HostFrame(List hl, Properties prop) + public MainFrame(Properties prop, HList hl) { - hostsList = hl; properties = prop; + hostsList = hl; + + server = new Server(); + server.addConsoleListener(this); + + styledDocument = consoleLog.getStyledDocument(); + consoleStyle = consoleLog.addStyle("Console Style", null); setContentPane(domainPane); updateHostsTable(); + updateConsolePane(); updateServerStatus(); // Call onAddHostDialog() on CTRL+N @@ -86,28 +111,34 @@ public HostFrame(List hl, Properties prop) restartServerBtn.addActionListener(e -> onRestartServer(true)); serverStatsBtn.addActionListener(e -> onServerStats()); -// addWindowFocusListener(new WindowFocusListener() -// { -// @Override -// public void windowGainedFocus(WindowEvent windowEvent) -// { -// updateServerStatus(); -// } -// -// @Override -// public void windowLostFocus(WindowEvent windowEvent) -// { -// -// } -// }); + addWindowFocusListener(new WindowFocusListener() + { + @Override + public void windowGainedFocus(WindowEvent windowEvent) + { + updateServerStatus(false); + } + + @Override + public void windowLostFocus(WindowEvent windowEvent) + { + + } + }); } public void build() { + String[] sizes = {"16", "24", "32", "64", "128", "256", "512"}; + List icons = new ArrayList<>(); + + for (String size : sizes) { + icons.add(new ImageIcon(getClass().getResource("icons/logo/logo_" + size + ".png")).getImage()); + } + pack(); setTitle(APP_NAME + " " + APP_VERSION); - setIconImage(new ImageIcon(getClass().getResource("icons/icon.png")).getImage()); - setResizable(false); + setIconImages(icons); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(DISPOSE_ON_CLOSE); @@ -116,25 +147,48 @@ public void build() protected void updateHostsTable() { HostsTableModel tableModel = new HostsTableModel(); + tableModel.addColumn("Status"); tableModel.addColumn("IP"); tableModel.addColumn("Domain"); + tableModel.addColumn("Details"); hostsTable.setModel(tableModel); hostsTable.setDefaultRenderer(Object.class, new HostsTableRenderer(hostsList)); hostsTable.getTableHeader().setReorderingAllowed(false); hostsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + hostsTable.getColumnModel().getColumn(0).setMaxWidth(50); + hostsTable.getColumnModel().getColumn(1).setMaxWidth(200); + for (Host host : hostsList) { - tableModel.addRow(new Object[]{host.getIp(), host.getDomain()}); + tableModel.addRow(new Object[]{host.getStatus(), host.getIp(), host.getServerName()}); } } + protected void updateConsolePane() + { + consolePane.setVisible(properties.getBoolean("console_log")); + } + protected void updateServerStatus() { - if (Server.isActive()) { + updateServerStatus(true); + } + + protected void updateServerStatus(boolean notifyConsole) + { + if (server.isActive()) { setServerStatus(SERVER_ACTIVED); + + if (notifyConsole) { + onConsoleInfo("Server active"); + } } else { setServerStatus(SERVER_STOPPED); + + if (notifyConsole) { + onConsoleError("Server stopped"); + } } } @@ -161,9 +215,69 @@ protected void setServerStatus(int status) serverStatus.setForeground(color); } + protected void saveHostFile() + { + boolean fileSaved = properties.getHostsFile().save( + properties.getString("hosts_file"), + hostsList, + APP_NAME, + APP_VERSION + ); + + if (!fileSaved) { + JOptionPane.showMessageDialog( + this, + "There was an error trying to update host file. Make sure the application has the necessary privileges.", + "Host file update error", + JOptionPane.ERROR_MESSAGE + ); + } + } + + protected void saveVHostFile() + { + boolean fileSaved = properties.getVHostsFile().save( + properties.getString("vhosts_file"), + properties.getMainDirectory(), + hostsList, + APP_NAME, + APP_VERSION + ); + + if (!fileSaved) { + JOptionPane.showMessageDialog( + this, + "There was an error trying to update virtual-host file. Make sure the application has the necessary privileges.", + "Virtual-Host file update error", + JOptionPane.ERROR_MESSAGE + ); + } + } + + protected void savePropertiesFile() + { + boolean fileSaved = properties.getPropertiesFile().save(properties); + + if (!fileSaved) { + JOptionPane.showMessageDialog( + this, + "An error occurred while trying to save the properties file. Please make sure the application has the necessary privileges.", + "Properties file error", + JOptionPane.ERROR_MESSAGE); + } + } + + protected String getCurrentTime() + { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime now = LocalDateTime.now(); + + return dtf.format(now); + } + protected void onAddHostDialog() { - AddHostDialog dialog = new AddHostDialog(this); + HostDialog dialog = new HostDialog(this, null, 0); dialog.addHostListener(this); dialog.build(); } @@ -172,7 +286,7 @@ protected void onEditHostDialog() { int row = hostsTable.getSelectedRow(); if (row > -1) { - EditHostDialog dialog = new EditHostDialog(this, hostsList.get(row), row); + HostDialog dialog = new HostDialog(this, hostsList.get(row), row); dialog.addHostListener(this); dialog.build(); } else { @@ -209,7 +323,7 @@ protected void onPhpConfigDialog() protected void onMainConfigDialog() { - PropertiesDialog dialog = new PropertiesDialog(this, properties); + PropertiesDialog dialog = new PropertiesDialog(this, properties, server.getOS()); dialog.addConfigListener(this); dialog.build(); } @@ -217,6 +331,7 @@ protected void onMainConfigDialog() protected void onAboutDialog() { AboutDialog dialog = new AboutDialog(this); + dialog.addConsoleListener(this); dialog.build(); } @@ -228,8 +343,9 @@ protected void onRestartServer() protected void onRestartServer(boolean force) { if (force || properties.getBoolean("restart_server")) { + onConsoleInfo("Restarting server..."); setServerStatus(SERVER_RESTARTING); - Server.restart(properties.getString("apache_path")); + server.restart(properties.getString("apache_path")); updateServerStatus(); } } @@ -243,18 +359,9 @@ protected void onServerStats() public void onHostAdded(Host host) { hostsList.add(host); - - if (HostsFile.save(properties.getString("hosts_file"), hostsList, APP_NAME, APP_VERSION)) { - updateHostsTable(); - onRestartServer(); - } else { - JOptionPane.showMessageDialog( - this, - "There was an error trying to update host file. Make sure the application has the necessary privileges.", - "Host file update error", - JOptionPane.ERROR_MESSAGE - ); - } + saveHostFile(); + updateHostsTable(); + onRestartServer(); } @Override @@ -262,18 +369,10 @@ public void onHostEdited(Host host, int row) { hostsList.remove(row); hostsList.add(row, host); - - if (HostsFile.save(properties.getString("hosts_file"), hostsList, APP_NAME, APP_VERSION)) { - updateHostsTable(); - onRestartServer(); - } else { - JOptionPane.showMessageDialog( - this, - "There was an error trying to update host file. Make sure the application has the necessary privileges.", - "Host file update error", - JOptionPane.ERROR_MESSAGE - ); - } + saveHostFile(); + saveVHostFile(); + updateHostsTable(); + onRestartServer(); } @Override @@ -290,26 +389,10 @@ public void onHostDeleted() if (option == JOptionPane.YES_OPTION) { hostsList.remove(row); - if (HostsFile.save(properties.getString("hosts_file"), hostsList, APP_NAME, APP_VERSION)) { - if (VHostsFile.save(properties.getString("vhosts_file"), hostsList, APP_NAME, APP_VERSION)) { - updateHostsTable(); - onRestartServer(); - } else { - JOptionPane.showMessageDialog( - this, - "There was an error trying to update virtual-host file. Make sure the application has the necessary privileges.", - "Virtual-Host file update error", - JOptionPane.ERROR_MESSAGE - ); - } - } else { - JOptionPane.showMessageDialog( - this, - "There was an error trying to update host file. Make sure the application has the necessary privileges.", - "Host file update error", - JOptionPane.ERROR_MESSAGE - ); - } + saveHostFile(); + saveVHostFile(); + updateHostsTable(); + onRestartServer(); } } else { JOptionPane.showMessageDialog( @@ -322,35 +405,42 @@ public void onHostDeleted() } @Override - public void onVirtualHostUpdated(Host host, int row) + public void onPropertiesUpdate(Map propertiesMap) { - hostsList.remove(row); - hostsList.add(row, host); + properties.setPropertiesMap(propertiesMap); + savePropertiesFile(); + updateConsolePane(); + onRestartServer(); + } - if (VHostsFile.save(properties.getString("vhosts_file"), hostsList, properties.getMainDirectory(), APP_NAME, APP_VERSION)) { - onRestartServer(); - } else { + @Override + public void onConsoleInfo(String info) + { + StyleConstants.setForeground(consoleStyle, INFO_COLOR); + + try { + styledDocument.insertString(styledDocument.getLength(), getCurrentTime() + " - " + info + "\n", consoleStyle); + } catch (BadLocationException e) { JOptionPane.showMessageDialog( this, - "There was an error trying to update virtual-host file. Make sure the application has the necessary privileges.", - "Host file update error", - JOptionPane.ERROR_MESSAGE - ); + e.getMessage(), + "Console error", + JOptionPane.ERROR_MESSAGE); } } @Override - public void onPropertiesUpdate(Map propertiesMap) + public void onConsoleError(String error) { - properties.setPropertiesMap(propertiesMap); + StyleConstants.setForeground(consoleStyle, ERROR_COLOR); - if (PropertiesFile.save(properties)) { - onRestartServer(); - } else { + try { + styledDocument.insertString(styledDocument.getLength(), getCurrentTime() + " - " + error + "\n", consoleStyle); + } catch (BadLocationException e) { JOptionPane.showMessageDialog( this, - "An error occurred while trying to save the properties file. Please make sure the application has the necessary privileges.", - "Properties file error", + e.getMessage(), + "Console error", JOptionPane.ERROR_MESSAGE); } } diff --git a/src/com/hoster/gui/PropertiesDialog.form b/src/com/hoster/gui/PropertiesDialog.form index 5fdda1a..18bd0b1 100644 --- a/src/com/hoster/gui/PropertiesDialog.form +++ b/src/com/hoster/gui/PropertiesDialog.form @@ -1,9 +1,9 @@
- + - + @@ -80,7 +80,7 @@ - + @@ -148,7 +148,7 @@ - + @@ -179,7 +179,7 @@ - + @@ -230,6 +230,14 @@ + + + + + + + + diff --git a/src/com/hoster/gui/PropertiesDialog.java b/src/com/hoster/gui/PropertiesDialog.java index 52822fb..1a9118e 100644 --- a/src/com/hoster/gui/PropertiesDialog.java +++ b/src/com/hoster/gui/PropertiesDialog.java @@ -2,7 +2,6 @@ import com.hoster.data.OperatingSystem; import com.hoster.data.Properties; -import com.hoster.data.Server; import com.hoster.gui.listeners.PropertiesListener; import javax.swing.*; @@ -17,11 +16,13 @@ public class PropertiesDialog extends JDialog { private Properties properties; private PropertiesListener propertiesListener; + private JFrame parent; private JPanel configPane; private JComboBox theme; private JTextField hostsFile; private JButton findHostsFile; + private JCheckBox consoleLog; private JTextField vhostsFile; private JButton findVhostFile; private JTextField directoryPath; @@ -35,7 +36,7 @@ public class PropertiesDialog extends JDialog private JButton accept; private JButton cancel; - public PropertiesDialog(JFrame p, Properties prop) + public PropertiesDialog(JFrame p, Properties prop, OperatingSystem os) { parent = p; properties = prop; @@ -65,7 +66,7 @@ public void windowClosing(WindowEvent e) // Call onCancel() on ESCAPE configPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); - if (Server.getOS() != OperatingSystem.WINDOWS) { + if (os != OperatingSystem.WINDOWS) { apachePane.setVisible(false); } } @@ -91,6 +92,7 @@ protected void setPropertiesConfig() theme.setSelectedIndex(properties.getString("theme").equals("light") ? 0 : 1); hostsFile.setText(properties.getString("hosts_file")); vhostsFile.setText(properties.getString("vhosts_file")); + consoleLog.setSelected(properties.getBoolean("console_log")); directoryPath.setText(properties.getMainDirectory().getPath()); require.setText(properties.getMainDirectory().getRequire()); allowOverride.setText(properties.getMainDirectory().getAllowOverride()); @@ -115,6 +117,7 @@ private void onFind(JTextField field, int mode) fileChooser.setFileSelectionMode(mode); fileChooser.setCurrentDirectory(new File(field.getText())); int selection = fileChooser.showOpenDialog(field); + if (selection == JFileChooser.APPROVE_OPTION) { field.setText(fileChooser.getSelectedFile().getAbsolutePath()); } @@ -126,6 +129,7 @@ protected void onAccept() propertiesMap.put("theme", theme.getSelectedItem().toString().toLowerCase()); propertiesMap.put("hosts_file", hostsFile.getText()); propertiesMap.put("vhosts_file", vhostsFile.getText()); + propertiesMap.put("console_log", consoleLog.isSelected() ? "1" : "0"); propertiesMap.put("directory_path", directoryPath.getText()); propertiesMap.put("directory_require", require.getText()); propertiesMap.put("directory_allow_override", allowOverride.getText()); diff --git a/src/com/hoster/gui/VHostDialog.java b/src/com/hoster/gui/VHostDialog.java index 7d087dd..ac8673d 100644 --- a/src/com/hoster/gui/VHostDialog.java +++ b/src/com/hoster/gui/VHostDialog.java @@ -10,9 +10,10 @@ public class VHostDialog extends JDialog { - private HostListener hostListener; private Host host; private int hostPosition; + private HostListener hostListener; + private JFrame parent; private JPanel vhostPane; private JTextField serverAdmin; @@ -29,12 +30,14 @@ public class VHostDialog extends JDialog public VHostDialog(JFrame p, Host h, int pos) { - setContentPane(vhostPane); - getRootPane().setDefaultButton(accept); - parent = p; host = h; hostPosition = pos; + + setContentPane(vhostPane); + getRootPane().setDefaultButton(accept); + setVHostConfig(); + accept.addActionListener(e -> onAccept()); cancel.addActionListener(e -> onCancel()); @@ -68,6 +71,19 @@ public void build() setVisible(true); } + protected void setVHostConfig() + { + serverAdmin.setText(host.getServerAdmin()); + serverName.setText(host.getServerName()); + documentRoot.setText(host.getDocumentRoot()); + serverAlias.setText(host.getServerAlias()); + port.setText(host.getPort()); + errorLog.setText(host.getErrorLog()); + customLog.setText(host.getCustomLog()); + require.setText(host.getDirectory().getRequire()); + allowOverride.setText(host.getDirectory().getAllowOverride()); + } + protected void onAccept() { if (fieldsFilled()) { @@ -81,7 +97,7 @@ protected void onAccept() host.getDirectory().setRequire(require.getText()); host.getDirectory().setAllowOverride(allowOverride.getText()); - hostListener.onVirtualHostUpdated(host, hostPosition); + hostListener.onHostEdited(host, hostPosition); dispose(); } else { JOptionPane.showMessageDialog( @@ -99,7 +115,7 @@ protected void onCancel() protected boolean fieldsFilled() { - return !serverName.getText().equals("") - || !documentRoot.getText().equals(""); + return !serverName.getText().isEmpty() + && !documentRoot.getText().isEmpty(); } } diff --git a/src/com/hoster/gui/icons/all_ok.png b/src/com/hoster/gui/icons/all_ok.png new file mode 100644 index 0000000..5644ac5 Binary files /dev/null and b/src/com/hoster/gui/icons/all_ok.png differ diff --git a/src/com/hoster/gui/icons/icon.png b/src/com/hoster/gui/icons/icon.png deleted file mode 100644 index db48cbe..0000000 Binary files a/src/com/hoster/gui/icons/icon.png and /dev/null differ diff --git a/src/com/hoster/gui/icons/inactive.png b/src/com/hoster/gui/icons/inactive.png new file mode 100644 index 0000000..29be567 Binary files /dev/null and b/src/com/hoster/gui/icons/inactive.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_128.png b/src/com/hoster/gui/icons/logo/logo_128.png new file mode 100644 index 0000000..9dbf941 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_128.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_16.png b/src/com/hoster/gui/icons/logo/logo_16.png new file mode 100644 index 0000000..8fb9db0 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_16.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_24.png b/src/com/hoster/gui/icons/logo/logo_24.png new file mode 100644 index 0000000..4b7af68 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_24.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_256.png b/src/com/hoster/gui/icons/logo/logo_256.png new file mode 100644 index 0000000..71d7305 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_256.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_32.png b/src/com/hoster/gui/icons/logo/logo_32.png new file mode 100644 index 0000000..0203d72 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_32.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_512.png b/src/com/hoster/gui/icons/logo/logo_512.png new file mode 100644 index 0000000..ae18f79 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_512.png differ diff --git a/src/com/hoster/gui/icons/logo/logo_64.png b/src/com/hoster/gui/icons/logo/logo_64.png new file mode 100644 index 0000000..f5750e0 Binary files /dev/null and b/src/com/hoster/gui/icons/logo/logo_64.png differ diff --git a/src/com/hoster/gui/icons/no_domain.png b/src/com/hoster/gui/icons/no_domain.png new file mode 100644 index 0000000..7736d69 Binary files /dev/null and b/src/com/hoster/gui/icons/no_domain.png differ diff --git a/src/com/hoster/gui/icons/no_vhost.png b/src/com/hoster/gui/icons/no_vhost.png new file mode 100644 index 0000000..e367141 Binary files /dev/null and b/src/com/hoster/gui/icons/no_vhost.png differ diff --git a/src/com/hoster/gui/listeners/ConsoleListener.java b/src/com/hoster/gui/listeners/ConsoleListener.java new file mode 100644 index 0000000..ff9667b --- /dev/null +++ b/src/com/hoster/gui/listeners/ConsoleListener.java @@ -0,0 +1,8 @@ +package com.hoster.gui.listeners; + +public interface ConsoleListener +{ + void onConsoleInfo(String info); + + void onConsoleError(String error); +} diff --git a/src/com/hoster/gui/listeners/HostListener.java b/src/com/hoster/gui/listeners/HostListener.java index 9dc5644..50e9f01 100644 --- a/src/com/hoster/gui/listeners/HostListener.java +++ b/src/com/hoster/gui/listeners/HostListener.java @@ -5,7 +5,8 @@ public interface HostListener { void onHostAdded(Host host); + void onHostEdited(Host host, int row); + void onHostDeleted(); - void onVirtualHostUpdated(Host host, int row); }