Modifying section addition to show an option list

Also adding form handler class that I forgot to commit earlier.
This commit is contained in:
str4d
2012-06-07 04:42:52 +00:00
parent 84344b6789
commit 3a75f8d7d1
3 changed files with 174 additions and 13 deletions

View File

@ -0,0 +1,135 @@
package net.i2p.router.web;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import net.i2p.data.DataHelper;
/**
* Simple summary bar configuration.
*
* @since 0.9.1
*/
public class ConfigSummaryHandler extends FormHandler {
private Map _settings;
@Override
protected void processForm() {
if (_action == null) return;
String group = getJettyString("group");
boolean deleting = _action.equals(_("Delete selected"));
boolean adding = _action.equals(_("Add item"));
boolean saving = _action.equals(_("Save order"));
if (_action.equals(_("Save")) && "0".equals(group)) {
try {
int refreshInterval = Integer.parseInt(getJettyString("refreshInterval"));
if (refreshInterval >= CSSHelper.MIN_REFRESH) {
_context.router().saveConfig(CSSHelper.PROP_REFRESH, "" + refreshInterval);
addFormNotice(_("Refresh interval changed"));
} else
addFormError(_("Refresh interval must be at least {0} seconds", CSSHelper.MIN_REFRESH));
} catch (java.lang.NumberFormatException e) {
addFormError(_("Refresh interval must be a number"));
return;
}
} else if ("1".equals(group)) {
if (_action.equals(_("Use full preset"))) {
_context.router().saveConfig(SummaryHelper.PROP_SUMMARYBAR, SummaryHelper.PRESET_FULL);
addFormNotice(_("Full summary bar preset selected.") +
_("Summary bar will refresh shortly."));
} else if (_action.equals(_("Use reduced preset"))) {
_context.router().saveConfig(SummaryHelper.PROP_SUMMARYBAR, SummaryHelper.PRESET_SHORT);
addFormNotice(_("Reduced summary bar preset selected.") +
_("Summary bar will refresh shortly."));
} else {
addFormError(_("Unsupported"));
}
} else if (adding || deleting || saving) {
Map<Integer, String> sections = new TreeMap<Integer, String>();
for (Object o : _settings.keySet()) {
if (!(o instanceof String))
continue;
String k = (String) o;
if (!k.startsWith("order_"))
continue;
String v = getJettyString(k);
k = k.substring(6);
k = k.substring(k.indexOf('_') + 1);
try {
int order = Integer.parseInt(v);
sections.put(order, k);
} catch (java.lang.NumberFormatException e) {
addFormError(_("Order must be an integer"));
return;
}
}
if (adding) {
String name = getJettyString("name");
if (name == null || name.length() <= 0) {
addFormError(_("No section selected"));
return;
}
String order = getJettyString("order");
if (order == null || order.length() <= 0) {
addFormError(_("No order entered"));
return;
}
name = DataHelper.escapeHTML(name).replace(",", "&#44;");
order = DataHelper.escapeHTML(order).replace(",", "&#44;");
try {
int ki = Integer.parseInt(order);
sections.put(ki, name);
addFormNotice(_("Added") + ": " + name);
} catch (java.lang.NumberFormatException e) {
addFormError(_("Order must be an integer"));
return;
}
} else if (deleting) {
Set<Integer> toDelete = new HashSet();
for (Object o : _settings.keySet()) {
if (!(o instanceof String))
continue;
String k = (String) o;
if (!k.startsWith("delete_"))
continue;
k = k.substring(7);
try {
int ki = Integer.parseInt(k);
toDelete.add(ki);
} catch (java.lang.NumberFormatException e) {
continue;
}
}
for (Iterator<Integer> iter = sections.keySet().iterator(); iter.hasNext(); ) {
int i = iter.next();
if (toDelete.contains(i)) {
String removedName = sections.get(i);
iter.remove();
addFormNotice(_("Removed") + ": " + removedName);
}
}
}
SummaryHelper.saveSummaryBarSections(_context, sections);
addFormError(_("Saved order of sections.") +
_("Summary bar will refresh shortly."));
} else {
addFormError(_("Unsupported"));
}
}
public void setSettings(Map settings) { _settings = new HashMap(settings); }
/** curses Jetty for returning arrays */
private String getJettyString(String key) {
String[] arr = (String[]) _settings.get(key);
if (arr == null)
return null;
return arr[0].trim();
}
}

