001: package tide.project;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import javax.swing.JMenuItem;
006: import javax.swing.JMenu;
007: import tide.editor.MainEditorFrame;
008: import tide.sources.*;
009: import java.util.*;
010: import java.io.*;
011: import java.util.regex.*;
012: import snow.utils.*;
013:
014: public final class ProjectUtils {
015: private ProjectUtils() {
016: }
017:
018: /** @param javaSource if null, returns null
019: */
020: public static String getJavaNameFor(File javaSource,
021: ProjectSettings proj) {
022: // occurs for example when no main project class is defined
023: if (javaSource == null)
024: return null;
025:
026: String mainClassName = javaSource.getAbsolutePath().substring(
027: proj.getSources_Home().getAbsolutePath().length());
028: mainClassName = mainClassName.replace('\\', '.');
029: mainClassName = mainClassName.replace('/', '.');
030: if (mainClassName.startsWith(".")) {
031: mainClassName = mainClassName.substring(1, mainClassName
032: .length());
033: }
034:
035: // remove .java (if any)
036: if (mainClassName.toLowerCase().endsWith(".java")) {
037: mainClassName = mainClassName.substring(0, mainClassName
038: .length() - 5);
039: } else {
040: return null;
041: }
042:
043: return mainClassName;
044: }
045:
046: /** on linux:
047: * "locate java/util/StringBuffer.html"
048: * on windows: guess !
049: */
050: public static List<File> searchJDKDocRoots() {
051: List<File> roots = new ArrayList<File>();
052:
053: if (!SysUtils.is_Windows_OS()) {
054: try {
055: List<String> cmd = new ArrayList<String>(Arrays.asList(
056: "/bin/sh", "-c"));
057: cmd.add("locate java/util/StringBuffer.html");
058: String rep = ProcessUtils.readWholeProcessStack(cmd);
059:
060: // TODO.
061: // rep.split("\n")
062:
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: }
067:
068: return roots;
069: }
070:
071: public static String removeXlints(String line) {
072: String rep = line;
073: int pos = -1;
074: while (true) {
075: pos = rep.indexOf("-Xlint");
076: if (pos == -1)
077: break;
078: int posEnd = rep.indexOf(' ', pos + 6);
079: String nrep = rep.substring(0, pos);
080: if (posEnd > 0)
081: nrep += rep.substring(posEnd);
082: rep = nrep;
083: }
084: return rep;
085: }
086:
087: /** Splits correctly, taking quotations in account.
088: * if not starting with -, glue with previous.
089: *@param glueKeyAndVals true => "-Xmaxerrs 20" instead of "-Xmaxerrs" and "20".
090: * DO not use when passing to process!
091: */
092: public static List<String> splitArgs(String argline,
093: boolean glueKeyAndVals) {
094: List<String> al = new ArrayList<String>();
095: boolean isInString = false;
096: StringBuilder sb = new StringBuilder();
097: // separate spaces, but consider strings
098: for (char ci : argline.toCharArray()) {
099: if (ci == '\"')
100: isInString = !isInString;
101: if (!isInString) {
102: if (ci == ' ') {
103: if (sb.length() > 0)
104: al.add(sb.toString());
105: sb.setLength(0);
106: continue;
107: }
108: }
109: sb.append(ci);
110: }
111: // don't forget the last
112: if (sb.length() > 0)
113: al.add(sb.toString());
114:
115: // glue with previous if not "-"
116: if (glueKeyAndVals) {
117: for (int i = al.size() - 1; i >= 1; i--) {
118: if (!al.get(i).startsWith("-")) {
119: al.set(i - 1, al.get(i - 1) + " " + al.get(i));
120: al.remove(i);
121: }
122: }
123: }
124:
125: return al;
126: }
127:
128: /** 1.6, 1.5, 1.4
129: */
130: public static void updateJDKDocsHelpMenu(final File docsRoot) {
131: JMenu menu = MainEditorFrame.instance.jdkDocsMenu;
132: menu.removeAll();
133: if (docsRoot == null || !docsRoot.exists()) {
134: menu
135: .add("Please set the JDK docs root in the Project Settings");
136: return;
137: }
138: menu.setToolTipText(docsRoot.getAbsolutePath());
139:
140: final File api = new File(docsRoot, "api/index.html");
141: if (api.exists()) {
142: JMenuItem browseAPI = new JMenuItem("Browse API JavaDocs");
143: menu.add(browseAPI);
144: browseAPI.addActionListener(new ActionListener() {
145: public void actionPerformed(ActionEvent ae) {
146: try {
147: SysUtils.openBrowser(api.getAbsolutePath());
148: } catch (Exception e) {
149: MainEditorFrame.instance
150: .displayErrorMessage(e,
151: "Can't open browser to see JDK api docs");
152: }
153: }
154: });
155: } else {
156: menu.add("API not found");
157: }
158:
159: File guides = new File(docsRoot, "technotes/guides/index.html");
160: if (!guides.exists()) {
161: // old 1.5.0 struct
162: guides = new File(docsRoot, "guide/index.html");
163: if (guides.exists()) {
164: guides = new File(docsRoot, "index.html");
165: }
166:
167: }
168:
169: if (guides.exists()) {
170: JMenuItem bguides = new JMenuItem("Browse Guides");
171: menu.add(bguides);
172: final String gp = guides.getAbsolutePath();
173: bguides.addActionListener(new ActionListener() {
174: public void actionPerformed(ActionEvent ae) {
175: try {
176: SysUtils.openBrowser(gp);
177: } catch (Exception e) {
178: MainEditorFrame.instance
179: .displayErrorMessage(e,
180: "Can't open browser to see JDK guides docs");
181: }
182: }
183: });
184: } else {
185: menu.add("Guides not found");
186: }
187:
188: File tooldocs = new File(docsRoot, "technotes/tools/index.html");
189: if (!tooldocs.exists()) {
190: // old 1.5.0 struct
191: tooldocs = new File(docsRoot, "tooldocs/index.html");
192: if (!tooldocs.exists()) {
193: tooldocs = new File(docsRoot, "tooldocs/tools.html");
194: }
195: }
196:
197: if (tooldocs.exists()) {
198: JMenuItem bguides = new JMenuItem("Browse Tooldocs");
199: menu.add(bguides);
200: final String gp = tooldocs.getAbsolutePath();
201: bguides.addActionListener(new ActionListener() {
202: public void actionPerformed(ActionEvent ae) {
203: try {
204: SysUtils.openBrowser(gp);
205: } catch (Exception e) {
206: MainEditorFrame.instance
207: .displayErrorMessage(e,
208: "Can't open browser to see JDK tool docs");
209: }
210: }
211: });
212: } else {
213: menu.add("Tooldocs not found");
214: }
215: }
216:
217: /*test
218: public static void main(String[] arguments)
219: {
220: System.out.println(removeXlints("1.5 -Xlint -Xloint:-sreial -23.3"));
221: System.out.println(""+ splitArgs("-a b \"c c\" -ddd 46 -f",true));
222: }*/
223:
224: }
|