/** * Initialize the preferences system. This will load * * <ul> * <li>general user prefs * <li>user filter settings * <li>user segmentation settings * </ul> * * from existing files in {@link StaticUtils#getConfigDir()} (and others for general prefs; see * {@link #getPreferencesFile()}) and set things up to create them via {@link #save()} if they * don't yet exist. * * <p>When the preferences system is required but actual user preferences shouldn't be loaded or * altered (testing scenarios), use {@link TestPreferencesInitializer} methods or be sure to set * the config dir with {@link RuntimePreferences#setConfigDir(String)} before calling this method. */ public static synchronized void init() { if (didInit) { return; } didInit = true; File srxFile = new File(StaticUtils.getConfigDir(), SRX.CONF_SENTSEG); SRX srx = SRX.loadSRX(srxFile); if (srx == null) { srx = SRX.getDefault(); } m_srx = srx; File filtersFile = new File(StaticUtils.getConfigDir(), FilterMaster.FILE_FILTERS); Filters filters = null; try { filters = FilterMaster.loadConfig(filtersFile); } catch (Exception ex) { Log.log(ex); } if (filters == null) { filters = FilterMaster.createDefaultFiltersConfig(); } m_filters = filters; File loadFile = getPreferencesFile(); File saveFile = new File(StaticUtils.getConfigDir(), Preferences.FILE_PREFERENCES); m_preferences = new PreferencesImpl(new PreferencesXML(loadFile, saveFile)); }
/** Displays the filters setup dialog to allow customizing file filters in detail. */ public void optionsSetupFileFiltersMenuItemActionPerformed() { FiltersCustomizer dlg = new FiltersCustomizer( mainWindow, false, FilterMaster.createDefaultFiltersConfig(), FilterMaster.loadConfig(StaticUtils.getConfigDir()), null); dlg.setVisible(true); if (dlg.getReturnStatus() == FiltersCustomizer.RET_OK) { // saving config FilterMaster.saveConfig(dlg.result, StaticUtils.getConfigDir()); if (Core.getProject().isProjectLoaded()) { if (FilterMaster.loadConfig(Core.getProject().getProjectProperties().getProjectInternal()) != null) { // project specific filters are in place. No need to reload project when // non-project-specific filters are changed return; } // asking to reload a project int res = JOptionPane.showConfirmDialog( mainWindow, OStrings.getString("MW_REOPEN_QUESTION"), OStrings.getString("MW_REOPEN_TITLE"), JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) ProjectUICommands.projectReload(); } } }
/** * Gets the prefs file to use. Looks in these places in this order: * * <ol> * <li>omegat.prefs in config dir * <li>omegat.prefs in install dir (defaults supplied with local install) * </ol> */ private static File getPreferencesFile() { File prefsFile = new File(StaticUtils.getConfigDir(), FILE_PREFERENCES); if (prefsFile.exists()) { return prefsFile; } // If user prefs don't exist, fall back to defaults (possibly) bundled with OmegaT. prefsFile = new File(StaticUtils.installDir(), FILE_PREFERENCES); if (prefsFile.exists()) { return prefsFile; } return null; }
public void testParseCLICommand() { String cmd = " sort \"/path with/spaces in/it\" /path\\ with/escaped\\ spaces/" + " \"escape\\\"escape\" 'noescape\\'noescape'' \"noescape\\ noescape\"" + " C:\\windows\\path"; String[] args = StaticUtils.parseCLICommand(cmd); assertEquals("/path with/spaces in/it", args[1]); assertEquals("/path with/escaped spaces/", args[2]); assertEquals("escape\"escape", args[3]); assertEquals("noescape\\noescape", args[4]); assertEquals("noescape\\ noescape", args[5]); assertEquals("C:\\windows\\path", args[6]); assertEquals(args.length, 7); args = StaticUtils.parseCLICommand(" "); assertEquals(args[0], ""); assertEquals(args.length, 1); }
public void testInstallDir() { File installDir = new File(StaticUtils.installDir()); assertTrue(installDir.isDirectory()); for (String dir : new String[] {"src", "docs", "lib"}) { assertTrue(new File(installDir, dir).isDirectory()); } }
public void toolsSingleValidateTagsMenuItemActionPerformed() { String midName = Core.getEditor().getCurrentFile(); List<ErrorReport> stes = null; if (!StringUtil.isEmpty(midName)) { String sourcePattern = StaticUtils.escapeNonRegex(midName); stes = Core.getTagValidation().listInvalidTags(sourcePattern); } Core.getTagValidation().displayTagValidationErrors(stes, null); }
private void runQuickScript(int index) { if (m_quickScripts[index] == null) { logResult(OStrings.getString("SCW_NO_SCRIPT_SELECTED")); return; } logResult(StaticUtils.format(OStrings.getString("SCW_QUICK_RUN"), (index + 1))); ScriptItem scriptFile = new ScriptItem(new File(m_scriptsDirectory, m_quickScripts[index])); executeScriptFile(scriptFile, true); }
public static void setSRX(SRX newSrx) { SRX oldValue = m_srx; m_srx = newSrx; File srxFile = new File(StaticUtils.getConfigDir() + SRX.CONF_SENTSEG); try { SRX.saveTo(m_srx, srxFile); } catch (IOException ex) { ex.printStackTrace(); } m_propChangeSupport.firePropertyChange(Preferences.PROPERTY_SRX, oldValue, newSrx); }
public static void setFilters(Filters newFilters) { Filters oldValue = m_filters; m_filters = newFilters; File filtersFile = new File(StaticUtils.getConfigDir(), FilterMaster.FILE_FILTERS); try { FilterMaster.saveConfig(m_filters, filtersFile); } catch (IOException ex) { ex.printStackTrace(); } m_propChangeSupport.firePropertyChange(Preferences.PROPERTY_FILTERS, oldValue, newFilters); }
public static void projectRemote(String url) { String dir = url.replace("/", "_").replace(':', '_'); File projectDir = new File(StaticUtils.getConfigDir() + "/remoteProjects/" + dir); File projectFile = new File(projectDir, OConsts.FILE_PROJECT); byte[] data; try { projectDir.mkdirs(); data = WikiGet.getURLasByteArray(url); FileUtils.writeByteArrayToFile(projectFile, data); } catch (Exception ex) { Log.logErrorRB(ex, "TEAM_REMOTE_RETRIEVE_ERROR", url); Core.getMainWindow().displayErrorRB(ex, "TEAM_REMOTE_RETRIEVE_ERROR", url); return; } projectOpen(projectDir); }
/** Create current translated document. */ public void projectSingleCompileMenuItemActionPerformed() { String midName = Core.getEditor().getCurrentFile(); if (StringUtil.isEmpty(midName)) { return; } String sourcePattern = StaticUtils.escapeNonRegex(midName); if (Preferences.isPreference(Preferences.TAGS_VALID_REQUIRED)) { List<ErrorReport> stes = Core.getTagValidation().listInvalidTags(sourcePattern); if (stes != null) { Core.getTagValidation() .displayTagValidationErrors(stes, OStrings.getString("TF_MESSAGE_COMPILE")); return; } } ProjectUICommands.projectSingleCompile(sourcePattern); }
private void runScript() { if (m_currentScriptItem == null) { logResult(OStrings.getString("SCW_NO_SCRIPT_SELECTED")); return; } if (!m_currentScriptItem.canRead()) { logResult(OStrings.getString("SCW_CANNOT_READ_SCRIPT")); return; } m_txtResult.setText(""); logResult( StaticUtils.format( OStrings.getString("SCW_RUNNING_SCRIPT"), m_currentScriptItem.getAbsolutePath())); executeScriptFile(m_currentScriptItem, false); }
/** * Redefine some keys behavior. We can't use key listeners, because we have to make something * AFTER standard keys processing. */ @Override protected void processKeyEvent(KeyEvent e) { if (e.getID() != KeyEvent.KEY_PRESSED) { // key released super.processKeyEvent(e); return; } boolean processed = false; boolean mac = StaticUtils.onMacOSX(); Document3 doc = getOmDocument(); // non-standard processing if (isKey(e, KeyEvent.VK_TAB, 0)) { // press TAB when 'Use TAB to advance' if (controller.settings.isUseTabForAdvance()) { controller.nextEntry(); processed = true; } } else if (isKey(e, KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK)) { // press Shift+TAB when 'Use TAB to advance' if (controller.settings.isUseTabForAdvance()) { controller.prevEntry(); processed = true; } } else if (isKey(e, KeyEvent.VK_ENTER, 0)) { // press ENTER if (!controller.settings.isUseTabForAdvance()) { controller.nextEntry(); processed = true; } else { processed = true; } } else if ((!mac && isKey(e, KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_ENTER, KeyEvent.META_MASK))) { // press Ctrl+ENTER (Cmd+Enter for MacOS) if (!controller.settings.isUseTabForAdvance()) { controller.prevEntry(); processed = true; } } else if (isKey(e, KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK)) { // convert Shift+Enter event to straight enter key KeyEvent ke = new KeyEvent(e.getComponent(), e.getID(), e.getWhen(), 0, KeyEvent.VK_ENTER, '\n'); super.processKeyEvent(ke); processed = true; } else if ((!mac && isKey(e, KeyEvent.VK_A, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_A, KeyEvent.META_MASK))) { // handling Ctrl+A manually (Cmd+A for MacOS) setSelectionStart(doc.getTranslationStart()); setSelectionEnd(doc.getTranslationEnd()); processed = true; } else if (isKey(e, KeyEvent.VK_O, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK)) { // handle Ctrl+Shift+O - toggle orientation LTR-RTL controller.toggleOrientation(); processed = true; } else if ((!mac && isKey(e, KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_MASK))) { // handle Ctrl+Backspace (Alt+Backspace for MacOS) try { int offset = getCaretPosition(); int prevWord = Utilities.getPreviousWord(this, offset); int c = Math.max(prevWord, doc.getTranslationStart()); setSelectionStart(c); setSelectionEnd(offset); replaceSelection(""); processed = true; } catch (BadLocationException ex) { // do nothing } } else if ((!mac && isKey(e, KeyEvent.VK_DELETE, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_DELETE, KeyEvent.ALT_MASK))) { // handle Ctrl+Backspace (Alt+Delete for MacOS) try { int offset = getCaretPosition(); int nextWord = Utilities.getNextWord(this, offset); int c = Math.min(nextWord, doc.getTranslationEnd()); setSelectionStart(offset); setSelectionEnd(c); replaceSelection(""); processed = true; } catch (BadLocationException ex) { // do nothing } } else if ((!mac && isKey(e, KeyEvent.VK_PAGE_UP, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_PAGE_UP, KeyEvent.META_MASK))) { // Ctrl+PgUp - to the begin of document(Cmd+PgUp for MacOS) setCaretPosition(0); processed = true; } else if ((!mac && isKey(e, KeyEvent.VK_PAGE_DOWN, KeyEvent.CTRL_MASK)) || (mac && isKey(e, KeyEvent.VK_PAGE_DOWN, KeyEvent.META_MASK))) { // Ctrl+PgDn - to the end of document(Cmd+PgDn for MacOS) setCaretPosition(getOmDocument().getLength()); processed = true; } // leave standard processing if need if (processed) { e.consume(); } else { if ((e.getModifiers() & (KeyEvent.CTRL_MASK | KeyEvent.META_MASK | KeyEvent.ALT_MASK)) == 0) { // there is no Alt,Ctrl,Cmd keys, i.e. it's char if (e.getKeyCode() != KeyEvent.VK_SHIFT) { // it's not a single 'shift' press checkAndFixCaret(); } } super.processKeyEvent(e); } controller.showLengthMessage(); // some after-processing catches if (!processed && e.getKeyChar() != 0) { switch (e.getKeyCode()) { case KeyEvent.VK_HOME: case KeyEvent.VK_END: case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: checkAndFixCaret(); } } }
public static ProjectProperties loadProjectProperties(File projectDir) throws IOException, TranslationException { ProjectProperties result = new ProjectProperties(projectDir); File inFile = new File(projectDir, OConsts.FILE_PROJECT); XMLStreamReader m_reader = new XMLStreamReader(); m_reader.killEmptyBlocks(); m_reader.setStream(inFile.getAbsolutePath(), "UTF-8"); // verify valid project file XMLBlock blk; List<XMLBlock> lst; // advance to omegat tag if (m_reader.advanceToTag("omegat") == null) return result; // advance to project tag if ((blk = m_reader.advanceToTag("project")) == null) return result; String ver = blk.getAttribute("version"); if (ver != null && !ver.equals(OConsts.PROJ_CUR_VERSION)) { throw new TranslationException( StaticUtils.format( OStrings.getString("PFR_ERROR_UNSUPPORTED_PROJECT_VERSION"), new Object[] {ver})); } // if folder is in default locations, name stored as __DEFAULT__ String m_root = inFile.getParentFile().getAbsolutePath() + File.separator; lst = m_reader.closeBlock(blk); if (lst == null) return result; for (int i = 0; i < lst.size(); i++) { blk = lst.get(i); if (blk.isClose()) continue; if (blk.getTagName().equals("target_dir")) { if (++i >= lst.size()) break; blk = lst.get(i); result.setTargetRoot(computeAbsolutePath(m_root, blk.getText(), OConsts.DEFAULT_TARGET)); } else if (blk.getTagName().equals("source_dir")) { if (++i >= lst.size()) break; blk = lst.get(i); result.setSourceRoot(computeAbsolutePath(m_root, blk.getText(), OConsts.DEFAULT_SOURCE)); } else if (blk.getTagName().equals("tm_dir")) { if (++i >= lst.size()) break; blk = lst.get(i); result.setTMRoot(computeAbsolutePath(m_root, blk.getText(), OConsts.DEFAULT_TM)); } else if (blk.getTagName().equals("glossary_dir")) { if (++i >= lst.size()) break; blk = lst.get(i); result.setGlossaryRoot( computeAbsolutePath(m_root, blk.getText(), OConsts.DEFAULT_GLOSSARY)); } else if (blk.getTagName().equals("dictionary_dir")) { if (++i >= lst.size()) break; blk = lst.get(i); result.setDictRoot(computeAbsolutePath(m_root, blk.getText(), OConsts.DEFAULT_DICT)); } else if (blk.getTagName().equals("source_lang")) { if (++i >= lst.size()) break; blk = lst.get(i); if (blk != null) result.setSourceLanguage(blk.getText()); } else if (blk.getTagName().equals("target_lang")) { if (++i >= lst.size()) break; blk = lst.get(i); if (blk != null) result.setTargetLanguage(blk.getText()); } else if (blk.getTagName().equals("sentence_seg")) { if (++i >= lst.size()) break; blk = lst.get(i); if (blk != null) result.setSentenceSegmentingEnabled(Boolean.parseBoolean(blk.getText())); } } return result; }
public void testGlobToRegex() { assertTrue(Pattern.matches(StaticUtils.globToRegex("ab?d", false), "abcd")); assertFalse(Pattern.matches(StaticUtils.globToRegex("ab?d", false), "abd")); assertTrue(Pattern.matches(StaticUtils.globToRegex("ab*d", false), "abcccccd")); assertTrue(Pattern.matches(StaticUtils.globToRegex("ab*d", false), "abd")); assertFalse(Pattern.matches(StaticUtils.globToRegex("ab*d", false), "abde")); assertTrue(Pattern.matches(StaticUtils.globToRegex("ab*", false), "abdefg")); assertTrue( Pattern.matches(StaticUtils.globToRegex("$a[b-c]!?*d{}", false), "$a[b-c]!?1234d{}")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a?", false), "a b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a ?", false), "a b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a*", false), "a b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a* b", false), "a b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a* b", true), "a b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a*b", false), "a b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a*b", false), "a\u00A0b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a*b", true), "a\u00A0b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a b", false), "a b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a b", true), "a b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a b", false), "a\u00A0b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a b", true), "a\u00A0b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a *", false), "a\u00A0b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a *", true), "a\u00A0b")); assertFalse(Pattern.matches(StaticUtils.globToRegex("a ?", false), "a\u00A0b")); assertTrue(Pattern.matches(StaticUtils.globToRegex("a ?", true), "a\u00A0b")); }
public void optionsAccessConfigDirMenuItemActionPerformed() { openFile(new File(StaticUtils.getConfigDir())); }