Skip to content

Commit 732ca99

Browse files
author
Michael P Schroeder
committed
closeTool
1 parent 2682603 commit 732ca99

File tree

8 files changed

+212
-35
lines changed

8 files changed

+212
-35
lines changed

org.gitools.ui.app/src/main/java/org/gitools/ui/app/batch/ToolFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class ToolFactory {
3535
addTool(new AddHeaderTextLabelsTool());
3636
addTool(new AddHeaderColoredLabelsTool());
3737
addTool(new SortByAnnotationTool());
38-
addTool(new HelpTool(TOOLS.keySet()));
38+
addTool(new CloseAndSaveHeatmapTool());
39+
addTool(new HelpTool(TOOLS.keySet())); //Last!
3940
}
4041

4142
static void addTool(ITool tool) {

org.gitools.ui.app/src/main/java/org/gitools/ui/app/batch/tools/AbstractTool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ void execute() {
9191
JobThread.execute(mainFrame, (JobRunnable) job);
9292
setExitStatus(job.getExitStatus());
9393

94+
} else {
95+
throw new RuntimeException("No Job");
9496
}
9597
}
9698

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* gitools-ui-app
4+
* %%
5+
* Copyright (C) 2013 Universitat Pompeu Fabra - Biomedical Genomics group
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
20+
* #L%
21+
*/
22+
package org.gitools.ui.app.batch.tools;
23+
24+
import org.gitools.ui.app.commands.CloseAndSaveCommand;
25+
import org.gitools.ui.app.commands.Command;
26+
import org.kohsuke.args4j.Option;
27+
28+
import java.io.File;
29+
30+
public class CloseAndSaveHeatmapTool extends HeatmapTool {
31+
32+
33+
@Option(name = "-s", aliases = "--save", required = false,
34+
usage = "Save current state as heatmap")
35+
protected boolean save;
36+
37+
@Option(name = "-a", aliases = "--as", metaVar = "<FILE_NAME>", required = false,
38+
usage = "path and filename indicating where to save")
39+
protected String saveAsFilename;
40+
41+
@Option(name = "-o", aliases = "--optimize", required = false,
42+
usage = "Optimize data file (slower saving process)")
43+
protected boolean optimize = true;
44+
45+
@Option(name = "-d", aliases = "--discard-hidden", required = false,
46+
usage = "Discard hidden data")
47+
protected boolean discardHidden = false;
48+
49+
public CloseAndSaveHeatmapTool() {
50+
super();
51+
}
52+
53+
54+
@Override
55+
public String getName() {
56+
return "close";
57+
}
58+
59+
@Override
60+
protected Command newJob() {
61+
System.out.println("yeees");
62+
return new CloseAndSaveCommand(save,
63+
(saveAsFilename != null) ? new File(saveAsFilename) : null,
64+
optimize, discardHidden, heatmap);
65+
}
66+
67+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* #%L
3+
* org.gitools.ui.app
4+
* %%
5+
* Copyright (C) 2013 - 2014 Universitat Pompeu Fabra - Biomedical Genomics group
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
20+
* #L%
21+
*/
22+
package org.gitools.ui.app.commands;
23+
24+
import org.gitools.api.analysis.IProgressMonitor;
25+
import org.gitools.ui.app.heatmap.editor.HeatmapEditor;
26+
import org.gitools.ui.core.Application;
27+
import org.gitools.ui.platform.progress.JobRunnable;
28+
import org.gitools.ui.platform.progress.JobThread;
29+
30+
import java.io.File;
31+
32+
33+
public class CloseAndSaveCommand extends HeatmapCommand {
34+
35+
private final boolean save;
36+
private final File saveAsFile;
37+
private final boolean optimize;
38+
private final boolean discardHidden;
39+
40+
/**
41+
* @param save true or false
42+
* @param saveAsFile optional
43+
* @param optimize true or false
44+
* @param discardHidden true or false
45+
* @param heatmapName optional
46+
*/
47+
public CloseAndSaveCommand(boolean save, File saveAsFile, boolean optimize, boolean discardHidden, String heatmapName) {
48+
super(heatmapName);
49+
this.save = save;
50+
this.saveAsFile = saveAsFile;
51+
this.optimize = optimize;
52+
this.discardHidden = discardHidden;
53+
}
54+
55+
@Override
56+
public void execute(IProgressMonitor monitor) throws CommandException {
57+
super.execute(monitor);
58+
59+
if (Application.get().getEditorsPanel().getSelectedEditor() instanceof HeatmapEditor) {
60+
final HeatmapEditor currentEditor = (HeatmapEditor) Application.get().getEditorsPanel().getSelectedEditor();
61+
62+
JobThread.execute(Application.get(), new JobRunnable() {
63+
@Override
64+
public void run(IProgressMonitor monitor) {
65+
66+
if (saveAsFile != null) {
67+
currentEditor.doSaveAsNoUI(monitor, saveAsFile, discardHidden, optimize, false);
68+
} else if (save) {
69+
currentEditor.doSave(heatmap.getLocator(), monitor, false);
70+
}
71+
72+
if (currentEditor.doClose(false)) {
73+
Application.get().getEditorsPanel().removeEditorNoUI(currentEditor);
74+
}
75+
76+
}
77+
});
78+
79+
}
80+
}
81+
82+
}

org.gitools.ui.app/src/main/java/org/gitools/ui/app/commands/HeaderCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public HeaderCommand(String heatmap, String side, String sort, String pattern) {
5656
@Override
5757
public void execute(IProgressMonitor monitor) throws CommandException {
5858

59-
try {
6059

61-
findHeatmap(monitor);
60+
try {
61+
super.execute(monitor);
6262

6363
switch (side) {
6464
case ROWS:

org.gitools.ui.app/src/main/java/org/gitools/ui/app/commands/HeatmapCommand.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public HeatmapCommand(String heatmap) {
3838
this.heatmapid = heatmap;
3939
}
4040

41-
protected void findHeatmap(IProgressMonitor monitor) throws Exception {
41+
@Override
42+
public void execute(IProgressMonitor monitor) throws CommandException {
43+
findHeatmap(monitor);
44+
}
45+
46+
protected void findHeatmap(IProgressMonitor monitor) throws CommandException {
4247

4348
Application appframe = Application.get();
4449

@@ -48,7 +53,7 @@ protected void findHeatmap(IProgressMonitor monitor) throws Exception {
4853
heatmap = ((HeatmapEditor) editor).getModel();
4954
return;
5055
} else {
51-
throw new Exception("No selected or open. Indicate name or open and select heatmap");
56+
throw new CommandException("No heatmap selected or open. Indicate name or open and select a heatmap");
5257
}
5358
}
5459

@@ -73,7 +78,7 @@ protected void findHeatmap(IProgressMonitor monitor) throws Exception {
7378
}
7479

7580
if (heatmap == null) {
76-
throw new Exception("No such heatmap loaded: " + heatmapid + availableHeatmaps);
81+
throw new CommandException("No such heatmap loaded: " + heatmapid + availableHeatmaps);
7782
}
7883
}
7984
}

org.gitools.ui.app/src/main/java/org/gitools/ui/app/heatmap/editor/HeatmapEditor.java

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ public void doSaveAs(IProgressMonitor monitor) {
223223
Settings.get().setLastPath(file.getParent());
224224
}
225225

226-
heatmap.setGitoolsVersion(Application.getGitoolsVersion());
227-
228226
String name = getName();
229227
int heatmapExt = name.indexOf(".heatmap");
230228
if (heatmapExt != -1) {
@@ -268,9 +266,19 @@ public void run() {
268266
return;
269267
}
270268

271-
Settings.get().setLastPath(wiz.getFolder());
272269
file = wiz.getPathAsFile();
270+
boolean discardHidden = page.isDiscardHidden();
271+
boolean optimizeData = page.isOptimizeData();
272+
273+
doSaveAsNoUI(monitor, file, discardHidden, optimizeData, true);
274+
275+
}
276+
277+
public void doSaveAsNoUI(IProgressMonitor monitor, File file, boolean discardHidden, boolean optimizeData, boolean reload) {
278+
273279
setFile(file);
280+
heatmap.setGitoolsVersion(Application.getGitoolsVersion());
281+
274282

275283
IResourceLocator toLocator;
276284
if (heatmap.getLocator() == null) {
@@ -282,7 +290,7 @@ public void run() {
282290
// Data matrix
283291
IMatrix data = heatmap.getData().get();
284292

285-
if (page.isDiscardHidden()) {
293+
if (discardHidden) {
286294

287295
// Discard hidden data
288296
data = new MatrixWrapper(data) {
@@ -304,43 +312,46 @@ public boolean isChanged() {
304312

305313
heatmap.setData(new ResourceReference<>("data", data));
306314

307-
} else if (page.isOptimizeData()) {
315+
} else {
316+
if (optimizeData) {
308317

309-
// Optimize mtabix index
310-
data = new MatrixWrapper(data) {
311-
@Override
312-
public boolean isChanged() {
313-
return true;
314-
}
315-
};
318+
// Optimize mtabix index
319+
data = new MatrixWrapper(data) {
320+
@Override
321+
public boolean isChanged() {
322+
return true;
323+
}
324+
};
316325

317-
heatmap.setData(new ResourceReference<>("data", data));
326+
heatmap.setData(new ResourceReference<>("data", data));
318327

319-
} else {
328+
} else {
320329

321-
IResourceLocator locator = heatmap.getData().getLocator();
322-
heatmap.setData(new ResourceReference<>("data", data));
323-
heatmap.getData().setLocator(locator);
324-
heatmap.getData().setChanged(data.isChanged());
325-
heatmap.getData().setBaseName(toLocator.getBaseName() + "-data");
330+
IResourceLocator locator = heatmap.getData().getLocator();
331+
heatmap.setData(new ResourceReference<>("data", data));
332+
heatmap.getData().setLocator(locator);
333+
heatmap.getData().setChanged(data.isChanged());
334+
heatmap.getData().setBaseName(toLocator.getBaseName() + "-data");
326335

336+
}
327337
}
328338

329339
HeatmapDimension rows = heatmap.getRows();
330340
HeatmapDimension columns = heatmap.getColumns();
331341
rows.setAnnotationsReference(new ResourceReference<>(rows.getId().toString().toLowerCase() + "-annotations", rows.getAnnotations()));
332342
columns.setAnnotationsReference(new ResourceReference<>(columns.getId().toString().toLowerCase() + "-annotations", columns.getAnnotations()));
333343

334-
doSave(toLocator, monitor);
344+
Settings.get().setLastPath(file.getAbsoluteFile().getParent());
335345

346+
doSave(toLocator, monitor, reload);
336347
}
337348

338349
@Override
339350
public void doSave(IProgressMonitor monitor) {
340-
doSave(heatmap.getLocator(), monitor);
351+
doSave(heatmap.getLocator(), monitor, true);
341352
}
342353

343-
private void doSave(IResourceLocator toLocator, IProgressMonitor monitor) {
354+
public void doSave(IResourceLocator toLocator, IProgressMonitor monitor, boolean reload) {
344355

345356
File file = getFile();
346357
if (file == null) {
@@ -373,7 +384,7 @@ private void doSave(IResourceLocator toLocator, IProgressMonitor monitor) {
373384
Settings.get().save();
374385

375386
// Force to reload the data after save
376-
if (!toLocator.equals(heatmap.getLocator()) || heatmap.getData().get().isChanged()) {
387+
if (reload && (!toLocator.equals(heatmap.getLocator()) || heatmap.getData().get().isChanged())) {
377388

378389
monitor.title("Reloading the heatmap...");
379390

@@ -392,7 +403,12 @@ private void doSave(IResourceLocator toLocator, IProgressMonitor monitor) {
392403

393404
@Override
394405
public boolean doClose() {
395-
if (isDirty()) {
406+
return doClose(true);
407+
}
408+
409+
public boolean doClose(boolean UI) {
410+
411+
if (UI && isDirty()) {
396412
int res = JOptionPane.showOptionDialog(Application.get(), "File " + getName() + " is modified.\n" +
397413
"Save changes ?", "Close", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"Cancel", "Discard", "Save"}, "Save");
398414

org.gitools.ui.core/src/main/java/org/gitools/ui/core/components/editor/EditorsPanel.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ public void removeEditor(AbstractEditor editor) {
120120
}
121121

122122
if (editor.doClose()) {
123-
int i = indexOfComponent(editor);
124-
if (i != -1) {
125-
remove(i);
126-
}
123+
removeEditorNoUI(editor);
124+
}
125+
}
127126

128-
refreshActions();
127+
public void removeEditorNoUI(AbstractEditor editor) {
128+
int i = indexOfComponent(editor);
129+
if (i != -1) {
130+
remove(i);
129131
}
132+
133+
refreshActions();
130134
}
131135

132136

0 commit comments

Comments
 (0)