Eclipse zest note

基本zest viewer建立

public class BaseViewer extends ViewPart implements IZoomableWorkbenchPart {

@Override
public void createPartControl(Composite parent) {
    view = new GraphViewer(parent, SWT.NONE);
    view.setContentProvider(new NodeContentProvider());
    view.setLabelProvider(new NodeLabelProvider());
    view.setLayoutAlgorithm(new MultiLayoutAlgorithm(), true);  //設定layout並執行
    fillToolbar();
}

//設定viewer的toolbar提供zoom的選項
private void fillToolbar() {
    ZoomContributionViewItem toolbarZoomContributionViewItem = new ZoomContributionViewItem(
this);
    IActionBars bars = getViewSite().getActionBars();
    bars.getMenuManager().add(toolbarZoomContributionViewItem);
}

@Override
public AbstractZoomableViewer getZoomableViewer() {
return view;
}
}

viewer.applyLayout(); //執行layout

/* 把有連線的node獨立出來執行baselayout
 * 沒有連線的node用類似gridlayout的方式處理,避免交互影響讓layout不好看
 * 把node layout在適當的大小內,用zoom來調整顯示的比例
*/
public class MultiLayoutAlgorithm extends AbstractLayoutAlgorithm {

private AbstractLayoutAlgorithm baseLayout;
private InternalNode[] isolateNodes;
private Map<Integer, Set<InternalNode>> nodeSetMap;
private Map<Integer, Set<InternalRelationship>> linkSetMap;
private DisplayIndependentRectangle layoutBounds = null;

public MultiLayoutAlgorithm(int style, AbstractLayoutAlgorithm layout) {
super(style);
baseLayout = layout;
nodeSetMap = new HashMap<Integer, Set<InternalNode>>();
linkSetMap = new HashMap<Integer, Set<InternalRelationship>>();
}

@Override
public void setLayoutArea(double x, double y, double width, double height) {
}

@Override
protected boolean isValidConfiguration(boolean asynchronous,
boolean continuous) {
return true;
}

@Override
protected void applyLayoutInternal(InternalNode[] entitiesToLayout,
InternalRelationship[] relationshipsToConsider, double boundsX,
double boundsY, double boundsWidth, double boundsHeight) {
Iterator<Integer> indexItr = nodeSetMap.keySet().iterator();
double layoutX = 0;
double layoutY = 0;
double layoutWidth = 0;
double layoutHeight = 0;
while (indexItr.hasNext()) {
int key = indexItr.next();
Set<InternalNode> nodeSet = nodeSetMap.get(key);
Set<InternalRelationship> linkSet = linkSetMap.get(key);
InternalNode[] layoutNodes = new InternalNode[nodeSet.size()];
layoutNodes = nodeSet.toArray(layoutNodes);
InternalRelationship[] links = new InternalRelationship[linkSet
.size()];
links = linkSet.toArray(links);
layoutX += layoutWidth;
layoutWidth = nodeSet.size() * (200 / Math.log(nodeSet.size()));
layoutHeight = nodeSet.size() * (200 / Math.log(nodeSet.size()));
try {
baseLayout.applyLayout(layoutNodes, links, layoutX, layoutY,
layoutWidth, layoutHeight, this.internalAsynchronous,
this.internalContinuous);
for (int i = 0; i < layoutNodes.length; i++) {
layoutNodes[i].getLayoutEntity().setLocationInLayout(
layoutNodes[i].getXInLayout(),
layoutNodes[i].getYInLayout());
}
} catch (InvalidLayoutConfiguration e) {
}
}
}

@Override
protected void preLayoutAlgorithm(InternalNode[] entitiesToLayout,
InternalRelationship[] relationshipsToConsider, double x, double y,
double width, double height) {
// create sub connected set
int totalIndex = 1;
for (int i = 0; i < relationshipsToConsider.length; i++) {
InternalRelationship relation = relationshipsToConsider[i];
InternalNode srcNode = relation.getSource();
InternalNode destNode = relation.getDestination();

int srcIdx = 0;
int destIdx = 0;
// find each node set
Iterator<Integer> indexItr = nodeSetMap.keySet().iterator();
while (indexItr.hasNext()) {
int key = indexItr.next();
Set<InternalNode> retrieveSet = nodeSetMap.get(key);
if (retrieveSet.contains(srcNode))
srcIdx = key;
if (retrieveSet.contains(destNode))
destIdx = key;
if (srcIdx != 0 && destIdx != 0)
break;
}
// check sets status need merge or not
if (srcIdx == 0 && destIdx == 0) {
// new node set
Set<InternalNode> tempSet = new HashSet<InternalNode>(2);
tempSet.add(srcNode);
tempSet.add(destNode);
nodeSetMap.put(totalIndex, tempSet);

// new link set
Set<InternalRelationship> linkSet = new HashSet<InternalRelationship>(
1);
linkSet.add(relation);
linkSetMap.put(totalIndex, linkSet);
totalIndex++;
} else if (srcIdx != 0 && destIdx == 0) {
nodeSetMap.get(srcIdx).add(destNode);
linkSetMap.get(srcIdx).add(relation);
} else if (srcIdx == 0 && destIdx != 0) {
nodeSetMap.get(destIdx).add(srcNode);
linkSetMap.get(destIdx).add(relation);
} else {
if (srcIdx != destIdx) {
nodeSetMap.get(srcIdx).addAll(nodeSetMap.get(destIdx));
nodeSetMap.remove(destIdx);
linkSetMap.get(srcIdx).addAll(linkSetMap.get(destIdx));
linkSetMap.get(srcIdx).add(relation);
linkSetMap.remove(destIdx);
}
}
}
Set<InternalNode> isolateSet = new HashSet<InternalNode>(
Arrays.asList(entitiesToLayout));
Iterator<Integer> indexItr = nodeSetMap.keySet().iterator();
while (indexItr.hasNext()) {
int key = indexItr.next();
isolateSet.removeAll(nodeSetMap.get(key));
}
isolateNodes = new InternalNode[isolateSet.size()];
isolateNodes = isolateSet.toArray(isolateNodes);
layoutBounds = new DisplayIndependentRectangle(x, y, width, height);
}

@Override
protected void postLayoutAlgorithm(InternalNode[] entitiesToLayout,
InternalRelationship[] relationshipsToConsider) {
double maxX = 0;
double maxY = 0;
for (int i = 0; i < entitiesToLayout.length; i++) {
InternalNode node = entitiesToLayout[i];
if (maxX < node.getXInLayout())
maxX = node.getXInLayout();
if (maxY < node.getYInLayout())
maxY = node.getYInLayout();
}
if (maxX < layoutBounds.width)
maxX = layoutBounds.width;
setIsolateNodePosition(maxX, maxY);
}

@Override
protected int getTotalNumberOfLayoutSteps() {
return 0;
}

@Override
protected int getCurrentLayoutStep() {
return 0;
}

private void setIsolateNodePosition(double endX, double endY) {
final double newX = layoutBounds.x;
double nodeX = newX;
double nodeY = endY + 50;
for (InternalNode node : isolateNodes) {
node.setInternalLocation(nodeX, nodeY);
node.setLocation(nodeX, nodeY);
nodeX += 120;
if (nodeX > endX) {
nodeX = newX;
nodeY += 80;
}
}
}
}

//設定node的外觀
public class NodeLabelProvider extends LabelProvider implements IColorProvider {

@Override
public Image getImage(Object element) {
return image;
}

@Override
public String getText(Object element) {
return element.toString();
}

@Override
public Color getForeground(Object element) {
return Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
}

@Override
public Color getBackground(Object element) {
return Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
}
}


//設定graph content
public class NodeContentProvider extends ArrayContentProvider implements
IGraphEntityRelationshipContentProvider {

private List<Device> list;

@Override
public void dispose() {
}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

@SuppressWarnings("unchecked")
@Override
public Object[] getElements(Object inputElement) {
list = (List<Device>) inputElement;
return list.toArray();
}

@Override
public Object[] getRelationships(Object source, Object dest) {
                if (link.getSource().equals(source) && link.getTarget().equals(dest))
      return link
}
}

留言

這個網誌中的熱門文章

Embedded Tomcat

COSCUP2010

SWT SSH terminal