PageRenderTime 24ms CodeModel.GetById 19ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/commands/workspaceEditor.bsh

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