Coverage Report - org.argouml.ui.ProjectSettingsTabProfile
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectSettingsTabProfile
64%
121/187
34%
32/94
3.571
ProjectSettingsTabProfile$1
5%
1/17
0%
0/8
3.571
ProjectSettingsTabProfile$ProfileFileFilter
90%
10/11
75%
6/8
3.571
 
 1  
 /* $Id: ProjectSettingsTabProfile.java 18281 2010-04-17 21:32:02Z tfmorris $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2007-2010 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  
  *    Marcos Aurelio - design and initial implementation
 11  
  *    thn
 12  
  *    euluis
 13  
  *****************************************************************************
 14  
  *
 15  
  * Some portions of this file was previously release using the BSD License:
 16  
  */
 17  
 
 18  
 // Copyright (c) 2007-2009 The Regents of the University of California. All
 19  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 20  
 // software and its documentation without fee, and without a written
 21  
 // agreement is hereby granted, provided that the above copyright notice
 22  
 // and this paragraph appear in all copies. This software program and
 23  
 // documentation are copyrighted by The Regents of the University of
 24  
 // California. The software program and documentation are supplied "AS
 25  
 // IS", without any accompanying services from The Regents. The Regents
 26  
 // does not warrant that the operation of the program will be
 27  
 // uninterrupted or error-free. The end-user understands that the program
 28  
 // was developed for research purposes and is advised not to rely
 29  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 30  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 31  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 32  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 33  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 34  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 35  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 36  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 37  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 38  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 39  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 40  
 
 41  
 package org.argouml.ui;
 42  
 
 43  
 import java.awt.BorderLayout;
 44  
 import java.awt.Dimension;
 45  
 import java.awt.FlowLayout;
 46  
 import java.awt.event.ActionEvent;
 47  
 import java.awt.event.ActionListener;
 48  
 import java.awt.event.ItemEvent;
 49  
 import java.awt.event.ItemListener;
 50  
 import java.io.File;
 51  
 import java.util.ArrayList;
 52  
 import java.util.Collection;
 53  
 import java.util.List;
 54  
 
 55  
 import javax.swing.BoxLayout;
 56  
 import javax.swing.DefaultComboBoxModel;
 57  
 import javax.swing.JButton;
 58  
 import javax.swing.JComboBox;
 59  
 import javax.swing.JFileChooser;
 60  
 import javax.swing.JLabel;
 61  
 import javax.swing.JList;
 62  
 import javax.swing.JOptionPane;
 63  
 import javax.swing.JPanel;
 64  
 import javax.swing.JScrollPane;
 65  
 import javax.swing.MutableComboBoxModel;
 66  
 import javax.swing.filechooser.FileFilter;
 67  
 
 68  
 import org.argouml.i18n.Translator;
 69  
 import org.argouml.kernel.ProfileConfiguration;
 70  
 import org.argouml.kernel.Project;
 71  
 import org.argouml.kernel.ProjectSettings;
 72  
 import org.argouml.profile.Profile;
 73  
 import org.argouml.profile.ProfileException;
 74  
 import org.argouml.profile.ProfileFacade;
 75  
 import org.argouml.profile.UserDefinedProfile;
 76  
 import org.argouml.uml.diagram.DiagramAppearance;
 77  
 import org.argouml.uml.diagram.DiagramSettings;
 78  
 
 79  
 /**
 80  
  * The Tab where new profiles can be added and the registered ones can be
 81  
  * activated or deactivated on current project
 82  
  * 
 83  
  * @author Marcos Aurelio
 84  
  */
 85  900
 public class ProjectSettingsTabProfile extends JPanel implements
 86  
         GUIProjectSettingsTabInterface, ActionListener {
 87  
 
 88  
     private Project p;
 89  
 
 90  
     private JButton loadFromFile;
 91  
 
 92  
     private JButton unregisterProfile;
 93  
 
 94  
     private JButton addButton;
 95  
 
 96  
     private JButton removeButton;
 97  
 
 98  
     private JList availableList;
 99  
 
 100  
     private JList usedList;
 101  
 
 102  
     // //////
 103  
 
 104  900
     private JLabel stereoLabel = new JLabel(Translator
 105  
             .localize("menu.popup.stereotype-view")
 106  
             + ": ");
 107  
 
 108  900
     private JComboBox stereoField = new JComboBox();
 109  
 
 110  
     private JFileChooser fileChooser;
 111  
 
 112  900
     private boolean initialized = false;
 113  
     
 114  
     /**
 115  
      * The default constructor for this class
 116  
      */
 117  900
     public ProjectSettingsTabProfile() {
 118  
         // Defer all work until we're actually needed
 119  900
     }
 120  
 
 121  
     private void buildDialog() {
 122  100
         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
 123  
 
 124  
         // ////////////
 125  
 
 126  100
         JPanel setDefStereoV = new JPanel();
 127  100
         setDefStereoV.setLayout(new FlowLayout());
 128  
 
 129  100
         stereoLabel.setLabelFor(stereoField);
 130  100
         setDefStereoV.add(stereoLabel);
 131  100
         setDefStereoV.add(stereoField);
 132  
 
 133  100
         DefaultComboBoxModel cmodel = new DefaultComboBoxModel();
 134  100
         stereoField.setModel(cmodel);
 135  
 
 136  100
         cmodel.addElement(Translator
 137  
                 .localize("menu.popup.stereotype-view.textual"));
 138  100
         cmodel.addElement(Translator
 139  
                 .localize("menu.popup.stereotype-view.big-icon"));
 140  100
         cmodel.addElement(Translator
 141  
                 .localize("menu.popup.stereotype-view.small-icon"));
 142  
 
 143  100
         stereoField.addItemListener(new ItemListener() {
 144  
 
 145  
             public void itemStateChanged(ItemEvent e) {
 146  0
                 if (p == null) {
 147  0
                     return;
 148  
                 }
 149  0
                 ProjectSettings ps = p.getProjectSettings();
 150  0
                 DiagramSettings ds = ps.getDefaultDiagramSettings();
 151  0
                 Object src = e.getSource();
 152  
 
 153  0
                 if (src == stereoField) {
 154  0
                     Object item = e.getItem();
 155  0
                     DefaultComboBoxModel model = 
 156  
                         (DefaultComboBoxModel) stereoField.getModel();
 157  0
                     int idx = model.getIndexOf(item);
 158  
 
 159  0
                     switch (idx) {
 160  
                     case 0:
 161  0
                         ds.setDefaultStereotypeView(
 162  
                                 DiagramAppearance.STEREOTYPE_VIEW_TEXTUAL);
 163  0
                         break;
 164  
                     case 1:
 165  0
                         ds.setDefaultStereotypeView(
 166  
                                 DiagramAppearance.STEREOTYPE_VIEW_BIG_ICON);
 167  0
                         break;
 168  
                     case 2:
 169  0
                         ds.setDefaultStereotypeView(
 170  
                                 DiagramAppearance.STEREOTYPE_VIEW_SMALL_ICON);
 171  
                         break;
 172  
                     }
 173  
                 }
 174  0
             }
 175  
 
 176  
         });
 177  
 
 178  100
         add(setDefStereoV);
 179  
 
 180  
         // //////////
 181  
 
 182  100
         JPanel configPanel = new JPanel();
 183  100
         configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS));
 184  
 
 185  100
         availableList = createProfileList();
 186  100
         usedList = createProfileList();
 187  
 
 188  100
         JPanel leftList = new JPanel();
 189  100
         leftList.setLayout(new BorderLayout());
 190  100
         leftList.add(new JLabel(Translator
 191  
                 .localize("tab.profiles.userdefined.available")),
 192  
                 BorderLayout.NORTH);
 193  100
         leftList.add(new JScrollPane(availableList), BorderLayout.CENTER);
 194  100
         configPanel.add(leftList);
 195  
 
 196  100
         JPanel centerButtons = new JPanel();
 197  100
         centerButtons.setLayout(new BoxLayout(centerButtons, BoxLayout.Y_AXIS));
 198  
 
 199  100
         addButton = new JButton(">>");
 200  100
         centerButtons.add(addButton);
 201  100
         removeButton = new JButton("<<");
 202  100
         centerButtons.add(removeButton);
 203  100
         configPanel.add(centerButtons);
 204  
 
 205  100
         JPanel rightList = new JPanel();
 206  100
         rightList.setLayout(new BorderLayout());
 207  100
         rightList.add(new JLabel(Translator
 208  
                 .localize("tab.profiles.userdefined.active")),
 209  
                 BorderLayout.NORTH);
 210  100
         rightList.add(new JScrollPane(usedList), BorderLayout.CENTER);
 211  100
         configPanel.add(rightList);
 212  
 
 213  
 
 214  100
         addButton.addActionListener(this);
 215  100
         removeButton.addActionListener(this);
 216  
 
 217  100
         add(configPanel);
 218  
 
 219  100
         JPanel lffPanel = new JPanel();
 220  100
         lffPanel.setLayout(new FlowLayout());
 221  
 
 222  100
         loadFromFile = new JButton(Translator
 223  
                 .localize("tab.profiles.userdefined.load"));
 224  100
         loadFromFile.addActionListener(this);
 225  100
         lffPanel.add(loadFromFile);
 226  
 
 227  100
         unregisterProfile = new JButton(Translator
 228  
                 .localize("tab.profiles.userdefined.unload"));
 229  100
         unregisterProfile.addActionListener(this);
 230  100
         lffPanel.add(unregisterProfile);
 231  
 
 232  100
         add(lffPanel);
 233  
         
 234  100
         initialized = true;
 235  100
     }
 236  
     
 237  
    
 238  
     @Override
 239  
     public void setVisible(boolean flag) {
 240  149
         if (flag && !initialized) {
 241  0
             buildDialog();
 242  
         }
 243  149
         super.setVisible(flag);
 244  149
     }
 245  
     
 246  
     private JList createProfileList() {
 247  200
         JList list = new JList();
 248  200
         list.setMinimumSize(new Dimension(50, 50));
 249  
         // TODO: Add double click listener 
 250  200
         return list;
 251  
     }
 252  
     private void refreshLists() {
 253  106
         availableList.setModel(new DefaultComboBoxModel(getAvailableProfiles()
 254  
                 .toArray()));
 255  106
         usedList.setModel(
 256  
                 new DefaultComboBoxModel(getUsedProfiles().toArray()));
 257  106
     }
 258  
 
 259  
     private List<Profile> getUsedProfiles() {
 260  212
         return new ArrayList<Profile>(
 261  
                 p.getProfileConfiguration().getProfiles());
 262  
     }
 263  
 
 264  
     private List<Profile> getAvailableProfiles() {
 265  106
         List<Profile> used = getUsedProfiles();
 266  106
         List<Profile> ret = new ArrayList<Profile>();
 267  
 
 268  106
         for (Profile profile : ProfileFacade.getManager()
 269  
                 .getRegisteredProfiles()) {
 270  424
             if (!used.contains(profile)) {
 271  106
                 ret.add(profile);
 272  
             }
 273  
         }
 274  
 
 275  106
         return ret;
 276  
     }
 277  
 
 278  
     /*
 279  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 280  
      */
 281  
     public void actionPerformed(ActionEvent arg0) {
 282  38
         MutableComboBoxModel modelAvailable = 
 283  
             ((MutableComboBoxModel) availableList.getModel());
 284  38
         MutableComboBoxModel modelUsed = 
 285  
             ((MutableComboBoxModel) usedList.getModel());
 286  
 
 287  38
         if (arg0.getSource() == addButton) {
 288  0
             Object[] selections = availableList.getSelectedValues();
 289  0
             for (Object s : selections) {
 290  0
                 Profile selected = (Profile) s;
 291  0
                 modelUsed.addElement(selected);
 292  0
                 modelAvailable.removeElement(selected);
 293  
 
 294  0
                 for (Profile profile : getAvailableDependents(selected)) {
 295  0
                     modelUsed.addElement(profile);
 296  0
                     modelAvailable.removeElement(profile);
 297  
                 }
 298  
             }
 299  0
         } else if (arg0.getSource() == removeButton) {
 300  0
             Object[] selections = usedList.getSelectedValues();
 301  0
             for (Object s : selections) {
 302  0
                 Profile selected = (Profile) s;
 303  0
                 Collection<Profile> dependents = getActiveDependents(selected);
 304  0
                 boolean remove = true;
 305  
 
 306  0
                 if (!dependents.isEmpty()) {
 307  0
                     String message = Translator.localize(
 308  
                             "tab.profiles.confirmdeletewithdependencies",
 309  
                             new Object[] {dependents});
 310  0
                     String title = Translator.localize(
 311  
                             "tab.profiles.confirmdeletewithdependencies.title");
 312  0
                     remove = (JOptionPane.showConfirmDialog(
 313  
                             this, message, title, JOptionPane.YES_NO_OPTION) 
 314  
                             == JOptionPane.YES_OPTION);
 315  
                 }
 316  
 
 317  0
                 if (remove) {
 318  0
                     if (!ProfileFacade.getManager().getRegisteredProfiles()
 319  
                             .contains(selected)
 320  
                             && !ProfileFacade.getManager().getDefaultProfiles()
 321  
                                     .contains(selected)) {
 322  0
                         remove = (JOptionPane
 323  
                                 .showConfirmDialog(
 324  
                                         this,
 325  
                                         Translator.localize(
 326  
                                  "tab.profiles.confirmdeleteunregistered"),
 327  
                                         Translator.localize(
 328  
                                 "tab.profiles.confirmdeleteunregistered.title"),
 329  
                                         JOptionPane.YES_NO_OPTION) 
 330  
                                         == JOptionPane.YES_OPTION);
 331  
                     }
 332  
 
 333  0
                     if (remove) {
 334  0
                         modelUsed.removeElement(selected);
 335  0
                         modelAvailable.addElement(selected);
 336  
 
 337  0
                         for (Profile profile : dependents) {
 338  0
                             modelUsed.removeElement(profile);
 339  0
                             modelAvailable.addElement(profile);
 340  
                         }
 341  
                     }
 342  
                 }
 343  
             }
 344  0
         } else if (arg0.getSource() == unregisterProfile) {
 345  8
             Object[] selections = availableList.getSelectedValues();
 346  8
             for (Object s : selections) {
 347  0
                 Profile selected = (Profile) s;
 348  0
                 if (selected instanceof UserDefinedProfile) {
 349  0
                     ProfileFacade.getManager().removeProfile(selected);
 350  0
                     modelAvailable.removeElement(selected);
 351  
                 } else {
 352  0
                     JOptionPane.showMessageDialog(this, Translator
 353  
                             .localize("tab.profiles.cannotdelete"));
 354  
                 }
 355  
             }
 356  8
         } else if (arg0.getSource() == loadFromFile) {
 357  30
             JFileChooser chooser = getFileChooser();
 358  
 
 359  30
             int ret = chooser.showOpenDialog(this);
 360  0
             if (ret == JFileChooser.APPROVE_OPTION) {
 361  0
                 File file = chooser.getSelectedFile();
 362  0
                 chooser.setCurrentDirectory(file.getParentFile());
 363  
                 try {
 364  0
                     UserDefinedProfile profile = new UserDefinedProfile(file,
 365  
                         ProfileFacade.getManager());
 366  0
                     ProfileFacade.getManager().registerProfile(profile);
 367  
 
 368  0
                     modelAvailable.addElement(profile);
 369  0
                 } catch (ProfileException e) {
 370  0
                     JOptionPane.showMessageDialog(this, Translator
 371  
                             .localize("tab.profiles.userdefined.errorloading"));
 372  0
                 }
 373  
             }
 374  
         }
 375  
 
 376  8
         availableList.validate();
 377  8
         usedList.validate();
 378  8
     }
 379  
 
 380  
     private JFileChooser getFileChooser() {
 381  30
         if (fileChooser == null) {
 382  30
             fileChooser = new JFileChooser();
 383  30
             fileChooser.setFileFilter(new ProfileFileFilter());
 384  
         }
 385  30
         return fileChooser;
 386  
     }
 387  
 
 388  
     private List<Profile> getAvailableDependents(Profile selected) {
 389  0
         MutableComboBoxModel modelAvl = ((MutableComboBoxModel) availableList
 390  
                 .getModel());
 391  
 
 392  0
         List<Profile> ret = new ArrayList<Profile>();
 393  0
         for (int i = 0; i < modelAvl.getSize(); ++i) {
 394  0
             Profile profile = (Profile) modelAvl.getElementAt(i);
 395  
 
 396  0
             if (!profile.equals(selected) 
 397  
                     && selected.getDependencies().contains(profile)) {
 398  0
                 ret.add(profile);
 399  
             }
 400  
         }
 401  
 
 402  0
         return ret;
 403  
     }
 404  
 
 405  
     private List<Profile> getActiveDependents(Profile selected) {
 406  0
         MutableComboBoxModel modelUsd = ((MutableComboBoxModel) usedList
 407  
                 .getModel());
 408  
 
 409  0
         List<Profile> ret = new ArrayList<Profile>();
 410  0
         for (int i = 0; i < modelUsd.getSize(); ++i) {
 411  0
             Profile profile = (Profile) modelUsd.getElementAt(i);
 412  
 
 413  0
             if (!profile.equals(selected) 
 414  
                     && profile.getDependencies().contains(selected)) {
 415  0
                 ret.add(profile);
 416  
             }
 417  
         }
 418  
 
 419  0
         return ret;
 420  
     }
 421  
 
 422  
     /**
 423  
      * @return the internationalization key that containing the name of this tab
 424  
      * @see org.argouml.application.api.GUISettingsTabInterface#getTabKey()
 425  
      */
 426  
     public String getTabKey() {
 427  100
         return "tab.profiles";
 428  
     }
 429  
 
 430  
     /**
 431  
      * @return the panel containing this tab
 432  
      * @see org.argouml.application.api.GUISettingsTabInterface#getTabPanel()
 433  
      */
 434  
     public JPanel getTabPanel() {
 435  100
         if (!initialized) {
 436  100
             buildDialog();
 437  
         }
 438  100
         return this;
 439  
     }
 440  
 
 441  
     public void handleResetToDefault() {
 442  6
         if (!initialized) {
 443  0
             buildDialog();
 444  
         }
 445  6
         refreshLists();
 446  6
     }
 447  
 
 448  
     public void handleSettingsTabCancel() {
 449  
 
 450  6
     }
 451  
 
 452  
     public void handleSettingsTabRefresh() {
 453  100
         if (!initialized) {
 454  0
             buildDialog();
 455  
         }
 456  100
         assert p != null;
 457  100
         ProjectSettings ps = p.getProjectSettings();
 458  100
         DiagramSettings ds = ps.getDefaultDiagramSettings();
 459  
         
 460  100
         switch (ds.getDefaultStereotypeViewInt()) {
 461  
         case DiagramAppearance.STEREOTYPE_VIEW_TEXTUAL:
 462  100
             stereoField.setSelectedIndex(0);
 463  100
             break;
 464  
         case DiagramAppearance.STEREOTYPE_VIEW_BIG_ICON:
 465  0
             stereoField.setSelectedIndex(1);
 466  0
             break;
 467  
         case DiagramAppearance.STEREOTYPE_VIEW_SMALL_ICON:
 468  0
             stereoField.setSelectedIndex(2);
 469  
             break;
 470  
         }
 471  
 
 472  100
         refreshLists();
 473  100
     }
 474  
 
 475  
     public void handleSettingsTabSave() {
 476  12
         if (!initialized) {
 477  0
             return;
 478  
         }
 479  12
         assert p != null;
 480  12
         List<Profile> toRemove = new ArrayList<Profile>();
 481  12
         ProfileConfiguration pc = p.getProfileConfiguration();
 482  12
         Object m = p.getUserDefinedModelList().get(0);
 483  
 
 484  12
         List<Profile> usedItens = new ArrayList<Profile>();
 485  
 
 486  12
         MutableComboBoxModel modelUsd = ((MutableComboBoxModel) usedList
 487  
                 .getModel());
 488  
 
 489  48
         for (int i = 0; i < modelUsd.getSize(); ++i) {
 490  36
             usedItens.add((Profile) modelUsd.getElementAt(i));
 491  
         }
 492  
 
 493  12
         for (Profile profile : pc.getProfiles()) {
 494  36
             if (!usedItens.contains(profile)) {
 495  0
                 toRemove.add(profile);
 496  
             }
 497  
         }
 498  
 
 499  12
         for (Profile profile : toRemove) {
 500  0
             pc.removeProfile(profile, m);
 501  
         }
 502  
 
 503  12
         for (Profile profile : usedItens) {
 504  36
             if (!pc.getProfiles().contains(profile)) {
 505  0
                 pc.addProfile(profile, m);
 506  
             }
 507  
         }
 508  
 
 509  12
         p.setProfileConfiguration(pc);
 510  12
     }
 511  
 
 512  
     public void setProject(Project project) {
 513  100
         assert project != null;
 514  100
         p = project;
 515  100
     }
 516  
     
 517  60
     private class ProfileFileFilter extends FileFilter {
 518  
         public boolean accept(File file) {
 519  324
             if (file.isDirectory()) {
 520  252
                 return true;
 521  
             }
 522  72
             if (file.isFile()) {
 523  72
                 String filename = file.getName().toLowerCase();
 524  72
                 String[] validEndings = {".xmi", ".xml", ".xmi.zip", 
 525  
                                          ".xml.zip"};
 526  360
                 for (String ending : validEndings) {
 527  288
                     if (filename.endsWith(ending)) {
 528  0
                         return true;
 529  
                     }
 530  
                 }
 531  
             }
 532  72
             return false;
 533  
         }
 534  
 
 535  
         public String getDescription() {
 536  90
             return "*.xmi *.xml *.xmi.zip *.xml.zip";
 537  
         }
 538  
     }
 539  
 }