Search in sources :

Example 1 with JXLoginFrame

use of org.jdesktop.swingx.JXLoginPane.JXLoginFrame in project ramus by Vitaliy-Yakovchuk.

the class TcpLightClient method start.

protected void start(final String[] args) throws Exception {
    if (args.length < 2) {
        System.err.println("Usage java -jar ... url ..., for example: java -jar my.jar localhost 38617 ");
        return;
    }
    try {
        String lookAndFeel = Options.getString("LookAndFeel");
        if (lookAndFeel != null)
            UIManager.setLookAndFeel(lookAndFeel);
        else {
            if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(UIManager.getSystemLookAndFeelClassName()))
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            else
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        UIManager.put("swing.boldMetal", Boolean.FALSE);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    StandardAttributesPlugin.setDefaultDisableAutoupdate(true);
    this.host = args[0];
    this.port = args[1];
    try {
        final PrintStream old = System.err;
        System.setErr(new PrintStream(new OutputStream() {

            FileOutputStream fos = null;

            private boolean err = false;

            @Override
            public void write(final int b) throws IOException {
                getFos();
                if (!err)
                    fos.write(b);
                old.write(b);
            }

            private FileOutputStream getFos() throws IOException {
                if (fos == null) {
                    try {
                        System.out.println("Getting calendar");
                        final Calendar c = Calendar.getInstance();
                        System.out.println("Getting options path");
                        String name = System.getProperty("user.home");
                        if (!name.equals(File.separator))
                            name += File.separator;
                        name += ".ramus" + File.separator + "log";
                        System.out.println("Creating dir: " + name);
                        new File(name).mkdirs();
                        name += File.separator + c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.HOUR_OF_DAY) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND) + "_" + c.get(Calendar.MILLISECOND) + "-client.log";
                        fos = new FileOutputStream(name);
                    } catch (final Exception e) {
                        err = true;
                        e.printStackTrace(System.out);
                    // throw e;
                    }
                }
                return fos;
            }
        }));
        connection = new TcpClientConnection(args[0], Integer.parseInt(args[1])) {

            private boolean exitShown = false;

            @Override
            protected void objectReaded(Object object) {
                if (tcpClientEngine != null)
                    tcpClientEngine.call((EvenstHolder) object);
            }

            @Override
            protected void showDialogEndExit(String message) {
                if (exitShown)
                    return;
                exitShown = true;
                System.err.println("Connection lost");
                // JOptionPane.showMessageDialog(framework.getMainFrame(),
                // message);
                System.exit(1);
            }
        };
        connection.start();
        Boolean canLogin = (Boolean) connection.invoke("canLogin", new Object[] {});
        if (!canLogin) {
            ResourceBundle bundle = ResourceBundle.getBundle("com.ramussoft.client.client");
            JOptionPane.showMessageDialog(null, bundle.getString("Message.ServerBusy"));
            System.exit(1);
            return;
        }
        final JXLoginFrame frame = JXLoginPane.showLoginFrame(new LoginService() {

            @Override
            public boolean authenticate(String name, char[] passwordChars, String server) throws Exception {
                String password = new String(passwordChars);
                try {
                    sessionId = (Long) connection.invoke("login", new Object[] { name, password });
                    if ((sessionId == null) || (sessionId.longValue() < 0l)) {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
                return sessionId != null;
            }

            @Override
            public void cancelAuthentication() {
                System.exit(0);
            }
        });
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/ramussoft/gui/application.png")));
        frame.setVisible(true);
        frame.addPropertyChangeListener("status", new PropertyChangeListener() {

            private boolean run = false;

            {
                Thread t = new Thread() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(120000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (!run)
                            System.exit(0);
                    }
                };
                t.start();
            }

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                Status status = frame.getStatus();
                if ((status.equals(Status.CANCELLED)) || (status.equals(Status.NOT_STARTED)) || (status.equals(Status.FAILED)) || (status.equals(Status.IN_PROGRESS))) {
                    return;
                }
                if (run)
                    return;
                run = true;
                userProvider = (UserProvider) createDeligate(UserProvider.class);
                if (season) {
                    boolean exit = true;
                    for (Group g : getMe().getGroups()) {
                        if (("admin".equals(g.getName())) || ("season".equals(g.getName()))) {
                            exit = false;
                        }
                    }
                    if (exit) {
                        JOptionPane.showMessageDialog(null, "Тільки користувач групи season або admin може працювати з системою планування");
                        System.exit(5);
                        return;
                    }
                }
                rules = (AccessRules) createDeligate(AccessRules.class);
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        TcpLightClient.this.run(args);
                    }
                });
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "Неможливо з’єднатись з сервером, дивіться log-файл для деталей " + e.getLocalizedMessage());
        System.exit(1);
    }
}
Also used : Group(com.ramussoft.net.common.Group) PropertyChangeListener(java.beans.PropertyChangeListener) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JXLoginFrame(org.jdesktop.swingx.JXLoginPane.JXLoginFrame) UserProvider(com.ramussoft.net.common.UserProvider) Status(org.jdesktop.swingx.JXLoginPane.Status) PrintStream(java.io.PrintStream) PropertyChangeEvent(java.beans.PropertyChangeEvent) Calendar(java.util.Calendar) IOException(java.io.IOException) LoginService(org.jdesktop.swingx.auth.LoginService) FileOutputStream(java.io.FileOutputStream) AccessRules(com.ramussoft.common.AccessRules) ResourceBundle(java.util.ResourceBundle) File(java.io.File)

Aggregations

AccessRules (com.ramussoft.common.AccessRules)1 Group (com.ramussoft.net.common.Group)1 UserProvider (com.ramussoft.net.common.UserProvider)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Calendar (java.util.Calendar)1 ResourceBundle (java.util.ResourceBundle)1 JXLoginFrame (org.jdesktop.swingx.JXLoginPane.JXLoginFrame)1 Status (org.jdesktop.swingx.JXLoginPane.Status)1 LoginService (org.jdesktop.swingx.auth.LoginService)1