Code Review Example


We recommend implementors to start their work from the example file. Feel free to copy the classes included without any limitation.

In this section we will try to understand the java code used to run the com.omondo.uml.profile.example project.
The following class will be reviewed:

PluginExample.java
ProfileExample.java
ProfileExamplePropertiesEditor.java
FragmentExample.java
FragmentExampleTabItem.java

1. PluginExample

This class is required by Ecplise to manage plugins. See here for more information.



package com.omondo.uml.profile.example;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class PluginExample extends AbstractUIPlugin
{

public static Shell getShell()
{
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
if (activeWorkbenchWindow == null)
{
return null;
}
return activeWorkbenchWindow.getShell();
}

public PluginExample()
{
}

}
2. ProfileExample

This is the main class of an Omondo profile implementation.



package com.omondo.uml.profile.example;

import com.omondo.uml.std.external.profile.AbstractProfile;
import com.omondo.uml.std.external.profile.ProfilePropertiesEditor;
import com.omondo.uml.std.external.profile.Types;

import org.eclipse.uml2.NamedElement;

import org.eclipse.jface.action.Action;

import org.eclipse.jface.dialogs.MessageDialog;

public class ProfileExample extends AbstractProfile
{

private ProfilePropertiesEditor editor = new ProfileExamplePropertiesEditor();

public ProfileExample()
{
}

This method is used to select the Actions that will be added to the diagram popup menu. The selected actions will be added into a group labeled with the name of the profile.

    protected void registerDefaultContributionItems()
{
registerAction(
new Action("Example Action") {
public void run()
{
NamedElement element = (NamedElement)getElement();
String message = "Associated element name:\n\t" + element.getQualifiedName();
MessageDialog.openInformation(PluginExample.getShell(), "title", message);
}

},
Types.CLASS_DIAGRAM
);
}

This method is used to select the Actions that will be added to the diagram popup menu. The selected actions will be added into the New group.

    protected void registerNewContributionItems()
{
}

This method is used to select the profile properties editor component that will be used in the Omondo profile section of the project properties, see ProfilePropertiesEditor class for more information.

    public ProfilePropertiesEditor getPropertyEditor()
{
return editor;
}

}
3. ProfileExamplePropertiesEditor

This class manages the properties editor of the profile. 



package com.omondo.uml.profile.example;

import com.omondo.uml.std.external.profile.ProfilePropertiesEditor;

import org.eclipse.swt.widgets.Composite;

public class ProfileExamplePropertiesEditor implements ProfilePropertiesEditor
{

This method return a SWT Composite object that will be displayed when the user selects the associated profile in the Omondo Profile section of the project properties.
Note that if the example returns null, a default empty composite will be used instead.

    public Composite createEditorArea(Composite parent)
{
return null;
}
    public void dispose()
{
}

public boolean isDirty()
{
return false;
}

public void save()
{
}

}
4. FragmentExample

The Fragment class is used at the element level to manage Actions additions to the popup menu and Tabs additions to the properties dialog.
A fragment is associated to a stereotype and to some element types. Thus when this stereotype is applied to an element of the associated type, the fragment is activated so the declared actions and tabs are added to the ihms.

Note that the association between fragment classes and stereotypes / element types is performed by means of the extension point properties.



package com.omondo.uml.profile.example;

import com.omondo.uml.std.external.profile.ExtensionTabItem;
import com.omondo.uml.std.external.profile.AbstractFragment;

import org.eclipse.core.resources.IProject;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;


public class FragmentExample extends AbstractFragment
{

public FragmentExample()
{
}
    public void init(IProject project)
{
super.init(project);
extensionTabItems = new ExtensionTabItem[] {
new FragmentExampleTabItem(getStereotypeFullName())
};
}

This method is used to declare the additional tabs. See below for more information.

    public ExtensionTabItem[] getExtensionTabItems()
{
return extensionTabItems;
}

This method is used to select the Actions that will be added to the element popup menu. The selected actions will be added into a group labeled with the name of the profile.

    protected void registerDefaultContributionItems()
{
addContributionItem(
new Action("Example action") {
public void run()
{
String buf = "Stereotype properties values: \n\n";
String name = "attr";
buf += "\t" + name + " -> " + getStereotypeValue(name) + "\n";
MessageDialog.openInformation(PluginExample.getShell(), "title", buf);
}
}
);
}

This method is used to select the Actions that will be added to the element popup menu. The selected actions will be added into the New group.

    protected void registerNewContributionItems()
{
}

private ExtensionTabItem extensionTabItems[];

}
5. FragmentExampleTabItem

The ExtensionTabItem object manages the additional tabs to the element properties dialogs (see example)


package com.omondo.uml.profile.example;

import com.omondo.uml.std.external.profile.ExtensionTabItem;
import com.omondo.uml.std.external.profile.ExtensionTabItemNotifier;

import com.omondo.uml.ui.editors.editmodel.Properties;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.uml2.Element;

public class FragmentExampleTabItem implements ExtensionTabItem
{

public FragmentExampleTabItem(String associatedStereotype)
{
tabLabel = "Example";
isReadOnly = false;
this.associatedStereotype = associatedStereotype;
}

public String getId()
{
return tabLabel;
}

This method is called when needed to create the SWT elements that will be added to the element properties dialog. The model object represents the current element using the UML2 metamodel. The taggedValues object should not be used because usage is deprecated. The notifier object should be used each time a control value is changed in order to validate the dialog content and update the warning/error message.

    public TabItem create(Element model, Properties taggedValues, TabFolder folder,
 ExtensionTabItemNotifier notifier, boolean isReadOnly)
{
setReadOnly(isReadOnly);
this.taggedValues = taggedValues;
rootTab = new Composite(folder, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.marginHeight = 5;
layout.marginWidth = 5;
rootTab.setLayout(layout);
GridData data = new GridData(GridData.FILL_BOTH);
rootTab.setLayoutData(data);
TabItem item = new TabItem(folder, SWT.NONE);
item.setText(tabLabel + " " + associatedStereotype);
item.setControl(rootTab);
return item;
}

public void read()
{
}

public void save()
{
}

public boolean canPerformCompletion()
{
return errorMessage == null;
}

public String getErrorMsg()
{
return errorMessage;
}

public String getWarningMsg()
{
return warningMessage;
}

public String getAssociatedStereotype()
{
return associatedStereotype;
}

public void beforeCompletion()
{
}

public void dispose()
{
rootTab = null;
taggedValues = null;
}

public boolean isReadOnly()
{
return isReadOnly;
}

public void setReadOnly(boolean b)
{
isReadOnly = b;
}

protected String tabLabel;
private Composite rootTab;
protected Properties taggedValues;
private String associatedStereotype;
private String errorMessage;
private String warningMessage;
private boolean isReadOnly;

}

To get more information on implementing the Omondo profile interfaces, please look at the javadoc available in the next section.

 

 
Last update Tue Feb 08 14:58:31 CEST 2005 Valid XHTML 1.0 Valid CSS All text, graphics © 2002-2005 by Omondo