Search in sources :

Example 6 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FiltersCustomizerController method initGui.

private void initGui() {
    panel = new FiltersCustomizerPanel();
    panel.filtersTable.setDefaultRenderer(String.class, new FilterFormatCellRenderer());
    panel.filtersTable.getSelectionModel().addListSelectionListener(e -> {
        if (e.getValueIsAdjusting()) {
            return;
        }
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        if (lsm.isSelectionEmpty()) {
            panel.editButton.setEnabled(false);
            panel.optionsButton.setEnabled(false);
        } else {
            panel.editButton.setEnabled(true);
            Filter currFilter = getFilterAtRow(panel.filtersTable.getSelectedRow());
            IFilter f = FilterMaster.getFilterInstance(currFilter.getClassName());
            panel.optionsButton.setEnabled(f != null && f.hasOptions());
        }
    });
    panel.filtersTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent me) {
            if (me.getClickCount() == 2 && me.getButton() == MouseEvent.BUTTON1) {
                doEdit(panel.filtersTable.rowAtPoint(me.getPoint()));
            }
        }
    });
    TableColumnSizer.autoSize(panel.filtersTable, 0, true);
    if (!isProjectSpecific) {
        panel.projectSpecificCB.setVisible(false);
    }
    panel.projectSpecificCB.addActionListener(e -> updateEnabledness());
    panel.cbRemoveTags.addActionListener(e -> editableFilters.setRemoveTags(panel.cbRemoveTags.isSelected()));
    panel.cbRemoveSpacesNonseg.addActionListener(e -> editableFilters.setRemoveSpacesNonseg(panel.cbRemoveSpacesNonseg.isSelected()));
    panel.cbPreserveSpaces.addActionListener(e -> editableFilters.setPreserveSpaces(panel.cbPreserveSpaces.isSelected()));
    panel.cbIgnoreFileContext.addActionListener(e -> editableFilters.setIgnoreFileContext(panel.cbIgnoreFileContext.isSelected()));
    panel.optionsButton.addActionListener(e -> {
        Filter currFilter = getFilterAtRow(panel.filtersTable.getSelectedRow());
        IFilter f = FilterMaster.getFilterInstance(currFilter.getClassName());
        if (f != null) {
            // new options handling
            Map<String, String> newConfig = f.changeOptions(SwingUtilities.windowForComponent(panel), FilterMaster.forFilter(currFilter.getOption()));
            if (newConfig != null) {
                FilterMaster.setOptions(currFilter, newConfig);
            }
        }
    });
    panel.editButton.addActionListener(e -> doEdit(panel.filtersTable.getSelectedRow()));
    if (Core.getProject().isProjectLoaded()) {
        inUseFilters = Core.getProject().getProjectFiles().stream().map(info -> info.filterFileFormatName).collect(Collectors.toSet());
    }
}
Also used : MouseEvent(java.awt.event.MouseEvent) Filter(gen.core.filters.Filter) IFilter(org.omegat.filters2.IFilter) IFilter(org.omegat.filters2.IFilter) MouseAdapter(java.awt.event.MouseAdapter) ListSelectionModel(javax.swing.ListSelectionModel)

Example 7 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class Convert20to21 method convertFiltersConfig.

/**
 * Convert filters config ('filters.conf') into new format.
 *
 * @param fromFile
 *            old config file
 * @param toFile
 *            new config file
 * @throws Exception
 */
