PageRenderTime 69ms CodeModel.GetById 66ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Unknown | 124 lines | 109 code | 15 blank | 0 comment | 0 complexity | 62a4fb4c6ff1749e2cf5241e3bebe7d2 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	textarea.font=new Font("Monospaced", 0, 12);
 29
 30	// probably should overload desktop makeInternalFrame
 31	this.frame = 
 32		new JInternalFrame("Editor for: "+name, true, true, true, true );
 33	frame.frameIcon=bsh.system.icons.script;
 34	frame.getContentPane().add( new JScrollPane(textarea), "Center");
 35	frame.setVisible( true );
 36
 37	open() 
 38	{
 39		this.chooser = new JFileChooser();
 40		chooser.setCurrentDirectory( pathToFile(bsh.cwd) );
 41		this.returnVal = chooser.showOpenDialog( bsh.system.desktop.pane );
 42		if (returnVal == JFileChooser.APPROVE_OPTION) {
 43			this.file = chooser.getSelectedFile();
 44			this.reader=new FileReader( file );
 45			this.ca=new char [file.length()];
 46			reader.read(ca);
 47			textarea.setText( new String(ca) );
 48		}
 49	}
 50
 51	save() 
 52	{
 53		this.chooser = new JFileChooser();
 54		chooser.setCurrentDirectory( pathToFile(bsh.cwd) );
 55		this.returnVal = chooser.showSaveDialog( bsh.system.desktop.pane );
 56		if (returnVal == JFileChooser.APPROVE_OPTION) {
 57			this.file = chooser.getSelectedFile();
 58			this.writer=new FileWriter( file );
 59			writer.write( textarea.getText().toCharArray() );
 60			writer.close();
 61		}
 62	}
 63
 64	run() 
 65	{
 66		this.interpreter = makeWorkspace( "Run Output: " +name);
 67		// should make this new namespace... look at run() command
 68    	interpreter.eval( textarea.getText() );
 69		print("done run...");
 70	}
 71
 72	actionPerformed(e) 
 73	{
 74		this.com = e.getActionCommand();
 75		if ( com.equals("Close") )
 76			frame.setClosed(true);
 77		else if ( com.equals("New") )
 78			textarea.setText("");
 79		else if ( com.equals("Open") )
 80			open();
 81		else if ( com.equals("Save") )
 82			save();
 83		else if ( com.equals("Eval in Workspace") )
 84			// eval in parent global namespace
 85    		parent.eval( textarea.getText() );
 86		else if ( com.equals("Run in new Workspace") )
 87			run();
 88	}
 89
 90	this.menubar = new JMenuBar();
 91	this.menu = new JMenu("File");
 92	this.mi = new JMenuItem("New");
 93	mi.addActionListener(this);
 94	menu.add(mi);
 95	mi = new JMenuItem("Open");
 96	mi.addActionListener(this);
 97	menu.add(mi);
 98	mi = new JMenuItem("Save");
 99	mi.addActionListener(this);
100	menu.add(mi);
101	mi = new JMenuItem("Close");
102	mi.addActionListener(this);
103	menu.add(mi);
104	menubar.add(menu);
105
106	menu = new JMenu("Evaluate");
107	mi = new JMenuItem("Eval in Workspace");
108	mi.addActionListener(this);
109	menu.add(mi);
110	mi = new JMenuItem("Run in new Workspace");
111	mi.addActionListener(this);
112	menu.add(mi);
113	menubar.add(menu);
114
115	menu = fontMenu( textarea );
116	menubar.add(menu);
117
118	frame.setMenuBar( menubar );
119
120	frame.pack();
121	bsh.system.desktop.addInternalFrame( frame );
122	frame.selected=true;
123	return frame;
124}