View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: LegendEditPart.java
13   * Created: 2009-07-27
14   * Author: cast
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.editpart;
19  
20  import org.eclipse.draw2d.ChopboxAnchor;
21  import org.eclipse.draw2d.ConnectionAnchor;
22  import org.eclipse.gef.ConnectionEditPart;
23  import org.eclipse.gef.NodeEditPart;
24  import org.eclipse.gef.Request;
25  import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
26  
27  import pl.edu.agh.cast.figure.AbstractLegendFigure;
28  import pl.edu.agh.cast.model.visual.Legend;
29  
30  /**
31   * Legend {@link org.eclipse.ui.part.EditorPart}.
32   * 
33   * @author AGH CAST Team
34   */
35  public abstract class AbstractLegendEditPart extends AbstractGraphicalEditPart implements NodeEditPart {
36  
37  	/**
38  	 * The underlying {@link Legend} instance.
39  	 */
40  	protected Legend legend;
41  
42  	/**
43  	 * Reference to legend figure object.
44  	 */
45  	protected AbstractLegendFigure legendFigure;
46  
47  	private ConnectionAnchor connectionAnchor;
48  
49  	/**
50  	 * Constructor.
51  	 * 
52  	 * @param legend
53  	 *            the legend
54  	 */
55  	public AbstractLegendEditPart(Legend legend) {
56  		this.legend = legend;
57  		setModel(legend);
58  	}
59  
60  	/**
61  	 * Returns the {@link Legend} instance.
62  	 * 
63  	 * @return the {@link Legend}
64  	 */
65  	public Legend getModel() {
66  		return legend;
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 * 
72  	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
73  	 */
74  	@Override
75  	protected void refreshVisuals() {
76  		// update the location only if it is not (-1,-1) or when preferred
77  		// location is not set
78  		// (i.e. when opening a diagram with default locations)
79  		if (legend.getLocation().x > 0) {
80  			legendFigure.setLocation(legend.getLocation());
81  		}
82  		refreshItems();
83  	}
84  
85  	/**
86  	 * Refreshes the legend.
87  	 */
88  	public void refreshItems() {
89  		legendFigure.refresh();
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 * 
95  	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
96  	 */
97  	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
98  		return getConnectionAnchor();
99  	}
100 
101 	/**
102 	 * {@inheritDoc}
103 	 * 
104 	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
105 	 */
106 	public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
107 		return getConnectionAnchor();
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 * 
113 	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
114 	 */
115 	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
116 		return getConnectionAnchor();
117 	}
118 
119 	/**
120 	 * {@inheritDoc}
121 	 * 
122 	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
123 	 */
124 	public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
125 		return getConnectionAnchor();
126 	}
127 
128 	private ConnectionAnchor getConnectionAnchor() {
129 		if (connectionAnchor == null) {
130 			connectionAnchor = new ChopboxAnchor(legendFigure);
131 		}
132 		return connectionAnchor;
133 
134 	}
135 
136 }