Author: rob.stryker(a)jboss.com
Date: 2011-01-18 18:22:44 -0500 (Tue, 18 Jan 2011)
New Revision: 28372
Added:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonPasswordStorageKey.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/SecurePasswordStore.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/wizards/
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/wizards/HudsonServerDialog.java
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/META-INF/MANIFEST.MF
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/AbstractHudsonListener.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonModel.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonServer.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/IHudsonListener.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonActionProvider.java
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonManagerView.java
Log:
workspace hudson tooling - allows creation and deletion of connections and secure
persistant storage
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/META-INF/MANIFEST.MF
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/META-INF/MANIFEST.MF 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/META-INF/MANIFEST.MF 2011-01-18
23:22:44 UTC (rev 28372)
@@ -5,7 +5,8 @@
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: org.jboss.tools.hudson.manager.core.Activator
Require-Bundle: org.eclipse.ui,
- org.eclipse.core.runtime
+ org.eclipse.core.runtime,
+ org.eclipse.equinox.security;bundle-version="1.0.200"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: org.jboss.tools.hudson.manager.core,
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/AbstractHudsonListener.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/AbstractHudsonListener.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/AbstractHudsonListener.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -4,5 +4,8 @@
public void jobFetched(HudsonJob job) {
}
-
+ public void serverAdded(HudsonServer server) {
+ }
+ public void serverRemoved(HudsonServer server) {
+ }
}
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonModel.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonModel.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonModel.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -1,14 +1,26 @@
package org.jboss.tools.hudson.manager.core.model;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
import java.net.URL;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import org.dom4j.Document;
+import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
import org.dom4j.io.SAXReader;
+import org.eclipse.core.runtime.IPath;
+import org.jboss.tools.hudson.manager.core.Activator;
public class HudsonModel {
public static final String API_XML = "api/xml";
@@ -54,12 +66,74 @@
return hudsons.toArray(new HudsonServer[hudsons.size()]);
}
+ public void addServer(HudsonServer server) {
+ hudsons.add(server);
+ saveHudsons();
+ fireServerAdded(server);
+ }
+ public void removeServer(HudsonServer server) {
+ hudsons.remove(server);
+ saveHudsons();
+ fireServerRemoved(server);
+ }
+
private HudsonServer[] loadHudsons() {
- return new HudsonServer[]{
- new HudsonServer("Test1",
"http://hudson.qa.jboss.com/hudson/view/DevStudio_Trunk/"),
- };
+ IPath stateLocation = Activator.getDefault().getStateLocation();
+ File f = stateLocation.append("hudsons.xml").toFile();
+ if( !f.exists()) {
+ return new HudsonServer[]{};
+ }
+
+ ArrayList<HudsonServer> storedServers = new ArrayList<HudsonServer>();
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(f);
+ Element root = document.getRootElement();
+ List servers = root.selectNodes("hudson");
+ for( int i = 0; i < servers.size(); i++ ) {
+ Element e = ((Element)servers.get(i));
+ storedServers.add(new HudsonServer(
+ URLDecoder.decode(e.attributeValue("name")),
+ URLDecoder.decode(e.attributeValue("url")),
+ URLDecoder.decode(e.attributeValue("user"))));
+ }
+ } catch( DocumentException de) {
+ de.printStackTrace();
+ }
+
+ return (HudsonServer[]) storedServers.toArray(new HudsonServer[storedServers.size()]);
+// return new HudsonServer[]{
+// new HudsonServer("Test1",
"http://hudson.qa.jboss.com/hudson/view/DevStudio_Trunk/"),
+// };
}
+ private void saveHudsons() {
+ IPath stateLocation = Activator.getDefault().getStateLocation();
+ File f = stateLocation.append("hudsons.xml").toFile();
+ String val = "<hudsons>\n";
+ HudsonServer tmp;
+ for( int i = 0; i < hudsons.size(); i++ ) {
+ tmp=hudsons.get(i);
+ val += "<hudson name=\"" + URLEncoder.encode(tmp.getName())
+ + "\" url=\"" + URLEncoder.encode(tmp.getUrl())
+ + "\" user=\"" + URLEncoder.encode(tmp.getUser()) +
"\"/>\n";
+ }
+ val += "</hudsons>";
+ Writer output = null;
+ try {
+ //use buffering
+ output = new BufferedWriter(new FileWriter(f));
+ //FileWriter always assumes default encoding is OK!
+ output.write( val );
+ } catch(IOException ioe) {
+ } finally {
+ if( output != null)
+ try {
+ output.close();
+ } catch(IOException ioe) {}
+ }
+ }
+
private DocumentRepository docRepo;
public DocumentRepository getDocumentRepository() {
if( docRepo == null ) {
@@ -105,12 +179,21 @@
}
}
-
void fireJobFetched(HudsonJob job) {
for( Iterator<IHudsonListener> i = listeners.iterator(); i.hasNext(); ) {
i.next().jobFetched(job);
}
}
-
+ void fireServerAdded(HudsonServer server) {
+ for( Iterator<IHudsonListener> i = listeners.iterator(); i.hasNext(); ) {
+ i.next().serverAdded(server);
+ }
+ }
+
+ void fireServerRemoved(HudsonServer server) {
+ for( Iterator<IHudsonListener> i = listeners.iterator(); i.hasNext(); ) {
+ i.next().serverRemoved(server);
+ }
+ }
}
Added:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonPasswordStorageKey.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonPasswordStorageKey.java
(rev 0)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonPasswordStorageKey.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hudson.manager.core.model;
+
+import org.eclipse.equinox.security.storage.EncodingUtils;
+import org.jboss.tools.hudson.manager.core.Activator;
+import org.jboss.tools.hudson.manager.core.model.SecurePasswordStore.IStorageKey;
+
+/**
+ * Implements a key to be used to store values in the preferences store.
+ *
+ * @author Andre Dietisheim
+ *
+ */
+public class HudsonPasswordStorageKey implements IStorageKey {
+
+ private static final String PREFERNCES_BASEKEY =
Activator.PLUGIN_ID.replace('.', '/');
+ private String hudsonName;
+ private String userName;
+
+ public HudsonPasswordStorageKey(String hudsonName, String userName) {
+ this.userName = EncodingUtils.encodeBase64(userName.getBytes());
+ this.hudsonName = EncodingUtils.encodeBase64(hudsonName.getBytes());
+ }
+
+ @Override
+ public String getKey() {
+ String key = new StringBuilder(PREFERNCES_BASEKEY)
+ .append(hudsonName)
+ .append('/') //$NON-NLS-1$
+ .append(userName)
+ .toString();
+ return EncodingUtils.encodeSlashes(key);
+ }
+
+ @Override
+ public boolean equals(IStorageKey key) {
+ if (!key.getClass().isAssignableFrom(HudsonPasswordStorageKey.class)) {
+ return false;
+ }
+ HudsonPasswordStorageKey hudsonKey = (HudsonPasswordStorageKey) key;
+ return userName.equals(hudsonKey.userName)
+ && hudsonName.equals(hudsonKey.hudsonName);
+ }
+}
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonServer.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonServer.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/HudsonServer.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -8,16 +8,33 @@
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
-import org.eclipse.core.runtime.Path;
public class HudsonServer {
private String name;
private String url;
+ private String user;
+ private SecurePasswordStore secureStore;
private ArrayList<HudsonJob> jobs = null;
- public HudsonServer(String name, String url) {
+
+ public HudsonServer(String name, String url, String user) {
+ this(name, url, user, null);
+ }
+ public HudsonServer(String name, String url, String user, String password) {
this.name = name;
this.url = url;
+ this.user = user;
+ secureStore = createSecurePasswordStore(name, url, password);
+ }
+
+ public String getPassword() {
+ return secureStore.getPassword();
}
+
+ protected SecurePasswordStore createSecurePasswordStore(String name2, String username2,
String password) {
+ return new SecurePasswordStore(new HudsonPasswordStorageKey(name, user), password);
+ }
+
+
public String getName() {
return name;
}
@@ -81,4 +98,10 @@
}
return (HudsonJob[]) list.toArray(new HudsonJob[list.size()]);
}
+ public String getUser() {
+ return user;
+ }
+ public void setUser(String user) {
+ this.user = user;
+ }
}
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/IHudsonListener.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/IHudsonListener.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/IHudsonListener.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -1,5 +1,7 @@
package org.jboss.tools.hudson.manager.core.model;
public interface IHudsonListener {
+ public void serverAdded(HudsonServer server);
+ public void serverRemoved(HudsonServer server);
public void jobFetched(HudsonJob job);
}
Added:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/SecurePasswordStore.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/SecurePasswordStore.java
(rev 0)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.core/src/org/jboss/tools/hudson/manager/core/model/SecurePasswordStore.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hudson.manager.core.model;
+
+import org.eclipse.equinox.security.storage.ISecurePreferences;
+import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
+import org.eclipse.equinox.security.storage.StorageException;
+
+/**
+ * @author Andre Dietisheim
+ */
+public class SecurePasswordStore {
+
+ public static interface IStorageKey {
+ public String getKey();
+ public boolean equals(IStorageKey key);
+ }
+
+ private String password;
+ private boolean stored;
+ private IStorageKey storageKey;
+
+ public SecurePasswordStore(IStorageKey key, String password) {
+ this.storageKey = key;
+ this.password = password;
+ }
+
+ public String getPassword() {
+ try {
+ if (password != null) {
+ if (!stored) {
+ storeInPreferences(password, storageKey);
+ stored = true;
+ }
+ return password;
+ } else {
+ return this.password = getFromPreferences(storageKey);
+ }
+ } catch (StorageException e) {
+ // TODO
+ }
+ return null;
+ }
+
+ public void setPassword(String password) {
+ update(storageKey, password);
+ }
+
+ public void update(IStorageKey key, String password) {
+ try {
+ if (!storageKey.equals(key)
+ || isPasswordChanged(password)) {
+ storeInPreferences(this.password = password, this.storageKey = key);
+ }
+ } catch(StorageException se) {
+ // TODO
+ }
+ }
+
+ private boolean isPasswordChanged(String password) {
+ if (this.password == null && password == null) {
+ return false;
+ } else {
+ return (this.password == null && password != null)
+ || (this.password != null && password == null)
+ || !password.equals(this.password);
+ }
+ }
+
+ public void remove() {
+ ISecurePreferences node = getNode(storageKey);
+ if (node != null)
+ node.clear();
+ }
+
+ private String getFromPreferences(IStorageKey key) throws StorageException {
+ ISecurePreferences node = getNode(key);
+ String password = node.get("password", null); //$NON-NLS-1$
+ return password;
+ }
+
+ private void storeInPreferences(String password, IStorageKey key) throws
StorageException {
+ ISecurePreferences node = getNode(key);
+ node.put("password", password, true /* encrypt */); //$NON-NLS-1$
+ }
+
+ private ISecurePreferences getNode(IStorageKey key) {
+ ISecurePreferences root = SecurePreferencesFactory.getDefault();
+ return root.node(key.getKey());
+ }
+}
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonActionProvider.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonActionProvider.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonActionProvider.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -2,20 +2,33 @@
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.navigator.CommonViewer;
import org.jboss.tools.hudson.manager.core.model.HudsonJob;
import org.jboss.tools.hudson.manager.core.model.HudsonJob.JobBuild;
+import org.jboss.tools.hudson.manager.core.model.HudsonModel;
+import org.jboss.tools.hudson.manager.core.model.HudsonServer;
+import org.jboss.tools.hudson.manager.ui.wizards.HudsonServerDialog;
public class HudsonActionProvider extends AbstractCommonNavActionProvider {
- private Action refreshJobAction, openBuiltOnAction;
+ private Action newConnectionAction, deleteConnectionAction,
+ refreshJobAction, openBuiltOnAction;
public HudsonActionProvider() {
super();
}
public void fillContextMenu(IMenuManager menu) {
- Object first = getSelection();
+ createActions(cv, null); // TODO remove
+
+ Object first = getSelectedItem();
+ Separator visible = new Separator();
+ visible.setVisible(true);
+ menu.add(newConnectionAction);
+ if( first != null && first instanceof HudsonServer )
+ menu.add(deleteConnectionAction);
+ menu.add(visible);
if (first == null)
return;
if( getSelectedItem() instanceof HudsonJob )
@@ -27,7 +40,30 @@
}
}
- public void createActions(CommonViewer tableViewer, ISelectionProvider provider) {
+ public void createActions(final CommonViewer tableViewer, ISelectionProvider provider)
{
+ newConnectionAction = new Action() {
+ public void run() {
+ HudsonServerDialog d = new HudsonServerDialog(tableViewer.getTree().getShell());
+ if( d.open() == HudsonServerDialog.OK) {
+ HudsonServer s = new HudsonServer(d.getName(), d.getUrl(), d.getUser(),
d.getPass());
+ if( s != null ) {
+ HudsonModel.getInstance().addServer(s);
+ }
+ }
+ }
+ };
+ newConnectionAction.setText("Create New Hudson Server");
+
+ deleteConnectionAction = new Action() {
+ public void run() {
+ HudsonServer sel = (HudsonServer)getSelectedItem();
+ if( sel != null ) {
+ HudsonModel.getInstance().removeServer(sel);
+ }
+ }
+ };
+ deleteConnectionAction.setText("Delete Hudson Server");
+
refreshJobAction = new Action() {
public void run() {
HudsonJob job = (HudsonJob)getSelectedItem();
Modified:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonManagerView.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonManagerView.java 2011-01-18
19:01:41 UTC (rev 28371)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/views/HudsonManagerView.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -2,13 +2,31 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.navigator.CommonNavigator;
-import org.eclipse.ui.navigator.CommonViewerSorter;
+import org.jboss.tools.hudson.manager.core.model.AbstractHudsonListener;
+import org.jboss.tools.hudson.manager.core.model.HudsonModel;
+import org.jboss.tools.hudson.manager.core.model.HudsonServer;
+import org.jboss.tools.hudson.manager.core.model.IHudsonListener;
public class HudsonManagerView extends CommonNavigator {
+ private IHudsonListener modelListener = new AbstractHudsonListener() {
+ public void serverAdded(HudsonServer server) {
+ getCommonViewer().refresh();
+ }
+ public void serverRemoved(HudsonServer server) {
+ getCommonViewer().refresh();
+ }
+ };
+
public HudsonManagerView() {
- // TODO Auto-generated constructor stub
+ HudsonModel.getInstance().addListener(modelListener);
}
+
+ public void dispose() {
+ super.dispose();
+ HudsonModel.getInstance().removeListener(modelListener);
+ }
+
protected Object getInitialInput() {
return this;
}
Added:
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/wizards/HudsonServerDialog.java
===================================================================
---
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/wizards/HudsonServerDialog.java
(rev 0)
+++
workspace/rstryker/hudson/plugins/org.jboss.tools.hudson.manager.ui/src/org/jboss/tools/hudson/manager/ui/wizards/HudsonServerDialog.java 2011-01-18
23:22:44 UTC (rev 28372)
@@ -0,0 +1,144 @@
+package org.jboss.tools.hudson.manager.ui.wizards;
+
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.layout.FormAttachment;
+import org.eclipse.swt.layout.FormData;
+import org.eclipse.swt.layout.FormLayout;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.jboss.tools.hudson.manager.core.model.HudsonModel;
+import org.jboss.tools.hudson.manager.core.model.HudsonServer;
+
+public class HudsonServerDialog extends TitleAreaDialog {
+
+ public static FormData createFormData(
+ Object top, int topOffset, Object bottom, int bottomOffset,
+ Object left, int leftOffset, Object right, int rightOffset) {
+ FormData data = new FormData();
+ if( top != null ) data.top = createAttachment(top, topOffset);
+ if( bottom != null ) data.bottom = createAttachment(bottom, bottomOffset);
+ if( left != null ) data.left = createAttachment(left, leftOffset);
+ if( right != null ) data.right = createAttachment(right, rightOffset);
+ return data;
+ }
+
+ public static FormAttachment createAttachment(Object attachment, int offset) {
+ if( attachment instanceof Integer) {
+ return new FormAttachment(((Integer)attachment).intValue(), offset);
+ }
+ return new FormAttachment((Control)attachment, offset);
+ }
+
+ private HudsonServer initialServer;
+ private String name, url, user, pass;
+ private Text nameText, urlText, usernameText, passText;
+
+ public HudsonServerDialog(Shell parentShell) {
+ super(parentShell);
+ this.initialServer = null;
+ }
+
+ public HudsonServerDialog(Shell parentShell, HudsonServer initial) {
+ this(parentShell);
+ this.initialServer = initial;
+ }
+
+ protected Control createDialogArea(Composite parent) {
+ setTitle("Create new Hudson Server");
+ setMessage("Get on it, dork");
+
+ // create the top level composite for the dialog area
+ Composite composite = new Composite(parent, SWT.NONE);
+ composite.setLayout(new FormLayout());
+ composite.setLayoutData(new GridData(GridData.FILL_BOTH));
+ composite.setFont(parent.getFont());
+ // Build the separator line
+ Label titleBarSeparator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
+ titleBarSeparator.setLayoutData(createFormData(0, 0, null, 0, 0,0,100,0));
+
+ Label name = new Label(composite, SWT.None);
+ Label url = new Label(composite, SWT.None);
+ name.setLayoutData(createFormData(titleBarSeparator, 5, null, 0, 0,5,null,0));
+ nameText = new Text(composite, SWT.BORDER);
+ nameText.setLayoutData(createFormData(titleBarSeparator,5,null,0,url,5,100,-5));
+
+ url.setLayoutData(createFormData(nameText, 5, null, 0, 0,5,null,0));
+ urlText = new Text(composite, SWT.BORDER);
+ urlText.setLayoutData(createFormData(nameText,5,null,0,url,5,100,-5));
+
+ Label username = new Label(composite, SWT.None);
+ username.setLayoutData(createFormData(urlText, 5, null, 0, 0,5,null,0));
+ usernameText = new Text(composite, SWT.BORDER);
+ usernameText.setLayoutData(createFormData(urlText,5,null,0,url,5,100,-5));
+
+ Label pass = new Label(composite, SWT.None);
+ pass.setLayoutData(createFormData(usernameText, 5, null, 0, 0,5,null,0));
+ passText = new Text(composite, SWT.BORDER | SWT.PASSWORD);
+ passText.setLayoutData(createFormData(usernameText,5,null,0,url,5,100,-5));
+
+ name.setText("Name: ");
+ url.setText("Hudson URL: ");
+ username.setText("Username: ");
+ pass.setText("Password: ");
+
+ // Initialize
+ if( initialServer != null ) {
+ // TODO fix this
+ }
+
+ ModifyListener listener = new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ saveVals();
+ }
+ };
+ nameText.addModifyListener(listener);
+ urlText.addModifyListener(listener);
+ usernameText.addModifyListener(listener);
+ passText.addModifyListener(listener);
+ return composite;
+ }
+
+ private void saveVals() {
+ this.name = nameText.getText();
+ this.url = urlText.getText();
+ this.user = usernameText.getText();
+ this.pass = passText.getText();
+
+ // validate
+ HudsonServer[] allServers = HudsonModel.getInstance().getServers();
+ for( int i = 0; i < allServers.length; i++ ) {
+ if( allServers[i].getName().equals(name)) {
+ setErrorMessage("Name already in use");
+ return;
+ }
+ }
+ setErrorMessage(null);
+ }
+
+ public HudsonServer getInitialServer() {
+ return initialServer;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public String getPass() {
+ return pass;
+ }
+}