View File

@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.i2p.data.DataHelper;
@ -40,6 +41,10 @@ public class SummaryBarRenderer {
}
ALL_SECTIONS = Collections.unmodifiableMap(aMap);
}*/
static final String ALL_SECTIONS[] =
{"HelpAndFAQ", "I2PServices", "I2PInternals", "General", "ShortGeneral", "NetworkReachability",
"UpdateStatus", "RestartStatus", "Peers", "FirewallAndReseedStatus", "Bandwidth", "Tunnels",
"Congestion", "TunnelStatus", "Destinations", "NewsHeadings" };
private final RouterContext _context;
private final SummaryHelper _helper;
@ -55,8 +60,8 @@ public class SummaryBarRenderer {
*/
public void renderSummaryHTML(Writer out) throws IOException {
StringBuilder buf = new StringBuilder(8*1024);
String[] sections = _helper.getSummaryBarSections();
for (int i = 0; i < sections.length; i++) {
List<String> sections = _helper.getSummaryBarSections();
for (String section : sections) {
// Commented out because broken. Replaced by if-elseif blob below.
/*try {
String section = (String)ALL_SECTIONS.get(sections[i]).invoke(this);
@ -68,8 +73,6 @@ public class SummaryBarRenderer {
}*/
buf.setLength(0);
String section = sections[i];
buf.append("<hr>\n");
if ("HelpAndFAQ".equals(section))
buf.append(renderHelpAndFAQHTML());

View File

@ -4,11 +4,13 @@ import java.io.IOException;
import java.text.Collator;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
@ -745,9 +747,9 @@ public class SummaryHelper extends HelperBase {
public void storeNewsHelper(NewsHelper n) { _newshelper = n; }
public NewsHelper getNewsHelper() { return _newshelper; }
public String[] getSummaryBarSections() {
public List<String> getSummaryBarSections() {
String config = _context.getProperty(PROP_SUMMARYBAR, PRESET_FULL);
return config.split("" + S);
return Arrays.asList(config.split("" + S));
}
static void saveSummaryBarSections(RouterContext ctx, Map<Integer, String> sections) {
@ -782,8 +784,17 @@ public class SummaryHelper extends HelperBase {
public String getRequestURI() { return _requestURI; }
public String getConfigTable() {
String[] sections = getSummaryBarSections();
StringBuilder buf = new StringBuilder(1024);
String[] allSections = SummaryBarRenderer.ALL_SECTIONS;
List<String> sections = getSummaryBarSections();
TreeSet<String> sortedSections = new TreeSet();
for (int i = 0; i < allSections.length; i++) {
String section = allSections[i];
if (!sections.contains(section))
sortedSections.add(section);
}
StringBuilder buf = new StringBuilder(2048);
buf.append("<table><tr><th>")
.append(_("Remove"))
.append("</th><th>")
@ -791,22 +802,34 @@ public class SummaryHelper extends HelperBase {
.append("</th><th>")
.append(_("Name"))
.append("</th></tr>\n");
for (int i = 0; i < sections.length; i++) {
for (String section : sections) {
int i = sections.indexOf(section);
buf.append("<tr><td align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"delete_")
.append(i)
.append("\"></td><td align=\"center\"><input type=\"text\" name=\"order_")
.append(i + "_" + sections[i])
.append(i + "_" + section)
.append("\" value=\"")
.append(i)
.append("\"></td><td align=\"left\">")
.append(sections[i])
.append(section)
.append("</td></tr>\n");
}
buf.append("<tr><td align=\"center\"><b>")
.append(_("Add")).append(":</b>" +
"</td><td align=\"left\"><input type=\"text\" name=\"order\"></td>" +
"<td align=\"left\"><input type=\"text\" name=\"name\"></td></tr>");
buf.append("</table>\n");
"<td align=\"left\">" +
"<select name=\"name\">\n" +
"<option value=\"\" selected=\"selected\">")
.append(_("Select a section to add"))
.append("</option>\n");
for (String s : sortedSections) {
buf.append("<option value=\"").append(s).append("\">")
.append(s).append("</option>\n");
}
buf.append("</select></td></tr>")
.append("</table>\n");
return buf.toString();
}
}