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 | |
|
41 | |
package org.argouml.ui; |
42 | |
|
43 | |
import java.awt.BorderLayout; |
44 | |
import java.awt.Component; |
45 | |
import java.awt.Dimension; |
46 | |
import java.awt.FlowLayout; |
47 | |
import java.awt.event.ActionEvent; |
48 | |
import java.awt.event.ActionListener; |
49 | |
import java.awt.event.ItemEvent; |
50 | |
import java.awt.event.ItemListener; |
51 | |
import java.io.File; |
52 | |
import java.util.ArrayList; |
53 | |
import java.util.Collection; |
54 | |
import java.util.List; |
55 | |
|
56 | |
import javax.swing.BoxLayout; |
57 | |
import javax.swing.DefaultComboBoxModel; |
58 | |
import javax.swing.JButton; |
59 | |
import javax.swing.JComboBox; |
60 | |
import javax.swing.JFileChooser; |
61 | |
import javax.swing.JLabel; |
62 | |
import javax.swing.JList; |
63 | |
import javax.swing.JOptionPane; |
64 | |
import javax.swing.JPanel; |
65 | |
import javax.swing.JScrollPane; |
66 | |
import javax.swing.MutableComboBoxModel; |
67 | |
import javax.swing.filechooser.FileFilter; |
68 | |
|
69 | |
import org.argouml.application.api.GUISettingsTabInterface; |
70 | |
import org.argouml.configuration.Configuration; |
71 | |
import org.argouml.i18n.Translator; |
72 | |
import org.argouml.kernel.ProfileConfiguration; |
73 | |
import org.argouml.model.Model; |
74 | |
import org.argouml.profile.Profile; |
75 | |
import org.argouml.profile.ProfileException; |
76 | |
import org.argouml.profile.ProfileFacade; |
77 | |
import org.argouml.profile.UserDefinedProfile; |
78 | |
import org.argouml.profile.UserDefinedProfileHelper; |
79 | |
import org.argouml.swingext.JLinkButton; |
80 | |
import org.argouml.uml.diagram.DiagramAppearance; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | public class SettingsTabProfile extends JPanel implements |
88 | |
GUISettingsTabInterface, ActionListener { |
89 | |
|
90 | |
private JButton loadFromFile; |
91 | |
|
92 | |
private JButton addButton; |
93 | |
|
94 | |
private JButton removeButton; |
95 | |
|
96 | |
private JList availableList; |
97 | |
|
98 | |
private JList defaultList; |
99 | |
|
100 | |
|
101 | |
|
102 | |
private JList directoryList; |
103 | |
|
104 | |
private JButton addDirectory; |
105 | |
|
106 | |
private JButton removeDirectory; |
107 | |
|
108 | 900 | private JButton refreshProfiles = new JButton(Translator |
109 | |
.localize("tab.profiles.directories.refresh")); |
110 | |
|
111 | |
|
112 | |
|
113 | |
private JLabel stereoLabel; |
114 | |
|
115 | |
private JComboBox stereoField; |
116 | |
|
117 | 900 | private boolean initialized = false; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | 900 | public SettingsTabProfile() { |
123 | 900 | } |
124 | |
|
125 | |
private void buildPanel() { |
126 | 19 | setLayout(new BorderLayout()); |
127 | |
|
128 | 19 | JPanel warning = new JPanel(); |
129 | 19 | warning.setLayout(new BoxLayout(warning, BoxLayout.PAGE_AXIS)); |
130 | 19 | JLabel warningLabel = new JLabel(Translator.localize("label.warning")); |
131 | 19 | warningLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); |
132 | 19 | warning.add(warningLabel); |
133 | |
|
134 | 19 | JLinkButton projectSettings = new JLinkButton(); |
135 | 19 | projectSettings.setAction(new ActionProjectSettings()); |
136 | 19 | projectSettings.setText(Translator.localize("button.project-settings")); |
137 | 19 | projectSettings.setIcon(null); |
138 | 19 | projectSettings.setAlignmentX(Component.RIGHT_ALIGNMENT); |
139 | 19 | warning.add(projectSettings); |
140 | |
|
141 | 19 | add(warning, BorderLayout.NORTH); |
142 | |
|
143 | 19 | JPanel profileSettings = new JPanel(); |
144 | 19 | profileSettings.setLayout(new BoxLayout(profileSettings, |
145 | |
BoxLayout.Y_AXIS)); |
146 | |
|
147 | 19 | profileSettings.add(initDefaultStereotypeViewSelector()); |
148 | |
|
149 | 19 | JPanel sdirPanel = new JPanel(); |
150 | 19 | sdirPanel.setLayout(new BoxLayout(sdirPanel, BoxLayout.Y_AXIS)); |
151 | |
|
152 | |
|
153 | 19 | JPanel lcb = new JPanel(); |
154 | 19 | lcb.setLayout(new BoxLayout(lcb, BoxLayout.Y_AXIS)); |
155 | |
|
156 | 19 | addDirectory = new JButton(Translator |
157 | |
.localize("tab.profiles.directories.add")); |
158 | 19 | removeDirectory = new JButton(Translator |
159 | |
.localize("tab.profiles.directories.remove")); |
160 | |
|
161 | 19 | lcb.add(addDirectory); |
162 | 19 | lcb.add(removeDirectory); |
163 | |
|
164 | 19 | addDirectory.addActionListener(this); |
165 | 19 | removeDirectory.addActionListener(this); |
166 | |
|
167 | 19 | directoryList = new JList(); |
168 | 19 | directoryList.setMinimumSize(new Dimension(50, 50)); |
169 | |
|
170 | 19 | JPanel dlist = new JPanel(); |
171 | 19 | dlist.setLayout(new BorderLayout()); |
172 | 19 | dlist.add(new JScrollPane(directoryList), BorderLayout.CENTER); |
173 | 19 | dlist.add(lcb, BorderLayout.EAST); |
174 | |
|
175 | 19 | sdirPanel.add(new JLabel(Translator |
176 | |
.localize("tab.profiles.directories.desc"))); |
177 | 19 | sdirPanel.add(dlist); |
178 | |
|
179 | 19 | profileSettings.add(sdirPanel); |
180 | |
|
181 | 19 | JPanel configPanel = new JPanel(); |
182 | 19 | configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS)); |
183 | |
|
184 | 19 | availableList = createProfileList(); |
185 | 19 | defaultList = createProfileList(); |
186 | |
|
187 | 19 | refreshLists(); |
188 | |
|
189 | 19 | JPanel leftList = new JPanel(); |
190 | 19 | leftList.setLayout(new BorderLayout()); |
191 | 19 | leftList.add(new JLabel(Translator |
192 | |
.localize("tab.profiles.userdefined.available")), |
193 | |
BorderLayout.NORTH); |
194 | 19 | leftList.add(new JScrollPane(availableList), BorderLayout.CENTER); |
195 | 19 | configPanel.add(leftList); |
196 | |
|
197 | 19 | JPanel centerButtons = new JPanel(); |
198 | 19 | centerButtons.setLayout(new BoxLayout(centerButtons, BoxLayout.Y_AXIS)); |
199 | 19 | addButton = new JButton(">>"); |
200 | 19 | removeButton = new JButton("<<"); |
201 | 19 | centerButtons.add(addButton); |
202 | 19 | centerButtons.add(removeButton); |
203 | 19 | configPanel.add(centerButtons); |
204 | |
|
205 | 19 | JPanel rightList = new JPanel(); |
206 | 19 | rightList.setLayout(new BorderLayout()); |
207 | 19 | rightList.add(new JLabel(Translator |
208 | |
.localize("tab.profiles.userdefined.default")), |
209 | |
BorderLayout.NORTH); |
210 | |
|
211 | 19 | rightList.add(new JScrollPane(defaultList), BorderLayout.CENTER); |
212 | 19 | configPanel.add(rightList); |
213 | |
|
214 | 19 | addButton.addActionListener(this); |
215 | 19 | removeButton.addActionListener(this); |
216 | |
|
217 | 19 | profileSettings.add(configPanel); |
218 | |
|
219 | 19 | JPanel lffPanel = new JPanel(); |
220 | 19 | lffPanel.setLayout(new FlowLayout()); |
221 | 19 | loadFromFile = new JButton(Translator |
222 | |
.localize("tab.profiles.userdefined.load")); |
223 | 19 | lffPanel.add(loadFromFile); |
224 | 19 | lffPanel.add(refreshProfiles); |
225 | |
|
226 | 19 | loadFromFile.addActionListener(this); |
227 | 19 | refreshProfiles.addActionListener(this); |
228 | |
|
229 | 19 | profileSettings.add(lffPanel); |
230 | |
|
231 | 19 | add(profileSettings, BorderLayout.CENTER); |
232 | |
|
233 | 19 | initialized = true; |
234 | 19 | } |
235 | |
|
236 | |
|
237 | |
@Override |
238 | |
public void setVisible(boolean flag) { |
239 | 19 | if (flag && !initialized) { |
240 | 0 | buildPanel(); |
241 | |
} |
242 | 19 | super.setVisible(flag); |
243 | 19 | } |
244 | |
|
245 | |
private JList createProfileList() { |
246 | 38 | JList list = new JList(); |
247 | 38 | list.setMinimumSize(new Dimension(50, 50)); |
248 | 38 | return list; |
249 | |
} |
250 | |
|
251 | |
private JPanel initDefaultStereotypeViewSelector() { |
252 | 19 | JPanel setDefStereoV = new JPanel(); |
253 | 19 | setDefStereoV.setLayout(new FlowLayout()); |
254 | 19 | stereoField = new JComboBox(); |
255 | 19 | stereoLabel = new JLabel(Translator |
256 | |
.localize("menu.popup.stereotype-view") |
257 | |
+ ": "); |
258 | 19 | stereoLabel.setLabelFor(stereoField); |
259 | 19 | setDefStereoV.add(stereoLabel); |
260 | 19 | setDefStereoV.add(stereoField); |
261 | |
|
262 | 19 | DefaultComboBoxModel cmodel = new DefaultComboBoxModel(); |
263 | 19 | stereoField.setModel(cmodel); |
264 | |
|
265 | 19 | cmodel.addElement(Translator |
266 | |
.localize("menu.popup.stereotype-view.textual")); |
267 | 19 | cmodel.addElement(Translator |
268 | |
.localize("menu.popup.stereotype-view.big-icon")); |
269 | 19 | cmodel.addElement(Translator |
270 | |
.localize("menu.popup.stereotype-view.small-icon")); |
271 | |
|
272 | 19 | stereoField.addItemListener(new ItemListener() { |
273 | |
|
274 | |
public void itemStateChanged(ItemEvent e) { |
275 | 0 | Object src = e.getSource(); |
276 | |
|
277 | 0 | if (src == stereoField) { |
278 | 0 | Object item = e.getItem(); |
279 | 0 | DefaultComboBoxModel model = |
280 | |
(DefaultComboBoxModel) stereoField.getModel(); |
281 | 0 | int idx = model.getIndexOf(item); |
282 | |
|
283 | 0 | switch (idx) { |
284 | |
case 0: |
285 | 0 | Configuration |
286 | |
.setInteger( |
287 | |
ProfileConfiguration.KEY_DEFAULT_STEREOTYPE_VIEW, |
288 | |
DiagramAppearance.STEREOTYPE_VIEW_TEXTUAL); |
289 | 0 | break; |
290 | |
case 1: |
291 | 0 | Configuration |
292 | |
.setInteger( |
293 | |
ProfileConfiguration.KEY_DEFAULT_STEREOTYPE_VIEW, |
294 | |
DiagramAppearance.STEREOTYPE_VIEW_BIG_ICON); |
295 | 0 | break; |
296 | |
case 2: |
297 | 0 | Configuration |
298 | |
.setInteger( |
299 | |
ProfileConfiguration.KEY_DEFAULT_STEREOTYPE_VIEW, |
300 | |
DiagramAppearance.STEREOTYPE_VIEW_SMALL_ICON); |
301 | |
break; |
302 | |
} |
303 | |
} |
304 | 0 | } |
305 | |
|
306 | |
}); |
307 | 19 | return setDefStereoV; |
308 | |
} |
309 | |
|
310 | |
private void refreshLists() { |
311 | 38 | availableList.setModel(new DefaultComboBoxModel(getAvailableProfiles() |
312 | |
.toArray())); |
313 | 38 | defaultList.setModel(new DefaultComboBoxModel(getUsedProfiles() |
314 | |
.toArray())); |
315 | 38 | directoryList.setModel(new DefaultComboBoxModel(ProfileFacade |
316 | |
.getManager().getSearchPathDirectories().toArray())); |
317 | 38 | } |
318 | |
|
319 | |
private List<Profile> getUsedProfiles() { |
320 | 76 | return new ArrayList<Profile>(ProfileFacade.getManager() |
321 | |
.getDefaultProfiles()); |
322 | |
} |
323 | |
|
324 | |
private List<Profile> getAvailableProfiles() { |
325 | 38 | List<Profile> used = getUsedProfiles(); |
326 | 38 | List<Profile> ret = new ArrayList<Profile>(); |
327 | |
|
328 | 38 | for (Profile profile : ProfileFacade.getManager() |
329 | |
.getRegisteredProfiles()) { |
330 | 152 | if (!used.contains(profile)) { |
331 | 38 | ret.add(profile); |
332 | |
} |
333 | |
} |
334 | |
|
335 | 38 | return ret; |
336 | |
} |
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public void actionPerformed(ActionEvent arg0) { |
342 | 0 | MutableComboBoxModel modelAvl = ((MutableComboBoxModel) availableList |
343 | |
.getModel()); |
344 | 0 | MutableComboBoxModel modelUsd = ((MutableComboBoxModel) defaultList |
345 | |
.getModel()); |
346 | |
|
347 | 0 | if (arg0.getSource() == addButton) { |
348 | 0 | Object[] selections = availableList.getSelectedValues(); |
349 | 0 | for (Object s : selections) { |
350 | 0 | Profile selected = (Profile) s; |
351 | 0 | modelUsd.addElement(selected); |
352 | 0 | modelAvl.removeElement(selected); |
353 | |
} |
354 | 0 | } else if (arg0.getSource() == removeButton) { |
355 | 0 | Object[] selections = defaultList.getSelectedValues(); |
356 | 0 | for (Object s : selections) { |
357 | 0 | Profile selected = (Profile) s; |
358 | 0 | if (selected == ProfileFacade.getManager().getUMLProfile() |
359 | |
&& Model.getFacade().getUmlVersion().charAt(0) != '1') { |
360 | 0 | JOptionPane.showMessageDialog(this, Translator |
361 | |
.localize("tab.profiles.cantremoveuml")); |
362 | |
} else { |
363 | 0 | modelUsd.removeElement(selected); |
364 | 0 | modelAvl.addElement(selected); |
365 | |
} |
366 | |
} |
367 | 0 | } else if (arg0.getSource() == loadFromFile) { |
368 | 0 | JFileChooser fileChooser = |
369 | |
UserDefinedProfileHelper.createUserDefinedProfileFileChooser(); |
370 | 0 | int ret = fileChooser.showOpenDialog(this); |
371 | 0 | List<File> files = null; |
372 | 0 | if (ret == JFileChooser.APPROVE_OPTION) { |
373 | 0 | files = UserDefinedProfileHelper.getFileList( |
374 | |
fileChooser.getSelectedFiles()); |
375 | |
} |
376 | 0 | if (files != null && files.size() > 0) { |
377 | 0 | for (File file : files) { |
378 | |
try { |
379 | 0 | UserDefinedProfile profile = |
380 | |
new UserDefinedProfile(file, |
381 | |
ProfileFacade.getManager()); |
382 | 0 | ProfileFacade.getManager().registerProfile(profile); |
383 | 0 | modelAvl.addElement(profile); |
384 | 0 | } catch (ProfileException e) { |
385 | 0 | JOptionPane.showMessageDialog(this, Translator |
386 | |
.localize("tab.profiles.userdefined.errorloading") |
387 | |
+ ": " + file.getAbsolutePath()); |
388 | 0 | } |
389 | |
} |
390 | |
} |
391 | |
|
392 | 0 | } else if (arg0.getSource() == removeDirectory) { |
393 | 0 | MutableComboBoxModel model = |
394 | |
((MutableComboBoxModel) directoryList.getModel()); |
395 | 0 | Object[] selections = directoryList.getSelectedValues(); |
396 | 0 | for (Object o : selections) { |
397 | 0 | model.removeElement(o); |
398 | |
} |
399 | 0 | } else if (arg0.getSource() == refreshProfiles) { |
400 | 0 | boolean refresh = JOptionPane.showConfirmDialog(this, Translator |
401 | |
.localize("tab.profiles.confirmrefresh"), Translator |
402 | |
.localize("tab.profiles.confirmrefresh.title"), |
403 | |
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; |
404 | 0 | if (refresh) { |
405 | 0 | handleSettingsTabSave(); |
406 | 0 | ProfileFacade.getManager().refreshRegisteredProfiles(); |
407 | 0 | refreshLists(); |
408 | |
} |
409 | 0 | } else if (arg0.getSource() == addDirectory) { |
410 | 0 | JFileChooser fileChooser = new JFileChooser(); |
411 | 0 | fileChooser.setFileFilter(new FileFilter() { |
412 | |
|
413 | |
public boolean accept(File file) { |
414 | 0 | return file.isDirectory(); |
415 | |
} |
416 | |
|
417 | |
public String getDescription() { |
418 | 0 | return "Directories"; |
419 | |
} |
420 | |
|
421 | |
}); |
422 | |
|
423 | 0 | fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
424 | |
|
425 | 0 | int ret = fileChooser.showOpenDialog(this); |
426 | 0 | if (ret == JFileChooser.APPROVE_OPTION) { |
427 | 0 | File file = fileChooser.getSelectedFile(); |
428 | |
|
429 | |
|
430 | 0 | Collection<File> dirs = new ArrayList<File>(); |
431 | 0 | collectSubdirs(file, dirs); |
432 | |
|
433 | 0 | MutableComboBoxModel cbModel = |
434 | |
((MutableComboBoxModel) directoryList.getModel()); |
435 | 0 | for (File dir : dirs) { |
436 | 0 | cbModel.addElement(dir.getAbsolutePath()); |
437 | |
} |
438 | |
} |
439 | |
} |
440 | |
|
441 | 0 | availableList.validate(); |
442 | 0 | defaultList.validate(); |
443 | 0 | } |
444 | |
|
445 | |
private void collectSubdirs(File file, Collection<File> subdirs) { |
446 | 0 | subdirs.add(file); |
447 | 0 | File[] files = file.listFiles(); |
448 | 0 | if (files != null) { |
449 | 0 | for (File f : files) { |
450 | 0 | if (f.isDirectory() & !f.isHidden()) { |
451 | 0 | collectSubdirs(f, subdirs); |
452 | |
} |
453 | |
} |
454 | |
} |
455 | 0 | } |
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
public String getTabKey() { |
462 | 19 | return "tab.profiles"; |
463 | |
} |
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
public JPanel getTabPanel() { |
470 | 19 | return this; |
471 | |
} |
472 | |
|
473 | |
public void handleResetToDefault() { |
474 | 0 | if (!initialized) { |
475 | 0 | buildPanel(); |
476 | |
} |
477 | 0 | refreshLists(); |
478 | 0 | } |
479 | |
|
480 | |
public void handleSettingsTabCancel() { |
481 | 0 | } |
482 | |
|
483 | |
public void handleSettingsTabRefresh() { |
484 | 19 | if (!initialized) { |
485 | 19 | buildPanel(); |
486 | |
} |
487 | 19 | refreshLists(); |
488 | |
|
489 | 19 | switch (Configuration.getInteger( |
490 | |
ProfileConfiguration.KEY_DEFAULT_STEREOTYPE_VIEW, |
491 | |
DiagramAppearance.STEREOTYPE_VIEW_TEXTUAL)) { |
492 | |
case DiagramAppearance.STEREOTYPE_VIEW_TEXTUAL: |
493 | 19 | stereoField.setSelectedIndex(0); |
494 | 19 | break; |
495 | |
case DiagramAppearance.STEREOTYPE_VIEW_BIG_ICON: |
496 | 0 | stereoField.setSelectedIndex(1); |
497 | 0 | break; |
498 | |
case DiagramAppearance.STEREOTYPE_VIEW_SMALL_ICON: |
499 | 0 | stereoField.setSelectedIndex(2); |
500 | |
break; |
501 | |
} |
502 | 19 | } |
503 | |
|
504 | |
public void handleSettingsTabSave() { |
505 | 0 | List<Profile> toRemove = new ArrayList<Profile>(); |
506 | 0 | List<Profile> usedItens = new ArrayList<Profile>(); |
507 | |
|
508 | 0 | MutableComboBoxModel modelUsd = ((MutableComboBoxModel) defaultList |
509 | |
.getModel()); |
510 | 0 | MutableComboBoxModel modelDir = ((MutableComboBoxModel) directoryList |
511 | |
.getModel()); |
512 | |
|
513 | 0 | for (int i = 0; i < modelUsd.getSize(); ++i) { |
514 | 0 | usedItens.add((Profile) modelUsd.getElementAt(i)); |
515 | |
} |
516 | |
|
517 | |
for (Profile profile |
518 | 0 | : ProfileFacade.getManager().getDefaultProfiles()) { |
519 | 0 | if (!usedItens.contains(profile)) { |
520 | 0 | toRemove.add(profile); |
521 | |
} |
522 | |
} |
523 | |
|
524 | 0 | for (Profile profile : toRemove) { |
525 | 0 | ProfileFacade.getManager().removeFromDefaultProfiles(profile); |
526 | |
} |
527 | |
|
528 | 0 | for (Profile profile : usedItens) { |
529 | 0 | if (!ProfileFacade.getManager().getDefaultProfiles().contains( |
530 | |
profile)) { |
531 | 0 | ProfileFacade.getManager().addToDefaultProfiles(profile); |
532 | |
} |
533 | |
} |
534 | |
|
535 | 0 | List<String> toRemoveDir = new ArrayList<String>(); |
536 | 0 | List<String> usedItensDir = new ArrayList<String>(); |
537 | |
|
538 | 0 | for (int i = 0; i < modelDir.getSize(); ++i) { |
539 | 0 | usedItensDir.add((String) modelDir.getElementAt(i)); |
540 | |
} |
541 | |
|
542 | 0 | for (String dirEntry : ProfileFacade.getManager() |
543 | |
.getSearchPathDirectories()) { |
544 | 0 | if (!usedItensDir.contains(dirEntry)) { |
545 | 0 | toRemoveDir.add(dirEntry); |
546 | |
} |
547 | |
} |
548 | |
|
549 | 0 | for (String dirEntry : toRemoveDir) { |
550 | 0 | ProfileFacade.getManager().removeSearchPathDirectory(dirEntry); |
551 | |
} |
552 | |
|
553 | 0 | for (String dirEntry : usedItensDir) { |
554 | 0 | if (!ProfileFacade.getManager().getSearchPathDirectories() |
555 | |
.contains(dirEntry)) { |
556 | 0 | ProfileFacade.getManager().addSearchPathDirectory(dirEntry); |
557 | |
} |
558 | |
} |
559 | |
|
560 | 0 | } |
561 | |
|
562 | |
} |