PageRenderTime 34ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/argouml-0.34/argouml/src/argouml-app/src/org/argouml/uml/diagram/state/ui/FigCompositeState.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 442 lines | 259 code | 57 blank | 126 comment | 33 complexity | 1d03facf973b8785a22363aba61dd8a6 MD5 | raw file
  1. /* $Id: FigCompositeState.java 19614 2011-07-20 12:10:13Z linus $
  2. *****************************************************************************
  3. * Copyright (c) 2009 Contributors - see below
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * mvw
  11. *****************************************************************************
  12. *
  13. * Some portions of this file was previously release using the BSD License:
  14. */
  15. // Copyright (c) 1996-2009 The Regents of the University of California. All
  16. // Rights Reserved. Permission to use, copy, modify, and distribute this
  17. // software and its documentation without fee, and without a written
  18. // agreement is hereby granted, provided that the above copyright notice
  19. // and this paragraph appear in all copies. This software program and
  20. // documentation are copyrighted by The Regents of the University of
  21. // California. The software program and documentation are supplied "AS
  22. // IS", without any accompanying services from The Regents. The Regents
  23. // does not warrant that the operation of the program will be
  24. // uninterrupted or error-free. The end-user understands that the program
  25. // was developed for research purposes and is advised not to rely
  26. // exclusively on the program for any reason. IN NO EVENT SHALL THE
  27. // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  28. // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  29. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  30. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  31. // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  32. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  34. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  35. // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  36. // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  37. package org.argouml.uml.diagram.state.ui;
  38. import java.awt.Color;
  39. import java.awt.Dimension;
  40. import java.awt.Rectangle;
  41. import java.awt.event.MouseEvent;
  42. import java.util.Iterator;
  43. import java.util.List;
  44. import java.util.TreeMap;
  45. import java.util.Vector;
  46. import org.argouml.model.Model;
  47. import org.argouml.model.RemoveAssociationEvent;
  48. import org.argouml.model.UmlChangeEvent;
  49. import org.argouml.ui.targetmanager.TargetManager;
  50. import org.argouml.uml.diagram.DiagramSettings;
  51. import org.argouml.uml.diagram.ui.ActionAddConcurrentRegion;
  52. import org.tigris.gef.presentation.Fig;
  53. import org.tigris.gef.presentation.FigLine;
  54. import org.tigris.gef.presentation.FigRRect;
  55. import org.tigris.gef.presentation.FigRect;
  56. import org.tigris.gef.presentation.FigText;
  57. /**
  58. * Class to display graphics for a UML CompositeState in a diagram.
  59. *
  60. * @author jrobbins@ics.uci.edu
  61. */
  62. public class FigCompositeState extends FigState {
  63. private FigRect cover;
  64. private FigLine divider;
  65. /**
  66. * Construct a new FigStubState.
  67. *
  68. * @param owner owning UML element
  69. * @param bounds position and size
  70. * @param settings rendering settings
  71. */
  72. public FigCompositeState(Object owner, Rectangle bounds,
  73. DiagramSettings settings) {
  74. super(owner, bounds, settings);
  75. initFigs();
  76. updateNameText();
  77. }
  78. private void initFigs() {
  79. cover =
  80. new FigRRect(getInitialX(), getInitialY(),
  81. getInitialWidth(), getInitialHeight(),
  82. LINE_COLOR, FILL_COLOR);
  83. getBigPort().setLineWidth(0);
  84. divider =
  85. new FigLine(getInitialX(),
  86. getInitialY() + 2 + getNameFig().getBounds().height + 1,
  87. getInitialWidth() - 1,
  88. getInitialY() + 2 + getNameFig().getBounds().height + 1,
  89. LINE_COLOR);
  90. // add Figs to the FigNode in back-to-front order
  91. addFig(getBigPort());
  92. addFig(cover);
  93. addFig(getNameFig());
  94. addFig(divider);
  95. addFig(getInternal());
  96. setBounds(getBounds());
  97. }
  98. /*
  99. * @see java.lang.Object#clone()
  100. */
  101. public Object clone() {
  102. FigCompositeState figClone = (FigCompositeState) super.clone();
  103. Iterator it = figClone.getFigs().iterator();
  104. figClone.setBigPort((FigRRect) it.next());
  105. figClone.cover = (FigRect) it.next();
  106. figClone.setNameFig((FigText) it.next());
  107. figClone.divider = (FigLine) it.next();
  108. figClone.setInternal((FigText) it.next());
  109. return figClone;
  110. }
  111. ////////////////////////////////////////////////////////////////
  112. // accessors
  113. /*
  114. * @see org.tigris.gef.presentation.Fig#getMinimumSize()
  115. */
  116. public Dimension getMinimumSize() {
  117. Dimension nameDim = getNameFig().getMinimumSize();
  118. Dimension internalDim = getInternal().getMinimumSize();
  119. int h =
  120. SPACE_TOP + nameDim.height
  121. + SPACE_MIDDLE + internalDim.height
  122. + SPACE_BOTTOM;
  123. int w =
  124. Math.max(nameDim.width + 2 * MARGIN,
  125. internalDim.width + 2 * MARGIN);
  126. return new Dimension(w, h);
  127. }
  128. /*
  129. * @see org.tigris.gef.presentation.Fig#getUseTrapRect()
  130. */
  131. public boolean getUseTrapRect() {
  132. return true;
  133. }
  134. /*
  135. * Override setBounds to keep shapes looking right.
  136. *
  137. * @see org.tigris.gef.presentation.Fig#setBoundsImpl(int, int, int, int)
  138. */
  139. protected void setStandardBounds(int x, int y, int w, int h) {
  140. if (getNameFig() == null) {
  141. return;
  142. }
  143. Rectangle oldBounds = getBounds();
  144. Dimension nameDim = getNameFig().getMinimumSize();
  145. List regionsList = getEnclosedFigs();
  146. /* If it is concurrent and contains concurrent regions,
  147. the bottom region has a minimum height*/
  148. if (getOwner() != null) {
  149. if (isConcurrent()
  150. && !regionsList.isEmpty()
  151. && regionsList.get(regionsList.size() - 1)
  152. instanceof FigConcurrentRegion) {
  153. FigConcurrentRegion f =
  154. ((FigConcurrentRegion) regionsList.get(
  155. regionsList.size() - 1));
  156. Rectangle regionBounds = f.getBounds();
  157. if ((h - oldBounds.height + regionBounds.height)
  158. <= (f.getMinimumSize().height)) {
  159. h = oldBounds.height;
  160. y = oldBounds.y;
  161. }
  162. }
  163. }
  164. getNameFig().setBounds(x + MARGIN,
  165. y + SPACE_TOP,
  166. w - 2 * MARGIN,
  167. nameDim.height);
  168. divider.setShape(x,
  169. y + DIVIDER_Y + nameDim.height,
  170. x + w - 1,
  171. y + DIVIDER_Y + nameDim.height);
  172. getInternal().setBounds(
  173. x + MARGIN,
  174. y + nameDim.height + SPACE_TOP + SPACE_MIDDLE,
  175. w - 2 * MARGIN,
  176. h - nameDim.height - SPACE_TOP - SPACE_MIDDLE - SPACE_BOTTOM);
  177. getBigPort().setBounds(x, y, w, h);
  178. cover.setBounds(x, y, w, h);
  179. calcBounds(); //_x = x; _y = y; _w = w; _h = h;
  180. updateEdges();
  181. firePropChange("bounds", oldBounds, getBounds());
  182. /*If it is concurrent and contains concurrent regions,
  183. the regions are resized*/
  184. if (getOwner() != null) {
  185. if (isConcurrent()
  186. && !regionsList.isEmpty()
  187. && regionsList.get(regionsList.size() - 1)
  188. instanceof FigConcurrentRegion) {
  189. FigConcurrentRegion f = ((FigConcurrentRegion) regionsList
  190. .get(regionsList.size() - 1));
  191. for (int i = 0; i < regionsList.size() - 1; i++) {
  192. ((FigConcurrentRegion) regionsList.get(i))
  193. .setBounds(x - oldBounds.x, y - oldBounds.y,
  194. w - 2 * FigConcurrentRegion.INSET_HORZ, true);
  195. }
  196. f.setBounds(x - oldBounds.x,
  197. y - oldBounds.y,
  198. w - 2 * FigConcurrentRegion.INSET_HORZ,
  199. h - oldBounds.height, true);
  200. }
  201. }
  202. }
  203. /*
  204. * The returned list of Figs is sorted according layout: from top to bottom.
  205. */
  206. @Override
  207. public Vector<Fig> getEnclosedFigs() {
  208. Vector<Fig> enclosedFigs = super.getEnclosedFigs();
  209. if (isConcurrent()) {
  210. TreeMap<Integer, Fig> figsByY = new TreeMap<Integer, Fig>();
  211. for (Fig fig : enclosedFigs) {
  212. if (fig instanceof FigConcurrentRegion) {
  213. figsByY.put(fig.getY(), fig);
  214. }
  215. }
  216. return new Vector<Fig>(figsByY.values());
  217. }
  218. return enclosedFigs;
  219. }
  220. /**
  221. * @return true if this is a concurrent state,
  222. * false otherwise, or if the owner is not known
  223. */
  224. public boolean isConcurrent() {
  225. Object owner = getOwner();
  226. if (owner == null) {
  227. return false;
  228. }
  229. return Model.getFacade().isConcurrent(owner);
  230. }
  231. /**
  232. * To resize only when a new concurrent region is added,
  233. * changing the height.
  234. * TODO: Probably shouldn't
  235. * exist as this class should be listening for added concurrent regions
  236. * and call this internally itself.
  237. *
  238. * @param h the new height
  239. */
  240. public void setCompositeStateHeight(int h) {
  241. if (getNameFig() == null) {
  242. return;
  243. }
  244. Rectangle oldBounds = getBounds();
  245. Dimension nameDim = getNameFig().getMinimumSize();
  246. int x = oldBounds.x;
  247. int y = oldBounds.y;
  248. int w = oldBounds.width;
  249. getInternal().setBounds(
  250. x + MARGIN,
  251. y + nameDim.height + 4,
  252. w - 2 * MARGIN,
  253. h - nameDim.height - 6);
  254. getBigPort().setBounds(x, y, w, h);
  255. cover.setBounds(x, y, w, h);
  256. calcBounds(); //_x = x; _y = y; _w = w; _h = h;
  257. updateEdges();
  258. firePropChange("bounds", oldBounds, getBounds());
  259. }
  260. /*
  261. * @see org.tigris.gef.ui.PopupGenerator#getPopUpActions(java.awt.event.MouseEvent)
  262. */
  263. public Vector getPopUpActions(MouseEvent me) {
  264. Vector popUpActions = super.getPopUpActions(me);
  265. /* Check if multiple items are selected: */
  266. boolean ms = TargetManager.getInstance().getTargets().size() > 1;
  267. if (!ms) {
  268. popUpActions.add(
  269. popUpActions.size() - getPopupAddOffset(),
  270. new ActionAddConcurrentRegion());
  271. }
  272. return popUpActions;
  273. }
  274. /*
  275. * @see org.tigris.gef.presentation.Fig#setLineColor(java.awt.Color)
  276. */
  277. public void setLineColor(Color col) {
  278. cover.setLineColor(col);
  279. divider.setLineColor(col);
  280. }
  281. /*
  282. * @see org.tigris.gef.presentation.Fig#getLineColor()
  283. */
  284. public Color getLineColor() {
  285. return cover.getLineColor();
  286. }
  287. /*
  288. * @see org.tigris.gef.presentation.Fig#setFillColor(java.awt.Color)
  289. */
  290. public void setFillColor(Color col) {
  291. cover.setFillColor(col);
  292. }
  293. /*
  294. * @see org.tigris.gef.presentation.Fig#getFillColor()
  295. */
  296. public Color getFillColor() {
  297. return cover.getFillColor();
  298. }
  299. /*
  300. * @see org.tigris.gef.presentation.Fig#setFilled(boolean)
  301. */
  302. public void setFilled(boolean f) {
  303. cover.setFilled(f);
  304. getBigPort().setFilled(f);
  305. }
  306. @Override
  307. public boolean isFilled() {
  308. return cover.isFilled();
  309. }
  310. /*
  311. * @see org.tigris.gef.presentation.Fig#setLineWidth(int)
  312. */
  313. public void setLineWidth(int w) {
  314. cover.setLineWidth(w);
  315. divider.setLineWidth(w);
  316. }
  317. /*
  318. * @see org.tigris.gef.presentation.Fig#getLineWidth()
  319. */
  320. public int getLineWidth() {
  321. return cover.getLineWidth();
  322. }
  323. ////////////////////////////////////////////////////////////////
  324. // event processing
  325. @Override
  326. protected void updateLayout(UmlChangeEvent event) {
  327. /* We only handle the case where a region has been removed: */
  328. if (!(event instanceof RemoveAssociationEvent)
  329. || !"subvertex".equals(event.getPropertyName())) {
  330. return;
  331. }
  332. final Object removedRegion = event.getOldValue();
  333. List<FigConcurrentRegion> regionFigs =
  334. ((List<FigConcurrentRegion>) getEnclosedFigs().clone());
  335. int totHeight = getInitialHeight();
  336. if (!regionFigs.isEmpty()) {
  337. Fig removedFig = null;
  338. for (FigConcurrentRegion figRegion : regionFigs) {
  339. if (figRegion.getOwner() == removedRegion) {
  340. removedFig = figRegion;
  341. removeEnclosedFig(figRegion);
  342. break;
  343. }
  344. }
  345. if (removedFig != null) {
  346. regionFigs.remove(removedFig);
  347. if (!regionFigs.isEmpty()) {
  348. for (FigConcurrentRegion figRegion : regionFigs) {
  349. if (figRegion.getY() > removedFig.getY()) {
  350. figRegion.displace(0, -removedFig.getHeight());
  351. }
  352. }
  353. totHeight = getHeight() - removedFig.getHeight();
  354. }
  355. }
  356. }
  357. setBounds(getX(), getY(), getWidth(), totHeight);
  358. // do we need to
  359. renderingChanged();
  360. }
  361. /*
  362. * @see org.argouml.uml.diagram.state.ui.FigState#getInitialHeight()
  363. */
  364. protected int getInitialHeight() {
  365. return 150;
  366. }
  367. /*
  368. * @see org.argouml.uml.diagram.state.ui.FigState#getInitialWidth()
  369. */
  370. protected int getInitialWidth() {
  371. return 180;
  372. }
  373. /*
  374. * @see org.argouml.uml.diagram.state.ui.FigState#getInitialX()
  375. */
  376. protected int getInitialX() {
  377. return 0;
  378. }
  379. /*
  380. * @see org.argouml.uml.diagram.state.ui.FigState#getInitialY()
  381. */
  382. protected int getInitialY() {
  383. return 0;
  384. }
  385. } /* end class FigCompositeState */