public static void convertFiltersConfig(final File fromFile, final File toFile) throws Exception {
    if (!fromFile.exists()) {
        return;
    }
    String c = read(fromFile);
    org.omegat.convert.v20to21.data.Filters filters;
    XMLDecoder xmldec = new XMLDecoder(new ByteArrayInputStream(c.getBytes("UTF-8")));
    try {
        filters = (org.omegat.convert.v20to21.data.Filters) xmldec.readObject();
    } finally {
        xmldec.close();
    }
    Filters res = new Filters();
    for (org.omegat.convert.v20to21.data.OneFilter f : filters.getFilter()) {
        Filter fo = new Filter();
        res.getFilters().add(fo);
        fo.setClassName(f.getClassName());
        fo.setEnabled(f.isOn());
        for (org.omegat.convert.v20to21.data.Instance i : f.getInstance()) {
            Files io = new Files();
            fo.getFiles().add(io);
            io.setSourceFilenameMask(i.getSourceFilenameMask());
            io.setTargetFilenamePattern(i.getTargetFilenamePattern());
            io.setSourceEncoding(i.getSourceEncoding());
            io.setTargetEncoding(i.getTargetEncoding());
        }
        Serializable opts = f.getOptions();
        if (opts != null) {
            BeanInfo bi = Introspector.getBeanInfo(opts.getClass());
            for (PropertyDescriptor prop : bi.getPropertyDescriptors()) {
                if ("class".equals(prop.getName())) {
                    continue;
                }
                Object value = prop.getReadMethod().invoke(opts);
                Filter.Option op = new Filter.Option();
                op.setName(prop.getName());
                op.setValue(value.toString());
                fo.getOption().add(op);
            }
        }
    }
    convertTextFilter(res);
    convertHTMLFilter2(res);
    JAXBContext ctx = JAXBContext.newInstance(Filters.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(res, toFile);
}
Also used : Serializable(java.io.Serializable) Marshaller(javax.xml.bind.Marshaller) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) JAXBContext(javax.xml.bind.JAXBContext) Filters(gen.core.filters.Filters) ByteArrayInputStream(java.io.ByteArrayInputStream) Filter(gen.core.filters.Filter) XMLDecoder(java.beans.XMLDecoder) Files(gen.core.filters.Files)

Example 8 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method getDefaultSettingsFromFilter.

/**
 * Create default filter's config.
 *
 * @param filterClassname
 *            filter's classname
 * @return default filter's config
 */
public static Filter getDefaultSettingsFromFilter(final String filterClassname) {
    IFilter f = getFilterInstance(filterClassname);
    if (f == null) {
        return null;
    }
    Filter fc = new Filter();
    fc.setClassName(f.getClass().getName());
    fc.setEnabled(true);
    for (Instance ins : f.getDefaultInstances()) {
        Files ff = new Files();
        ff.setSourceEncoding(ins.getSourceEncoding());
        ff.setSourceFilenameMask(ins.getSourceFilenameMask());
        ff.setTargetEncoding(ins.getTargetEncoding());
        ff.setTargetFilenamePattern(ins.getTargetFilenamePattern());
        fc.getFiles().add(ff);
    }
    return fc;
}
Also used : IFilter(org.omegat.filters2.IFilter) IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) Instance(org.omegat.filters2.Instance) Files(gen.core.filters.Files)

Example 9 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method cloneConfig.

/**
 * Clone config for editing
 *
 * @return new config instance
 */
public static Filters cloneConfig(Filters orig) {
    Filters c = new Filters();
    c.setRemoveTags(orig.isRemoveTags());
    c.setRemoveSpacesNonseg(orig.isRemoveSpacesNonseg());
    c.setPreserveSpaces(orig.isPreserveSpaces());
    c.setIgnoreFileContext(orig.isIgnoreFileContext());
    for (Filter f : orig.getFilters()) {
        c.getFilters().add(cloneFilter(f));
    }
    return c;
}
Also used : Filters(gen.core.filters.Filters) IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter)

Example 10 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method setOptions.

/**
 * Convert options to xml from map.
 *
 * @param f
 *            filter
 * @param newOptions
 *            options
 */
public static void setOptions(Filter f, Map<String, String> newOptions) {
    f.getOption().clear();
    for (Map.Entry<String, String> en : newOptions.entrySet()) {
        Filter.Option opt = new Filter.Option();
        opt.setName(en.getKey());
        opt.setValue(en.getValue());
        f.getOption().add(opt);
    }
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) Option(gen.core.filters.Filter.Option) Option(gen.core.filters.Filter.Option) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

Filter (gen.core.filters.Filter)10 IFilter (org.omegat.filters2.IFilter)9 AbstractFilter (org.omegat.filters2.AbstractFilter)7 Files (gen.core.filters.Files)5 Option (gen.core.filters.Filter.Option)2 Filters (gen.core.filters.Filters)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 XMLDecoder (java.beans.XMLDecoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ListSelectionModel (javax.swing.ListSelectionModel)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 FilterContext (org.omegat.filters2.FilterContext)1 Instance (org.omegat.filters2.Instance)1