Skip to content

Commit

Permalink
JBIDE-21697 Server Adapter: user choose choose default route when cre…
Browse files Browse the repository at this point in the history
…ating the server
  • Loading branch information
scabanovich committed Feb 19, 2016
1 parent 5c006f7 commit 8b9d8bb
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 6 deletions.
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.core.runtime.IAdaptable;
import org.jboss.ide.eclipse.as.core.server.internal.extendedproperties.ServerExtendedProperties;
import org.jboss.tools.openshift.common.core.utils.StringUtils;
import org.jboss.tools.openshift.core.IRouteChooser;
import org.jboss.tools.openshift.core.OpenShiftCoreUIIntegration;

Expand Down Expand Up @@ -48,6 +49,14 @@ public String getWelcomePageUrl() {
IProject project = service.getProject();
if (project != null) {
List<IRoute> routes = project.getResources(ResourceKind.ROUTE);
String url = OpenShiftServerUtils.getRouteURL(server);
if(!StringUtils.isEmpty(url)) {
for (IRoute route: routes) {
if(url.equals(route.getURL())) {
return welcomePageUrl = url;
}
}
}
IRoute route = getRoute(routes);
if (route != null) {
welcomePageUrl = route.getURL();
Expand Down
Expand Up @@ -52,6 +52,7 @@ public class OpenShiftServerUtils {
public static final String ATTR_DEPLOYPROJECT = "org.jboss.tools.openshift.DeployProject"; //$NON-NLS-1$
public static final String ATTR_SOURCE_PATH = "org.jboss.tools.openshift.SourcePath"; //$NON-NLS-1$
public static final String ATTR_POD_PATH = "org.jboss.tools.openshift.PodPath"; //$NON-NLS-1$
public static final String ATTR_ROUTE = "org.jboss.tools.openshift.Route"; //$NON-NLS-1$

public static final String ATTR_IGNORE_CONTEXT_ROOT = "org.jboss.tools.openshift.IgnoreContextRoot";//$NON-NLS-1$
public static final String ATTR_OVERRIDE_PROJECT_SETTINGS = "org.jboss.tools.openshift.project.Override";//$NON-NLS-1$
Expand All @@ -74,9 +75,13 @@ public static String getServerName(IService service, Connection connection) {
}

public static void updateServer(String serverName, String connectionUrl, IService service, String podPath, String sourcePath, IProject deployProject, IServerWorkingCopy server) {
updateServer(serverName, connectionUrl, service, podPath, sourcePath, deployProject, null, server);
}

public static void updateServer(String serverName, String connectionUrl, IService service, String podPath, String sourcePath, IProject deployProject, String routeURL, IServerWorkingCopy server) {
String host = getHost(service);
String deployProjectName = ProjectUtils.getName(deployProject);
updateServer(serverName, host, connectionUrl, deployProjectName, OpenShiftResourceUniqueId.get(service), sourcePath, podPath, server);
updateServer(serverName, host, connectionUrl, deployProjectName, OpenShiftResourceUniqueId.get(service), sourcePath, podPath, routeURL, server);
}

private static String getHost(IService service) {
Expand Down Expand Up @@ -128,6 +133,10 @@ private static IRoute getRoute(List<IRoute> routes) {
* the application name for the server adapter
*/
public static void updateServer(String serverName, String host, String connectionUrl, String deployProjectName, String serviceId, String sourcePath, String podPath, IServerWorkingCopy server) {
updateServer(serverName, host, connectionUrl, deployProjectName, serviceId, sourcePath, podPath, null, server);
}

public static void updateServer(String serverName, String host, String connectionUrl, String deployProjectName, String serviceId, String sourcePath, String podPath, String routeURL, IServerWorkingCopy server) {
updateServer(server);

server.setName(serverName);
Expand All @@ -138,6 +147,7 @@ public static void updateServer(String serverName, String host, String connectio
server.setAttribute(ATTR_SOURCE_PATH, sourcePath);
server.setAttribute(ATTR_POD_PATH, podPath);
server.setAttribute(ATTR_SERVICE, serviceId);
server.setAttribute(ATTR_ROUTE, routeURL);
}

private static void updateServer(IServerWorkingCopy server) {
Expand All @@ -153,16 +163,29 @@ private static void updateServer(IServerWorkingCopy server) {
}

public static void updateServerProject(String connectionUrl, IService service, String sourcePath, String podPath, IProject project) {
updateServerProject(connectionUrl, OpenShiftResourceUniqueId.get(service), sourcePath, podPath, project);
updateServerProject(connectionUrl, service, sourcePath, podPath, project);
}

public static void updateServerProject(String connectionUrl, IService service, String sourcePath, String podPath, String routeURL, IProject project) {
updateServerProject(connectionUrl, OpenShiftResourceUniqueId.get(service), sourcePath, podPath, routeURL, project);
}

public static void updateServerProject(String connectionUrl, String serviceId, String sourcePath, String podPath, IProject project) {
updateServerProject(connectionUrl, serviceId, sourcePath, podPath, null, project);
}

public static void updateServerProject(String connectionUrl, String serviceId, String sourcePath, String podPath, String routeURL, IProject project) {
IEclipsePreferences node = ServerUtils.getProjectNode(SERVER_PROJECT_QUALIFIER, project);
node.put(ATTR_CONNECTIONURL, connectionUrl);
node.put(ATTR_DEPLOYPROJECT, project.getName());
node.put(ATTR_SOURCE_PATH, sourcePath);
node.put(ATTR_POD_PATH, podPath);
node.put(ATTR_SERVICE, serviceId);
if(StringUtils.isEmpty(routeURL)) {
node.remove(ATTR_ROUTE);
} else {
node.put(ATTR_ROUTE, routeURL);
}
try {
node.flush();
} catch (BackingStoreException e) {
Expand Down Expand Up @@ -245,6 +268,10 @@ public static IService getService(IServerAttributes attributes, Connection conne
OpenShiftResourceUniqueId.getProject(uniqueId)));
}

public static String getRouteURL(IServerAttributes attributes) {
return getProjectAttribute(ATTR_ROUTE, null, getDeployProject(attributes));
}

public static String getPodPath(IServerAttributes attributes) {
// TODO: implement override project settings with server settings
return getProjectAttribute(ATTR_POD_PATH, null, getDeployProject(attributes));
Expand Down
Expand Up @@ -69,7 +69,7 @@ public IRoute getSelectedRoute() {
}
}

private static class RouteLabelProvider extends LabelProvider {
public static class RouteLabelProvider extends LabelProvider {

@Override
public Image getImage(Object element) {
Expand Down
Expand Up @@ -303,10 +303,11 @@ private void updateServer(IServerWorkingCopy server) throws OpenShiftException {
String baseServerName = OpenShiftServerUtils.getServerName(getService(), getConnection());
//Find a free name based on the computed name
String serverName = ServerUtils.getServerName(baseServerName);
String routeURL = isSelectDefaultRoute() && getRoute() != null ? getRoute().getURL() : null;
OpenShiftServerUtils.updateServer(
serverName, connectionUrl, getService(), sourcePath, podPath, deployProject, server);
serverName, connectionUrl, getService(), sourcePath, podPath, deployProject, routeURL, server);
OpenShiftServerUtils.updateServerProject(
connectionUrl, getService(), sourcePath, podPath, deployProject);
connectionUrl, getService(), sourcePath, podPath, routeURL, deployProject);
}

private String getConnectionUrl(Connection connection) {
Expand Down
Expand Up @@ -82,6 +82,7 @@
import org.eclipse.wst.server.ui.wizard.IWizardHandle;
import org.jboss.ide.eclipse.as.ui.editor.DeploymentTypeUIUtil.ICompletable;
import org.jboss.tools.common.ui.WizardUtils;
import org.jboss.tools.common.ui.databinding.InvertingBooleanConverter;
import org.jboss.tools.common.ui.databinding.ValueBindingBuilder;
import org.jboss.tools.openshift.common.core.OpenShiftCoreException;
import org.jboss.tools.openshift.common.core.utils.ProjectUtils;
Expand All @@ -95,9 +96,11 @@
import org.jboss.tools.openshift.internal.common.ui.databinding.FormPresenterSupport.IFormPresenter;
import org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater;
import org.jboss.tools.openshift.internal.common.ui.utils.UIUtils;
import org.jboss.tools.openshift.internal.ui.dialog.SelectRouteDialog.RouteLabelProvider;
import org.jboss.tools.openshift.internal.ui.treeitem.Model2ObservableTreeItemConverter;
import org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem;
import org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem2ModelConverter;
import org.jboss.tools.openshift.internal.ui.wizard.importapp.IGitCloningPageModel;

import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.IService;
Expand Down Expand Up @@ -224,6 +227,7 @@ private Composite createControls(Composite parent, ServerSettingsViewModel model
createSourcePathControls(container, model, dbc);
createDeploymentControls(container, model, dbc);
createServiceControls(container, model, dbc);
createRouteControls(container, model, dbc);

return container;
}
Expand Down Expand Up @@ -552,6 +556,46 @@ public IStatus validate(Object value) {
new ServiceDetailViews(selectedService, detailsContainer, dbc).createControls();
}

private void createRouteControls(Composite container, ServerSettingsViewModel model, DataBindingContext dbc) {
Group defaultRouteGroup = new Group(container, SWT.NONE);
defaultRouteGroup.setText("Default Route");
GridDataFactory.fillDefaults()
.span(4, 1).align(SWT.FILL, SWT.FILL).grab(true, true)
.applyTo(defaultRouteGroup);
GridLayoutFactory.fillDefaults()
.numColumns(2).margins(10,10)
.applyTo(defaultRouteGroup);

Button selectDefaultRouteButton = new Button(defaultRouteGroup, SWT.CHECK);
selectDefaultRouteButton.setText("Select Route:");
selectDefaultRouteButton.setToolTipText("Uncheck if you want to select route in a dialog");
GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).span(1, 1).applyTo(selectDefaultRouteButton);

StructuredViewer routesViewer = new ComboViewer(defaultRouteGroup);
GridDataFactory.fillDefaults()
.span(1,1).align(SWT.FILL, SWT.CENTER).grab(true, false)
.applyTo(routesViewer.getControl());

routesViewer.setContentProvider(new ObservableListContentProvider());
routesViewer.setLabelProvider(new RouteLabelProvider());
routesViewer.setInput(
BeanProperties.list(ServerSettingsViewModel.PROPERTY_ROUTES).observe(model));

IObservableValue selectedRouteObservable = ViewerProperties.singleSelection().observe(routesViewer);
Binding selectedRouteBinding =
ValueBindingBuilder.bind(selectedRouteObservable)
.to(BeanProperties.value(ServerSettingsViewModel.PROPERTY_ROUTE).observe(model))
.in(dbc);

final IObservableValue isSelectDefaultRouteObservable =
WidgetProperties.selection().observe(selectDefaultRouteButton);
final IObservableValue selectDefaultRouteModelObservable = BeanProperties.value(
ServerSettingsViewModel.PROPERTY_SELECT_DEFAULT_ROUTE).observe(model);
ValueBindingBuilder.bind(isSelectDefaultRouteObservable).to(selectDefaultRouteModelObservable).in(dbc);
ValueBindingBuilder.bind(WidgetProperties.enabled().observe(routesViewer.getControl()))
.notUpdating(selectDefaultRouteModelObservable).in(dbc);
}

private IListChangeListener onServiceItemsChanged(final TreeViewer servicesViewer) {
return new IListChangeListener() {

Expand Down
Expand Up @@ -13,7 +13,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.tools.common.databinding.ObservablePojo;
import org.jboss.tools.openshift.common.core.connection.ConnectionsRegistrySingleton;
Expand All @@ -24,6 +26,7 @@
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.model.IProject;
import com.openshift.restclient.model.IService;
import com.openshift.restclient.model.route.IRoute;

/**
* @author Andre Dietisheim
Expand All @@ -34,11 +37,20 @@ public class ServiceViewModel extends ObservablePojo {
public static final String PROPERTY_CONNECTIONS = "connections";
public static final String PROPERTY_SERVICE = "service";
public static final String PROPERTY_SERVICE_ITEMS = "serviceItems";

public static final String PROPERTY_SELECT_DEFAULT_ROUTE = "selectDefaultRoute";
public static final String PROPERTY_ROUTE = "route";
public static final String PROPERTY_ROUTES = "routes";

private Connection connection;
private List<Connection> connections = new ArrayList<>();
private List<ObservableTreeItem> serviceItems = new ArrayList<>();
private IService service;
private boolean selectDefaultRoute = false;
private List<IRoute> routes = new ArrayList<>();
private IRoute route;

private Map<IProject, List<IRoute>> routeMap = new HashMap<>();

public ServiceViewModel(Connection connection) {
this(null, connection);
Expand Down Expand Up @@ -85,7 +97,56 @@ private void updateServiceItems(List<ObservableTreeItem> newServiceItems) {
}

protected void updateService(IService service, List<ObservableTreeItem> serviceItems) {
firePropertyChange(PROPERTY_SERVICE, null, this.service = getServiceOrDefault(service, serviceItems));
IService newService = getServiceOrDefault(service, serviceItems);
if(newService != this.service) {
boolean needUpdateRoutes = newService == null || this.service == null || newService.getProject() != this.service.getProject();
firePropertyChange(PROPERTY_SERVICE, null, this.service = newService);
if(needUpdateRoutes) {
updateRoutes();
}
}
}

/**
* Replaces choices in the route selector as needed.
* If choices are replaced calls updateRoute() to reset its selected value.
*/
protected void updateRoutes() {
IRoute routeToReset = null;
if(this.service == null && !routes.isEmpty()) {
List<IRoute> oldRoutes = new ArrayList<>(this.routes);
this.routes.clear();
firePropertyChange(PROPERTY_ROUTES, oldRoutes, this.routes);
routeToReset = null;
} else if(this.service != null) {
List<IRoute> oldRoutes = new ArrayList<>(this.routes);
this.routes.clear();
List<IRoute> newRoutes = routeMap.get(this.service.getProject());
if(newRoutes != null) {
this.routes.addAll(newRoutes);
}
routeToReset = this.route;
firePropertyChange(PROPERTY_ROUTES, oldRoutes, this.routes);
} else {
return; //No need to reset the selected route.
}
updateRoute(routeToReset);
}

/**
* Called when route is chosen in the route selector and when choices in the route selector
* are replaced because of change in selected service.
* @param route
*/
protected void updateRoute(IRoute route) {
if(route != null && !this.routes.contains(route)) {
//clean to kick combo.
firePropertyChange(PROPERTY_ROUTE, this.route, this.route = null);
}
if(route == null || !this.routes.contains(route)) {
route = this.routes.isEmpty() ? null : this.routes.get(0);
}
firePropertyChange(PROPERTY_ROUTE, this.route, this.route = route);
}

public void setConnections(List<Connection> connections) {
Expand Down Expand Up @@ -125,6 +186,26 @@ public void setService(IService service) {
update(this.connection, this.connections, service, this.serviceItems);
}

public boolean isSelectDefaultRoute() {
return selectDefaultRoute;
}

public void setSelectDefaultRoute(boolean selectDefaultRoute) {
firePropertyChange(PROPERTY_SELECT_DEFAULT_ROUTE, this.selectDefaultRoute, this.selectDefaultRoute = selectDefaultRoute);
}

public List<IRoute> getRoutes() {
return routes;
}

public IRoute getRoute() {
return route;
}

public void setRoute(IRoute newRoute) {
updateRoute(newRoute);
}

protected IService getServiceOrDefault(IService service, List<ObservableTreeItem> items) {
if (service == null || !containsService(items, service)) {
service = getDefaultService(items);
Expand Down Expand Up @@ -170,6 +251,7 @@ public void loadResources() {
if (connection == null) {
return;
}
loadRoutes(connection);
setServiceItems(loadServices(connection));
}

Expand All @@ -183,9 +265,18 @@ public void loadResources(Connection newConnection) {
if (newConnection == null) {
return;
}
loadRoutes(newConnection);
setServiceItems(loadServices(newConnection));
}

void loadRoutes(Connection newConnection) {
routeMap.clear();
List<IProject> projects = newConnection.getResources(ResourceKind.PROJECT);
if(projects != null) {
projects.stream().forEach(project -> routeMap.put(project, project.getResources(ResourceKind.ROUTE)));
}
}

private List<Connection> loadConnections() {
return (List<Connection>) ConnectionsRegistrySingleton.getInstance().getAll(Connection.class);
}
Expand Down

0 comments on commit 8b9d8bb

Please sign in to comment.