1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-15 12:29:26 +01:00

Schematic URL. Actually tag is "@schematics <url>" that should be put

inside code comments.
This commit is contained in:
Cristian Maglie 2011-08-19 19:51:02 +02:00
parent 4b0a87b405
commit a6c8753a80
2 changed files with 127 additions and 25 deletions

View File

@ -46,6 +46,7 @@ import gnu.io.*;
/** /**
* Main editor panel for the Processing Development Environment. * Main editor panel for the Processing Development Environment.
*/ */
@SuppressWarnings("serial")
public class Editor extends JFrame implements RunnerListener { public class Editor extends JFrame implements RunnerListener {
Base base; Base base;
@ -113,7 +114,7 @@ public class Editor extends JFrame implements RunnerListener {
EditorLineStatus lineStatus; EditorLineStatus lineStatus;
JEditorPane editorPane; //JEditorPane editorPane;
JEditTextArea textarea; JEditTextArea textarea;
EditorListener listener; EditorListener listener;
@ -1838,7 +1839,7 @@ public class Editor extends JFrame implements RunnerListener {
public void run() { public void run() {
try { try {
sketch.prepare(); sketch.prepare();
String appletClassName = sketch.build(false); sketch.build(false);
statusNotice("Done compiling."); statusNotice("Done compiling.");
} catch (Exception e) { } catch (Exception e) {
status.unprogress(); status.unprogress();
@ -1855,7 +1856,7 @@ public class Editor extends JFrame implements RunnerListener {
public void run() { public void run() {
try { try {
sketch.prepare(); sketch.prepare();
String appletClassName = sketch.build(true); sketch.build(true);
statusNotice("Done compiling."); statusNotice("Done compiling.");
} catch (Exception e) { } catch (Exception e) {
status.unprogress(); status.unprogress();
@ -2628,17 +2629,30 @@ public class Editor extends JFrame implements RunnerListener {
* Returns the edit popup menu. * Returns the edit popup menu.
*/ */
class TextAreaPopup extends JPopupMenu { class TextAreaPopup extends JPopupMenu {
//String currentDir = System.getProperty("user.dir"); //private String currentDir = System.getProperty("user.dir");
String referenceFile = null; private String referenceFile = null;
JMenuItem cutItem; private JMenuItem cutItem;
JMenuItem copyItem; private JMenuItem copyItem;
JMenuItem discourseItem; private JMenuItem discourseItem;
JMenuItem referenceItem; private JMenuItem referenceItem;
private JMenuItem openURLItem;
private JSeparator openURLItemSeparator;
private String clickedURL;
public TextAreaPopup() { public TextAreaPopup() {
JMenuItem item; openURLItem = new JMenuItem("Open URL");
openURLItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Base.openURL(clickedURL);
}
});
add(openURLItem);
openURLItemSeparator = new JSeparator();
add(openURLItemSeparator);
cutItem = new JMenuItem("Cut"); cutItem = new JMenuItem("Cut");
cutItem.addActionListener(new ActionListener() { cutItem.addActionListener(new ActionListener() {
@ -2646,7 +2660,7 @@ public class Editor extends JFrame implements RunnerListener {
handleCut(); handleCut();
} }
}); });
this.add(cutItem); add(cutItem);
copyItem = new JMenuItem("Copy"); copyItem = new JMenuItem("Copy");
copyItem.addActionListener(new ActionListener() { copyItem.addActionListener(new ActionListener() {
@ -2654,7 +2668,7 @@ public class Editor extends JFrame implements RunnerListener {
handleCopy(); handleCopy();
} }
}); });
this.add(copyItem); add(copyItem);
discourseItem = new JMenuItem("Copy for Forum"); discourseItem = new JMenuItem("Copy for Forum");
discourseItem.addActionListener(new ActionListener() { discourseItem.addActionListener(new ActionListener() {
@ -2662,7 +2676,7 @@ public class Editor extends JFrame implements RunnerListener {
handleDiscourseCopy(); handleDiscourseCopy();
} }
}); });
this.add(discourseItem); add(discourseItem);
discourseItem = new JMenuItem("Copy as HTML"); discourseItem = new JMenuItem("Copy as HTML");
discourseItem.addActionListener(new ActionListener() { discourseItem.addActionListener(new ActionListener() {
@ -2670,15 +2684,15 @@ public class Editor extends JFrame implements RunnerListener {
handleHTMLCopy(); handleHTMLCopy();
} }
}); });
this.add(discourseItem); add(discourseItem);
item = new JMenuItem("Paste"); JMenuItem item = new JMenuItem("Paste");
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
handlePaste(); handlePaste();
} }
}); });
this.add(item); add(item);
item = new JMenuItem("Select All"); item = new JMenuItem("Select All");
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@ -2686,9 +2700,9 @@ public class Editor extends JFrame implements RunnerListener {
handleSelectAll(); handleSelectAll();
} }
}); });
this.add(item); add(item);
this.addSeparator(); addSeparator();
item = new JMenuItem("Comment/Uncomment"); item = new JMenuItem("Comment/Uncomment");
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@ -2696,7 +2710,7 @@ public class Editor extends JFrame implements RunnerListener {
handleCommentUncomment(); handleCommentUncomment();
} }
}); });
this.add(item); add(item);
item = new JMenuItem("Increase Indent"); item = new JMenuItem("Increase Indent");
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@ -2704,7 +2718,7 @@ public class Editor extends JFrame implements RunnerListener {
handleIndentOutdent(true); handleIndentOutdent(true);
} }
}); });
this.add(item); add(item);
item = new JMenuItem("Decrease Indent"); item = new JMenuItem("Decrease Indent");
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@ -2712,9 +2726,9 @@ public class Editor extends JFrame implements RunnerListener {
handleIndentOutdent(false); handleIndentOutdent(false);
} }
}); });
this.add(item); add(item);
this.addSeparator(); addSeparator();
referenceItem = new JMenuItem("Find in Reference"); referenceItem = new JMenuItem("Find in Reference");
referenceItem.addActionListener(new ActionListener() { referenceItem.addActionListener(new ActionListener() {
@ -2722,11 +2736,31 @@ public class Editor extends JFrame implements RunnerListener {
handleFindReference(); handleFindReference();
} }
}); });
this.add(referenceItem); add(referenceItem);
}
private boolean clickedURL(String line, int offset) {
String[] parse = SyntaxUtilities.parseCommentUrls(line);
if (parse==null)
return false;
int pos = parse[0].length()+parse[1].length();
if (offset<pos || offset>pos+2)
return false;
clickedURL = parse[1];
return true;
} }
// if no text is selected, disable copy and cut menu items // if no text is selected, disable copy and cut menu items
public void show(Component component, int x, int y) { public void show(Component component, int x, int y) {
int line = textarea.getLineOfOffset(textarea.xyToOffset(x, y));
if (clickedURL(textarea.getLineText(line), textarea.xToOffset(line, x))) {
openURLItem.setVisible(true);
openURLItemSeparator.setVisible(true);
} else {
openURLItem.setVisible(false);
openURLItemSeparator.setVisible(false);
}
if (textarea.isSelectionActive()) { if (textarea.isSelectionActive()) {
cutItem.setEnabled(true); cutItem.setEnabled(true);
copyItem.setEnabled(true); copyItem.setEnabled(true);

View File

@ -11,6 +11,8 @@ package processing.app.syntax;
import javax.swing.text.*; import javax.swing.text.*;
import java.awt.*; import java.awt.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
@ -148,7 +150,10 @@ public class SyntaxUtilities
styles[id].setGraphicsFlags(gfx,defaultFont); styles[id].setGraphicsFlags(gfx,defaultFont);
line.count = length; line.count = length;
x = Utilities.drawTabbedText(line,x,y,gfx,expander,0); if (id == Token.COMMENT1 || id == Token.COMMENT2)
x = drawTabbedCommentsText(line, x, y, gfx, expander);
else
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
line.offset += length; line.offset += length;
offset += length; offset += length;
@ -158,6 +163,69 @@ public class SyntaxUtilities
return x; return x;
} }
/**
* Parse comments and identify "@schematics <b>&lt;something&gt;</b>" pattern.
*
* @param line
* A string to parse
* @return <b>null</b> if the pattern is not found, otherwise an array of
* String is returned: the elements with index 0, 1 and 2 are
* respectively the preamble, the <b>&lt;something&gt;</b> stuff, and
* the remaining part of the string.
*/
public static String[] parseCommentUrls(String line) {
// Try to find pattern
Pattern schematics = Pattern.compile("@schematics\\s+([^\\s]+)");
Matcher m = schematics.matcher(line.toString());
if (!m.find())
return null;
String res[] = new String[3];
res[0] = line.substring(0, m.start(1));
res[1] = line.substring(m.start(1), m.end(1));
res[2] = line.substring(m.end(1));
// System.out.println("0 =>"+res[0]+"<\n1 =>"+res[1]+"< \n2 =>"+res[2]+"<");
return res;
}
public static Segment stringToSegment(String v) {
return new Segment(v.toCharArray(), 0, v.length());
}
private static int drawTabbedCommentsText(Segment line, int x, int y,
Graphics gfx, TabExpander expander) {
String parse[] = parseCommentUrls(line.toString());
if (parse == null)
// Revert to plain writing.
return Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
Segment pre = stringToSegment(parse[0]);
Segment tag = stringToSegment(parse[1]);
Segment post = stringToSegment(parse[2]);
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0);
// Draw arrow.
FontMetrics metrics = gfx.getFontMetrics();
int h = metrics.getHeight() - 2;
drawArrow(gfx, x, y - h + metrics.getDescent() - 1, h, h);
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
return x;
}
private static void drawArrow(Graphics gfx, int x, int y, int w, int h) {
int h2 = h / 2;
int h4 = h / 4;
gfx.drawLine(x, y+h2, x+h2, y);
gfx.drawLine(x+h2, y, x+h2, y+h4);
gfx.drawLine(x+h2, y+h4, x+h, y+h4);
gfx.drawLine(x+h, y+h4, x+h, y+h-h4);
gfx.drawLine(x+h, y+h-h4, x+h2, y+h-h4);
gfx.drawLine(x+h2,y+h-h4, x+h2, y+h);
gfx.drawLine(x, y+h2, x+h2, y+h);
}
// private members // private members
private SyntaxUtilities() {} private SyntaxUtilities() {}
} }