diff --git a/settings.properties b/settings.properties new file mode 100644 index 0000000..83df68b --- /dev/null +++ b/settings.properties @@ -0,0 +1,5 @@ +#3306 +#Wed Feb 20 08:05:50 COT 2019 +port=3306 +hostname=localhost +daemon=true diff --git a/src/technomotica/java/forms/Configuration.form b/src/technomotica/java/forms/Configuration.form index 3c624df..5524dd7 100644 --- a/src/technomotica/java/forms/Configuration.form +++ b/src/technomotica/java/forms/Configuration.form @@ -25,6 +25,16 @@ + + + + + + + + + + @@ -32,25 +42,27 @@ - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - @@ -62,9 +74,19 @@ - - - + + + + + + + + + + + + + @@ -86,7 +108,7 @@ - + @@ -111,6 +133,9 @@ + + + @@ -125,5 +150,23 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/technomotica/java/forms/Configuration.java b/src/technomotica/java/forms/Configuration.java index 3a221cb..6647d94 100644 --- a/src/technomotica/java/forms/Configuration.java +++ b/src/technomotica/java/forms/Configuration.java @@ -7,7 +7,9 @@ import java.awt.Image; import javax.swing.ImageIcon; +import javax.swing.JOptionPane; import javax.swing.UIManager; +import technomotica.objs.Config; /** * @@ -15,6 +17,8 @@ */ public class Configuration extends javax.swing.JDialog { + private Config cfg = null; + public String VERSION = "Versión 0.1"; /** @@ -28,6 +32,18 @@ public Configuration(java.awt.Frame parent, boolean modal) { setIconImage(new ImageIcon("src/technomotica/media/L4.png").getImage()); setLocationRelativeTo(null); + + cfg = new Config(); + if(cfg.getConfigKey("daemon").equalsIgnoreCase("false")) { + tEjecucion2do.setText("Activar"); + tEjecucion2do.setSelected(false); + } + else { + tEjecucion2do.setText("Desactivar"); + tEjecucion2do.setSelected(true); + } + txtHostname.setText(cfg.getConfigKey("hostname")); + tHostPort.setText(cfg.getConfigKey("port")); } /** @@ -46,6 +62,10 @@ private void initComponents() { jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); + jLabel4 = new javax.swing.JLabel(); + txtHostname = new javax.swing.JTextField(); + jLabel5 = new javax.swing.JLabel(); + tHostPort = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Configuración - Tech Domotica"); @@ -57,7 +77,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { } }); - jLabel1.setText("Ejecución en segundo plano: La ejecución en segundo plano permitirá que la aplicación se siga ejecutando sin estar en tu barra de tareas."); + jLabel1.setText("Dirección/Nombre del host:"); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); @@ -72,6 +92,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }); jButton2.setText("Guardar configuración"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); jButton3.setText("Restablecer predeterminados"); jButton3.addActionListener(new java.awt.event.ActionListener() { @@ -82,31 +107,46 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jLabel3.setText("Ejecución en segundo plano: La ejecución en segundo plano permitirá que la aplicación se siga ejecutando sin estar en tu barra de tareas."); + jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N + jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel4.setText("Configuración avanzada"); + + jLabel5.setText("Puerto de escucha del host:"); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addGap(23, 23, 23) + .addComponent(jButton2) + .addGap(18, 18, 18) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(jButton3) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(tEjecucion2do, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() - .addGap(23, 23, 23) - .addComponent(jButton2) - .addGap(18, 18, 18) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) - .addComponent(jButton3) - .addGap(0, 0, Short.MAX_VALUE))) + .addGap(20, 20, 20) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(txtHostname)) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel5) + .addGap(89, 89, 89) + .addComponent(tHostPort))))) .addContainerGap()) - .addGroup(layout.createSequentialGroup() - .addGap(20, 20, 20) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -116,9 +156,17 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tEjecucion2do) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(42, 42, 42) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel4) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel5) + .addComponent(tHostPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) @@ -138,18 +186,44 @@ private void tEjecucion2doActionPerformed(java.awt.event.ActionEvent evt) {//GEN // TODO add your handling code here: if(tEjecucion2do.isSelected()) { tEjecucion2do.setText("Desactivar"); + tEjecucion2do.setSelected(true); } else { tEjecucion2do.setText("Activar"); + tEjecucion2do.setSelected(false); } }//GEN-LAST:event_tEjecucion2doActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: - + int confirm = JOptionPane.showConfirmDialog(null, "¿Estás seguro de que deseas reestablecer la configuración predeterminada?", "Reestablecer configuración", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) { + cfg.cambiarConfigKey("daemon", "true"); + cfg.cambiarConfigKey("hostname", "localhost"); + cfg.cambiarConfigKey("port", "3306"); + cfg.closeConfigFile(); + JOptionPane.showMessageDialog(null, "Configuración reestablecida.", "¡Éxito en la operación!", JOptionPane.INFORMATION_MESSAGE); + this.dispose(); + } }//GEN-LAST:event_jButton3ActionPerformed + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + // TODO add your handling code here: + if(!tHostPort.getText().isEmpty() && !txtHostname.getText().isEmpty()) { + int confirm = JOptionPane.showConfirmDialog(null, "¿Estás seguro de que deseas guardar esta configuración?", "Guardar configuración", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) { + cfg.cambiarConfigKey("daemon", (tEjecucion2do.isSelected()) ? "true" : "false"); + cfg.cambiarConfigKey("port", tHostPort.getText()); + cfg.cambiarConfigKey("hostname", txtHostname.getText()); + cfg.closeConfigFile(); + JOptionPane.showMessageDialog(null, "Configuración guardada.", "¡Éxito en la operación!", JOptionPane.INFORMATION_MESSAGE); + this.dispose(); + } + } + else JOptionPane.showMessageDialog(null, "Uno de los campos de configuración está vacío.", "No se puede guardar", JOptionPane.ERROR_MESSAGE); + }//GEN-LAST:event_jButton2ActionPerformed + /** * @param args the command line arguments */ @@ -195,6 +269,10 @@ public void windowClosing(java.awt.event.WindowEvent e) { private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JLabel jLabel5; private javax.swing.JToggleButton tEjecucion2do; + private javax.swing.JTextField tHostPort; + private javax.swing.JTextField txtHostname; // End of variables declaration//GEN-END:variables } diff --git a/src/technomotica/java/forms/LoginPage.form b/src/technomotica/java/forms/LoginPage.form index c368f6a..62c1589 100644 --- a/src/technomotica/java/forms/LoginPage.form +++ b/src/technomotica/java/forms/LoginPage.form @@ -61,7 +61,7 @@ - + @@ -148,7 +148,7 @@ - + diff --git a/src/technomotica/java/forms/LoginPage.java b/src/technomotica/java/forms/LoginPage.java index 6108edd..11573de 100644 --- a/src/technomotica/java/forms/LoginPage.java +++ b/src/technomotica/java/forms/LoginPage.java @@ -7,7 +7,7 @@ import javax.swing.JOptionPane; import javax.swing.UIManager; -import technomotica.java.forms.About; +//import technomotica.java.forms.About; public class LoginPage extends javax.swing.JFrame { @@ -44,7 +44,7 @@ private void initComponents() { jMenuItem1 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); - setTitle("Iniciar sesión - Technomotica"); + setTitle("Iniciar sesión - Tech Domotica"); setBackground(new java.awt.Color(197, 208, 230)); setIconImages(null); setResizable(false); @@ -56,7 +56,7 @@ public void windowClosing(java.awt.event.WindowEvent evt) { jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - jLabel1.setText("Iniciar sesión en TechDomotica"); + jLabel1.setText("Iniciar sesión en Tech Domotica"); jLabel3.setText("Nombre de usuario: "); diff --git a/src/technomotica/java/forms/Main.java b/src/technomotica/java/forms/Main.java index ca91e04..4f0c602 100644 --- a/src/technomotica/java/forms/Main.java +++ b/src/technomotica/java/forms/Main.java @@ -15,13 +15,15 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.awt.event.WindowAdapter; import javax.swing.ImageIcon; -import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; +import technomotica.objs.Config; /** - * + * GLITCH: Al salir del dialog de Configuration.java e intentar refrescar la configuración + * no se logra recargar. * @author Andres */ public class Main extends javax.swing.JFrame { @@ -32,6 +34,8 @@ public class Main extends javax.swing.JFrame { public boolean onSystemTray = false; private TrayIcon appSystemTray = null; + private Config cfg = new Config(); + public Main() { initComponents(); setLocationRelativeTo(null); @@ -219,9 +223,11 @@ private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing // TODO add your handling code here: - addAppToSystemTray(); - this.setVisible(false); - //exit(); + if(cfg.getConfigKey("daemon").equalsIgnoreCase("true")) { + addAppToSystemTray(); + this.setVisible(false); + } + else exit(); }//GEN-LAST:event_formWindowClosing private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed @@ -239,11 +245,20 @@ private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI public void openConfig() { Configuration cfg = new Configuration(Main.this, true); + cfg.addWindowListener(new WindowAdapter() { + public void windowClosed() { + //Main.this.cfg.refrescarFile(); + int confirm = JOptionPane.showConfirmDialog(null, "Tech Domotica debe de ser reiniciado para que los cambios surtan efecto.\n¿Deseas hacerlo ahora?", "Reinicio requerido", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) { + System.exit(0); + } + } + }); cfg.setVisible(true); } public void exit() { - int confirm = JOptionPane.showConfirmDialog(null, "¿Estás seguro de salir de Technomotica?", "Confirmación", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + int confirm = JOptionPane.showConfirmDialog(null, "¿Estás seguro de salir de Tech Domotica?", "Confirmación de salida", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(confirm == JOptionPane.YES_OPTION) { System.exit(0); } diff --git a/src/technomotica/media/L1.png b/src/technomotica/media/L1.png index 4f0bd2b..2d9ea3d 100644 Binary files a/src/technomotica/media/L1.png and b/src/technomotica/media/L1.png differ diff --git a/src/technomotica/media/L4.png b/src/technomotica/media/L4.png index 786ca4c..4ec69df 100644 Binary files a/src/technomotica/media/L4.png and b/src/technomotica/media/L4.png differ diff --git a/src/technomotica/objs/Conectar.java b/src/technomotica/objs/Conectar.java index 50dbedd..c5779be 100644 --- a/src/technomotica/objs/Conectar.java +++ b/src/technomotica/objs/Conectar.java @@ -9,7 +9,7 @@ public class Conectar { private static final String driver = "com.mysql.jdbc.Driver", usuario = "root", contrasena = "", - url = "jdbc:mysql://localhost:3306/technomotica"; + url = "jdbc:mysql://" + new Config().getConfigKey("hostname") + ":" + new Config().getConfigKey("port") +"/technomotica"; //Hay que reemplazar la base de datos de url. /*private static final String driver = "com.mysql.jdbc.Driver"; diff --git a/src/technomotica/objs/Config.java b/src/technomotica/objs/Config.java index 5072628..bd9e04f 100644 --- a/src/technomotica/objs/Config.java +++ b/src/technomotica/objs/Config.java @@ -6,17 +6,30 @@ public class Config { - private static Properties configHandler = null; - private static boolean configReaden = false; + private Properties configHandler = null; + private boolean configReaden = false; private String[] configKeys = null; - public static void openConfigFile() { + /** + * Crea una instancia de este objeto para leer las propiedades del archivo de configuración. + */ + public Config() { + openConfigFile(); + } + + /** + * Es un método que permite abrir el archivo de configuración. + * Debe de ejecutarse para poder leer propiedades de settings.properties. + * Este método se ejecuta por defecto en el método constructor de Config. + */ + public void openConfigFile() { if(configReaden == false) { try { - FileInputStream input = new FileInputStream("config.properties"); + FileInputStream input = new FileInputStream("settings.properties"); configHandler = new Properties(); configHandler.load(input); + configReaden = true; input.close(); } catch(Exception e) { @@ -26,6 +39,11 @@ public static void openConfigFile() { else System.out.println("Ya hay un archivo de configuración abierto."); } + /*** + * Obtiene una propiedad de la lista de configuración. + * @param key La propiedad que se desea obtener en forma de String. + * @return Retorna el valor de la propiedad en forma de String. + */ public String getConfigKey(String key) { if(configReaden) return configHandler.getProperty(key); else { @@ -34,9 +52,48 @@ public String getConfigKey(String key) { } } + /** + * Cambiar un valor de propiedad y lo guarda inmediatamente. + * @param key + * @param value + */ + public void cambiarConfigKey(String key, String value) { + try { + System.out.println("Cambiando propiedad " + key + " a " + value); + configHandler.setProperty(key, value); + configHandler.store(new FileOutputStream("settings.properties"), value); + } + catch(java.io.IOException e) { + System.out.println("Error " + e); + } + } + + /** + * Este método permite refrescar el archivo de configuración. Es básicamente similar a openConfigFile. + */ + public void refrescarFile() { + if(configReaden) { + try { + FileInputStream input = new FileInputStream("settings.properties"); + configHandler = new Properties(); + configHandler.load(input); + input.close(); + } + catch(Exception e) { + System.out.println("Error: " + e); + } + } + else System.out.println("No hay un archivo de configuración abierto. Debe de haber un archivo abierto para refrescar."); + } + + /*** + * Cierra el archivo de configuración. Usar para liberar recursos. + */ public void closeConfigFile() { configHandler = null; configReaden = false; } + + } \ No newline at end of file