| /******************************************************************************* |
| * Copyright (c) 2011 University of Illinois 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: |
| * Albert L. Rossi - design and implementation |
| ******************************************************************************/ |
| |
| package org.eclipse.ptp.rm.jaxb.ui.dialogs; |
| |
| import java.io.File; |
| |
| import org.eclipse.ptp.remote.core.IRemoteConnection; |
| import org.eclipse.ptp.remote.core.IRemoteConnectionManager; |
| import org.eclipse.ptp.remote.core.IRemoteServices; |
| import org.eclipse.ptp.remote.core.PTPRemoteCorePlugin; |
| import org.eclipse.ptp.remote.ui.IRemoteUIFileManager; |
| import org.eclipse.ptp.remote.ui.IRemoteUIServices; |
| import org.eclipse.ptp.remote.ui.PTPRemoteUIPlugin; |
| import org.eclipse.ptp.rm.jaxb.core.IJAXBResourceManagerConfiguration; |
| import org.eclipse.ptp.rm.jaxb.core.rm.JAXBRMConfigurationManager; |
| import org.eclipse.ptp.rm.jaxb.core.utils.JAXBInitializationUtils; |
| import org.eclipse.ptp.rm.jaxb.ui.IJAXBUINonNLSConstants; |
| import org.eclipse.ptp.rm.jaxb.ui.JAXBUIPlugin; |
| import org.eclipse.ptp.rm.jaxb.ui.messages.Messages; |
| import org.eclipse.ptp.rm.jaxb.ui.util.ConfigUtils; |
| import org.eclipse.ptp.rm.jaxb.ui.util.WidgetBuilderUtils; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.events.SelectionListener; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Combo; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Group; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.swt.widgets.Text; |
| import org.eclipse.ui.IMemento; |
| |
| public abstract class ConfigurationChoiceContainer implements IJAXBUINonNLSConstants { |
| |
| private class WidgetListener implements SelectionListener { |
| private boolean disabled = false; |
| |
| public void widgetDefaultSelected(SelectionEvent e) { |
| widgetSelected(e); |
| } |
| |
| public synchronized void widgetSelected(SelectionEvent e) { |
| if (disabled) { |
| return; |
| } |
| Object source = e.getSource(); |
| if (source == preset) { |
| handlePresetSelected(); |
| } else if (source == external) { |
| handleExternalSelected(); |
| } else if (source == browseHomeButton) { |
| handlePathBrowseButtonSelected(ConfigUtils.getUserHome()); |
| } else if (source == browseProjectButton) { |
| try { |
| handlePathBrowseButtonSelected(ConfigUtils.chooseLocalProject(shell)); |
| } catch (Throwable t) { |
| JAXBUIPlugin.log(t); |
| } |
| } |
| onUpdate(); |
| } |
| |
| private synchronized void disable() { |
| disabled = true; |
| } |
| |
| private synchronized void enable() { |
| disabled = false; |
| } |
| } |
| |
| private final Text choice; |
| private final Combo preset; |
| private final Combo external; |
| private final Button browseHomeButton; |
| private final Button browseProjectButton; |
| private final WidgetListener listener; |
| private final Shell shell; |
| |
| private String selected; |
| private boolean isPreset; |
| private IJAXBResourceManagerConfiguration config; |
| private IMemento memento; |
| private JAXBRMConfigurationManager available; |
| |
| public ConfigurationChoiceContainer(Composite parent) { |
| shell = parent.getShell(); |
| listener = new WidgetListener(); |
| |
| GridLayout layout = WidgetBuilderUtils.createGridLayout(3, true); |
| GridData gd = WidgetBuilderUtils.createGridDataFillH(3); |
| Group group = WidgetBuilderUtils.createGroup(parent, SWT.SHADOW_ETCHED_IN, layout, gd); |
| |
| WidgetBuilderUtils.createLabel(group, Messages.JAXBRMConfigurationSelectionWizardPage_4, SWT.LEFT, 1); |
| gd = WidgetBuilderUtils.createGridDataFillH(2); |
| choice = WidgetBuilderUtils.createText(group, SWT.BORDER, gd, true, selected); |
| |
| layout = WidgetBuilderUtils.createGridLayout(3, true); |
| gd = WidgetBuilderUtils.createGridDataFillH(3); |
| group = WidgetBuilderUtils.createGroup(parent, SWT.SHADOW_ETCHED_IN, layout, gd); |
| |
| preset = WidgetBuilderUtils.createCombo(group, 2, new String[0], ZEROSTR, |
| Messages.JAXBRMConfigurationSelectionComboTitle_0, ZEROSTR, listener); |
| |
| layout = WidgetBuilderUtils.createGridLayout(3, true); |
| gd = WidgetBuilderUtils.createGridDataFillH(3); |
| group = WidgetBuilderUtils.createGroup(parent, SWT.SHADOW_ETCHED_IN, layout, gd); |
| |
| external = WidgetBuilderUtils.createCombo(group, 2, new String[0], ZEROSTR, |
| Messages.JAXBRMConfigurationSelectionComboTitle_1, ZEROSTR, listener); |
| |
| browseHomeButton = WidgetBuilderUtils.createPushButton(group, Messages.JAXBRMConfigurationSelectionWizardPage_1, listener); |
| browseProjectButton = WidgetBuilderUtils.createPushButton(group, Messages.JAXBRMConfigurationSelectionWizardPage_2, |
| listener); |
| |
| selected = ZEROSTR; |
| isPreset = true; |
| } |
| |
| public boolean choiceIsPreset() { |
| return isPreset; |
| } |
| |
| public String getSelected() { |
| return selected; |
| } |
| |
| public void setAvailableConfigurations() { |
| available = JAXBRMConfigurationManager.getInstance(); |
| |
| if (preset != null) { |
| listener.disable(); |
| preset.setItems(available.getTypes()); |
| listener.enable(); |
| } |
| |
| if (config != null) { |
| available.addExternalPaths(config.getExternalRMInstanceXMLLocations()); |
| } else if (memento != null) { |
| available.addExternalPaths(getExternalLocations()); |
| } |
| |
| if (external != null) { |
| String[] items = available.getExternal(); |
| external.setItems(items); |
| if (config != null) { |
| config.setExternalRMInstanceXMLLocations(items); |
| } else if (memento != null) { |
| setExternalRMInstanceXMLLocations(items); |
| } |
| } |
| |
| if (config != null) { |
| selected = config.getRMInstanceXMLLocation(); |
| } else if (memento != null) { |
| selected = memento.getString(RM_XSD_PATH); |
| } |
| |
| if (selected != null) { |
| String type = available.getTypeForPath(selected); |
| if (type != null) { |
| isPreset = true; |
| choice.setText(type); |
| for (int i = 0; i < preset.getItems().length; i++) { |
| if (type.equals(preset.getItem(i))) { |
| preset.select(i); |
| break; |
| } |
| } |
| } else { |
| isPreset = false; |
| choice.setText(selected); |
| for (int i = 0; i < external.getItems().length; i++) { |
| if (selected.equals(external.getItem(i))) { |
| preset.select(i); |
| break; |
| } |
| } |
| } |
| } |
| onUpdate(); |
| } |
| |
| public void setConfig(IJAXBResourceManagerConfiguration config) { |
| this.config = config; |
| } |
| |
| public void setExternalRMInstanceXMLLocations(String[] locations) { |
| if (locations == null || locations.length == 0) { |
| memento.putString(EXTERNAL_RM_XSD_PATHS, ZEROSTR); |
| } else { |
| StringBuffer list = new StringBuffer(locations[0]); |
| for (int i = 1; i < locations.length; i++) { |
| list.append(CM).append(locations[i]); |
| } |
| memento.putString(EXTERNAL_RM_XSD_PATHS, list.toString()); |
| } |
| } |
| |
| public void setMemento(IMemento memento) { |
| this.memento = memento; |
| } |
| |
| protected abstract void onUpdate(); |
| |
| private String[] getExternalLocations() { |
| String list = memento.getString(EXTERNAL_RM_XSD_PATHS); |
| if (list == null) { |
| return new String[0]; |
| } |
| return list.split(CM); |
| } |
| |
| private void handleExternalSelected() { |
| String text = external.getText(); |
| if (text != null) { |
| selected = text; |
| validateSelected(); |
| isPreset = false; |
| } else { |
| selected = ZEROSTR; |
| } |
| |
| if (memento != null) { |
| memento.putString(RM_XSD_PATH, selected); |
| } else if (config != null) { |
| config.setRMInstanceXMLLocation(selected); |
| } |
| |
| choice.setText(text); |
| } |
| |
| private void handlePathBrowseButtonSelected(File initPath) { |
| if (initPath == null) { |
| return; |
| } |
| IRemoteServices localServices = PTPRemoteCorePlugin.getDefault().getDefaultServices(); |
| IRemoteUIServices localUIServices = PTPRemoteUIPlugin.getDefault().getRemoteUIServices(localServices); |
| if (localServices != null && localUIServices != null) { |
| IRemoteConnectionManager lconnMgr = localServices.getConnectionManager(); |
| IRemoteConnection lconn = lconnMgr.getConnection(ZEROSTR); |
| IRemoteUIFileManager localUIFileMgr = localUIServices.getUIFileManager(); |
| localUIFileMgr.setConnection(lconn); |
| String result = localUIFileMgr.browseFile(shell, Messages.JAXBRMConfigurationSelectionWizardPage_0, |
| initPath.getAbsolutePath(), 0); |
| if (result != null) { |
| selected = result; |
| choice.setText(selected); |
| isPreset = false; |
| updateExternal(); |
| } |
| } |
| } |
| |
| private void handlePresetSelected() { |
| String text = preset.getText(); |
| if (text != null) { |
| selected = available.getPathForType(text); |
| validateSelected(); |
| isPreset = true; |
| } else { |
| selected = ZEROSTR; |
| } |
| |
| if (memento != null) { |
| memento.putString(RM_XSD_PATH, selected); |
| } else if (config != null) { |
| config.setRMInstanceXMLLocation(selected); |
| } |
| |
| choice.setText(text); |
| } |
| |
| private void updateExternal() { |
| int len = external.getItems().length; |
| int i = 0; |
| for (; i < len; i++) { |
| if (selected.equals(external.getItem(i))) { |
| external.select(i); |
| break; |
| } |
| } |
| if (i == len) { |
| available.addExternalPath(selected); |
| String[] refreshed = available.getExternal(); |
| if (config != null) { |
| config.setExternalRMInstanceXMLLocations(refreshed); |
| } else if (memento != null) { |
| setExternalRMInstanceXMLLocations(refreshed); |
| } |
| external.setItems(available.getExternal()); |
| external.select(i); |
| } |
| } |
| |
| private boolean validateSelected() { |
| try { |
| JAXBInitializationUtils.validate(selected); |
| } catch (Throwable t) { |
| JAXBUIPlugin.log(t); |
| return false; |
| } |
| return true; |
| } |
| } |