tok) {
List l = new ArrayList();
for (int i = 0; i < l(tok); i++) {
String t = tok.get(i);
if (odd(i) && eq(t, "{")) {
int j = findEndOfCurlyBracketPart(tok, i);
l.add(joinSubList(tok, i, j));
i = j - 1;
} else
l.add(t);
}
return l;
}
public static boolean startsWithAndEndsWith(String s, String prefix, String suffix) {
return startsWith(s, prefix) && endsWith(s, suffix);
}
public static boolean containsNewLines(String s) {
return containsNewLine(s);
}
public static String jlabel_textAsHTML_center(String text) {
return "" + replace(htmlencode(text), "\n", "
") + "
";
}
public static A popLast(List l) {
return liftLast(l);
}
public static int findEndOfCurlyBracketPart(List cnc, int i) {
int j = i + 2, level = 1;
while (j < cnc.size()) {
if (eq(cnc.get(j), "{"))
++level;
else if (eq(cnc.get(j), "}"))
--level;
if (level == 0)
return j + 1;
++j;
}
return cnc.size();
}
public static boolean containsNewLine(String s) {
return contains(s, '\n');
}
public static List replace(List l, A a, A b) {
for (int i = 0; i < l(l); i++) if (eq(l.get(i), a))
l.set(i, b);
return l;
}
public static String replace(String s, String a, String b) {
return s == null ? null : a == null || b == null ? s : s.replace(a, b);
}
public static String replace(String s, char a, char b) {
return s == null ? null : s.replace(a, b);
}
public static String htmlencode(Object o) {
return htmlencode(str(o));
}
public static String htmlencode(String s) {
if (s == null)
return "";
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
int cp = s.codePointAt(i);
out.append("");
out.append(intToHex_flexLength(cp));
out.append(';');
i += Character.charCount(cp) - 1;
} else
out.append(c);
}
return out.toString();
}
public static A liftLast(List l) {
if (l.isEmpty())
return null;
int i = l(l) - 1;
A a = l.get(i);
l.remove(i);
return a;
}
public static String intToHex_flexLength(int i) {
return Integer.toHexString(i);
}
public static class Var implements IVar {
public Var() {
}
public Var(A v) {
this.v = v;
}
public A v;
public synchronized void set(A a) {
if (v != a) {
v = a;
notifyAll();
}
}
public synchronized A get() {
return v;
}
public synchronized boolean has() {
return v != null;
}
public synchronized void clear() {
v = null;
}
public String toString() {
return str(get());
}
}
public static interface IVF1 {
public void get(A a);
}
public abstract static class LiveValue {
public abstract Class getType();
public abstract A get();
public abstract void onChange(Runnable l);
public abstract void removeOnChangeListener(Runnable l);
public void onChangeAndNow(Runnable l) {
onChange(l);
callF(l);
}
}
public static class Pair implements Comparable> {
public A a;
public B b;
public Pair() {
}
public Pair(A a, B b) {
this.b = b;
this.a = a;
}
public int hashCode() {
return hashCodeFor(a) + 2 * hashCodeFor(b);
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Pair))
return false;
Pair t = (Pair) o;
return eq(a, t.a) && eq(b, t.b);
}
public String toString() {
return "<" + a + ", " + b + ">";
}
public int compareTo(Pair p) {
if (p == null)
return 1;
int i = ((Comparable) a).compareTo(p.a);
if (i != 0)
return i;
return ((Comparable) b).compareTo(p.b);
}
}
public static interface IVar {
public void set(A a);
public A get();
public boolean has();
public void clear();
}
public static java.awt.Color colorFromHex(String hex) {
return awtColor(hex);
}
public static void vm_cleanPrints() {
print_preprocess = new F1() {
public String get(String x) {
try {
return vm_cleanForPrint(x);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "vm_cleanForPrint(x)";
}
};
}
public static boolean logModuleOutput_installed = false;
public static void logModuleOutput() {
if (logModuleOutput_installed)
return;
logModuleOutput_installed = true;
final File logFile = getProgramFile("output.txt");
final LineBuffer buf = new LineBuffer(new VF1() {
public void get(String line) {
try {
appendToTextFile(logFile, "[" + localDateWithMilliseconds() + "] " + line + "\n");
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "appendToTextFile(logFile, \"[\" + localDateWithMilliseconds() + \"] \" + line + \"...";
}
});
print_forAllThreads(new VF1() {
public void get(String s) {
try {
buf.append(s);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "buf.append(s);";
}
});
}
public static JDA discordBot(final VF1 onMessage) {
return discordBot(new ListenerAdapter() {
@Override
public void onMessageReceived(MessageReceivedEvent e) {
if (e.getAuthor().isBot()) {
print(" Msg from bot, skipping");
return;
}
pcallF(onMessage, e);
}
});
}
public static JDA discordBot(ListenerAdapter listener, Object... __) {
try {
JDABuilder builder = new JDABuilder(AccountType.BOT);
String token = stringPar("token", __);
if (empty(token)) {
File tokenFile = javaxSecretDir("discord-bot-token");
token = assertNempty("Need: " + tokenFile, trim(loadTextFile(tokenFile)));
}
builder.setToken(token);
builder.addEventListener(listener);
JDA jda = builder.build();
jda.awaitReady();
print("Started Discord bot (token: " + shorten(token, 7) + ")");
return jda;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Object dm_discord_lineForMsgID_unimported(long msgID) {
return dm_call(dm_gazelle_linesCRUD(), "lineForID", msgID);
}
public static Object[] litmapparams(Object... l) {
return new Object[] { litmap(l) };
}
public static DynModule dm_me() {
return dm_current_mandatory();
}
public static long rcall_long(String method, Object o, Object... args) {
return toLong(rcall(method, o, args));
}
public static Object rcall(String method, Object o, Object... args) {
return call_withVarargs(o, method, args);
}
public static AutoCloseable dm_registerAs(String... msgs) {
F0 f = new F0() {
public String get() {
try {
return dm_moduleID();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret dm_moduleID();";
}
};
return combineAutoCloseables(map(msgs, msg -> dm_vmBus_answerToMessage(msg, f)));
}
public static AutoCloseable dm_vmBus_answerToMessage(String msg, final F0 f) {
final DynModule m = dm_current_mandatory();
return dm_ownResource(vmBus_addListener_basic(msg, new F2() {
public Object get(String _msg, Object arg) {
try {
AutoCloseable __85 = m.enter();
try {
return callF(f);
} finally {
_close(__85);
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "temp m.enter();\r\n ret callF(f);";
}
}));
}
public static AutoCloseable dm_vmBus_answerToMessage(String msg, final F1 f) {
final DynModule m = dm_current_mandatory();
return dm_ownResource(vmBus_addListener_basic(msg, new F2() {
public Object get(String _msg, Object arg) {
try {
AutoCloseable __86 = m.enter();
try {
return callF(f, arg);
} finally {
_close(__86);
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "temp m.enter();\r\n ret callF(f, arg);";
}
}));
}
public static String jda_selfUserName(JDA jda) {
return jda_selfUser(jda).getName();
}
public static long jda_selfUserID(JDA jda) {
return jda.getSelfUser().getIdLong();
}
public static String dm_gazelle_linesCRUD() {
return dm_require("#1021628/GLines");
}
public static Object[] litobjectarray(Object... l) {
return litObjectArray(l);
}
public static String getChannel() {
return getChannelName();
}
public static String shortenForDiscord(String msg) {
return shorten(msg, 2000);
}
public static String gazelle_postedLinesCRUD() {
return dm_require("#1022603/GPostedLines");
}
public static long dm_discord_channelID(String name) {
return (long) dm_call(dm_gazelle_linesCRUD(), "getChannelID", name);
}
public static Object dm_pcall(Object moduleOrID, String method, Object... args) {
try {
return dm_call(moduleOrID, method, args);
} catch (Throwable __e) {
return null;
}
}
public static String absoluteURL(String url) {
return isAbsoluteURL(url) ? url : "http://" + url;
}
public static String dm_discord_startKeepAliveModule(Object mod) {
String discordToken = (String) (dm_get(mod, "discordToken"));
return dm_showNewModuleWithParams("#1024196/DiscordKeepAlive", "discordToken", discordToken);
}
public static JPanel centerAndSouthWithMargins(Component c, Component s) {
return applyDefaultMargin(centerAndSouth(c, withTopMargin(s)));
}
public static JPanel jrightalignedline(Component... components) {
return jrightAlignedLine(components);
}
public static JPanel jrightalignedline(List components) {
return jrightAlignedLine(components);
}
public static JCheckBox dm_fieldCheckBox(String field) {
return dm_fieldCheckBox(humanizeFormLabel(field), field);
}
public static JCheckBox dm_fieldCheckBox(String text, String field) {
if (isIdentifier(text) && !isIdentifier(field)) {
String temp = field;
field = text;
text = temp;
}
return jLiveValueCheckBox(text, dm_fieldLiveValue(field));
}
public static void onChange(JSpinner spinner, Object r) {
spinner.addChangeListener(changeListener(r));
}
public static A onChange(A b, Object r) {
b.addItemListener(itemListener(r));
return b;
}
public static void onChange(JTextComponent tc, Object r) {
onUpdate(tc, r);
}
public static void onChange(final JSlider slider, final Object r) {
{
swing(new Runnable() {
public void run() {
try {
slider.addChangeListener(changeListener(r));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "slider.addChangeListener(changeListener(r));";
}
});
}
}
public static void onChange(JComboBox cb, final Object r) {
if (isEditableComboBox(cb))
onChange(textFieldFromComboBox(cb), r);
else
onSelectedItem(cb, new VF1() {
public void get(String s) {
try {
callF(r);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "callF(r)";
}
});
}
public static int hashCodeFor(Object a) {
return a == null ? 0 : a.hashCode();
}
public static java.awt.Color awtColor(String hex) {
byte[] b = bytesFromHex(dropPrefix("#", hex));
return new Color(ubyteToInt(b[0]), ubyteToInt(b[1]), ubyteToInt(b[2]));
}
public static String vm_cleanForPrint(Object o) {
String s = str(o);
return or((String) vmBus_query("cleanForPrint", s), s);
}
public static File getProgramFile(String progID, String fileName) {
if (new File(fileName).isAbsolute())
return new File(fileName);
return new File(getProgramDir(progID), fileName);
}
public static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
public static void appendToTextFile(File file, String s) {
appendToFile(file, s);
}
public static void appendToTextFile(String file, String s) {
appendToTextFile(programFile(file), s);
}
public static String localDateWithMilliseconds(Date time) {
return localDateWithMilliseconds(time.getTime());
}
public static String localDateWithMilliseconds(long time) {
SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS");
return format.format(time);
}
public static String localDateWithMilliseconds() {
return localDateWithMilliseconds(now());
}
public static void print_forAllThreads(Object f) {
assertNull("todo", print_allThreads);
print_allThreads = f;
}
public static String stringPar(Object[] params, String name) {
return stringOptPar(params, name);
}
public static String stringPar(String name, Object[] params) {
return stringOptPar(params, name);
}
public static String stringPar(String name, Map params) {
return (String) optPar(name, params);
}
public static String stringPar(String name, Object[] params, String defaultValue) {
return optPar(name, params, defaultValue);
}
public static A assertNempty(A a) {
return assertNempty("empty", a);
}
public static A assertNempty(String msg, A a) {
if (empty(a))
throw fail(msg + ": " + a);
return a;
}
public static int shorten_default = 100;
public static String shorten(String s) {
return shorten(s, shorten_default);
}
public static String shorten(String s, int max) {
return shorten(s, max, "...");
}
public static String shorten(String s, int max, String shortener) {
if (s == null)
return "";
if (max < 0)
return s;
return s.length() <= max ? s : substring(s, 0, min(s.length(), max - l(shortener))) + shortener;
}
public static String shorten(int max, String s) {
return shorten(s, max);
}
public static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
public static void litmap_impl(Map map, Object... x) {
if (x != null)
for (int i = 0; i < x.length - 1; i += 2) if (x[i + 1] != null)
map.put(x[i], x[i + 1]);
}
public static String dm_moduleID(Object module) {
return strOrNull(getOpt(dm_getStem(module), "id"));
}
public static String dm_moduleID() {
return dm_moduleID(dm_current_mandatory_generic());
}
public static AutoCloseable combineAutoCloseables(final AutoCloseable a, final AutoCloseable b) {
return a == null ? b : b == null ? a : new AutoCloseable() {
public String toString() {
return "a.close(); b.close();";
}
public void close() throws Exception {
a.close();
b.close();
}
};
}
public static AutoCloseable combineAutoCloseables(AutoCloseable a, AutoCloseable b, AutoCloseable c, AutoCloseable... more) {
return combineAutoCloseables(concatLists(ll(a, b, c), asList(more)));
}
public static AutoCloseable combineAutoCloseables(Iterable l) {
return foldl(new F2() {
public AutoCloseable get(AutoCloseable a, AutoCloseable b) {
try {
return combineAutoCloseables(a, b);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "combineAutoCloseables(a,b)";
}
}, null, l);
}
public static AutoCloseable vmBus_addListener_basic(String msg, Object listener) {
Map map = vm_busListenersByMessage_live();
synchronized (map) {
Set listeners = map.get(msg);
if (listeners == null)
map.put(msg, listeners = syncIdentityHashSet());
return tempAdd(listeners, listener);
}
}
public static SelfUser jda_selfUser(JDA jda) {
return jda.getSelfUser();
}
public static String dm_require(String moduleLibID) {
return dm_makeModule(moduleLibID);
}
public static Object[] litObjectArray(Object... l) {
return l;
}
public static String getChannelName() {
return (String) callOpt(getMainBot(), "getChannelName");
}
public static boolean isAbsoluteURL(String s) {
return contains(s, "://");
}
public static Object dm_get(Object moduleOrID, String field) {
return get(dm_getModule(moduleOrID), field);
}
public static Object dm_get(String field, long moduleID) {
return dm_get(moduleID, field);
}
public static A applyDefaultMargin(final A c) {
if (c != null) {
swing(new Runnable() {
public void run() {
try {
c.setBorder(BorderFactory.createEmptyBorder(withMargin_defaultWidth, withMargin_defaultWidth, withMargin_defaultWidth, withMargin_defaultWidth));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "c.setBorder(BorderFactory.createEmptyBorder(withMargin_defaultWidth, withMarg...";
}
});
}
return c;
}
public static JPanel centerAndSouth(final Component c, final Component s) {
return swing(new F0() {
public JPanel get() {
try {
JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.CENTER, wrap(c));
if (s != null)
panel.add(BorderLayout.SOUTH, wrap(s));
return panel;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "JPanel panel = new JPanel(new BorderLayout);\r\n panel.add(BorderLayout.CENT...";
}
});
}
public static int withTopMargin_defaultWidth = 6;
public static JPanel withTopMargin(Component c) {
return withTopMargin(withTopMargin_defaultWidth, c);
}
public static JPanel withTopMargin(final int w, final Component c) {
return swing(new F0() {
public JPanel get() {
try {
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(w, 0, 0, 0));
p.add(c);
return p;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat...";
}
});
}
public static JPanel jrightAlignedLine(final Component... components) {
return swing(new F0() {
public RightAlignedLine get() {
try {
return new RightAlignedLine(components);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret RightAlignedLine(components);";
}
});
}
public static JPanel jrightAlignedLine(List extends Component> components) {
return jrightAlignedLine(asArray(Component.class, components));
}
public static Map humanizeFormLabel_replacements = litmap("id", "ID", "md5", "MD5");
public static String humanizeFormLabel(String s) {
if (containsSpace(s))
return s;
return firstToUpper(joinWithSpace(replaceElementsUsingMap(splitCamelCase(s), humanizeFormLabel_replacements)).replace("I D", "ID"));
}
public static JCheckBox jLiveValueCheckBox(String text, final SimpleLiveValue lv) {
final JCheckBox cb = jCheckBox(text);
bindCheckBoxToLiveValue(cb, lv);
return cb;
}
public static SimpleLiveValue dm_fieldLiveValue(String fieldName) {
return dm_fieldLiveValue(dm_current_mandatory(), fieldName);
}
public static SimpleLiveValue dm_fieldLiveValue(final DynModule module, final String fieldName) {
Lock __17 = module.lock;
lock(__17);
try {
Class type = getFieldType(module, fieldName);
final SimpleLiveValue value = new SimpleLiveValue(type, get(module, fieldName));
module.onChange(new Runnable() {
public void run() {
try {
Object o = get(module, fieldName);
value.set(o);
;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ifdef dm_fieldLiveValue_debug\r\n print(\"dm_fieldLiveValue: setting \" + fi...";
}
});
value.onChange(new Runnable() {
public void run() {
try {
module.setField(fieldName, value.get());
;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ifdef dm_fieldLiveValue_debug\r\n print(\"dm_fieldLiveValue: setting 2 \" + ...";
}
});
return value;
} finally {
unlock(__17);
}
}
public static ChangeListener changeListener(final Object r) {
return new ChangeListener() {
public void stateChanged(ChangeEvent e) {
pcallF(r);
}
};
}
public static ItemListener itemListener(final Object r) {
return new ItemListener() {
public void itemStateChanged(ItemEvent e) {
pcallF(r);
}
};
}
public static void onUpdate(JComponent c, final Object r) {
if (c instanceof JTextComponent)
((JTextComponent) c).getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
call(r);
}
public void removeUpdate(DocumentEvent e) {
call(r);
}
public void changedUpdate(DocumentEvent e) {
call(r);
}
});
else if (c instanceof ItemSelectable)
((ItemSelectable) c).addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
call(r);
}
});
else
print("Warning: onUpdate doesn't know " + getClassName(c));
}
public static void onUpdate(List extends JComponent> l, Object r) {
for (JComponent c : l) onUpdate(c, r);
}
public static boolean isEditableComboBox(final JComboBox cb) {
return cb != null && swing(new F0() {
public Boolean get() {
try {
return cb.isEditable();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret cb.isEditable();";
}
});
}
public static JTextField textFieldFromComboBox(JComboBox cb) {
return (JTextField) cb.getEditor().getEditorComponent();
}
public static JComboBox onSelectedItem(final JComboBox cb, final VF1 f) {
addActionListener(cb, new Runnable() {
public void run() {
try {
pcallF(f, selectedItem(cb));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "pcallF(f, selectedItem(cb))";
}
});
return cb;
}
public static int withMargin_defaultWidth = 6;
public static JPanel withMargin(Component c) {
return withMargin(withMargin_defaultWidth, c);
}
public static JPanel withMargin(int w, Component c) {
return withMargin(w, w, c);
}
public static JPanel withMargin(int w, int h, Component c) {
return withMargin(w, h, w, h, c);
}
public static JPanel withMargin(final int top, final int left, final int bottom, final int right, final Component c) {
return swing(new F0() {
public JPanel get() {
try {
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
p.add(c);
return p;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat...";
}
});
}
public static byte[] bytesFromHex(String s) {
return hexToBytes(s);
}
public static int ubyteToInt(byte b) {
return b & 0x0FF;
}
public static Lock appendToFile_lock = lock();
public static boolean appendToFile_keepOpen = false;
public static HashMap appendToFile_writers = new HashMap();
public static void appendToFile(String path, String s) {
try {
Lock __1098 = appendToFile_lock;
lock(__1098);
try {
mkdirsForFile(new File(path));
path = getCanonicalPath(path);
Writer writer = appendToFile_writers.get(path);
if (writer == null) {
writer = new BufferedWriter(new OutputStreamWriter(newFileOutputStream(path, true), "UTF-8"));
if (appendToFile_keepOpen)
appendToFile_writers.put(path, writer);
}
writer.write(s);
if (!appendToFile_keepOpen)
writer.close();
} finally {
unlock(__1098);
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static void appendToFile(File path, String s) {
if (path != null)
appendToFile(path.getPath(), s);
}
public static void cleanMeUp_appendToFile() {
AutoCloseable __1101 = tempCleaningUp();
try {
Lock __1100 = appendToFile_lock;
lock(__1100);
try {
closeAllWriters(values(appendToFile_writers));
appendToFile_writers.clear();
} finally {
unlock(__1100);
}
} finally {
_close(__1101);
}
}
public static File programFile(String name) {
return prepareProgramFile(name);
}
public static File programFile(String progID, String name) {
return prepareProgramFile(progID, name);
}
public static SimpleDateFormat simpleDateFormat_local(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTimeZone());
return sdf;
}
public static A assertNull(A a) {
assertTrue(a == null);
return a;
}
public static A assertNull(String msg, A a) {
assertTrue(msg, a == null);
return a;
}
public static String stringOptPar(Object[] params, String name) {
return (String) optPar(params, name);
}
public static A optPar(ThreadLocal tl, A defaultValue) {
A a = tl.get();
if (a != null) {
tl.set(null);
return a;
}
return defaultValue;
}
public static A optPar(ThreadLocal tl) {
return optPar(tl, null);
}
public static Object optPar(Object[] params, String name) {
return optParam(params, name);
}
public static Object optPar(String name, Object[] params) {
return optParam(params, name);
}
public static Object optPar(String name, Map params) {
return optParam(name, params);
}
public static A optPar(Object[] params, String name, A defaultValue) {
return optParam(params, name, defaultValue);
}
public static A optPar(String name, Object[] params, A defaultValue) {
return optParam(params, name, defaultValue);
}
public static String strOrNull(Object o) {
return o == null ? null : str(o);
}
public static A foldl(F2 f, A seed, Iterable l) {
A a = seed;
if (l != null)
for (B b : l) a = callF(f, a, b);
return a;
}
public static String dm_makeModule(String moduleLibID) {
return (String) dm_callOS("makeModule", moduleLibID);
}
public static Object mainBot;
public static Object getMainBot() {
return mainBot;
}
public static JComponent wrap(Object swingable) {
return _recordNewSwingComponent(wrap_2(swingable));
}
public static JComponent wrap_2(Object swingable) {
if (swingable == null)
return null;
JComponent c;
if (swingable instanceof Component)
c = componentToJComponent(((Component) swingable));
else
c = componentToJComponent((Component) callOpt(swingable, "swing"));
if (c instanceof JTable || c instanceof JList || c instanceof JTextArea || c instanceof JEditorPane || c instanceof JTextPane || c instanceof JTree)
return jscroll(c);
return c == null ? jlabel(str(swingable)) : c;
}
public static boolean containsSpace(String s) {
return containsSpaces(s);
}
public static String firstToUpper(String s) {
if (empty(s))
return s;
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
public static List replaceElementsUsingMap(Iterable l, final Map map) {
return map(l, new F1() {
public A get(A a) {
try {
return getOrKeep(map, a);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "getOrKeep(map, a)";
}
});
}
public static List splitCamelCase(String s) {
return ai_splitCamelCase(s);
}
public static JCheckBox jCheckBox() {
return swingNu(JCheckBox.class);
}
public static JCheckBox jCheckBox(boolean checked) {
return swingNu(JCheckBox.class, "", checked);
}
public static JCheckBox jCheckBox(String text, boolean checked) {
return swingNu(JCheckBox.class, text, checked);
}
public static JCheckBox jCheckBox(String text) {
return swingNu(JCheckBox.class, text);
}
public static JCheckBox jCheckBox(String text, boolean checked, final Object onChange) {
JCheckBox cb = jCheckBox(checked, onChange);
cb.setText(text);
return cb;
}
public static JCheckBox jCheckBox(boolean checked, final Object onChange) {
final JCheckBox cb = jCheckBox(checked);
cb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
pcallF(onChange, cb.isSelected());
}
});
return cb;
}
public static A bindCheckBoxToLiveValue(final A cb, final SimpleLiveValue lv) {
bindLiveValueListenerToComponent(cb, lv, new Runnable() {
public void run() {
try {
setChecked(cb, isTrue(lv.get()));
;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ifdef bindCheckBoxToLiveValue_debug\r\n print(\"bindCheckBoxToLiveValue: se...";
}
});
onChange(cb, new Runnable() {
public void run() {
try {
lv.set(isChecked(cb));
;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ifdef bindCheckBoxToLiveValue_debug\r\n print(\"bindCheckBoxToLiveValue: se...";
}
});
return cb;
}
public static Class getFieldType(Object o, String field) {
return fieldType(o, field);
}
public static void addActionListener(JTextField tf, final Runnable action) {
onEnter(tf, action);
}
public static void addActionListener(final JComboBox cb, final Runnable action) {
if (cb != null) {
swing(new Runnable() {
public void run() {
try {
cb.addActionListener(actionListener(action));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "cb.addActionListener(actionListener(action));";
}
});
}
}
public static void addActionListener(final AbstractButton b, final Runnable action) {
if (b != null) {
swing(new Runnable() {
public void run() {
try {
b.addActionListener(actionListener(action));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "b.addActionListener(actionListener(action));";
}
});
}
}
public static String selectedItem(JList l) {
return getSelectedItem(l);
}
public static String selectedItem(JComboBox cb) {
return getSelectedItem(cb);
}
public static byte[] hexToBytes(String s) {
if (odd(l(s)))
throw fail("Hex string has odd length: " + quote(shorten(10, s)));
int n = l(s) / 2;
byte[] bytes = new byte[n];
for (int i = 0; i < n; i++) {
int a = parseHexChar(s.charAt(i * 2));
int b = parseHexChar(s.charAt(i * 2 + 1));
if (a < 0 || b < 0)
throw fail("Bad hex byte: " + quote(substring(s, i * 2, i * 2 + 2)) + " at " + i * 2 + "/" + l(s));
bytes[i] = (byte) ((a << 4) | b);
}
return bytes;
}
public static String getCanonicalPath(File f) {
try {
return f == null ? null : f.getCanonicalPath();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static String getCanonicalPath(String path) {
return getCanonicalPath(newFile(path));
}
public static AutoCloseable tempCleaningUp() {
return tempSetTL(ping_isCleanUpThread, true);
}
public static void closeAllWriters(Collection extends Writer> l) {
for (Writer w : unnull(l)) {
try {
w.close();
} catch (Throwable __e) {
_handleException(__e);
}
}
}
public static Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
public static File prepareProgramFile(String name) {
return mkdirsForFile(getProgramFile(name));
}
public static File prepareProgramFile(String progID, String name) {
return mkdirsForFile(getProgramFile(progID, name));
}
public static TimeZone localTimeZone() {
return getTimeZone(standardTimeZone());
}
public static A optParam(ThreadLocal tl, A defaultValue) {
return optPar(tl, defaultValue);
}
public static A optParam(ThreadLocal tl) {
return optPar(tl);
}
public static Object optParam(String name, Map params) {
return mapGet(params, name);
}
public static A optParam(Object[] opt, String name, A defaultValue) {
int n = l(opt);
if (n == 1 && opt[0] instanceof Map) {
Map map = (Map) (opt[0]);
return map.containsKey(name) ? (A) map.get(name) : defaultValue;
}
if (!even(l(opt)))
throw fail("Odd parameter length");
for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name))
return (A) opt[i + 1];
return defaultValue;
}
public static Object optParam(Object[] opt, String name) {
return optParam(opt, name, null);
}
public static A _recordNewSwingComponent(A c) {
if (c != null)
callF((Object) vm_generalMap_get("newSwingComponentRegistry"), (Object) c);
return c;
}
public static JComponent componentToJComponent(Component c) {
if (c instanceof JComponent)
return (JComponent) c;
if (c instanceof JFrame)
return ((JFrame) c).getRootPane();
if (c == null)
return null;
throw fail("boohoo " + getClassName(c));
}
public static JScrollPane jscroll(final Component c) {
return swing(new F0() {
public JScrollPane get() {
try {
return new JScrollPane(c);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret new JScrollPane(c);";
}
});
}
public static JLabel jlabel(final String text) {
return swingConstruct(BetterLabel.class, text);
}
public static JLabel jlabel() {
return jlabel(" ");
}
public static boolean containsSpaces(String s) {
return indexOf(s, ' ') >= 0;
}
public static A getOrKeep(Map map, A a) {
if (map == null)
return a;
A v = map.get(a);
return v != null ? v : a;
}
public static List ai_splitCamelCase(String s) {
int j = 0;
List l = new ArrayList();
if (isAllUpperCase(s)) {
l.add(s);
return l;
}
for (int i = 0; i < l(s); i++) if (isUpperCaseLetter(s.charAt(i)) && i > j) {
l.add(substring(s, j, i));
j = i;
}
if (j < l(s))
l.add(substring(s, j));
return l;
}
public static A swingNu(final Class c, final Object... args) {
return swingConstruct(c, args);
}
public static void setChecked(final JCheckBox checkBox, final boolean b) {
if (checkBox != null) {
swing(new Runnable() {
public void run() {
try {
if (isChecked(checkBox) != b)
checkBox.setSelected(b);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "if (isChecked(checkBox) != b)\r\n checkBox.setSelected(b);";
}
});
}
}
public static boolean isChecked(final JCheckBox checkBox) {
return checkBox != null && (boolean) swing(new F0() {
public Boolean get() {
try {
return checkBox.isSelected();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret checkBox.isSelected();";
}
});
}
public static boolean isChecked(final JCheckBoxMenuItem mi) {
return mi != null && (boolean) swing(new F0() {
public Boolean get() {
try {
return mi.isSelected();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret mi.isSelected();";
}
});
}
public static JTextField onEnter(final JTextField tf, final Object action) {
if (action == null || tf == null)
return tf;
tf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent _evt) {
try {
tf.selectAll();
callF(action);
} catch (Throwable __e) {
messageBox(__e);
}
}
});
return tf;
}
public static JButton onEnter(JButton btn, final Object action) {
if (action == null || btn == null)
return btn;
btn.addActionListener(actionListener(action));
return btn;
}
public static JList onEnter(JList list, Object action) {
list.addKeyListener(enterKeyListener(rCallOnSelectedListItem(list, action)));
return list;
}
public static JComboBox onEnter(final JComboBox cb, final Object action) {
{
swing(new Runnable() {
public void run() {
try {
if (cb.isEditable()) {
JTextField text = (JTextField) cb.getEditor().getEditorComponent();
onEnter(text, action);
} else {
cb.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter");
cb.getActionMap().put("enter", abstractAction("", new Runnable() {
public void run() {
try {
cb.hidePopup();
callF(action);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "cb.hidePopup(); callF(action);";
}
}));
}
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "if (cb.isEditable()) {\r\n JTextField text = (JTextField) cb.getEditor().g...";
}
});
}
return cb;
}
public static JTable onEnter(final JTable table, final Object action) {
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
table.getActionMap().put("Enter", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
callF(action, table.getSelectedRow());
}
});
return table;
}
public static JTextField onEnter(Object action, JTextField tf) {
return onEnter(tf, action);
}
public static String getSelectedItem(JList l) {
return (String) l.getSelectedValue();
}
public static String getSelectedItem(JComboBox cb) {
return strOrNull(cb.getSelectedItem());
}
public static int parseHexChar(char c) {
if (c >= '0' && c <= '9')
return charDiff(c, '0');
if (c >= 'a' && c <= 'f')
return charDiff(c, 'a') + 10;
if (c >= 'A' && c <= 'F')
return charDiff(c, 'A') + 10;
return -1;
}
public static AutoCloseable tempSetTL(ThreadLocal tl, A a) {
return tempSetThreadLocal(tl, a);
}
public static TimeZone getTimeZone(String name) {
return TimeZone.getTimeZone(name);
}
public static String standardTimeZone_name = "Europe/Berlin";
public static String standardTimeZone() {
return standardTimeZone_name;
}
public static B mapGet(Map map, A a) {
return map == null || a == null ? null : map.get(a);
}
public static B mapGet(A a, Map map) {
return map == null || a == null ? null : map.get(a);
}
public static boolean even(int i) {
return (i & 1) == 0;
}
public static boolean even(long i) {
return (i & 1) == 0;
}
public static A swingConstruct(final Class c, final Object... args) {
return swing(new F0() {
public A get() {
try {
return nuObject(c, args);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "ret nuObject(c, args);";
}
});
}
public static boolean isAllUpperCase(String s) {
return hasLettersAllUpperCase(s);
}
public static boolean isUpperCaseLetter(char c) {
return Character.isUpperCase(c);
}
public static KeyListener enterKeyListener(final Object action) {
return new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
pcallF(action);
}
};
}
public static Runnable rCallOnSelectedListItem(final JList list, final Object action) {
return new Runnable() {
public void run() {
try {
pcallF(action, getSelectedItem(list));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "pcallF(action, getSelectedItem(list))";
}
};
}
public static AbstractAction abstractAction(String name, final Object runnable) {
return new AbstractAction(name) {
public void actionPerformed(ActionEvent evt) {
pcallF(runnable);
}
};
}
public static int charDiff(char a, char b) {
return (int) a - (int) b;
}
public static int charDiff(String a, char b) {
return charDiff(stringToChar(a), b);
}
public static Object nuObject(String className, Object... args) {
try {
return nuObject(classForName(className), args);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static A nuObject(Class c, Object... args) {
try {
if (args.length == 0)
return nuObjectWithoutArguments(c);
Constructor m = nuObject_findConstructor(c, args);
m.setAccessible(true);
return (A) m.newInstance(args);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Constructor nuObject_findConstructor(Class c, Object... args) {
for (Constructor m : c.getDeclaredConstructors()) {
if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
continue;
return m;
}
throw fail("Constructor " + c.getName() + getClasses(args) + " not found" + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}
public static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
public static boolean hasLettersAllUpperCase(String s) {
return hasLetters(s) && !containsLowerCase(s);
}
public static char stringToChar(String s) {
if (l(s) != 1)
throw fail("bad stringToChar: " + s);
return firstChar(s);
}
public static Map classForName_cache = synchroHashMap();
public static Class classForName(String name) {
try {
if (classForName_cache == null)
return Class.forName(name);
Class c = classForName_cache.get(name);
if (c == null)
classForName_cache.put(name, c = Class.forName(name));
return c;
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Map nuObjectWithoutArguments_cache = newDangerousWeakHashMap();
public static Object nuObjectWithoutArguments(String className) {
try {
return nuObjectWithoutArguments(classForName(className));
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static A nuObjectWithoutArguments(Class c) {
try {
if (nuObjectWithoutArguments_cache == null)
return (A) nuObjectWithoutArguments_findConstructor(c).newInstance();
Constructor m = nuObjectWithoutArguments_cache.get(c);
if (m == null)
nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c));
return (A) m.newInstance();
} catch (Exception __e) {
throw rethrow(__e);
}
}
public static Constructor nuObjectWithoutArguments_findConstructor(Class c) {
for (Constructor m : c.getDeclaredConstructors()) if (empty(m.getParameterTypes())) {
m.setAccessible(true);
return m;
}
throw fail("No default constructor found in " + c.getName());
}
public static List getClasses(Object[] array) {
List l = new ArrayList();
for (Object o : array) l.add(_getClass(o));
return l;
}
public static boolean hasLetters(String s) {
for (int i = 0; i < s.length(); i++) if (Character.isLetter(s.charAt(i)))
return true;
return false;
}
public static boolean containsLowerCase(String s) {
for (int i = 0; i < l(s); i++) if (isLowerCase(s.charAt(i)))
return true;
return false;
}
public static char firstChar(String s) {
return s.charAt(0);
}
public static boolean isLowerCase(char c) {
return Character.isLowerCase(c);
}
public static class SimpleLiveValue extends LiveValue {
public Class type;
public volatile A value;
public transient List onChange = synchroList();
public SimpleLiveValue(Class type) {
this.type = type;
}
public SimpleLiveValue(Class type, A value) {
this.value = value;
this.type = type;
}
public Class getType() {
return type;
}
public A get() {
return value;
}
public void onChange(Runnable l) {
onChange.add(l);
}
public void onChangeAndNow(Runnable l) {
onChange(l);
callF(l);
}
public void removeOnChangeListener(Runnable l) {
onChange.remove(l);
}
public void fireChanged() {
pcallFAll(onChange);
}
public void set(A a) {
if (neq(value, a)) {
value = a;
fireChanged();
}
}
}
public static class LineBuffer {
public VF1 onLine;
public StringBuilder currentLine = new StringBuilder();
public LineBuffer() {
}
public LineBuffer(VF1 onLine) {
this.onLine = onLine;
}
public void append(String s) {
append(s, onLine);
}
public void append(String s, VF1 onLine) {
currentLine.append(s);
if (contains(s, '\n')) {
int i = 0, j;
s = str(currentLine);
while ((j = indexOf(s, i, '\n')) >= 0) {
String line = dropTrailingBackslashR(substring(s, i, j));
callF(onLine, line);
i = j + 1;
}
currentLine = new StringBuilder(substring(s, i));
}
}
public int size() {
return l(currentLine);
}
}
public abstract static class F2 {
public abstract C get(A a, B b);
}
public static class RightAlignedLine extends JPanel {
public RightAlignedLine(Component... components) {
setLayout(LetterLayout.rightAlignedRow());
for (Component component : components) add(component);
}
public void add(String text) {
add(new JLabel(text));
}
}
public static class LetterLayout implements LayoutManager {
public String[] lines;
public Map map = new TreeMap();
public RC[] rows;
public RC[] cols;
public Cell[][] cells;
public int spacingX = 10, spacingY = 10;
public int insetTop, insetBottom, insetLeft, insetRight;
public int template;
public boolean formWideLeftSide, formWideRightSide;
public static final int STALACTITE = 1, LEFT_ALIGNED_ROW = 2, CENTERED_ROW = 3, FORM = 4, RIGHT_ALIGNED_ROW = 5;
public boolean debug = false;
public void setLeftBorder(int border) {
insetLeft = border;
}
public void setRightBorder(int border) {
insetRight = border;
}
public static JComponent withBorder(JComponent component, int border) {
JPanel panel = new JPanel(new LetterLayout("C").setBorder(border));
panel.add("C", component);
return panel;
}
public static JPanel panel(String... lines) {
return new JPanel(new LetterLayout(lines));
}
public static JPanel stalactitePanel() {
return new JPanel(stalactite());
}
public static class DummyComponent extends JComponent {
}
public static class Cell {
public boolean aux = false;
public int minWidth, minHeight;
public Component component;
public int colspan, rowspan;
public double weightX, weightY;
}
public static class RC {
public int min;
public double weightSum;
public int start;
public int minEnd;
}
public LetterLayout(int template) {
this.template = template;
}
public LetterLayout(String... lines) {
this.lines = lines;
}
public void removeLayoutComponent(Component component) {
map.values().remove(component);
}
public void layoutContainer(Container container) {
prepareLayout(container);
if (debug)
System.out.println("Container size: " + container.getSize());
Insets insets = getInsets(container);
for (int r = 0; r < rows.length; r++) {
for (int i = 0; i < cols.length; ) {
Cell cell = cells[i][r];
if (cell.aux)
++i;
else {
if (cell.component != null) {
int x1 = cols[i].start;
int y1 = rows[r].start;
int x2 = i + cell.colspan < cols.length ? cols[i + cell.colspan].start - spacingX : container.getWidth() - insets.right;
int y2 = r + cell.rowspan < rows.length ? rows[r + cell.rowspan].start - spacingY : container.getHeight() - insets.bottom;
if (debug)
System.out.println("Layouting (" + i + ", " + r + ", " + cell.component.getClass().getName() + "): " + x1 + " " + y1 + " " + x2 + " " + y2);
cell.component.setBounds(x1, y1, x2 - x1, y2 - y1);
}
i += cells[i][r].colspan;
}
}
}
}
public final void prepareLayout(Container container) {
applyTemplate(container);
int numRows = lines.length, numCols = lines[0].length();
for (int i = 1; i < numRows; i++) if (lines[i].length() != numCols)
throw new IllegalArgumentException("Lines have varying length");
cells = new Cell[numCols][numRows];
rows = new RC[numRows];
cols = new RC[numCols];
for (int r = 0; r < numRows; r++) rows[r] = new RC();
for (int i = 0; i < numCols; i++) cols[i] = new RC();
for (int r = 0; r < numRows; r++) for (int i = 0; i < numCols; i++) cells[i][r] = new Cell();
for (int r = 0; r < numRows; r++) {
String line = lines[r];
for (int i = 0; i < numCols; ) {
Cell cell = cells[i][r];
if (cell.aux) {
++i;
continue;
}
char ch = line.charAt(i);
int iNext = i;
do ++iNext; while (iNext < numCols && ch == line.charAt(iNext));
int rNext = r;
do ++rNext; while (rNext < numRows && ch == lines[rNext].charAt(i));
cell.weightX = numCols == 1 || iNext > i + 1 ? 1.0 : 0.0;
cell.weightY = numRows == 1 || rNext > r + 1 ? 1.0 : 0.0;
Component c = map.get(String.valueOf(ch));
cell.component = c;
if (c != null) {
cell.minWidth = c.getMinimumSize().width + spacingX;
cell.minHeight = getMinimumHeight(c) + spacingY;
}
cell.colspan = iNext - i;
cell.rowspan = rNext - r;
if (cell.colspan == 1)
cols[i].min = Math.max(cols[i].min, cell.minWidth);
if (cell.rowspan == 1)
rows[r].min = Math.max(rows[r].min, cell.minHeight);
for (int r2 = r; r2 < rNext; r2++) for (int i2 = i; i2 < iNext; i2++) if (r2 != r || i2 != i)
cells[i2][r2].aux = true;
i = iNext;
}
}
while (true) {
for (int i = 0; i < numCols; i++) {
int minStart = i == 0 ? 0 : cols[i - 1].minEnd;
double weightStart = i == 0 ? 0.0 : cols[i - 1].weightSum;
for (int r = 0; r < numRows; r++) {
Cell cell = cells[i][r];
if (!cell.aux) {
RC rc = cols[i + cell.colspan - 1];
rc.minEnd = Math.max(rc.minEnd, minStart + cell.minWidth);
rc.weightSum = Math.max(rc.weightSum, weightStart + cell.weightX);
}
}
}
for (int r = 0; r < numRows; r++) {
int minStart = r == 0 ? 0 : rows[r - 1].minEnd;
double weightStart = r == 0 ? 0.0 : rows[r - 1].weightSum;
for (int i = 0; i < numCols; i++) {
Cell cell = cells[i][r];
if (!cell.aux) {
RC rc = rows[r + cell.rowspan - 1];
rc.minEnd = Math.max(rc.minEnd, minStart + cell.minHeight);
rc.weightSum = Math.max(rc.weightSum, weightStart + cell.weightY);
}
}
}
if (allWeightsZero(cols)) {
for (int r = 0; r < numRows; r++) for (int i = 0; i < numCols; i++) cells[i][r].weightX = 1.0;
continue;
}
if (allWeightsZero(rows)) {
for (int r = 0; r < numRows; r++) for (int i = 0; i < numCols; i++) cells[i][r].weightY = 1.0;
continue;
}
break;
}
Insets insets = getInsets(container);
determineStarts(cols, insets.left, container.getWidth() - insets.left - insets.right + spacingX, spacingX);
determineStarts(rows, insets.top, container.getHeight() - insets.top - insets.bottom + spacingY, spacingY);
}
public final boolean allWeightsZero(RC[] rcs) {
for (int i = 0; i < rcs.length; i++) if (rcs[i].weightSum != 0.0)
return false;
return true;
}
public static int getMinimumHeight(Component c) {
return c.getMinimumSize().height;
}
public final void applyTemplate(Container container) {
if (template == STALACTITE) {
Component[] components = container.getComponents();
lines = new String[components.length + 2];
map.clear();
for (int i = 0; i < components.length; i++) {
String s = String.valueOf(makeIndexChar(i));
map.put(s, components[i]);
lines[i] = s;
}
lines[components.length] = lines[components.length + 1] = " ";
} else if (template == FORM) {
Component[] components = container.getComponents();
int numRows = components.length / 2;
lines = new String[numRows + 2];
map.clear();
for (int row = 0; row < numRows; row++) {
String lower = String.valueOf(makeIndexChar(row));
String upper = String.valueOf(makeAlternateIndexChar(row));
Component rightComponent = components[row * 2 + 1];
if (rightComponent instanceof DummyComponent)
upper = lower;
lines[row] = (formWideLeftSide ? lower + lower : lower) + (formWideRightSide ? upper + upper : upper);
map.put(lower, components[row * 2]);
if (!(rightComponent instanceof DummyComponent))
map.put(upper, rightComponent);
}
lines[numRows] = lines[numRows + 1] = (formWideLeftSide ? " " : " ") + (formWideRightSide ? " " : " ");
} else if (template == LEFT_ALIGNED_ROW) {
lines = new String[] { makeSingleRow(container) + RIGHT_CHAR + RIGHT_CHAR };
} else if (template == CENTERED_ROW) {
lines = new String[] { "" + LEFT_CHAR + LEFT_CHAR + makeSingleRow(container) + RIGHT_CHAR + RIGHT_CHAR };
} else if (template == RIGHT_ALIGNED_ROW) {
lines = new String[] { "" + LEFT_CHAR + LEFT_CHAR + makeSingleRow(container) };
}
}
public final String makeSingleRow(Container container) {
Component[] components = container.getComponents();
StringBuffer buf = new StringBuffer();
map.clear();
for (int i = 0; i < components.length; i++) {
String s = String.valueOf(makeAlternateIndexChar(i));
map.put(s, components[i]);
buf.append(s);
}
return buf.toString();
}
public static void determineStarts(RC[] rcs, int start, int totalSize, int spacing) {
int minTotal = rcs[rcs.length - 1].minEnd;
double weightSum = rcs[rcs.length - 1].weightSum;
int spare = (int) ((totalSize - minTotal) / (weightSum == 0.0 ? 1.0 : weightSum));
int x = start, minSum = 0;
double prevWeightSum = 0.0;
for (int i = 0; i < rcs.length; i++) {
int width = rcs[i].minEnd - minSum + (int) ((rcs[i].weightSum - prevWeightSum) * spare) - spacing;
rcs[i].start = x;
x += width + spacing;
prevWeightSum = rcs[i].weightSum;
minSum = rcs[i].minEnd;
}
}
public void addLayoutComponent(String s, Component component) {
map.put(s, component);
}
public Dimension minimumLayoutSize(Container container) {
prepareLayout(container);
Insets insets = getInsets(container);
Dimension result = new Dimension(insets.left + cols[cols.length - 1].minEnd + insets.right - spacingX, insets.top + rows[rows.length - 1].minEnd + insets.bottom - spacingY);
return result;
}
public final Insets getInsets(Container container) {
Insets insets = container.getInsets();
return new Insets(insets.top + insetTop, insets.left + insetLeft, insets.bottom + insetBottom, insets.right + insetRight);
}
public Dimension preferredLayoutSize(Container container) {
return minimumLayoutSize(container);
}
public LetterLayout setSpacing(int x, int y) {
spacingX = x;
spacingY = y;
return this;
}
public LetterLayout setSpacing(int spacing) {
return setSpacing(spacing, spacing);
}
public LetterLayout setBorder(int top, int left, int bottom, int right) {
insetTop = top;
insetLeft = left;
insetBottom = bottom;
insetRight = right;
return this;
}
public LetterLayout setBorder(int inset) {
return setBorder(inset, inset, inset, inset);
}
public LetterLayout setTopBorder(int inset) {
insetTop = inset;
return this;
}
public static LetterLayout stalactite() {
return new LetterLayout(STALACTITE);
}
public static LetterLayout leftAlignedRow() {
return new LetterLayout(LEFT_ALIGNED_ROW);
}
public static LetterLayout leftAlignedRow(int spacing) {
return leftAlignedRow().setSpacing(spacing);
}
public static LetterLayout centeredRow() {
return new LetterLayout(CENTERED_ROW);
}
public static LetterLayout rightAlignedRow() {
return new LetterLayout(RIGHT_ALIGNED_ROW);
}
public static JPanel rightAlignedRowPanel(JComponent... components) {
return makePanel(new LetterLayout(RIGHT_ALIGNED_ROW), components);
}
public static JPanel makePanel(LetterLayout letterLayout, JComponent[] components) {
JPanel panel = new JPanel(letterLayout);
for (JComponent component : components) {
panel.add(component);
}
return panel;
}
public static LetterLayout form() {
LetterLayout letterLayout = new LetterLayout(FORM);
letterLayout.formWideLeftSide = true;
letterLayout.formWideRightSide = true;
return letterLayout;
}
public static LetterLayout formWideRightSide() {
LetterLayout letterLayout = new LetterLayout(FORM);
letterLayout.formWideRightSide = true;
return letterLayout;
}
public static Component getDummyComponent() {
return new DummyComponent();
}
public static JPanel newPanel(String... lines) {
return new JPanel(new LetterLayout(lines));
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public static char makeIndexChar(int idx) {
return (char) ('a' + idx * 2);
}
public static char makeAlternateIndexChar(int idx) {
return (char) ('b' + idx * 2);
}
public static char LEFT_CHAR = ',', RIGHT_CHAR = '.';
public static void main(String[] args) {
System.out.println((int) makeIndexChar(0));
System.out.println((int) makeAlternateIndexChar(0));
System.out.println((int) makeIndexChar(32000));
System.out.println((int) makeAlternateIndexChar(32000));
System.out.println((int) LEFT_CHAR);
System.out.println((int) RIGHT_CHAR);
}
}
public static String dropTrailingBackslashR(String s) {
int i = l(s);
while (i > 0 && s.charAt(i - 1) == '\r') --i;
return substring(s, 0, i);
}
public static void add(BitSet bs, int i) {
bs.set(i);
}
public static boolean add(Collection c, A a) {
return c != null && c.add(a);
}
public static void add(Container c, Component x) {
addToContainer(c, x);
}
public static void addToContainer(final Container a, final Component b) {
if (a != null && b != null) {
swing(new Runnable() {
public void run() {
try {
a.add(b);
} catch (Exception __e) {
throw rethrow(__e);
}
}
public String toString() {
return "a.add(b);";
}
});
}
}
}