org.nuiton.jaxx.widgets.jformattedtextfield.JFormatterTextFieldInternalGroups Maven / Gradle / Ivy
package org.nuiton.jaxx.widgets.jformattedtextfield;
/*
* #%L
* JAXX :: Widgets
* %%
* Copyright (C) 2008 - 2024 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.ImmutableSet;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Created on 3/20/15.
*
* @author Tony Chemit - [email protected]
* @since 2.23
*/
public class JFormatterTextFieldInternalGroups implements Iterable {
/** Logger. */
private static final Logger log = LogManager.getLogger(JFormatterTextFieldInternalGroups.class);
public static JFormatterTextFieldInternalGroups create(String mask) {
// We need to double this caracter for MaskFormatter creation, but here we don't
// TODO See which other caracters must be especad by MaskFormatter
mask = mask.replaceAll("''", "'");
Set groups = new LinkedHashSet<>();
JFormatterTextFieldInternalGroup previousGroup = null;
int length = mask.length();
for (int i = 0; i < length; i++) {
char c = mask.charAt(i);
if (c != '*') {
int endIndex = i - 1;
int endGroupIndex = getGroupLastIndex(mask, i, length);
i = endGroupIndex + 1;
// new group detected
previousGroup = addGroup(groups, previousGroup, endIndex, endGroupIndex);
}
}
if (previousGroup != null && previousGroup.getEndGroupIndex() < length - 1) {
// adding the last remaining group
addGroup(groups, previousGroup, length, length);
}
return new JFormatterTextFieldInternalGroups(groups);
}
protected static int getGroupLastIndex(String mask, int i, int length) {
do {
char c2 = mask.charAt(i);
if (c2 == '*') {
break;
}
i++;
} while (i < length);
return i - 1;
}
protected static JFormatterTextFieldInternalGroup addGroup(Set componentPositions, JFormatterTextFieldInternalGroup previousGroup, int endIndex, int endSymbolIndex) {
boolean withPreviousGroup = previousGroup != null;
JFormatterTextFieldInternalGroup newGroup;
if (withPreviousGroup) {
newGroup = new JFormatterTextFieldInternalGroup(previousGroup.getEndGroupIndex() + 1, endIndex, endSymbolIndex);
} else {
newGroup = new JFormatterTextFieldInternalGroup(0, endIndex, endSymbolIndex);
}
if (log.isDebugEnabled()) {
log.debug(String.format("New component (%d): %d - %d - %d", componentPositions.size(), newGroup.getStartIndex(), newGroup.getEndIndex(), newGroup.getEndGroupIndex()));
}
componentPositions.add(newGroup);
if (withPreviousGroup) {
previousGroup.setNextGroup(newGroup);
newGroup.setPreviousGroup(previousGroup);
}
return newGroup;
}
private final Set componentPositions;
public JFormatterTextFieldInternalGroup getGroupAtPosition(int position) {
JFormatterTextFieldInternalGroup result = null;
for (JFormatterTextFieldInternalGroup componentPosition : this) {
if (componentPosition.containsPosition(position)) {
result = componentPosition;
break;
}
}
return result;
}
protected JFormatterTextFieldInternalGroups(Set componentPositions) {
this.componentPositions = ImmutableSet.copyOf(componentPositions);
}
@Override
public Iterator iterator() {
return componentPositions.iterator();
}
}