Skip to content

Instantly share code, notes, and snippets.

@lenards
Created July 12, 2010 16:32
Show Gist options
  • Save lenards/472683 to your computer and use it in GitHub Desktop.
Save lenards/472683 to your computer and use it in GitHub Desktop.
package org.iplantc.de.client.views.panels;
import org.iplantc.de.client.ClientAction;
import org.iplantc.de.client.I18N;
import org.iplantc.de.client.formatters.AddQuotesFormatter;
import org.iplantc.de.client.metadata.property.Property;
import org.iplantc.de.client.models.File;
import org.iplantc.de.client.utils.ComponentValueTable;
import org.iplantc.de.client.utils.ComponentValueTableValidator;
import org.iplantc.de.client.utils.ValidatorHelper;
import org.iplantc.de.client.validator.IPlantValidator;
import org.iplantc.de.client.views.FileSelector;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
public class SimpleMatePanel extends VerticalPanel
{
protected ComponentValueTable tblComponentVals;
protected FileSelector fileSelector;
protected ContentPanel pnlMate;
private CheckBox cbMateSelection;
private IPlantValidator validatorFileSelect;
private final static String fileReadId = "read2FileId";
public SimpleMatePanel(final Property property, final ComponentValueTable tblComponentVals)
{
this.tblComponentVals = tblComponentVals;
init(property);
initValidators();
initInstanceVariables(property);
addSupportingComponentVals();
compose(property);
updateComponentValueTable();
ComponentValueTableValidator.validate(tblComponentVals);
}
private void init(final Property property)
{
setSpacing(5);
}
protected void initInstanceVariables(final Property property)
{
fileSelector = new FileSelector(new ClientAction()
{
@Override
public void execute()
{
updateComponentValueTable();
ComponentValueTableValidator.validate(tblComponentVals);
}
});
initMatePanel();
initMateCheckbox(property);
}
private void initMatePanel()
{
pnlMate = new ContentPanel();
pnlMate.setHeaderVisible(false);
pnlMate.disable();
}
protected void addSupportingComponentVals()
{
File file = fileSelector.getSelectedFile();
String idFile = (file == null) ? "" : file.getId();
tblComponentVals.setValue(fileReadId, idFile);
tblComponentVals.setFormatter(fileReadId, new AddQuotesFormatter());
}
protected void updateComponentValueTable()
{
tblComponentVals.setValue(cbMateSelection.getId(), cbMateSelection.getValue().toString());
File file = fileSelector.getSelectedFile();
String idFile = (file == null) ? "" : file.getId();
tblComponentVals.setValue(fileReadId, idFile);
}
protected void updateValidators(boolean enabled)
{
if(enabled)
{
//set our validator if the user checks the 'has mate pair' checkbox
tblComponentVals.setValidator(fileReadId, validatorFileSelect);
}
else
{
//if the user does not have a mate pair, we do not need to validate
tblComponentVals.clearValidator(fileReadId);
}
}
private void initMateCheckbox(final Property property)
{
cbMateSelection = new CheckBox();
cbMateSelection.setId(property.getId());
cbMateSelection.setBoxLabel(property.getName());
cbMateSelection.addListener(Events.Change, new Listener<BaseEvent>()
{
public void handleEvent(final BaseEvent be)
{
boolean checked = cbMateSelection.getValue();
pnlMate.setEnabled(checked);
updateValidators(checked);
updateComponentValueTable();
ComponentValueTableValidator.validate(tblComponentVals);
}
});
}
protected void initValidators()
{
String json = "{\"name\" : \"Mate pair file\", \"required\" : true}";
validatorFileSelect = new IPlantValidator(ValidatorHelper.buildValidator(json));
}
protected void composeMatePanel()
{
VerticalPanel panelInner = new VerticalPanel();
panelInner.setSpacing(5);
panelInner.setWidth(400);
panelInner.addStyleName("accordianbody");
panelInner.add(new Label(I18N.DISPLAY.matePairSelection() + ":"));
panelInner.add(fileSelector);
pnlMate.add(panelInner);
}
private void compose(final Property property)
{
add(cbMateSelection);
composeMatePanel();
add(pnlMate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment