1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
package org.argouml.uml.ui; |
41 | |
|
42 | |
import java.awt.BorderLayout; |
43 | |
import java.awt.Font; |
44 | |
import java.awt.event.ActionEvent; |
45 | |
import java.awt.event.ComponentEvent; |
46 | |
import java.awt.event.ComponentListener; |
47 | |
import java.util.Collection; |
48 | |
|
49 | |
import javax.swing.Action; |
50 | |
import javax.swing.DefaultCellEditor; |
51 | |
import javax.swing.DefaultListSelectionModel; |
52 | |
import javax.swing.JButton; |
53 | |
import javax.swing.JLabel; |
54 | |
import javax.swing.JPanel; |
55 | |
import javax.swing.JScrollPane; |
56 | |
import javax.swing.JTable; |
57 | |
import javax.swing.JToolBar; |
58 | |
import javax.swing.event.ListSelectionEvent; |
59 | |
import javax.swing.event.ListSelectionListener; |
60 | |
import javax.swing.table.TableCellEditor; |
61 | |
import javax.swing.table.TableColumn; |
62 | |
|
63 | |
import org.apache.log4j.Logger; |
64 | |
import org.argouml.application.api.AbstractArgoJPanel; |
65 | |
import org.argouml.application.helpers.ResourceLoaderWrapper; |
66 | |
import org.argouml.i18n.Translator; |
67 | |
import org.argouml.model.InvalidElementException; |
68 | |
import org.argouml.model.Model; |
69 | |
import org.argouml.swingext.UpArrowIcon; |
70 | |
import org.argouml.ui.LookAndFeelMgr; |
71 | |
import org.argouml.ui.TabModelTarget; |
72 | |
import org.argouml.ui.targetmanager.TargetEvent; |
73 | |
import org.argouml.uml.ui.foundation.extension_mechanisms.ActionNewTagDefinition; |
74 | |
import org.argouml.uml.ui.foundation.extension_mechanisms.UMLTagDefinitionComboBoxModel; |
75 | |
import org.tigris.gef.presentation.Fig; |
76 | |
import org.argouml.ui.UndoableAction; |
77 | |
import org.tigris.toolbar.ToolBar; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public class TabTaggedValues extends AbstractArgoJPanel |
83 | |
implements TabModelTarget, ListSelectionListener, ComponentListener { |
84 | |
|
85 | 900 | private static final Logger LOG = Logger.getLogger(TabTaggedValues.class); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private static final long serialVersionUID = -8566948113385239423L; |
91 | |
|
92 | |
private Object target; |
93 | 900 | private boolean shouldBeEnabled = false; |
94 | 900 | private JTable table = new JTable(10, 2); |
95 | |
private JLabel titleLabel; |
96 | |
private JToolBar buttonPanel; |
97 | |
|
98 | |
private UMLComboBox2 tagDefinitionsComboBox; |
99 | |
|
100 | |
private UMLComboBoxModel2 tagDefinitionsComboBoxModel; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public TabTaggedValues() { |
107 | 900 | super("tab.tagged-values"); |
108 | 900 | setIcon(new UpArrowIcon()); |
109 | 900 | buttonPanel = new ToolBar(); |
110 | 900 | buttonPanel.setName(getTitle()); |
111 | 900 | buttonPanel.setFloatable(false); |
112 | |
|
113 | 900 | JButton b = null; |
114 | 900 | if (Model.getFacade().getUmlVersion().charAt(0) == '1') { |
115 | |
|
116 | 900 | b = new JButton(); |
117 | 900 | buttonPanel.add(b); |
118 | 900 | b.setAction(new ActionNewTagDefinition()); |
119 | 900 | b.setText(""); |
120 | 900 | b.setFocusable(false); |
121 | |
} |
122 | |
|
123 | 900 | b = new JButton(); |
124 | 900 | buttonPanel.add(b); |
125 | 900 | b.setToolTipText(Translator.localize("button.delete")); |
126 | 900 | b.setAction(new ActionRemoveTaggedValue(table)); |
127 | 900 | b.setText(""); |
128 | 900 | b.setFocusable(false); |
129 | |
|
130 | 900 | table.setModel(new TabTaggedValuesModel()); |
131 | 900 | table.setRowSelectionAllowed(false); |
132 | 900 | tagDefinitionsComboBoxModel = new UMLTagDefinitionComboBoxModel(); |
133 | 900 | tagDefinitionsComboBox = new UMLComboBox2(tagDefinitionsComboBoxModel); |
134 | 900 | Class tagDefinitionClass = (Class) Model.getMetaTypes() |
135 | |
.getTagDefinition(); |
136 | 900 | tagDefinitionsComboBox.setRenderer(new UMLListCellRenderer2(false)); |
137 | 900 | table.setDefaultEditor(tagDefinitionClass, |
138 | |
new DefaultCellEditor(tagDefinitionsComboBox)); |
139 | 900 | table.setDefaultRenderer(tagDefinitionClass, |
140 | |
new UMLTableCellRenderer()); |
141 | 900 | table.getSelectionModel().addListSelectionListener(this); |
142 | |
|
143 | 900 | JScrollPane sp = new JScrollPane(table); |
144 | 900 | Font labelFont = LookAndFeelMgr.getInstance().getStandardFont(); |
145 | 900 | table.setFont(labelFont); |
146 | |
|
147 | 900 | titleLabel = new JLabel("none"); |
148 | 900 | resizeColumns(); |
149 | 900 | setLayout(new BorderLayout()); |
150 | 900 | titleLabel.setLabelFor(buttonPanel); |
151 | |
|
152 | 900 | JPanel topPane = new JPanel(new BorderLayout()); |
153 | 900 | topPane.add(titleLabel, BorderLayout.WEST); |
154 | 900 | topPane.add(buttonPanel, BorderLayout.CENTER); |
155 | |
|
156 | 900 | add(topPane, BorderLayout.NORTH); |
157 | 900 | add(sp, BorderLayout.CENTER); |
158 | |
|
159 | 900 | addComponentListener(this); |
160 | 900 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void resizeColumns() { |
166 | 900 | TableColumn keyCol = table.getColumnModel().getColumn(0); |
167 | 900 | TableColumn valCol = table.getColumnModel().getColumn(1); |
168 | 900 | keyCol.setMinWidth(50); |
169 | 900 | keyCol.setWidth(150); |
170 | 900 | keyCol.setPreferredWidth(150); |
171 | 900 | valCol.setMinWidth(250); |
172 | 900 | valCol.setWidth(550); |
173 | 900 | valCol.setPreferredWidth(550); |
174 | 900 | table.doLayout(); |
175 | 900 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public void setTarget(Object theTarget) { |
181 | 0 | stopEditing(); |
182 | |
|
183 | 0 | Object t = (theTarget instanceof Fig) |
184 | |
? ((Fig) theTarget).getOwner() : theTarget; |
185 | 0 | if (!Model.getFacade().isAUMLElement(t)) { |
186 | 0 | target = null; |
187 | 0 | shouldBeEnabled = false; |
188 | 0 | return; |
189 | |
} |
190 | 0 | target = t; |
191 | 0 | shouldBeEnabled = true; |
192 | |
|
193 | |
|
194 | 0 | if (isVisible()) { |
195 | 0 | setTargetInternal(target); |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
private void stopEditing() { |
203 | 900 | if (table.isEditing()) { |
204 | 0 | TableCellEditor ce = table.getCellEditor(); |
205 | |
try { |
206 | 0 | if (ce != null && !ce.stopCellEditing()) { |
207 | 0 | ce.cancelCellEditing(); |
208 | |
} |
209 | 0 | } catch (InvalidElementException e) { |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 0 | LOG.warn("failed to cancel editing - " |
215 | |
+ "model element deleted while edit in progress"); |
216 | 0 | } |
217 | |
} |
218 | 900 | } |
219 | |
|
220 | |
private void setTargetInternal(Object t) { |
221 | 900 | tagDefinitionsComboBoxModel.setTarget(t); |
222 | |
|
223 | 900 | table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); |
224 | |
|
225 | 900 | ((TabTaggedValuesModel) table.getModel()).setTarget(t); |
226 | 900 | table.sizeColumnsToFit(0); |
227 | |
|
228 | 900 | if (t != null) { |
229 | 0 | titleLabel.setText("Target: " |
230 | |
+ Model.getFacade().getUMLClassName(t) |
231 | |
+ " (" |
232 | |
+ Model.getFacade().getName(t) + ")"); |
233 | |
} else { |
234 | 900 | titleLabel.setText("none"); |
235 | |
} |
236 | 900 | validate(); |
237 | 900 | } |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public Object getTarget() { |
243 | 0 | return target; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public void refresh() { |
250 | 0 | setTarget(target); |
251 | 0 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public boolean shouldBeEnabled(Object theTarget) { |
257 | 1327 | Object t = (theTarget instanceof Fig) |
258 | |
? ((Fig) theTarget).getOwner() : theTarget; |
259 | 1327 | shouldBeEnabled = Model.getFacade().isAUMLElement(t); |
260 | 1327 | return shouldBeEnabled; |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
public void targetAdded(TargetEvent e) { |
268 | 0 | setTarget(e.getNewTarget()); |
269 | 0 | } |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public void targetRemoved(TargetEvent e) { |
276 | 0 | setTarget(e.getNewTarget()); |
277 | 0 | } |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public void targetSet(TargetEvent e) { |
284 | 0 | setTarget(e.getNewTarget()); |
285 | 0 | } |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
protected TabTaggedValuesModel getTableModel() { |
291 | 0 | return (TabTaggedValuesModel) table.getModel(); |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
protected JTable getTable() { |
297 | 0 | return table; |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public void valueChanged(ListSelectionEvent e) { |
304 | 0 | if (!e.getValueIsAdjusting()) { |
305 | 0 | DefaultListSelectionModel sel = |
306 | |
(DefaultListSelectionModel) e.getSource(); |
307 | 0 | Collection tvs = |
308 | |
Model.getFacade().getTaggedValuesCollection(target); |
309 | 0 | int index = sel.getLeadSelectionIndex(); |
310 | 0 | if (index >= 0 && index < tvs.size()) { |
311 | 0 | Object tagDef = Model.getFacade().getTagDefinition( |
312 | |
TabTaggedValuesModel.getFromCollection(tvs, index)); |
313 | 0 | tagDefinitionsComboBoxModel.setSelectedItem(tagDef); |
314 | |
} |
315 | |
} |
316 | 0 | } |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
public void componentShown(ComponentEvent e) { |
322 | |
|
323 | 0 | setTargetInternal(target); |
324 | 0 | } |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public void componentHidden(ComponentEvent e) { |
330 | |
|
331 | 900 | stopEditing(); |
332 | 900 | setTargetInternal(null); |
333 | 900 | } |
334 | |
|
335 | |
public void componentMoved(ComponentEvent e) { |
336 | |
|
337 | 900 | } |
338 | |
|
339 | |
public void componentResized(ComponentEvent e) { |
340 | |
|
341 | 900 | } |
342 | |
|
343 | |
|
344 | |
} |
345 | |
|
346 | |
class ActionRemoveTaggedValue extends UndoableAction { |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
private static final long serialVersionUID = 8276763533039642549L; |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
private JTable table; |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public ActionRemoveTaggedValue(JTable tableTv) { |
364 | 900 | super(Translator.localize("button.delete"), |
365 | |
ResourceLoaderWrapper.lookupIcon("Delete")); |
366 | |
|
367 | 900 | putValue(Action.SHORT_DESCRIPTION, |
368 | |
Translator.localize("button.delete")); |
369 | 900 | table = tableTv; |
370 | 900 | } |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
@Override |
376 | |
public void actionPerformed(ActionEvent e) { |
377 | 0 | super.actionPerformed(e); |
378 | 0 | TabTaggedValuesModel model = (TabTaggedValuesModel) table.getModel(); |
379 | 0 | model.removeRow(table.getSelectedRow()); |
380 | 0 | } |
381 | |
} |