Login panel desktop
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:30:37 +0200
branchdesktop
changeset 263ac802aa234fc
parent 262 6427ae4c4ef2
child 267 d52fcec12bec
Login panel
desktop/desktop-sample/pom.xml
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginAction.java
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginPanel.form
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginPanel.java
desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/layer.xml
desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties
     1.1 --- a/desktop/desktop-sample/pom.xml	Mon Sep 13 21:08:27 2010 +0200
     1.2 +++ b/desktop/desktop-sample/pom.xml	Tue Sep 14 08:30:37 2010 +0200
     1.3 @@ -97,6 +97,11 @@
     1.4            <artifactId>jettison</artifactId>
     1.5            <version>1.2</version>
     1.6          </dependency>
     1.7 +        <dependency>
     1.8 +            <groupId>org.netbeans.api</groupId>
     1.9 +            <artifactId>org-openide-dialogs</artifactId>
    1.10 +            <version>RELEASE691</version>
    1.11 +        </dependency>
    1.12      </dependencies>
    1.13      <build> 
    1.14          <plugins> 
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginAction.java	Tue Sep 14 08:30:37 2010 +0200
     2.3 @@ -0,0 +1,34 @@
     2.4 +/*
     2.5 + * To change this template, choose Tools | Templates
     2.6 + * and open the template in the editor.
     2.7 + */
     2.8 +package cz.xelfi.quoridor.desktop.sample;
     2.9 +
    2.10 +import java.awt.event.ActionEvent;
    2.11 +import java.awt.event.ActionListener;
    2.12 +import org.openide.DialogDisplayer;
    2.13 +import org.openide.NotifyDescriptor;
    2.14 +import org.openide.util.NbBundle;
    2.15 +
    2.16 +public final class LoginAction implements ActionListener {
    2.17 +
    2.18 +    @Override
    2.19 +    public void actionPerformed(ActionEvent e) {
    2.20 +        LoginPanel lp = new LoginPanel();
    2.21 +        NotifyDescriptor nd = new NotifyDescriptor(
    2.22 +            lp,
    2.23 +            NbBundle.getMessage(LoginAction.class, "LoginAction.title"),
    2.24 +            NotifyDescriptor.OK_CANCEL_OPTION,
    2.25 +            NotifyDescriptor.QUESTION_MESSAGE, null, null
    2.26 +        );
    2.27 +        Object res = DialogDisplayer.getDefault().notify(nd);
    2.28 +        if (res == NotifyDescriptor.OK_OPTION) {
    2.29 +            if (!Quoridor.getDefault().login(lp.getLogin(), lp.getPassword())) {
    2.30 +                nd = new NotifyDescriptor.Message(
    2.31 +                    NbBundle.getMessage(LoginAction.class, "LoginAction.fail")
    2.32 +                );
    2.33 +                DialogDisplayer.getDefault().notify(nd);
    2.34 +            }
    2.35 +        }
    2.36 +    }
    2.37 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginPanel.form	Tue Sep 14 08:30:37 2010 +0200
     3.3 @@ -0,0 +1,82 @@
     3.4 +<?xml version="1.1" encoding="UTF-8" ?>
     3.5 +
     3.6 +<Form version="1.5" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
     3.7 +  <AuxValues>
     3.8 +    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="1"/>
     3.9 +    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    3.10 +    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    3.11 +    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    3.12 +    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="true"/>
    3.13 +    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    3.14 +    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    3.15 +    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    3.16 +    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
    3.17 +  </AuxValues>
    3.18 +
    3.19 +  <Layout>
    3.20 +    <DimensionLayout dim="0">
    3.21 +      <Group type="103" groupAlignment="0" attributes="0">
    3.22 +          <Group type="102" attributes="0">
    3.23 +              <EmptySpace max="-2" attributes="0"/>
    3.24 +              <Group type="103" groupAlignment="0" attributes="0">
    3.25 +                  <Component id="loginLabel" min="-2" max="-2" attributes="0"/>
    3.26 +                  <Component id="passwordLabel" alignment="0" min="-2" max="-2" attributes="0"/>
    3.27 +              </Group>
    3.28 +              <EmptySpace max="-2" attributes="0"/>
    3.29 +              <Group type="103" groupAlignment="0" attributes="0">
    3.30 +                  <Component id="password" pref="289" max="32767" attributes="0"/>
    3.31 +                  <Component id="login" alignment="0" pref="289" max="32767" attributes="0"/>
    3.32 +              </Group>
    3.33 +              <EmptySpace max="-2" attributes="0"/>
    3.34 +          </Group>
    3.35 +      </Group>
    3.36 +    </DimensionLayout>
    3.37 +    <DimensionLayout dim="1">
    3.38 +      <Group type="103" groupAlignment="0" attributes="0">
    3.39 +          <Group type="102" alignment="0" attributes="0">
    3.40 +              <EmptySpace max="-2" attributes="0"/>
    3.41 +              <Group type="103" groupAlignment="3" attributes="0">
    3.42 +                  <Component id="loginLabel" alignment="3" min="-2" max="-2" attributes="0"/>
    3.43 +                  <Component id="login" alignment="3" min="-2" max="-2" attributes="0"/>
    3.44 +              </Group>
    3.45 +              <EmptySpace type="unrelated" max="-2" attributes="0"/>
    3.46 +              <Group type="103" groupAlignment="3" attributes="0">
    3.47 +                  <Component id="passwordLabel" alignment="3" min="-2" max="-2" attributes="0"/>
    3.48 +                  <Component id="password" alignment="3" min="-2" max="-2" attributes="0"/>
    3.49 +              </Group>
    3.50 +              <EmptySpace max="32767" attributes="0"/>
    3.51 +          </Group>
    3.52 +      </Group>
    3.53 +    </DimensionLayout>
    3.54 +  </Layout>
    3.55 +  <SubComponents>
    3.56 +    <Component class="javax.swing.JLabel" name="loginLabel">
    3.57 +      <Properties>
    3.58 +        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
    3.59 +          <ResourceString bundle="cz/xelfi/quoridor/desktop/sample/Bundle.properties" key="LoginPanel.loginLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
    3.60 +        </Property>
    3.61 +      </Properties>
    3.62 +    </Component>
    3.63 +    <Component class="javax.swing.JTextField" name="login">
    3.64 +      <Properties>
    3.65 +        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
    3.66 +          <ResourceString bundle="cz/xelfi/quoridor/desktop/sample/Bundle.properties" key="LoginPanel.login.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
    3.67 +        </Property>
    3.68 +      </Properties>
    3.69 +    </Component>
    3.70 +    <Component class="javax.swing.JLabel" name="passwordLabel">
    3.71 +      <Properties>
    3.72 +        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
    3.73 +          <ResourceString bundle="cz/xelfi/quoridor/desktop/sample/Bundle.properties" key="LoginPanel.passwordLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
    3.74 +        </Property>
    3.75 +      </Properties>
    3.76 +    </Component>
    3.77 +    <Component class="javax.swing.JPasswordField" name="password">
    3.78 +      <Properties>
    3.79 +        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
    3.80 +          <ResourceString bundle="cz/xelfi/quoridor/desktop/sample/Bundle.properties" key="LoginPanel.password.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
    3.81 +        </Property>
    3.82 +      </Properties>
    3.83 +    </Component>
    3.84 +  </SubComponents>
    3.85 +</Form>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/LoginPanel.java	Tue Sep 14 08:30:37 2010 +0200
     4.3 @@ -0,0 +1,86 @@
     4.4 +/*
     4.5 + * To change this template, choose Tools | Templates
     4.6 + * and open the template in the editor.
     4.7 + */
     4.8 +
     4.9 +package cz.xelfi.quoridor.desktop.sample;
    4.10 +
    4.11 +/**
    4.12 + *
    4.13 + * @author Jaroslav Tulach
    4.14 + */
    4.15 +class LoginPanel extends javax.swing.JPanel {
    4.16 +
    4.17 +    /** Creates new form LoginPanel */
    4.18 +    public LoginPanel() {
    4.19 +        initComponents();
    4.20 +    }
    4.21 +
    4.22 +    /** This method is called from within the constructor to
    4.23 +     * initialize the form.
    4.24 +     * WARNING: Do NOT modify this code. The content of this method is
    4.25 +     * always regenerated by the Form Editor.
    4.26 +     */
    4.27 +    @SuppressWarnings("unchecked")
    4.28 +    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    4.29 +    private void initComponents() {
    4.30 +
    4.31 +        loginLabel = new javax.swing.JLabel();
    4.32 +        login = new javax.swing.JTextField();
    4.33 +        passwordLabel = new javax.swing.JLabel();
    4.34 +        password = new javax.swing.JPasswordField();
    4.35 +
    4.36 +        loginLabel.setText(org.openide.util.NbBundle.getMessage(LoginPanel.class, "LoginPanel.loginLabel.text")); // NOI18N
    4.37 +
    4.38 +        login.setText(org.openide.util.NbBundle.getMessage(LoginPanel.class, "LoginPanel.login.text")); // NOI18N
    4.39 +
    4.40 +        passwordLabel.setText(org.openide.util.NbBundle.getMessage(LoginPanel.class, "LoginPanel.passwordLabel.text")); // NOI18N
    4.41 +
    4.42 +        password.setText(org.openide.util.NbBundle.getMessage(LoginPanel.class, "LoginPanel.password.text")); // NOI18N
    4.43 +
    4.44 +        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    4.45 +        this.setLayout(layout);
    4.46 +        layout.setHorizontalGroup(
    4.47 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    4.48 +            .addGroup(layout.createSequentialGroup()
    4.49 +                .addContainerGap()
    4.50 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    4.51 +                    .addComponent(loginLabel)
    4.52 +                    .addComponent(passwordLabel))
    4.53 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    4.54 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    4.55 +                    .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
    4.56 +                    .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
    4.57 +                .addContainerGap())
    4.58 +        );
    4.59 +        layout.setVerticalGroup(
    4.60 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    4.61 +            .addGroup(layout.createSequentialGroup()
    4.62 +                .addContainerGap()
    4.63 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    4.64 +                    .addComponent(loginLabel)
    4.65 +                    .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    4.66 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    4.67 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    4.68 +                    .addComponent(passwordLabel)
    4.69 +                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    4.70 +                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    4.71 +        );
    4.72 +    }// </editor-fold>//GEN-END:initComponents
    4.73 +
    4.74 +
    4.75 +    // Variables declaration - do not modify//GEN-BEGIN:variables
    4.76 +    private javax.swing.JTextField login;
    4.77 +    private javax.swing.JLabel loginLabel;
    4.78 +    private javax.swing.JPasswordField password;
    4.79 +    private javax.swing.JLabel passwordLabel;
    4.80 +    // End of variables declaration//GEN-END:variables
    4.81 +
    4.82 +    public String getLogin() {
    4.83 +        return login.getText();
    4.84 +    }
    4.85 +
    4.86 +    public String getPassword() {
    4.87 +        return new String(password.getPassword());
    4.88 +    }
    4.89 +}
     5.1 --- a/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Mon Sep 13 21:08:27 2010 +0200
     5.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Tue Sep 14 08:30:37 2010 +0200
     5.3 @@ -7,10 +7,13 @@
     5.4  
     5.5  import com.sun.jersey.api.client.Client;
     5.6  import com.sun.jersey.api.client.GenericType;
     5.7 +import com.sun.jersey.api.client.UniformInterfaceException;
     5.8  import com.sun.jersey.api.client.WebResource;
     5.9  import cz.xelfi.quoridor.webidor.Game;
    5.10  import cz.xelfi.quoridor.webidor.GameId;
    5.11  import java.util.List;
    5.12 +import java.util.logging.Level;
    5.13 +import java.util.logging.Logger;
    5.14  import javax.ws.rs.core.MediaType;
    5.15  import org.openide.util.Lookup;
    5.16  
    5.17 @@ -19,17 +22,28 @@
    5.18   * @author Jaroslav Tulach
    5.19   */
    5.20  abstract class Quoridor {
    5.21 +    private static Quoridor q;
    5.22  
    5.23      public static Quoridor getDefault() {
    5.24 -        Quoridor q = Lookup.getDefault().lookup(Quoridor.class);
    5.25 -        return q == null ? new Impl() : q;
    5.26 +        if (q != null) {
    5.27 +            return q;
    5.28 +        }
    5.29 +        q = Lookup.getDefault().lookup(Quoridor.class);
    5.30 +        if (q == null) {
    5.31 +            q = new Impl();
    5.32 +        }
    5.33 +        return q;
    5.34      }
    5.35  
    5.36      public abstract List<GameId> listGames();
    5.37      public abstract Game getGame(String id);
    5.38  
    5.39 +    public abstract boolean login(String login, String password);
    5.40 +
    5.41      private static class Impl extends Quoridor {
    5.42          private final WebResource wr;
    5.43 +        private String id = "";
    5.44 +        private static final Logger LOG = Logger.getLogger(Impl.class.getName());
    5.45  
    5.46          public Impl() {
    5.47              Client c = new Client();
    5.48 @@ -39,16 +53,30 @@
    5.49          @Override
    5.50          public List<GameId> listGames() {
    5.51              GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
    5.52 -            List<GameId> ids = wr.path("games").accept(MediaType.TEXT_XML).get(gType);
    5.53 +            List<GameId> ids = wr.path("games").queryParam("loginID", id).
    5.54 +                accept(MediaType.TEXT_XML).get(gType);
    5.55              return ids;
    5.56          }
    5.57  
    5.58  
    5.59          @Override
    5.60          public Game getGame(String id) {
    5.61 -            Game g = wr.path("games").path(id).accept(MediaType.TEXT_XML).get(Game.class);
    5.62 +            Game g = wr.path("games").path(id).queryParam("loginID", id).
    5.63 +                accept(MediaType.TEXT_XML).get(Game.class);
    5.64              return g;
    5.65          }
    5.66  
    5.67 +        @Override
    5.68 +        public boolean login(String login, String password) {
    5.69 +            try {
    5.70 +                id = wr.path("login").queryParam("name", login).
    5.71 +                    queryParam("password", password).put(String.class);
    5.72 +                return id != null;
    5.73 +            } catch (UniformInterfaceException ex) {
    5.74 +                LOG.log(Level.INFO, "Cannot login", ex);
    5.75 +                return false;
    5.76 +            }
    5.77 +        }
    5.78 +
    5.79      }
    5.80  }
     6.1 --- a/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/layer.xml	Mon Sep 13 21:08:27 2010 +0200
     6.2 +++ b/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/layer.xml	Tue Sep 14 08:30:37 2010 +0200
     6.3 @@ -11,9 +11,21 @@
     6.4                  <attr name="noIconInMenu" boolvalue="false"/>
     6.5              </file>
     6.6          </folder>
     6.7 +        <folder name="File">
     6.8 +            <file name="cz-xelfi-quoridor-desktop-sample-LoginAction.instance">
     6.9 +                <attr name="delegate" newvalue="cz.xelfi.quoridor.desktop.sample.LoginAction"/>
    6.10 +                <attr name="displayName" bundlevalue="cz.xelfi.quoridor.desktop.sample.Bundle#CTL_LoginAction"/>
    6.11 +                <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
    6.12 +                <attr name="noIconInMenu" boolvalue="false"/>
    6.13 +            </file>
    6.14 +        </folder>
    6.15      </folder>
    6.16      <folder name="Menu">
    6.17          <folder name="File">
    6.18 +            <file name="cz-xelfi-quoridor-desktop-sample-LoginAction.shadow">
    6.19 +                <attr name="originalFile" stringvalue="Actions/File/cz-xelfi-quoridor-desktop-sample-LoginAction.instance"/>
    6.20 +                <attr name="position" intvalue="1200"/>
    6.21 +            </file>
    6.22              <file name="cz-xelfi-quoridor-desktop-sample-ShowGames.shadow">
    6.23                  <attr name="originalFile" stringvalue="Actions/Edit/cz-xelfi-quoridor-desktop-sample-ShowGames.instance"/>
    6.24                  <attr name="position" intvalue="1300"/>
     7.1 --- a/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties	Mon Sep 13 21:08:27 2010 +0200
     7.2 +++ b/desktop/desktop-sample/src/main/resources/cz/xelfi/quoridor/desktop/sample/Bundle.properties	Tue Sep 14 08:30:37 2010 +0200
     7.3 @@ -1,9 +1,18 @@
     7.4  CTL_Hello=Hello
     7.5  CTL_KukAction=Kuk
     7.6  CTL_KukTopComponent={0}/{1}
     7.7 +CTL_LoginAction=Login
     7.8  CTL_OpenGameAction=Open a game
     7.9  CTL_ShowGames=Show Games
    7.10  HINT_KukTopComponent=White {0} against black {1}
    7.11  KukTopComponent.jLabel1.text=
    7.12  KukTopComponent.jButton1.text=
    7.13  KukTopComponent.jButton2.text=
    7.14 +
    7.15 +
    7.16 +LoginPanel.login.text=
    7.17 +LoginPanel.loginLabel.text=Login:
    7.18 +LoginPanel.passwordLabel.text=Password:
    7.19 +LoginAction.title=Login to Quoridor
    7.20 +LoginAction.fail=Login failed.
    7.21 +LoginPanel.password.text=