Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ShadowComboBox |
|
| 2.0;2 | ||||
ShadowComboBox$ShadowFig |
|
| 2.0;2 | ||||
ShadowComboBox$ShadowRenderer |
|
| 2.0;2 |
1 | /* $Id: ShadowComboBox.java 17841 2010-01-12 19:17:52Z 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 | * bobtarling | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2009 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.ui; | |
40 | ||
41 | import java.awt.Component; | |
42 | import java.awt.Dimension; | |
43 | import java.awt.Graphics; | |
44 | ||
45 | import javax.swing.JComboBox; | |
46 | import javax.swing.JComponent; | |
47 | import javax.swing.JList; | |
48 | import javax.swing.ListCellRenderer; | |
49 | ||
50 | import org.argouml.i18n.Translator; | |
51 | import org.argouml.uml.diagram.DiagramSettings; | |
52 | import org.argouml.uml.diagram.ui.FigNodeModelElement; | |
53 | import org.argouml.uml.diagram.ui.FigStereotypesGroup; | |
54 | ||
55 | /** | |
56 | * A ComboBox that contains the set of possible Shadow Width values. | |
57 | * | |
58 | * @author Jeremy Jones | |
59 | */ | |
60 | 19852 | public class ShadowComboBox extends JComboBox { |
61 | ||
62 | /** | |
63 | * The UID. | |
64 | */ | |
65 | private static final long serialVersionUID = 3440806802523267746L; | |
66 | ||
67 | private static ShadowFig[] shadowFigs; | |
68 | ||
69 | /** | |
70 | * The constructor. | |
71 | * | |
72 | */ | |
73 | public ShadowComboBox() { | |
74 | 1800 | super(); |
75 | ||
76 | 1800 | addItem(Translator.localize("label.stylepane.no-shadow")); |
77 | 1800 | addItem("1"); |
78 | 1800 | addItem("2"); |
79 | 1800 | addItem("3"); |
80 | 1800 | addItem("4"); |
81 | 1800 | addItem("5"); |
82 | 1800 | addItem("6"); |
83 | 1800 | addItem("7"); |
84 | 1800 | addItem("8"); |
85 | ||
86 | 1800 | setRenderer(new ShadowRenderer()); |
87 | 1800 | } |
88 | ||
89 | /** | |
90 | * Renders each combo box entry as a shadowed diagram figure with the | |
91 | * associated level of shadow. | |
92 | */ | |
93 | private class ShadowRenderer extends JComponent | |
94 | implements ListCellRenderer { | |
95 | ||
96 | /** | |
97 | * The UID. | |
98 | */ | |
99 | private static final long serialVersionUID = 5939340501470674464L; | |
100 | ||
101 | private ShadowFig currentFig; | |
102 | ||
103 | /** | |
104 | * Constructor. | |
105 | */ | |
106 | 1800 | public ShadowRenderer() { |
107 | 1800 | super(); |
108 | 1800 | } |
109 | ||
110 | /* | |
111 | * @see javax.swing.ListCellRenderer#getListCellRendererComponent( | |
112 | * javax.swing.JList, java.lang.Object, int, boolean, boolean) | |
113 | */ | |
114 | public Component getListCellRendererComponent( | |
115 | JList list, | |
116 | Object value, | |
117 | int index, | |
118 | boolean isSelected, | |
119 | boolean cellHasFocus) { | |
120 | ||
121 | 2170 | if (shadowFigs == null) { |
122 | 119 | shadowFigs = new ShadowFig[ShadowComboBox.this.getItemCount()]; |
123 | ||
124 | 1190 | for (int i = 0; i < shadowFigs.length; ++i) { |
125 | 1071 | shadowFigs[i] = new ShadowFig(); |
126 | 1071 | shadowFigs[i].setShadowSize(i); |
127 | 1071 | shadowFigs[i].setName( |
128 | (String) ShadowComboBox.this.getItemAt(i)); | |
129 | } | |
130 | } | |
131 | ||
132 | 2170 | if (isSelected) { |
133 | 0 | setBackground(list.getSelectionBackground()); |
134 | } else { | |
135 | 2170 | setBackground(list.getBackground()); |
136 | } | |
137 | ||
138 | 2170 | int figIndex = index; |
139 | 2170 | if (figIndex < 0) { |
140 | 10990 | for (int i = 0; i < shadowFigs.length; ++i) { |
141 | 9891 | if (value == ShadowComboBox.this.getItemAt(i)) { |
142 | 1099 | figIndex = i; |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | 2170 | if (figIndex >= 0) { |
148 | 2170 | currentFig = shadowFigs[figIndex]; |
149 | 2170 | setPreferredSize(new Dimension( |
150 | currentFig.getWidth() + figIndex + 4, | |
151 | currentFig.getHeight() + figIndex + 2)); | |
152 | } else { | |
153 | 0 | currentFig = null; |
154 | } | |
155 | ||
156 | 2170 | return this; |
157 | } | |
158 | ||
159 | /* | |
160 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) | |
161 | */ | |
162 | protected void paintComponent(Graphics g) { | |
163 | 28 | g.setColor(getBackground()); |
164 | 28 | g.fillRect(0, 0, getWidth(), getHeight()); |
165 | 28 | if (currentFig != null) { |
166 | 28 | currentFig.setLocation(2, 1); |
167 | 28 | currentFig.paint(g); |
168 | } | |
169 | 28 | } |
170 | } | |
171 | ||
172 | /** | |
173 | * This Fig is never placed on a diagram. It is only used by the call | |
174 | * renderer so that pick list items look like diagram Figs. | |
175 | * TODO: This Fig does not represent a model element and so it | |
176 | * should not extend FigNodeModelElement. We should split | |
177 | * FigNodeModelElement in two, one for base functionality for all nodes | |
178 | * and one that is truly for model elements. | |
179 | */ | |
180 | private static class ShadowFig extends FigNodeModelElement { | |
181 | ||
182 | /** | |
183 | * The UID. | |
184 | */ | |
185 | private static final long serialVersionUID = 4999132551417131227L; | |
186 | ||
187 | /** | |
188 | * Constructor. | |
189 | */ | |
190 | public ShadowFig() { | |
191 | 1071 | super(null, null, new DiagramSettings()); |
192 | 1071 | addFig(getBigPort()); |
193 | 1071 | addFig(getNameFig()); |
194 | 1071 | } |
195 | ||
196 | public void setName(String text) { | |
197 | 1071 | getNameFig().setText(text); |
198 | 1071 | } |
199 | ||
200 | /** | |
201 | * TODO: Bob says - This is a really nasty horrible hack. | |
202 | * ShadowFig should not extend FigNodeModelElement. Instead | |
203 | * we require a base class FigNode with common behaviour of ALL | |
204 | * nodes in ArgoUML. ShadowFig should extend that and | |
205 | * FigNodeModelElement should extend that same base class adding | |
206 | * common functionality for FigNode that represent model element. | |
207 | * @see org.argouml.uml.diagram.ui.FigNodeModelElement#setShadowSize(int) | |
208 | */ | |
209 | public void setShadowSize(int size) { | |
210 | 2142 | super.setShadowSizeFriend(size); |
211 | 2142 | } |
212 | ||
213 | /** | |
214 | * This isn't really a Fig representing a model element so | |
215 | * there is always no stereotype. | |
216 | * @return null | |
217 | */ | |
218 | protected FigStereotypesGroup createStereotypeFig() { | |
219 | 1071 | return null; |
220 | } | |
221 | } | |
222 | } |