Author: ruschein
Date: 2010-10-13 13:14:30 -0700 (Wed, 13 Oct 2010)
New Revision: 22226
Modified:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loaddatatable/LoadDataTableTask.java
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loadnetwork/LoadNetworkFileTask.java
core3/work-api/trunk/src/main/java/org/cytoscape/work/AbstractTunableHandler.java
core3/work-api/trunk/src/main/java/org/cytoscape/work/Tunable.java
core3/work-api/trunk/src/main/java/org/cytoscape/work/TunableHandler.java
core3/work-api/trunk/src/test/java/org/cytoscape/work/HasAnnotatedField.java
core3/work-api/trunk/src/test/java/org/cytoscape/work/TunableHandlerTest.java
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/FileHandler.java
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/SupportedFileTypesManager.java
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/XorPanel.java
Log:
Stage 1 in the removal of the Tunables Params enum.
Modified:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loaddatatable/LoadDataTableTask.java
===================================================================
---
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loaddatatable/LoadDataTableTask.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loaddatatable/LoadDataTableTask.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -16,7 +16,7 @@
public class LoadDataTableTask extends AbstractTask {
- @Tunable(description = "Data table file to load", flags = {
Param.ATTRIBUTES })
+ @Tunable(description="Data table file to load",
params="fileCategory=attribute")
public File file;
private CyTableReader reader;
@@ -41,10 +41,6 @@
insertTasksAfterCurrentTask(reader, new
FinalStatusMessageUpdateTask(reader));
}
-
- @Override
- public void cancel() {
- }
}
Modified:
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loadnetwork/LoadNetworkFileTask.java
===================================================================
---
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loadnetwork/LoadNetworkFileTask.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/core-task-impl/trunk/src/main/java/org/cytoscape/task/internal/loadnetwork/LoadNetworkFileTask.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -45,7 +45,7 @@
* Specific instance of AbstractLoadNetworkTask that loads a File.
*/
public class LoadNetworkFileTask extends AbstractLoadNetworkTask {
- @Tunable(description = "Network file to load",flags = {Param.NETWORK})
+ @Tunable(description="Network file to load",
params="fileCategory=network;input=true")
public File file;
public LoadNetworkFileTask(CyNetworkViewReaderManager mgr,
CyNetworkManager netmgr,
Modified:
core3/work-api/trunk/src/main/java/org/cytoscape/work/AbstractTunableHandler.java
===================================================================
---
core3/work-api/trunk/src/main/java/org/cytoscape/work/AbstractTunableHandler.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-api/trunk/src/main/java/org/cytoscape/work/AbstractTunableHandler.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -5,10 +5,18 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
/** Provides the standard implementation for most of the methods declared by
the TunableHandler interface.
*/
public class AbstractTunableHandler implements TunableHandler {
+ private enum ParamsParseState {
+ KEY_START, LOOKING_FOR_EQUAL_SIGN, VALUE_START,
LOOKING_FOR_SEMICOLON;
+ }
+
final private Field field;
final private Method getter;
final private Method setter;
@@ -129,4 +137,76 @@
return
unqualifiedClassName.substring(unqualifiedClassName.lastIndexOf(".") + 1) + "."
+ getName();
}
+
+ /**
+ * @return the parsed result from Tunable.getParams()
+ */
+ final public Properties getParams() throws IllegalArgumentException {
+ final String rawString = tunable.params();
+ final Properties keyValuesPairs = new Properties();
+
+ StringBuilder key = null;
+ StringBuilder value = null;
+ ParamsParseState state = ParamsParseState.KEY_START;
+ boolean escaped = false;
+ for (int i = 0; i < rawString.length(); ++i) {
+ final char ch = rawString.charAt(i);
+
+ switch (state) {
+ case KEY_START:
+ key = new StringBuilder();
+ if (!Character.isLetter(ch))
+ throw new
IllegalArgumentException(getName() + "'s getParams() returns an invalid key!");
+ key.append(ch);
+ state = ParamsParseState.LOOKING_FOR_EQUAL_SIGN;
+ break;
+ case LOOKING_FOR_EQUAL_SIGN:
+ if (ch == '=')
+ state = ParamsParseState.VALUE_START;
+ else {
+ if (!Character.isLetter(ch))
+ throw new
IllegalArgumentException(getName() + "'s getParams() returns an invalid key!");
+ key.append(ch);
+ }
+ break;
+ case VALUE_START:
+ value = new StringBuilder();
+ if (ch == ';')
+ throw new
IllegalArgumentException(getName() + "'s getParams() returns an invalid
value!");
+ if (ch == '\\')
+ escaped = true;
+ else
+ value.append(ch);
+ state = ParamsParseState.LOOKING_FOR_SEMICOLON;
+ break;
+ case LOOKING_FOR_SEMICOLON:
+ if (escaped) {
+ value.append(ch);
+ escaped = false;
+ } else if (ch == ';') {
+
keyValuesPairs.setProperty(key.toString(), value.toString());
+ state = ParamsParseState.KEY_START;
+ } else {
+ if (ch == '\\')
+ escaped = true;
+ else
+ value.append(ch);
+ }
+ break;
+ }
+ }
+
+ if (escaped)
+ throw new IllegalArgumentException(getName() + "'s
getParams() returns an invalid escaped character!");
+ if (state != ParamsParseState.KEY_START && state !=
ParamsParseState.LOOKING_FOR_SEMICOLON)
+ throw new IllegalArgumentException(getName() + "'s
getParams() returns an incomplete string: \"" + rawString + "\"!");
+
+ if (key != null) {
+ if (value == null)
+ throw new IllegalArgumentException(getName() +
"'s getParams() returns a key without a value!");
+ keyValuesPairs.setProperty(key.toString(),
value.toString());
+ }
+
+ return keyValuesPairs;
+ }
}
Modified: core3/work-api/trunk/src/main/java/org/cytoscape/work/Tunable.java
===================================================================
--- core3/work-api/trunk/src/main/java/org/cytoscape/work/Tunable.java
2010-10-13 17:12:50 UTC (rev 22225)
+++ core3/work-api/trunk/src/main/java/org/cytoscape/work/Tunable.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -69,7 +69,6 @@
*/
Param[] flags() default {};
-
/**
* Used to define all the groups in which the Tunable takes part (by
default, its doesn't belong to any group).
*
@@ -212,9 +211,27 @@
* </pre></p>
*/
Param[] groupTitles() default {};
-
-
+
/**
+ * Returns a key1=value1;key2=value2;...;keyN=valueN type string. To
include commas,
+ * semicolons or backslashes in a value you must escape it with a
leading backslash.
+ *
+ * Possible keys (which must consist of letters only) are<br/>
+ * <ul>
+ * <li>
+ * fileCategory: this is used solely for File tunables and must be
one of "network",
+ * "table", "image", "attribute", "session", or "unspecified".
+ * </li>
+ * <li>
+ * input: this is used solely for File tunables and must be either
"true" or "false"
+ * </li>
+ * </ul>
+ *
+ * Note: Blanks/spaces in values are significant!
+ */
+ String params() default "";
+
+ /**
* Enumeration that contains the parameters used for
<code>flag{}</code>, groupTitles{}, and <code>alignment{}</code>
*
* <p><pre>
@@ -250,21 +267,6 @@
COLLAPSED,
/**
- * Filter for network files in a <code>Tunable File</code> :
only network files will be choosable in the JFileChooser dialog
- */
- NETWORK,
-
- /**
- * Filter for session files in a <code>Tunable File</code> :
only session files will be choosable in the JFileChooser dialog
- */
- SESSION,
-
- /**
- * Filter for attributes files in a <code>Tunable File</code> :
only attributes files will be choosable in the JFileChooser dialog
- */
- ATTRIBUTES,
-
- /**
* The name of the group whose this <code>Tunable</code> is
taking part shouldn't be displayed in the Borders in the GUI
*/
HIDDEN,
@@ -273,12 +275,6 @@
* The name of the group whose this <code>Tunable</code> is
taking part should be displayed in the Borders in the GUI<br>
* This is the default state
*/
- DISPLAYED,
-
- /**
- * If the <code>Tunable</code> is a Java <code>File</code>
object, this will allow
- * the creation of a new file. For any other type of
<code>Tunable</code> this will be ignored.
- */
- SAVE_FILE
+ DISPLAYED
}
}
Modified:
core3/work-api/trunk/src/main/java/org/cytoscape/work/TunableHandler.java
===================================================================
--- core3/work-api/trunk/src/main/java/org/cytoscape/work/TunableHandler.java
2010-10-13 17:12:50 UTC (rev 22225)
+++ core3/work-api/trunk/src/main/java/org/cytoscape/work/TunableHandler.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -2,11 +2,13 @@
import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.Properties;
// TODO: Should we strip out the methods that are simply pass-thrus for
Tunable and replace them with a single getTunable() method?
-/** Interface for classes that deal with reading out and writing back Tunables
and their properties.
+/** Interface for classes that deal with reading out and writing back
<code>Tunable</code>s and their properties.
*/
public interface TunableHandler {
/**
@@ -71,4 +73,9 @@
* Please note that the returned String will always contain a single
embedded dot.
*/
String getQualifiedName();
+
+ /**
+ * @return the parsed result from Tunable.getParams()
+ */
+ Properties getParams();
}
Modified:
core3/work-api/trunk/src/test/java/org/cytoscape/work/HasAnnotatedField.java
===================================================================
---
core3/work-api/trunk/src/test/java/org/cytoscape/work/HasAnnotatedField.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-api/trunk/src/test/java/org/cytoscape/work/HasAnnotatedField.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -29,7 +29,9 @@
class HasAnnotatedField {
- @Tunable(description="An annotated field", groups={"group1"},
dependsOn="Fred",
flags={Tunable.Param.SLIDER,Tunable.Param.HORIZONTAL,Tunable.Param.VERTICAL,Tunable.Param.UNCOLLAPSED,Tunable.Param.COLLAPSED,Tunable.Param.NETWORK,Tunable.Param.SESSION,Tunable.Param.ATTRIBUTES,Tunable.Param.HIDDEN,Tunable.Param.DISPLAYED,Tunable.Param.SAVE_FILE})
+ @Tunable(description="An annotated field", groups={"group1"},
dependsOn="Fred",
+
params="input=true;escaped=\\\\\\,\\;;multiple=first,second,third",
+
flags={Tunable.Param.SLIDER,Tunable.Param.HORIZONTAL,Tunable.Param.VERTICAL,Tunable.Param.UNCOLLAPSED,Tunable.Param.COLLAPSED,Tunable.Param.HIDDEN,Tunable.Param.DISPLAYED})
public int annotatedInt;
public int getAnnotatedInt() { return annotatedInt; }
Modified:
core3/work-api/trunk/src/test/java/org/cytoscape/work/TunableHandlerTest.java
===================================================================
---
core3/work-api/trunk/src/test/java/org/cytoscape/work/TunableHandlerTest.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-api/trunk/src/test/java/org/cytoscape/work/TunableHandlerTest.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -31,6 +31,8 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
import static org.junit.Assert.*;
import org.junit.Before;
@@ -143,6 +145,28 @@
assertEquals("Qualified name of an annotated getter/setter pair
is not as expected!",
"HasAnnotatedSetterAndGetterMethods.PrivateInt",
getterAndSetterHandler.getQualifiedName());
}
+
+ @Test
+ public final void testGetParams() {
+ final Properties keysAndValues = fieldHandler.getParams();
+ assertTrue("key \"input\" is missing!",
keysAndValues.containsKey("input"));
+ if (keysAndValues.containsKey("input")) {
+ final String value = keysAndValues.getProperty("input");
+ assertEquals("\"input\" does not contain \"true\"!",
value, "true");
+ }
+
+ assertTrue("key \"escaped\" is missing!",
keysAndValues.containsKey("escaped"));
+ if (keysAndValues.containsKey("escaped")) {
+ final String value =
keysAndValues.getProperty("escaped");
+ assertEquals("\"escaped\" does not contain \"\\,;\"!",
value, "\\,;");
+ }
+
+ assertTrue("key \"multiple\" is missing!",
keysAndValues.containsKey("multiple"));
+ if (keysAndValues.containsKey("multiple")) {
+ final String value =
keysAndValues.getProperty("multiple");
+ assertEquals("\"multiple\" does not contain \"first\"
in the 0th position!", value, "first,second,third");
+ }
+ }
}
Modified:
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/FileHandler.java
===================================================================
---
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/FileHandler.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/FileHandler.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -44,10 +44,8 @@
private JSeparator titleSeparator;
private MouseClick mouseClick;
private GroupLayout layout;
- private enum Type { NETWORK, SESSION, ATTRIBUTES, DEFAULT };
- private Type type;
private SupportedFileTypesManager fileTypesManager;
- private boolean openMode; // true if we'd like to open a file and false
if we'd like to save a file
+ private boolean input;
/**
* Constructs the <code>GUIHandler</code> for the <code>File</code> type
@@ -74,19 +72,10 @@
}
private void init() {
- // Determine whether we're dealing w/ an "open" or "save" mode:
- openMode = true;
- for (final Param param : getFlags()) {
- if (param == Param.SAVE_FILE) {
- openMode = false;
- break;
- }
- }
-
//Construction of GUI
fileChooser = new JFileChooser();
- setFileType();
- setGui(type);
+ input = isInput();
+ setGui();
setLayout();
panel.setLayout(layout);
}
@@ -105,29 +94,11 @@
}
}
- //set the type of file that will be imported depending on the "Param"
Tunable annotation of the file
- private void setFileType() {
- for (Param s : getFlags()) {
- if (s.equals(Param.NETWORK)) {
- type = Type.NETWORK;
- return;
- } else if(s.equals(Param.SESSION)) {
- type = Type.SESSION;
- return;
- } else if(s.equals(Param.ATTRIBUTES)) {
- type = Type.ATTRIBUTES;
- return;
- }
- }
- type = Type.DEFAULT;
- }
-
-
//construction of the GUI depending on the file type expected:
// -field to display the file's path
// -button to open the FileCHooser
//add listener to the field and button
- private void setGui(Type type) {
+ private void setGui() {
titleSeparator = new JSeparator();
titleLabel = new JLabel();
image = new
ImageIcon(getClass().getResource("/images/ximian/stock_open.png"));
@@ -137,48 +108,32 @@
fileTextField.setFont(new Font(null, Font.ITALIC,12));
mouseClick = new MouseClick(fileTextField);
fileTextField.addMouseListener(mouseClick);
- chooseButton = new JButton(openMode ? "Open a File..." : "Save
a File...", image);
- chooseButton.setActionCommand(openMode ? "open" : "save");
+ chooseButton = new JButton(input ? "Open a File..." : "Save a
File...", image);
+ chooseButton.setActionCommand(input ? "open" : "save");
chooseButton.addActionListener(new myFileActionListener());
- //for each type of file : set titlelabel and fileTextField
text, and set FileChooser in order to just display files of the specified
"Param" : network,attributes,session
- switch (type) {
- case NETWORK : {
- //set title and textfield text for network type
- fileTextField.setText("Please select a network
file...");
- titleLabel.setText("import network file");
+ //set title and textfield text for the file type
+ final String fileCategory = getFileCategory().toUpperCase();
+ fileTextField.setText("Please select a " +
fileCategory.toLowerCase() + " file...");
+ titleLabel.setText("Import " + initialCaps(fileCategory) + "
File");
+ List<FileChooserFilter> filters =
fileTypesManager.getSupportedFileTypes(DataCategory.valueOf(fileCategory),
input);
+ for (FileChooserFilter filter : filters)
+ fileChooser.addChoosableFileFilter(filter);
+ }
- List<FileChooserFilter> filters =
fileTypesManager.getSupportedFileTypes(DataCategory.NETWORK);
- for (FileChooserFilter filter : filters) {
- fileChooser.addChoosableFileFilter(filter);
- }
- break;
- }
- case SESSION: {
- //set title and textfield text for session type
- fileTextField.setText("Please select a session
file...");
- titleLabel.setText("import session file");
+ private String getFileCategory() {
+ return getParams().getProperty("fileCategory", "unspecified");
+ }
- //set session filter for filechooser
- fileChooser.addChoosableFileFilter(new
FileChooserFilter("Session files (*.cys)",".cys"));
- break;
- }
- case ATTRIBUTES: {
- //set title and textfield text for attribute type
- fileTextField.setText("Please select an attributes
file...");
- titleLabel.setText("import attributes file");
+ private boolean isInput() {
+ return getParams().getProperty("input",
"false").equalsIgnoreCase("true");
+ }
- //set filters for filechooser
- String[] attr = {"attr","attrs"};
- fileChooser.addChoosableFileFilter(new
FileChooserFilter("Attributes files",attr));
- break;
- }
- default: {
- //set title and textfield text for attribute type
- fileTextField.setText("Please select a file...");
- titleLabel.setText("import file");
- }
- }
+ private String initialCaps(final String s) {
+ if (s.isEmpty())
+ return "";
+ else
+ return Character.toUpperCase(s.charAt(0)) +
s.substring(1).toLowerCase();
}
//diplays the panel's component in a good view
Modified:
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/SupportedFileTypesManager.java
===================================================================
---
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/SupportedFileTypesManager.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/SupportedFileTypesManager.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -1,5 +1,6 @@
package org.cytoscape.work.internal.tunables.utils;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -10,37 +11,56 @@
import org.cytoscape.io.CyFileFilter;
import org.cytoscape.io.DataCategory;
+import org.cytoscape.io.FileIOFactory;
import org.cytoscape.io.read.InputStreamTaskFactory;
+import org.cytoscape.io.write.CyWriterFactory;
+
/**
* Provides a list of available file types by consulting all registered
- * <code>InputStreamTaskFactory</code> instances.
+ * <code>InputStreamTaskFactory</code> and
<code>CyWriterFactory</code>instances.
*/
public class SupportedFileTypesManager {
- Set<InputStreamTaskFactory> factories;
-
+ Set<FileIOFactory> inputFactories;
+ Set<FileIOFactory> outputFactories;
+
public SupportedFileTypesManager() {
- factories = new HashSet<InputStreamTaskFactory>();
+ inputFactories = new HashSet<FileIOFactory>();
+ outputFactories = new HashSet<FileIOFactory>();
}
-
+
public void addInputStreamTaskFactory(InputStreamTaskFactory factory,
Map<?,?> properties) {
- factories.add(factory);
+ inputFactories.add(factory);
}
-
+
public void removeInputStreamTaskFactory(InputStreamTaskFactory
factory, Map<?,?> properties) {
- factories.remove(factory);
+ inputFactories.remove(factory);
}
-
- public List<FileChooserFilter> getSupportedFileTypes(DataCategory
category) {
+
+ public void addOutputStreamTaskFactory(CyWriterFactory factory,
Map<?,?> properties) {
+ outputFactories.add(factory);
+ }
+
+ public void removeOutputStreamTaskFactory(CyWriterFactory factory,
Map<?,?> properties) {
+ outputFactories.remove(factory);
+ }
+
+ public List<FileChooserFilter> getSupportedFileTypes(final DataCategory
category, boolean input) {
+ if (input)
+ return getSupportedFileTypes(category, inputFactories);
+ else
+ return getSupportedFileTypes(category, outputFactories);
+ }
+
+ private List<FileChooserFilter> getSupportedFileTypes(final
DataCategory category, final Set<FileIOFactory> factories) {
List<FileChooserFilter> types = new
ArrayList<FileChooserFilter>();
-
+
Set<String> allExtensions = new HashSet<String>();
- for (InputStreamTaskFactory factory : factories) {
+ for (final FileIOFactory factory : factories) {
CyFileFilter filter = factory.getCyFileFilter();
- if (filter.getDataCategory() != category) {
+ if (filter.getDataCategory() != category)
continue;
- }
-
+
String description = filter.getDescription();
Set<String> filterExtensions = filter.getExtensions();
String[] extensions = new
String[filterExtensions.size()];
@@ -52,18 +72,17 @@
}
types.add(new FileChooserFilter(description,
extensions));
}
-
- if (types.size() == 0) {
+
+ if (types.isEmpty())
return types;
- }
-
+
Collections.sort(types, new Comparator<FileChooserFilter>() {
@Override
public int compare(FileChooserFilter o1,
FileChooserFilter o2) {
return
o1.getDescription().compareTo(o2.getDescription());
}
});
-
+
String description = String.format("All %1$s files",
category.toString().toLowerCase());
types.add(new FileChooserFilter(description, new
ArrayList<String>(allExtensions).toArray(new String[allExtensions.size()])));
return types;
Modified:
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/XorPanel.java
===================================================================
---
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/XorPanel.java
2010-10-13 17:12:50 UTC (rev 22225)
+++
core3/work-swing-impl/trunk/src/main/java/org/cytoscape/work/internal/tunables/utils/XorPanel.java
2010-10-13 20:14:30 UTC (rev 22226)
@@ -8,6 +8,9 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Properties;
+
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
@@ -95,6 +98,7 @@
@Override public Object getValue() { return null; }
@Override public void setValue(final Object newValue) { }
@Override public String getQualifiedName() { return null; }
+ @Override public Properties getParams() { return null; }
public String getName() { return null; }
public JPanel getJPanel() { return null; }
public void handle() {}
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.