package Current.popups.Unfold; import Current.gui.*; import Current.basic.*; import Current.manage.*; import Current.plot.Dyadic.*; import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class UnfoldControlCanvas extends DBCanvas implements MouseListener { public Manager M; public UnfoldCanvas U; public UnfoldFunctionCanvas F; public UnfoldContainer UContainer; public ListenSquare[] L=new ListenSquare[6]; public int choice,select; public int[] t=new int[4]; public int[] v1=new int[5]; public int[] v2=new int[5]; public VertexPair CV; public UnfoldControlCanvas(Manager M, UnfoldCanvas U, UnfoldFunctionCanvas F) { // Add the Manager this.M=M; // Add UnfoldCanvas this.U=U; // Add UnfoldFunctionCanvas this.F=F; addMouseListener(this); //buttons L[1]=new ListenSquare(3,3,74,30,Color.yellow); //leaders L[2]=new ListenSquare(3,36,74,30,new Color(0,200,0)); //function L[3]=new ListenSquare(3,73,43,18,new Color(160,0,0)); //vertex selector L[4]=new ListenSquare(3,93,43,18,new Color(160,0,0)); //vertex selector L[5]=new ListenSquare(3,115,12,12,Color.white); //spine info L[2].on=1; L[3].on=1; //set variables v1[0]=1; for(int i=1;i<5;++i) { v1[i]=0; v2[i]=0; } CV=new VertexPair(0,0,0,0); choice=0; select=-1; } // set this if it is contained in an UnfoldContainer public void addUnfoldContainer(UnfoldContainer UContainer){ this.UContainer=UContainer; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawControls(g); showData(g); } public void drawControls(Graphics g) { int h=this.getHeight(); g.setColor(new Color(130,0,0)); g.fillRect(0,0,80,h); g.setColor(Color.white); g.drawRect(0,0,79,h); L[1].render(g,new Color(50,100,255)); L[3].render(g,new Color(0,0,0)); L[4].render(g,new Color(0,0,0)); L[2].render(g,new Color(50,100,255)); L[5].infoRender(g); g.setColor(Color.white); g.setFont(new Font("Helvetica",Font.PLAIN,10)); g.drawString("compute",7,15); g.drawString("leaders",7,28); g.drawString("open function",7,48); g.drawString("window",7,61); } public void resetSpine() { eraseMarks(); for(int i=1;i<=3;++i) {v1[i]=0;v2[i]=0;} } public void showData(Graphics g) { int n=U.W.length(); g.translate(43,-40); if((choice>0)&&(select<=n)) { CombinatorialTriangle T=CombinatorialTriangle.copy(U.CT[select]); g.setColor(Color.white); if(choice==1) { Integer I0=new Integer(T.b[0]); Integer I1=new Integer(T.b[1]); Integer I2=new Integer(T.b[2]); g.drawString(I0.toString(),7,125); g.drawString(I1.toString(),7,137); g.drawString(I2.toString(),7,149); } if((choice>1)&&(choice<=4)) { int[] m=new int[3]; if(choice==2) m=T.d[2]; if(choice==3) m=T.d[0]; if(choice==4) m=T.d[1]; Integer I0=new Integer(m[0]); Integer I1=new Integer(m[1]); g.drawString(I0.toString(),7,130); g.drawString(I1.toString(),7,142); } if(choice>4) { String S=new String(); int[] m=Converter.typeToCoords(T,choice-5); int q=Converter.coordsToType(T,m[0]); Integer I2=new Integer(q); g.drawString(I2.toString(),7,137); } } g.translate(-43,40); g.setColor(Color.white); String S1=new String(); Integer I1=new Integer(v1[3]); if(v1[1]==1) S1="a"; if(v1[1]==2) S1="b"; if(v1[3]>0) { g.drawString(S1,7,86); g.drawString(I1.toString(),14,86); } String S2=new String(); Integer I2=new Integer(v2[3]); if(v2[1]==1) S2="a"; if(v2[1]==2) S2="b"; if(v2[3]>0) { g.drawString(S2,7,106); g.drawString(I2.toString(),14,106); } } public void recolor(Point X,int n) { if(n>0) { Color C=UContainer.colors.selectColor; Complex B=U.B; double A=U.A; choice=0; for(int i=n+1;i>0;--i) { if(choice==0) { choice=U.T[i].modify(A,B,X,C,8); if(choice>0) select=i; } } if(select>0) { CombinatorialTriangle T=CombinatorialTriangle.copy(U.CT[select]); int[] m=new int[3]; if(choice>4) { m=Converter.typeToCoords(T,choice-5); if(v1[0]==1) { if((m[0]!=v2[1])||(m[1]!=v2[3])) { v1[1]=m[0]; //up or down v1[2]=select; //triangle number v1[3]=m[1]; //vertex number v1[4]=choice-5; } } if(v1[0]==2) { if((m[0]!=v1[1])||(m[1]!=v1[3])) { v2[1]=m[0]; v2[2]=select; v2[3]=m[1]; v2[4]=choice-5; } } } } repaint(); } } public void mouseEntered(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) { e.consume(); Point X=new Point(); X.x=e.getX(); X.y=e.getY(); if(L[1].inside(X)==1) L[1].on=1-L[1].on; if(L[3].inside(X)==1) {L[3].on=1;L[4].on=0;v1[0]=1;} if(L[4].inside(X)==1) {L[3].on=0;L[4].on=1;v1[0]=2;} if(L[5].inside(X)==1) document1(); if(L[2].inside(X)==1) { L[2].on=1-L[2].on; if (UContainer!=null) if (L[2].on==1) UContainer.showFunction(); else UContainer.hideFunction(); } repaint(); U.repaint(); } public void spineFunction() { if((v1[1]!=v2[1])||(v1[2]!=v2[2])) { if((v1[0]>0)&&(v1[1]*v2[1]>0)) { doSpine(); if(L[2].on==1) { CV=new VertexPair(v1[1],v1[2],v2[1],v2[2]); U.T[v1[2]].on[5+v1[4]]=1; U.T[v2[2]].on[5+v2[4]]=1; doFunction(CV,F.choice); F.repaint(); } } } } public void eraseMarks() { for(int i=0;i<=U.W.length();++i) { for(int j=0;j<=8;++j) U.T[i].on[j]=0; for(int j=1;j<=3;++j) U.T[i].C[j]=UContainer.colors.edgeColor; } } /**This routine deals with the situation where we have selected some vertices on a long unfolding, and then we change words so that the corresponding unfolding is shorter. In this case we set the selected vertices to the initial state*/ public void checkOverextension() { int test2=0; int wlen=U.W.length(); if(v1[2]>wlen) test2=1; if(v2[2]>wlen) test2=1; if(test2==1) { v1[1]=1; v1[2]=1; v2[1]=2; v2[2]=1; v1[3]=1; v2[3]=1; repaint(); } } public void doSpine() { if((v1[1]>0)&&(v1[1]*v2[1]>0)) { checkOverextension(); CV=new VertexPair(v1[1],v1[2],v2[1],v2[2]); M.mcbSend(new VertexPairDummy(CV)); eraseMarks(); U.T[v1[2]].on[5+v1[4]]=1; U.T[v2[2]].on[5+v2[4]]=1; int spine=Converter.spineNumber(CV,U.CT); int[] n=Spine.computeSpine(CV,U.CT); int sp=spine; if(sp==0) sp=3; int test=F.choice*Converter.palindromeTest(U.CT); if(test==0) { /**denominator - not computed in palindrome case*/ int[] d=Spine.fullSpine(spine,U.CT); for(int i=1;i<=d[0];++i) { U.T[d[i]].on[sp]=2; U.T[d[i]].C[sp]=UContainer.colors.spineColor; if(U.CT[d[i]+1].c[0]==spine) { U.T[d[i]+1].on[sp]=2; U.T[d[i]+1].C[sp]=UContainer.colors.spineColor; } } } /**numerator*/ for(int i=1;i<=n[0];++i) { U.T[n[i]].on[sp]=1; U.T[n[i]].C[sp]=UContainer.colors.saddleColor; if(U.CT[n[i]+1].c[0]==spine) { U.T[n[i]+1].on[sp]=1; U.T[n[i]+1].C[sp]=UContainer.colors.saddleColor; } } /**goes to the word window*/ int[] q=new int[4]; q[0]=spine+1; q[1]=n[1]; q[2]=n[n[0]]; M.mcbSend(q); } F.repaint(); repaint(); } /**Computing the functions*/ public void doFunction(VertexPair V,int choice) { int test=choice*Converter.palindromeTest(U.CT); if(test==0) doRegularFunction(V); if(test==1) doPalindromeFunction(V); } public void doRegularFunction(VertexPair V) { int[][] n=Function.computeNumerator(V,U.CT); int[][] d=Function.computeDenominator(V,U.CT); int sign=Function.getSign(n[0][1],d); F.n[0][0]=n[0][0]; for(int j=0;j<=3;++j) { for(int i=0;i<=n[0][0];++i) { if(j<3) F.n[j][i]=n[j][i]; if(j==3) F.n[j][i]=sign*n[j][i]; if(j<3) F.N[j][i]=new Integer(n[j][i]); if(j==3) F.N[j][i]=new Integer(sign*n[j][i]); }} F.d[0][0]=d[0][0]; for(int j=0;j<=3;++j) { for(int i=0;i<=d[0][0];++i) { F.d[j][i]=d[j][i]; F.D[j][i]=new Integer(d[j][i]); }} int[][] f=Function.foil(F.n,F.d); F.f[0][0]=f[0][0]; for(int j=0;j<=2;++j) { for(int i=1;i<=f[0][0];++i) { F.f[j][i]=f[j][i]; F.F[j][i]=new Integer(f[j][i]); }} } public void doPalindromeFunction(VertexPair V) { F.palindrome=1; int[][] f=PalindromeFunction.getFunction(V,U.CT); F.n[0][0]=f[0][0]; for(int j=1;j<=3;++j) { for(int i=0;i<=f[0][0];++i) { F.n[j][i]=f[j-1][i]; F.N[j][i]=new Integer(f[j-1][i]); }} F.d[0][0]=0; F.f[0][0]=f[0][0]; for(int j=0;j<3;++j) { for(int i=1;i<=f[0][0];++i) { F.f[j][i]=f[j][i]; F.F[j][i]=new Integer(f[j][i]); }} } public void document1() { M.mcbSend(new HelpDocument("UnfoldCanvas.html")); } }