blob: d67aba5d1a5fe30f1032c234be756f69c2488c31 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010-2011, Istvan Rath and Zoltan Ujhelyi
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.visualisation.common.labelproviders.internal;
import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.DelegatingLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Locator;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
/**
* This figure class will provide a "Label" and a "Shape" which will be centered above the label.
* @author Istvan Rath
*
*/
public abstract class ShapeCenteringFigure extends Figure
{
protected Shape iShapeFigure;
protected BorderLayout iLayout;
static FontRegistry registry = new FontRegistry();
static {
registry.put("default", new FontData[]{new FontData("Sans", 9, SWT.BOLD)} );
registry.put("code", new FontData[]{new FontData("Sans", 9, SWT.NORMAL)});
}
/**
* This helper class will be given as a constraint for the image figure.
*
*/
protected class CenteringLocator implements Locator
{
/**
* The image figure will be centered.
*/
public void relocate(IFigure target)
{
IFigure parent = target.getParent();
int width = iShapeFigure.getSize().width;
int height = iShapeFigure.getSize().height;
int x = parent.getClientArea().width / 2 - width / 2;
int y = parent.getClientArea().height / 2 - height / 2;
target.setLocation(new Point(x,y));
}
}
protected boolean isActivated = false;
protected boolean needBorder = false;
IFigure tooltip;
public ShapeCenteringFigure(boolean _needBorder, String labelText, IFigure tooltip)
{
this.needBorder = _needBorder;
this.tooltip = tooltip;
// Create general layout
iLayout = new BorderLayout();
// Create bottom panel for label
Panel ptmp = new LocalCoordinatePanel();
ptmp.setOpaque(true);
ptmp.setLayoutManager(new BorderLayout());
FlowPage fp = new FlowPage();
fp.setHorizontalAligment(PositionConstants.CENTER);
fp.setMaximumSize(new Dimension(100,100));
TextFlow label = new TextFlow();
label.setFont(registry.get("code"));
label.setText(labelText);
fp.add(label);
// Add label to panel and add bottom panel to the whole figure
ptmp.add(fp);
ptmp.getLayoutManager().setConstraint(fp, BorderLayout.CENTER);
add(ptmp);
iLayout.setConstraint(ptmp, BorderLayout.BOTTOM);
// Create center panel for the shape figure
Panel ptop = new LocalCoordinatePanel();
ptop.setOpaque(true);
ptop.setLayoutManager(new DelegatingLayout());
// Create shape figure
createShapeFigure();
// add shape figure to top panel
ptop.add(iShapeFigure);
ptop.getLayoutManager().setConstraint(iShapeFigure, new CenteringLocator());
add(ptop);
iLayout.setConstraint(ptop, BorderLayout.CENTER);
// set whole size
setSize(10+Math.max(iShapeFigure.getSize().width, fp.getPreferredSize().width),
3+iShapeFigure.getSize().height+fp.getPreferredSize().height);
setLayoutManager(iLayout);
setOpaque(true);
if (needBorder) {
setBorder(new DashedBorder(ColorConstants.gray, 1));
}
isActivated = true;
}
/**
* Subclasses should initialize the iShapeFigure internal member in this method.
*/
protected abstract void createShapeFigure();
/*
* (non-Javadoc)
*
* @see org.eclipse.draw2d.Figure#useLocalCoordinates()
*/
@Override
protected boolean useLocalCoordinates() {
return true;
}
@Override
public IFigure getToolTip() {
return tooltip;
}
protected abstract Dimension getShapeDimension();
}