19 |
import org.nongnu.libvob.*; |
import org.nongnu.libvob.*; |
20 |
import org.nongnu.libvob.vobs.*; |
import org.nongnu.libvob.vobs.*; |
21 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
22 |
|
import java.awt.Color; |
23 |
|
|
24 |
/** FenPDF context what handles keystrokes and |
/** FenPDF context what handles keystrokes and |
25 |
* accursed node at least. |
* accursed node at least. |
26 |
*/ |
*/ |
27 |
public class FenPDFContext implements |
public class FenPDFContext implements |
28 |
ContextNodeView.Context, |
ContextNodeView.Context, |
29 |
AbstractMainNode2D.Render, |
AbstractMainNode2D.Render |
|
AbstractMainNode2D.DoMouse |
|
30 |
{ |
{ |
31 |
public static boolean dbg = false; |
public static boolean dbg = false; |
32 |
private static void p(String s) { System.out.println("FenPDFContext:: "+s); } |
private static void p(String s) { System.out.println("FenPDFContext:: "+s); } |
43 |
} |
} |
44 |
|
|
45 |
|
|
|
private int cursorOffset = -1; |
|
|
public void setCursorOffset(int offset) { cursorOffset = offset; } |
|
46 |
|
|
47 |
private Object accursed = null, toBeLinked = null; |
// current main node nro. |
48 |
|
private int CURR = 0; |
49 |
|
public void initMainNodes(int count) { |
50 |
|
if (cursorOffset != null && |
51 |
|
cursorOffset.length == count) return; |
52 |
|
cursorOffset = new int[count]; |
53 |
|
accursedColor = new Color[]{ Color.red, Color.blue, |
54 |
|
Color.yellow, Color.white}; |
55 |
|
accursed = new Object[count]; |
56 |
|
for (int i=0; i<count; i++) { |
57 |
|
cursorOffset[i] = 0; |
58 |
|
} |
59 |
|
} |
60 |
|
public void setMainNodeIndex(int index) { |
61 |
|
CURR = index; |
62 |
|
if (dbg) p("set current: "+index); |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
|
private Color accursedColor[] = null; |
67 |
|
|
68 |
|
private int cursorOffset[] = null; |
69 |
|
public void setCursorOffset(int offset) { cursorOffset[CURR] = offset; } |
70 |
|
|
71 |
|
private Object[] accursed = null; |
72 |
public void setAccursed(Object node) { |
public void setAccursed(Object node) { |
73 |
accursed=node; |
accursed[CURR] = node; |
74 |
cursorOffset = 0; |
cursorOffset[CURR] = 0; |
75 |
} |
} |
76 |
|
|
77 |
|
private boolean isAccursed(Object node) { |
78 |
|
for (int i=0; i<accursed.length; i++) { |
79 |
|
if (accursed[i] == node) return true; |
80 |
|
} |
81 |
|
return false; |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
/** call back from ContextNodeView */ |
/** call back from ContextNodeView */ |
86 |
public void isCurrent(Object node, ContextNodeView nv) { |
public void isCurrent(Object node, ContextNodeView nv) { |
87 |
if (node==accursed) { |
if (isAccursed(node)) { |
88 |
nv.setUnCached(); |
nv.setUnCached(); |
89 |
} else { |
} else { |
90 |
nv.setCached(); |
nv.setCached(); |
93 |
|
|
94 |
/** call back from RSTText */ |
/** call back from RSTText */ |
95 |
public void isToBePlaced(Object node, TextHandler text) { |
public void isToBePlaced(Object node, TextHandler text) { |
96 |
if (dbg) p("n"+node+" == "+accursed); |
if (dbg) p("n"+node+" == "+accursed[CURR]); |
97 |
if (node==accursed) { |
|
98 |
text.setColor(java.awt.Color.red); |
if (isAccursed(node)) { |
99 |
text.setCursorOffset(cursorOffset); |
float count=0; |
100 |
|
float r = 0, g = 0, b = 0; |
101 |
|
for (int i=0; i<accursed.length; i++) { |
102 |
|
if (accursed[i]==node) { |
103 |
|
r += accursedColor[i].getRed(); |
104 |
|
g += accursedColor[i].getGreen(); |
105 |
|
b += accursedColor[i].getBlue(); |
106 |
|
count++; |
107 |
|
} |
108 |
|
} |
109 |
|
r /= count; |
110 |
|
g /= count; |
111 |
|
b /= count; |
112 |
|
//p("colo: " +r+" "+g+" "+b); |
113 |
|
text.setColor(new Color(r/255,g/255,b/255)); |
114 |
|
text.setCursorOffset(cursorOffset[CURR]); |
115 |
} else { |
} else { |
116 |
text.setColor(java.awt.Color.black); |
text.setColor(java.awt.Color.black); |
117 |
text.setCursorOffset(-1); |
text.setCursorOffset(-1); |
120 |
|
|
121 |
|
|
122 |
private boolean cursor = false; |
private boolean cursor = false; |
123 |
private boolean leftLink = false; |
private boolean link = false; |
|
private boolean rigthLink = false; |
|
|
private boolean setOrigin = false; |
|
|
private boolean unSetOrigin = false; |
|
124 |
|
|
125 |
/** call back from AbstractMainNodeView2D */ |
/** call back from AbstractMainNodeView2D */ |
126 |
public void mainNodeToBeRender(VobScene vs, int into, |
public void mainNodeToBeRender(VobScene vs, int into, |
128 |
{ |
{ |
129 |
// first set everything to false |
// first set everything to false |
130 |
cursor = false; |
cursor = false; |
131 |
leftLink = false; |
link = false; |
|
rigthLink = false; |
|
|
setOrigin = false; |
|
|
unSetOrigin = false; |
|
132 |
|
|
133 |
// if in canvas |
// if in canvas |
134 |
if (main instanceof MainNode2D) { |
if (main instanceof MainNode2D) { |
135 |
// what to do if accursed is NULL? |
// what to do if accursed is NULL? |
136 |
if (accursed==null) { |
if (accursed[CURR]==null) { |
137 |
if (main instanceof MainNode2D) |
if (main instanceof MainNode2D) |
138 |
cursor = true; |
cursor = true; |
139 |
// fishey doesn't need cursor (currently) |
// fishey doesn't need cursor (currently) |
140 |
} |
} |
141 |
// and what to do if accursed is choosed? |
if (accursed.length >= 2) { |
142 |
else { |
if (accursed[0] != accursed[1] && |
143 |
if (toBeLinked == null) setOrigin = true; |
accursed[0] != null && |
144 |
if (toBeLinked != null) unSetOrigin = true; |
accursed[1] != null) link = true; |
145 |
|
} |
|
if (toBeLinked != accursed) { |
|
|
setOrigin = true; |
|
|
|
|
|
// and if link from has been choosed? |
|
|
if (toBeLinked != null) { |
|
|
leftLink = true; |
|
|
rigthLink = true; |
|
|
} |
|
|
} |
|
|
} |
|
146 |
} |
} |
147 |
|
|
148 |
//render! |
//render! |
149 |
renderContext(vs, into, main); |
renderMain(vs, into, main); |
150 |
} |
} |
151 |
|
|
152 |
private void renderContext(VobScene vs, int into, |
private void renderMain(VobScene vs, int into, |
153 |
AbstractMainNode2D main) |
AbstractMainNode2D main) |
154 |
{ |
{ |
|
|
|
155 |
if (cursor) { |
if (cursor) { |
156 |
float tmp[] = new float[2]; |
float tmp[] = new float[2]; |
157 |
vs.coords.getSqSize(into, tmp); |
vs.coords.getSqSize(into, tmp); |
159 |
tmp[0]/2, tmp[1]/2, 1,1, tmp[0]/12, tmp[1]/12); |
tmp[0]/2, tmp[1]/2, 1,1, tmp[0]/12, tmp[1]/12); |
160 |
vs.put(new LineVob(0,0,0,1, java.awt.Color.darkGray), cursor); |
vs.put(new LineVob(0,0,0,1, java.awt.Color.darkGray), cursor); |
161 |
} |
} |
162 |
|
} |
163 |
|
|
164 |
Button b_always = new Button(vs, 0, GraphicsAPI.getInstance(). |
public void renderContext(VobScene vs, int into) |
165 |
|
{ |
166 |
|
Button b = new Button(vs, 0, GraphicsAPI.getInstance(). |
167 |
getTextStyle("sans", 0, 24), |
getTextStyle("sans", 0, 24), |
168 |
30, 0, 50); |
30, 0, 50); |
169 |
b_always.add("[Import PS/PDF]", "IMPORT"); |
b.add("[Import PS/PDF]", "IMPORT"); |
170 |
|
b.add("[New paper]", "NEW_PAPER"); |
171 |
|
|
172 |
Button b = new Button(vs, 0, GraphicsAPI.getInstance(). |
if (link) b.add("[Link!]", "LINK"); |
|
getTextStyle("sans", 0, 24), |
|
|
40, 25, 50); |
|
|
if (leftLink) b.add("[<--]", "LINK_LEFT"); |
|
|
if (setOrigin) b.add("[Set link origin]", "SET_ORIG"); |
|
|
if (unSetOrigin)b.add("[Unset link origin]","UNSET_ORIG"); |
|
|
if (rigthLink) b.add("[-->]", "LINK_RIGTH"); |
|
173 |
} |
} |
174 |
|
|
175 |
|
|
176 |
public boolean doMouse(MouseEvent e, VobScene oldVS, |
/** @return object of new plane for mainnode view 2d |
177 |
AbstractMainNode2D main) { |
*/ |
178 |
if (e.getID() != MouseEvent.MOUSE_CLICKED) return false; |
public Object doMouse(MouseEvent e, VobScene oldVS) throws RemoteException { |
179 |
|
if (e.getID() != MouseEvent.MOUSE_CLICKED) return null; |
180 |
int cs = oldVS.coords.getCSAt(0, e.getX(), e.getY(), null); |
int cs = oldVS.coords.getCSAt(0, e.getX(), e.getY(), null); |
181 |
if(cs < 0) return false; |
if(cs < 0) return null; |
182 |
Object obKey = oldVS.matcher.getKey(cs); |
Object obKey = oldVS.matcher.getKey(cs); |
183 |
if (obKey == null) return false; |
if (obKey == null) return null; |
184 |
if (obKey instanceof String) { |
if (obKey instanceof String) { |
185 |
String key = (String)obKey; |
String key = (String)obKey; |
186 |
if (dbg) p("found something! "+key); |
if (dbg) p("found something! "+key); |
187 |
if (key.equals("SET_ORIG")) actionSetLinkOrigin(); |
if (key.equals("IMPORT")) p("NOT IMPLEMENTED"); |
188 |
else if (key.equals("UNSET_ORIG")) actionUnSetLinkOrigin(); |
else if (key.equals("LINK")) { |
189 |
else if (key.equals("LINK_LEFT")) actionLinkRigth(); |
if (CURR >0) actionLink(1); else actionLink(-1); |
190 |
else if (key.equals("LINK_RIGTH")) actionLinkLeft(); |
} |
191 |
|
else if (key.equals("NEW_PAPER")) { |
192 |
else if (key.equals("IMPORT")) p("NOT IMPLEMENTED"); |
String paper = ppAct.newPaper(); |
193 |
else return false; |
return new MainNode2D(Nodes.get(paper), null, 0,0,1); |
194 |
|
} |
195 |
|
else return null; |
196 |
|
|
197 |
AbstractUpdateManager.chg(); |
AbstractUpdateManager.chg(); |
198 |
return true; |
return null; |
199 |
} |
} |
200 |
return false; |
return null; |
|
} |
|
|
|
|
|
private void actionUnSetLinkOrigin() { |
|
|
toBeLinked = null; |
|
|
} |
|
|
private void actionSetLinkOrigin() { |
|
|
p("set origin"); |
|
|
if (toBeLinked==accursed) |
|
|
toBeLinked = null; |
|
|
else toBeLinked = accursed; |
|
201 |
} |
} |
202 |
|
|
203 |
private void actionLinkLeft() { actionLink(-1); } |
private void actionLinkLeft() { actionLink(-1); } |
204 |
private void actionLinkRigth() { actionLink(1); } |
private void actionLinkRigth() { actionLink(1); } |
205 |
private void actionLink(int side) { |
private void actionLink(int side) { |
206 |
if (toBeLinked==null) { |
{ try { |
|
p("to be linked is null?!"); |
|
|
return; |
|
|
} |
|
|
else if (accursed==toBeLinked) { |
|
|
p("to be linked to itself??!"); |
|
|
return; |
|
|
} else { try { |
|
207 |
if (side < 0) { |
if (side < 0) { |
208 |
ppAct.assocNotes(Nodes.toString(toBeLinked), -1, |
ppAct.assocNotes(Nodes.toString(accursed[0]), -1, |
209 |
Nodes.toString(accursed)); |
Nodes.toString(accursed[1])); |
210 |
} else { |
} else { |
211 |
ppAct.assocNotes(Nodes.toString(toBeLinked), 1, |
ppAct.assocNotes(Nodes.toString(accursed[0]), 1, |
212 |
Nodes.toString(accursed)); |
Nodes.toString(accursed[1])); |
213 |
} |
} |
214 |
} catch (Exception e) { |
} catch (Exception e) { |
215 |
p("exception - do nothing.."+e); |
p("exception - do nothing.."+e); |
223 |
p("s: "+stroke+", v: "+main); |
p("s: "+stroke+", v: "+main); |
224 |
|
|
225 |
if (stroke.equals("Return")) { |
if (stroke.equals("Return")) { |
226 |
if (accursed==null) return; |
if (accursed[CURR]==null) return; |
227 |
|
|
228 |
String acc = Nodes.toString(accursed); |
String acc = Nodes.toString(accursed[CURR]); |
229 |
ppAct.insertText(acc, cursorOffset, "\n"); |
ppAct.insertText(acc, cursorOffset[CURR], "\n"); |
230 |
cursorOffset++; |
cursorOffset[CURR]++; |
|
} |
|
|
else if (stroke.equals("Ctrl-C")) { |
|
|
actionSetLinkOrigin(); |
|
|
} |
|
|
else if (stroke.equals("Ctrl-X")) actionLinkLeft(); |
|
|
else if (stroke.equals("Ctrl-V")) { |
|
|
actionLinkRigth(); |
|
231 |
} |
} |
232 |
else if (stroke.equals("Backspace")) { |
else if (stroke.equals("Backspace")) { |
233 |
if (accursed==null) return; |
if (accursed[CURR]==null) return; |
234 |
|
|
235 |
String note = Nodes.toString(accursed); |
String note = Nodes.toString(accursed[CURR]); |
236 |
if (cursorOffset >= 1) { |
if (cursorOffset[CURR] >= 1) { |
237 |
ppAct.deleteText(note, |
ppAct.deleteText(note, |
238 |
cursorOffset - 1, |
cursorOffset[CURR] - 1, |
239 |
cursorOffset); |
cursorOffset[CURR]); |
240 |
cursorOffset--; |
cursorOffset[CURR]--; |
241 |
String text = alphContent.getText(fen.constgraph, accursed); |
String text = alphContent.getText(fen.constgraph, |
242 |
if (cursorOffset == 0 && |
accursed[CURR]); |
243 |
|
if (cursorOffset[CURR] == 0 && |
244 |
text.length() == 0) { |
text.length() == 0) { |
245 |
if (isLinked(accursed)){ |
if (isLinked(accursed[CURR])){ |
246 |
ppAct.insertText(note, 0, "#"); |
ppAct.insertText(note, 0, "#"); |
247 |
} else { |
} else { |
248 |
ppAct.deleteNote(note); |
ppAct.deleteNote(note); |
249 |
accursed = null; |
accursed[CURR] = null; |
250 |
cursorOffset = -1; |
cursorOffset[CURR] = -1; |
251 |
} |
} |
252 |
} |
} |
253 |
} |
} |
254 |
} |
} |
255 |
else if (stroke.equals("Left")) { |
else if (stroke.equals("Left")) { |
256 |
if (accursed==null) return; |
if (accursed[CURR]==null) return; |
257 |
if (cursorOffset > 0) cursorOffset--; |
if (cursorOffset[CURR] > 0) cursorOffset[CURR]--; |
258 |
} |
} |
259 |
else if (stroke.equals("Right")) { |
else if (stroke.equals("Right")) { |
260 |
if (accursed==null) return; |
if (accursed[CURR]==null) return; |
261 |
String text = alphContent.getText(fen.constgraph, accursed); |
String text = alphContent.getText(fen.constgraph, accursed[CURR]); |
262 |
if ( cursorOffset < text.length()) |
if ( cursorOffset[CURR] < text.length()) |
263 |
cursorOffset++; |
cursorOffset[CURR]++; |
264 |
} |
} |
265 |
else if (stroke.length() == 1) { |
else if (stroke.length() == 1) { |
266 |
if (accursed==null) { |
if (accursed[CURR]==null) { |
267 |
String paper = Nodes.toString(main.getPlane()); |
String paper = Nodes.toString(main.getPlane()); |
268 |
String note = ppAct.newNote(paper, (int)main.getPanX(), |
String note = ppAct.newNote(paper, (int)main.getPanX(), |
269 |
(int)main.getPanY(), ""); |
(int)main.getPanY(), ""); |
270 |
cursorOffset = 0; |
cursorOffset[CURR] = 0; |
271 |
accursed = Nodes.get(note); |
accursed[CURR] = Nodes.get(note); |
272 |
} |
} |
273 |
String acc = Nodes.toString(accursed); |
String acc = Nodes.toString(accursed[CURR]); |
274 |
ppAct.insertText(acc, cursorOffset, stroke); |
ppAct.insertText(acc, cursorOffset[CURR], stroke); |
275 |
cursorOffset++; |
cursorOffset[CURR]++; |
276 |
return; |
return; |
277 |
} else p("No keystroke defined!"); |
} else p("No keystroke defined!"); |
278 |
} catch (RemoteException e) { |
} catch (RemoteException e) { |