Listing 7. Sample code that implements the property section
public void createControls(Composite parent, TabbedPropertySheetPage page) {
super.createControls(parent, page);
TabbedPropertySheetWidgetFactory factory = getWidgetFactory();
Composite comp = factory.createFlatFormComposite(parent);
// Privacy checkbox
privacyCheckbox = factory.createButton(comp, "Private Data", SWT.CHECK);
FormData data = new FormData();
data.left = new FormAttachment(0, 0);
data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
privacyCheckbox.setLayoutData(data);
privacyCheckbox.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent evt) {
Sample1PropertySection.this.onCheckboxSelected();
}
public void widgetDefaultSelected(SelectionEvent evt) {
}
});
// Masking text
CLabel label = factory.createCLabel(comp, "Masking method:");
data = new FormData();
data.left = new FormAttachment(0, 0);
data.top = new FormAttachment(privacyCheckbox,
ITabbedPropertyConstants.HSPACE);
label.setLayoutData(data);
maskingText = factory.createText(comp, "");
data = new FormData();
data.left = new FormAttachment(label, ITabbedPropertyConstants.HSPACE);
data.right = new FormAttachment(100, 0);
data.top = new FormAttachment(label, 0, SWT.CENTER);
maskingText.setLayoutData(data);
textListener = new TextChangeListener() {
protected void changeProperty(Event e) {
Sample1PropertySection.this.onLeaveText(); }
};
maskingText.addListener(SWT.FocusOut, textListener);
maskingText.addListener(SWT.DefaultSelection, textListener);
}
public void setInput(IWorkbenchPart part, ISelection selection) {
super.setInput(part, selection);
if(!(selection instanceof IStructuredSelection))
return;
Object firstObject = ((IStructuredSelection) selection).getFirstElement();
if(firstObject instanceof Column) {
column = (Column)firstObject;
refresh();
}
else {
column = null;
}
}
public void refresh()
{
super.refresh();
// read the property value
if(column != null) {
privacyCheckbox.setSelection(getPrivacyProperty());
maskingText.setText(getMaskingProperty());
}
}
private void onCheckboxSelected() {
// save the property value
boolean checkboxSelected = privacyCheckbox.getSelection();
boolean propertyStatus = this.getPrivacyProperty();
if(checkboxSelected != propertyStatus) {
setPrivacyProperty(checkboxSelected);
}
}
private void onLeaveText() {
// save the property value
String newText = this.maskingText.getText();
String oldText = this.getMaskingProperty();
if(!newText.equals(oldText)) {
setMaskingProperty(newText);
}
}
private String getMaskingProperty() {
String maskingStr = "";
EAnnotation ea = column.getEAnnotation(SAMPLE_EANNOTAITN_NAME);
if(ea != null) {
// get the details with the key SAMPLE_MASKING_PROPERTY_NAME
Object valObj = ea.getDetails().get(SAMPLE_MASKING_PROPERTY_NAME);
if(valObj != null) {
maskingStr = (String)valObj;
if(maskingStr != null)
return maskingStr;
}
}
return "";
}
private void setMaskingProperty(String maskingStr) {
EAnnotation ea = column.getEAnnotation(SAMPLE_EANNOTAITN_NAME);
DataToolsCompositeCommand cmd =
new DataToolsCompositeCommand(COMMAND_LABEL);
String newText = this.maskingText.getText();
if(ea == null) {
ea = createSampleEAnnotaiton(cmd);
}
EMap details = ea.getDetails();
int entryCount = details.size();
EStringToStringMapEntryImpl entry = null;
for(int i=0; i < entryCount; i++) {
EStringToStringMapEntryImpl anEntry =
(EStringToStringMapEntryImpl)ea.getDetails().get(i);
if(anEntry.getKey().equals(SAMPLE_MASKING_PROPERTY_NAME)) {
entry = anEntry;
break;
}
}
if(entry != null) {
EStructuralFeature feature =
EcorePackage.eINSTANCE.getEStringToStringMapEntry_Value();
cmd.compose(
new SetCommand(COMMAND_LABEL, entry, feature, newText));
DataToolsPlugin.getDefault().getCommandManager().execute(cmd);
}
}
|
