Skip to content

Commit

Permalink
Moved static variables from the KEGGtranslator interface to the actua…
Browse files Browse the repository at this point in the history
…l main class "Translator.java".
  • Loading branch information
Clemens82 committed Jul 4, 2011
1 parent a954aa6 commit b6bb490
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 60 deletions.
44 changes: 36 additions & 8 deletions src/de/zbit/kegg/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,39 @@
* @version $Rev$
*/
public class Translator {

/**
* Filename of the KEGG cache file (implemented just
* like the browser cache). Must be loaded upon start
* and saved upon exit.
*/
public final static String cacheFileName = "keggdb.dat";

/**
* Filename of the KEGG function cache file (implemented just
* like the browser cache). Must be loaded upon start
* and saved upon exit.
*/
public final static String cacheFunctionFileName = "keggfc.dat";

/**
* The name of the application.
* Removed the final attribute, such that referencing applications can still change this.
*/
public static String APPLICATION_NAME = "KEGGtranslator";

/**
* Version number of this translator
*/
public static final String VERSION_NUMBER = "1.2.0";


/**
* The cache to be used by all KEGG interacting classes.
* Access via {@link #getManager()}.
*/
private static KeggInfoManagement manager=null;

/**
* The cache to be used by all KEGG-Functions interacting classes.
* Access via {@link #getFunctionManager()}.
Expand Down Expand Up @@ -201,14 +229,14 @@ public static boolean translate(Format format, String input, String output)
public synchronized static KeggInfoManagement getManager() {

// Try to load from cache file
if (manager==null && new File(KEGGtranslator.cacheFileName).exists() && new File(KEGGtranslator.cacheFileName).length() > 0) {
if (manager==null && new File(Translator.cacheFileName).exists() && new File(Translator.cacheFileName).length() > 0) {
try {
manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(KEGGtranslator.cacheFileName);
manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(Translator.cacheFileName);
} catch (Throwable e) { // IOException or class cast, if class is moved.
e.printStackTrace();
// Delete invalid cache file
try {
File f = new File(KEGGtranslator.cacheFileName);
File f = new File(Translator.cacheFileName);
if (f.exists() && f.canRead()) {
System.out.println("Deleting invalid cache file " + f.getName());
f.delete();
Expand All @@ -229,15 +257,15 @@ public synchronized static KeggInfoManagement getManager() {
public synchronized static KeggFunctionManagement getFunctionManager() {

// Try to load from cache file
if (managerFunction==null && new File(KEGGtranslator.cacheFunctionFileName).exists() && new File(KEGGtranslator.cacheFunctionFileName).length() > 0) {
if (managerFunction==null && new File(Translator.cacheFunctionFileName).exists() && new File(Translator.cacheFunctionFileName).length() > 0) {
try {
managerFunction = (KeggFunctionManagement) KeggFunctionManagement.loadFromFilesystem(KEGGtranslator.cacheFunctionFileName);
managerFunction = (KeggFunctionManagement) KeggFunctionManagement.loadFromFilesystem(Translator.cacheFunctionFileName);
} catch (Throwable e) { // IOException or class cast, if class is moved.
e.printStackTrace();

// Delete invalid cache file
try {
File f = new File(KEGGtranslator.cacheFunctionFileName);
File f = new File(Translator.cacheFunctionFileName);
if (f.exists() && f.canRead()) {
System.out.println("Deleting invalid cache file " + f.getName());
f.delete();
Expand All @@ -259,10 +287,10 @@ public synchronized static KeggFunctionManagement getFunctionManager() {
*/
public synchronized static void saveCache() {
if (manager!=null && manager.hasChanged()) {
KeggInfoManagement.saveToFilesystem(KEGGtranslator.cacheFileName, manager);
KeggInfoManagement.saveToFilesystem(Translator.cacheFileName, manager);
}
if (managerFunction!=null && managerFunction.isCacheChangedSinceLastLoading()) {
KeggFunctionManagement.saveToFilesystem(KEGGtranslator.cacheFunctionFileName, managerFunction);
KeggFunctionManagement.saveToFilesystem(Translator.cacheFunctionFileName, managerFunction);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/de/zbit/kegg/ext/RestrictedEditMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
import y.view.Overview;
import de.zbit.gui.GUITools;
import de.zbit.gui.SystemBrowser;
import de.zbit.kegg.Translator;
import de.zbit.kegg.TranslatorTools;
import de.zbit.kegg.io.KEGG2jSBML;
import de.zbit.kegg.io.KEGG2yGraph;
import de.zbit.kegg.io.KEGGtranslator;
import de.zbit.util.EscapeChars;
import de.zbit.util.StringUtil;

Expand Down Expand Up @@ -231,7 +231,7 @@ public void mouseClicked(double x, double y) {
String kgId = TranslatorTools.getKeggIDs(n);
if (kgId.toLowerCase().startsWith("path:")) {
int ret = GUITools.showQuestionMessage(null, "Do you want to download and open the referenced pathway in a new tab?",
KEGGtranslator.APPLICATION_NAME, new String[]{"Yes", "No"});
Translator.APPLICATION_NAME, new String[]{"Yes", "No"});
if (ret==0) {
ActionEvent e = new ActionEvent(kgId.trim().substring(5).toLowerCase(), JOptionPane.OK_OPTION, OPEN_PATHWAY);
aListener.actionPerformed(e);
Expand Down
4 changes: 2 additions & 2 deletions src/de/zbit/kegg/ext/TranslatorPanelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
package de.zbit.kegg.ext;

import de.zbit.kegg.Translator;
import de.zbit.kegg.gui.TranslatorPanel;
import de.zbit.kegg.io.KEGGtranslator;
import de.zbit.util.prefs.KeyProvider;
import de.zbit.util.prefs.Option;
import de.zbit.util.prefs.OptionGroup;
Expand All @@ -39,7 +39,7 @@ public abstract interface TranslatorPanelOptions extends KeyProvider{
* as background image.
*/
public static final Option<Boolean> SHOW_LOGO_IN_GRAPH_BACKGROUND = new Option<Boolean>("SHOW_LOGO_IN_GRAPH_BACKGROUND",Boolean.class,
"If true, shows the " + KEGGtranslator.APPLICATION_NAME + " logo in the background of each graph.", false);
"If true, shows the " + Translator.APPLICATION_NAME + " logo in the background of each graph.", false);

/**
* Shows an overview and navigation panel in every graph frame.
Expand Down
5 changes: 2 additions & 3 deletions src/de/zbit/kegg/gui/TranslatorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import de.zbit.kegg.io.BatchKEGGtranslator;
import de.zbit.kegg.io.KEGG2jSBML;
import de.zbit.kegg.io.KEGG2yGraph;
import de.zbit.kegg.io.KEGGtranslator;
import de.zbit.kegg.io.KEGGtranslatorIOOptions;
import de.zbit.kegg.io.KEGGtranslatorIOOptions.Format;
import de.zbit.util.AbstractProgressBar;
Expand Down Expand Up @@ -431,7 +430,7 @@ private static AbstractProgressBar generateLoadingPanel(Container parent, String
} else {
// Display the panel in an jFrame
JDialog f = new JDialog();
f.setTitle(KEGGtranslator.APPLICATION_NAME);
f.setTitle(Translator.APPLICATION_NAME);
f.setSize(panel.getPreferredSize());
f.setContentPane(panel);
f.setPreferredSize(panel.getPreferredSize());
Expand Down Expand Up @@ -604,7 +603,7 @@ public void updateButtons(JMenuBar menuBar) {
public void writeLaTeXReport(File targetFile) {
if (document==null) return;
if (!isSBML()) {
GUITools.showMessage("This option is only available for SBML documents.", KEGGtranslator.APPLICATION_NAME);
GUITools.showMessage("This option is only available for SBML documents.", Translator.APPLICATION_NAME);
return;
}

Expand Down
13 changes: 6 additions & 7 deletions src/de/zbit/kegg/gui/TranslatorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import de.zbit.gui.prefs.PreferencesPanel;
import de.zbit.kegg.Translator;
import de.zbit.kegg.ext.RestrictedEditMode;
import de.zbit.kegg.io.KEGGtranslator;
import de.zbit.kegg.io.KEGGtranslatorIOOptions;
import de.zbit.kegg.io.KEGGtranslatorIOOptions.Format;
import de.zbit.util.AbstractProgressBar;
Expand Down Expand Up @@ -105,7 +104,7 @@ public static enum Action implements ActionCommand {
/**
* This is coming from {@link RestrictedEditMode#OPEN_PATHWAY} and must be
* renamed accordingly. The source is a kegg pathway id that should be opened
* as new tab, when this action is fired.
* as new tab, when this action is fired. This is an invisible action.
*/
OPEN_PATHWAY,
/**
Expand Down Expand Up @@ -325,15 +324,15 @@ private void createNewTab(File inFile, String format) {
if (inFile!=null) {
message = '\'' + inFile.getName() + "' is no valid input file.";
}
JOptionPane.showMessageDialog(this, message, KEGGtranslator.APPLICATION_NAME, JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(this, message, Translator.APPLICATION_NAME, JOptionPane.WARNING_MESSAGE);
} else {
Format f = null;
try {
f = Format.valueOf(format);
} catch (Throwable exc) {
exc.printStackTrace();
JOptionPane.showMessageDialog(this, '\'' + format + "' is no valid output format.",
KEGGtranslator.APPLICATION_NAME, JOptionPane.WARNING_MESSAGE);
Translator.APPLICATION_NAME, JOptionPane.WARNING_MESSAGE);
}
if (f != null) {
// Tanslate and add tab.
Expand Down Expand Up @@ -476,7 +475,7 @@ public File[] openFile(File... files) {
if ( askOutputFormat || (format == null) || (format.length() < 1)) {
JLabeledComponent outputFormat = (JLabeledComponent) PreferencesPanel.getJComponentForOption(KEGGtranslatorIOOptions.FORMAT, prefsIO, null);
outputFormat.setTitle("Please select the output format");
JOptionPane.showMessageDialog(this, outputFormat, KEGGtranslator.APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(this, outputFormat, Translator.APPLICATION_NAME, JOptionPane.QUESTION_MESSAGE);
format = outputFormat.getSelectedItem().toString();
}

Expand Down Expand Up @@ -718,7 +717,7 @@ public URL getURLOnlineHelp() {
* @see de.zbit.gui.BaseFrame#getApplicationName()
*/
public String getApplicationName() {
return KEGGtranslator.APPLICATION_NAME;
return Translator.APPLICATION_NAME;
}

/*
Expand All @@ -727,7 +726,7 @@ public String getApplicationName() {
* @see de.zbit.gui.BaseFrame#getDottedVersionNumber()
*/
public String getDottedVersionNumber() {
return KEGGtranslator.VERSION_NUMBER;
return Translator.VERSION_NUMBER;
}

/*
Expand Down
15 changes: 8 additions & 7 deletions src/de/zbit/kegg/io/KEGG2jSBML.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import de.zbit.kegg.KEGGtranslatorOptions;
import de.zbit.kegg.KeggInfoManagement;
import de.zbit.kegg.KeggInfos;
import de.zbit.kegg.Translator;
import de.zbit.kegg.io.KEGGtranslatorIOOptions.Format;
import de.zbit.kegg.parser.KeggParser;
import de.zbit.kegg.parser.pathway.Entry;
Expand Down Expand Up @@ -315,7 +316,7 @@ public boolean writeToFile(SBMLDocument doc, String outFile) {
if (new File(outFile).exists()) lastFileWasOverwritten=true;
try {
SBMLWriter writer = new SBMLWriter();
writer.write(doc, outFile, KEGGtranslator.APPLICATION_NAME, KEGGtranslator.VERSION_NUMBER);
writer.write(doc, outFile, Translator.APPLICATION_NAME, Translator.VERSION_NUMBER);
} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down Expand Up @@ -434,7 +435,7 @@ protected SBMLDocument translateWithoutPreprocessing(Pathway p) {
notes.append("</p>");
}
notes.append(String.format("<div align=\"right\"><i><small>This file has been generated by %s version %s</small></i></div><br/>\n",
KEGGtranslator.APPLICATION_NAME, VERSION_NUMBER));
Translator.APPLICATION_NAME, Translator.VERSION_NUMBER));

// Save all reaction modifiers in a list. String = reaction id.
SortedArrayList<Info<String, ModifierSpeciesReference>> reactionModifiers = new SortedArrayList<Info<String, ModifierSpeciesReference>>();
Expand Down Expand Up @@ -1177,9 +1178,9 @@ public static void main(String[] args) throws IOException {
// Speedup Kegg2SBML by loading alredy queried objects. Reduces network
// load and heavily reduces computation time.
AbstractKEGGtranslator<SBMLDocument> k2s;
if (new File(KEGGtranslator.cacheFileName).exists()
&& new File(KEGGtranslator.cacheFileName).length() > 0) {
KeggInfoManagement manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(KEGGtranslator.cacheFileName);
if (new File(Translator.cacheFileName).exists()
&& new File(Translator.cacheFileName).length() > 0) {
KeggInfoManagement manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(Translator.cacheFileName);
k2s = new KEGG2jSBML(manager);
} else {
k2s = new KEGG2jSBML();
Expand Down Expand Up @@ -1211,7 +1212,7 @@ && new File(KEGGtranslator.cacheFileName).length() > 0) {

// Remember already queried objects (save cache)
if (k2s.getKeggInfoManager().hasChanged()) {
KeggInfoManagement.saveToFilesystem(KEGGtranslator.cacheFileName, k2s.getKeggInfoManager());
KeggInfoManagement.saveToFilesystem(Translator.cacheFileName, k2s.getKeggInfoManager());
}

return;
Expand All @@ -1228,7 +1229,7 @@ && new File(KEGGtranslator.cacheFileName).length() > 0) {

// Remember already queried objects
if (k2s.getKeggInfoManager().hasChanged()) {
KeggInfoManagement.saveToFilesystem(KEGGtranslator.cacheFileName, k2s.getKeggInfoManager());
KeggInfoManagement.saveToFilesystem(Translator.cacheFileName, k2s.getKeggInfoManager());
}

} catch (Exception e) {
Expand Down
13 changes: 7 additions & 6 deletions src/de/zbit/kegg/io/KEGG2yGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
import y.view.ShapeNodeRealizer;
import y.view.hierarchy.GroupNodeRealizer;
import y.view.hierarchy.HierarchyManager;
import de.zbit.kegg.KEGGtranslatorOptions;
import de.zbit.kegg.KeggInfoManagement;
import de.zbit.kegg.KeggInfos;
import de.zbit.kegg.KEGGtranslatorOptions;
import de.zbit.kegg.Translator;
import de.zbit.kegg.ext.GenericDataMap;
import de.zbit.kegg.io.KEGGtranslatorIOOptions.Format;
import de.zbit.kegg.parser.KeggParser;
Expand Down Expand Up @@ -1202,9 +1203,9 @@ public static KEGG2yGraph createKEGG2YGF(KeggInfoManagement manager) {
*/
public static void main(String[] args) throws IOException {
KeggInfoManagement manager;
if (new File(KEGGtranslator.cacheFileName).exists()
&& new File(KEGGtranslator.cacheFileName).length() > 0) {
manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(KEGGtranslator.cacheFileName);
if (new File(Translator.cacheFileName).exists()
&& new File(Translator.cacheFileName).length() > 0) {
manager = (KeggInfoManagement) KeggInfoManagement.loadFromFilesystem(Translator.cacheFileName);
} else {
manager = new KeggInfoManagement();
}
Expand Down Expand Up @@ -1236,7 +1237,7 @@ && new File(KEGGtranslator.cacheFileName).length() > 0) {

// Remember already queried objects (save cache)
if (k2g.getKeggInfoManager().hasChanged()) {
KeggInfoManagement.saveToFilesystem(KEGGtranslator.cacheFileName, k2g.getKeggInfoManager());
KeggInfoManagement.saveToFilesystem(Translator.cacheFileName, k2g.getKeggInfoManager());
}

return;
Expand All @@ -1253,7 +1254,7 @@ && new File(KEGGtranslator.cacheFileName).length() > 0) {

// Remember already queried objects
if (k2g.getKeggInfoManager().hasChanged()) {
KeggInfoManagement.saveToFilesystem(KEGGtranslator.cacheFileName, k2g.getKeggInfoManager());
KeggInfoManagement.saveToFilesystem(Translator.cacheFileName, k2g.getKeggInfoManager());
}

} catch (Exception e) {
Expand Down
23 changes: 0 additions & 23 deletions src/de/zbit/kegg/io/KEGGtranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@
*/
public interface KEGGtranslator {

/**
* The name of the application.
* Removed the final attribute, such that referencing applications can still change this.
*/
public static String APPLICATION_NAME = "KEGGtranslator";

/**
* Version number of this translator
*/
public static final String VERSION_NUMBER = "1.2.0";

/**
* Filename of the KEGG cache file (implemented just
* like the browser cache). Must be loaded upon start
* and saved upon exit.
*/
public static String cacheFileName = "keggdb.dat";
/**
* Filename of the KEGG function cache file (implemented just
* like the browser cache). Must be loaded upon start
* and saved upon exit.
*/
public static String cacheFunctionFileName = "keggfc.dat";

/**
* Translate a given KEGG Pathway and write it in the new format to outfile.
Expand Down
6 changes: 4 additions & 2 deletions src/de/zbit/kegg/io/YFilesWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.HashMap;
import java.util.Map;

import de.zbit.kegg.Translator;

/**
* This class creates an {@link OutputStream} that can be used with
* the yFiles Java library. It ensures that KEGGtranslator name
Expand Down Expand Up @@ -72,8 +74,8 @@ public YFilesWriter(OutputStream out) {
* </ul>
* such that no notes of yFiles are being written to the file.
*/
toReplace.put("yFiles", KEGGtranslator.APPLICATION_NAME);
toReplace.put(y.util.YVersion.currentVersionString(), KEGGtranslator.VERSION_NUMBER);
toReplace.put("yFiles", Translator.APPLICATION_NAME);
toReplace.put(y.util.YVersion.currentVersionString(), Translator.VERSION_NUMBER);

realOut = out;

Expand Down

0 comments on commit b6bb490

Please sign in to comment.