|
From: <ada...@us...> - 2013-09-27 13:20:37
|
Revision: 16950
http://sourceforge.net/p/gate/code/16950
Author: adamfunk
Date: 2013-09-27 13:20:34 +0000 (Fri, 27 Sep 2013)
Log Message:
-----------
Shifting some code around for clarity.
Reloading not working yet.
Modified Paths:
--------------
gate/trunk/plugins/Twitter/src/gate/corpora/twitter/Population.java
gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationConfig.java
Added Paths:
-----------
gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationDialogWrapper.java
Modified: gate/trunk/plugins/Twitter/src/gate/corpora/twitter/Population.java
===================================================================
--- gate/trunk/plugins/Twitter/src/gate/corpora/twitter/Population.java 2013-09-27 12:53:53 UTC (rev 16949)
+++ gate/trunk/plugins/Twitter/src/gate/corpora/twitter/Population.java 2013-09-27 13:20:34 UTC (rev 16950)
@@ -18,29 +18,12 @@
import gate.creole.metadata.CreoleResource;
import gate.gui.*;
import gate.util.*;
-
-import java.awt.Window;
import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.*;
-
import javax.swing.AbstractAction;
import javax.swing.Action;
-import javax.swing.Box;
-import javax.swing.BoxLayout;
-import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JDialog;
-import javax.swing.JFileChooser;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JSeparator;
-import javax.swing.JTextField;
-import javax.swing.SwingConstants;
-import javax.swing.SwingUtilities;
-
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.*;
@@ -200,233 +183,3 @@
}
}
-
-
-class PopulationDialogWrapper {
- protected JDialog dialog;
- private PopulationConfig config;
- private JTextField encodingField;
- private JCheckBox checkbox;
- private JFileChooser chooser;
- private List<URL> fileUrls;
- private ListEditor featureKeysEditor, contentKeysEditor;
-
-
- public PopulationDialogWrapper() {
- config = new PopulationConfig();
-
- dialog = new JDialog(MainFrame.getInstance(), "Populate from Twitter JSON", true);
- MainFrame.getGuiRoots().add(dialog);
- dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
- dialog.add(Box.createVerticalStrut(3));
-
- Box encodingBox = Box.createHorizontalBox();
- JLabel encodingLabel = new JLabel("Encoding:");
- encodingField = new JTextField(config.getEncoding());
- encodingBox.add(encodingLabel);
- encodingBox.add(encodingField);
- dialog.add(encodingBox);
- dialog.add(Box.createVerticalStrut(4));
-
- Box checkboxBox = Box.createHorizontalBox();
- checkboxBox.setToolTipText("If unchecked, one document per file");
- JLabel checkboxLabel = new JLabel("One document per tweet");
- checkbox = new JCheckBox();
- checkbox.setSelected(config.getOneDocCheckbox());
- checkboxBox.add(checkboxLabel);
- checkboxBox.add(Box.createHorizontalGlue());
- checkboxBox.add(checkbox);
- dialog.add(checkboxBox);
- dialog.add(Box.createVerticalStrut(4));
-
- contentKeysEditor = new ListEditor("Content keys: ", config.getContentKeys());
- contentKeysEditor.setToolTipText("JSON key paths to be turned into DocumentContent");
- dialog.add(contentKeysEditor);
- dialog.add(Box.createVerticalStrut(4));
-
- featureKeysEditor = new ListEditor("Feature keys: ", config.getFeatureKeys());
- featureKeysEditor.setToolTipText("JSON key paths to be turned into Tweet annotation features");
- dialog.add(featureKeysEditor);
- dialog.add(Box.createVerticalStrut(6));
-
- Box configPersistenceBox = Box.createHorizontalBox();
- configPersistenceBox.add(Box.createHorizontalGlue());
- JButton loadConfigButton = new JButton("Load configuration");
- loadConfigButton.setToolTipText("Replace the configuration above with a previously saved one");
- loadConfigButton.addActionListener(new LoadConfigListener(config, this));
- configPersistenceBox.add(loadConfigButton);
- configPersistenceBox.add(Box.createHorizontalGlue());
- JButton saveConfigButton = new JButton("Save configuration");
- saveConfigButton.setToolTipText("Save the configuration above for re-use");
- saveConfigButton.addActionListener(new SaveConfigListener(config, this));
- configPersistenceBox.add(saveConfigButton);
- configPersistenceBox.add(Box.createHorizontalGlue());
-
- // TODO keep these commented out on svn until reloading works properly
- //dialog.add(configPersistenceBox);
- //dialog.add(Box.createVerticalStrut(5));
-
- dialog.add(new JSeparator(SwingConstants.HORIZONTAL));
- dialog.add(Box.createVerticalStrut(2));
-
- chooser = new JFileChooser();
- chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- chooser.setMultiSelectionEnabled(true);
- chooser.setDialogTitle("Select a Twitter JSON file");
- chooser.resetChoosableFileFilters();
- chooser.setAcceptAllFileFilterUsed(false);
- ExtensionFileFilter filter = new ExtensionFileFilter("Twitter JSON files (*.json)", "json");
- chooser.addChoosableFileFilter(filter);
- chooser.setFileFilter(filter);
- chooser.setApproveButtonText("Populate");
- chooser.addActionListener(new PopulationDialogListener(this));
-
- dialog.add(chooser);
- dialog.pack();
- dialog.setLocationRelativeTo(dialog.getOwner());
- dialog.setVisible(true);
- }
-
-
- public String getEncoding() {
- return this.config.getEncoding();
- }
-
- public List<URL> getFileUrls() throws MalformedURLException {
- return this.fileUrls;
- }
-
- public int getTweetsPerDoc() {
- return this.config.getTweetsPerDoc();
- }
-
- public List<String> getContentKeys() {
- return this.config.getContentKeys();
- }
-
- public List<String> getFeatureKeys() {
- return this.config.getFeatureKeys();
- }
-
-
- protected void updateConfig() {
- this.config.setTweetsPerDoc(this.checkbox.isSelected() ? 1 : 0);
- this.config.setContentKeys(this.contentKeysEditor.getValues());
- this.config.setFeatureKeys(this.featureKeysEditor.getValues());
- this.config.setEncoding(this.encodingField.getText());
- }
-
-
- protected void updateGui() {
- this.encodingField.setText(config.getEncoding());
- this.contentKeysEditor.setValues(config.getContentKeys());
- this.featureKeysEditor.setValues(config.getFeatureKeys());
- this.checkbox.setSelected(config.getOneDocCheckbox());
- }
-
-
- protected void loadFile() {
- updateConfig();
-
- try {
- this.fileUrls = new ArrayList<URL>();
- for (File file : this.chooser.getSelectedFiles()) {
- this.fileUrls.add(file.toURI().toURL());
- }
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- }
- finally {
- this.dialog.dispose();
- }
- }
-
-
- protected void cancel() {
- this.dialog.dispose();
- }
-
-}
-
-
-class PopulationDialogListener implements ActionListener {
-
- private PopulationDialogWrapper dialog;
-
- public PopulationDialogListener(PopulationDialogWrapper dialog) {
- this.dialog = dialog;
- }
-
-
- @Override
- public void actionPerformed(ActionEvent event) {
- if (event.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)){
- this.dialog.loadFile();
- }
- else {
- this.dialog.cancel();
- }
- }
-
-}
-
-
-class ListEditor extends JPanel {
- private static final long serialVersionUID = -1578463259277343578L;
-
- private JButton listButton;
- private ListEditorDialog listEditor;
- private List<String> values;
- private JLabel label;
- private JTextField field;
-
- @Override
- public void setToolTipText(String text) {
- super.setToolTipText(text);
- label.setToolTipText(text);
- field.setToolTipText(text);
- }
-
-
- public ListEditor(String labelString, List<String> initialValues) {
- label = new JLabel(labelString);
- field = new JTextField();
- values = initialValues;
- field.setText(Strings.toString(initialValues));
- field.setEditable(false);
-
- listEditor = new ListEditorDialog(SwingUtilities.getAncestorOfClass(
- Window.class, this), values, List.class, String.class.getName());
-
- listButton = new JButton(MainFrame.getIcon("edit-list"));
- listButton.setToolTipText("Edit the list");
-
- listButton.addActionListener(new ActionListener() {
- @SuppressWarnings("unchecked")
- public void actionPerformed(ActionEvent e) {
- List<?> returnedList = listEditor.showDialog();
- if(returnedList != null) {
- values = (List<String>) returnedList;
- field.setText(Strings.toString(returnedList));
- }
- }
- });
-
- this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
- this.add(label);
- this.add(field);
- this.add(listButton);
- }
-
-
- public List<String> getValues() {
- return this.values;
- }
-
- public void setValues(List<String> values) {
- this.values = values;
- this.field.setText(Strings.toString(values));
- }
-
-}
Modified: gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationConfig.java
===================================================================
--- gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationConfig.java 2013-09-27 12:53:53 UTC (rev 16949)
+++ gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationConfig.java 2013-09-27 13:20:34 UTC (rev 16950)
@@ -109,12 +109,14 @@
public static PopulationConfig load(File file) {
XStream xstream = new XStream(new StaxDriver());
+ // setClassLoader needed so XStream finds plugin classes
xstream.setClassLoader(Gate.getClassLoader());
return (PopulationConfig) xstream.fromXML(file);
}
public static PopulationConfig load(URL url) {
XStream xstream = new XStream(new StaxDriver());
+ // setClassLoader needed so XStream finds plugin classes
xstream.setClassLoader(Gate.getClassLoader());
return (PopulationConfig) xstream.fromXML(url);
}
@@ -122,6 +124,7 @@
public void saveXML(File file) throws IOException {
XStream xstream = new XStream(new StaxDriver());
+ //TODO slap an XML prolog on the front
PrettyPrintWriter ppw = new PrettyPrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
xstream.marshal(this, ppw);
ppw.close();
@@ -131,11 +134,9 @@
class LoadConfigListener implements ActionListener {
- PopulationConfig config;
PopulationDialogWrapper wrapper;
- public LoadConfigListener(PopulationConfig config, PopulationDialogWrapper wrapper) {
- this.config = config;
+ public LoadConfigListener(PopulationDialogWrapper wrapper) {
this.wrapper = wrapper;
}
@@ -146,7 +147,7 @@
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int chosen = chooser.showOpenDialog(this.wrapper.dialog);
if (chosen == JFileChooser.APPROVE_OPTION) {
- config.reload(chooser.getSelectedFile());
+ wrapper.config = PopulationConfig.load(chooser.getSelectedFile());
wrapper.updateGui();
}
}
@@ -154,11 +155,9 @@
class SaveConfigListener implements ActionListener {
- PopulationConfig config;
PopulationDialogWrapper wrapper;
- public SaveConfigListener(PopulationConfig config, PopulationDialogWrapper wrapper) {
- this.config = config;
+ public SaveConfigListener(PopulationDialogWrapper wrapper) {
this.wrapper = wrapper;
}
@@ -170,7 +169,7 @@
int chosen = chooser.showSaveDialog(this.wrapper.dialog);
if (chosen == JFileChooser.APPROVE_OPTION) {
try {
- config.saveXML(chooser.getSelectedFile());
+ wrapper.config.saveXML(chooser.getSelectedFile());
}
catch(IOException e1) {
e1.printStackTrace();
Copied: gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationDialogWrapper.java (from rev 16949, gate/trunk/plugins/Twitter/src/gate/corpora/twitter/Population.java)
===================================================================
--- gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationDialogWrapper.java (rev 0)
+++ gate/trunk/plugins/Twitter/src/gate/corpora/twitter/PopulationDialogWrapper.java 2013-09-27 13:20:34 UTC (rev 16950)
@@ -0,0 +1,264 @@
+/*
+ * Copyright (c) 1995-2013, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ * $Id$
+ */
+package gate.corpora.twitter;
+
+import gate.gui.*;
+import gate.util.*;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JDialog;
+import javax.swing.JFileChooser;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JSeparator;
+import javax.swing.JTextField;
+import javax.swing.SwingConstants;
+import javax.swing.SwingUtilities;
+
+
+public class PopulationDialogWrapper {
+ protected JDialog dialog;
+ protected PopulationConfig config;
+ private JTextField encodingField;
+ private JCheckBox checkbox;
+ private JFileChooser chooser;
+ private List<URL> fileUrls;
+ private ListEditor featureKeysEditor, contentKeysEditor;
+
+
+ public PopulationDialogWrapper() {
+ config = new PopulationConfig();
+
+ dialog = new JDialog(MainFrame.getInstance(), "Populate from Twitter JSON", true);
+ MainFrame.getGuiRoots().add(dialog);
+ dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
+ dialog.add(Box.createVerticalStrut(3));
+
+ Box encodingBox = Box.createHorizontalBox();
+ JLabel encodingLabel = new JLabel("Encoding:");
+ encodingField = new JTextField(config.getEncoding());
+ encodingBox.add(encodingLabel);
+ encodingBox.add(encodingField);
+ dialog.add(encodingBox);
+ dialog.add(Box.createVerticalStrut(4));
+
+ Box checkboxBox = Box.createHorizontalBox();
+ checkboxBox.setToolTipText("If unchecked, one document per file");
+ JLabel checkboxLabel = new JLabel("One document per tweet");
+ checkbox = new JCheckBox();
+ checkbox.setSelected(config.getOneDocCheckbox());
+ checkboxBox.add(checkboxLabel);
+ checkboxBox.add(Box.createHorizontalGlue());
+ checkboxBox.add(checkbox);
+ dialog.add(checkboxBox);
+ dialog.add(Box.createVerticalStrut(4));
+
+ contentKeysEditor = new ListEditor("Content keys: ", config.getContentKeys());
+ contentKeysEditor.setToolTipText("JSON key paths to be turned into DocumentContent");
+ dialog.add(contentKeysEditor);
+ dialog.add(Box.createVerticalStrut(4));
+
+ featureKeysEditor = new ListEditor("Feature keys: ", config.getFeatureKeys());
+ featureKeysEditor.setToolTipText("JSON key paths to be turned into Tweet annotation features");
+ dialog.add(featureKeysEditor);
+ dialog.add(Box.createVerticalStrut(6));
+
+ Box configPersistenceBox = Box.createHorizontalBox();
+ configPersistenceBox.add(Box.createHorizontalGlue());
+ JButton loadConfigButton = new JButton("Load configuration");
+ loadConfigButton.setToolTipText("Replace the configuration above with a previously saved one");
+ loadConfigButton.addActionListener(new LoadConfigListener(this));
+ configPersistenceBox.add(loadConfigButton);
+ configPersistenceBox.add(Box.createHorizontalGlue());
+ JButton saveConfigButton = new JButton("Save configuration");
+ saveConfigButton.setToolTipText("Save the configuration above for re-use");
+ saveConfigButton.addActionListener(new SaveConfigListener(this));
+ configPersistenceBox.add(saveConfigButton);
+ configPersistenceBox.add(Box.createHorizontalGlue());
+
+ // TODO keep these commented out on svn until reloading works properly
+ //dialog.add(configPersistenceBox);
+ //dialog.add(Box.createVerticalStrut(5));
+
+ dialog.add(new JSeparator(SwingConstants.HORIZONTAL));
+ dialog.add(Box.createVerticalStrut(2));
+
+ chooser = new JFileChooser();
+ chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
+ chooser.setMultiSelectionEnabled(true);
+ chooser.setDialogTitle("Select a Twitter JSON file");
+ chooser.resetChoosableFileFilters();
+ chooser.setAcceptAllFileFilterUsed(false);
+ ExtensionFileFilter filter = new ExtensionFileFilter("Twitter JSON files (*.json)", "json");
+ chooser.addChoosableFileFilter(filter);
+ chooser.setFileFilter(filter);
+ chooser.setApproveButtonText("Populate");
+ chooser.addActionListener(new PopulationDialogListener(this));
+
+ dialog.add(chooser);
+ dialog.pack();
+ dialog.setLocationRelativeTo(dialog.getOwner());
+ dialog.setVisible(true);
+ }
+
+
+ public String getEncoding() {
+ return this.config.getEncoding();
+ }
+
+ public List<URL> getFileUrls() throws MalformedURLException {
+ return this.fileUrls;
+ }
+
+ public int getTweetsPerDoc() {
+ return this.config.getTweetsPerDoc();
+ }
+
+ public List<String> getContentKeys() {
+ return this.config.getContentKeys();
+ }
+
+ public List<String> getFeatureKeys() {
+ return this.config.getFeatureKeys();
+ }
+
+
+ protected void updateConfig() {
+ this.config.setTweetsPerDoc(this.checkbox.isSelected() ? 1 : 0);
+ this.config.setContentKeys(this.contentKeysEditor.getValues());
+ this.config.setFeatureKeys(this.featureKeysEditor.getValues());
+ this.config.setEncoding(this.encodingField.getText());
+ }
+
+
+ protected void updateGui() {
+ this.encodingField.setText(config.getEncoding());
+ this.contentKeysEditor.setValues(config.getContentKeys());
+ this.featureKeysEditor.setValues(config.getFeatureKeys());
+ this.checkbox.setSelected(config.getOneDocCheckbox());
+ }
+
+
+ protected void loadFile() {
+ updateConfig();
+
+ try {
+ this.fileUrls = new ArrayList<URL>();
+ for (File file : this.chooser.getSelectedFiles()) {
+ this.fileUrls.add(file.toURI().toURL());
+ }
+ }
+ catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ finally {
+ this.dialog.dispose();
+ }
+ }
+
+
+ protected void cancel() {
+ this.dialog.dispose();
+ }
+
+}
+
+
+class PopulationDialogListener implements ActionListener {
+
+ private PopulationDialogWrapper dialog;
+
+ public PopulationDialogListener(PopulationDialogWrapper dialog) {
+ this.dialog = dialog;
+ }
+
+
+ @Override
+ public void actionPerformed(ActionEvent event) {
+ if (event.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)){
+ this.dialog.loadFile();
+ }
+ else {
+ this.dialog.cancel();
+ }
+ }
+
+}
+
+
+class ListEditor extends JPanel {
+ private static final long serialVersionUID = -1578463259277343578L;
+
+ private JButton listButton;
+ private ListEditorDialog listEditor;
+ private List<String> values;
+ private JLabel label;
+ private JTextField field;
+
+ @Override
+ public void setToolTipText(String text) {
+ super.setToolTipText(text);
+ label.setToolTipText(text);
+ field.setToolTipText(text);
+ }
+
+
+ public ListEditor(String labelString, List<String> initialValues) {
+ label = new JLabel(labelString);
+ field = new JTextField();
+ values = initialValues;
+ field.setText(Strings.toString(initialValues));
+ field.setEditable(false);
+
+ listEditor = new ListEditorDialog(SwingUtilities.getAncestorOfClass(
+ Window.class, this), values, List.class, String.class.getName());
+
+ listButton = new JButton(MainFrame.getIcon("edit-list"));
+ listButton.setToolTipText("Edit the list");
+
+ listButton.addActionListener(new ActionListener() {
+ @SuppressWarnings("unchecked")
+ public void actionPerformed(ActionEvent e) {
+ List<?> returnedList = listEditor.showDialog();
+ if(returnedList != null) {
+ values = (List<String>) returnedList;
+ field.setText(Strings.toString(returnedList));
+ }
+ }
+ });
+
+ this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+ this.add(label);
+ this.add(field);
+ this.add(listButton);
+ }
+
+
+ public List<String> getValues() {
+ return this.values;
+ }
+
+ public void setValues(List<String> values) {
+ this.values = values;
+ this.field.setText(Strings.toString(values));
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|