001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.casetool.patternimplementor;
012:
013: import java.io.File;
014: import java.util.ArrayList;
015: import java.util.Observable;
016: import java.util.StringTokenizer;
017:
018: import javax.swing.event.TreeSelectionEvent;
019: import javax.swing.event.TreeSelectionListener;
020:
021: public class PatternMechanism extends Observable implements
022: TreeSelectionListener {
023:
024: AbstractPatternImplementor pattern;
025:
026: Class[] patterns;
027:
028: Class currentClass;
029:
030: private boolean isCancelled = false;
031:
032: public PatternMechanism() {
033: patterns = findAllPatterns();
034:
035: try {
036: setPattern(patterns[0]);
037: } catch (Exception e) {
038: e.printStackTrace();
039: }
040: }
041:
042: public void valueChanged(TreeSelectionEvent e) {
043: Object source = e.getPath().getLastPathComponent();
044:
045: //System.out.println("valueChanged " + e.getPath());
046: if (source instanceof PMTreeNode) {
047: this .setPattern(((PMTreeNode) source).getPattern());
048: } else {
049: //System.out.println(source);
050: }
051: }
052:
053: public Class[] getPatterns() {
054: return patterns;
055: }
056:
057: public boolean setPattern(Class patternClass) {
058: try {
059: setPattern((AbstractPatternImplementor) patternClass
060: .newInstance());
061:
062: return true;
063: } catch (Exception e) {
064: System.err.println("Error when dealing with : "
065: + patternClass.getName());
066: e.printStackTrace();
067: }
068:
069: return false;
070: }
071:
072: public AbstractPatternImplementor getPattern() {
073: return pattern;
074: }
075:
076: public void setPattern(AbstractPatternImplementor pattern) {
077: //System.out.println("setPattern to " + pattern.getName());
078:
079: if (!pattern.equals(this .pattern)) {
080: this .pattern = pattern;
081: this .setChanged();
082: this .notifyObservers(this );
083: }
084: }
085:
086: public void cancel() {
087: isCancelled = true;
088: }
089:
090: public SourceCode getImplementation() {
091: //if(pattern != null) {
092: if (!isCancelled) {
093: return pattern.getImplementation();
094: } else {
095: return null;
096: }
097:
098: //}
099: //else
100: // return null;
101: }
102:
103: public PIParameterGroup getParameters() {
104: return pattern.getParameters();
105: }
106:
107: public String getUIName() {
108: return pattern.getName();
109: }
110:
111: private static Class[] findAllPatterns() {
112: ArrayList the_patterns = new ArrayList();
113:
114: String packageUrl = AbstractPatternImplementor.class
115: .getPackage().toString();
116: packageUrl = packageUrl.substring(packageUrl.indexOf(" ") + 1);
117:
118: byte[] packageUrlasArray = packageUrl.getBytes();
119:
120: for (int i = 0; i < packageUrlasArray.length; i++) {
121: if (packageUrlasArray[i] == '.') {
122: packageUrlasArray[i] = (byte) File.separatorChar;
123: }
124: }
125:
126: String packagePath = new String(packageUrlasArray);
127: packagePath = File.separatorChar + packagePath
128: + File.separatorChar + "patterns" + File.separatorChar;
129: //System.out.println(packagePath + "\t" + packageUrl);
130:
131: String classPath = System.getProperty("java.class.path", ".");
132:
133: StringTokenizer cp = new StringTokenizer(classPath,
134: File.pathSeparator);
135:
136: while (cp.hasMoreTokens()) {
137: String cPath = cp.nextToken();
138:
139: //System.out.println("Full path = " + cPath + "\t" + packagePath);
140:
141: //URL url =
142: // AbstractPatternImplementor.class.getResource(cPath+packagePath);
143: //System.out.println("URL = " + url + "\t");
144: //File directory = new File(url.getFile());
145: File directory = new File(cPath + packagePath);
146:
147: if (directory.exists()) {
148: String[] files = directory.list();
149:
150: //Class[] patterns = new Class[files.length];
151: for (int i = 0; i < files.length; i++) {
152: //System.out.println("file : " + files[i]);
153:
154: if (files[i].endsWith(".class")) {
155: try {
156: String tmp = files[i].substring(0, files[i]
157: .length() - 6);
158: tmp = packageUrl + ".patterns." + tmp;
159: //System.err.println(tmp);
160:
161: Class c = Class.forName(tmp);
162: Class[] interfaces = c.getInterfaces();
163:
164: for (int j = 0; j < interfaces.length; j++) {
165: if (interfaces[j]
166: .equals(AbstractPatternImplementor.class /* .getName() */)) {
167: the_patterns.add(c);
168: //System.err.print(c.getName());
169: //System.err
170: // .println("\t"
171: // + interfaces[j]
172: // .equals(AbstractPatternImplementor.class));
173: } else {
174: //System.out.println(""+c.getName());
175: }
176: }
177: } catch (Exception e) {
178: System.err
179: .println("Error with " + files[i]);
180: e.printStackTrace();
181: }
182: }
183: }
184: }
185: }
186:
187: Class[] retval = new Class[the_patterns.size()];
188:
189: for (int i = 0; i < the_patterns.size(); i++) {
190: retval[i] = ((Class) the_patterns.get(i));
191: //System.err.print(retval[i].toString() + "\t");
192: }
193:
194: //System.err.println("");
195:
196: return retval;
197: }
198: }
|