summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSilenio Quarti <silenio>2008-08-07 19:43:16 +0000
committerSilenio Quarti <silenio>2008-08-07 19:43:16 +0000
commit37dd049635a9e27d1a8ac8a9fdb6deee1a837896 (patch)
treea49f2878f41f7e57e0359d9eb2a973a5f291d335
parent8ed739aeb6bdf363f7b0e5c8d1e2c8f2b47241eb (diff)
downloadeclipse.platform.swt-37dd049635a9e27d1a8ac8a9fdb6deee1a837896.tar.gz
eclipse.platform.swt-37dd049635a9e27d1a8ac8a9fdb6deee1a837896.tar.xz
eclipse.platform.swt-37dd049635a9e27d1a8ac8a9fdb6deee1a837896.zip
*** empty log message ***
-rw-r--r--bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/DOMWriter.java2
-rw-r--r--bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGenerator.java171
-rw-r--r--bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGeneratorUI.java279
3 files changed, 350 insertions, 102 deletions
diff --git a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/DOMWriter.java b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/DOMWriter.java
index 0c02cbff19..163fe44748 100644
--- a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/DOMWriter.java
+++ b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/DOMWriter.java
@@ -1 +1 @@
-/******************************************************************************* * Copyright (c) 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.tools.internal; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMWriter { static String ENCONDING = "UTF8"; PrintStream out; boolean canonical; public DOMWriter(PrintStream out, boolean canonical) { this.out = new PrintStream(out); this.canonical = canonical; } String nodeName(Node node) { // TODO use getLocalName()? return node.getNodeName(); } void print(String str) { out.print(str); } void println() { out.println(); } public void print(Node node) { if (node == null) return; int type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { if (!canonical) { String Encoding = ENCONDING; print("<?xml version=\"1.0\" encoding=\""); print(Encoding); print("\"?>"); println(); } print(((Document) node).getDocumentElement()); out.flush(); break; } case Node.ELEMENT_NODE: { print("<"); print(nodeName(node)); Attr attrs[] = sort(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; print(" "); print(nodeName(attr)); print("=\""); print(normalize(attr.getNodeValue())); print("\""); } print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } break; } case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } } else { print("&"); print(nodeName(node)); print(";"); } break; } case Node.CDATA_SECTION_NODE: { if (canonical) { print(normalize(node.getNodeValue())); } else { print("<![CDATA["); print(node.getNodeValue()); print("]]>"); } break; } case Node.TEXT_NODE: { print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { print("<?"); print(nodeName(node)); String data = node.getNodeValue(); if (data != null && data.length() > 0) { print(" "); print(data); } print("?>"); break; } } if (type == Node.ELEMENT_NODE) { print("</"); print(nodeName(node)); print(">"); } out.flush(); } Attr[] sort(NamedNodeMap attrs) { if (attrs == null) return new Attr[0]; Attr result[] = new Attr[attrs.getLength()]; for (int i = 0; i < result.length; i++) { result[i] = (Attr) attrs.item(i); } Arrays.sort(result, new Comparator() { public int compare(Object arg0, Object arg1) { return nodeName((Node) arg0).compareTo(nodeName((Node) arg1)); } }); return result; } String normalize(String s) { if (s == null) return ""; StringBuffer str = new StringBuffer(); for (int i = 0, length = s.length(); i < length; i++) { char ch = s.charAt(i); switch (ch) { case '"': str.append("\""); break; case '\r': case '\n': if (canonical) { str.append("&#"); str.append(Integer.toString(ch)); str.append(';'); break; } // FALL THROUGH default: str.append(ch); } } return str.toString(); } } \ No newline at end of file
+/******************************************************************************* * Copyright (c) 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.tools.internal; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMWriter { static String ENCONDING = "UTF8"; PrintStream out; boolean canonical, pretty = true; public DOMWriter(PrintStream out, boolean canonical) { this.out = new PrintStream(out); this.canonical = canonical; } String nodeName(Node node) { // TODO use getLocalName()? return node.getNodeName(); } void print(String str) { out.print(str); } void println() { out.println(); } public void print(Node node) { print(node, 0); } public void print(Node node, int level) { if (node == null) return; int type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { if (!canonical) { print("<?xml version=\"1.0\" encoding=\""); print(ENCONDING); print("\"?>"); println(); } print(((Document) node).getDocumentElement()); out.flush(); break; } case Node.ELEMENT_NODE: { if (!canonical && pretty) { println(); for (int i = 0; i < level; i++) print("\t"); } print("<"); print(nodeName(node)); Attr attrs[] = sort(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; print(" "); print(nodeName(attr)); print("=\""); print(normalize(attr.getNodeValue())); print("\""); } print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i), level + 1); } } break; } case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i), level + 1); } } } else { print("&"); print(nodeName(node)); print(";"); } break; } case Node.CDATA_SECTION_NODE: { if (canonical) { print(normalize(node.getNodeValue())); } else { print("<![CDATA["); print(node.getNodeValue()); print("]]>"); } break; } case Node.TEXT_NODE: { print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { print("<?"); print(nodeName(node)); String data = node.getNodeValue(); if (data != null && data.length() > 0) { print(" "); print(data); } print("?>"); break; } } if (type == Node.ELEMENT_NODE) { if (!canonical && pretty) { if (node.getChildNodes().getLength() > 0) { println(); for (int i = 0; i < level; i++) print("\t"); } } print("</"); print(nodeName(node)); print(">"); } out.flush(); } Attr[] sort(NamedNodeMap attrs) { if (attrs == null) return new Attr[0]; Attr result[] = new Attr[attrs.getLength()]; for (int i = 0; i < result.length; i++) { result[i] = (Attr) attrs.item(i); } Arrays.sort(result, new Comparator() { public int compare(Object arg0, Object arg1) { return nodeName((Node) arg0).compareTo(nodeName((Node) arg1)); } }); return result; } String normalize(String s) { if (s == null) return ""; StringBuffer str = new StringBuffer(); for (int i = 0, length = s.length(); i < length; i++) { char ch = s.charAt(i); switch (ch) { case '"': str.append("\""); break; case '\r': case '\n': if (canonical) { str.append("&#"); str.append(Integer.toString(ch)); str.append(';'); break; } // FALL THROUGH default: str.append(ch); } } return str.toString(); } } \ No newline at end of file
diff --git a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGenerator.java b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGenerator.java
index 70a9007125..452e4fb972 100644
--- a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGenerator.java
+++ b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGenerator.java
@@ -13,11 +13,18 @@ package org.eclipse.swt.tools.internal;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.TreeSet;
@@ -27,13 +34,13 @@ import org.xml.sax.InputSource;
public class MacGenerator {
String[] classes;
- String xml[];
+ String xmls[];
String outputDir;
PrintStream out;
-public MacGenerator(String[] xml) throws Exception {
- this.xml = xml;
+public MacGenerator(String[] xml) {
+ this.xmls = xml;
}
public void out(String str) {
@@ -49,8 +56,8 @@ public void outln() {
}
public void generateConstants() throws Exception {
- for (int j = 0; j < xml.length; j++) {
- Document document = getDocument(xml[j]);
+ for (int j = 0; j < xmls.length; j++) {
+ Document document = getDocument(xmls[j]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -66,8 +73,8 @@ public void generateConstants() throws Exception {
}
public void generateConstantsMetaData() throws Exception {
- for (int j = 0; j < xml.length; j++) {
- Document document = getDocument(xml[j]);
+ for (int j = 0; j < xmls.length; j++) {
+ Document document = getDocument(xmls[j]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -83,8 +90,8 @@ public void generateConstantsMetaData() throws Exception {
}
public void generateEnums() throws Exception {
- for (int j = 0; j < xml.length; j++) {
- Document document = getDocument(xml[j]);
+ for (int j = 0; j < xmls.length; j++) {
+ Document document = getDocument(xmls[j]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -133,9 +140,83 @@ boolean isBoolean(Node node) {
return code.equals("B");
}
-Document getDocument(String xmlPath) throws Exception {
- BufferedInputStream is = new BufferedInputStream(new FileInputStream(xmlPath));
- return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(is));
+public Document getDocument(String xmlPath) {
+ try {
+ InputStream is = null;
+ if (xmlPath.indexOf(File.separatorChar) == -1) is = getClass().getResourceAsStream(xmlPath);
+ if (is == null) is = new BufferedInputStream(new FileInputStream(xmlPath));
+ if (is != null) return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(is));
+ } catch (Exception e) {
+// e.printStackTrace();
+ }
+ return null;
+}
+
+public String[] getExtraAttributes() {
+ return new String[]{
+ "swt_superclass",
+ "swt_vararg_max",
+ "swt_not_static",
+ };
+}
+
+String getMetaDataDir() {
+ return "./Mac Generation/org/eclipse/swt/tools/internal/";
+}
+
+public String getKey (Node node) {
+ StringBuffer buffer = new StringBuffer();
+ while (node != null) {
+ if (buffer.length() > 0) buffer.append("_");
+ String name = node.getNodeName();
+ StringBuffer key = new StringBuffer(name);
+ Node nameAttrib = getNameAttribute(node);
+ if (nameAttrib != null) {
+ key.append("-");
+ key.append(nameAttrib.getNodeValue());
+ }
+ buffer.append(key.reverse());
+ node = node.getParentNode();
+ }
+ buffer.reverse();
+ return buffer.toString();
+}
+
+public Node getNameAttribute(Node node) {
+ NamedNodeMap attributes = node.getAttributes();
+ if (attributes == null) return null;
+ Node nameAttrib = attributes.getNamedItem("name");
+ if (nameAttrib == null) nameAttrib = attributes.getNamedItem("selector");
+ if (nameAttrib == null) nameAttrib = attributes.getNamedItem("path");
+ return nameAttrib;
+}
+
+void buildExtrasLookup(Node node, Hashtable table) {
+ NodeList list = node.getChildNodes();
+ for (int i = 0; i < list.getLength(); i++) {
+ Node childNode = list.item(i);
+ String key = getKey(childNode);
+ table.put(key, childNode);
+ buildExtrasLookup(childNode, table);
+ }
+}
+
+public String getFileName(String xmlPath) {
+ String fileName = xmlPath;
+ int index = fileName.lastIndexOf(File.separatorChar);
+ if (index != -1) fileName = fileName.substring(index + 1);
+ return fileName;
+}
+
+public Hashtable loadExtraAttributesLookup (String xmlPath) {
+ Hashtable table = new Hashtable();
+ Document doc = getDocument(getFileName(xmlPath) + ".extras");
+ if (doc != null) buildExtrasLookup(doc, table);
+ return table;
+}
+
+public String[] getXmls() {
+ return xmls;
}
boolean getGenerateClass(String className) {
@@ -170,8 +251,8 @@ public boolean isUnique(Node method, NodeList methods) {
}
public void generateClasses() throws Exception {
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -353,8 +434,8 @@ public void generateClasses() throws Exception {
public void generateSelectorsConst() throws Exception {
HashSet set = new HashSet();
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -389,8 +470,8 @@ public void generateSelectorsConst() throws Exception {
public void generateSends() throws Exception {
HashSet set = new HashSet();
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -448,8 +529,8 @@ public void generateSends() throws Exception {
public void generateSendsMetaData() throws Exception {
HashMap set = new HashMap();
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -547,8 +628,8 @@ String getSelConst(String sel) {
public void generateClassesConst() throws Exception {
TreeSet set = new TreeSet();
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -576,8 +657,8 @@ public void generateClassesConst() throws Exception {
public void generateProtocolsConst() throws Exception {
TreeSet set = new TreeSet();
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -710,8 +791,8 @@ String getJavaType(Node node) {
}
public void generateFunctions() throws Exception {
- for (int x = 0; x < xml.length; x++) {
- Document document = getDocument(xml[x]);
+ for (int x = 0; x < xmls.length; x++) {
+ Document document = getDocument(xmls[x]);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
@@ -783,6 +864,44 @@ public void generateMetadata() throws Exception {
generateSendsMetaData();
}
+public void saveExtraAttributes(String xmlPath, Document document) {
+ try {
+ String fileName = getMetaDataDir() + getFileName(xmlPath) + ".extras";
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new DOMWriter(new PrintStream(out), false).print(document);
+ if (out.size() > 0) output(out.toByteArray(), fileName);
+ } catch (Exception e) {
+ System.out.println("Problem");
+ e.printStackTrace(System.out);
+ }
+}
+
+boolean compare(InputStream is1, InputStream is2) throws IOException {
+ while (true) {
+ int c1 = is1.read();
+ int c2 = is2.read();
+ if (c1 != c2) return false;
+ if (c1 == -1) break;
+ }
+ return true;
+}
+
+void output(byte[] bytes, String fileName) throws IOException {
+ FileInputStream is = null;
+ try {
+ is = new FileInputStream(fileName);
+ if (compare(new ByteArrayInputStream(bytes), new BufferedInputStream(is))) return;
+ } catch (FileNotFoundException e) {
+ } finally {
+ try {
+ if (is != null) is.close();
+ } catch (IOException e) {}
+ }
+ FileOutputStream out = new FileOutputStream(fileName);
+ out.write(bytes);
+ out.close();
+}
+
public void setClasses(String[] classes) {
this.classes = classes;
}
diff --git a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGeneratorUI.java b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGeneratorUI.java
index 9ffcb9e847..6bf521d110 100644
--- a/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGeneratorUI.java
+++ b/bundles/org.eclipse.swt.tools/Mac Generation/org/eclipse/swt/tools/internal/MacGeneratorUI.java
@@ -10,32 +10,35 @@
*******************************************************************************/
package org.eclipse.swt.tools.internal;
-import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
import java.util.Hashtable;
+import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.eclipse.swt.*;
+import org.eclipse.swt.custom.TreeEditor;
+import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
+import org.w3c.dom.Attr;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
public class MacGeneratorUI {
- String[] xmls;
+ MacGenerator gen;
Display display;
Shell shell;
Tree nodesTree;
+
+ static String DELIMETER = "\n";
public MacGeneratorUI(String[] xmls) {
- this.xmls = xmls;
+ gen = new MacGenerator(xmls);
}
TreeItem addChild (Node node, TreeItem superItem, Hashtable extras) {
@@ -54,41 +57,120 @@ public class MacGeneratorUI {
parentItem.setData(name);
parentItem.setText(getPrettyText(name));
}
- NamedNodeMap attributes = node.getAttributes();
TreeItem item = new TreeItem(parentItem, SWT.NONE);
- Node nameAttrib = getNameAttribute(attributes);
+ Node nameAttrib = gen.getNameAttribute(node);
String text = nameAttrib != null ? nameAttrib.getNodeValue() : name;
item.setText(text);
- Node extra = (Node)extras.get(getKey(node));
+ item.setData(node);
+ Node extra = (Node)extras.get(gen.getKey(node));
if (extra != null) {
- NamedNodeMap extraAttributes = extra.getAttributes();
- Node gen = extraAttributes.getNamedItem("swt_gen");
- if (gen != null && gen.getNodeValue().equals("true")) {
- item.setChecked(true);
+ NamedNodeMap attributes = extra.getAttributes();
+ Node gen = attributes.getNamedItem("swt_gen");
+ item.setChecked(gen != null && gen.getNodeValue().equals("true"));
+ for (int i = 0; i < attributes.getLength(); i++) {
+ Node attrib = attributes.item(i);
+ String attriName = attrib.getNodeName();
+ if (attriName.equals("swt_gen") || !attriName.startsWith("swt_")) continue;
+ int columnIndex = getColumnFor(attrib.getNodeName());
+ item.setText(columnIndex, attrib.getNodeValue());
}
}
+ NamedNodeMap attributes = node.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attrib = attributes.item(i);
- text = attrib.getNodeName();
if (attrib.equals(nameAttrib)) continue;
- int columnIndex = getColumnFor(text);
+ int columnIndex = getColumnFor(attrib.getNodeName());
item.setText(columnIndex, attrib.getNodeValue());
}
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
addChild(childNodes.item(i), item, extras);
}
+ boolean checked = item.getChecked();
+ checkItems(item, checked);
+ checkPath(item.getParentItem(), checked, false);
return item;
}
-
- void buildLookup(Node node, Hashtable table) {
- NodeList list = node.getChildNodes();
- for (int i = 0; i < list.getLength(); i++) {
- Node childNode = list.item(i);
- String key = getKey(childNode);
- table.put(key, childNode);
- buildLookup(childNode, table);
+
+ void buildExtrasNode(Document document, Node parent, TreeItem item, int level) {
+ TreeItem[] items = item.getItems();
+ if (item.getData() instanceof Node) {
+ TreeColumn[] columns = item.getParent().getColumns();
+ Node node = (Node)item.getData();
+ String nodeName = node.getNodeName();
+ Node nameAttrib = gen.getNameAttribute(node);
+ Element newNode = document.createElement(nodeName);
+ if (nameAttrib != null) {
+ Attr attr = document.createAttribute(nameAttrib.getNodeName());
+ attr.setNodeValue(nameAttrib.getNodeValue());
+ newNode.setAttributeNode(attr);
+ }
+ if (item.getChecked()) {
+ Attr attr = document.createAttribute("swt_gen");
+ attr.setNodeValue("true");
+ newNode.setAttributeNode(attr);
+ }
+ for (int i = 0; i < columns.length; i++) {
+ String attrName = columns[i].getText();
+ if (attrName.startsWith("swt_")) {
+ String value = item.getText(i);
+ if (value.length() != 0) {
+ Attr attr = document.createAttribute(attrName);
+ attr.setNodeValue(value);
+ newNode.setAttributeNode(attr);
+ }
+ }
+ }
+ parent.appendChild(newNode);
+ parent = newNode;
+ }
+ for (int i = 0; i < items.length; i++) {
+ buildExtrasNode(document, parent, items[i], level + 1);
+ }
+ }
+
+ Document buildExtrasDocument(TreeItem item) {
+ try {
+ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document document = builder.newDocument();
+ Node node = document.createElement("signatures");
+ document.appendChild(node);
+ buildExtrasNode(document, node, item, 0);
+ if (node.getChildNodes().getLength() == 0) return null;
+ return document;
+ } catch (Exception e) {
+ e.printStackTrace();
}
+ return null;
+ }
+
+ void checkPath(TreeItem item, boolean checked, boolean grayed) {
+ if (item == null) return;
+ if (grayed) {
+ checked = true;
+ } else {
+ int index = 0;
+ TreeItem[] items = item.getItems();
+ while (index < items.length) {
+ TreeItem child = items[index];
+ if (child.getGrayed() || checked != child.getChecked()) {
+ checked = grayed = true;
+ break;
+ }
+ index++;
+ }
+ }
+ item.setChecked(checked);
+ item.setGrayed(grayed);
+ checkPath(item.getParentItem(), checked, grayed);
+ }
+ void checkItems(TreeItem item, boolean checked) {
+ item.setGrayed(false);
+ item.setChecked(checked);
+ TreeItem[] items = item.getItems();
+ for (int i = 0; i < items.length; i++) {
+ checkItems(items[i], checked);
+ }
}
void cleanup() {
@@ -104,50 +186,11 @@ public class MacGeneratorUI {
column.setText(attribute);
return columns.length;
}
-
- Document getDocument(String xmlPath) {
- try {
- InputStream is = null;
- if (xmlPath.indexOf(File.separatorChar) == -1) is = getClass().getResourceAsStream(xmlPath);
- if (is == null) is = new BufferedInputStream(new FileInputStream(xmlPath));
- if (is != null) return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(is));
- } catch (Exception e) {
-// e.printStackTrace();
- }
- return null;
- }
-
- String getKey (Node node) {
- StringBuffer buffer = new StringBuffer();
- while (node != null) {
- if (buffer.length() > 0) buffer.append("_");
- String name = node.getNodeName();
- StringBuffer key = new StringBuffer(name);
- NamedNodeMap attributes = node.getAttributes();
- Node nameAttrib = getNameAttribute(attributes);
- if (nameAttrib != null) {
- key.append("-");
- key.append(nameAttrib.getNodeValue());
- }
- buffer.append(key.reverse());
- node = node.getParentNode();
- }
- buffer.reverse();
- return buffer.toString();
- }
- Node getNameAttribute(NamedNodeMap attributes) {
- if (attributes == null) return null;
- Node nameAttrib = attributes.getNamedItem("name");
- if (nameAttrib == null) nameAttrib = attributes.getNamedItem("selector");
- if (nameAttrib == null) nameAttrib = attributes.getNamedItem("path");
- return nameAttrib;
- }
-
public void open() {
display = new Display();
shell = new Shell(display);
- shell.setLayout(new GridLayout(1, false));
+ shell.setLayout(new GridLayout(2, false));
Composite parent = shell;
@@ -158,7 +201,95 @@ public class MacGeneratorUI {
TreeColumn nodesColumn = new TreeColumn(nodesTree, SWT.NONE);
nodesColumn.setText("Name");
- nodesColumn.setWidth(300);
+ String[] extraAttributes = gen.getExtraAttributes();
+ for (int i = 0; i < extraAttributes.length; i++) {
+ TreeColumn column = new TreeColumn(nodesTree, SWT.NONE);
+ column.setText(extraAttributes[i]);
+ }
+
+ nodesTree.addListener(SWT.Selection, new Listener() {
+ public void handleEvent(Event event) {
+ if (event.detail != SWT.CHECK) return;
+ TreeItem item = (TreeItem)event.item;
+ if (item == null) return;
+ boolean checked = item.getChecked();
+ checkItems(item, checked);
+ checkPath(item.getParentItem(), checked, false);
+ }
+ });
+
+ nodesTree.addListener(SWT.MouseDown, new Listener() {
+ public void handleEvent(final Event e) {
+ e.display.asyncExec (new Runnable () {
+ public void run () {
+ if (nodesTree.isDisposed ()) return;
+ if (e.button != 1) return;
+ Point pt = new Point(e.x, e.y);
+ TreeItem item = nodesTree.getItem(pt);
+ if (item == null) return;
+ int column = -1;
+ for (int i = 0; i < nodesTree.getColumnCount(); i++) {
+ if (item.getBounds(i).contains(pt)) {
+ column = i;
+ break;
+ }
+ }
+ if (column == -1) return;
+ if (!nodesTree.getColumn(column).getText().startsWith("swt_")) return;
+ final TreeEditor editor = new TreeEditor(nodesTree);
+ editor.grabHorizontal = true;
+ final Text editorTx = new Text(nodesTree, SWT.SINGLE);
+ Listener memberTextListener = new Listener() {
+ public void handleEvent(Event e) {
+ if (e.type == SWT.Traverse) {
+ switch (e.detail) {
+ case SWT.TRAVERSE_ESCAPE:
+ editor.setItem(null);
+ break;
+ default:
+ return;
+ }
+ }
+ editorTx.setVisible(false);
+ TreeItem item = editor.getItem();
+ if (item == null) return;
+ int column = editor.getColumn();
+ item.setText(column, editorTx.getText());
+ }
+ };
+ editorTx.addListener(SWT.DefaultSelection, memberTextListener);
+ editorTx.addListener(SWT.FocusOut, memberTextListener);
+ editorTx.addListener(SWT.Traverse, memberTextListener);
+ editor.setEditor(editorTx);
+ editor.setColumn(column);
+ editor.setItem(item);
+ editorTx.setText(item.getText(column));
+ editorTx.selectAll();
+ editorTx.setVisible(true);
+ editorTx.setFocus();
+ }
+ });
+ }
+ });
+
+ Composite panel = new Composite(parent, SWT.NONE);
+ panel.setLayout(new GridLayout(1, true));
+
+ Button generate = new Button(panel, SWT.PUSH);
+ generate.setText("Generate");
+ generate.addListener(SWT.Selection, new Listener() {
+ public void handleEvent(Event event) {
+ TreeItem[] items = nodesTree.getItems();
+ for (int i = 0; i < items.length; i++) {
+ TreeItem item = items[i];
+ Document document = buildExtrasDocument(item);
+ if (document != null) {
+ gen.saveExtraAttributes((String)item.getData(), document);
+ }
+ }
+ }
+ });
+
updateNodes();
}
@@ -179,26 +310,20 @@ public class MacGeneratorUI {
}
void updateNodes() {
+ String[] xmls = gen.getXmls();
if (xmls == null) return;
for (int x = 0; x < xmls.length; x++) {
String xmlPath = xmls[x];
- String xmlSimplePath = xmlPath;
- int index = xmlSimplePath.lastIndexOf(File.separatorChar);
- if (index != -1) xmlSimplePath = xmlSimplePath.substring(index + 1);
-
- Document document = getDocument(xmlPath);
+ Document document = gen.getDocument(xmlPath);
if (document == null) {
System.out.println("Could not find: " + xmlPath);
continue;
}
- Hashtable extras = new Hashtable();
- Document extraDocument = getDocument(xmlSimplePath + ".extras");
- if (extraDocument != null) {
- buildLookup(extraDocument, extras);
- }
+ Hashtable extras = gen.loadExtraAttributesLookup(xmlPath);
TreeItem xmlItem = new TreeItem(nodesTree, SWT.NONE);
- xmlItem.setText(xmlSimplePath);
+ xmlItem.setText(gen.getFileName(xmlPath));
+ xmlItem.setData(xmlPath);
NodeList list = document.getDocumentElement().getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
addChild(list.item(i), xmlItem, extras);
@@ -211,8 +336,12 @@ public class MacGeneratorUI {
}
public static void main(String[] args) {
+ try {
MacGeneratorUI ui = new MacGeneratorUI(args);
ui.open();
ui.run();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}