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 | |
package org.argouml.ui.explorer; |
40 | |
|
41 | |
import java.awt.event.ActionEvent; |
42 | |
import java.io.File; |
43 | |
import java.io.FileOutputStream; |
44 | |
import java.io.IOException; |
45 | |
import java.io.OutputStream; |
46 | |
import java.util.Collection; |
47 | |
|
48 | |
import javax.swing.AbstractAction; |
49 | |
import javax.swing.JFileChooser; |
50 | |
import javax.swing.filechooser.FileFilter; |
51 | |
|
52 | |
import org.apache.log4j.Logger; |
53 | |
import org.argouml.application.helpers.ApplicationVersion; |
54 | |
import org.argouml.configuration.Configuration; |
55 | |
import org.argouml.i18n.Translator; |
56 | |
import org.argouml.model.Model; |
57 | |
import org.argouml.model.UmlException; |
58 | |
import org.argouml.model.XmiWriter; |
59 | |
import org.argouml.persistence.PersistenceManager; |
60 | |
import org.argouml.persistence.ProjectFileView; |
61 | |
import org.argouml.persistence.UmlFilePersister; |
62 | |
import org.argouml.profile.Profile; |
63 | |
import org.argouml.profile.ProfileException; |
64 | |
import org.argouml.util.ArgoFrame; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | public class ActionExportProfileXMI extends AbstractAction { |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | private static final Logger LOG = Logger |
77 | |
.getLogger(ActionExportProfileXMI.class); |
78 | |
|
79 | |
private Profile selectedProfile; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public ActionExportProfileXMI(Profile profile) { |
87 | 0 | super(Translator.localize("action.export-profile-as-xmi")); |
88 | 0 | this.selectedProfile = profile; |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
public void actionPerformed(ActionEvent arg0) { |
93 | |
try { |
94 | 0 | final Collection profilePackages = |
95 | |
selectedProfile.getProfilePackages(); |
96 | 0 | final Object model = profilePackages.iterator().next(); |
97 | |
|
98 | 0 | if (model != null) { |
99 | 0 | File destiny = getTargetFile(); |
100 | 0 | if (destiny != null) { |
101 | 0 | saveModel(destiny, model); |
102 | |
} |
103 | |
} |
104 | 0 | } catch (ProfileException e) { |
105 | |
|
106 | 0 | LOG.error("Exception", e); |
107 | 0 | } catch (IOException e) { |
108 | 0 | LOG.error("Exception", e); |
109 | 0 | } catch (UmlException e) { |
110 | 0 | LOG.error("Exception", e); |
111 | 0 | } |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
private void saveModel(File destiny, Object model) throws IOException, |
116 | |
UmlException { |
117 | 0 | OutputStream stream = new FileOutputStream(destiny); |
118 | 0 | XmiWriter xmiWriter = |
119 | |
Model.getXmiWriter(model, stream, |
120 | |
ApplicationVersion.getVersion() + "(" |
121 | |
+ UmlFilePersister.PERSISTENCE_VERSION + ")"); |
122 | 0 | xmiWriter.write(); |
123 | 0 | } |
124 | |
|
125 | |
private File getTargetFile() { |
126 | |
|
127 | 0 | JFileChooser chooser = new JFileChooser(); |
128 | 0 | chooser.setDialogTitle(Translator.localize( |
129 | |
"action.export-profile-as-xmi")); |
130 | 0 | chooser.setFileView(ProjectFileView.getInstance()); |
131 | 0 | chooser.setApproveButtonText(Translator.localize( |
132 | |
"filechooser.export")); |
133 | 0 | chooser.setAcceptAllFileFilterUsed(true); |
134 | 0 | chooser.setFileFilter(new FileFilter() { |
135 | |
|
136 | |
public boolean accept(File file) { |
137 | 0 | return file.isDirectory() || isXmiFile(file); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
public String getDescription() { |
143 | 0 | return "*.XMI"; |
144 | |
} |
145 | |
|
146 | |
}); |
147 | |
|
148 | 0 | String fn = |
149 | |
Configuration.getString( |
150 | |
PersistenceManager.KEY_PROJECT_NAME_PATH); |
151 | 0 | if (fn.length() > 0) { |
152 | 0 | fn = PersistenceManager.getInstance().getBaseName(fn); |
153 | 0 | chooser.setSelectedFile(new File(fn)); |
154 | |
} |
155 | |
|
156 | 0 | int result = chooser.showSaveDialog(ArgoFrame.getFrame()); |
157 | 0 | if (result == JFileChooser.APPROVE_OPTION) { |
158 | 0 | File theFile = chooser.getSelectedFile(); |
159 | 0 | if (theFile != null) { |
160 | 0 | if (!theFile.getName().toUpperCase().endsWith(".XMI")) { |
161 | 0 | theFile = new File(theFile.getAbsolutePath() + ".XMI"); |
162 | |
} |
163 | 0 | return theFile; |
164 | |
} |
165 | |
} |
166 | |
|
167 | 0 | return null; |
168 | |
} |
169 | |
|
170 | |
private static boolean isXmiFile(File file) { |
171 | 0 | return file.isFile() |
172 | |
&& (file.getName().toLowerCase().endsWith(".xml") |
173 | |
|| file.getName().toLowerCase().endsWith(".xmi")); |
174 | |
} |
175 | |
} |