]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/auth/LoginComposite.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / auth / LoginComposite.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.auth;
13
14 import org.eclipse.jface.layout.GridDataFactory;
15 import org.eclipse.jface.layout.GridLayoutFactory;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.FocusEvent;
18 import org.eclipse.swt.events.FocusListener;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Text;
27 import org.simantics.ui.auth.model.LoginModel;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class LoginComposite extends Composite {
33
34 //    private FormToolkit toolkit;
35
36     private LoginModel  model;
37
38     public LoginComposite(Composite parent, int style, LoginModel m) {
39         super(parent, style);
40         
41         this.model = m;
42
43 //        toolkit = new FormToolkit(parent.getDisplay());
44
45         Composite body = this;
46         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(2).applyTo(body);
47         GridDataFactory.fillDefaults().grab(true, true).applyTo(body);
48
49         Label desc = new Label(this, 0);
50         desc.setText("Enter credentials for " + model.getServer().getInfo());
51         GridDataFactory.fillDefaults().span(2, 1).applyTo(desc);
52         
53 //        Label nameLabel = toolkit.createLabel(this, "User name:");
54         Label nameLabel = new Label(this, 0);
55         nameLabel.setText("&User name:");
56         GridDataFactory.fillDefaults().align(SWT.LEAD, SWT.CENTER).applyTo(nameLabel);
57 //        final Text nameText = toolkit.createText(this, "", SWT.NONE);
58         final Text nameText = new Text(this, SWT.BORDER);
59         nameText.setText(model.getName());
60         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);
61
62 //        Label passwordLabel = toolkit.createLabel(this, "Password:");
63         Label passwordLabel = new Label(this, 0);
64         passwordLabel.setText("&Password:");
65         GridDataFactory.fillDefaults().align(SWT.LEAD, SWT.CENTER).applyTo(passwordLabel);
66 //        final Text passwordText = toolkit.createText(this, "", SWT.NONE);
67         final Text passwordText = new Text(this, SWT.PASSWORD | SWT.BORDER);
68         passwordText.setText(model.getPassword());
69         GridDataFactory.fillDefaults().grab(true, false).applyTo(passwordText);
70
71 //        final Button rememberButton = toolkit.createButton(this, "Remember Login", SWT.CHECK);
72         final Button rememberButton = new Button(this, SWT.CHECK);
73         rememberButton.setText("&Remember Login");
74         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(rememberButton);
75         
76         // Build the separator line
77         Label separator = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
78         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BOTTOM).span(2, 1).grab(false, true).applyTo(separator);
79         
80         nameText.addModifyListener(new ModifyListener() {
81             @Override
82             public void modifyText(ModifyEvent e) {
83                 model.setName(nameText.getText());
84             }
85         });
86         passwordText.addModifyListener(new ModifyListener() {
87             @Override
88             public void modifyText(ModifyEvent e) {
89                 model.setPassword(passwordText.getText());
90             }
91         });
92         rememberButton.addSelectionListener(new SelectionAdapter() {
93             @Override
94             public void widgetSelected(SelectionEvent e) {
95                 model.setRemember(rememberButton.getSelection());
96             }
97         });
98         
99         new FocusSelector(nameText);
100         new FocusSelector(passwordText);
101     }
102     
103     static class FocusSelector implements FocusListener {
104         Text text;
105         FocusSelector(Text text) {
106             this.text = text;
107             text.addFocusListener(this);
108         }
109         @Override
110         public void focusGained(FocusEvent e) {
111             text.selectAll();
112         }
113         @Override
114         public void focusLost(FocusEvent e) {
115             text.setSelection(0);
116         }
117     }
118
119     @Override
120     public void dispose() {
121         if (isDisposed()) {
122 //            if (toolkit != null) {
123 //                toolkit.dispose();
124 //                toolkit = null;
125 //            }
126             super.dispose();
127         }
128     }
129
130 }