PageRenderTime 95ms CodeModel.GetById 91ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/commands/workspaceEditor.bsh

#
Unknown | 123 lines | 108 code | 15 blank | 0 comment | 0 complexity | 0cf4dd259824c85d623ef6e97f3d551d MD5 | raw file
  1
  2/**
  3 * Make a new workspaceEditor associated with a workspace and place it on the
  4 * desktop.
  5 *
  6 * @method workspaceEditor( bsh.Interpreter parent, String name )
  7 *
  8 * @author Pat Niemeyer
  9 * @author Daniel Leuck
 10 */
 11 
 12import java.awt.Insets;
 13import bsh.Interpreter;
 14
 15workspaceEditor(Interpreter parent, String name ) 
 16{
 17	
 18	if ( bsh.system.desktop == void ) {
 19		print("This only works with the bsh desktop...");
 20		return;
 21	}
 22
 23	this.textarea = new JTextArea(15,40);
 24	textarea.setLineWrap(true);
 25	textarea.setWrapStyleWord(true);
 26	textarea.setTabSize(4);
 27	textarea.setMargin( new Insets(5,5,5,5) );
 28
 29	// probably should overload desktop makeInternalFrame
 30	this.frame = 
 31		new JInternalFrame("Editor for: "+name, true, true, true, true );
 32	frame.frameIcon=bsh.system.icons.script;
 33	frame.getContentPane().add( new JScrollPane(textarea), "Center");
 34	frame.setVisible( true );
 35
 36	open() 
 37	{
 38		this.chooser = new JFileChooser();
 39		chooser.setCurrentDirectory( pathToFile(bsh.cwd) );
 40		this.returnVal = chooser.showOpenDialog( bsh.system.desktop.pane );
 41		if (returnVal == JFileChooser.APPROVE_OPTION) {
 42			this.file = chooser.getSelectedFile();
 43			this.reader=new FileReader( file );
 44			this.ca=new char [file.length()];
 45			reader.read(ca);
 46			textarea.setText( new String(ca) );
 47		}
 48	}
 49
 50	save() 
 51	{
 52		this.chooser = new JFileChooser();
 53		chooser.setCurrentDirectory( pathToFile(bsh.cwd) );
 54		this.returnVal = chooser.showSaveDialog( bsh.system.desktop.pane );
 55		if (returnVal == JFileChooser.APPROVE_OPTION) {
 56			this.file = chooser.getSelectedFile();
 57			this.writer=new FileWriter( file );
 58			writer.write( textarea.getText().toCharArray() );
 59			writer.close();
 60		}
 61	}
 62
 63	run() 
 64	{
 65		this.interpreter = makeWorkspace( "Run Output: " +name);
 66		// should make this new namespace... look at run() command
 67    	interpreter.eval( textarea.getText() );
 68		print("done run...");
 69	}
 70
 71	actionPerformed(e) 
 72	{
 73		this.com = e.getActionCommand();
 74		if ( com.equals("Close") )
 75			frame.dispose();
 76		else if ( com.equals("New") )
 77			textarea.setText("");
 78		else if ( com.equals("Open") )
 79			open();
 80		else if ( com.equals("Save") )
 81			save();
 82		else if ( com.equals("Eval in Workspace") )
 83			// eval in parent global namespace
 84    		parent.eval( textarea.getText() );
 85		else if ( com.equals("Run in new Workspace") )
 86			run();
 87	}
 88
 89	this.menubar = new JMenuBar();
 90	this.menu = new JMenu("File");
 91	this.mi = new JMenuItem("New");
 92	mi.addActionListener(this);
 93	menu.add(mi);
 94	mi = new JMenuItem("Open");
 95	mi.addActionListener(this);
 96	menu.add(mi);
 97	mi = new JMenuItem("Save");
 98	mi.addActionListener(this);
 99	menu.add(mi);
100	mi = new JMenuItem("Close");
101	mi.addActionListener(this);
102	menu.add(mi);
103	menubar.add(menu);
104
105	menu = new JMenu("Evaluate");
106	mi = new JMenuItem("Eval in Workspace");
107	mi.addActionListener(this);
108	menu.add(mi);
109	mi = new JMenuItem("Run in new Workspace");
110	mi.addActionListener(this);
111	menu.add(mi);
112	menubar.add(menu);
113
114	menu = fontMenu( textarea );
115	menubar.add(menu);
116
117	frame.setMenuBar( menubar );
118
119	frame.pack();
120	bsh.system.desktop.addInternalFrame( frame );
121	frame.selected=true;
122	return frame;
123}