1 |
|
package jalview.gui; |
2 |
|
|
3 |
|
import jalview.bin.Cache; |
4 |
|
import jalview.bin.Jalview; |
5 |
|
import jalview.io.JalviewFileChooser; |
6 |
|
import jalview.io.JalviewFileView; |
7 |
|
import jalview.util.ImageMaker; |
8 |
|
import jalview.util.ImageMaker.TYPE; |
9 |
|
import jalview.util.MessageManager; |
10 |
|
import jalview.util.dialogrunner.RunResponse; |
11 |
|
|
12 |
|
import java.awt.Component; |
13 |
|
import java.awt.Graphics; |
14 |
|
import java.io.File; |
15 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
|
|
17 |
|
import javax.swing.JOptionPane; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
@author |
31 |
|
|
32 |
|
|
|
|
| 0% |
Uncovered Elements: 60 (60) |
Complexity: 17 |
Complexity Density: 0.39 |
|
33 |
|
public class ImageExporter |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
41 |
|
public interface ImageWriterI |
42 |
|
{ |
43 |
|
void exportImage(Graphics g) |
44 |
|
throws Exception; |
45 |
|
} |
46 |
|
|
47 |
|
private IProgressIndicator messageBoard; |
48 |
|
|
49 |
|
private ImageWriterI imageWriter; |
50 |
|
|
51 |
|
TYPE imageType; |
52 |
|
|
53 |
|
private String title; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@param |
60 |
|
@param |
61 |
|
@param |
62 |
|
@param |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
64 |
0 |
public ImageExporter(ImageWriterI writer, IProgressIndicator statusBar,... |
65 |
|
TYPE type, String fileTitle) |
66 |
|
{ |
67 |
0 |
this.imageWriter = writer; |
68 |
0 |
this.messageBoard = statusBar; |
69 |
0 |
this.imageType = type; |
70 |
0 |
this.title = fileTitle; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@param |
79 |
|
|
80 |
|
@param |
81 |
|
|
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
|
86 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 9 |
Complexity Density: 0.33 |
|
87 |
0 |
public void doExport(File file, Component parent, int width, int height,... |
88 |
|
String imageSource) |
89 |
|
{ |
90 |
0 |
final long messageId = System.currentTimeMillis(); |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
0 |
if (file == null && !Jalview.isHeadlessMode()) |
96 |
|
{ |
97 |
0 |
JalviewFileChooser chooser = imageType.getFileChooser(); |
98 |
0 |
chooser.setFileView(new JalviewFileView()); |
99 |
0 |
MessageManager.formatMessage("label.create_image_of", |
100 |
|
imageType.getName(), imageSource); |
101 |
0 |
String title = "Create " + imageType.getName() |
102 |
|
+ " image from alignment"; |
103 |
0 |
chooser.setDialogTitle(title); |
104 |
0 |
chooser.setToolTipText(MessageManager.getString("action.save")); |
105 |
0 |
int value = chooser.showSaveDialog(parent); |
106 |
0 |
if (value != JalviewFileChooser.APPROVE_OPTION) |
107 |
|
{ |
108 |
0 |
String msg = MessageManager.formatMessage( |
109 |
|
"status.cancelled_image_export_operation", imageType.name); |
110 |
0 |
setStatus(msg, messageId); |
111 |
0 |
return; |
112 |
|
} |
113 |
0 |
Cache.setProperty("LAST_DIRECTORY", |
114 |
|
chooser.getSelectedFile().getParent()); |
115 |
0 |
file = chooser.getSelectedFile(); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
0 |
String renderStyle = Cache.getDefault( |
124 |
|
imageType.getName() + "_RENDERING", |
125 |
|
LineartOptions.PROMPT_EACH_TIME); |
126 |
0 |
if (Jalview.isJS()) |
127 |
|
{ |
128 |
0 |
renderStyle = "Text"; |
129 |
|
} |
130 |
0 |
AtomicBoolean textSelected = new AtomicBoolean( |
131 |
|
!"Lineart".equals(renderStyle)); |
132 |
0 |
if ((imageType == TYPE.EPS || imageType == TYPE.SVG) |
133 |
|
&& LineartOptions.PROMPT_EACH_TIME.equals(renderStyle) |
134 |
|
&& !Jalview.isHeadlessMode()) |
135 |
|
{ |
136 |
0 |
final File chosenFile = file; |
137 |
0 |
RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) |
138 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
0 |
@Override... |
140 |
|
public void run() |
141 |
|
{ |
142 |
0 |
exportImage(chosenFile, !textSelected.get(), width, height, |
143 |
|
messageId); |
144 |
|
} |
145 |
|
}; |
146 |
0 |
LineartOptions epsOption = new LineartOptions(TYPE.EPS.getName(), |
147 |
|
textSelected); |
148 |
0 |
epsOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION) |
149 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
0 |
@Override... |
151 |
|
public void run() |
152 |
|
{ |
153 |
0 |
setStatus(MessageManager.formatMessage( |
154 |
|
"status.cancelled_image_export_operation", |
155 |
|
imageType.getName()), messageId); |
156 |
|
} |
157 |
|
}); |
158 |
0 |
epsOption.setResponseAction(okAction); |
159 |
0 |
epsOption.showDialog(); |
160 |
|
|
161 |
|
} |
162 |
|
else |
163 |
|
{ |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
0 |
exportImage(file, !textSelected.get(), width, height, messageId); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@param |
178 |
|
@param |
179 |
|
@param |
180 |
|
@param |
181 |
|
@param |
182 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
183 |
0 |
protected void exportImage(File chosenFile, boolean asLineart, int width,... |
184 |
|
int height, long messageId) |
185 |
|
{ |
186 |
0 |
String type = imageType.getName(); |
187 |
0 |
try |
188 |
|
{ |
189 |
0 |
setStatus( |
190 |
|
MessageManager.formatMessage( |
191 |
|
"status.exporting_alignment_as_x_file", type), |
192 |
|
messageId); |
193 |
0 |
ImageMaker im = new ImageMaker(imageType, width, height, chosenFile, |
194 |
|
title, asLineart); |
195 |
0 |
imageWriter.exportImage(im.getGraphics()); |
196 |
0 |
im.writeImage(); |
197 |
0 |
setStatus( |
198 |
|
MessageManager.formatMessage("status.export_complete", type), |
199 |
|
messageId); |
200 |
|
} catch (Exception e) |
201 |
|
{ |
202 |
0 |
System.out |
203 |
|
.println(String.format("Error creating %s file: %s", type, |
204 |
|
e.toString())); |
205 |
0 |
setStatus(MessageManager.formatMessage("info.error_creating_file", |
206 |
|
type), messageId); |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
@param |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
216 |
0 |
void setStatus(String msg, long id)... |
217 |
|
{ |
218 |
0 |
if (messageBoard != null && !Jalview.isHeadlessMode()) |
219 |
|
{ |
220 |
0 |
messageBoard.setProgressBar(msg, id); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
|
} |