use of gate.util.OptionsMap in project gate-core by GateNLP.
the class Main method applyUserPreferences.
// runGui()
/**
* Reads the user config data and applies the required settings.
* This must be called <b>after</b> {@link Gate#init()} but <b>before</b>
* any GUI components are created.
*/
public static void applyUserPreferences() {
// look and feel
String lnfClassName;
if (System.getProperty("swing.defaultlaf") != null) {
lnfClassName = System.getProperty("swing.defaultlaf");
} else {
lnfClassName = Gate.getUserConfig().getString(GateConstants.LOOK_AND_FEEL);
}
if (lnfClassName == null) {
// doesn't play nicely with most Gnome themes
if (System.getProperty("os.name").toLowerCase().indexOf("linux") != -1) {
// running on Linux
lnfClassName = UIManager.getCrossPlatformLookAndFeelClassName();
} else {
lnfClassName = UIManager.getSystemLookAndFeelClassName();
}
}
try {
UIManager.setLookAndFeel(lnfClassName);
} catch (Exception e) {
System.err.print("Could not set your preferred Look and Feel. The error was:\n" + e.toString() + "\nReverting to using Java Look and Feel");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e1) {
// we just can't catch a break here. Let's forget about look and feel.
System.err.print("Could not set the cross-platform Look and Feel either. The error was:\n" + e1.toString() + "\nGiving up on Look and Feel.");
}
}
Gate.getUserConfig().put(GateConstants.LOOK_AND_FEEL, lnfClassName);
// read the user config data
OptionsMap userConfig = Gate.getUserConfig();
// text font
Font font = userConfig.getFont(GateConstants.TEXT_COMPONENTS_FONT);
if (font == null) {
font = UIManager.getFont("TextPane.font");
}
if (font != null) {
OptionsDialog.setTextComponentsFont(font);
}
// menus font
font = userConfig.getFont(GateConstants.MENUS_FONT);
if (font == null) {
font = UIManager.getFont("Menu.font");
}
if (font != null) {
OptionsDialog.setMenuComponentsFont(font);
}
// other gui font
font = userConfig.getFont(GateConstants.OTHER_COMPONENTS_FONT);
if (font == null) {
font = UIManager.getFont("Button.font");
}
if (font != null) {
OptionsDialog.setComponentsFont(font);
}
}
use of gate.util.OptionsMap in project gate-core by GateNLP.
the class DocumentEditor method initViews.
protected void initViews() {
viewsInited = true;
// start building the UI
setLayout(new BorderLayout());
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setMaximumSize(new Dimension(Integer.MAX_VALUE, progressBar.getPreferredSize().height));
add(progressBar, BorderLayout.CENTER);
progressBar.setString("Building views");
progressBar.setValue(10);
// create the skeleton UI
topSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, null, null);
topSplit.setResizeWeight(0.3);
topSplit.setContinuousLayout(true);
topSplit.setOneTouchExpandable(true);
bottomSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topSplit, null);
bottomSplit.setResizeWeight(0.7);
bottomSplit.setContinuousLayout(true);
bottomSplit.setOneTouchExpandable(true);
horizontalSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, bottomSplit, null);
horizontalSplit.setResizeWeight(0.7);
horizontalSplit.setContinuousLayout(true);
horizontalSplit.setOneTouchExpandable(true);
// create the bars
topBar = new JToolBar(JToolBar.HORIZONTAL);
topBar.setFloatable(false);
add(topBar, BorderLayout.NORTH);
progressBar.setValue(40);
centralViews = new ArrayList<DocumentView>();
verticalViews = new ArrayList<DocumentView>();
horizontalViews = new ArrayList<DocumentView>();
// parse all Creole resources and look for document views
Set<String> vrSet = Gate.getCreoleRegister().getVrTypes();
List<ResourceData> viewTypes = new ArrayList<ResourceData>();
for (String vr : vrSet) {
ResourceData rData = Gate.getCreoleRegister().get(vr);
try {
if (DocumentView.class.isAssignableFrom(rData.getResourceClass())) {
viewTypes.add(rData);
}
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
// sort view types by label
Collections.sort(viewTypes, new Comparator<ResourceData>() {
@Override
public int compare(ResourceData rd1, ResourceData rd2) {
return rd1.getName().compareTo(rd2.getName());
}
});
for (ResourceData viewType : viewTypes) {
try {
// create the resource
DocumentView aView = (DocumentView) Factory.createResource(viewType.getClassName());
aView.setTarget(document);
aView.setOwner(this);
// add the view
addView(aView, viewType.getName());
} catch (ResourceInstantiationException rie) {
rie.printStackTrace();
}
}
// select the main central view only
if (centralViews.size() > 0)
setCentralView(0);
// populate the main VIEW
remove(progressBar);
add(horizontalSplit, BorderLayout.CENTER);
searchAction = new SearchAction();
JButton searchButton = new JButton(searchAction);
searchButton.setMargin(new Insets(0, 0, 0, 0));
topBar.add(Box.createHorizontalStrut(5));
topBar.add(searchButton);
// add a key binding for the search function
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control F"), "Search in text");
getActionMap().put("Search in text", searchAction);
// create menu that contains several options for the document editor
final OptionsMap config = Gate.getUserConfig();
final JPopupMenu optionsMenu = new JPopupMenu("Options menu");
final JMenuItem saveCurrentLayoutMenuItem = new JMenuItem(new AbstractAction("Save Current Layout") {
@Override
public void actionPerformed(ActionEvent evt) {
saveSettings();
}
});
optionsMenu.add(saveCurrentLayoutMenuItem);
final JCheckBoxMenuItem restoreLayoutAutomaticallyMenuItem = new JCheckBoxMenuItem("Restore Layout Automatically");
restoreLayoutAutomaticallyMenuItem.setSelected(Gate.getUserConfig().getBoolean(DocumentEditor.class.getName() + ".restoreLayoutAutomatically"));
restoreLayoutAutomaticallyMenuItem.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// whenever the user checks/unchecks, update the config
config.put(DocumentEditor.class.getName() + ".restoreLayoutAutomatically", restoreLayoutAutomaticallyMenuItem.isSelected());
}
});
optionsMenu.add(restoreLayoutAutomaticallyMenuItem);
final JCheckBoxMenuItem readOnly = new JCheckBoxMenuItem("Read-only");
readOnly.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
config.put(GateConstants.DOCEDIT_READ_ONLY, readOnly.isSelected());
setEditable(!readOnly.isSelected());
}
});
readOnly.setSelected(config.getBoolean(GateConstants.DOCEDIT_READ_ONLY));
optionsMenu.addSeparator();
optionsMenu.add(readOnly);
// right to left orientation
final JCheckBoxMenuItem rightToLeftOrientation = new JCheckBoxMenuItem("Right To Left Orientation");
rightToLeftOrientation.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
config.put(GateConstants.DOC_RTOL_ORIENTATION, rightToLeftOrientation.isSelected());
setRightToLeftOrientation(rightToLeftOrientation.isSelected());
}
});
optionsMenu.addSeparator();
optionsMenu.add(rightToLeftOrientation);
ButtonGroup buttonGroup = new ButtonGroup();
final JRadioButtonMenuItem insertAppend = new JRadioButtonMenuItem("Insert Append");
buttonGroup.add(insertAppend);
insertAppend.setSelected(config.getBoolean(GateConstants.DOCEDIT_INSERT_APPEND));
insertAppend.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
config.put(GateConstants.DOCEDIT_INSERT_APPEND, insertAppend.isSelected());
}
});
optionsMenu.addSeparator();
optionsMenu.add(insertAppend);
final JRadioButtonMenuItem insertPrepend = new JRadioButtonMenuItem("Insert Prepend");
buttonGroup.add(insertPrepend);
insertPrepend.setSelected(config.getBoolean(GateConstants.DOCEDIT_INSERT_PREPEND));
insertPrepend.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
config.put(GateConstants.DOCEDIT_INSERT_PREPEND, insertPrepend.isSelected());
}
});
optionsMenu.add(insertPrepend);
// if none set then set the default one
if (!(insertAppend.isSelected() || insertPrepend.isSelected())) {
insertAppend.setSelected(true);
}
JMenuButton menuButton = new JMenuButton(optionsMenu);
menuButton.setIcon(MainFrame.getIcon("expanded"));
menuButton.setToolTipText("Options for the document editor");
// icon is not centred
menuButton.setMargin(new Insets(0, 0, 0, 1));
topBar.add(Box.createHorizontalGlue());
topBar.add(menuButton);
// when the editor is shown restore views if layout saving is enable
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (Gate.getUserConfig().getBoolean(DocumentEditor.class.getName() + ".restoreLayoutAutomatically")) {
restoreSettings();
}
}
});
validate();
}
use of gate.util.OptionsMap in project gate-core by GateNLP.
the class TestConfig method testConfigUpdating.
// readConfig
/**
* Test config loading
*/
// currently disabled as the files have moved on gate.ac.uk due to a change in svn structure
// which means the test can't read the files it needs and fails.
/*public void testConfigReading() throws Exception {
System.out.println("Reading GATE config from : " + new URL(TestDocument.getTestServerName()+"tests/gate.xml"));
readConfig(new URL(TestDocument.getTestServerName()+"tests/gate.xml"));
// check that we got the CREOLE dir entry; then remove it
// so it doesn't get accessed in other tests
CreoleRegister reg = Gate.getCreoleRegister();
Set dirs = reg.getDirectories();
assertTrue(
"CREOLE register doesn't contain URL from test gate.xml",
dirs != null && ! dirs.isEmpty() &&
dirs.contains(new URL("http://gate.ac.uk/tests/"))
);
// we should have a GATECONFIG entry on Gate
String fullSizeKeyName = "FULLSIZE";
String fullSizeValueName = "yes";
Map gateConfig = Gate.getUserConfig();
assertNotNull("no gate config map", gateConfig);
String fullSizeValue = (String) gateConfig.get(fullSizeKeyName);
assertNotNull("no full size value", fullSizeValue);
assertEquals(
"incorrect config data from tests/gate.xml",
fullSizeValueName, fullSizeValue
);
// clear the gate config for subsequent tests
gateConfig.clear();
// the code below is removed after serial controller stopped
// being a PR. the XML config scripting of runnable systems isn't
// working anyhow. when/if it does work, appropriate tests should be
// re-added here
// // get a test system
// ResourceData controllerResData =
// (ResourceData) reg.get("gate.creole.SerialController");
// assertNotNull("no resdata for serial controller", controllerResData);
// ProcessingResource controller =
// (ProcessingResource) controllerResData.getInstantiations().pop();
// assertNotNull("no controller instance", controller);
//
// // try running the system
// controller.execute();
} // testConfigReading()
*/
/**
* Test config updating
*/
@Test
public void testConfigUpdating() throws Exception {
// in the resources instead of relying on the running user
if (Gate.getUserConfigFile() == null)
return;
// clear the gate config so we don't write values from the
// system initialisation into the test file
OptionsMap configMap = Gate.getUserConfig();
configMap.clear();
// if user config file exists, save it and remember the name
// String configName = Gate.getUserConfigFileName();
// File userConfigFile = new File(configName);
File userConfigFile = Gate.getUserConfigFile();
String configName = userConfigFile.getAbsolutePath();
File savedConfigFile = null;
if (userConfigFile.exists()) {
if (DEBUG) {
Out.prln(userConfigFile);
Out.prln("can write: " + userConfigFile.canWrite());
}
String userConfigDirectory = userConfigFile.getParent();
if (userConfigDirectory == null)
userConfigDirectory = "";
savedConfigFile = new File(userConfigDirectory + Strings.getFileSep() + "__saved_gate.xml__for_TestConfig__" + System.currentTimeMillis());
if (DEBUG)
Out.prln(savedConfigFile);
boolean renamed = userConfigFile.renameTo(savedConfigFile);
assertTrue("rename failed", renamed);
}
assertTrue("user config file still there", !userConfigFile.exists());
// call Gate.writeConfig - check it creates an empty config file
// this is no longer a valid test as the written user config will at least
// contain the values for the known and autload plugin paths.
Gate.writeUserConfig();
@SuppressWarnings("unused") String writtenConfig = Files.getString(new File(configName));
@SuppressWarnings("unused") String empty = Gate.getEmptyConfigFile();
// assertEquals("written config doesn't match", writtenConfig, empty);
// set some config attributes via Gate.getConfigData
configMap.put("A", "1");
configMap.put("B", "2");
// call Gate.writeConfig, delete the config data from Gate's map,
// read the config file and check that the new data is present
Gate.writeUserConfig();
configMap.clear();
readConfig(userConfigFile.toURI().toURL());
// reinstante saved user config file if not null
userConfigFile.delete();
if (savedConfigFile != null) {
savedConfigFile.renameTo(userConfigFile);
}
}
use of gate.util.OptionsMap in project gate-core by GateNLP.
the class XmlDocumentHandler method startDocument.
// XmlDocumentHandler()/
/**
* This method is called when the SAX parser encounts the beginning of the
* XML document.
*/
@Override
public void startDocument() throws org.xml.sax.SAXException {
// init of variables in the parent
super.startDocument();
/**
* We will attempt to add namespace feature info to each namespaced element
* only if three parameters are set in the global or local config file:
* ADD_NAMESPACE_FEATURES: boolean flag
* ELEMENT_NAMESPACE_URI: feature name to use to hold namespace uri
* ELEMENT_NAMESPACE_PREFIX: feature name to use to hold namespace prefix
*/
OptionsMap configData = Gate.getUserConfig();
boolean addNSFeature = Boolean.parseBoolean((String) configData.get(GateConstants.ADD_NAMESPACE_FEATURES));
namespaceURIFeature = (String) configData.get(GateConstants.ELEMENT_NAMESPACE_URI);
namespacePrefixFeature = (String) configData.get(GateConstants.ELEMENT_NAMESPACE_PREFIX);
deserializeNamespaceInfo = (addNSFeature && namespacePrefixFeature != null && !namespacePrefixFeature.isEmpty() && namespaceURIFeature != null && !namespaceURIFeature.isEmpty());
}
Aggregations