Skip to content

Commit a6c8753

Browse files
committed
Schematic URL. Actually tag is "@schematics <url>" that should be put
inside code comments.
1 parent 4b0a87b commit a6c8753

File tree

2 files changed

+127
-25
lines changed

2 files changed

+127
-25
lines changed

app/src/processing/app/Editor.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/**
4747
* Main editor panel for the Processing Development Environment.
4848
*/
49+
@SuppressWarnings("serial")
4950
public class Editor extends JFrame implements RunnerListener {
5051

5152
Base base;
@@ -113,7 +114,7 @@ public class Editor extends JFrame implements RunnerListener {
113114

114115
EditorLineStatus lineStatus;
115116

116-
JEditorPane editorPane;
117+
//JEditorPane editorPane;
117118

118119
JEditTextArea textarea;
119120
EditorListener listener;
@@ -1838,7 +1839,7 @@ class DefaultRunHandler implements Runnable {
18381839
public void run() {
18391840
try {
18401841
sketch.prepare();
1841-
String appletClassName = sketch.build(false);
1842+
sketch.build(false);
18421843
statusNotice("Done compiling.");
18431844
} catch (Exception e) {
18441845
status.unprogress();
@@ -1855,7 +1856,7 @@ class DefaultPresentHandler implements Runnable {
18551856
public void run() {
18561857
try {
18571858
sketch.prepare();
1858-
String appletClassName = sketch.build(true);
1859+
sketch.build(true);
18591860
statusNotice("Done compiling.");
18601861
} catch (Exception e) {
18611862
status.unprogress();
@@ -2628,105 +2629,138 @@ public void statusEmpty() {
26282629
* Returns the edit popup menu.
26292630
*/
26302631
class TextAreaPopup extends JPopupMenu {
2631-
//String currentDir = System.getProperty("user.dir");
2632-
String referenceFile = null;
2632+
//private String currentDir = System.getProperty("user.dir");
2633+
private String referenceFile = null;
26332634

2634-
JMenuItem cutItem;
2635-
JMenuItem copyItem;
2636-
JMenuItem discourseItem;
2637-
JMenuItem referenceItem;
2635+
private JMenuItem cutItem;
2636+
private JMenuItem copyItem;
2637+
private JMenuItem discourseItem;
2638+
private JMenuItem referenceItem;
2639+
private JMenuItem openURLItem;
2640+
private JSeparator openURLItemSeparator;
26382641

2642+
private String clickedURL;
26392643

26402644
public TextAreaPopup() {
2641-
JMenuItem item;
2642-
2645+
openURLItem = new JMenuItem("Open URL");
2646+
openURLItem.addActionListener(new ActionListener() {
2647+
@Override
2648+
public void actionPerformed(ActionEvent e) {
2649+
Base.openURL(clickedURL);
2650+
}
2651+
});
2652+
add(openURLItem);
2653+
2654+
openURLItemSeparator = new JSeparator();
2655+
add(openURLItemSeparator);
2656+
26432657
cutItem = new JMenuItem("Cut");
26442658
cutItem.addActionListener(new ActionListener() {
26452659
public void actionPerformed(ActionEvent e) {
26462660
handleCut();
26472661
}
26482662
});
2649-
this.add(cutItem);
2663+
add(cutItem);
26502664

26512665
copyItem = new JMenuItem("Copy");
26522666
copyItem.addActionListener(new ActionListener() {
26532667
public void actionPerformed(ActionEvent e) {
26542668
handleCopy();
26552669
}
26562670
});
2657-
this.add(copyItem);
2671+
add(copyItem);
26582672

26592673
discourseItem = new JMenuItem("Copy for Forum");
26602674
discourseItem.addActionListener(new ActionListener() {
26612675
public void actionPerformed(ActionEvent e) {
26622676
handleDiscourseCopy();
26632677
}
26642678
});
2665-
this.add(discourseItem);
2679+
add(discourseItem);
26662680

26672681
discourseItem = new JMenuItem("Copy as HTML");
26682682
discourseItem.addActionListener(new ActionListener() {
26692683
public void actionPerformed(ActionEvent e) {
26702684
handleHTMLCopy();
26712685
}
26722686
});
2673-
this.add(discourseItem);
2687+
add(discourseItem);
26742688

2675-
item = new JMenuItem("Paste");
2689+
JMenuItem item = new JMenuItem("Paste");
26762690
item.addActionListener(new ActionListener() {
26772691
public void actionPerformed(ActionEvent e) {
26782692
handlePaste();
26792693
}
26802694
});
2681-
this.add(item);
2695+
add(item);
26822696

26832697
item = new JMenuItem("Select All");
26842698
item.addActionListener(new ActionListener() {
26852699
public void actionPerformed(ActionEvent e) {
26862700
handleSelectAll();
26872701
}
26882702
});
2689-
this.add(item);
2703+
add(item);
26902704

2691-
this.addSeparator();
2705+
addSeparator();
26922706

26932707
item = new JMenuItem("Comment/Uncomment");
26942708
item.addActionListener(new ActionListener() {
26952709
public void actionPerformed(ActionEvent e) {
26962710
handleCommentUncomment();
26972711
}
26982712
});
2699-
this.add(item);
2713+
add(item);
27002714

27012715
item = new JMenuItem("Increase Indent");
27022716
item.addActionListener(new ActionListener() {
27032717
public void actionPerformed(ActionEvent e) {
27042718
handleIndentOutdent(true);
27052719
}
27062720
});
2707-
this.add(item);
2721+
add(item);
27082722

27092723
item = new JMenuItem("Decrease Indent");
27102724
item.addActionListener(new ActionListener() {
27112725
public void actionPerformed(ActionEvent e) {
27122726
handleIndentOutdent(false);
27132727
}
27142728
});
2715-
this.add(item);
2729+
add(item);
27162730

2717-
this.addSeparator();
2731+
addSeparator();
27182732

27192733
referenceItem = new JMenuItem("Find in Reference");
27202734
referenceItem.addActionListener(new ActionListener() {
27212735
public void actionPerformed(ActionEvent e) {
27222736
handleFindReference();
27232737
}
27242738
});
2725-
this.add(referenceItem);
2739+
add(referenceItem);
27262740
}
27272741

2742+
private boolean clickedURL(String line, int offset) {
2743+
String[] parse = SyntaxUtilities.parseCommentUrls(line);
2744+
if (parse==null)
2745+
return false;
2746+
int pos = parse[0].length()+parse[1].length();
2747+
if (offset<pos || offset>pos+2)
2748+
return false;
2749+
clickedURL = parse[1];
2750+
return true;
2751+
}
2752+
27282753
// if no text is selected, disable copy and cut menu items
27292754
public void show(Component component, int x, int y) {
2755+
int line = textarea.getLineOfOffset(textarea.xyToOffset(x, y));
2756+
if (clickedURL(textarea.getLineText(line), textarea.xToOffset(line, x))) {
2757+
openURLItem.setVisible(true);
2758+
openURLItemSeparator.setVisible(true);
2759+
} else {
2760+
openURLItem.setVisible(false);
2761+
openURLItemSeparator.setVisible(false);
2762+
}
2763+
27302764
if (textarea.isSelectionActive()) {
27312765
cutItem.setEnabled(true);
27322766
copyItem.setEnabled(true);

app/src/processing/app/syntax/SyntaxUtilities.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import javax.swing.text.*;
1313
import java.awt.*;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
1416

1517

1618
/**
@@ -148,7 +150,10 @@ public static int paintSyntaxLine(Segment line, Token tokens,
148150
styles[id].setGraphicsFlags(gfx,defaultFont);
149151

150152
line.count = length;
151-
x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
153+
if (id == Token.COMMENT1 || id == Token.COMMENT2)
154+
x = drawTabbedCommentsText(line, x, y, gfx, expander);
155+
else
156+
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
152157
line.offset += length;
153158
offset += length;
154159

@@ -158,6 +163,69 @@ public static int paintSyntaxLine(Segment line, Token tokens,
158163
return x;
159164
}
160165

166+
/**
167+
* Parse comments and identify "@schematics <b>&lt;something&gt;</b>" pattern.
168+
*
169+
* @param line
170+
* A string to parse
171+
* @return <b>null</b> if the pattern is not found, otherwise an array of
172+
* String is returned: the elements with index 0, 1 and 2 are
173+
* respectively the preamble, the <b>&lt;something&gt;</b> stuff, and
174+
* the remaining part of the string.
175+
*/
176+
public static String[] parseCommentUrls(String line) {
177+
// Try to find pattern
178+
Pattern schematics = Pattern.compile("@schematics\\s+([^\\s]+)");
179+
Matcher m = schematics.matcher(line.toString());
180+
if (!m.find())
181+
return null;
182+
183+
String res[] = new String[3];
184+
res[0] = line.substring(0, m.start(1));
185+
res[1] = line.substring(m.start(1), m.end(1));
186+
res[2] = line.substring(m.end(1));
187+
// System.out.println("0 =>"+res[0]+"<\n1 =>"+res[1]+"< \n2 =>"+res[2]+"<");
188+
return res;
189+
}
190+
191+
public static Segment stringToSegment(String v) {
192+
return new Segment(v.toCharArray(), 0, v.length());
193+
}
194+
195+
private static int drawTabbedCommentsText(Segment line, int x, int y,
196+
Graphics gfx, TabExpander expander) {
197+
198+
String parse[] = parseCommentUrls(line.toString());
199+
if (parse == null)
200+
// Revert to plain writing.
201+
return Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
202+
Segment pre = stringToSegment(parse[0]);
203+
Segment tag = stringToSegment(parse[1]);
204+
Segment post = stringToSegment(parse[2]);
205+
206+
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
207+
x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0);
208+
209+
// Draw arrow.
210+
FontMetrics metrics = gfx.getFontMetrics();
211+
int h = metrics.getHeight() - 2;
212+
drawArrow(gfx, x, y - h + metrics.getDescent() - 1, h, h);
213+
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
214+
return x;
215+
}
216+
217+
private static void drawArrow(Graphics gfx, int x, int y, int w, int h) {
218+
int h2 = h / 2;
219+
int h4 = h / 4;
220+
gfx.drawLine(x, y+h2, x+h2, y);
221+
gfx.drawLine(x+h2, y, x+h2, y+h4);
222+
gfx.drawLine(x+h2, y+h4, x+h, y+h4);
223+
gfx.drawLine(x+h, y+h4, x+h, y+h-h4);
224+
gfx.drawLine(x+h, y+h-h4, x+h2, y+h-h4);
225+
gfx.drawLine(x+h2,y+h-h4, x+h2, y+h);
226+
gfx.drawLine(x, y+h2, x+h2, y+h);
227+
}
228+
161229
// private members
162230
private SyntaxUtilities() {}
163231
}

0 commit comments

Comments
 (0)