001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.client.windows;
020
021import java.awt.Dimension;
022import java.awt.Image;
023import java.awt.Toolkit;
024import java.awt.event.KeyEvent;
025import java.net.URL;
026import java.util.concurrent.Semaphore;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import javax.swing.ImageIcon;
032import javax.swing.JOptionPane;
033import javax.swing.SwingUtilities;
034import org.jvnet.substance.SubstanceLookAndFeel;
035import org.jvnet.substance.skin.BusinessBlackSteelSkin;
036
037
038import pt.ua.dicoogle.rGUI.client.ConnectServer;
039
040import pt.ua.dicoogle.Main;
041import pt.ua.dicoogle.core.ClientSettings;
042import pt.ua.dicoogle.server.users.HashService;
043
044
045/**
046 *
047 * @author Samuel Campos <samuelcampos@ua.pt>
048 */
049@Deprecated
050public class ConnectWindow extends javax.swing.JFrame {
051
052    private static ConnectWindow instance;
053    private static Semaphore sem = new Semaphore(1, true);
054
055    private boolean isUsingDefaultPass = false;
056
057    public static synchronized ConnectWindow getInstance()
058    {
059        try
060        {
061            sem.acquire();
062            if (instance == null)
063            {
064                instance = new ConnectWindow();
065            }
066            sem.release();
067        }
068        catch (InterruptedException ex)
069        {
070            LoggerFactory.getLogger(ConnectWindow.class).error(ex.getMessage(), ex);
071        }
072        return instance;
073    }
074
075
076    public static Image getImage(final String pathAndFileName) {
077        final URL url = Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
078        return Toolkit.getDefaultToolkit().getImage(url);
079    }
080    
081    
082    /** Creates new form ConnectWindow */
083    private ConnectWindow() {
084        
085        initComponents();
086        // change to Dicoogle Default Skin
087        SwingUtilities.invokeLater(new Runnable() {
088            public void run() {
089                SubstanceLookAndFeel.setSkin(new BusinessBlackSteelSkin());
090            }
091        });
092
093        Image image = getImage("trayicon.gif");
094        this.setIconImage(image);
095
096        // Positions the window in the center of screen
097        int width = this.getWidth();
098        int height = this.getHeight();
099        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
100        int x = (screen.width-width)/2;
101        int y = (screen.height-height)/2;
102        setBounds(x,y,width,height);
103        
104        
105        jLabel8.setVisible(false);
106
107        ClientSettings settings = ClientSettings.getInstance();
108        Host.setText(settings.getDefaultServerHost());
109        Port.setText(String.valueOf(settings.getDefaultServerPort()));
110        User.setText(settings.getDefaultUserName());
111
112        if(settings.getDefaultPassword() != null && !settings.getDefaultPassword().equals(""))
113        {
114            Pass.setText("lixo_lixo_lixo");
115            isUsingDefaultPass = true;
116            ConnectButton.grabFocus();
117        }
118        else
119            Pass.grabFocus();
120        
121        if(Main.isFixedClient()){
122            Host.setEnabled(false);
123            Port.setEnabled(false);
124            Port.setText(Main.getRemoteGUIPort() + "");
125        }
126
127        //this.setVisible(true);
128
129        // set this window visible
130       SwingUtilities.invokeLater(new Runnable()
131        {
132            @Override
133            public void run()
134            {
135                ConnectWindow.getInstance().setVisible(true);
136
137                if(ClientSettings.getInstance().getAutoConnect())
138                    ConnectWindow.getInstance().ConnectButtonActionPerformed(null);
139            }
140        });
141       
142
143    }
144
145    /** This method is called from within the constructor to
146     * initialize the form.
147     * WARNING: Do NOT modify this code. The content of this method is
148     * always regenerated by the Form Editor.
149     */
150    @SuppressWarnings("unchecked")
151    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
152    private void initComponents() {
153
154        jLabel4 = new javax.swing.JLabel();
155        Port = new javax.swing.JTextField();
156        jLabel1 = new javax.swing.JLabel();
157        ConnectButton = new javax.swing.JButton();
158        jLabel2 = new javax.swing.JLabel();
159        jLabel3 = new javax.swing.JLabel();
160        Host = new javax.swing.JTextField();
161        User = new javax.swing.JTextField();
162        Pass = new javax.swing.JPasswordField();
163        jLabel5 = new javax.swing.JLabel();
164        jLabel6 = new javax.swing.JLabel();
165        jLabel7 = new javax.swing.JLabel();
166        jLabel8 = new javax.swing.JLabel();
167
168        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
169        setTitle("Dicoogle GUI Connector");
170        setResizable(false);
171
172        jLabel4.setText("Port:");
173
174        Port.addKeyListener(new java.awt.event.KeyAdapter() {
175            public void keyPressed(java.awt.event.KeyEvent evt) {
176                PortKeyPressed(evt);
177            }
178        });
179
180        jLabel1.setText("Host:");
181
182        ConnectButton.setText("Connect");
183        ConnectButton.addActionListener(new java.awt.event.ActionListener() {
184            public void actionPerformed(java.awt.event.ActionEvent evt) {
185                ConnectButtonActionPerformed(evt);
186            }
187        });
188
189        jLabel2.setText("User:");
190
191        jLabel3.setText("Pass:");
192
193        Host.addKeyListener(new java.awt.event.KeyAdapter() {
194            public void keyTyped(java.awt.event.KeyEvent evt) {
195                HostKeyTyped(evt);
196            }
197            public void keyPressed(java.awt.event.KeyEvent evt) {
198                HostKeyPressed(evt);
199            }
200        });
201
202        User.addKeyListener(new java.awt.event.KeyAdapter() {
203            public void keyPressed(java.awt.event.KeyEvent evt) {
204                UserKeyPressed(evt);
205            }
206        });
207
208        Pass.addFocusListener(new java.awt.event.FocusAdapter() {
209            public void focusGained(java.awt.event.FocusEvent evt) {
210                PassFocusGained(evt);
211            }
212        });
213        Pass.addKeyListener(new java.awt.event.KeyAdapter() {
214            public void keyPressed(java.awt.event.KeyEvent evt) {
215                PassKeyPressed(evt);
216            }
217        });
218
219        jLabel5.setFont(new java.awt.Font("Lucida Grande", 1, 17)); // NOI18N
220        jLabel5.setText("Dicoogle GUI Connector");
221
222        jLabel6.setIcon(new ImageIcon(getImage("icone_dicoogle_small.png")));
223
224        jLabel7.setText("Version 0.5ALPHA");
225
226        jLabel8.setIcon(new ImageIcon(getImage("ajax-loader.gif")));
227        jLabel8.setText("Connecting...");
228
229        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
230        getContentPane().setLayout(layout);
231        layout.setHorizontalGroup(
232            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
233            .add(layout.createSequentialGroup()
234                .addContainerGap()
235                .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 255, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
236                .add(33, 33, 33)
237                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
238                    .add(layout.createSequentialGroup()
239                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
240                            .add(layout.createSequentialGroup()
241                                .add(jLabel4)
242                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
243                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
244                                .add(layout.createSequentialGroup()
245                                    .add(jLabel8)
246                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 81, Short.MAX_VALUE)
247                                    .add(ConnectButton))
248                                .add(layout.createSequentialGroup()
249                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
250                                        .add(jLabel2)
251                                        .add(jLabel3)
252                                        .add(jLabel1))
253                                    .add(28, 28, 28)
254                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
255                                        .add(Pass, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
256                                        .add(Host, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
257                                        .add(Port, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
258                                        .add(User, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)))))
259                        .add(80, 80, 80))
260                    .add(layout.createSequentialGroup()
261                        .add(jLabel7)
262                        .addContainerGap())))
263            .add(layout.createSequentialGroup()
264                .add(202, 202, 202)
265                .add(jLabel5)
266                .addContainerGap(222, Short.MAX_VALUE))
267        );
268        layout.setVerticalGroup(
269            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
270            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
271                .add(20, 20, 20)
272                .add(jLabel5)
273                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
274                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
275                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
276                        .add(jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
277                        .addContainerGap())
278                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
279                        .add(jLabel7)
280                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
281                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
282                            .add(Host, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
283                            .add(jLabel1))
284                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
285                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
286                            .add(jLabel4)
287                            .add(Port, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
288                        .add(12, 12, 12)
289                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
290                            .add(jLabel2)
291                            .add(User, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
292                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
293                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
294                            .add(jLabel3)
295                            .add(Pass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
296                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
297                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
298                            .add(ConnectButton)
299                            .add(jLabel8))
300                        .add(37, 37, 37))))
301        );
302
303        pack();
304    }// </editor-fold>//GEN-END:initComponents
305
306    private void ConnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnectButtonActionPerformed
307        if (Host.getText().length() == 0 || Port.getText().length() == 0
308                || Pass.getPassword().length == 0 || User.getText().length() == 0)
309        {
310            JOptionPane.showMessageDialog(this, "You need to fill in all fields!",
311                    "Error", JOptionPane.INFORMATION_MESSAGE);
312            
313        }
314        else{
315            jLabel8.setVisible(true);
316            
317            try{
318                
319                ConnectServer connect;
320                try {
321                    
322                    connect = new ConnectServer(Host.getText(), Integer.valueOf(Port.getText()));
323                    
324                    String passwordHash;
325                    if(isUsingDefaultPass){
326                        passwordHash = ClientSettings.getInstance().getDefaultPassword();
327                    }
328                    else{
329                        String passPlainText = new String(Pass.getPassword());
330                        passwordHash = HashService.getSHA1Hash(passPlainText);
331                    }
332                    if (!connect.login(User.getText(), passwordHash)) {
333                        jLabel8.setVisible(false);
334                        JOptionPane.showMessageDialog(this, "The username or password is wrong!", "Wrong Username or Password", JOptionPane.ERROR_MESSAGE);
335                    }
336                    
337
338                } catch (Exception ex) {
339                   //LoggerFactory.getLogger(ConnectWindow.class).error(ex.getMessage(), ex);
340                    JOptionPane.showMessageDialog(this, "Unable to connect to the GUI Server\n"+ex.getMessage(),
341                    "Connection Error", JOptionPane.ERROR_MESSAGE);
342                    ex.printStackTrace();
343                }
344                
345            }
346            catch(NumberFormatException ex){
347                jLabel8.setVisible(false);
348
349                JOptionPane.showMessageDialog(this, "Wrong port number!",
350                    "Error", JOptionPane.ERROR_MESSAGE);
351            }
352
353        }
354}//GEN-LAST:event_ConnectButtonActionPerformed
355
356    private void HostKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_HostKeyTyped
357
358    }//GEN-LAST:event_HostKeyTyped
359
360    private void HostKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_HostKeyPressed
361        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
362            ConnectButton.doClick();
363    }//GEN-LAST:event_HostKeyPressed
364
365    private void PortKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PortKeyPressed
366        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
367            ConnectButton.doClick();
368    }//GEN-LAST:event_PortKeyPressed
369
370    private void UserKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_UserKeyPressed
371        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
372            ConnectButton.doClick();
373    }//GEN-LAST:event_UserKeyPressed
374
375    private void PassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PassKeyPressed
376        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
377            ConnectButton.doClick();
378    }//GEN-LAST:event_PassKeyPressed
379
380    private void PassFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PassFocusGained
381        if(isUsingDefaultPass){
382            Pass.setText("");
383            isUsingDefaultPass = false;
384        }
385    }//GEN-LAST:event_PassFocusGained
386
387    // Variables declaration - do not modify//GEN-BEGIN:variables
388    private javax.swing.JButton ConnectButton;
389    private javax.swing.JTextField Host;
390    private javax.swing.JPasswordField Pass;
391    private javax.swing.JTextField Port;
392    private javax.swing.JTextField User;
393    private javax.swing.JLabel jLabel1;
394    private javax.swing.JLabel jLabel2;
395    private javax.swing.JLabel jLabel3;
396    private javax.swing.JLabel jLabel4;
397    private javax.swing.JLabel jLabel5;
398    private javax.swing.JLabel jLabel6;
399    private javax.swing.JLabel jLabel7;
400    private javax.swing.JLabel jLabel8;
401    // End of variables declaration//GEN-END:variables
402
403}