001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.dialog;
051:
052: import java.awt.Frame;
053: import java.awt.Window;
054: import java.awt.event.WindowEvent;
055: import java.awt.event.WindowListener;
056: import java.io.InputStream;
057: import java.io.ObjectInputStream;
058: import java.io.ObjectOutputStream;
059: import java.io.OutputStream;
060: import java.net.URL;
061:
062: import javax.swing.JCheckBox;
063: import javax.swing.JComponent;
064: import javax.swing.JFrame;
065: import javax.swing.JPasswordField;
066: import javax.swing.JTextField;
067:
068: import com.jgoodies.forms.builder.DefaultFormBuilder;
069: import com.jgoodies.forms.layout.CellConstraints;
070: import com.jgoodies.forms.layout.FormLayout;
071: import com.projity.pm.graphic.IconManager;
072: import com.projity.strings.Messages;
073: import com.projity.util.ClassLoaderUtils;
074:
075: public final class LoginDialog extends AbstractDialog {
076: private static final long serialVersionUID = 1L;
077:
078: private LoginForm form;
079:
080: // use property utils to copy to project like struts
081: JTextField login;
082: JPasswordField password;
083: JCheckBox storeCredentials;
084: JCheckBox useMenus;
085:
086: private JFrame standaloneFrame = null;
087:
088: private static String getDialogTitle() {
089: return Messages.getContextString("Text.ApplicationTitle"); //$NON-NLS-1$
090: }
091:
092: public static LoginForm doLogin(Frame owner, URL serverUrl) {
093:
094: if (owner == null) {
095: final JFrame standaloneFrame = new JFrame(getDialogTitle());
096: standaloneFrame.setIconImage(IconManager
097: .getImage("application.icon")); //$NON-NLS-1$
098: standaloneFrame.addWindowListener(new WindowListener() {
099: public void windowOpened(WindowEvent arg0) {
100: }
101:
102: public void windowClosing(WindowEvent arg0) {
103: }
104:
105: public void windowClosed(WindowEvent arg0) {
106: }
107:
108: public void windowIconified(WindowEvent arg0) {
109: }
110:
111: public void windowDeiconified(WindowEvent arg0) {
112: }
113:
114: public void windowDeactivated(WindowEvent arg0) {
115: }
116:
117: public void windowActivated(WindowEvent arg0) {
118: Window w[] = standaloneFrame.getOwnedWindows();
119: for (int i = 0; i < w.length; i++)
120: w[i].toFront();
121: }
122: });
123: owner = standaloneFrame;
124: }
125:
126: LoginForm form = null;
127: if (serverUrl != null)
128: try {
129: Object ps = ClassLoaderUtils
130: .getLocalClassLoader()
131: .loadClass("javax.jnlp.ServiceManager").getMethod("lookup", new Class[] { String.class }).invoke(null, new Object[] { "javax.jnlp.PersistenceService" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
132: // Object ps=Class.forName("javax.jnlp.ServiceManager").getMethod("lookup",new Class[]{String.class}).invoke(null,new Object[]{"javax.jnlp.PersistenceService"});
133: Object contents = ps
134: .getClass()
135: .getMethod("get", new Class[] { URL.class }).invoke(ps, new Object[] { serverUrl }); //$NON-NLS-1$
136: ObjectInputStream in = new ObjectInputStream(
137: (InputStream) ClassLoaderUtils
138: .getLocalClassLoader()
139: .loadClass("javax.jnlp.FileContents").getMethod("getInputStream", null).invoke(contents, null)); //$NON-NLS-1$ //$NON-NLS-2$
140: // ObjectInputStream in=new ObjectInputStream((InputStream)Class.forName("javax.jnlp.FileContents").getMethod("getInputStream",null).invoke(contents,null));
141: form = (LoginForm) in.readObject();
142: in.close();
143: } catch (Exception e) {
144: }
145:
146: LoginDialog dlg = getInstance(owner, form);
147:
148: // make sure dialog shows
149: dlg.requestFocus();
150: // Because setAlwaysOnTop is not in JDK 1.4, I added treatment as per http://www.codecomments.com/archive250-2004-12-347421.html -HK 25/2/05
151: // dlg.setAlwaysOnTop(true); // this is not in JDK 1.4!!!
152:
153: dlg.doModal();
154:
155: if (serverUrl != null)
156: try {
157: Object ps = Class
158: .forName("javax.jnlp.ServiceManager").getMethod("lookup", new Class[] { String.class }).invoke(null, new Object[] { "javax.jnlp.PersistenceService" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
159: if (dlg.form.isStoreCredentials()) {
160:
161: if (form == null)
162: ps
163: .getClass()
164: .getMethod(
165: "create", new Class[] { URL.class, long.class }).invoke(ps, new Object[] { serverUrl, new Long(1000) }); //$NON-NLS-1$
166: Object contents = ps
167: .getClass()
168: .getMethod("get", new Class[] { URL.class }).invoke(ps, new Object[] { serverUrl }); //$NON-NLS-1$
169: ObjectOutputStream out = new ObjectOutputStream(
170: (OutputStream) Class
171: .forName("javax.jnlp.FileContents").getMethod("getOutputStream", new Class[] { boolean.class }).invoke(contents, new Object[] { new Boolean(true) })); //$NON-NLS-1$ //$NON-NLS-2$
172: out.writeObject(dlg.form);
173: out.close();
174: } else if (form != null)
175: ps
176: .getClass()
177: .getMethod(
178: "delete", new Class[] { URL.class }).invoke(ps, new Object[] { serverUrl }); //$NON-NLS-1$
179:
180: } catch (Exception e) {
181: }
182:
183: return dlg.form;
184: }
185:
186: public static LoginDialog getInstance(Frame owner, LoginForm form) {
187: return new LoginDialog(owner, form);
188: }
189:
190: private LoginDialog(Frame owner, LoginForm form) {
191: super (owner, getDialogTitle(), true);
192: this .form = (form == null) ? new LoginForm() : form;
193:
194: addWindowListener(new WindowListener() {
195: public void windowOpened(WindowEvent arg0) {
196: }
197:
198: public void windowClosing(WindowEvent arg0) {
199: }
200:
201: public void windowClosed(WindowEvent arg0) {
202: }
203:
204: public void windowIconified(WindowEvent arg0) {
205: }
206:
207: public void windowDeiconified(WindowEvent arg0) {
208: }
209:
210: public void windowDeactivated(WindowEvent arg0) {
211: }
212:
213: public void windowActivated(WindowEvent arg0) {
214: toFront();
215: }
216: });
217: }
218:
219: // Component Creation and Initialization **********************************
220:
221: /**
222: * Creates, intializes and configures the UI components. Real applications
223: * may further bind the components to underlying models.
224: */
225: protected void initControls() {
226: login = new JTextField();
227: password = new JPasswordField();
228: storeCredentials = new JCheckBox();
229: useMenus = new JCheckBox();
230: bind(true);
231: }
232:
233: protected boolean bind(boolean get) {
234: if (get) {
235: login.setText(form.getLogin());
236: password.setText(form.getPassword());
237: storeCredentials.setSelected(form.isStoreCredentials());
238: useMenus.setSelected(form.isUseMenus());
239: } else {
240: if (login.getText().trim().length() == 0
241: || password.getPassword().length == 0) // prevent empty fields
242: return false;
243: form.setLogin(login.getText());
244: form.setPassword(new String(password.getPassword()));
245: form.setStoreCredentials(storeCredentials.isSelected());
246: form.setUseMenus(useMenus.isSelected());
247: }
248: return true;
249: }
250:
251: // Building *************************************************************
252:
253: /**
254: * Builds the panel. Initializes and configures components first, then
255: * creates a FormLayout, configures the layout, creates a builder, sets a
256: * border, and finally adds the components.
257: *
258: * @return the built panel
259: */
260:
261: public JComponent createContentPanel() {
262: // Separating the component initialization and configuration
263: // from the layout code makes both parts easier to read.
264: initControls();
265: //TODO set minimum size
266: FormLayout layout = new FormLayout(
267: "default, 3dlu, 120dlu:grow", // cols //$NON-NLS-1$
268: "p, 3dlu,p,3dlu,p,3dlu,p"); // rows //$NON-NLS-1$
269:
270: // Create a builder that assists in adding components to the container.
271: // Wrap the panel with a standardized border.
272: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
273: builder.setDefaultDialogBorder();
274: CellConstraints cc = new CellConstraints();
275: builder.append(Messages.getString("LoginDialog.Login"), login); //$NON-NLS-1$
276: builder.nextLine(2);
277: builder.append(
278: Messages.getString("LoginDialog.Password"), password); //$NON-NLS-1$
279: builder.nextLine(2);
280: // builder.append(useMenus);
281: // builder.append(Messages.getString("LoginDialog.UseOfficeLook")); //$NON-NLS-1$
282: builder.nextLine(2);
283: builder.append(storeCredentials);
284: builder.append(Messages.getString("LoginDialog.RememberMe")); //$NON-NLS-1$
285:
286: return builder.getPanel();
287: }
288:
289: /**
290: * @return Returns the form.
291: */
292: public LoginForm getForm() {
293: return form;
294: }
295:
296: protected void onCancel() {
297: getForm().setCancelled(true);
298: super.onCancel();
299: }
300:
301: }
|