package org.talend.designer.core.ui.editor.properties.controllers;
import java.beans.PropertyChangeEvent;
import java.io.File;
import org.eclipse.core.runtime.Path;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.talend.commons.ui.runtime.image.ImageProvider;
import org.talend.core.CorePlugin;
import org.talend.core.model.process.IElementParameter;
import org.talend.core.properties.tab.IDynamicProperty;
import org.talend.core.utils.PathExtractor;
import org.talend.designer.core.ui.editor.nodes.Node;
import org.talend.designer.core.ui.editor.properties.controllers.uidialog.tns.TnsEditorDialog;
import org.talend.designer.core.ui.editor.properties.controllers.uidialog.tns.TnsInfo;
import org.talend.designer.core.ui.editor.properties.controllers.uidialog.tns.TnsPropertyCommand;
public class TNSEditorController extends AbstractElementPropertySectionController {
public TNSEditorController(IDynamicProperty dp) {
super(dp);
}
public Command createCommand(Button button) {
IElementParameter elementParameter = this.curParameter.getElement().getElementParameter("TNS_FILE");
Command command = null;
if (elementParameter != null) {
String filePath = new Path(PathExtractor.extractPath(elementParameter.getValue().toString())).toOSString();
if (filePath != null && new File(filePath).exists()) {
TnsEditorDialog tnsDialog = new TnsEditorDialog(composite.getShell(), new File(filePath));
if (tnsDialog.open() == Window.OK) {
TnsInfo tnsInfo = tnsDialog.getTnsInfo();
command = new TnsPropertyCommand(tnsInfo, this.elem);
}
} else {
MessageBox mBox = new MessageBox(composite.getShell(), SWT.ICON_ERROR);
mBox.setText("Error"); //$NON-NLS-1$
mBox.setMessage("TNS File Not Found"); //$NON-NLS-1$
mBox.open();
}
}
return command;
}
@Override
public Control createControl(Composite subComposite, IElementParameter param, int numInRow, int nbInRow, int top,
Control lastControl) {
this.curParameter = param;
Button btnEdit;
btnEdit = getWidgetFactory().createButton(subComposite, "", SWT.PUSH); //$NON-NLS-1$
FormData data;
btnEdit.setImage(ImageProvider.getImage(CorePlugin.getImageDescriptor(DOTS_BUTTON)));
data = new FormData();
data.left = new FormAttachment(((numInRow * MAX_PERCENT) / nbInRow), -STANDARD_BUTTON_WIDTH);
data.right = new FormAttachment(((numInRow * MAX_PERCENT) / nbInRow), 0);
data.top = new FormAttachment(0, top);
data.height = STANDARD_HEIGHT - 2;
btnEdit.setLayoutData(data);
btnEdit.setData(NAME, "");
btnEdit.setData(PARAMETER_NAME, param.getName());
btnEdit.setEnabled(!param.isReadOnly());
btnEdit.addSelectionListener(listenerSelection);
if (elem instanceof Node) {
btnEdit.setToolTipText(VARIABLE_TOOLTIP + param.getVariableName());
}
CLabel labelLabel = getWidgetFactory().createCLabel(subComposite, param.getDisplayName()); //$NON-NLS-1$
data = new FormData();
if (lastControl != null) {
data.left = new FormAttachment(lastControl, 0);
} else {
data.left = new FormAttachment((((numInRow - 1) * MAX_PERCENT) / nbInRow), 0);
}
data.top = new FormAttachment(0, top);
labelLabel.setLayoutData(data);
if (numInRow != 1) {
labelLabel.setAlignment(SWT.RIGHT);
}
// **************************
data = new FormData();
int currentLabelWidth = STANDARD_LABEL_WIDTH;
GC gc = new GC(labelLabel);
Point labelSize = gc.stringExtent(param.getDisplayName());
gc.dispose();
if ((labelSize.x + ITabbedPropertyConstants.HSPACE) > currentLabelWidth) {
currentLabelWidth = labelSize.x + ITabbedPropertyConstants.HSPACE;
}
if (numInRow == 1) {
if (lastControl != null) {
data.left = new FormAttachment(lastControl, currentLabelWidth);
data.right = new FormAttachment(lastControl, currentLabelWidth + STANDARD_BUTTON_WIDTH);
} else {
data.left = new FormAttachment(0, currentLabelWidth);
data.right = new FormAttachment(0, currentLabelWidth + STANDARD_BUTTON_WIDTH);
}
} else {
data.left = new FormAttachment(labelLabel, 0, SWT.RIGHT);
data.right = new FormAttachment(labelLabel, STANDARD_BUTTON_WIDTH, SWT.RIGHT);
}
data.top = new FormAttachment(0, top);
btnEdit.setLayoutData(data);
// **************************
hashCurControls.put(param.getName(), btnEdit);
Point initialSize = btnEdit.computeSize(SWT.DEFAULT, SWT.DEFAULT);
dynamicProperty.setCurRowSize(initialSize.y + ITabbedPropertyConstants.VSPACE);
return btnEdit;
}
@Override
public int estimateRowSize(Composite subComposite, IElementParameter param) {
// TODO Auto-generated method stub
Button btnEdit = getWidgetFactory().createButton(subComposite, "", SWT.PUSH); //$NON-NLS-1$
btnEdit.setImage(ImageProvider.getImage(CorePlugin.getImageDescriptor(DOTS_BUTTON)));
Point initialSize = btnEdit.computeSize(SWT.DEFAULT, SWT.DEFAULT);
btnEdit.dispose();
return initialSize.y + ITabbedPropertyConstants.VSPACE;
}
@Override
public void refresh(IElementParameter param, boolean check) {
}
public void propertyChange(PropertyChangeEvent evt) {
}
SelectionListener listenerSelection = new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
Command cmd = createCommand((Button) e.getSource());
executeCommand(cmd);
}
};
}