Files

192 lines
5.7 KiB
Java
Raw Permalink Normal View History

2026-09-14 20:19:29 -04:00
/*
* Mind.java
*
* @author asko.soukka@iki.fi
* @version 0.1
*
* Copyright (c) 2002 Asko Soukka
* GZigZag is written by Tuomas Lukka
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* See the LICENSE.lgpl for the specific terms of the license.
*
* This library 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
* Library General Public License for more details.
*/
/*
* Written by Asko Soukka and Tuukka Hastrup
*/
package org.gzigzag.module;
import org.gzigzag.*;
import java.util.*;
import java.io.*;
import java.awt.*;
/** Mindmap tools from Asko Soukka for mainly hierarcical structures
*/
public class Mind {
public static final String rcsid = "$Id: Mind.java,v 1.1.2.2 2002/02/16 15:46:58 tuukkah Exp $";
public static boolean dbg = false;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
static final void pa(String s) { ZZLogger.log(s); }
static public ZZModule module = new ZZModule() {
public ZOb newZOb(String id) {
if(id.equals("Sundew"))
return new MindSundew();
else if(id.equals("Sun"))
return new MindSun();
return null;
}
public void action(String id, ZZCell code,
ZZCell target,
ZZView v, ZZView cv,
String key, Point pt,
ZZScene xi) {
try {
ZZCell viewCell = v.getViewcell();
ZZCell viewCursor = ZZCursorReal.get(viewCell);
if(id.equals("TextOut")) {
FileWriter fileout = new FileWriter(target.getText() + ".txt");
BufferedWriter writer = new BufferedWriter(fileout);
writer.write(target.getText()+"\n");
if (target.s("d.1", 1) != null) {
writer.write("\n");
printTextTree("d.2", "d.1", target.s("d.1", 1), writer,
1, 10);
writer.write("\n");
}
writer.close();
} else if(id.equals("HtmlOut")) {
FileWriter fileout = new FileWriter(target.getText()+".html");
BufferedWriter writer = new BufferedWriter(fileout);
writer.write("<html>\n");
writer.write("<head><title>\n");
writer.write(target.getText()+"\n");
writer.write("</title></head>\n");
writer.write("<body>\n");
writer.write("<h1>" + target.getText() + "</h1>\n");
if (target.s("d.1", 1) != null) {
writer.write("\n");
printHtmlTree("d.2", "d.1", "d.clone", target.s("d.1", 1),
writer, 1, 10, 2);
writer.write("\n");
}
writer.write("</body>\n");
writer.write("</html>\n");
writer.close();
} else {
pa("UNKNOWN Mind COMMAND "+id);
}
} catch(Exception e) {
e.printStackTrace();
throw new ZZError(""+e);
}
}
};
private static void printTextTree(String dim, String dimTop, ZZCell accursed,
BufferedWriter writer, int start, int end) {
Vector cells = getAllCells(dim, accursed);
int i = 0;
try {
while (i < cells.size()) {
if (!((ZZCell)cells.elementAt(i)).getText().equals("")) {
writer.write(/*"<li>"+*/((ZZCell)cells.elementAt(i)).getText()+"\n");
}
if (((ZZCell)cells.elementAt(i)).s(dimTop, 1) != null) {
for (int j=0; j<start; j++) writer.write("-"); writer.write("\n");
if(start < end) printTextTree(dim, dimTop, ((ZZCell)cells.elementAt(i)).s(dimTop, 1), writer,
start + 1, end);
writer.write("\n");
}
i++;
}
} catch (IOException iOE) {}
}
private static void printHtmlTree(String dim, String dimTop, String dimLink,
ZZCell accursed, BufferedWriter writer,
int start, int end, int topics) {
Vector cells = getAllCells(dim, accursed);
int i = 0;
try {
if (start > topics) writer.write("<ul>\n");
while (i < cells.size()) {
if (!((ZZCell)cells.elementAt(i)).getText().equals("")) {
if (start > topics) {
writer.write("<li>");
if (((ZZCell)cells.elementAt(i)).s(dimLink, -1) == null) {
if (((ZZCell)cells.elementAt(i)).s(dimLink, 1) != null)
writer.write("<a name=\""+((ZZCell)cells.elementAt(i)).getText()+"\"></a>");
writer.write(((ZZCell)cells.elementAt(i)).getText());
} else {
writer.write("<a href=\"#"+((ZZCell)cells.elementAt(i)).getText()+"\">");
writer.write(((ZZCell)cells.elementAt(i)).getText()+"</a>");
}
writer.write("\n");
} else {
writer.write("<h"+start+">");
if (((ZZCell)cells.elementAt(i)).s(dimLink, -1) == null) {
if (((ZZCell)cells.elementAt(i)).s(dimLink, 1) != null)
writer.write("<a name=\""+((ZZCell)cells.elementAt(i)).getText()+"\"></a>");
writer.write(((ZZCell)cells.elementAt(i)).getText());
} else {
writer.write("<a href=\"#"+((ZZCell)cells.elementAt(i)).getText()+"\">");
writer.write(((ZZCell)cells.elementAt(i)).getText()+"</a>");
}
writer.write("</h"+start+">\n");
}
}
if (((ZZCell)cells.elementAt(i)).s(dimTop, 1) != null) {
writer.write("\n");
if(start < end) printHtmlTree(dim, dimTop, dimLink, ((ZZCell)cells.elementAt(i)).s(dimTop, 1), writer,
start + 1, end, topics);
writer.write("\n");
}
i++;
}
if (start > topics) writer.write("</ul>\n");
} catch (IOException iOE) {}
}
private static Vector getAllCells(String dim, ZZCell accursed) {
int LIMIT = 1000;
Vector cells = new Vector();
ZZCell next = null;
cells.addElement(accursed);
int i = 1;
while (cells.size() < LIMIT ) {
next = accursed.s(dim, i++);
if(next != null && !cells.contains(next)) cells.addElement(next);
else break;
}
return cells;
}
}