package org.eclipse.gmf.examples.eclipsecon.library.diagram.edit.parts;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.examples.eclipsecon.library.diagram.part.Library07VisualIDRegistry;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Text;
/**
* @generated
*/
public class Library07EditPartFactory implements EditPartFactory {
/**
* @generated
*/
public EditPart createEditPart(EditPart context, Object model) {
if (model instanceof View) {
View view = (View) model;
switch (Library07VisualIDRegistry.getVisualID(view)) {
case LibraryEditPart.VISUAL_ID:
return new LibraryEditPart(view);
case AuthorEditPart.VISUAL_ID:
return new AuthorEditPart(view);
case AuthorNameEditPart.VISUAL_ID:
return new AuthorNameEditPart(view);
case ShelfEditPart.VISUAL_ID:
return new ShelfEditPart(view);
case ShelfNameEditPart.VISUAL_ID:
return new ShelfNameEditPart(view);
case EmployeeEditPart.VISUAL_ID:
return new EmployeeEditPart(view);
case EmployeeNameEditPart.VISUAL_ID:
return new EmployeeNameEditPart(view);
case BookEditPart.VISUAL_ID:
return new BookEditPart(view);
case ShelfBooksCompartmentEditPart.VISUAL_ID:
return new ShelfBooksCompartmentEditPart(view);
case EmployeeShelvesEditPart.VISUAL_ID:
return new EmployeeShelvesEditPart(view);
}
}
return createUnrecognizedEditPart(context, model);
}
/**
* @generated
*/
private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
// Handle creation of unrecognized child node EditParts here
return null;
}
/**
* @generated
*/
public static CellEditorLocator getTextCellEditorLocator(
ITextAwareEditPart source) {
if (source.getFigure() instanceof WrapLabel)
return new TextCellEditorLocator((WrapLabel) source.getFigure());
else {
return new LabelCellEditorLocator((Label) source.getFigure());
}
}
/**
* @generated
*/
static private class TextCellEditorLocator implements CellEditorLocator {
/**
* @generated
*/
private WrapLabel wrapLabel;
/**
* @generated
*/
public TextCellEditorLocator(WrapLabel wrapLabel) {
this.wrapLabel = wrapLabel;
}
/**
* @generated
*/
public WrapLabel getWrapLabel() {
return wrapLabel;
}
/**
* @generated
*/
public void relocate(CellEditor celleditor) {
Text text = (Text) celleditor.getControl();
Rectangle rect = getWrapLabel().getTextBounds().getCopy();
getWrapLabel().translateToAbsolute(rect);
if (getWrapLabel().isTextWrapped()
&& getWrapLabel().getText().length() > 0) {
rect.setSize(new Dimension(text.computeSize(rect.width,
org.eclipse.swt.SWT.DEFAULT)));
} else {
int avr = FigureUtilities.getFontMetrics(text.getFont())
.getAverageCharWidth();
rect.setSize(new Dimension(text.computeSize(
org.eclipse.swt.SWT.DEFAULT,
org.eclipse.swt.SWT.DEFAULT)).expand(avr * 2, 0));
}
if (!rect.equals(new Rectangle(text.getBounds()))) {
text.setBounds(rect.x, rect.y, rect.width, rect.height);
}
}
}
/**
* @generated
*/
private static class LabelCellEditorLocator implements CellEditorLocator {
/**
* @generated
*/
private Label label;
/**
* @generated
*/
public LabelCellEditorLocator(Label label) {
this.label = label;
}
/**
* @generated
*/
public Label getLabel() {
return label;
}
/**
* @generated
*/
public void relocate(CellEditor celleditor) {
Text text = (Text) celleditor.getControl();
Rectangle rect = getLabel().getTextBounds().getCopy();
getLabel().translateToAbsolute(rect);
int avr = FigureUtilities.getFontMetrics(text.getFont())
.getAverageCharWidth();
rect.setSize(new Dimension(text.computeSize(
org.eclipse.swt.SWT.DEFAULT, org.eclipse.swt.SWT.DEFAULT))
.expand(avr * 2, 0));
if (!rect.equals(new Rectangle(text.getBounds()))) {
text.setBounds(rect.x, rect.y, rect.width, rect.height);
}
}
}
}