Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionDeployProfile |
|
| 4.333333333333333;4.333 |
1 | /* $Id: ActionDeployProfile.java 18902 2010-12-08 09:53:33Z thn $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009-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 | * thn | |
11 | * euluis | |
12 | ***************************************************************************** | |
13 | * | |
14 | * Some portions of this file was previously release using the BSD License: | |
15 | */ | |
16 | ||
17 | // Copyright (c) 2009 The Regents of the University of California. All | |
18 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
19 | // software and its documentation without fee, and without a written | |
20 | // agreement is hereby granted, provided that the above copyright notice | |
21 | // and this paragraph appear in all copies. This software program and | |
22 | // documentation are copyrighted by The Regents of the University of | |
23 | // California. The software program and documentation are supplied "AS | |
24 | // IS", without any accompanying services from The Regents. The Regents | |
25 | // does not warrant that the operation of the program will be | |
26 | // uninterrupted or error-free. The end-user understands that the program | |
27 | // was developed for research purposes and is advised not to rely | |
28 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
29 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
30 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
31 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
32 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
33 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
34 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
35 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
36 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
37 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
38 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
39 | ||
40 | package org.argouml.ui.explorer; | |
41 | ||
42 | import java.awt.event.ActionEvent; | |
43 | import java.io.File; | |
44 | import java.util.Collection; | |
45 | ||
46 | import javax.swing.AbstractAction; | |
47 | import javax.swing.JFileChooser; | |
48 | ||
49 | import org.apache.log4j.Logger; | |
50 | import org.argouml.i18n.Translator; | |
51 | import org.argouml.model.Model; | |
52 | import org.argouml.persistence.PersistenceManager; | |
53 | import org.argouml.persistence.ProjectFileView; | |
54 | import org.argouml.profile.ProfileException; | |
55 | import org.argouml.profile.ProfileFacade; | |
56 | import org.argouml.profile.ProfileManager; | |
57 | import org.argouml.profile.UserDefinedProfile; | |
58 | import org.argouml.ui.ProjectBrowser; | |
59 | import org.argouml.util.ArgoFrame; | |
60 | ||
61 | /** | |
62 | * Explorer action for deploying a user editable profile. Deploying means: save | |
63 | * it as an XMI file, immediately load it as a user profile, but don't activate | |
64 | * it (pointless, because a profile cannot be applied to itself), and also don't | |
65 | * change the configuration (so it will only be loaded on every startup when it | |
66 | * is deployed into a default profile directory). | |
67 | * | |
68 | * @author Thomas Neustupny | |
69 | */ | |
70 | public class ActionDeployProfile extends AbstractAction { | |
71 | ||
72 | 0 | private static final Logger LOG = Logger |
73 | .getLogger(ActionDeployProfile.class); | |
74 | ||
75 | private Object undeployedProfile; | |
76 | ||
77 | /** | |
78 | * Default Constructor | |
79 | * | |
80 | * @param profile the selected profile | |
81 | */ | |
82 | public ActionDeployProfile(Object profile) { | |
83 | 0 | super(Translator.localize("action.deploy-profile") + "..."); |
84 | 0 | undeployedProfile = profile; |
85 | 0 | } |
86 | ||
87 | public void actionPerformed(ActionEvent arg0) { | |
88 | // get one of the default profile dirs, if available | |
89 | // (as a default value for the following save dialog) | |
90 | 0 | ProfileManager profileManager = ProfileFacade.getManager(); |
91 | 0 | StringBuffer path = new StringBuffer(); |
92 | 0 | Collection dirs = profileManager.getSearchPathDirectories(); |
93 | 0 | if (dirs != null && !dirs.isEmpty()) { |
94 | 0 | String s = (String) dirs.iterator().next(); |
95 | 0 | path.append(s); |
96 | 0 | if (!(s.endsWith("/") || s.endsWith("\\"))) { |
97 | 0 | path.append('/'); |
98 | } | |
99 | } | |
100 | // save profile | |
101 | 0 | path.append(Model.getFacade().getName(undeployedProfile)); |
102 | 0 | File f = saveProfile(path.toString()); |
103 | 0 | if (f == null) { |
104 | 0 | return; |
105 | } | |
106 | // register it as a user profile | |
107 | try { | |
108 | 0 | profileManager.registerProfile(new UserDefinedProfile(f, |
109 | profileManager)); | |
110 | 0 | } catch (ProfileException e) { |
111 | 0 | LOG.warn("failed to load profile from file " + f.getPath(), e); |
112 | 0 | } |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * Offers the saving of the profile by presenting a file dialog. | |
117 | * | |
118 | * @param fn the suggested default path for the file dialog | |
119 | * @return the File instance where the profile is saved, null on abort | |
120 | */ | |
121 | private File saveProfile(String fn) { | |
122 | 0 | File theFile = null; |
123 | 0 | PersistenceManager pm = PersistenceManager.getInstance(); |
124 | // show a chooser dialog for the file name, only xmi is allowed | |
125 | 0 | JFileChooser chooser = new JFileChooser(); |
126 | 0 | chooser.setDialogTitle(Translator.localize("action.deploy-profile")); |
127 | 0 | chooser.setFileView(ProjectFileView.getInstance()); |
128 | 0 | chooser.setApproveButtonText(Translator.localize("filechooser.export")); |
129 | 0 | chooser.setAcceptAllFileFilterUsed(true); |
130 | 0 | pm.setXmiFileChooserFilter(chooser); |
131 | 0 | if (fn.length() > 0) { |
132 | 0 | fn = PersistenceManager.getInstance().getBaseName(fn); |
133 | 0 | chooser.setSelectedFile(new File(fn)); |
134 | } | |
135 | 0 | int result = chooser.showSaveDialog(ArgoFrame.getFrame()); |
136 | 0 | if (result == JFileChooser.APPROVE_OPTION) { |
137 | 0 | theFile = chooser.getSelectedFile(); |
138 | 0 | if (theFile != null) { |
139 | 0 | Model.getExtensionMechanismsHelper().makeProfileApplicable( |
140 | undeployedProfile); | |
141 | 0 | String name = theFile.getName(); |
142 | 0 | name = pm.fixXmiExtension(name); |
143 | 0 | theFile = new File(theFile.getParent(), name); |
144 | 0 | ProjectBrowser.getInstance().trySaveWithProgressMonitor(true, |
145 | theFile, false); | |
146 | } | |
147 | } | |
148 | 0 | return theFile; |
149 | } | |
150 | } |