Blame view

circus/src/circusRefine/gui/TelaCodigo.java 7.54 KB
8d0dc533f   Madiel de Souza Conserva Filho   first
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  package circusRefine.gui;
  
  /*
  * Projeto : Refine - Ferramenta Educacional para Refinamentos
  *
  * Tipo : TelaCodigo
  *
  */
  
  import circusRefine.core.ExternalManager;
  import circusRefine.core.print.Printer;
  //import circusRefine.core.printUni.PrinterUni;
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.FlowLayout;
  import java.awt.SystemColor;
  
  import javax.swing.JFileChooser;
  import javax.swing.JFrame;
  import javax.swing.JInternalFrame;
  import javax.swing.JLabel;
  import javax.swing.JMenu;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  import javax.swing.JPopupMenu;
  import javax.swing.JTabbedPane;
  import javax.swing.JTextArea;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  
  import jcircus.JCircusControllerFrame;
  import jcircus.gui.JCircusFrame;
  
  
  
  import net.sourceforge.czt.base.ast.Term;
  
  
  /**
  * TelaCodigo � um tipo que representa a tela do c�digo gerado.
  */
  public class TelaCodigo extends JInternalFrame {
  
  	/**
  	 * 
  	 */
  	private static final long serialVersionUID = 1L;
  	private ExternalManager gerInterface;
  	public static final int Unicode = 1;
  	public static final int Latex = 2;
  	JTextArea TACodigo = new JTextArea();
  	JScrollPane rolagem;
  
  	JCircusControllerCodigo controllerCodigo = new JCircusControllerCodigo ();
  	JPopupMenu menuTraducao = new JPopupMenu (); //By Samuel Barrocas
  	JMenu jcircus = new JMenu ("JCircus"); //By Samuel Barrocas
  	JMenuItem translate = new JMenuItem ("Translate 2 Java"); //By Samuel Barrocas
  	private String filePath = "";//By Samuel Barrocas
  	public void setFilePath (String p) { //By Samuel Barrocas
  		this.filePath = p;
  	}
  	public String getFilePath () { //By Samuel Barrocas
  		return this.filePath;
  	}
  	
     /**
  	* Construtor da classe TelaCodigo.
  	*
  	* @param gerInterface       Gerenciador de todas as telas do sistema.
  	*
  	*/
  	public TelaCodigo(ExternalManager gerInterface) {
  		
  		super (gerInterface.getMessage("COD0083"),true,false,true,true);
  		
  		//resizeble
    		//maximizable
    		//iconifiable
  		
  		try  {
  			this.gerInterface = gerInterface;
  			jbInit();
  		}
  		catch (Exception e) {
  			e.printStackTrace();
  		}
  	}
  
     /**
  	* M�todo que retorna uma String representando todo o conte�do da tela de
  	* c�digo.
  	*
  	* @return        Uma String representando todo o conte�do da tela de
  	*                c�digo.
  	*
  	*/
  	public String getTextoCodigo() {
  		String textoCodigo = TACodigo.getText();
  		return textoCodigo;
  	}
  	public static String pathFromJFileChooser (JFileChooser jfc) {
  		String path = "";
  		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  		if(jfc.showOpenDialog(jfc) == JFileChooser.APPROVE_OPTION){   
              path = jfc.getSelectedFile().getPath();   
          }
  		return path;
  	}
  
  	/**
  	  * Atualiza os campos de InfoTelas referentes a janela de Codigo
  	  */
  	public void atualizarInfoTela () {
  		this.gerInterface.getInfoTelas().AtualizarCod(this);
  	}
  	
     /**
  	* Inicializa os atributos da tela de c�digo.
  	*
  	*/
    	private void jbInit() throws Exception {
  		this.setBackground(SystemColor.scrollbar);
  
  		this.setLocation(   ( (gerInterface.getTamanhoHorizontal()*545)/800 ) ,  ( (gerInterface.getTamanhoVertical()*6)/600));
  		this.setSize(( (gerInterface.getTamanhoHorizontal()*240) /800) , ( (gerInterface.getTamanhoVertical()*477)/600) );
  
  		this.addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener () {
  			public void ancestorMoved(java.awt.event.HierarchyEvent e) {
  			}
  			public void ancestorResized(java.awt.event.HierarchyEvent e) {
  				atualizarInfoTela();
  			}
  		}
  		);
  
  		translate.addActionListener(
  			new ActionListener () {
  				public void actionPerformed (ActionEvent e) {
  		
  					String path = "";
  					JFileChooser chooser = new JFileChooser ();
  					path = pathFromJFileChooser (chooser);
  		
  
  					try {
  						controllerCodigo.translate(getFilePath(), path, "paper", getNameFromPath (path), true, true, false/*, 0, max*/);
  					} catch (Exception e1) {
  						// TODO Auto-generated catch block
  						System.out.println (e1);
  					}
  				}
  			}
  		);
  
  		jcircus.add(translate);
  		menuTraducao.add(jcircus);
  
  		TACodigo.addMouseListener (
  			new MouseListener () {
  				public void mouseClicked(MouseEvent arg0) {
  					//System.out.println ("Mouse Clicked");
  					if (arg0.getButton() == arg0.BUTTON1) {
  						//System.out.println ("Esquerdo");
  					}
  					else if (arg0.getButton() == arg0.BUTTON3) { //Botão direito do mouse
  						menuTraducao.show(arg0.getComponent(), arg0.getX(), arg0.getY());
  					}
  				}
  				public void mouseEntered(MouseEvent arg0) {
  					//System.out.println ("Mouse Entered");
  				}
  				public void mouseExited(MouseEvent arg0) {
  					//System.out.println ("Mouse Exited");
  				}
  				public void mousePressed(MouseEvent arg0) {
  					System.out.println ("Mouse Pressed");
  				}
  				public void mouseReleased(MouseEvent arg0) {
  					System.out.println ("Mouse Released");
  				}
  			}
  		);
  
  		this.add(menuTraducao);
  		//this.setTitle(this.retornarMensagem("COD0083"));
  	    this.setLayout(new BorderLayout());
  		TACodigo.setBackground(Color.white);
  		TACodigo.setBounds(-3, 0, 334, 218);
  		TACodigo.setFont(new Font("CZT", 4, 12));
  		TACodigo.setEditable(false);
  		rolagem = new JScrollPane(TACodigo);
  		this.add(rolagem, BorderLayout.CENTER);
    	} 
    	
    	public String getNameFromPath (String path) {
    		String str = "";
    		int b2 = path.length() - 1;
    		int aux = b2;
    		for (int i = b2; i >= 0; i--) {
    			if (path.charAt(i) == '/') {
    				aux = i;
    			}
    		}
    		return path.substring (aux + 1, b2);
    	}
    	
     /**
  	* Limpa o conte�do de texto que est� na tela de c�digo.
  	*
  	*/
  	public void limparTexto() {
  		TACodigo.setText("");
  	}
     /**
  	* M�todo que funciona como uma esp�cie de reset da tela de c�digo.
  	* Respons�vel por inicializar a maioria dos atributos desta tela.
  	*
  	* @param gerInterface       Gerenciador de todas as telas do sistema.
  	*
  	*/
  	public void novaTela(ExternalManager gerInterface) {
  		TACodigo.setText("");
  		this.gerInterface = gerInterface;
  	}
  
    /**
  	* M�todo que retorna uma mensagem que ser� impressa na tela, tanto na forma
  	* de t�tulo quanto na forma de mensagem de erro.
  	*
  	* @param codigo     O c�digo da mensagem que ser� retornada.
  	*
  	* @return           Uma String que representa a mensagem de retorno.
  	*
  	*/
  	public String retornarMensagem(String codigo) {
  		String mensagem = gerInterface.getMessage(codigo);
  		return mensagem;
  	}
     /**
  	* M�todo que limpa o texto anterior da tela e coloca o novo texto.
  	*
  	* @param programa               Programa � ser colocado na tela de c�digo.
  	*
  	*/
  	public void setarTela(Term programa) {
  		this.limparTexto();
  		this.setarTelaTexto(programa);
  	}
     /**
  	* M�todo que pega uma String obtida a partir de um programa e seta na
  	* tela no formato padr�o de c�lculo de programas.
  	*
  	* @param programa               Programa a ser colocado na tela.
  	*
  	*/
  	private void setarTelaTexto(Term programa) {
  		
  		Printer printer = null;
  		
  		printer = new Printer(gerInterface.getMark(),false);
  
  		String[] strProg = (String[])printer.visitTerm(programa);
  		
  		String strShow = "";
  		int tamTexto = strProg.length;
  
  		for(int i=0; i<tamTexto; i++) {
  			strShow = strShow + strProg[i] + "
  ";
  		}
  
  		TACodigo.setText(strShow);
  	}
  }