PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/commands/workspaceEditor.bsh

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