/* A basic implementation of the JFrame class. */ package application.texteditor; import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*; import java.net.*; import com.symantec.itools.javax.swing.icons.ImageIcon; import controlledparts.*; import nodesystem.*; public class TextEditFrame extends ControlledFrame implements DialogListener, EditDialog, FrameWithControlledButton, FrameWithControlledPane, FrameWithControlledTextAreas { public void clearAll() { this.textArea.setText(""); } public void setUrlField(JLabel x) { this.urlField=x; } public JLabel getUrlField() { return this.urlField; } public void setTextOnTheText(int id, String s) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); t.setText(s); } public void sendAll() { this.textArea.ControlledTextArea_textPasted(); } public void receiveEvent(String s) { if(!this.communicationNode.isReceivingEvents) return; InputQueue iq=null; try{ BufferedReader dinstream=new BufferedReader( new StringReader(s) ); iq=new InputQueue(dinstream); } catch(Exception e){ System.out.println("exception:"+e); return; } if(iq==null) return; ParseTextEditEvent evParser=new ParseTextEditEvent(this,iq); evParser.parsingString=s; evParser.run(); } public void setCommunicationNode(CommunicationNode c) { this.communicationNode=c; } public ControlledFrame spawnMain(CommunicationNode cn, String args, int pID, String controlMode) { TextEditFrame f=this; f.setTitle("DSR/Text Editor"); f.communicationNode=cn; f.pID=pID; // wf.encodingCode=encodingCode; f.ebuff=cn.commandTranceiver.ebuff; // wf.show(); f.setControlledFrame(null); f.textArea.setText(""); f.setListener(cn); f.setVisible(true); return f; } public boolean isControlledByLocalUser() { // This method is derived from interface FrameWithControlledPane // to do: code goes here if(this.dialogListener!=null) { return this.dialogListener.isControlledByLocalUser(); } if(this.communicationNode!=null){ return this.communicationNode.isControlledByLocalUser(); } else return true; } public void sendEvent(String x) { if(frame==null) { if(this.communicationNode==null) return; this.communicationNode.sendEvent("tedit","txtedit."+x); return; } frame.sendEvent("txtedit."+x); } public boolean isShowingRmouse() { if(frame!=null){ if(frame.communicationNode!=null) return this.frame.communicationNode.rmouseIsActivated; } if(this.communicationNode!=null) return communicationNode.rmouseIsActivated; return false; } public void moveMouseOnTheText(int id, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); // t.rmouse.move(x,y); t.moveMouse(x,y); } public void mouseMoveAtTextArea(int id, int x, int y) { } public void exitMouseOnTheText(int id, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); // t.rmouse.setVisible(false); t.exitMouse(); } public void mouseExitAtTheText(int id, int x, int y) { } public void enterMouseOnTheText(int id, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); // t.rmouseIsShown=true; // t.rmouse.setVisible(true); // t.moveMouse(x,y); t.enterMouse(x,y); } public void mouseEnteredAtTheText(int id, int x, int y) { } public void gainFocus() { // This method is derived from interface FrameWithControlledFocus // to do: code goes here this.requestFocus(); } public void loseFocus() { // This method is derived from interface FrameWithControlledFocus // to do: code goes here } public void pressMouseOnTheText(int id, int p, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); t.pressMouse(p,x,y); } public void mousePressedAtTextArea(int id, int p, int x, int y) { if(frame==null) return; } public void showScrollBar(int paneID, int barID) { ControlledScrollPane p=(ControlledScrollPane)(panes.elementAt(paneID)); p.showScrollBar(barID); } public void hideScrollBar(int paneID, int barID) { ControlledScrollPane p=(ControlledScrollPane)(panes.elementAt(paneID)); p.hideScrollBar(barID); } public void changeScrollbarValue(int paneID, int barID, int value) { ControlledScrollPane p=(ControlledScrollPane)(panes.elementAt(paneID)); p.setScrollBarValue(barID,value); } public Vector panes; public void scrollBarHidden(int paneID, int barID) { // This method is derived from interface FrameWithControlledPane // to do: code goes here } public void scrollBarShown(int paneID, int barID) { // This method is derived from interface FrameWithControlledPane // to do: code goes here /* if(frame==null) return; if(frame.communicationNode.sendEventFlag) frame.sendEvent("txtedit.sb.shown("+paneID+","+barID+")\n"); */ } public void scrollBarValueChanged(int paneID, int barID, int v) { // This method is derived from interface FrameWithControlledPane // to do: code goes here /* if(frame==null) return; if(frame.communicationNode.sendEventFlag) frame.sendEvent("txtedit.sb.value("+paneID+","+barID+","+v+")\n"); */ } public String tempText; public void saveText(String urlname) { // JTextArea mes=frame.messages; if(urlname==null) return; // int startTime=frame.getTimeNow(); int startTime=this.getTimeNow(); this.messageLabel.setText("saving "+urlname+"..."); // mes.append("saving "+urlname+"\n"); FileOutputStream fouts=null; try{ fouts= new FileOutputStream(new File(urlname));} catch(FileNotFoundException e){ messageLabel.setText("wrong directory:"+urlname+" ?"); } catch(IOException e){ messageLabel.setText("cannot access"+urlname+"."); } String outx=this.textArea.getText(); // byte[] buff=new byte[outx.length()]; byte[] buff=null; try{ // buff=outx.getBytes(frame.communicationNode.encodingCode); buff=outx.getBytes(communicationNode.getEncodingCode()); } catch(java.io.UnsupportedEncodingException e){ messageLabel.setText("exception:"+e); } // outx.getBytes(0,outx.length(),buff,0); try{ fouts.write(buff); fouts.flush(); } catch(IOException e) { messageLabel.setText("save:IOExceptin while flushing.");} try{ fouts.close(); } catch(IOException e) { messageLabel.setText("save:IOException while closing.");} messageLabel.setText("Saving done."); int endTime=// frame.getTimeNow(); this.getTimeNow(); int savingtime=endTime-startTime; double length=(double)(outx.length()); String savingSpeed=""; if(savingtime>0){ savingSpeed=""+10.0*length/savingtime; } else savingSpeed="(na)"; this.recordMessage(""+startTime+ ",\"savefig("+urlname+") \","+savingtime*0.1+ ","+length+","+savingSpeed); } public void loadText(String urlname) { if(!this.communicationNode.isReadableFromEachFile()) return; BufferedReader inputStream=null; URL url; // JTextArea mes=draw.messages; int startTime=this.getTimeNow(); if(urlname==null){ messageLabel.setText("URL Name is expected."); return; } messageLabel.setText("reading "+urlname+"..."); try{ url=new URL(urlname); } catch(MalformedURLException e) { messageLabel.setText("MalformedURLException."); return; } try{ InputStream is=url.openStream(); InputStreamReader isr=new InputStreamReader(is, this.communicationNode.getEncodingCode()); inputStream = new BufferedReader( isr); } catch(UnsupportedEncodingException e){ messageLabel.setText("encoding unsupported."); return; } catch(IOException e){ messageLabel.setText("url open error."); return; } this.textArea.setText(""); int tlength=0; String x=null; do{ x=null; try{ x=inputStream.readLine(); } catch(java.io.IOException e){ break;} if(x!=null){ tlength=tlength+x.length(); this.textArea.append(x+"\n"); } } while(x!=null); try{ inputStream.close(); } catch(IOException e) { messageLabel.setText("read:IOException while closing.");} messageLabel.setText("reading done.\n"); int endTime=this.getTimeNow(); int readingtime=endTime-startTime; String readingSpeed=""; if(readingtime>0){ readingSpeed=""+10.0*((double)(tlength))/readingtime; } else readingSpeed="(na)"; // npMain.recordMessage("-"+startTime+":readfig("+urlname+") done."); this.recordMessage(""+startTime+ ",\"readfig("+urlname+") \""+","+readingtime*0.1+ ","+tlength+","+readingSpeed); if(this.communicationNode.isSending()) this.sendAll(); } public void setDialogName(String s) { } public void setSubName(String n) { } public String getSubName() { return null; } public Vector getDialogs() { return null; } public void sendFileDialogMessage(String s) { if(frame==null) return; frame.sendEvent("txtedit.file."+s); } public void releaseMouseOnTheText(int id, int position, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); t.releaseMouse(position,x,y); } public void dragMouseOnTheText(int id, int position, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(id)); t.dragMouse(position,x,y); } public void mouseReleasedAtTextArea(int id, int position, int x, int y) { } public void mouseDraggedAtTextArea(int id, int position, int x, int y) { } public void clickMouseOnTheText(int i, int p, int x, int y) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(i)); t.clickMouse(p,x,y); } public void mouseClickedAtTextArea(int i, int p, int x, int y) { // This method is derived from interface FrameWithControlledTextAreas // to do: code goes here } public void typeKey(int i,int p, int key) { ControlledTextArea t=(ControlledTextArea)(texts.elementAt(i)); /* String s=t.getText(); String pre=""; String post=""; if(p>0) pre=s.substring(0,p); else pre=""; if(ptheText.length()) endPosition=theText.length(); this.communicationNode.tempText=theText.substring(startPosition,endPosition); newText=theText.substring(0,startPosition); newText=newText+ theText.substring(endPosition); this.textArea.setText(newText); return; } else if(i==6){ // pasteButton_actionPerformed_Interaction1(null); theText=this.textArea.getText(); startPosition=this.textArea.getSelectionStart(); if(startPosition<0) startPosition=0; endPosition=this.textArea.getSelectionEnd(); if(endPositiontheText.length()) endPosition=theText.length(); newText=theText.substring(0,startPosition); newText=newText+ this.communicationNode.tempText+ (this.textArea.getText()).substring(endPosition); this.textArea.setText(newText); return; } else if(i==7){ // searchButton_actionPerformed_Interaction(null); // this.dialogListener.whenCancelButtonPressed(this); // this.hide(); } else if(i==8){ // replaceButton_actionPerformed_Interaction(null); // this.dialogListener.whenCancelButtonPressed(this); // this.hide(); } else if(i==this.paste2Button.getID()){ if(this.isControlledByLocalUser()){ this.textArea.paste(); // this.sendEvent("txt.set(0,"+this.textArea.getStrConst()); this.textArea.ControlledTextArea_textPasted(); } } } public void focusButton(int i) { SelectedButton button=(SelectedButton)(buttons.elementAt(i)); // button.controlledButton_mouseEntered(null); button.focus(); } public void clickButton(int i) { SelectedButton b=(SelectedButton)(buttons.elementAt(i)); b.click(); this.mouseClickedAtButton(i); } public JFileDialog fileNameDialog; public Vector buttons; public void setListener(DialogListener l) { dialogListener=l; } DialogListener dialogListener; public void whenCancelButtonPressed(EditDialog d) { // This method is derived from interface DialogListener // to do: code goes here } public void whenActionButtonPressed(EditDialog d) { // This method is derived from interface DialogListener // to do: code goes here String dname=d.getDialogName(); if(dname.equals("input common file name:")){ String url=""; String fileName=d.getText(); // File commonDataPath=communicationNode.commonDataDir; // File thePath=new File(commonDataPath.getPath(),fileName); String separator=""+System.getProperty("file.separator"); if(separator.equals("\\")) // url="file:///"+thePath.getPath(); url="file:///"+fileName; else // url="file://"+thePath.getPath(); url="file://"+fileName; urlField.setText(fileName); this.loadText(url); return; } if(dname.equals("input user file name:")){ String url=""; String fileName=d.getText(); File userDataPath=communicationNode.userDataDir; if(!userDataPath.exists()) userDataPath.mkdir(); // File thePath=new File(userDataPath.getPath(),fileName); String separator=""+System.getProperty("file.separator"); if(separator.equals("\\")) // url="file:///"+thePath.getPath(); url="file:///"+fileName; else // url="file://"+thePath.getPath(); url="file://"+fileName; urlField.setText(fileName); this.loadText(url); return; } if(dname.equals("output user file name:")){ String url=""; String fileName=d.getText(); File userDataPath=communicationNode.userDataDir; if(!userDataPath.exists()) userDataPath.mkdir(); // File thePath=new File(userDataPath.getPath(),fileName); urlField.setText(fileName); // this.saveText(thePath.getPath()); this.saveText(fileName); return; } if(dname.equals("output common file name:")){ String fileName=d.getText(); if(!this.communicationNode.getControlMode().equals("teach")) return; // File commonDataPath=this.communicationNode.commonDataDir; // File thePath=new File(commonDataPath.getPath(),fileName); // System.out.println(thePath.getPath()); // this.saveText(thePath.getPath()); this.saveText(fileName); // editdispatch.save(thePath); return; } if(dname.equals("url:")){ String url=d.getText(); urlField.setText(url); this.loadText(url); return; } } public File getDefaultPath() { // This method is derived from interface DialogListener // to do: code goes here return dialogListener.getDefaultPath(); } public String getText() { // This method is derived from interface EditDialog // to do: code goes here return this.textArea.getText(); } public String getDialogName() { // This method is derived from interface EditDialog // to do: code goes here return "textEditor"; } public FileFrame fileFrame; public ControlledTextArea getTextArea(){ return this.textArea; } public TextEditFrame() { // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS setResizable(false); getContentPane().setLayout(null); getContentPane().setBackground(new java.awt.Color(204,204,204)); setSize(572,478); setVisible(false); okButton.setText("OK"); okButton.setActionCommand("OK"); getContentPane().add(okButton); okButton.setBackground(new java.awt.Color(204,204,204)); okButton.setForeground(java.awt.Color.black); okButton.setFont(new Font("Dialog", Font.BOLD, 12)); okButton.setBounds(108,72,84,24); JLabel1.setText("Distributed System Recorder/ Draw - Text Editor"); getContentPane().add(JLabel1); JLabel1.setBackground(new java.awt.Color(204,204,204)); JLabel1.setForeground(new java.awt.Color(102,102,153)); JLabel1.setFont(new Font("Dialog", Font.BOLD, 12)); JLabel1.setBounds(24,12,408,36); fileButton.setActionCommand("load"); getContentPane().add(fileButton); fileButton.setBackground(new java.awt.Color(204,204,204)); fileButton.setForeground(java.awt.Color.black); fileButton.setFont(new Font("Dialog", Font.BOLD, 12)); fileButton.setBounds(24,72,84,24); clearButton.setActionCommand("clear"); clearButton.setToolTipText("clear"); getContentPane().add(clearButton); clearButton.setBackground(new java.awt.Color(204,204,204)); clearButton.setForeground(java.awt.Color.black); clearButton.setFont(new Font("Dialog", Font.BOLD, 12)); clearButton.setBounds(192,72,84,24); textAreaPane.setOpaque(true); getContentPane().add(textAreaPane); textAreaPane.setBounds(24,132,528,264); textAreaPane.getViewport().add(textArea); textArea.setBackground(java.awt.Color.white); textArea.setForeground(java.awt.Color.black); textArea.setFont(new Font("SansSerif", Font.PLAIN, 12)); textArea.setBounds(0,0,524,260); cancelButton.setText("cancel/exit"); cancelButton.setActionCommand("cancel"); getContentPane().add(cancelButton); cancelButton.setBackground(new java.awt.Color(204,204,204)); cancelButton.setForeground(java.awt.Color.black); cancelButton.setFont(new Font("Dialog", Font.BOLD, 12)); cancelButton.setBounds(372,72,180,24); copyButton.setActionCommand("copy"); copyButton.setToolTipText("copy"); getContentPane().add(copyButton); copyButton.setBackground(new java.awt.Color(204,204,204)); copyButton.setForeground(java.awt.Color.black); copyButton.setFont(new Font("Dialog", Font.BOLD, 12)); copyButton.setBounds(24,96,84,24); cutButton.setActionCommand("cut"); cutButton.setToolTipText("cut"); getContentPane().add(cutButton); cutButton.setBackground(new java.awt.Color(204,204,204)); cutButton.setForeground(java.awt.Color.black); cutButton.setFont(new Font("Dialog", Font.BOLD, 12)); cutButton.setBounds(108,96,84,24); pasteButton.setActionCommand("paste"); pasteButton.setToolTipText("paste"); getContentPane().add(pasteButton); pasteButton.setBackground(new java.awt.Color(204,204,204)); pasteButton.setForeground(java.awt.Color.black); pasteButton.setFont(new Font("Dialog", Font.BOLD, 12)); pasteButton.setBounds(192,96,84,24); searchButton.setText("search"); searchButton.setActionCommand("search"); getContentPane().add(searchButton); searchButton.setBackground(new java.awt.Color(204,204,204)); searchButton.setForeground(java.awt.Color.black); searchButton.setFont(new Font("Dialog", Font.BOLD, 12)); searchButton.setBounds(372,96,96,24); replaceButton.setText("replace"); replaceButton.setActionCommand("replace"); getContentPane().add(replaceButton); replaceButton.setBackground(new java.awt.Color(204,204,204)); replaceButton.setForeground(java.awt.Color.black); replaceButton.setFont(new Font("Dialog", Font.BOLD, 12)); replaceButton.setBounds(468,96,84,24); getContentPane().add(urlField); urlField.setBounds(24,48,468,24); getContentPane().add(messageLabel); messageLabel.setBounds(24,396,456,24); try { copyIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/copy-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ copyIcon.move(24,396); try { cutIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/cut-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ cutIcon.move(48,396); try { pasteIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/paste-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ pasteIcon.move(72,396); paste2Button.setText("S"); paste2Button.setActionCommand("paste2"); paste2Button.setToolTipText("paste from system copy"); getContentPane().add(paste2Button); paste2Button.setBackground(new java.awt.Color(204,204,204)); paste2Button.setForeground(java.awt.Color.black); paste2Button.setFont(new Font("Dialog", Font.BOLD, 12)); paste2Button.setBounds(276,96,96,24); try { fileIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/file-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ fileIcon.move(96,396); try { exitIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/exit-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ exitIcon.move(60,384); try { clearIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/clear-icon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ clearIcon.move(144,396); try { editIcon.setImageLocation(symantec.itools.net.RelativeURL.getURL("images/editIcon.GIF")); } catch (java.net.MalformedURLException error) { } //$$ editIcon.move(168,396); fileButton.setIcon(fileIcon); cutButton.setIcon(cutIcon); cancelButton.setIcon(exitIcon); clearButton.setIcon(clearIcon); copyButton.setIcon(copyIcon); JLabel1.setIcon(editIcon); paste2Button.setIcon(pasteIcon); pasteButton.setIcon(pasteIcon); //}} //{{INIT_MENUS //}} //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); fileButton.addActionListener(lSymAction); okButton.addActionListener(lSymAction); clearButton.addActionListener(lSymAction); cancelButton.addActionListener(lSymAction); copyButton.addActionListener(lSymAction); cutButton.addActionListener(lSymAction); pasteButton.addActionListener(lSymAction); searchButton.addActionListener(lSymAction); replaceButton.addActionListener(lSymAction); SymFocus aSymFocus = new SymFocus(); this.addFocusListener(aSymFocus); SymWindow aSymWindow = new SymWindow(); this.addWindowListener(aSymWindow); paste2Button.addActionListener(lSymAction); //}} fileFrame=new FileFrame("Text File I/O"); buttons=new Vector(); buttons.addElement(this.fileButton); buttons.addElement(this.okButton); buttons.addElement(this.clearButton); buttons.addElement(this.cancelButton); buttons.addElement(this.copyButton); buttons.addElement(this.cutButton); buttons.addElement(this.pasteButton); buttons.addElement(this.searchButton); buttons.addElement(this.replaceButton); buttons.addElement(this.paste2Button); int numberOfButtons=buttons.size(); for(int i=0;i