Author: adietish
Date: 2012-10-12 17:23:49 -0400 (Fri, 12 Oct 2012)
New Revision: 44495
Added:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionAwareConnectionComparer.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionMarker.java
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/Connection.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/ConnectionsModel.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/ConnectionColumLabelProvider.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPage.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPageModel.java
Log:
[JBIDE-12798] implemented connection dialog that allows to select existing connections or
create new ones
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/Connection.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/Connection.java 2012-10-12
18:10:47 UTC (rev 44494)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/Connection.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -97,22 +97,21 @@
if (!StringUtils.isEmpty(username)) {
return username;
}
- username = OpenShiftPreferences.INSTANCE.getLastUsername();
- if (StringUtils.isEmpty(username)) {
- username = getDefaultUsername();
- }
- return username;
+ return getDefaultUsername();
}
private String getDefaultUsername() {
- try {
- return getOpenShiftConfiguration().getRhlogin();
- } catch (Exception e) {
- Logger.error("Could not load default user name from OpenShift
configuration.", e);
+ String username = OpenShiftPreferences.INSTANCE.getLastUsername();
+ if (StringUtils.isEmpty(username)) {
+ try {
+ username = getOpenShiftConfiguration().getRhlogin();
+ } catch (Exception e) {
+ Logger.error("Could not load default user name from OpenShift
configuration.", e);
+ }
}
- return null;
+ return username;
}
-
+
private void setUser(IUser user) {
if (user == null) {
return;
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/ConnectionsModel.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/ConnectionsModel.java 2012-10-12
18:10:47 UTC (rev 44494)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/core/connection/ConnectionsModel.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -74,6 +74,18 @@
}
}
+ public boolean hasConnection(Connection connection) {
+ try {
+ String url = connection.toURLString();
+ return getConnectionByUrl(url) != null;
+ } catch (UnsupportedEncodingException e) {
+ OpenShiftUIActivator.log(
+ NLS.bind("Could not get url for connection {0} - {1}",
connection.getUsername(),
+ connection.getHost()), e);
+ return false;
+ }
+ }
+
public void fireConnectionChanged(Connection connection) {
fireModelChange(connection, CHANGED);
}
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/ConnectionColumLabelProvider.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/ConnectionColumLabelProvider.java 2012-10-12
18:10:47 UTC (rev 44494)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/ConnectionColumLabelProvider.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -18,8 +18,14 @@
*/
public class ConnectionColumLabelProvider extends ColumnLabelProvider {
+ private static final String LABEL_NEW_CONNECTION = "<New Connection>";
+
@Override
public String getText(Object element) {
+ if (element instanceof NewConnectionMarker) {
+ return LABEL_NEW_CONNECTION;
+ }
+
if (!(element instanceof Connection)) {
return super.getText(element);
}
Added:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionAwareConnectionComparer.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionAwareConnectionComparer.java
(rev 0)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionAwareConnectionComparer.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat Inc..
+ * All rights reserved. This program and the accompanying materials
+ * are 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 Incorporated - initial API and implementation
+ *******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.viewer;
+
+import org.eclipse.jface.viewers.IElementComparer;
+import org.jboss.tools.openshift.express.internal.core.connection.Connection;
+
+/**
+ * @author Andre Dietisheim
+ */
+public class NewConnectionAwareConnectionComparer implements IElementComparer {
+
+ @Override
+ public int hashCode(Object element) {
+ return 0;
+ }
+
+ @Override
+ public boolean equals(Object thisObject, Object thatObject) {
+ if (thisObject == null) {
+ if (thatObject == null) {
+ return true;
+ } else if (thatObject instanceof NewConnectionMarker) {
+ return true;
+ } else {
+ return false;
+ }
+ } else if (thisObject instanceof NewConnectionMarker) {
+ if (thatObject == null
+ || thatObject instanceof NewConnectionMarker) {
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ if (thatObject == null) {
+ return false;
+ } else if (thatObject instanceof NewConnectionMarker) {
+ return true;
+ }
+ Connection thisConnection = (Connection) thisObject;
+ Connection thatConnection = (Connection) thatObject;
+ return thisConnection.getUsername().equals(thatConnection.getUsername())
+ && thisConnection.getHost().equals(thatConnection.getHost());
+ }
+ }
+}
Property changes on:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionAwareConnectionComparer.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionMarker.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionMarker.java
(rev 0)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionMarker.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat Inc..
+ * All rights reserved. This program and the accompanying materials
+ * are 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 Incorporated - initial API and implementation
+ *******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.viewer;
+
+import org.jboss.tools.openshift.express.internal.core.connection.Connection;
+
+/**
+ * A class that is used to have an entry <New Connection> in a combo view with
+ * connections.
+ *
+ * @author Andre Dietisheim
+ *
+ */
+public class NewConnectionMarker extends Connection {
+
+ public NewConnectionMarker() {
+ super();
+ }
+}
Property changes on:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/viewer/NewConnectionMarker.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPage.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPage.java 2012-10-12
18:10:47 UTC (rev 44494)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPage.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -16,6 +16,9 @@
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.core.databinding.observable.value.IValueChangeListener;
+import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
+import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -24,20 +27,20 @@
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
-import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.PageChangingEvent;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.wizard.IWizard;
-import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
@@ -59,6 +62,9 @@
import org.jboss.tools.openshift.express.internal.ui.utils.Logger;
import org.jboss.tools.openshift.express.internal.ui.utils.StringUtils;
import org.jboss.tools.openshift.express.internal.ui.utils.UIUpdatingJob;
+import
org.jboss.tools.openshift.express.internal.ui.viewer.ConnectionColumLabelProvider;
+import
org.jboss.tools.openshift.express.internal.ui.viewer.NewConnectionAwareConnectionComparer;
+import org.jboss.tools.openshift.express.internal.ui.viewer.NewConnectionMarker;
import org.jboss.tools.openshift.express.internal.ui.wizard.AbstractOpenShiftWizardPage;
import org.jboss.tools.openshift.express.internal.ui.wizard.IConnectionAwareModel;
@@ -94,8 +100,133 @@
GridDataFactory.fillDefaults()
.span(2, 1).hint(SWT.DEFAULT, 6).applyTo(fillerLabel);
+ Label connectionLabel = new Label(container, SWT.NONE);
+ connectionLabel.setText("Connection:");
+ GridDataFactory.fillDefaults()
+ .align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(connectionLabel);
+ Combo connectionCombo = new Combo(container, SWT.DEFAULT);
+ GridDataFactory.fillDefaults()
+ .align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(connectionCombo);
+ final ComboViewer connectionComboViewer = new ComboViewer(connectionCombo);
+ connectionComboViewer.setContentProvider(ArrayContentProvider.getInstance());
+ connectionComboViewer.setLabelProvider(new ConnectionColumLabelProvider());
+ connectionComboViewer.setInput(pageModel.getConnections());
+ connectionComboViewer.setComparer(new NewConnectionAwareConnectionComparer());
+
+ Binding selectedConnectionBinding = ValueBindingBuilder
+ .bind(ViewerProperties.singleSelection().observe(connectionComboViewer))
+ .validatingAfterGet(new IValidator() {
+
+ @Override
+ public IStatus validate(Object value) {
+ if (value == null) {
+ return ValidationStatus.cancel("You have to select or create a new
connection.");
+ }
+ return ValidationStatus.ok();
+ }
+ })
+ .to(BeanProperties.value(
+ ConnectionWizardPageModel.PROPERTY_SELECTED_CONNECTION, Connection.class).observe(
+ pageModel))
+ .in(dbc);
+ ControlDecorationSupport
+ .create(selectedConnectionBinding, SWT.LEFT | SWT.TOP, null, new
RequiredControlDecorationUpdater());
+
+ // stack with connection widgets / password widget
+ Composite connectionWidgetsContainer = new Composite(container, SWT.NONE);
+ GridDataFactory.fillDefaults()
+ .span(2, 1).applyTo(connectionWidgetsContainer);
+ StackLayout stackLayout = new StackLayout();
+ connectionWidgetsContainer.setLayout(stackLayout);
+ Composite connectionWidgets = createConnectionWidgets(connectionWidgetsContainer,
dbc);
+ Composite passwordWidgets = createPasswordWidgets(connectionWidgetsContainer, dbc);
+
+ showConnectionWidgets(pageModel.isCreateNewConnection(), passwordWidgets,
connectionWidgets, stackLayout,
+ connectionWidgetsContainer);
+
+ BeanProperties
+ .value(ConnectionWizardPageModel.PROPERTY_SELECTED_CONNECTION)
+ .observe(pageModel)
+ .addValueChangeListener(
+ onNewConnectionSelected(passwordWidgets, connectionWidgets, stackLayout,
+ connectionWidgetsContainer));
+ }
+
+ private IValueChangeListener onNewConnectionSelected(final Composite passwordWidget,
+ final Composite connectionWidgets, final StackLayout stackLayout, final Composite
container) {
+ return new IValueChangeListener() {
+
+ @Override
+ public void handleValueChange(ValueChangeEvent event) {
+ Connection selectedConnection = pageModel.getSelectedConnection();
+ boolean isNewConnection = selectedConnection instanceof NewConnectionMarker;
+ showConnectionWidgets(isNewConnection, passwordWidget, connectionWidgets,
stackLayout, container);
+ }
+ };
+ }
+
+ private void showConnectionWidgets(boolean isNewConnection, Composite passwordWidgets,
+ Composite connectionsWidgets,
+ StackLayout stackLayout, Composite container) {
+ Control topControl = null;
+ if (isNewConnection) {
+ topControl = connectionsWidgets;
+ } else {
+ topControl = passwordWidgets;
+ }
+ stackLayout.topControl = topControl;
+ container.layout();
+ }
+
+ private Composite createPasswordWidgets(Composite container, DataBindingContext dbc) {
+ Composite passwordWidgets = new Composite(container, SWT.NONE);
+ GridLayoutFactory.fillDefaults()
+ .numColumns(2).applyTo(passwordWidgets);
+
+ // password
+ Label passwordLabel = new Label(passwordWidgets, SWT.NONE);
+ passwordLabel.setText("&Password:");
+ GridDataFactory.fillDefaults()
+ .align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(passwordLabel);
+ passwordText = new Text(passwordWidgets, SWT.BORDER | SWT.PASSWORD);
+ GridDataFactory.fillDefaults()
+ .align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(passwordText);
+ Binding passwordBinding = ValueBindingBuilder
+ .bind(WidgetProperties.text(SWT.Modify).observe(passwordText))
+ .validatingAfterGet(new RequiredStringValidator("password"))
+ .to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_PASSWORD).observe(pageModel))
+ .in(dbc);
+ ControlDecorationSupport
+ .create(passwordBinding, SWT.LEFT | SWT.TOP, null, new
RequiredControlDecorationUpdater());
+
+ // remember password
+ Button rememberPasswordCheckBox = new Button(passwordWidgets, SWT.CHECK);
+ rememberPasswordCheckBox.setText(OpenshiftUIMessages.OpenshiftWizardSavePassword);
+ GridDataFactory.fillDefaults()
+ .align(SWT.FILL, SWT.CENTER).span(2, 1).grab(true,
false).applyTo(rememberPasswordCheckBox);
+ ValueBindingBuilder
+ .bind(WidgetProperties.selection().observe(rememberPasswordCheckBox))
+ .to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_REMEMBER_PASSWORD).observe(pageModel))
+ .in(dbc);
+
+ // credentials status
+ IObservableValue credentialsStatusObservable =
+ BeanProperties.value(ConnectionWizardPageModel.PROPERTY_VALID,
IStatus.class).observe(pageModel);
+ final CredentialsValidator credentialsValidator =
+ new CredentialsValidator(credentialsStatusObservable);
+ dbc.addValidationStatusProvider(credentialsValidator);
+ ControlDecorationSupport.create(credentialsValidator, SWT.LEFT | SWT.TOP);
+
+ return passwordWidgets;
+ }
+
+ private Composite createConnectionWidgets(Composite container, DataBindingContext dbc)
{
+ Composite connectionWidgets = new Composite(container, SWT.NONE);
+ GridLayoutFactory.fillDefaults()
+ .numColumns(2).applyTo(connectionWidgets);
+
// use default server
- Button defaultServerCheckbox = new Button(container, SWT.CHECK);
+ Button defaultServerCheckbox = new Button(connectionWidgets, SWT.CHECK);
defaultServerCheckbox.setText("Use default server");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).span(2, 1).applyTo(defaultServerCheckbox);
@@ -105,11 +236,11 @@
.in(dbc);
// server
- Label serverLabel = new Label(container, SWT.NONE);
+ Label serverLabel = new Label(connectionWidgets, SWT.NONE);
serverLabel.setText("&Server:");
GridDataFactory.fillDefaults()
- .align(SWT.LEFT, SWT.CENTER).applyTo(serverLabel);
- Combo serversCombo = new Combo(container, SWT.BORDER);
+ .align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(serverLabel);
+ Combo serversCombo = new Combo(connectionWidgets, SWT.BORDER);
Binding serverBinding = ValueBindingBuilder
.bind(WidgetProperties.text().observe(serversCombo))
.validatingAfterGet(new HostNameValidator())
@@ -135,11 +266,11 @@
.in(dbc);
// username
- Label rhLoginLabel = new Label(container, SWT.NONE);
+ Label rhLoginLabel = new Label(connectionWidgets, SWT.NONE);
rhLoginLabel.setText("&Username:");
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).applyTo(rhLoginLabel);
- rhLoginText = new Text(container, SWT.BORDER);
+ rhLoginText = new Text(connectionWidgets, SWT.BORDER);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).span(1, 1).applyTo(rhLoginText);
Binding usernameBinding = ValueBindingBuilder
@@ -152,11 +283,11 @@
.create(usernameBinding, SWT.LEFT | SWT.TOP, null, new
RequiredControlDecorationUpdater());
// password
- Label passwordLabel = new Label(container, SWT.NONE);
+ Label passwordLabel = new Label(connectionWidgets, SWT.NONE);
passwordLabel.setText("&Password:");
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).applyTo(passwordLabel);
- passwordText = new Text(container, SWT.BORDER | SWT.PASSWORD);
+ passwordText = new Text(connectionWidgets, SWT.BORDER | SWT.PASSWORD);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(passwordText);
Binding passwordBinding = ValueBindingBuilder
@@ -167,7 +298,7 @@
ControlDecorationSupport
.create(passwordBinding, SWT.LEFT | SWT.TOP, null, new
RequiredControlDecorationUpdater());
- Button rememberPasswordCheckBox = new Button(container, SWT.CHECK);
+ Button rememberPasswordCheckBox = new Button(connectionWidgets, SWT.CHECK);
rememberPasswordCheckBox.setText(OpenshiftUIMessages.OpenshiftWizardSavePassword);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).span(2, 1).grab(true,
false).applyTo(rememberPasswordCheckBox);
@@ -183,6 +314,7 @@
dbc.addValidationStatusProvider(credentialsValidator);
ControlDecorationSupport.create(credentialsValidator, SWT.LEFT | SWT.TOP);
+ return connectionWidgets;
}
protected SelectionAdapter onSignupLinkClicked() {
@@ -253,6 +385,15 @@
}
}
+ public Connection getConnection() {
+ return pageModel.getConnection();
+ }
+
+ @Override
+ protected void setupWizardPageSupport(DataBindingContext dbc) {
+ ParametrizableWizardPageSupport.create(IStatus.ERROR | IStatus.CANCEL, this, dbc);
+ }
+
private class ServerLabelProvider extends AbstractLabelProvider {
@Override
public String getText(Object element) {
@@ -263,11 +404,6 @@
}
}
- @Override
- protected void setupWizardPageSupport(DataBindingContext dbc) {
- ParametrizableWizardPageSupport.create(IStatus.ERROR | IStatus.CANCEL, this, dbc);
- }
-
private class ConnectJob extends UIUpdatingJob {
private IStatus connectionStatus;
@@ -288,24 +424,9 @@
if (!JobUtils.isOk(connectionStatus)) {
return Status.OK_STATUS;
}
- boolean newConnection = false;
- if (pageModel.shouldCreateNewConnection()) {
- Connection connection = pageModel.getConnection();
- if (MessageDialog.openQuestion(getShell(),
- "Create new connection?",
- NLS.bind("You changed your connection to {0} on server {1}.\n" +
- "Do you want to create a new connection?",
- connection.getUsername(), connection.getHost()))) {
- newConnection = true;
- }
- }
- pageModel.createOrUpdateConnection(newConnection);
+ pageModel.createOrUpdateConnection();
+
return Status.OK_STATUS;
}
}
-
- public Connection getConnection(){
- return pageModel.getConnection();
- }
-
}
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPageModel.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPageModel.java 2012-10-12
18:10:47 UTC (rev 44494)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/connection/ConnectionWizardPageModel.java 2012-10-12
21:23:49 UTC (rev 44495)
@@ -22,6 +22,8 @@
import org.jboss.tools.openshift.express.internal.core.connection.Connection;
import org.jboss.tools.openshift.express.internal.core.connection.ConnectionsModel;
import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
+import org.jboss.tools.openshift.express.internal.ui.utils.CollectionUtils;
+import org.jboss.tools.openshift.express.internal.ui.viewer.NewConnectionMarker;
import org.jboss.tools.openshift.express.internal.ui.wizard.IConnectionAwareModel;
import com.openshift.client.IUser;
@@ -36,25 +38,45 @@
*/
public class ConnectionWizardPageModel extends ObservableUIPojo {
+ public static final String PROPERTY_SELECTED_CONNECTION =
"selectedConnection";
public static final String PROPERTY_USERNAME = "username";
public static final String PROPERTY_PASSWORD = "password";
public static final String PROPERTY_SERVER = "server";
public static final String PROPERTY_REMEMBER_PASSWORD = "rememberPassword";
public static final String PROPERTY_USE_DEFAULTSERVER = "useDefaultServer";
public static final String PROPERTY_VALID = "valid";
-
+ public static final String PROPERTY_DUPLICATE_CONNECTION =
"duplicateConnection";
+ public static final String PROPERTY_CREATE_CONNECTION = "createConnection";
+
+ final private IConnectionAwareModel wizardModel;
+ private Connection selectedConnection;
final private List<String> servers;
private boolean isDefaultServer = true;
- private IConnectionAwareModel wizardModel;
- private Connection connection;
+ private Connection editedConnection;
private IStatus valid;
-
+ private boolean isCreateNewConnection;
+
public ConnectionWizardPageModel(IConnectionAwareModel wizardModel) {
this.wizardModel = wizardModel;
- this.connection = createConnection(wizardModel.getConnection());
- this.servers = getServers(connection);
+ Connection wizardModelConnection = wizardModel.getConnection();
+ this.selectedConnection = getSelectedConnection(wizardModelConnection);
+ this.isCreateNewConnection = getIsCreateNewConnection(selectedConnection);
+ this.editedConnection = createConnection(wizardModelConnection);
+ this.servers = getServers(editedConnection);
}
+ private boolean getIsCreateNewConnection(Connection connection) {
+ return connection instanceof NewConnectionMarker;
+ }
+
+ private Connection getSelectedConnection(Connection wizardModelConnection) {
+ if (wizardModelConnection == null) {
+ return new NewConnectionMarker();
+ } else {
+ return wizardModelConnection;
+ }
+ }
+
/**
* Returns a new Connection for a given connection. The new connection gets
* created with the username and password from the preferences (and secure
@@ -70,15 +92,45 @@
* @see IUser
*/
private Connection createConnection(Connection connection) {
- Connection newUser = null;
- if (connection != null) {
- newUser = new Connection(connection);
+ Connection newConnection = null;
+ if (connection instanceof NewConnectionMarker
+ || connection == null) {
+ newConnection = new Connection();
} else {
- newUser = new Connection();
+ newConnection = new Connection(connection);
}
- return newUser;
+ return newConnection;
}
+ public void setSelectedConnection(Connection connection) {
+ if ((this.selectedConnection instanceof NewConnectionMarker
+ && connection instanceof NewConnectionMarker)
+ || Diffs.equals(selectedConnection, connection)) {
+ return;
+ }
+ this.isCreateNewConnection = getIsCreateNewConnection(connection);
+ setEditedConnection(createConnection(connection));
+ firePropertyChange(PROPERTY_SELECTED_CONNECTION, this.selectedConnection,
this.selectedConnection = connection);
+ }
+
+ private void setEditedConnection(Connection connection) {
+ Connection oldValue = editedConnection;
+ this.editedConnection = connection;
+ firePropertyChange(PROPERTY_SERVER, oldValue.getHost(),
this.editedConnection.getHost());
+ firePropertyChange(PROPERTY_USERNAME, oldValue.getUsername(),
this.editedConnection.getUsername());
+ firePropertyChange(PROPERTY_PASSWORD, oldValue.getPassword(),
this.editedConnection.getPassword());
+ }
+
+ public Connection getSelectedConnection() {
+ return selectedConnection;
+ }
+
+ public List<Connection> getConnections() {
+ List<Connection> connections =
CollectionUtils.toList(ConnectionsModel.getDefault().getConnections());
+ connections.add(new NewConnectionMarker());
+ return connections;
+ }
+
public boolean isUseDefaultServer() {
return isDefaultServer;
}
@@ -88,17 +140,17 @@
firePropertyChange(PROPERTY_USE_DEFAULTSERVER,
this.isDefaultServer, this.isDefaultServer = isDefaultServer);
if (isDefaultServer) {
- setServer(connection.getHost());
+ setServer(editedConnection.getHost());
}
resetValid();
}
}
- private List<String> getServers(Connection user) {
+ private List<String> getServers(Connection connection) {
List<String> servers = new ArrayList<String>();
HashSet<String> uniqueServers = new HashSet<String>();
uniqueServers.add(getDefaultServer());
- servers.add(user.getHost());
+ servers.add(connection.getHost());
return servers;
}
@@ -110,40 +162,44 @@
return null;
}
}
-
+
public String getUsername() {
- return connection.getUsername();
+ return editedConnection.getUsername();
}
public void setUsername(String username) {
- if (!Diffs.equals(connection.getUsername(), username)) {
- firePropertyChange(PROPERTY_USERNAME, connection.getUsername(),
connection.setUsername(username));
+ if (!Diffs.equals(editedConnection.getUsername(), username)) {
+ firePropertyChange(PROPERTY_USERNAME, editedConnection.getUsername(),
+ editedConnection.setUsername(username));
resetValid();
+ fireDuplicateConnectionUpdated();
}
}
public String getPassword() {
- return connection.getPassword();
+ return editedConnection.getPassword();
}
public void setPassword(String password) {
- if (!Diffs.equals(password, connection.getPassword())) {
- firePropertyChange(PROPERTY_PASSWORD, connection.getPassword(),
connection.setPassword(password));
+ if (!Diffs.equals(password, editedConnection.getPassword())) {
+ firePropertyChange(PROPERTY_PASSWORD, editedConnection.getPassword(),
+ editedConnection.setPassword(password));
resetValid();
}
}
public String getServer() {
- return connection.getHost();
+ return editedConnection.getHost();
}
public void setServer(String server) {
if (server == null) { // workaround
return;
}
- if (!Diffs.equals(connection.getHost(), server)) {
- firePropertyChange(PROPERTY_SERVER, connection.getHost(),
connection.setHost(server));
+ if (!Diffs.equals(editedConnection.getHost(), server)) {
+ firePropertyChange(PROPERTY_SERVER, editedConnection.getHost(),
editedConnection.setHost(server));
resetValid();
+ fireDuplicateConnectionUpdated();
}
}
@@ -152,12 +208,12 @@
}
public boolean isRememberPassword() {
- return connection.isRememberPassword();
+ return editedConnection.isRememberPassword();
}
public void setRememberPassword(boolean rememberPassword) {
firePropertyChange(PROPERTY_REMEMBER_PASSWORD,
- connection.isRememberPassword(), connection.setRememberPassword(rememberPassword));
+ editedConnection.isRememberPassword(),
editedConnection.setRememberPassword(rememberPassword));
}
private void resetValid() {
@@ -175,15 +231,15 @@
public IStatus connect() {
IStatus status = Status.OK_STATUS;
try {
- if (!connection.isConnected()) {
+ if (!editedConnection.isConnected()) {
try {
- connection.connect();
+ editedConnection.connect();
} catch (OpenShiftTimeoutException e) {
status = OpenShiftUIActivator.createErrorStatus(NLS.bind(
- "Could not reach server at {0}. Connection timeouted.",
connection.getHost()));
+ "Could not reach server at {0}. Connection timeouted.",
editedConnection.getHost()));
} catch (OpenShiftException e) {
status = OpenShiftUIActivator.createErrorStatus(NLS.bind(
- "The credentials for user {0} are not valid",
connection.getUsername()));
+ "The credentials for user {0} are not valid",
editedConnection.getUsername()));
}
}
} catch (NotFoundOpenShiftException e) {
@@ -196,24 +252,23 @@
return status;
}
- /**
- * Returns <code>true</code> if the connection that this wizards edits was
- * changed in a way that would suggest creating a new connection (instead of
- * updating it). The current implementation would suggests to create a new
- * connection as soon as the username or the server was changed.
- *
- * @return true if you should create a new connection
- */
- public boolean shouldCreateNewConnection() {
- Connection wizardModelConnection = wizardModel.getConnection();
- return wizardModelConnection != null
- && (
- // username changed
- !wizardModelConnection.getUsername().equals(connection.getUsername())
- // server changed
- || !wizardModelConnection.getHost().equals(connection.getHost()));
+ public boolean isDuplicateConnection() {
+ return editedConnection == null
+ || ConnectionsModel.getDefault().hasConnection(editedConnection);
}
+ private void fireDuplicateConnectionUpdated() {
+ firePropertyChange(PROPERTY_DUPLICATE_CONNECTION, null, isDuplicateConnection());
+ }
+
+ public Connection getConnection() {
+ return editedConnection;
+ }
+
+ public boolean isCreateNewConnection() {
+ return isCreateNewConnection;
+ }
+
/**
* Updates the connection that this wizard operates on or creates a new one.
* Will create a new connection if the wizard had no connection to operate
@@ -224,21 +279,18 @@
* if true, creates a new connection if the wizard had a
* connection to edit. Updates the existing one otherwise.
*/
- public void createOrUpdateConnection(boolean create) {
+ public void createOrUpdateConnection() {
Connection wizardModelConnection = wizardModel.getConnection();
if (wizardModelConnection == null
- || create) {
- wizardModel.setConnection(connection);
- ConnectionsModel.getDefault().addConnection(connection);
-// connection.save();
+ || isCreateNewConnection()) {
+ wizardModel.setConnection(editedConnection);
+ ConnectionsModel.getDefault().addConnection(editedConnection);
+ // editedConnection.save();
} else {
- wizardModelConnection.update(connection);
+ wizardModelConnection.update(editedConnection);
ConnectionsModel.getDefault().fireConnectionChanged(wizardModelConnection);
-// wizardModelConnection.save();
+ // wizardModelConnection.save();
}
}
- public Connection getConnection() {
- return connection;
- }
}