001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.midpnb.screen.display;
042:
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
045: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
046: import org.netbeans.modules.vmd.midp.screen.display.DisplayableDisplayPresenter;
047: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenStringPropertyEditor;
048: import org.netbeans.modules.vmd.midpnb.components.displayables.LoginScreenCD;
049: import javax.swing.*;
050: import java.awt.*;
051: import java.util.ArrayList;
052: import java.util.Arrays;
053: import java.util.Collection;
054: import org.netbeans.modules.vmd.api.model.PropertyValue;
055: import org.openide.util.NbBundle;
056:
057: /**
058: *
059: * @author Karol Harezlak
060: */
061: public class LoginScreenDisplayPresenter extends
062: DisplayableDisplayPresenter {
063:
064: private JComponent view;
065: private LoginView loginView;
066: private static final String USER_CODE = NbBundle.getMessage(
067: LoginScreenDisplayPresenter.class, "LBL_UserCode"); //NOI18N
068: private static final String NULL_TEXT = NbBundle.getMessage(
069: LoginScreenDisplayPresenter.class, "LBL_NULL"); //NOI18N
070:
071: @Override
072: public void reload(ScreenDeviceInfo deviceInfo) {
073: super .reload(deviceInfo);
074: if (loginView != null) {
075: loginView.updateView();
076: }
077: }
078:
079: @Override
080: public JComponent getView() {
081: if (view == null) {
082: view = super .getView();
083: loginView = new LoginView();
084: super .getView().add(loginView);
085: }
086: return view;
087: }
088:
089: @Override
090: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
091: Collection<ScreenPropertyDescriptor> desciptors = new ArrayList<ScreenPropertyDescriptor>(
092: super .getPropertyDescriptors());
093: desciptors.addAll(Arrays.asList(
094: new ScreenPropertyDescriptor(getComponent(),
095: loginView.passwordTextField,
096: new ScreenStringPropertyEditor(
097: LoginScreenCD.PROP_PASSWORD,
098: JTextField.CENTER)),
099: new ScreenPropertyDescriptor(getComponent(),
100: loginView.passwordLabel,
101: new ScreenStringPropertyEditor(
102: LoginScreenCD.PROP_PASSWORD_LABEL,
103: JTextField.CENTER)),
104: new ScreenPropertyDescriptor(getComponent(),
105: loginView.usernameLabel,
106: new ScreenStringPropertyEditor(
107: LoginScreenCD.PROP_USERNAME_LABEL,
108: JTextField.CENTER)),
109: new ScreenPropertyDescriptor(getComponent(),
110: loginView.usernameTextField,
111: new ScreenStringPropertyEditor(
112: LoginScreenCD.PROP_USERNAME,
113: JTextField.CENTER))));
114: return desciptors;
115: }
116:
117: private class LoginView extends JPanel {
118:
119: JButton loginButton;
120: JLabel passwordLabel;
121: JPasswordField passwordTextField;
122: JLabel usernameLabel;
123: JTextField usernameTextField;
124: GridBagConstraints gridBagConstraints;
125:
126: LoginView() {
127: initComponents();
128: }
129:
130: private void initComponents() {
131: usernameLabel = new javax.swing.JLabel();
132: passwordLabel = new javax.swing.JLabel();
133: usernameTextField = new javax.swing.JTextField();
134: passwordTextField = new javax.swing.JPasswordField();
135: loginButton = new javax.swing.JButton();
136:
137: setLayout(new java.awt.GridBagLayout());
138:
139: gridBagConstraints = new java.awt.GridBagConstraints();
140: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
141: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
142: add(usernameLabel, gridBagConstraints);
143:
144: gridBagConstraints = new java.awt.GridBagConstraints();
145: gridBagConstraints.gridx = 0;
146: gridBagConstraints.gridy = 1;
147: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
148: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
149: add(passwordLabel, gridBagConstraints);
150:
151: gridBagConstraints = new java.awt.GridBagConstraints();
152: gridBagConstraints.gridwidth = 3;
153: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
154: add(usernameTextField, gridBagConstraints);
155:
156: gridBagConstraints = new java.awt.GridBagConstraints();
157: gridBagConstraints.gridx = 1;
158: gridBagConstraints.gridy = 1;
159: gridBagConstraints.gridwidth = 3;
160: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
161: gridBagConstraints.ipadx = 100;
162: add(passwordTextField, gridBagConstraints);
163:
164: addLoginButton();
165:
166: updateView();
167: }
168:
169: private void addLoginButton() {
170: gridBagConstraints = new java.awt.GridBagConstraints();
171: gridBagConstraints.gridx = 3;
172: gridBagConstraints.gridy = 4;
173: gridBagConstraints.ipadx = 10;
174: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
175: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
176: add(loginButton, gridBagConstraints);
177: loginButton.setText("Login"); //NOI18N
178: }
179:
180: void updateView() {
181: final DesignComponent component = LoginScreenDisplayPresenter.this
182: .getComponent();
183: component.getDocument().getTransactionManager().readAccess(
184: new Runnable() {
185:
186: public void run() {
187: PropertyValue usernameLabelPV = component
188: .readProperty(LoginScreenCD.PROP_USERNAME_LABEL);
189: if (usernameLabelPV.getKind() == PropertyValue.Kind.VALUE) {
190: usernameLabel
191: .setText((String) usernameLabelPV
192: .getPrimitiveValue());
193: } else if (usernameLabelPV.getKind() == PropertyValue.Kind.USERCODE) {
194: usernameLabel.setText(USER_CODE);
195: } else if (usernameLabelPV.getKind() == PropertyValue.Kind.NULL) {
196: usernameLabel.setText(NULL_TEXT);
197: }
198: PropertyValue passwordLabelPV = component
199: .readProperty(LoginScreenCD.PROP_PASSWORD_LABEL);
200: if (passwordLabelPV.getKind() == PropertyValue.Kind.VALUE) {
201: passwordLabel
202: .setText((String) passwordLabelPV
203: .getPrimitiveValue());
204: } else if (passwordLabelPV.getKind() == PropertyValue.Kind.USERCODE) {
205: passwordLabel.setText(USER_CODE);
206: } else {
207: passwordLabel.setText(NULL_TEXT);
208: }
209: PropertyValue usernameTextFieldPV = component
210: .readProperty(LoginScreenCD.PROP_USERNAME);
211: if (usernameTextFieldPV.getKind() == PropertyValue.Kind.VALUE) {
212: usernameTextField.setEnabled(true);
213: usernameTextField
214: .setText((String) usernameTextFieldPV
215: .getPrimitiveValue());
216: } else if (usernameTextFieldPV.getKind() == PropertyValue.Kind.USERCODE) {
217: usernameTextField.setText(USER_CODE);
218: usernameTextField.setEnabled(false);
219: }
220:
221: PropertyValue passwordTextFieldPV = component
222: .readProperty(LoginScreenCD.PROP_PASSWORD);
223: if (passwordTextFieldPV.getKind() == PropertyValue.Kind.VALUE) {
224: passwordTextField.setEnabled(true);
225: passwordTextField
226: .setText((String) passwordTextFieldPV
227: .getPrimitiveValue());
228: } else if (passwordTextFieldPV.getKind() == PropertyValue.Kind.USERCODE) {
229: passwordTextField.setText(USER_CODE);
230: passwordTextField.setEnabled(false);
231: }
232:
233: Integer bckColor = (Integer) component
234: .readProperty(
235: LoginScreenCD.PROP_BGK_COLOR)
236: .getPrimitiveValue();
237: Integer frgColor = (Integer) component
238: .readProperty(
239: LoginScreenCD.PROP_FRG_COLOR)
240: .getPrimitiveValue();
241: if (bckColor != null) {
242: Color color = new Color(bckColor);
243: setBackground(color);
244: loginButton.setBackground(color);
245: }
246: if (frgColor != null) {
247: Color color = new Color(frgColor);
248: usernameLabel.setForeground(color);
249: usernameTextField.setForeground(color);
250: passwordLabel.setForeground(color);
251: passwordTextField.setForeground(color);
252: loginButton.setForeground(color);
253: }
254: Boolean buttonUsed = (Boolean) component
255: .readProperty(
256: LoginScreenCD.PROP_USE_LOGIN_BUTTON)
257: .getPrimitiveValue();
258: if (buttonUsed == null) {
259: throw new IllegalArgumentException();
260: }
261: if (buttonUsed) {
262: addLoginButton();
263: } else {
264: remove(loginButton);
265: }
266: }
267: });
268: }
269: }
270: }
|