/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.design.wizards;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.dialogs.AddPortletModeDialog;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.models.WGAPortletModesModel;
import de.innovationgate.eclipse.utils.ui.EditableTableControl;
import de.innovationgate.eclipse.utils.ui.FolderSelectionPage;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class NewWGAPortletPage extends ValidatedWizardPage {
private Text _txtPortletName;
private Text _txtTargetLocation;
private EditableTableControl<String> _tableModes;
private WGAPortletModesModel _model;
private FolderSelectionPage _selectionPage;
private boolean _created = false;
private IContainer _selectedContainer;
public boolean isCreated() {
return _created;
}
public NewWGAPortletPage(String title, String description) {
super(title);
setTitle(title);
setPageComplete(false);
setDescription(description);
}
public NewWGAPortletPage(String title, String description, IStructuredSelection selection) {
super(title);
setTitle(title);
setPageComplete(false);
setDescription(description);
if (selection != null && selection.getFirstElement() instanceof IResource) {
IResource selectedResource = (IResource) selection.getFirstElement();
if (selectedResource instanceof IContainer) {
_selectedContainer = (IContainer) selection.getFirstElement();
}
if(selectedResource instanceof IFile){
_selectedContainer = ((IFile) selection.getFirstElement()).getParent();
}
}
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.verticalSpacing = 9;
container.setLayout(layout);
Display display = getWizard().getContainer().getShell().getDisplay();
new Label(container, SWT.NULL).setText("Portlet name:");
_txtPortletName = new Text(container, SWT.BORDER);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL | SWT.LEFT);
_txtPortletName.setLayoutData(txtStyle);
_txtPortletName.addModifyListener(this);
String defaultPortletName = ((NewWGAPortlet) getWizard()).getDefaultPortletName();
if (defaultPortletName != null) {
_txtPortletName.setText(defaultPortletName);
}
new Label(container, SWT.None).setText("Target location: ");
_txtTargetLocation = new Text(container, SWT.READ_ONLY);
_txtTargetLocation.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
GridData innerLayout = new GridData(GridData.FILL_HORIZONTAL);
_txtTargetLocation.setLayoutData(innerLayout);
if(_selectedContainer!=null){
_txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
}
GridData data = new GridData(GridData.FILL_BOTH);
data.horizontalSpan = 2;
_tableModes = new EditableTableControl<String>(container, SWT.NONE);
_tableModes.setLayoutData(data);
String[] columnNames = new String[] { "Mode Name" };
_model = new WGAPortletModesModel(new ArrayList<String>());
_tableModes.init(columnNames, _model);
_tableModes.getButton(EditableTableControl.BUTTON_ADD).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAdd();
}
});
_tableModes.getButton(EditableTableControl.BUTTON_REMOVE).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemove();
}
});
_tableModes.getTable().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (_tableModes.getTable().getSelectionCount() > 0) {
String mode = (String) _tableModes.getTable().getSelection()[0].getData();
if (mode.equals("view")) {
_tableModes.getButton(EditableTableControl.BUTTON_REMOVE).setEnabled(false);
} else {
_tableModes.getButton(EditableTableControl.BUTTON_REMOVE).setEnabled(true);
}
} else {
_tableModes.getButton(EditableTableControl.BUTTON_REMOVE).setEnabled(false);
}
}
});
setControl(container);
_created = true;
}
public List<IStatus> validate() {
List<IStatus> list = new ArrayList<IStatus>();
if (_txtPortletName.getText().equals("")) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Please specify a name for the portlet"));
} else if (!WGADesignStructureHelper.isValidModuleName(_txtPortletName.getText())) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Invalid chars in portlet name."));
} else {
IContainer targetContainer = _selectedContainer.getFolder(new Path(_txtPortletName.getText()));
IFile targetFile = _selectedContainer.getFile(new Path(_txtPortletName.getText()));
if (targetContainer == null || targetContainer.exists()) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a folder named \"" + _txtPortletName.getText() + "\" below \"" + _selectedContainer.getName() + "\"."));
} else if (targetFile == null || targetFile.exists()) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a file named \"" + _txtPortletName.getText() + "\" below \"" + _selectedContainer.getName() + "\"."));
}
}
return list;
}
private void dialogChanged() {
performValidation();
}
private void handleAdd() {
AddPortletModeDialog addDialog = new AddPortletModeDialog(getShell(), _model);
addDialog.open();
if (addDialog.getReturnCode() == Dialog.OK) {
_model.add(addDialog.getMode());
}
}
private void handleRemove() {
if (_tableModes.getTable().getSelectionCount() > 0) {
String mode = (String) _tableModes.getTable().getSelection()[0].getData();
_model.remove(mode);
}
}
public String getPortletName() {
return _txtPortletName.getText();
}
public IContainer getTargetContainer() {
return _selectedContainer;
}
public List<String> getModes() {
return _model.getBeans();
}
@Override
public void computeResponse() {
// TODO Auto-generated method stub
}
@Override
public void show() {
IWizardPage prevPage = getWizard().getPreviousPage(this);
if(prevPage!=null){
if (prevPage instanceof FolderSelectionPage) {
_selectionPage = (FolderSelectionPage) prevPage;
}
if (_selectionPage.getSelectedResource() instanceof IContainer) {
_selectedContainer = (IContainer) _selectionPage.getSelectedResource();
}
_txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
}
performValidation();
}
}